LLVM API Documentation
00001 //===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the X86 specific subclass of TargetSubtargetInfo. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "X86Subtarget.h" 00015 #include "X86InstrInfo.h" 00016 #include "X86TargetMachine.h" 00017 #include "llvm/IR/Attributes.h" 00018 #include "llvm/IR/Function.h" 00019 #include "llvm/IR/GlobalValue.h" 00020 #include "llvm/Support/CommandLine.h" 00021 #include "llvm/Support/Debug.h" 00022 #include "llvm/Support/ErrorHandling.h" 00023 #include "llvm/Support/Host.h" 00024 #include "llvm/Support/raw_ostream.h" 00025 #include "llvm/Target/TargetMachine.h" 00026 #include "llvm/Target/TargetOptions.h" 00027 00028 #if defined(_MSC_VER) 00029 #include <intrin.h> 00030 #endif 00031 00032 using namespace llvm; 00033 00034 #define DEBUG_TYPE "subtarget" 00035 00036 #define GET_SUBTARGETINFO_TARGET_DESC 00037 #define GET_SUBTARGETINFO_CTOR 00038 #include "X86GenSubtargetInfo.inc" 00039 00040 // Temporary option to control early if-conversion for x86 while adding machine 00041 // models. 00042 static cl::opt<bool> 00043 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden, 00044 cl::desc("Enable early if-conversion on X86")); 00045 00046 00047 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the 00048 /// current subtarget according to how we should reference it in a non-pcrel 00049 /// context. 00050 unsigned char X86Subtarget::ClassifyBlockAddressReference() const { 00051 if (isPICStyleGOT()) // 32-bit ELF targets. 00052 return X86II::MO_GOTOFF; 00053 00054 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode. 00055 return X86II::MO_PIC_BASE_OFFSET; 00056 00057 // Direct static reference to label. 00058 return X86II::MO_NO_FLAG; 00059 } 00060 00061 /// ClassifyGlobalReference - Classify a global variable reference for the 00062 /// current subtarget according to how we should reference it in a non-pcrel 00063 /// context. 00064 unsigned char X86Subtarget:: 00065 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const { 00066 // DLLImport only exists on windows, it is implemented as a load from a 00067 // DLLIMPORT stub. 00068 if (GV->hasDLLImportStorageClass()) 00069 return X86II::MO_DLLIMPORT; 00070 00071 // Determine whether this is a reference to a definition or a declaration. 00072 // Materializable GVs (in JIT lazy compilation mode) do not require an extra 00073 // load from stub. 00074 bool isDecl = GV->hasAvailableExternallyLinkage(); 00075 if (GV->isDeclaration() && !GV->isMaterializable()) 00076 isDecl = true; 00077 00078 // X86-64 in PIC mode. 00079 if (isPICStyleRIPRel()) { 00080 // Large model never uses stubs. 00081 if (TM.getCodeModel() == CodeModel::Large) 00082 return X86II::MO_NO_FLAG; 00083 00084 if (isTargetDarwin()) { 00085 // If symbol visibility is hidden, the extra load is not needed if 00086 // target is x86-64 or the symbol is definitely defined in the current 00087 // translation unit. 00088 if (GV->hasDefaultVisibility() && 00089 (isDecl || GV->isWeakForLinker())) 00090 return X86II::MO_GOTPCREL; 00091 } else if (!isTargetWin64()) { 00092 assert(isTargetELF() && "Unknown rip-relative target"); 00093 00094 // Extra load is needed for all externally visible. 00095 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility()) 00096 return X86II::MO_GOTPCREL; 00097 } 00098 00099 return X86II::MO_NO_FLAG; 00100 } 00101 00102 if (isPICStyleGOT()) { // 32-bit ELF targets. 00103 // Extra load is needed for all externally visible. 00104 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility()) 00105 return X86II::MO_GOTOFF; 00106 return X86II::MO_GOT; 00107 } 00108 00109 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode. 00110 // Determine whether we have a stub reference and/or whether the reference 00111 // is relative to the PIC base or not. 00112 00113 // If this is a strong reference to a definition, it is definitely not 00114 // through a stub. 00115 if (!isDecl && !GV->isWeakForLinker()) 00116 return X86II::MO_PIC_BASE_OFFSET; 00117 00118 // Unless we have a symbol with hidden visibility, we have to go through a 00119 // normal $non_lazy_ptr stub because this symbol might be resolved late. 00120 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference. 00121 return X86II::MO_DARWIN_NONLAZY_PIC_BASE; 00122 00123 // If symbol visibility is hidden, we have a stub for common symbol 00124 // references and external declarations. 00125 if (isDecl || GV->hasCommonLinkage()) { 00126 // Hidden $non_lazy_ptr reference. 00127 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE; 00128 } 00129 00130 // Otherwise, no stub. 00131 return X86II::MO_PIC_BASE_OFFSET; 00132 } 00133 00134 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode. 00135 // Determine whether we have a stub reference. 00136 00137 // If this is a strong reference to a definition, it is definitely not 00138 // through a stub. 00139 if (!isDecl && !GV->isWeakForLinker()) 00140 return X86II::MO_NO_FLAG; 00141 00142 // Unless we have a symbol with hidden visibility, we have to go through a 00143 // normal $non_lazy_ptr stub because this symbol might be resolved late. 00144 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference. 00145 return X86II::MO_DARWIN_NONLAZY; 00146 00147 // Otherwise, no stub. 00148 return X86II::MO_NO_FLAG; 00149 } 00150 00151 // Direct static reference to global. 00152 return X86II::MO_NO_FLAG; 00153 } 00154 00155 00156 /// getBZeroEntry - This function returns the name of a function which has an 00157 /// interface like the non-standard bzero function, if such a function exists on 00158 /// the current subtarget and it is considered prefereable over memset with zero 00159 /// passed as the second argument. Otherwise it returns null. 00160 const char *X86Subtarget::getBZeroEntry() const { 00161 // Darwin 10 has a __bzero entry point for this purpose. 00162 if (getTargetTriple().isMacOSX() && 00163 !getTargetTriple().isMacOSXVersionLT(10, 6)) 00164 return "__bzero"; 00165 00166 return nullptr; 00167 } 00168 00169 bool X86Subtarget::hasSinCos() const { 00170 return getTargetTriple().isMacOSX() && 00171 !getTargetTriple().isMacOSXVersionLT(10, 9) && 00172 is64Bit(); 00173 } 00174 00175 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls 00176 /// to immediate address. 00177 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const { 00178 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32 00179 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does, 00180 // the following check for Win32 should be removed. 00181 if (In64BitMode || isTargetWin32()) 00182 return false; 00183 return isTargetELF() || TM.getRelocationModel() == Reloc::Static; 00184 } 00185 00186 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { 00187 std::string CPUName = CPU; 00188 if (CPUName.empty()) 00189 CPUName = "generic"; 00190 00191 // Make sure 64-bit features are available in 64-bit mode. (But make sure 00192 // SSE2 can be turned off explicitly.) 00193 std::string FullFS = FS; 00194 if (In64BitMode) { 00195 if (!FullFS.empty()) 00196 FullFS = "+64bit,+sse2," + FullFS; 00197 else 00198 FullFS = "+64bit,+sse2"; 00199 } 00200 00201 // If feature string is not empty, parse features string. 00202 ParseSubtargetFeatures(CPUName, FullFS); 00203 00204 // Make sure the right MCSchedModel is used. 00205 InitCPUSchedModel(CPUName); 00206 00207 InstrItins = getInstrItineraryForCPU(CPUName); 00208 00209 // It's important to keep the MCSubtargetInfo feature bits in sync with 00210 // target data structure which is shared with MC code emitter, etc. 00211 if (In64BitMode) 00212 ToggleFeature(X86::Mode64Bit); 00213 else if (In32BitMode) 00214 ToggleFeature(X86::Mode32Bit); 00215 else if (In16BitMode) 00216 ToggleFeature(X86::Mode16Bit); 00217 else 00218 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!"); 00219 00220 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel 00221 << ", 3DNowLevel " << X863DNowLevel 00222 << ", 64bit " << HasX86_64 << "\n"); 00223 assert((!In64BitMode || HasX86_64) && 00224 "64-bit code requested on a subtarget that doesn't support it!"); 00225 00226 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both 00227 // 32 and 64 bit) and for all 64-bit targets. 00228 if (StackAlignOverride) 00229 stackAlignment = StackAlignOverride; 00230 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() || 00231 In64BitMode) 00232 stackAlignment = 16; 00233 } 00234 00235 void X86Subtarget::initializeEnvironment() { 00236 X86SSELevel = NoMMXSSE; 00237 X863DNowLevel = NoThreeDNow; 00238 HasCMov = false; 00239 HasX86_64 = false; 00240 HasPOPCNT = false; 00241 HasSSE4A = false; 00242 HasAES = false; 00243 HasPCLMUL = false; 00244 HasFMA = false; 00245 HasFMA4 = false; 00246 HasXOP = false; 00247 HasTBM = false; 00248 HasMOVBE = false; 00249 HasRDRAND = false; 00250 HasF16C = false; 00251 HasFSGSBase = false; 00252 HasLZCNT = false; 00253 HasBMI = false; 00254 HasBMI2 = false; 00255 HasRTM = false; 00256 HasHLE = false; 00257 HasERI = false; 00258 HasCDI = false; 00259 HasPFI = false; 00260 HasDQI = false; 00261 HasBWI = false; 00262 HasVLX = false; 00263 HasADX = false; 00264 HasSHA = false; 00265 HasSGX = false; 00266 HasPRFCHW = false; 00267 HasRDSEED = false; 00268 HasSMAP = false; 00269 IsBTMemSlow = false; 00270 IsSHLDSlow = false; 00271 IsUAMemFast = false; 00272 HasVectorUAMem = false; 00273 HasCmpxchg16b = false; 00274 UseLeaForSP = false; 00275 HasSlowDivide = false; 00276 PadShortFunctions = false; 00277 CallRegIndirect = false; 00278 LEAUsesAG = false; 00279 SlowLEA = false; 00280 SlowIncDec = false; 00281 stackAlignment = 4; 00282 // FIXME: this is a known good value for Yonah. How about others? 00283 MaxInlineSizeThreshold = 128; 00284 } 00285 00286 static std::string computeDataLayout(const Triple &TT) { 00287 // X86 is little endian 00288 std::string Ret = "e"; 00289 00290 Ret += DataLayout::getManglingComponent(TT); 00291 // X86 and x32 have 32 bit pointers. 00292 if ((TT.isArch64Bit() && 00293 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) || 00294 !TT.isArch64Bit()) 00295 Ret += "-p:32:32"; 00296 00297 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32. 00298 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl()) 00299 Ret += "-i64:64"; 00300 else 00301 Ret += "-f64:32:64"; 00302 00303 // Some ABIs align long double to 128 bits, others to 32. 00304 if (TT.isOSNaCl()) 00305 ; // No f80 00306 else if (TT.isArch64Bit() || TT.isOSDarwin()) 00307 Ret += "-f80:128"; 00308 else 00309 Ret += "-f80:32"; 00310 00311 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits. 00312 if (TT.isArch64Bit()) 00313 Ret += "-n8:16:32:64"; 00314 else 00315 Ret += "-n8:16:32"; 00316 00317 // The stack is aligned to 32 bits on some ABIs and 128 bits on others. 00318 if (!TT.isArch64Bit() && TT.isOSWindows()) 00319 Ret += "-S32"; 00320 else 00321 Ret += "-S128"; 00322 00323 return Ret; 00324 } 00325 00326 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU, 00327 StringRef FS) { 00328 initializeEnvironment(); 00329 initSubtargetFeatures(CPU, FS); 00330 return *this; 00331 } 00332 00333 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU, 00334 const std::string &FS, X86TargetMachine &TM, 00335 unsigned StackAlignOverride) 00336 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others), 00337 PICStyle(PICStyles::None), TargetTriple(TT), 00338 DL(computeDataLayout(TargetTriple)), 00339 StackAlignOverride(StackAlignOverride), 00340 In64BitMode(TargetTriple.getArch() == Triple::x86_64), 00341 In32BitMode(TargetTriple.getArch() == Triple::x86 && 00342 TargetTriple.getEnvironment() != Triple::CODE16), 00343 In16BitMode(TargetTriple.getArch() == Triple::x86 && 00344 TargetTriple.getEnvironment() == Triple::CODE16), 00345 TSInfo(DL), InstrInfo(initializeSubtargetDependencies(CPU, FS)), 00346 TLInfo(TM), FrameLowering(TargetFrameLowering::StackGrowsDown, 00347 getStackAlignment(), is64Bit() ? -8 : -4) { 00348 // Determine the PICStyle based on the target selected. 00349 if (TM.getRelocationModel() == Reloc::Static) { 00350 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None. 00351 setPICStyle(PICStyles::None); 00352 } else if (is64Bit()) { 00353 // PIC in 64 bit mode is always rip-rel. 00354 setPICStyle(PICStyles::RIPRel); 00355 } else if (isTargetCOFF()) { 00356 setPICStyle(PICStyles::None); 00357 } else if (isTargetDarwin()) { 00358 if (TM.getRelocationModel() == Reloc::PIC_) 00359 setPICStyle(PICStyles::StubPIC); 00360 else { 00361 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC); 00362 setPICStyle(PICStyles::StubDynamicNoPIC); 00363 } 00364 } else if (isTargetELF()) { 00365 setPICStyle(PICStyles::GOT); 00366 } 00367 } 00368 00369 bool X86Subtarget::enableEarlyIfConversion() const { 00370 return hasCMov() && X86EarlyIfConv; 00371 } 00372