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 return classifyLocalReference(nullptr); 51 } 52 53 /// Classify a global variable reference for the current subtarget according to 54 /// how we should reference it in a non-pcrel context. 55 unsigned char 56 X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const { 57 return classifyGlobalReference(GV, *GV->getParent()); 58 } 59 60 unsigned char 61 X86Subtarget::classifyLocalReference(const GlobalValue *GV) const { 62 // 64 bits can use %rip addressing for anything local. 63 if (is64Bit()) 64 return X86II::MO_NO_FLAG; 65 66 // If this is for a position dependent executable, the static linker can 67 // figure it out. 68 if (!isPositionIndependent()) 69 return X86II::MO_NO_FLAG; 70 71 // The COFF dynamic linker just patches the executable sections. 72 if (isTargetCOFF()) 73 return X86II::MO_NO_FLAG; 74 75 if (isTargetDarwin()) { 76 // 32 bit macho has no relocation for a-b if a is undefined, even if 77 // b is in the section that is being relocated. 78 // This means we have to use o load even for GVs that are known to be 79 // local to the dso. 80 if (GV && (GV->isDeclarationForLinker() || GV->hasCommonLinkage())) 81 return X86II::MO_DARWIN_NONLAZY_PIC_BASE; 82 83 return X86II::MO_PIC_BASE_OFFSET; 84 } 85 86 return X86II::MO_GOTOFF; 87 } 88 89 unsigned char X86Subtarget::classifyGlobalReference(const GlobalValue *GV, 90 const Module &M) const { 91 // Large model never uses stubs. 92 if (TM.getCodeModel() == CodeModel::Large) 93 return X86II::MO_NO_FLAG; 94 95 // Absolute symbols can be referenced directly. 96 if (GV && GV->isAbsoluteSymbolRef()) 97 return X86II::MO_NO_FLAG; 98 99 if (TM.shouldAssumeDSOLocal(M, GV)) 100 return classifyLocalReference(GV); 101 102 if (isTargetCOFF()) 103 return X86II::MO_DLLIMPORT; 104 105 if (is64Bit()) 106 return X86II::MO_GOTPCREL; 107 108 if (isTargetDarwin()) { 109 if (!isPositionIndependent()) 110 return X86II::MO_DARWIN_NONLAZY; 111 return X86II::MO_DARWIN_NONLAZY_PIC_BASE; 112 } 113 114 return X86II::MO_GOT; 115 } 116 117 unsigned char 118 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const { 119 return classifyGlobalFunctionReference(GV, *GV->getParent()); 120 } 121 122 unsigned char 123 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV, 124 const Module &M) const { 125 if (TM.shouldAssumeDSOLocal(M, GV)) 126 return X86II::MO_NO_FLAG; 127 128 assert(!isTargetCOFF()); 129 130 if (isTargetELF()) 131 return X86II::MO_PLT; 132 133 if (is64Bit()) { 134 auto *F = dyn_cast_or_null<Function>(GV); 135 if (F && F->hasFnAttribute(Attribute::NonLazyBind)) 136 // If the function is marked as non-lazy, generate an indirect call 137 // which loads from the GOT directly. This avoids runtime overhead 138 // at the cost of eager binding (and one extra byte of encoding). 139 return X86II::MO_GOTPCREL; 140 return X86II::MO_NO_FLAG; 141 } 142 143 return X86II::MO_NO_FLAG; 144 } 145 146 /// This function returns the name of a function which has an interface like 147 /// the non-standard bzero function, if such a function exists on the 148 /// current subtarget and it is considered preferable over memset with zero 149 /// passed as the second argument. Otherwise it returns null. 150 const char *X86Subtarget::getBZeroEntry() const { 151 // Darwin 10 has a __bzero entry point for this purpose. 152 if (getTargetTriple().isMacOSX() && 153 !getTargetTriple().isMacOSXVersionLT(10, 6)) 154 return "__bzero"; 155 156 return nullptr; 157 } 158 159 bool X86Subtarget::hasSinCos() const { 160 return getTargetTriple().isMacOSX() && 161 !getTargetTriple().isMacOSXVersionLT(10, 9) && 162 is64Bit(); 163 } 164 165 /// Return true if the subtarget allows calls to immediate address. 166 bool X86Subtarget::isLegalToCallImmediateAddr() const { 167 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32 168 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does, 169 // the following check for Win32 should be removed. 170 if (In64BitMode || isTargetWin32()) 171 return false; 172 return isTargetELF() || TM.getRelocationModel() == Reloc::Static; 173 } 174 175 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { 176 std::string CPUName = CPU; 177 if (CPUName.empty()) 178 CPUName = "generic"; 179 180 // Make sure 64-bit features are available in 64-bit mode. (But make sure 181 // SSE2 can be turned off explicitly.) 182 std::string FullFS = FS; 183 if (In64BitMode) { 184 if (!FullFS.empty()) 185 FullFS = "+64bit,+sse2," + FullFS; 186 else 187 FullFS = "+64bit,+sse2"; 188 } 189 190 // LAHF/SAHF are always supported in non-64-bit mode. 191 if (!In64BitMode) { 192 if (!FullFS.empty()) 193 FullFS = "+sahf," + FullFS; 194 else 195 FullFS = "+sahf"; 196 } 197 198 199 // Parse features string and set the CPU. 200 ParseSubtargetFeatures(CPUName, FullFS); 201 202 // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of 203 // 16-bytes and under that are reasonably fast. These features were 204 // introduced with Intel's Nehalem/Silvermont and AMD's Family10h 205 // micro-architectures respectively. 206 if (hasSSE42() || hasSSE4A()) 207 IsUAMem16Slow = false; 208 209 InstrItins = getInstrItineraryForCPU(CPUName); 210 211 // It's important to keep the MCSubtargetInfo feature bits in sync with 212 // target data structure which is shared with MC code emitter, etc. 213 if (In64BitMode) 214 ToggleFeature(X86::Mode64Bit); 215 else if (In32BitMode) 216 ToggleFeature(X86::Mode32Bit); 217 else if (In16BitMode) 218 ToggleFeature(X86::Mode16Bit); 219 else 220 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!"); 221 222 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel 223 << ", 3DNowLevel " << X863DNowLevel 224 << ", 64bit " << HasX86_64 << "\n"); 225 assert((!In64BitMode || HasX86_64) && 226 "64-bit code requested on a subtarget that doesn't support it!"); 227 228 // Stack alignment is 16 bytes on Darwin, Linux, kFreeBSD and Solaris (both 229 // 32 and 64 bit) and for all 64-bit targets. 230 if (StackAlignOverride) 231 stackAlignment = StackAlignOverride; 232 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() || 233 isTargetKFreeBSD() || In64BitMode) 234 stackAlignment = 16; 235 236 assert((!isPMULLDSlow() || hasSSE41()) && 237 "Feature Slow PMULLD can only be set on a subtarget with SSE4.1"); 238 } 239 240 void X86Subtarget::initializeEnvironment() { 241 X86SSELevel = NoSSE; 242 X863DNowLevel = NoThreeDNow; 243 HasX87 = false; 244 HasCMov = false; 245 HasX86_64 = false; 246 HasPOPCNT = false; 247 HasSSE4A = false; 248 HasAES = false; 249 HasFXSR = false; 250 HasXSAVE = false; 251 HasXSAVEOPT = false; 252 HasXSAVEC = false; 253 HasXSAVES = false; 254 HasPCLMUL = false; 255 HasFMA = false; 256 HasFMA4 = false; 257 HasXOP = false; 258 HasTBM = false; 259 HasMOVBE = false; 260 HasRDRAND = false; 261 HasF16C = false; 262 HasFSGSBase = false; 263 HasLZCNT = false; 264 HasBMI = false; 265 HasBMI2 = false; 266 HasVBMI = false; 267 HasIFMA = false; 268 HasRTM = false; 269 HasHLE = false; 270 HasERI = false; 271 HasCDI = false; 272 HasPFI = false; 273 HasDQI = false; 274 HasBWI = false; 275 HasVLX = false; 276 HasADX = false; 277 HasPKU = false; 278 HasSHA = false; 279 HasPRFCHW = false; 280 HasRDSEED = false; 281 HasLAHFSAHF = false; 282 HasMWAITX = false; 283 HasMPX = false; 284 IsBTMemSlow = false; 285 IsPMULLDSlow = false; 286 IsSHLDSlow = false; 287 IsUAMem16Slow = false; 288 IsUAMem32Slow = false; 289 HasSSEUnalignedMem = false; 290 HasCmpxchg16b = false; 291 UseLeaForSP = false; 292 HasFastPartialYMMWrite = false; 293 HasFastScalarFSQRT = false; 294 HasFastVectorFSQRT = false; 295 HasFastLZCNT = false; 296 HasSlowDivide32 = false; 297 HasSlowDivide64 = false; 298 PadShortFunctions = false; 299 CallRegIndirect = false; 300 LEAUsesAG = false; 301 SlowLEA = false; 302 SlowIncDec = false; 303 stackAlignment = 4; 304 // FIXME: this is a known good value for Yonah. How about others? 305 MaxInlineSizeThreshold = 128; 306 UseSoftFloat = false; 307 } 308 309 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU, 310 StringRef FS) { 311 initializeEnvironment(); 312 initSubtargetFeatures(CPU, FS); 313 return *this; 314 } 315 316 X86Subtarget::X86Subtarget(const Triple &TT, StringRef CPU, StringRef FS, 317 const X86TargetMachine &TM, 318 unsigned StackAlignOverride) 319 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others), 320 PICStyle(PICStyles::None), TM(TM), TargetTriple(TT), 321 StackAlignOverride(StackAlignOverride), 322 In64BitMode(TargetTriple.getArch() == Triple::x86_64), 323 In32BitMode(TargetTriple.getArch() == Triple::x86 && 324 TargetTriple.getEnvironment() != Triple::CODE16), 325 In16BitMode(TargetTriple.getArch() == Triple::x86 && 326 TargetTriple.getEnvironment() == Triple::CODE16), 327 TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)), 328 TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) { 329 // Determine the PICStyle based on the target selected. 330 if (!isPositionIndependent()) 331 setPICStyle(PICStyles::None); 332 else if (is64Bit()) 333 setPICStyle(PICStyles::RIPRel); 334 else if (isTargetCOFF()) 335 setPICStyle(PICStyles::None); 336 else if (isTargetDarwin()) 337 setPICStyle(PICStyles::StubPIC); 338 else if (isTargetELF()) 339 setPICStyle(PICStyles::GOT); 340 } 341 342 const CallLowering *X86Subtarget::getCallLowering() const { 343 assert(GISel && "Access to GlobalISel APIs not set"); 344 return GISel->getCallLowering(); 345 } 346 347 const InstructionSelector *X86Subtarget::getInstructionSelector() const { 348 assert(GISel && "Access to GlobalISel APIs not set"); 349 return GISel->getInstructionSelector(); 350 } 351 352 const LegalizerInfo *X86Subtarget::getLegalizerInfo() const { 353 assert(GISel && "Access to GlobalISel APIs not set"); 354 return GISel->getLegalizerInfo(); 355 } 356 357 const RegisterBankInfo *X86Subtarget::getRegBankInfo() const { 358 assert(GISel && "Access to GlobalISel APIs not set"); 359 return GISel->getRegBankInfo(); 360 } 361 362 bool X86Subtarget::enableEarlyIfConversion() const { 363 return hasCMov() && X86EarlyIfConv; 364 } 365 366