1 //===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the X86 specific subclass of TargetSubtargetInfo. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "X86Subtarget.h" 15 #include "X86InstrInfo.h" 16 #include "X86TargetMachine.h" 17 #include "llvm/IR/Attributes.h" 18 #include "llvm/IR/Function.h" 19 #include "llvm/IR/GlobalValue.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/Host.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include "llvm/Target/TargetMachine.h" 26 #include "llvm/Target/TargetOptions.h" 27 28 #if defined(_MSC_VER) 29 #include <intrin.h> 30 #endif 31 32 using namespace llvm; 33 34 #define DEBUG_TYPE "subtarget" 35 36 #define GET_SUBTARGETINFO_TARGET_DESC 37 #define GET_SUBTARGETINFO_CTOR 38 #include "X86GenSubtargetInfo.inc" 39 40 // Temporary option to control early if-conversion for x86 while adding machine 41 // models. 42 static cl::opt<bool> 43 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden, 44 cl::desc("Enable early if-conversion on X86")); 45 46 47 /// Classify a blockaddress reference for the current subtarget according to how 48 /// we should reference it in a non-pcrel context. 49 unsigned char X86Subtarget::ClassifyBlockAddressReference() const { 50 if (isPICStyleGOT()) // 32-bit ELF targets. 51 return X86II::MO_GOTOFF; 52 53 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode. 54 return X86II::MO_PIC_BASE_OFFSET; 55 56 // Direct static reference to label. 57 return X86II::MO_NO_FLAG; 58 } 59 60 /// Classify a global variable reference for the current subtarget according to 61 /// how we should reference it in a non-pcrel context. 62 unsigned char X86Subtarget:: 63 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const { 64 // DLLImport only exists on windows, it is implemented as a load from a 65 // DLLIMPORT stub. 66 if (GV->hasDLLImportStorageClass()) 67 return X86II::MO_DLLIMPORT; 68 69 bool isDef = GV->isStrongDefinitionForLinker(); 70 71 // X86-64 in PIC mode. 72 if (isPICStyleRIPRel()) { 73 // Large model never uses stubs. 74 if (TM.getCodeModel() == CodeModel::Large) 75 return X86II::MO_NO_FLAG; 76 77 if (isTargetDarwin()) { 78 // If symbol visibility is hidden, the extra load is not needed if 79 // target is x86-64 or the symbol is definitely defined in the current 80 // translation unit. 81 if (GV->hasDefaultVisibility() && !isDef) 82 return X86II::MO_GOTPCREL; 83 } else if (!isTargetWin64()) { 84 assert(isTargetELF() && "Unknown rip-relative target"); 85 86 // Extra load is needed for all externally visible globals except with 87 // PIE as the definition of the global in an executable is not 88 // overridden. 89 90 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility() && 91 !isGlobalDefinedInPIE(GV, TM)) 92 return X86II::MO_GOTPCREL; 93 } 94 95 return X86II::MO_NO_FLAG; 96 } 97 98 if (isPICStyleGOT()) { // 32-bit ELF targets. 99 // Extra load is needed for all externally visible globals except with 100 // PIE as the definition of the global in an executable is not overridden. 101 102 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility() || 103 isGlobalDefinedInPIE(GV, TM)) 104 return X86II::MO_GOTOFF; 105 return X86II::MO_GOT; 106 } 107 108 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode. 109 // Determine whether we have a stub reference and/or whether the reference 110 // is relative to the PIC base or not. 111 112 // If this is a strong reference to a definition, it is definitely not 113 // through a stub. 114 if (isDef) 115 return X86II::MO_PIC_BASE_OFFSET; 116 117 // Unless we have a symbol with hidden visibility, we have to go through a 118 // normal $non_lazy_ptr stub because this symbol might be resolved late. 119 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference. 120 return X86II::MO_DARWIN_NONLAZY_PIC_BASE; 121 122 // If symbol visibility is hidden, we have a stub for common symbol 123 // references and external declarations. 124 if (GV->isDeclarationForLinker() || GV->hasCommonLinkage()) { 125 // Hidden $non_lazy_ptr reference. 126 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE; 127 } 128 129 // Otherwise, no stub. 130 return X86II::MO_PIC_BASE_OFFSET; 131 } 132 133 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode. 134 // Determine whether we have a stub reference. 135 136 // If this is a strong reference to a definition, it is definitely not 137 // through a stub. 138 if (isDef) 139 return X86II::MO_NO_FLAG; 140 141 // Unless we have a symbol with hidden visibility, we have to go through a 142 // normal $non_lazy_ptr stub because this symbol might be resolved late. 143 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference. 144 return X86II::MO_DARWIN_NONLAZY; 145 146 // Otherwise, no stub. 147 return X86II::MO_NO_FLAG; 148 } 149 150 // Direct static reference to global. 151 return X86II::MO_NO_FLAG; 152 } 153 154 unsigned char X86Subtarget::classifyGlobalFunctionReference( 155 const GlobalValue *GV, const TargetMachine &TM) const { 156 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to 157 // external symbols most go through the PLT in PIC mode. If the symbol 158 // has hidden or protected visibility, or if it is static or local, then 159 // we don't need to use the PLT - we can directly call it. 160 // In PIE mode, calls to global functions don't need to go through PLT 161 if (isTargetELF() && TM.getRelocationModel() == Reloc::PIC_ && 162 (!TM.Options.PositionIndependentExecutable || 163 GV->isDeclarationForLinker()) && 164 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) { 165 return X86II::MO_PLT; 166 } else if (isPICStyleStubAny() && !GV->isStrongDefinitionForLinker() && 167 (!getTargetTriple().isMacOSX() || 168 getTargetTriple().isMacOSXVersionLT(10, 5))) { 169 // PC-relative references to external symbols should go through $stub, 170 // unless we're building with the leopard linker or later, which 171 // automatically synthesizes these stubs. 172 return X86II::MO_DARWIN_STUB; 173 } else if (isPICStyleRIPRel() && isa<Function>(GV) && 174 cast<Function>(GV)->hasFnAttribute(Attribute::NonLazyBind)) { 175 // If the function is marked as non-lazy, generate an indirect call 176 // which loads from the GOT directly. This avoids runtime overhead 177 // at the cost of eager binding (and one extra byte of encoding). 178 return X86II::MO_GOTPCREL; 179 } 180 181 return X86II::MO_NO_FLAG; 182 } 183 184 /// This function returns the name of a function which has an interface like 185 /// the non-standard bzero function, if such a function exists on the 186 /// current subtarget and it is considered preferable over memset with zero 187 /// passed as the second argument. Otherwise it returns null. 188 const char *X86Subtarget::getBZeroEntry() const { 189 // Darwin 10 has a __bzero entry point for this purpose. 190 if (getTargetTriple().isMacOSX() && 191 !getTargetTriple().isMacOSXVersionLT(10, 6)) 192 return "__bzero"; 193 194 return nullptr; 195 } 196 197 bool X86Subtarget::hasSinCos() const { 198 return getTargetTriple().isMacOSX() && 199 !getTargetTriple().isMacOSXVersionLT(10, 9) && 200 is64Bit(); 201 } 202 203 /// Return true if the subtarget allows calls to immediate address. 204 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const { 205 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32 206 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does, 207 // the following check for Win32 should be removed. 208 if (In64BitMode || isTargetWin32()) 209 return false; 210 return isTargetELF() || TM.getRelocationModel() == Reloc::Static; 211 } 212 213 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { 214 std::string CPUName = CPU; 215 if (CPUName.empty()) 216 CPUName = "generic"; 217 218 // Make sure 64-bit features are available in 64-bit mode. (But make sure 219 // SSE2 can be turned off explicitly.) 220 std::string FullFS = FS; 221 if (In64BitMode) { 222 if (!FullFS.empty()) 223 FullFS = "+64bit,+sse2," + FullFS; 224 else 225 FullFS = "+64bit,+sse2"; 226 } 227 228 // LAHF/SAHF are always supported in non-64-bit mode. 229 if (!In64BitMode) { 230 if (!FullFS.empty()) 231 FullFS = "+sahf," + FullFS; 232 else 233 FullFS = "+sahf"; 234 } 235 236 237 // Parse features string and set the CPU. 238 ParseSubtargetFeatures(CPUName, FullFS); 239 240 // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of 241 // 16-bytes and under that are reasonably fast. These features were 242 // introduced with Intel's Nehalem/Silvermont and AMD's Family10h 243 // micro-architectures respectively. 244 if (hasSSE42() || hasSSE4A()) 245 IsUAMem16Slow = false; 246 247 InstrItins = getInstrItineraryForCPU(CPUName); 248 249 // It's important to keep the MCSubtargetInfo feature bits in sync with 250 // target data structure which is shared with MC code emitter, etc. 251 if (In64BitMode) 252 ToggleFeature(X86::Mode64Bit); 253 else if (In32BitMode) 254 ToggleFeature(X86::Mode32Bit); 255 else if (In16BitMode) 256 ToggleFeature(X86::Mode16Bit); 257 else 258 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!"); 259 260 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel 261 << ", 3DNowLevel " << X863DNowLevel 262 << ", 64bit " << HasX86_64 << "\n"); 263 assert((!In64BitMode || HasX86_64) && 264 "64-bit code requested on a subtarget that doesn't support it!"); 265 266 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both 267 // 32 and 64 bit) and for all 64-bit targets. 268 if (StackAlignOverride) 269 stackAlignment = StackAlignOverride; 270 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() || 271 In64BitMode) 272 stackAlignment = 16; 273 } 274 275 void X86Subtarget::initializeEnvironment() { 276 X86SSELevel = NoSSE; 277 X863DNowLevel = NoThreeDNow; 278 HasX87 = false; 279 HasCMov = false; 280 HasX86_64 = false; 281 HasPOPCNT = false; 282 HasSSE4A = false; 283 HasAES = false; 284 HasFXSR = false; 285 HasXSAVE = false; 286 HasXSAVEOPT = false; 287 HasXSAVEC = false; 288 HasXSAVES = false; 289 HasPCLMUL = false; 290 HasFMA = false; 291 HasFMA4 = false; 292 HasXOP = false; 293 HasTBM = false; 294 HasMOVBE = false; 295 HasRDRAND = false; 296 HasF16C = false; 297 HasFSGSBase = false; 298 HasLZCNT = false; 299 HasBMI = false; 300 HasBMI2 = false; 301 HasVBMI = false; 302 HasIFMA = false; 303 HasRTM = false; 304 HasHLE = false; 305 HasERI = false; 306 HasCDI = false; 307 HasPFI = false; 308 HasDQI = false; 309 HasBWI = false; 310 HasVLX = false; 311 HasADX = false; 312 HasPKU = false; 313 HasSHA = false; 314 HasPRFCHW = false; 315 HasRDSEED = false; 316 HasLAHFSAHF = false; 317 HasMPX = false; 318 IsBTMemSlow = false; 319 IsSHLDSlow = false; 320 IsUAMem16Slow = false; 321 IsUAMem32Slow = false; 322 HasSSEUnalignedMem = false; 323 HasCmpxchg16b = false; 324 UseLeaForSP = false; 325 HasFastPartialYMMWrite = false; 326 HasSlowDivide32 = false; 327 HasSlowDivide64 = false; 328 PadShortFunctions = false; 329 CallRegIndirect = false; 330 LEAUsesAG = false; 331 SlowLEA = false; 332 SlowIncDec = false; 333 stackAlignment = 4; 334 // FIXME: this is a known good value for Yonah. How about others? 335 MaxInlineSizeThreshold = 128; 336 UseSoftFloat = false; 337 } 338 339 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU, 340 StringRef FS) { 341 initializeEnvironment(); 342 initSubtargetFeatures(CPU, FS); 343 return *this; 344 } 345 346 X86Subtarget::X86Subtarget(const Triple &TT, const std::string &CPU, 347 const std::string &FS, const X86TargetMachine &TM, 348 unsigned StackAlignOverride) 349 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others), 350 PICStyle(PICStyles::None), TargetTriple(TT), 351 StackAlignOverride(StackAlignOverride), 352 In64BitMode(TargetTriple.getArch() == Triple::x86_64), 353 In32BitMode(TargetTriple.getArch() == Triple::x86 && 354 TargetTriple.getEnvironment() != Triple::CODE16), 355 In16BitMode(TargetTriple.getArch() == Triple::x86 && 356 TargetTriple.getEnvironment() == Triple::CODE16), 357 TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)), 358 TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) { 359 // Determine the PICStyle based on the target selected. 360 if (TM.getRelocationModel() == Reloc::Static) { 361 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None. 362 setPICStyle(PICStyles::None); 363 } else if (is64Bit()) { 364 // PIC in 64 bit mode is always rip-rel. 365 setPICStyle(PICStyles::RIPRel); 366 } else if (isTargetCOFF()) { 367 setPICStyle(PICStyles::None); 368 } else if (isTargetDarwin()) { 369 if (TM.getRelocationModel() == Reloc::PIC_) 370 setPICStyle(PICStyles::StubPIC); 371 else { 372 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC); 373 setPICStyle(PICStyles::StubDynamicNoPIC); 374 } 375 } else if (isTargetELF()) { 376 setPICStyle(PICStyles::GOT); 377 } 378 } 379 380 bool X86Subtarget::enableEarlyIfConversion() const { 381 return hasCMov() && X86EarlyIfConv; 382 } 383 384