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 !isGlobalDefinedInPIE(GV, TM) && 163 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) { 164 return X86II::MO_PLT; 165 } else if (isPICStyleStubAny() && !GV->isStrongDefinitionForLinker() && 166 (!getTargetTriple().isMacOSX() || 167 getTargetTriple().isMacOSXVersionLT(10, 5))) { 168 // PC-relative references to external symbols should go through $stub, 169 // unless we're building with the leopard linker or later, which 170 // automatically synthesizes these stubs. 171 return X86II::MO_DARWIN_STUB; 172 } else if (isPICStyleRIPRel() && isa<Function>(GV) && 173 cast<Function>(GV)->hasFnAttribute(Attribute::NonLazyBind)) { 174 // If the function is marked as non-lazy, generate an indirect call 175 // which loads from the GOT directly. This avoids runtime overhead 176 // at the cost of eager binding (and one extra byte of encoding). 177 return X86II::MO_GOTPCREL; 178 } 179 180 return X86II::MO_NO_FLAG; 181 } 182 183 /// This function returns the name of a function which has an interface like 184 /// the non-standard bzero function, if such a function exists on the 185 /// current subtarget and it is considered preferable over memset with zero 186 /// passed as the second argument. Otherwise it returns null. 187 const char *X86Subtarget::getBZeroEntry() const { 188 // Darwin 10 has a __bzero entry point for this purpose. 189 if (getTargetTriple().isMacOSX() && 190 !getTargetTriple().isMacOSXVersionLT(10, 6)) 191 return "__bzero"; 192 193 return nullptr; 194 } 195 196 bool X86Subtarget::hasSinCos() const { 197 return getTargetTriple().isMacOSX() && 198 !getTargetTriple().isMacOSXVersionLT(10, 9) && 199 is64Bit(); 200 } 201 202 /// Return true if the subtarget allows calls to immediate address. 203 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const { 204 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32 205 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does, 206 // the following check for Win32 should be removed. 207 if (In64BitMode || isTargetWin32()) 208 return false; 209 return isTargetELF() || TM.getRelocationModel() == Reloc::Static; 210 } 211 212 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { 213 std::string CPUName = CPU; 214 if (CPUName.empty()) 215 CPUName = "generic"; 216 217 // Make sure 64-bit features are available in 64-bit mode. (But make sure 218 // SSE2 can be turned off explicitly.) 219 std::string FullFS = FS; 220 if (In64BitMode) { 221 if (!FullFS.empty()) 222 FullFS = "+64bit,+sse2," + FullFS; 223 else 224 FullFS = "+64bit,+sse2"; 225 } 226 227 // LAHF/SAHF are always supported in non-64-bit mode. 228 if (!In64BitMode) { 229 if (!FullFS.empty()) 230 FullFS = "+sahf," + FullFS; 231 else 232 FullFS = "+sahf"; 233 } 234 235 236 // Parse features string and set the CPU. 237 ParseSubtargetFeatures(CPUName, FullFS); 238 239 // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of 240 // 16-bytes and under that are reasonably fast. These features were 241 // introduced with Intel's Nehalem/Silvermont and AMD's Family10h 242 // micro-architectures respectively. 243 if (hasSSE42() || hasSSE4A()) 244 IsUAMem16Slow = false; 245 246 InstrItins = getInstrItineraryForCPU(CPUName); 247 248 // It's important to keep the MCSubtargetInfo feature bits in sync with 249 // target data structure which is shared with MC code emitter, etc. 250 if (In64BitMode) 251 ToggleFeature(X86::Mode64Bit); 252 else if (In32BitMode) 253 ToggleFeature(X86::Mode32Bit); 254 else if (In16BitMode) 255 ToggleFeature(X86::Mode16Bit); 256 else 257 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!"); 258 259 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel 260 << ", 3DNowLevel " << X863DNowLevel 261 << ", 64bit " << HasX86_64 << "\n"); 262 assert((!In64BitMode || HasX86_64) && 263 "64-bit code requested on a subtarget that doesn't support it!"); 264 265 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both 266 // 32 and 64 bit) and for all 64-bit targets. 267 if (StackAlignOverride) 268 stackAlignment = StackAlignOverride; 269 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() || 270 In64BitMode) 271 stackAlignment = 16; 272 } 273 274 void X86Subtarget::initializeEnvironment() { 275 X86SSELevel = NoSSE; 276 X863DNowLevel = NoThreeDNow; 277 HasX87 = false; 278 HasCMov = false; 279 HasX86_64 = false; 280 HasPOPCNT = false; 281 HasSSE4A = false; 282 HasAES = false; 283 HasFXSR = false; 284 HasXSAVE = false; 285 HasXSAVEOPT = false; 286 HasXSAVEC = false; 287 HasXSAVES = false; 288 HasPCLMUL = false; 289 HasFMA = false; 290 HasFMA4 = false; 291 HasXOP = false; 292 HasTBM = false; 293 HasMOVBE = false; 294 HasRDRAND = false; 295 HasF16C = false; 296 HasFSGSBase = false; 297 HasLZCNT = false; 298 HasBMI = false; 299 HasBMI2 = false; 300 HasVBMI = false; 301 HasIFMA = false; 302 HasRTM = false; 303 HasHLE = false; 304 HasERI = false; 305 HasCDI = false; 306 HasPFI = false; 307 HasDQI = false; 308 HasBWI = false; 309 HasVLX = false; 310 HasADX = false; 311 HasPKU = false; 312 HasSHA = false; 313 HasPRFCHW = false; 314 HasRDSEED = false; 315 HasLAHFSAHF = false; 316 HasMPX = false; 317 IsBTMemSlow = false; 318 IsSHLDSlow = false; 319 IsUAMem16Slow = false; 320 IsUAMem32Slow = false; 321 HasSSEUnalignedMem = false; 322 HasCmpxchg16b = false; 323 UseLeaForSP = false; 324 HasFastPartialYMMWrite = false; 325 HasSlowDivide32 = false; 326 HasSlowDivide64 = false; 327 PadShortFunctions = false; 328 CallRegIndirect = false; 329 LEAUsesAG = false; 330 SlowLEA = false; 331 SlowIncDec = false; 332 stackAlignment = 4; 333 // FIXME: this is a known good value for Yonah. How about others? 334 MaxInlineSizeThreshold = 128; 335 UseSoftFloat = false; 336 } 337 338 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU, 339 StringRef FS) { 340 initializeEnvironment(); 341 initSubtargetFeatures(CPU, FS); 342 return *this; 343 } 344 345 X86Subtarget::X86Subtarget(const Triple &TT, const std::string &CPU, 346 const std::string &FS, const X86TargetMachine &TM, 347 unsigned StackAlignOverride) 348 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others), 349 PICStyle(PICStyles::None), TargetTriple(TT), 350 StackAlignOverride(StackAlignOverride), 351 In64BitMode(TargetTriple.getArch() == Triple::x86_64), 352 In32BitMode(TargetTriple.getArch() == Triple::x86 && 353 TargetTriple.getEnvironment() != Triple::CODE16), 354 In16BitMode(TargetTriple.getArch() == Triple::x86 && 355 TargetTriple.getEnvironment() == Triple::CODE16), 356 TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)), 357 TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) { 358 // Determine the PICStyle based on the target selected. 359 if (TM.getRelocationModel() == Reloc::Static) { 360 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None. 361 setPICStyle(PICStyles::None); 362 } else if (is64Bit()) { 363 // PIC in 64 bit mode is always rip-rel. 364 setPICStyle(PICStyles::RIPRel); 365 } else if (isTargetCOFF()) { 366 setPICStyle(PICStyles::None); 367 } else if (isTargetDarwin()) { 368 if (TM.getRelocationModel() == Reloc::PIC_) 369 setPICStyle(PICStyles::StubPIC); 370 else { 371 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC); 372 setPICStyle(PICStyles::StubDynamicNoPIC); 373 } 374 } else if (isTargetELF()) { 375 setPICStyle(PICStyles::GOT); 376 } 377 } 378 379 bool X86Subtarget::enableEarlyIfConversion() const { 380 return hasCMov() && X86EarlyIfConv; 381 } 382 383