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