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