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 "X86GenSubtarget.inc"
00017 #include "llvm/Module.h"
00018 #include "llvm/Support/CommandLine.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Target/TargetMachine.h"
00021 #include "llvm/Target/TargetOptions.h"
00022 using namespace llvm;
00023 
00024 static cl::opt<X86Subtarget::AsmWriterFlavorTy>
00025 AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::Unset),
00026   cl::desc("Choose style of code to emit from X86 backend:"),
00027   cl::values(
00028     clEnumValN(X86Subtarget::ATT,   "att",   "Emit AT&T-style assembly"),
00029     clEnumValN(X86Subtarget::Intel, "intel", "Emit Intel-style assembly"),
00030     clEnumValEnd));
00031 
00032 
00033 /// True if accessing the GV requires an extra load. For Windows, dllimported
00034 /// symbols are indirect, loading the value at address GV rather then the
00035 /// value of GV itself. This means that the GlobalAddress must be in the base
00036 /// or index register of the address, not the GV offset field.
00037 bool X86Subtarget::GVRequiresExtraLoad(const GlobalValue* GV,
00038                                        const TargetMachine& TM,
00039                                        bool isDirectCall) const
00040 {
00041   // FIXME: PIC
00042   if (TM.getRelocationModel() != Reloc::Static &&
00043       TM.getCodeModel() != CodeModel::Large) {
00044     if (isTargetDarwin()) {
00045       if (isDirectCall)
00046         return false;
00047       bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
00048       if (GV->hasHiddenVisibility() &&
00049           (Is64Bit || (!isDecl && !GV->hasCommonLinkage())))
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         return false;
00054       return !isDirectCall && (isDecl || GV->mayBeOverridden());
00055     } else if (isTargetELF()) {
00056       // Extra load is needed for all externally visible.
00057       if (isDirectCall)
00058         return false;
00059       if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
00060         return false;
00061       return true;
00062     } else if (isTargetCygMing() || isTargetWindows()) {
00063       return (GV->hasDLLImportLinkage());
00064     }
00065   }
00066   return false;
00067 }
00068 
00069 /// True if accessing the GV requires a register.  This is a superset of the
00070 /// cases where GVRequiresExtraLoad is true.  Some variations of PIC require
00071 /// a register, but not an extra load.
00072 bool X86Subtarget::GVRequiresRegister(const GlobalValue *GV,
00073                                        const TargetMachine& TM,
00074                                        bool isDirectCall) const
00075 {
00076   if (GVRequiresExtraLoad(GV, TM, isDirectCall))
00077     return true;
00078   // Code below here need only consider cases where GVRequiresExtraLoad
00079   // returns false.
00080   if (TM.getRelocationModel() == Reloc::PIC_)
00081     return !isDirectCall && 
00082       (GV->hasLocalLinkage() || GV->hasExternalLinkage());
00083   return false;
00084 }
00085 
00086 /// getBZeroEntry - This function returns the name of a function which has an
00087 /// interface like the non-standard bzero function, if such a function exists on
00088 /// the current subtarget and it is considered prefereable over memset with zero
00089 /// passed as the second argument. Otherwise it returns null.
00090 const char *X86Subtarget::getBZeroEntry() const {
00091   // Darwin 10 has a __bzero entry point for this purpose.
00092   if (getDarwinVers() >= 10)
00093     return "__bzero";
00094 
00095   return 0;
00096 }
00097 
00098 /// getSpecialAddressLatency - For targets where it is beneficial to
00099 /// backschedule instructions that compute addresses, return a value
00100 /// indicating the number of scheduling cycles of backscheduling that
00101 /// should be attempted.
00102 unsigned X86Subtarget::getSpecialAddressLatency() const {
00103   // For x86 out-of-order targets, back-schedule address computations so
00104   // that loads and stores aren't blocked.
00105   // This value was chosen arbitrarily.
00106   return 200;
00107 }
00108 
00109 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
00110 /// specified arguments.  If we can't run cpuid on the host, return true.
00111 bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
00112                           unsigned *rECX, unsigned *rEDX) {
00113 #if defined(__x86_64__)
00114   // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
00115   asm ("movq\t%%rbx, %%rsi\n\t"
00116        "cpuid\n\t"
00117        "xchgq\t%%rbx, %%rsi\n\t"
00118        : "=a" (*rEAX),
00119          "=S" (*rEBX),
00120          "=c" (*rECX),
00121          "=d" (*rEDX)
00122        :  "a" (value));
00123   return false;
00124 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
00125 #if defined(__GNUC__)
00126   asm ("movl\t%%ebx, %%esi\n\t"
00127        "cpuid\n\t"
00128        "xchgl\t%%ebx, %%esi\n\t"
00129        : "=a" (*rEAX),
00130          "=S" (*rEBX),
00131          "=c" (*rECX),
00132          "=d" (*rEDX)
00133        :  "a" (value));
00134   return false;
00135 #elif defined(_MSC_VER)
00136   __asm {
00137     mov   eax,value
00138     cpuid
00139     mov   esi,rEAX
00140     mov   dword ptr [esi],eax
00141     mov   esi,rEBX
00142     mov   dword ptr [esi],ebx
00143     mov   esi,rECX
00144     mov   dword ptr [esi],ecx
00145     mov   esi,rEDX
00146     mov   dword ptr [esi],edx
00147   }
00148   return false;
00149 #endif
00150 #endif
00151   return true;
00152 }
00153 
00154 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
00155   Family = (EAX >> 8) & 0xf; // Bits 8 - 11
00156   Model  = (EAX >> 4) & 0xf; // Bits 4 - 7
00157   if (Family == 6 || Family == 0xf) {
00158     if (Family == 0xf)
00159       // Examine extended family ID if family ID is F.
00160       Family += (EAX >> 20) & 0xff;    // Bits 20 - 27
00161     // Examine extended model ID if family ID is 6 or F.
00162     Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
00163   }
00164 }
00165 
00166 void X86Subtarget::AutoDetectSubtargetFeatures() {
00167   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
00168   union {
00169     unsigned u[3];
00170     char     c[12];
00171   } text;
00172   
00173   if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
00174     return;
00175 
00176   X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
00177   
00178   if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
00179   if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
00180   if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
00181   if (ECX & 0x1)         X86SSELevel = SSE3;
00182   if ((ECX >> 9)  & 0x1) X86SSELevel = SSSE3;
00183   if ((ECX >> 19) & 0x1) X86SSELevel = SSE41;
00184   if ((ECX >> 20) & 0x1) X86SSELevel = SSE42;
00185 
00186   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
00187   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
00188   if (IsIntel || IsAMD) {
00189     // Determine if bit test memory instructions are slow.
00190     unsigned Family = 0;
00191     unsigned Model  = 0;
00192     DetectFamilyModel(EAX, Family, Model);
00193     IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
00194 
00195     X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
00196     HasX86_64 = (EDX >> 29) & 0x1;
00197   }
00198 }
00199 
00200 static const char *GetCurrentX86CPU() {
00201   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
00202   if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
00203     return "generic";
00204   unsigned Family = 0;
00205   unsigned Model  = 0;
00206   DetectFamilyModel(EAX, Family, Model);
00207 
00208   X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
00209   bool Em64T = (EDX >> 29) & 0x1;
00210 
00211   union {
00212     unsigned u[3];
00213     char     c[12];
00214   } text;
00215 
00216   X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
00217   if (memcmp(text.c, "GenuineIntel", 12) == 0) {
00218     switch (Family) {
00219       case 3:
00220         return "i386";
00221       case 4:
00222         return "i486";
00223       case 5:
00224         switch (Model) {
00225         case 4:  return "pentium-mmx";
00226         default: return "pentium";
00227         }
00228       case 6:
00229         switch (Model) {
00230         case 1:  return "pentiumpro";
00231         case 3:
00232         case 5:
00233         case 6:  return "pentium2";
00234         case 7:
00235         case 8:
00236         case 10:
00237         case 11: return "pentium3";
00238         case 9:
00239         case 13: return "pentium-m";
00240         case 14: return "yonah";
00241         case 15:
00242         case 22: // Celeron M 540
00243           return "core2";
00244         case 23: // 45nm: Penryn , Wolfdale, Yorkfield (XE)
00245           return "penryn";
00246         default: return "i686";
00247         }
00248       case 15: {
00249         switch (Model) {
00250         case 3:  
00251         case 4:
00252         case 6: // same as 4, but 65nm
00253           return (Em64T) ? "nocona" : "prescott";
00254         case 26:
00255           return "corei7";
00256         case 28:
00257           return "atom";
00258         default:
00259           return (Em64T) ? "x86-64" : "pentium4";
00260         }
00261       }
00262         
00263     default:
00264       return "generic";
00265     }
00266   } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
00267     // FIXME: this poorly matches the generated SubtargetFeatureKV table.  There
00268     // appears to be no way to generate the wide variety of AMD-specific targets
00269     // from the information returned from CPUID.
00270     switch (Family) {
00271       case 4:
00272         return "i486";
00273       case 5:
00274         switch (Model) {
00275         case 6:
00276         case 7:  return "k6";
00277         case 8:  return "k6-2";
00278         case 9:
00279         case 13: return "k6-3";
00280         default: return "pentium";
00281         }
00282       case 6:
00283         switch (Model) {
00284         case 4:  return "athlon-tbird";
00285         case 6:
00286         case 7:
00287         case 8:  return "athlon-mp";
00288         case 10: return "athlon-xp";
00289         default: return "athlon";
00290         }
00291       case 15:
00292         switch (Model) {
00293         case 1:  return "opteron";
00294         case 5:  return "athlon-fx"; // also opteron
00295         default: return "athlon64";
00296         }
00297     default:
00298       return "generic";
00299     }
00300   } else {
00301     return "generic";
00302   }
00303 }
00304 
00305 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
00306   : AsmFlavor(AsmWriterFlavor)
00307   , PICStyle(PICStyles::None)
00308   , X86SSELevel(NoMMXSSE)
00309   , X863DNowLevel(NoThreeDNow)
00310   , HasX86_64(false)
00311   , IsBTMemSlow(false)
00312   , DarwinVers(0)
00313   , IsLinux(false)
00314   , stackAlignment(8)
00315   // FIXME: this is a known good value for Yonah. How about others?
00316   , MaxInlineSizeThreshold(128)
00317   , Is64Bit(is64Bit)
00318   , TargetType(isELF) { // Default to ELF unless otherwise specified.
00319     
00320   // Determine default and user specified characteristics
00321   if (!FS.empty()) {
00322     // If feature string is not empty, parse features string.
00323     std::string CPU = GetCurrentX86CPU();
00324     ParseSubtargetFeatures(FS, CPU);
00325     // All X86-64 CPUs also have SSE2, however user might request no SSE via 
00326     // -mattr, so don't force SSELevel here.
00327   } else {
00328     // Otherwise, use CPUID to auto-detect feature set.
00329     AutoDetectSubtargetFeatures();
00330     // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
00331     if (Is64Bit && X86SSELevel < SSE2)
00332       X86SSELevel = SSE2;
00333   }
00334 
00335   // If requesting codegen for X86-64, make sure that 64-bit features
00336   // are enabled.
00337   if (Is64Bit)
00338     HasX86_64 = true;
00339 
00340   DOUT << "Subtarget features: SSELevel " << X86SSELevel
00341        << ", 3DNowLevel " << X863DNowLevel
00342        << ", 64bit " << HasX86_64 << "\n";
00343   assert((!Is64Bit || HasX86_64) &&
00344          "64-bit code requested on a subtarget that doesn't support it!");
00345 
00346   // Set the boolean corresponding to the current target triple, or the default
00347   // if one cannot be determined, to true.
00348   const std::string& TT = M.getTargetTriple();
00349   if (TT.length() > 5) {
00350     size_t Pos;
00351     if ((Pos = TT.find("-darwin")) != std::string::npos) {
00352       TargetType = isDarwin;
00353       
00354       // Compute the darwin version number.
00355       if (isdigit(TT[Pos+7]))
00356         DarwinVers = atoi(&TT[Pos+7]);
00357       else
00358         DarwinVers = 8;  // Minimum supported darwin is Tiger.
00359     } else if (TT.find("linux") != std::string::npos) {
00360       // Linux doesn't imply ELF, but we don't currently support anything else.
00361       TargetType = isELF;
00362       IsLinux = true;
00363     } else if (TT.find("cygwin") != std::string::npos) {
00364       TargetType = isCygwin;
00365     } else if (TT.find("mingw") != std::string::npos) {
00366       TargetType = isMingw;
00367     } else if (TT.find("win32") != std::string::npos) {
00368       TargetType = isWindows;
00369     } else if (TT.find("windows") != std::string::npos) {
00370       TargetType = isWindows;
00371     }
00372   } else if (TT.empty()) {
00373 #if defined(__CYGWIN__)
00374     TargetType = isCygwin;
00375 #elif defined(__MINGW32__) || defined(__MINGW64__)
00376     TargetType = isMingw;
00377 #elif defined(__APPLE__)
00378     TargetType = isDarwin;
00379 #if __APPLE_CC__ > 5400
00380     DarwinVers = 9;  // GCC 5400+ is Leopard.
00381 #else
00382     DarwinVers = 8;  // Minimum supported darwin is Tiger.
00383 #endif
00384     
00385 #elif defined(_WIN32) || defined(_WIN64)
00386     TargetType = isWindows;
00387 #elif defined(__linux__)
00388     // Linux doesn't imply ELF, but we don't currently support anything else.
00389     TargetType = isELF;
00390     IsLinux = true;
00391 #endif
00392   }
00393 
00394   // If the asm syntax hasn't been overridden on the command line, use whatever
00395   // the target wants.
00396   if (AsmFlavor == X86Subtarget::Unset) {
00397     AsmFlavor = (TargetType == isWindows)
00398       ? X86Subtarget::Intel : X86Subtarget::ATT;
00399   }
00400 
00401   // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
00402   // bit targets.
00403   if (TargetType == isDarwin || Is64Bit)
00404     stackAlignment = 16;
00405 
00406   if (StackAlignment)
00407     stackAlignment = StackAlignment;
00408 }