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 "X86.h" 15 16 #include "X86CallLowering.h" 17 #include "X86LegalizerInfo.h" 18 #include "X86RegisterBankInfo.h" 19 #include "X86Subtarget.h" 20 #include "MCTargetDesc/X86BaseInfo.h" 21 #include "X86TargetMachine.h" 22 #include "llvm/ADT/Triple.h" 23 #include "llvm/CodeGen/GlobalISel/CallLowering.h" 24 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" 25 #include "llvm/CodeGen/GlobalISel/Legalizer.h" 26 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" 27 #include "llvm/IR/Attributes.h" 28 #include "llvm/IR/ConstantRange.h" 29 #include "llvm/IR/Function.h" 30 #include "llvm/IR/GlobalValue.h" 31 #include "llvm/Support/Casting.h" 32 #include "llvm/Support/CodeGen.h" 33 #include "llvm/Support/CommandLine.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/raw_ostream.h" 37 #include "llvm/Target/TargetMachine.h" 38 #include <cassert> 39 #include <string> 40 41 #if defined(_MSC_VER) 42 #include <intrin.h> 43 #endif 44 45 using namespace llvm; 46 47 #define DEBUG_TYPE "subtarget" 48 49 #define GET_SUBTARGETINFO_TARGET_DESC 50 #define GET_SUBTARGETINFO_CTOR 51 #include "X86GenSubtargetInfo.inc" 52 53 // Temporary option to control early if-conversion for x86 while adding machine 54 // models. 55 static cl::opt<bool> 56 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden, 57 cl::desc("Enable early if-conversion on X86")); 58 59 60 /// Classify a blockaddress reference for the current subtarget according to how 61 /// we should reference it in a non-pcrel context. 62 unsigned char X86Subtarget::classifyBlockAddressReference() const { 63 return classifyLocalReference(nullptr); 64 } 65 66 /// Classify a global variable reference for the current subtarget according to 67 /// how we should reference it in a non-pcrel context. 68 unsigned char 69 X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const { 70 return classifyGlobalReference(GV, *GV->getParent()); 71 } 72 73 unsigned char 74 X86Subtarget::classifyLocalReference(const GlobalValue *GV) const { 75 // 64 bits can use %rip addressing for anything local. 76 if (is64Bit()) 77 return X86II::MO_NO_FLAG; 78 79 // If this is for a position dependent executable, the static linker can 80 // figure it out. 81 if (!isPositionIndependent()) 82 return X86II::MO_NO_FLAG; 83 84 // The COFF dynamic linker just patches the executable sections. 85 if (isTargetCOFF()) 86 return X86II::MO_NO_FLAG; 87 88 if (isTargetDarwin()) { 89 // 32 bit macho has no relocation for a-b if a is undefined, even if 90 // b is in the section that is being relocated. 91 // This means we have to use o load even for GVs that are known to be 92 // local to the dso. 93 if (GV && (GV->isDeclarationForLinker() || GV->hasCommonLinkage())) 94 return X86II::MO_DARWIN_NONLAZY_PIC_BASE; 95 96 return X86II::MO_PIC_BASE_OFFSET; 97 } 98 99 return X86II::MO_GOTOFF; 100 } 101 102 unsigned char X86Subtarget::classifyGlobalReference(const GlobalValue *GV, 103 const Module &M) const { 104 // Large model never uses stubs. 105 if (TM.getCodeModel() == CodeModel::Large) 106 return X86II::MO_NO_FLAG; 107 108 // Absolute symbols can be referenced directly. 109 if (GV) { 110 if (Optional<ConstantRange> CR = GV->getAbsoluteSymbolRange()) { 111 // See if we can use the 8-bit immediate form. Note that some instructions 112 // will sign extend the immediate operand, so to be conservative we only 113 // accept the range [0,128). 114 if (CR->getUnsignedMax().ult(128)) 115 return X86II::MO_ABS8; 116 else 117 return X86II::MO_NO_FLAG; 118 } 119 } 120 121 if (TM.shouldAssumeDSOLocal(M, GV)) 122 return classifyLocalReference(GV); 123 124 if (isTargetCOFF()) 125 return X86II::MO_DLLIMPORT; 126 127 if (is64Bit()) 128 return X86II::MO_GOTPCREL; 129 130 if (isTargetDarwin()) { 131 if (!isPositionIndependent()) 132 return X86II::MO_DARWIN_NONLAZY; 133 return X86II::MO_DARWIN_NONLAZY_PIC_BASE; 134 } 135 136 return X86II::MO_GOT; 137 } 138 139 unsigned char 140 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const { 141 return classifyGlobalFunctionReference(GV, *GV->getParent()); 142 } 143 144 unsigned char 145 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV, 146 const Module &M) const { 147 if (TM.shouldAssumeDSOLocal(M, GV)) 148 return X86II::MO_NO_FLAG; 149 150 if (isTargetCOFF()) { 151 assert(GV->hasDLLImportStorageClass() && 152 "shouldAssumeDSOLocal gave inconsistent answer"); 153 return X86II::MO_DLLIMPORT; 154 } 155 156 const Function *F = dyn_cast_or_null<Function>(GV); 157 158 if (isTargetELF()) { 159 if (is64Bit() && F && (CallingConv::X86_RegCall == F->getCallingConv())) 160 // According to psABI, PLT stub clobbers XMM8-XMM15. 161 // In Regcall calling convention those registers are used for passing 162 // parameters. Thus we need to prevent lazy binding in Regcall. 163 return X86II::MO_GOTPCREL; 164 if (F && F->hasFnAttribute(Attribute::NonLazyBind) && is64Bit()) 165 return X86II::MO_GOTPCREL; 166 return X86II::MO_PLT; 167 } 168 169 if (is64Bit()) { 170 if (F && F->hasFnAttribute(Attribute::NonLazyBind)) 171 // If the function is marked as non-lazy, generate an indirect call 172 // which loads from the GOT directly. This avoids runtime overhead 173 // at the cost of eager binding (and one extra byte of encoding). 174 return X86II::MO_GOTPCREL; 175 return X86II::MO_NO_FLAG; 176 } 177 178 return X86II::MO_NO_FLAG; 179 } 180 181 /// This function returns the name of a function which has an interface like 182 /// the non-standard bzero function, if such a function exists on the 183 /// current subtarget and it is considered preferable over memset with zero 184 /// passed as the second argument. Otherwise it returns null. 185 const char *X86Subtarget::getBZeroEntry() const { 186 // Darwin 10 has a __bzero entry point for this purpose. 187 if (getTargetTriple().isMacOSX() && 188 !getTargetTriple().isMacOSXVersionLT(10, 6)) 189 return "__bzero"; 190 191 return nullptr; 192 } 193 194 bool X86Subtarget::hasSinCos() const { 195 if (getTargetTriple().isMacOSX()) { 196 return !getTargetTriple().isMacOSXVersionLT(10, 9) && is64Bit(); 197 } else if (getTargetTriple().isOSFuchsia()) { 198 return true; 199 } 200 return false; 201 } 202 203 /// Return true if the subtarget allows calls to immediate address. 204 bool X86Subtarget::isLegalToCallImmediateAddr() 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 // 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, kFreeBSD 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 isTargetKFreeBSD() || In64BitMode) 271 stackAlignment = 16; 272 273 // Gather is available since Haswell (AVX2 set). So technically, we can generate Gathers 274 // on all AVX2 processors. But the overhead on HSW is high. Skylake Client processor has 275 // faster Gathers than HSW and performance is similar to Skylake Server (AVX-512). 276 // The specified overhead is relative to the Load operation."2" is the number provided 277 // by Intel architects, This parameter is used for cost estimation of Gather Op and 278 // comparison with other alternatives. 279 if (X86ProcFamily == IntelSkylake || hasAVX512()) 280 GatherOverhead = 2; 281 if (hasAVX512()) 282 ScatterOverhead = 2; 283 } 284 285 void X86Subtarget::initializeEnvironment() { 286 X86SSELevel = NoSSE; 287 X863DNowLevel = NoThreeDNow; 288 HasX87 = false; 289 HasCMov = false; 290 HasX86_64 = false; 291 HasPOPCNT = false; 292 HasSSE4A = false; 293 HasAES = false; 294 HasFXSR = false; 295 HasXSAVE = false; 296 HasXSAVEOPT = false; 297 HasXSAVEC = false; 298 HasXSAVES = false; 299 HasPCLMUL = false; 300 HasFMA = false; 301 HasFMA4 = false; 302 HasXOP = false; 303 HasTBM = false; 304 HasLWP = false; 305 HasMOVBE = false; 306 HasRDRAND = false; 307 HasF16C = false; 308 HasFSGSBase = false; 309 HasLZCNT = false; 310 HasBMI = false; 311 HasBMI2 = false; 312 HasVBMI = false; 313 HasIFMA = false; 314 HasRTM = false; 315 HasERI = false; 316 HasCDI = false; 317 HasPFI = false; 318 HasDQI = false; 319 HasVPOPCNTDQ = false; 320 HasBWI = false; 321 HasVLX = false; 322 HasADX = false; 323 HasPKU = false; 324 HasSHA = false; 325 HasPRFCHW = false; 326 HasRDSEED = false; 327 HasLAHFSAHF = false; 328 HasMWAITX = false; 329 HasCLZERO = false; 330 HasMPX = false; 331 HasSGX = false; 332 HasCLFLUSHOPT = false; 333 HasCLWB = false; 334 IsPMULLDSlow = false; 335 IsSHLDSlow = false; 336 IsUAMem16Slow = false; 337 IsUAMem32Slow = false; 338 HasSSEUnalignedMem = false; 339 HasCmpxchg16b = false; 340 UseLeaForSP = false; 341 HasFastPartialYMMorZMMWrite = false; 342 HasFastScalarFSQRT = false; 343 HasFastVectorFSQRT = false; 344 HasFastLZCNT = false; 345 HasFastSHLDRotate = false; 346 HasMacroFusion = false; 347 HasERMSB = false; 348 HasSlowDivide32 = false; 349 HasSlowDivide64 = false; 350 PadShortFunctions = false; 351 SlowTwoMemOps = false; 352 LEAUsesAG = false; 353 SlowLEA = false; 354 Slow3OpsLEA = false; 355 SlowIncDec = false; 356 stackAlignment = 4; 357 // FIXME: this is a known good value for Yonah. How about others? 358 MaxInlineSizeThreshold = 128; 359 UseSoftFloat = false; 360 X86ProcFamily = Others; 361 GatherOverhead = 1024; 362 ScatterOverhead = 1024; 363 } 364 365 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU, 366 StringRef FS) { 367 initializeEnvironment(); 368 initSubtargetFeatures(CPU, FS); 369 return *this; 370 } 371 372 X86Subtarget::X86Subtarget(const Triple &TT, StringRef CPU, StringRef FS, 373 const X86TargetMachine &TM, 374 unsigned StackAlignOverride) 375 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others), 376 PICStyle(PICStyles::None), TM(TM), TargetTriple(TT), 377 StackAlignOverride(StackAlignOverride), 378 In64BitMode(TargetTriple.getArch() == Triple::x86_64), 379 In32BitMode(TargetTriple.getArch() == Triple::x86 && 380 TargetTriple.getEnvironment() != Triple::CODE16), 381 In16BitMode(TargetTriple.getArch() == Triple::x86 && 382 TargetTriple.getEnvironment() == Triple::CODE16), 383 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this), 384 FrameLowering(*this, getStackAlignment()) { 385 // Determine the PICStyle based on the target selected. 386 if (!isPositionIndependent()) 387 setPICStyle(PICStyles::None); 388 else if (is64Bit()) 389 setPICStyle(PICStyles::RIPRel); 390 else if (isTargetCOFF()) 391 setPICStyle(PICStyles::None); 392 else if (isTargetDarwin()) 393 setPICStyle(PICStyles::StubPIC); 394 else if (isTargetELF()) 395 setPICStyle(PICStyles::GOT); 396 397 CallLoweringInfo.reset(new X86CallLowering(*getTargetLowering())); 398 Legalizer.reset(new X86LegalizerInfo(*this, TM)); 399 400 auto *RBI = new X86RegisterBankInfo(*getRegisterInfo()); 401 RegBankInfo.reset(RBI); 402 InstSelector.reset(createX86InstructionSelector(TM, *this, *RBI)); 403 } 404 405 const CallLowering *X86Subtarget::getCallLowering() const { 406 return CallLoweringInfo.get(); 407 } 408 409 const InstructionSelector *X86Subtarget::getInstructionSelector() const { 410 return InstSelector.get(); 411 } 412 413 const LegalizerInfo *X86Subtarget::getLegalizerInfo() const { 414 return Legalizer.get(); 415 } 416 417 const RegisterBankInfo *X86Subtarget::getRegBankInfo() const { 418 return RegBankInfo.get(); 419 } 420 421 bool X86Subtarget::enableEarlyIfConversion() const { 422 return hasCMov() && X86EarlyIfConv; 423 } 424