Compute feature bits at time of MCSubtargetInfo initialization.
[oota-llvm.git] / lib / Target / X86 / X86Subtarget.cpp
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 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/GlobalValue.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/SmallVector.h"
23
24 #define GET_SUBTARGETINFO_CTOR
25 #define GET_SUBTARGETINFO_MC_DESC
26 #define GET_SUBTARGETINFO_TARGET_DESC
27 #include "X86GenSubtargetInfo.inc"
28
29 using namespace llvm;
30
31 #if defined(_MSC_VER)
32 #include <intrin.h>
33 #endif
34
35 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
36 /// current subtarget according to how we should reference it in a non-pcrel
37 /// context.
38 unsigned char X86Subtarget::
39 ClassifyBlockAddressReference() const {
40   if (isPICStyleGOT())    // 32-bit ELF targets.
41     return X86II::MO_GOTOFF;
42   
43   if (isPICStyleStubPIC())   // Darwin/32 in PIC mode.
44     return X86II::MO_PIC_BASE_OFFSET;
45   
46   // Direct static reference to label.
47   return X86II::MO_NO_FLAG;
48 }
49
50 /// ClassifyGlobalReference - Classify a global variable reference for the
51 /// current subtarget according to how we should reference it in a non-pcrel
52 /// context.
53 unsigned char X86Subtarget::
54 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
55   // DLLImport only exists on windows, it is implemented as a load from a
56   // DLLIMPORT stub.
57   if (GV->hasDLLImportLinkage())
58     return X86II::MO_DLLIMPORT;
59
60   // Determine whether this is a reference to a definition or a declaration.
61   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
62   // load from stub.
63   bool isDecl = GV->hasAvailableExternallyLinkage();
64   if (GV->isDeclaration() && !GV->isMaterializable())
65     isDecl = true;
66
67   // X86-64 in PIC mode.
68   if (isPICStyleRIPRel()) {
69     // Large model never uses stubs.
70     if (TM.getCodeModel() == CodeModel::Large)
71       return X86II::MO_NO_FLAG;
72       
73     if (isTargetDarwin()) {
74       // If symbol visibility is hidden, the extra load is not needed if
75       // target is x86-64 or the symbol is definitely defined in the current
76       // translation unit.
77       if (GV->hasDefaultVisibility() &&
78           (isDecl || GV->isWeakForLinker()))
79         return X86II::MO_GOTPCREL;
80     } else if (!isTargetWin64()) {
81       assert(isTargetELF() && "Unknown rip-relative target");
82
83       // Extra load is needed for all externally visible.
84       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
85         return X86II::MO_GOTPCREL;
86     }
87
88     return X86II::MO_NO_FLAG;
89   }
90   
91   if (isPICStyleGOT()) {   // 32-bit ELF targets.
92     // Extra load is needed for all externally visible.
93     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
94       return X86II::MO_GOTOFF;
95     return X86II::MO_GOT;
96   }
97   
98   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
99     // Determine whether we have a stub reference and/or whether the reference
100     // is relative to the PIC base or not.
101     
102     // If this is a strong reference to a definition, it is definitely not
103     // through a stub.
104     if (!isDecl && !GV->isWeakForLinker())
105       return X86II::MO_PIC_BASE_OFFSET;
106
107     // Unless we have a symbol with hidden visibility, we have to go through a
108     // normal $non_lazy_ptr stub because this symbol might be resolved late.
109     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
110       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
111     
112     // If symbol visibility is hidden, we have a stub for common symbol
113     // references and external declarations.
114     if (isDecl || GV->hasCommonLinkage()) {
115       // Hidden $non_lazy_ptr reference.
116       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
117     }
118     
119     // Otherwise, no stub.
120     return X86II::MO_PIC_BASE_OFFSET;
121   }
122   
123   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
124     // Determine whether we have a stub reference.
125     
126     // If this is a strong reference to a definition, it is definitely not
127     // through a stub.
128     if (!isDecl && !GV->isWeakForLinker())
129       return X86II::MO_NO_FLAG;
130     
131     // Unless we have a symbol with hidden visibility, we have to go through a
132     // normal $non_lazy_ptr stub because this symbol might be resolved late.
133     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
134       return X86II::MO_DARWIN_NONLAZY;
135
136     // Otherwise, no stub.
137     return X86II::MO_NO_FLAG;
138   }
139   
140   // Direct static reference to global.
141   return X86II::MO_NO_FLAG;
142 }
143
144
145 /// getBZeroEntry - This function returns the name of a function which has an
146 /// interface like the non-standard bzero function, if such a function exists on
147 /// the current subtarget and it is considered prefereable over memset with zero
148 /// passed as the second argument. Otherwise it returns null.
149 const char *X86Subtarget::getBZeroEntry() const {
150   // Darwin 10 has a __bzero entry point for this purpose.
151   if (getTargetTriple().isMacOSX() &&
152       !getTargetTriple().isMacOSXVersionLT(10, 6))
153     return "__bzero";
154
155   return 0;
156 }
157
158 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
159 /// to immediate address.
160 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
161   if (Is64Bit)
162     return false;
163   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
164 }
165
166 /// getSpecialAddressLatency - For targets where it is beneficial to
167 /// backschedule instructions that compute addresses, return a value
168 /// indicating the number of scheduling cycles of backscheduling that
169 /// should be attempted.
170 unsigned X86Subtarget::getSpecialAddressLatency() const {
171   // For x86 out-of-order targets, back-schedule address computations so
172   // that loads and stores aren't blocked.
173   // This value was chosen arbitrarily.
174   return 200;
175 }
176
177 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
178 /// specified arguments.  If we can't run cpuid on the host, return true.
179 static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
180                             unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
181 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
182   #if defined(__GNUC__)
183     // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
184     asm ("movq\t%%rbx, %%rsi\n\t"
185          "cpuid\n\t"
186          "xchgq\t%%rbx, %%rsi\n\t"
187          : "=a" (*rEAX),
188            "=S" (*rEBX),
189            "=c" (*rECX),
190            "=d" (*rEDX)
191          :  "a" (value));
192     return false;
193   #elif defined(_MSC_VER)
194     int registers[4];
195     __cpuid(registers, value);
196     *rEAX = registers[0];
197     *rEBX = registers[1];
198     *rECX = registers[2];
199     *rEDX = registers[3];
200     return false;
201   #endif
202 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
203   #if defined(__GNUC__)
204     asm ("movl\t%%ebx, %%esi\n\t"
205          "cpuid\n\t"
206          "xchgl\t%%ebx, %%esi\n\t"
207          : "=a" (*rEAX),
208            "=S" (*rEBX),
209            "=c" (*rECX),
210            "=d" (*rEDX)
211          :  "a" (value));
212     return false;
213   #elif defined(_MSC_VER)
214     __asm {
215       mov   eax,value
216       cpuid
217       mov   esi,rEAX
218       mov   dword ptr [esi],eax
219       mov   esi,rEBX
220       mov   dword ptr [esi],ebx
221       mov   esi,rECX
222       mov   dword ptr [esi],ecx
223       mov   esi,rEDX
224       mov   dword ptr [esi],edx
225     }
226     return false;
227   #endif
228 #endif
229   return true;
230 }
231
232 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
233   Family = (EAX >> 8) & 0xf; // Bits 8 - 11
234   Model  = (EAX >> 4) & 0xf; // Bits 4 - 7
235   if (Family == 6 || Family == 0xf) {
236     if (Family == 0xf)
237       // Examine extended family ID if family ID is F.
238       Family += (EAX >> 20) & 0xff;    // Bits 20 - 27
239     // Examine extended model ID if family ID is 6 or F.
240     Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
241   }
242 }
243
244 void X86Subtarget::AutoDetectSubtargetFeatures() {
245   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
246   union {
247     unsigned u[3];
248     char     c[12];
249   } text;
250   
251   if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
252     return;
253
254   GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
255   
256   if ((EDX >> 15) & 1) HasCMov = true;
257   if ((EDX >> 23) & 1) X86SSELevel = MMX;
258   if ((EDX >> 25) & 1) X86SSELevel = SSE1;
259   if ((EDX >> 26) & 1) X86SSELevel = SSE2;
260   if (ECX & 0x1)       X86SSELevel = SSE3;
261   if ((ECX >> 9)  & 1) X86SSELevel = SSSE3;
262   if ((ECX >> 19) & 1) X86SSELevel = SSE41;
263   if ((ECX >> 20) & 1) X86SSELevel = SSE42;
264   // FIXME: AVX codegen support is not ready.
265   //if ((ECX >> 28) & 1) { HasAVX = true; X86SSELevel = NoMMXSSE; }
266
267   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
268   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
269
270   HasCLMUL = IsIntel && ((ECX >> 1) & 0x1);
271   HasFMA3  = IsIntel && ((ECX >> 12) & 0x1);
272   HasPOPCNT = IsIntel && ((ECX >> 23) & 0x1);
273   HasAES   = IsIntel && ((ECX >> 25) & 0x1);
274
275   if (IsIntel || IsAMD) {
276     // Determine if bit test memory instructions are slow.
277     unsigned Family = 0;
278     unsigned Model  = 0;
279     DetectFamilyModel(EAX, Family, Model);
280     IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
281     // If it's Nehalem, unaligned memory access is fast.
282     if (Family == 15 && Model == 26)
283       IsUAMemFast = true;
284
285     GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
286     HasX86_64 = (EDX >> 29) & 0x1;
287     HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
288     HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
289   }
290 }
291
292 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
293                            const std::string &FS, 
294                            bool is64Bit, unsigned StackAlignOverride)
295   : X86GenSubtargetInfo(TT, CPU, FS)
296   , PICStyle(PICStyles::None)
297   , X86SSELevel(NoMMXSSE)
298   , X863DNowLevel(NoThreeDNow)
299   , HasCMov(false)
300   , HasX86_64(false)
301   , HasPOPCNT(false)
302   , HasSSE4A(false)
303   , HasAVX(false)
304   , HasAES(false)
305   , HasCLMUL(false)
306   , HasFMA3(false)
307   , HasFMA4(false)
308   , IsBTMemSlow(false)
309   , IsUAMemFast(false)
310   , HasVectorUAMem(false)
311   , stackAlignment(8)
312   // FIXME: this is a known good value for Yonah. How about others?
313   , MaxInlineSizeThreshold(128)
314   , TargetTriple(TT)
315   , Is64Bit(is64Bit) {
316
317   // Determine default and user specified characteristics
318   if (!CPU.empty() || !FS.empty()) {
319     // If feature string is not empty, parse features string.
320     std::string CPUName = CPU;
321     if (CPUName.empty())
322       CPUName = sys::getHostCPUName();
323     ParseSubtargetFeatures(CPUName, FS);
324     // All X86-64 CPUs also have SSE2, however user might request no SSE via 
325     // -mattr, so don't force SSELevel here.
326     if (HasAVX)
327       X86SSELevel = NoMMXSSE;
328   } else {
329     // Otherwise, use CPUID to auto-detect feature set.
330     AutoDetectSubtargetFeatures();
331     // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
332     if (Is64Bit && !HasAVX && X86SSELevel < SSE2)
333       X86SSELevel = SSE2;
334   }
335
336   // If requesting codegen for X86-64, make sure that 64-bit features
337   // are enabled.
338   if (Is64Bit) {
339     HasX86_64 = true;
340
341     // All 64-bit cpus have cmov support.
342     HasCMov = true;
343   }
344     
345   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
346                << ", 3DNowLevel " << X863DNowLevel
347                << ", 64bit " << HasX86_64 << "\n");
348   assert((!Is64Bit || HasX86_64) &&
349          "64-bit code requested on a subtarget that doesn't support it!");
350
351   // Stack alignment is 16 bytes on Darwin, FreeBSD, Linux and Solaris (both
352   // 32 and 64 bit) and for all 64-bit targets.
353   if (StackAlignOverride)
354     stackAlignment = StackAlignOverride;
355   else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
356            isTargetSolaris() || Is64Bit)
357     stackAlignment = 16;
358 }