db9a09f76dadd7efdf8c5c61ba333ee7d64d0666
[oota-llvm.git] / lib / Target / X86 / X86Subtarget.cpp
1 //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
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 TargetSubtarget.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "X86GenSubtarget.inc"
18 #include "llvm/Module.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetOptions.h"
23 using namespace llvm;
24
25 #if defined(_MSC_VER)
26     #include <intrin.h>
27 #endif
28
29 static cl::opt<X86Subtarget::AsmWriterFlavorTy>
30 AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::Unset),
31   cl::desc("Choose style of code to emit from X86 backend:"),
32   cl::values(
33     clEnumValN(X86Subtarget::ATT,   "att",   "Emit AT&T-style assembly"),
34     clEnumValN(X86Subtarget::Intel, "intel", "Emit Intel-style assembly"),
35     clEnumValEnd));
36
37 bool X86Subtarget::isPICStyleStubPIC(const TargetMachine &TM) const {
38   return PICStyle == PICStyles::Stub &&
39          TM.getRelocationModel() == Reloc::PIC_;
40 }
41
42 bool X86Subtarget::isPICStyleStubNoDynamic(const TargetMachine &TM) const {
43   return PICStyle == PICStyles::Stub &&
44          TM.getRelocationModel() == Reloc::DynamicNoPIC;
45 }
46
47
48
49 /// ClassifyGlobalReference - Classify a global variable reference for the
50 /// current subtarget according to how we should reference it in a non-pcrel
51 /// context.
52 unsigned char X86Subtarget::
53 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
54   // DLLImport only exists on windows, it is implemented as a load from a
55   // DLLIMPORT stub.
56   if (GV->hasDLLImportLinkage())
57     return X86II::MO_DLLIMPORT;
58
59   // X86-64 in PIC mode.
60   if (isPICStyleRIPRel()) {
61     // Large model never uses stubs.
62     if (TM.getCodeModel() == CodeModel::Large)
63       return X86II::MO_NO_FLAG;
64       
65       if (isTargetDarwin()) {
66         // If symbol visibility is hidden, the extra load is not needed if
67         // target is x86-64 or the symbol is definitely defined in the current
68         // translation unit.
69         if (GV->hasDefaultVisibility() &&
70             (GV->isDeclaration() || GV->isWeakForLinker()))
71           return X86II::MO_GOTPCREL;
72       } else {
73         assert(isTargetELF() && "Unknown rip-relative target");
74
75         // Extra load is needed for all externally visible.
76         if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
77           return X86II::MO_GOTPCREL;
78       }
79
80     return X86II::MO_NO_FLAG;
81   }
82   
83   if (isPICStyleGOT()) {   // 32-bit ELF targets.
84     // Extra load is needed for all externally visible.
85     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
86       return X86II::MO_GOTOFF;
87     return X86II::MO_GOT;
88   }
89   
90   if (isPICStyleStubPIC(TM)) {  // Darwin/32 in PIC mode.
91     // Determine whether we have a stub reference and/or whether the reference
92     // is relative to the PIC base or not.
93     
94     // If this is a strong reference to a definition, it is definitely not
95     // through a stub.
96     if (!GV->isDeclaration() && !GV->isWeakForLinker())
97       return X86II::MO_PIC_BASE_OFFSET;
98
99     // Unless we have a symbol with hidden visibility, we have to go through a
100     // normal $non_lazy_ptr stub because this symbol might be resolved late.
101     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
102       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
103     
104     // If symbol visibility is hidden, we have a stub for common symbol
105     // references and external declarations.
106     if (GV->isDeclaration() || GV->hasCommonLinkage()) {
107       // Hidden $non_lazy_ptr reference.
108       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
109     }
110     
111     // Otherwise, no stub.
112     return X86II::MO_PIC_BASE_OFFSET;
113   }
114   
115   if (isPICStyleStubNoDynamic(TM)) {  // Darwin/32 in -mdynamic-no-pic mode.
116     // Determine whether we have a stub reference.
117     
118     // If this is a strong reference to a definition, it is definitely not
119     // through a stub.
120     if (!GV->isDeclaration() && !GV->isWeakForLinker())
121       return X86II::MO_NO_FLAG;
122     
123     // Unless we have a symbol with hidden visibility, we have to go through a
124     // normal $non_lazy_ptr stub because this symbol might be resolved late.
125     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
126       return X86II::MO_DARWIN_NONLAZY;
127     
128     // If symbol visibility is hidden, we have a stub for common symbol
129     // references and external declarations.
130     if (GV->isDeclaration() || GV->hasCommonLinkage()) {
131       // Hidden $non_lazy_ptr reference.
132       return X86II::MO_DARWIN_HIDDEN_NONLAZY;
133     }
134     
135     // Otherwise, no stub.
136     return X86II::MO_NO_FLAG;
137   }
138   
139   // Direct static reference to global.
140   return X86II::MO_NO_FLAG;
141 }
142
143
144 /// getBZeroEntry - This function returns the name of a function which has an
145 /// interface like the non-standard bzero function, if such a function exists on
146 /// the current subtarget and it is considered prefereable over memset with zero
147 /// passed as the second argument. Otherwise it returns null.
148 const char *X86Subtarget::getBZeroEntry() const {
149   // Darwin 10 has a __bzero entry point for this purpose.
150   if (getDarwinVers() >= 10)
151     return "__bzero";
152
153   return 0;
154 }
155
156 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
157 /// to immediate address.
158 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
159   if (Is64Bit)
160     return false;
161   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
162 }
163
164 /// getSpecialAddressLatency - For targets where it is beneficial to
165 /// backschedule instructions that compute addresses, return a value
166 /// indicating the number of scheduling cycles of backscheduling that
167 /// should be attempted.
168 unsigned X86Subtarget::getSpecialAddressLatency() const {
169   // For x86 out-of-order targets, back-schedule address computations so
170   // that loads and stores aren't blocked.
171   // This value was chosen arbitrarily.
172   return 200;
173 }
174
175 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
176 /// specified arguments.  If we can't run cpuid on the host, return true.
177 bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
178                           unsigned *rECX, unsigned *rEDX) {
179 #if defined(__x86_64__) || defined(_M_AMD64)
180   #if defined(__GNUC__)
181     // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
182     asm ("movq\t%%rbx, %%rsi\n\t"
183          "cpuid\n\t"
184          "xchgq\t%%rbx, %%rsi\n\t"
185          : "=a" (*rEAX),
186            "=S" (*rEBX),
187            "=c" (*rECX),
188            "=d" (*rEDX)
189          :  "a" (value));
190     return false;
191   #elif defined(_MSC_VER)
192     int registers[4];
193     __cpuid(registers, value);
194     *rEAX = registers[0];
195     *rEBX = registers[1];
196     *rECX = registers[2];
197     *rEDX = registers[3];
198     return false;
199   #endif
200 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
201   #if defined(__GNUC__)
202     asm ("movl\t%%ebx, %%esi\n\t"
203          "cpuid\n\t"
204          "xchgl\t%%ebx, %%esi\n\t"
205          : "=a" (*rEAX),
206            "=S" (*rEBX),
207            "=c" (*rECX),
208            "=d" (*rEDX)
209          :  "a" (value));
210     return false;
211   #elif defined(_MSC_VER)
212     __asm {
213       mov   eax,value
214       cpuid
215       mov   esi,rEAX
216       mov   dword ptr [esi],eax
217       mov   esi,rEBX
218       mov   dword ptr [esi],ebx
219       mov   esi,rECX
220       mov   dword ptr [esi],ecx
221       mov   esi,rEDX
222       mov   dword ptr [esi],edx
223     }
224     return false;
225   #endif
226 #endif
227   return true;
228 }
229
230 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
231   Family = (EAX >> 8) & 0xf; // Bits 8 - 11
232   Model  = (EAX >> 4) & 0xf; // Bits 4 - 7
233   if (Family == 6 || Family == 0xf) {
234     if (Family == 0xf)
235       // Examine extended family ID if family ID is F.
236       Family += (EAX >> 20) & 0xff;    // Bits 20 - 27
237     // Examine extended model ID if family ID is 6 or F.
238     Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
239   }
240 }
241
242 void X86Subtarget::AutoDetectSubtargetFeatures() {
243   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
244   union {
245     unsigned u[3];
246     char     c[12];
247   } text;
248   
249   if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
250     return;
251
252   X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
253   
254   if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
255   if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
256   if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
257   if (ECX & 0x1)         X86SSELevel = SSE3;
258   if ((ECX >> 9)  & 0x1) X86SSELevel = SSSE3;
259   if ((ECX >> 19) & 0x1) X86SSELevel = SSE41;
260   if ((ECX >> 20) & 0x1) X86SSELevel = SSE42;
261
262   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
263   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
264
265   HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
266   HasAVX = ((ECX >> 28) & 0x1);
267
268   if (IsIntel || IsAMD) {
269     // Determine if bit test memory instructions are slow.
270     unsigned Family = 0;
271     unsigned Model  = 0;
272     DetectFamilyModel(EAX, Family, Model);
273     IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
274
275     X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
276     HasX86_64 = (EDX >> 29) & 0x1;
277     HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
278     HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
279   }
280 }
281
282 static const char *GetCurrentX86CPU() {
283   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
284   if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
285     return "generic";
286   unsigned Family = 0;
287   unsigned Model  = 0;
288   DetectFamilyModel(EAX, Family, Model);
289
290   X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
291   bool Em64T = (EDX >> 29) & 0x1;
292   bool HasSSE3 = (ECX & 0x1);
293
294   union {
295     unsigned u[3];
296     char     c[12];
297   } text;
298
299   X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
300   if (memcmp(text.c, "GenuineIntel", 12) == 0) {
301     switch (Family) {
302       case 3:
303         return "i386";
304       case 4:
305         return "i486";
306       case 5:
307         switch (Model) {
308         case 4:  return "pentium-mmx";
309         default: return "pentium";
310         }
311       case 6:
312         switch (Model) {
313         case 1:  return "pentiumpro";
314         case 3:
315         case 5:
316         case 6:  return "pentium2";
317         case 7:
318         case 8:
319         case 10:
320         case 11: return "pentium3";
321         case 9:
322         case 13: return "pentium-m";
323         case 14: return "yonah";
324         case 15:
325         case 22: // Celeron M 540
326           return "core2";
327         case 23: // 45nm: Penryn , Wolfdale, Yorkfield (XE)
328           return "penryn";
329         default: return "i686";
330         }
331       case 15: {
332         switch (Model) {
333         case 3:  
334         case 4:
335         case 6: // same as 4, but 65nm
336           return (Em64T) ? "nocona" : "prescott";
337         case 26:
338           return "corei7";
339         case 28:
340           return "atom";
341         default:
342           return (Em64T) ? "x86-64" : "pentium4";
343         }
344       }
345         
346     default:
347       return "generic";
348     }
349   } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
350     // FIXME: this poorly matches the generated SubtargetFeatureKV table.  There
351     // appears to be no way to generate the wide variety of AMD-specific targets
352     // from the information returned from CPUID.
353     switch (Family) {
354       case 4:
355         return "i486";
356       case 5:
357         switch (Model) {
358         case 6:
359         case 7:  return "k6";
360         case 8:  return "k6-2";
361         case 9:
362         case 13: return "k6-3";
363         default: return "pentium";
364         }
365       case 6:
366         switch (Model) {
367         case 4:  return "athlon-tbird";
368         case 6:
369         case 7:
370         case 8:  return "athlon-mp";
371         case 10: return "athlon-xp";
372         default: return "athlon";
373         }
374       case 15:
375         if (HasSSE3) {
376           switch (Model) {
377           default: return "k8-sse3";
378           }
379         } else {
380           switch (Model) {
381           case 1:  return "opteron";
382           case 5:  return "athlon-fx"; // also opteron
383           default: return "athlon64";
384           }
385         }
386       case 16:
387         switch (Model) {
388         default: return "amdfam10";
389         }
390     default:
391       return "generic";
392     }
393   } else {
394     return "generic";
395   }
396 }
397
398 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
399   : AsmFlavor(AsmWriterFlavor)
400   , PICStyle(PICStyles::None)
401   , X86SSELevel(NoMMXSSE)
402   , X863DNowLevel(NoThreeDNow)
403   , HasX86_64(false)
404   , HasSSE4A(false)
405   , HasAVX(false)
406   , HasFMA3(false)
407   , HasFMA4(false)
408   , IsBTMemSlow(false)
409   , DarwinVers(0)
410   , IsLinux(false)
411   , stackAlignment(8)
412   // FIXME: this is a known good value for Yonah. How about others?
413   , MaxInlineSizeThreshold(128)
414   , Is64Bit(is64Bit)
415   , TargetType(isELF) { // Default to ELF unless otherwise specified.
416
417   // default to hard float ABI
418   if (FloatABIType == FloatABI::Default)
419     FloatABIType = FloatABI::Hard;
420     
421   // Determine default and user specified characteristics
422   if (!FS.empty()) {
423     // If feature string is not empty, parse features string.
424     std::string CPU = GetCurrentX86CPU();
425     ParseSubtargetFeatures(FS, CPU);
426     // All X86-64 CPUs also have SSE2, however user might request no SSE via 
427     // -mattr, so don't force SSELevel here.
428   } else {
429     // Otherwise, use CPUID to auto-detect feature set.
430     AutoDetectSubtargetFeatures();
431     // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
432     if (Is64Bit && X86SSELevel < SSE2)
433       X86SSELevel = SSE2;
434   }
435
436   // If requesting codegen for X86-64, make sure that 64-bit features
437   // are enabled.
438   if (Is64Bit)
439     HasX86_64 = true;
440
441   DOUT << "Subtarget features: SSELevel " << X86SSELevel
442        << ", 3DNowLevel " << X863DNowLevel
443        << ", 64bit " << HasX86_64 << "\n";
444   assert((!Is64Bit || HasX86_64) &&
445          "64-bit code requested on a subtarget that doesn't support it!");
446
447   // Set the boolean corresponding to the current target triple, or the default
448   // if one cannot be determined, to true.
449   const std::string& TT = M.getTargetTriple();
450   if (TT.length() > 5) {
451     size_t Pos;
452     if ((Pos = TT.find("-darwin")) != std::string::npos) {
453       TargetType = isDarwin;
454       
455       // Compute the darwin version number.
456       if (isdigit(TT[Pos+7]))
457         DarwinVers = atoi(&TT[Pos+7]);
458       else
459         DarwinVers = 8;  // Minimum supported darwin is Tiger.
460     } else if (TT.find("linux") != std::string::npos) {
461       // Linux doesn't imply ELF, but we don't currently support anything else.
462       TargetType = isELF;
463       IsLinux = true;
464     } else if (TT.find("cygwin") != std::string::npos) {
465       TargetType = isCygwin;
466     } else if (TT.find("mingw") != std::string::npos) {
467       TargetType = isMingw;
468     } else if (TT.find("win32") != std::string::npos) {
469       TargetType = isWindows;
470     } else if (TT.find("windows") != std::string::npos) {
471       TargetType = isWindows;
472     }
473     else if (TT.find("-cl") != std::string::npos) {
474       TargetType = isDarwin;
475       DarwinVers = 9;
476     }
477   } else if (TT.empty()) {
478 #if defined(__CYGWIN__)
479     TargetType = isCygwin;
480 #elif defined(__MINGW32__) || defined(__MINGW64__)
481     TargetType = isMingw;
482 #elif defined(__APPLE__)
483     TargetType = isDarwin;
484 #if __APPLE_CC__ > 5400
485     DarwinVers = 9;  // GCC 5400+ is Leopard.
486 #else
487     DarwinVers = 8;  // Minimum supported darwin is Tiger.
488 #endif
489     
490 #elif defined(_WIN32) || defined(_WIN64)
491     TargetType = isWindows;
492 #elif defined(__linux__)
493     // Linux doesn't imply ELF, but we don't currently support anything else.
494     TargetType = isELF;
495     IsLinux = true;
496 #endif
497   }
498
499   // If the asm syntax hasn't been overridden on the command line, use whatever
500   // the target wants.
501   if (AsmFlavor == X86Subtarget::Unset) {
502     AsmFlavor = (TargetType == isWindows)
503       ? X86Subtarget::Intel : X86Subtarget::ATT;
504   }
505
506   // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
507   // bit targets.
508   if (TargetType == isDarwin || Is64Bit)
509     stackAlignment = 16;
510
511   if (StackAlignment)
512     stackAlignment = StackAlignment;
513 }