Add Mode64Bit feature and sink it down to MC layer.
[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 (In64BitMode)
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 void X86Subtarget::AutoDetectSubtargetFeatures() {
178   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
179   union {
180     unsigned u[3];
181     char     c[12];
182   } text;
183   
184   if (X86_MC::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
185     return;
186
187   X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
188   
189   if ((EDX >> 15) & 1) HasCMov = true;
190   if ((EDX >> 23) & 1) X86SSELevel = MMX;
191   if ((EDX >> 25) & 1) X86SSELevel = SSE1;
192   if ((EDX >> 26) & 1) X86SSELevel = SSE2;
193   if (ECX & 0x1)       X86SSELevel = SSE3;
194   if ((ECX >> 9)  & 1) X86SSELevel = SSSE3;
195   if ((ECX >> 19) & 1) X86SSELevel = SSE41;
196   if ((ECX >> 20) & 1) X86SSELevel = SSE42;
197   // FIXME: AVX codegen support is not ready.
198   //if ((ECX >> 28) & 1) { HasAVX = true; X86SSELevel = NoMMXSSE; }
199
200   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
201   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
202
203   HasCLMUL = IsIntel && ((ECX >> 1) & 0x1);
204   HasFMA3  = IsIntel && ((ECX >> 12) & 0x1);
205   HasPOPCNT = IsIntel && ((ECX >> 23) & 0x1);
206   HasAES   = IsIntel && ((ECX >> 25) & 0x1);
207
208   if (IsIntel || IsAMD) {
209     // Determine if bit test memory instructions are slow.
210     unsigned Family = 0;
211     unsigned Model  = 0;
212     X86_MC::DetectFamilyModel(EAX, Family, Model);
213     IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
214     // If it's Nehalem, unaligned memory access is fast.
215     if (Family == 15 && Model == 26)
216       IsUAMemFast = true;
217
218     X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
219     HasX86_64 = (EDX >> 29) & 0x1;
220     HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
221     HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
222   }
223 }
224
225 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
226                            const std::string &FS, 
227                            unsigned StackAlignOverride)
228   : X86GenSubtargetInfo(TT, CPU, FS)
229   , PICStyle(PICStyles::None)
230   , X86SSELevel(NoMMXSSE)
231   , X863DNowLevel(NoThreeDNow)
232   , HasCMov(false)
233   , HasX86_64(false)
234   , HasPOPCNT(false)
235   , HasSSE4A(false)
236   , HasAVX(false)
237   , HasAES(false)
238   , HasCLMUL(false)
239   , HasFMA3(false)
240   , HasFMA4(false)
241   , IsBTMemSlow(false)
242   , IsUAMemFast(false)
243   , HasVectorUAMem(false)
244   , stackAlignment(8)
245   // FIXME: this is a known good value for Yonah. How about others?
246   , MaxInlineSizeThreshold(128)
247   , TargetTriple(TT)
248   , In64BitMode(false) {
249   // Insert the architecture feature derived from the target triple into the
250   // feature string. This is important for setting features that are implied
251   // based on the architecture version.
252   std::string ArchFS = X86_MC::ParseX86Triple(TT);
253   if (!FS.empty()) {
254     if (!ArchFS.empty())
255       ArchFS = ArchFS + "," + FS;
256     else
257       ArchFS = FS;
258   }
259
260   std::string CPUName = CPU;
261   if (CPUName.empty())
262     CPUName = sys::getHostCPUName();
263
264   // Determine default and user specified characteristics
265   if (!CPUName.empty() || !ArchFS.empty()) {
266     // If feature string is not empty, parse features string.
267     ParseSubtargetFeatures(CPUName, ArchFS);
268     // All X86-64 CPUs also have SSE2, however user might request no SSE via 
269     // -mattr, so don't force SSELevel here.
270     if (HasAVX)
271       X86SSELevel = NoMMXSSE;
272   } else {
273     // Otherwise, use CPUID to auto-detect feature set.
274     AutoDetectSubtargetFeatures();
275
276     // If CPU is 64-bit capable, default to 64-bit mode if not specified.
277     In64BitMode = HasX86_64;
278
279     // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
280     if (In64BitMode && !HasAVX && X86SSELevel < SSE2)
281       X86SSELevel = SSE2;
282   }
283
284   // If requesting codegen for X86-64, make sure that 64-bit features
285   // are enabled.
286   // FIXME: Remove this feature since it's not actually being used.
287   if (In64BitMode) {
288     HasX86_64 = true;
289
290     // All 64-bit cpus have cmov support.
291     HasCMov = true;
292   }
293     
294   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
295                << ", 3DNowLevel " << X863DNowLevel
296                << ", 64bit " << HasX86_64 << "\n");
297   assert((!In64BitMode || HasX86_64) &&
298          "64-bit code requested on a subtarget that doesn't support it!");
299
300   // Stack alignment is 16 bytes on Darwin, FreeBSD, Linux and Solaris (both
301   // 32 and 64 bit) and for all 64-bit targets.
302   if (StackAlignOverride)
303     stackAlignment = StackAlignOverride;
304   else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
305            isTargetSolaris() || In64BitMode)
306     stackAlignment = 16;
307 }