LLVM API Documentation

X86Subtarget.cpp

Go to the documentation of this file.
00001 //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
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 TargetSubtarget.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "subtarget"
00015 #include "X86Subtarget.h"
00016 #include "X86InstrInfo.h"
00017 #include "X86GenSubtarget.inc"
00018 #include "llvm/GlobalValue.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 #include "llvm/Target/TargetMachine.h"
00022 #include "llvm/Target/TargetOptions.h"
00023 using namespace llvm;
00024 
00025 #if defined(_MSC_VER)
00026 #include <intrin.h>
00027 #endif
00028 
00029 /// ClassifyGlobalReference - Classify a global variable reference for the
00030 /// current subtarget according to how we should reference it in a non-pcrel
00031 /// context.
00032 unsigned char X86Subtarget::
00033 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
00034   // DLLImport only exists on windows, it is implemented as a load from a
00035   // DLLIMPORT stub.
00036   if (GV->hasDLLImportLinkage())
00037     return X86II::MO_DLLIMPORT;
00038 
00039   // GV with ghost linkage (in JIT lazy compilation mode) do not require an
00040   // extra load from stub.
00041   bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
00042 
00043   // X86-64 in PIC mode.
00044   if (isPICStyleRIPRel()) {
00045     // Large model never uses stubs.
00046     if (TM.getCodeModel() == CodeModel::Large)
00047       return X86II::MO_NO_FLAG;
00048       
00049     if (isTargetDarwin()) {
00050       // If symbol visibility is hidden, the extra load is not needed if
00051       // target is x86-64 or the symbol is definitely defined in the current
00052       // translation unit.
00053       if (GV->hasDefaultVisibility() &&
00054           (isDecl || GV->isWeakForLinker()))
00055         return X86II::MO_GOTPCREL;
00056     } else {
00057       assert(isTargetELF() && "Unknown rip-relative target");
00058 
00059       // Extra load is needed for all externally visible.
00060       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
00061         return X86II::MO_GOTPCREL;
00062     }
00063 
00064     return X86II::MO_NO_FLAG;
00065   }
00066   
00067   if (isPICStyleGOT()) {   // 32-bit ELF targets.
00068     // Extra load is needed for all externally visible.
00069     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
00070       return X86II::MO_GOTOFF;
00071     return X86II::MO_GOT;
00072   }
00073   
00074   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
00075     // Determine whether we have a stub reference and/or whether the reference
00076     // is relative to the PIC base or not.
00077     
00078     // If this is a strong reference to a definition, it is definitely not
00079     // through a stub.
00080     if (!isDecl && !GV->isWeakForLinker())
00081       return X86II::MO_PIC_BASE_OFFSET;
00082 
00083     // Unless we have a symbol with hidden visibility, we have to go through a
00084     // normal $non_lazy_ptr stub because this symbol might be resolved late.
00085     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
00086       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
00087     
00088     // If symbol visibility is hidden, we have a stub for common symbol
00089     // references and external declarations.
00090     if (isDecl || GV->hasCommonLinkage()) {
00091       // Hidden $non_lazy_ptr reference.
00092       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
00093     }
00094     
00095     // Otherwise, no stub.
00096     return X86II::MO_PIC_BASE_OFFSET;
00097   }
00098   
00099   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
00100     // Determine whether we have a stub reference.
00101     
00102     // If this is a strong reference to a definition, it is definitely not
00103     // through a stub.
00104     if (!isDecl && !GV->isWeakForLinker())
00105       return X86II::MO_NO_FLAG;
00106     
00107     // Unless we have a symbol with hidden visibility, we have to go through a
00108     // normal $non_lazy_ptr stub because this symbol might be resolved late.
00109     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
00110       return X86II::MO_DARWIN_NONLAZY;
00111     
00112     // If symbol visibility is hidden, we have a stub for common symbol
00113     // references and external declarations.
00114     if (isDecl || GV->hasCommonLinkage()) {
00115       // Hidden $non_lazy_ptr reference.
00116       return X86II::MO_DARWIN_HIDDEN_NONLAZY;
00117     }
00118     
00119     // Otherwise, no stub.
00120     return X86II::MO_NO_FLAG;
00121   }
00122   
00123   // Direct static reference to global.
00124   return X86II::MO_NO_FLAG;
00125 }
00126 
00127 
00128 /// getBZeroEntry - This function returns the name of a function which has an
00129 /// interface like the non-standard bzero function, if such a function exists on
00130 /// the current subtarget and it is considered prefereable over memset with zero
00131 /// passed as the second argument. Otherwise it returns null.
00132 const char *X86Subtarget::getBZeroEntry() const {
00133   // Darwin 10 has a __bzero entry point for this purpose.
00134   if (getDarwinVers() >= 10)
00135     return "__bzero";
00136 
00137   return 0;
00138 }
00139 
00140 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
00141 /// to immediate address.
00142 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
00143   if (Is64Bit)
00144     return false;
00145   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
00146 }
00147 
00148 /// getSpecialAddressLatency - For targets where it is beneficial to
00149 /// backschedule instructions that compute addresses, return a value
00150 /// indicating the number of scheduling cycles of backscheduling that
00151 /// should be attempted.
00152 unsigned X86Subtarget::getSpecialAddressLatency() const {
00153   // For x86 out-of-order targets, back-schedule address computations so
00154   // that loads and stores aren't blocked.
00155   // This value was chosen arbitrarily.
00156   return 200;
00157 }
00158 
00159 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
00160 /// specified arguments.  If we can't run cpuid on the host, return true.
00161 bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
00162                           unsigned *rECX, unsigned *rEDX) {
00163 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
00164   #if defined(__GNUC__)
00165     // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
00166     asm ("movq\t%%rbx, %%rsi\n\t"
00167          "cpuid\n\t"
00168          "xchgq\t%%rbx, %%rsi\n\t"
00169          : "=a" (*rEAX),
00170            "=S" (*rEBX),
00171            "=c" (*rECX),
00172            "=d" (*rEDX)
00173          :  "a" (value));
00174     return false;
00175   #elif defined(_MSC_VER)
00176     int registers[4];
00177     __cpuid(registers, value);
00178     *rEAX = registers[0];
00179     *rEBX = registers[1];
00180     *rECX = registers[2];
00181     *rEDX = registers[3];
00182     return false;
00183   #endif
00184 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
00185   #if defined(__GNUC__)
00186     asm ("movl\t%%ebx, %%esi\n\t"
00187          "cpuid\n\t"
00188          "xchgl\t%%ebx, %%esi\n\t"
00189          : "=a" (*rEAX),
00190            "=S" (*rEBX),
00191            "=c" (*rECX),
00192            "=d" (*rEDX)
00193          :  "a" (value));
00194     return false;
00195   #elif defined(_MSC_VER)
00196     __asm {
00197       mov   eax,value
00198       cpuid
00199       mov   esi,rEAX
00200       mov   dword ptr [esi],eax
00201       mov   esi,rEBX
00202       mov   dword ptr [esi],ebx
00203       mov   esi,rECX
00204       mov   dword ptr [esi],ecx
00205       mov   esi,rEDX
00206       mov   dword ptr [esi],edx
00207     }
00208     return false;
00209   #endif
00210 #endif
00211   return true;
00212 }
00213 
00214 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
00215   Family = (EAX >> 8) & 0xf; // Bits 8 - 11
00216   Model  = (EAX >> 4) & 0xf; // Bits 4 - 7
00217   if (Family == 6 || Family == 0xf) {
00218     if (Family == 0xf)
00219       // Examine extended family ID if family ID is F.
00220       Family += (EAX >> 20) & 0xff;    // Bits 20 - 27
00221     // Examine extended model ID if family ID is 6 or F.
00222     Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
00223   }
00224 }
00225 
00226 void X86Subtarget::AutoDetectSubtargetFeatures() {
00227   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
00228   union {
00229     unsigned u[3];
00230     char     c[12];
00231   } text;
00232   
00233   if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
00234     return;
00235 
00236   X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
00237   
00238   if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
00239   if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
00240   if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
00241   if (ECX & 0x1)         X86SSELevel = SSE3;
00242   if ((ECX >> 9)  & 0x1) X86SSELevel = SSSE3;
00243   if ((ECX >> 19) & 0x1) X86SSELevel = SSE41;
00244   if ((ECX >> 20) & 0x1) X86SSELevel = SSE42;
00245 
00246   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
00247   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
00248 
00249   HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
00250   HasAVX = ((ECX >> 28) & 0x1);
00251 
00252   if (IsIntel || IsAMD) {
00253     // Determine if bit test memory instructions are slow.
00254     unsigned Family = 0;
00255     unsigned Model  = 0;
00256     DetectFamilyModel(EAX, Family, Model);
00257     IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
00258 
00259     X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
00260     HasX86_64 = (EDX >> 29) & 0x1;
00261     HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
00262     HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
00263   }
00264 }
00265 
00266 static const char *GetCurrentX86CPU() {
00267   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
00268   if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
00269     return "generic";
00270   unsigned Family = 0;
00271   unsigned Model  = 0;
00272   DetectFamilyModel(EAX, Family, Model);
00273 
00274   X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
00275   bool Em64T = (EDX >> 29) & 0x1;
00276   bool HasSSE3 = (ECX & 0x1);
00277 
00278   union {
00279     unsigned u[3];
00280     char     c[12];
00281   } text;
00282 
00283   X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
00284   if (memcmp(text.c, "GenuineIntel", 12) == 0) {
00285     switch (Family) {
00286       case 3:
00287         return "i386";
00288       case 4:
00289         return "i486";
00290       case 5:
00291         switch (Model) {
00292         case 4:  return "pentium-mmx";
00293         default: return "pentium";
00294         }
00295       case 6:
00296         switch (Model) {
00297         case 1:  return "pentiumpro";
00298         case 3:
00299         case 5:
00300         case 6:  return "pentium2";
00301         case 7:
00302         case 8:
00303         case 10:
00304         case 11: return "pentium3";
00305         case 9:
00306         case 13: return "pentium-m";
00307         case 14: return "yonah";
00308         case 15:
00309         case 22: // Celeron M 540
00310           return "core2";
00311         case 23: // 45nm: Penryn , Wolfdale, Yorkfield (XE)
00312           return "penryn";
00313         default: return "i686";
00314         }
00315       case 15: {
00316         switch (Model) {
00317         case 3:  
00318         case 4:
00319         case 6: // same as 4, but 65nm
00320           return (Em64T) ? "nocona" : "prescott";
00321         case 26:
00322           return "corei7";
00323         case 28:
00324           return "atom";
00325         default:
00326           return (Em64T) ? "x86-64" : "pentium4";
00327         }
00328       }
00329         
00330     default:
00331       return "generic";
00332     }
00333   } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
00334     // FIXME: this poorly matches the generated SubtargetFeatureKV table.  There
00335     // appears to be no way to generate the wide variety of AMD-specific targets
00336     // from the information returned from CPUID.
00337     switch (Family) {
00338       case 4:
00339         return "i486";
00340       case 5:
00341         switch (Model) {
00342         case 6:
00343         case 7:  return "k6";
00344         case 8:  return "k6-2";
00345         case 9:
00346         case 13: return "k6-3";
00347         default: return "pentium";
00348         }
00349       case 6:
00350         switch (Model) {
00351         case 4:  return "athlon-tbird";
00352         case 6:
00353         case 7:
00354         case 8:  return "athlon-mp";
00355         case 10: return "athlon-xp";
00356         default: return "athlon";
00357         }
00358       case 15:
00359         if (HasSSE3) {
00360           return "k8-sse3";
00361         } else {
00362           switch (Model) {
00363           case 1:  return "opteron";
00364           case 5:  return "athlon-fx"; // also opteron
00365           default: return "athlon64";
00366           }
00367         }
00368       case 16:
00369         return "amdfam10";
00370     default:
00371       return "generic";
00372     }
00373   } else {
00374     return "generic";
00375   }
00376 }
00377 
00378 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &FS, 
00379                            bool is64Bit)
00380   : PICStyle(PICStyles::None)
00381   , X86SSELevel(NoMMXSSE)
00382   , X863DNowLevel(NoThreeDNow)
00383   , HasX86_64(false)
00384   , HasSSE4A(false)
00385   , HasAVX(false)
00386   , HasFMA3(false)
00387   , HasFMA4(false)
00388   , IsBTMemSlow(false)
00389   , DarwinVers(0)
00390   , IsLinux(false)
00391   , stackAlignment(8)
00392   // FIXME: this is a known good value for Yonah. How about others?
00393   , MaxInlineSizeThreshold(128)
00394   , Is64Bit(is64Bit)
00395   , TargetType(isELF) { // Default to ELF unless otherwise specified.
00396 
00397   // default to hard float ABI
00398   if (FloatABIType == FloatABI::Default)
00399     FloatABIType = FloatABI::Hard;
00400     
00401   // Determine default and user specified characteristics
00402   if (!FS.empty()) {
00403     // If feature string is not empty, parse features string.
00404     std::string CPU = GetCurrentX86CPU();
00405     ParseSubtargetFeatures(FS, CPU);
00406     // All X86-64 CPUs also have SSE2, however user might request no SSE via 
00407     // -mattr, so don't force SSELevel here.
00408   } else {
00409     // Otherwise, use CPUID to auto-detect feature set.
00410     AutoDetectSubtargetFeatures();
00411     // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
00412     if (Is64Bit && X86SSELevel < SSE2)
00413       X86SSELevel = SSE2;
00414   }
00415 
00416   // If requesting codegen for X86-64, make sure that 64-bit features
00417   // are enabled.
00418   if (Is64Bit)
00419     HasX86_64 = true;
00420 
00421   DEBUG(errs() << "Subtarget features: SSELevel " << X86SSELevel
00422                << ", 3DNowLevel " << X863DNowLevel
00423                << ", 64bit " << HasX86_64 << "\n");
00424   assert((!Is64Bit || HasX86_64) &&
00425          "64-bit code requested on a subtarget that doesn't support it!");
00426 
00427   // Set the boolean corresponding to the current target triple, or the default
00428   // if one cannot be determined, to true.
00429   if (TT.length() > 5) {
00430     size_t Pos;
00431     if ((Pos = TT.find("-darwin")) != std::string::npos) {
00432       TargetType = isDarwin;
00433       
00434       // Compute the darwin version number.
00435       if (isdigit(TT[Pos+7]))
00436         DarwinVers = atoi(&TT[Pos+7]);
00437       else
00438         DarwinVers = 8;  // Minimum supported darwin is Tiger.
00439     } else if (TT.find("linux") != std::string::npos) {
00440       // Linux doesn't imply ELF, but we don't currently support anything else.
00441       TargetType = isELF;
00442       IsLinux = true;
00443     } else if (TT.find("cygwin") != std::string::npos) {
00444       TargetType = isCygwin;
00445     } else if (TT.find("mingw") != std::string::npos) {
00446       TargetType = isMingw;
00447     } else if (TT.find("win32") != std::string::npos) {
00448       TargetType = isWindows;
00449     } else if (TT.find("windows") != std::string::npos) {
00450       TargetType = isWindows;
00451     } else if (TT.find("-cl") != std::string::npos) {
00452       TargetType = isDarwin;
00453       DarwinVers = 9;
00454     }
00455   }
00456 
00457   // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
00458   // bit targets.
00459   if (TargetType == isDarwin || Is64Bit)
00460     stackAlignment = 16;
00461 
00462   if (StackAlignment)
00463     stackAlignment = StackAlignment;
00464 }