9f05a2ba4498ed234fca2d47f366b40aab4a4a77
[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 #include "X86Subtarget.h"
15 #include "X86InstrInfo.h"
16 #include "X86TargetMachine.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Host.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetOptions.h"
27
28 #if defined(_MSC_VER)
29 #include <intrin.h>
30 #endif
31
32 using namespace llvm;
33
34 #define DEBUG_TYPE "subtarget"
35
36 #define GET_SUBTARGETINFO_TARGET_DESC
37 #define GET_SUBTARGETINFO_CTOR
38 #include "X86GenSubtargetInfo.inc"
39
40 // Temporary option to control early if-conversion for x86 while adding machine
41 // models.
42 static cl::opt<bool>
43 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
44                cl::desc("Enable early if-conversion on X86"));
45
46
47 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
48 /// current subtarget according to how we should reference it in a non-pcrel
49 /// context.
50 unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
51   if (isPICStyleGOT())    // 32-bit ELF targets.
52     return X86II::MO_GOTOFF;
53
54   if (isPICStyleStubPIC())   // Darwin/32 in PIC mode.
55     return X86II::MO_PIC_BASE_OFFSET;
56
57   // Direct static reference to label.
58   return X86II::MO_NO_FLAG;
59 }
60
61 /// ClassifyGlobalReference - Classify a global variable reference for the
62 /// current subtarget according to how we should reference it in a non-pcrel
63 /// context.
64 unsigned char X86Subtarget::
65 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
66   // DLLImport only exists on windows, it is implemented as a load from a
67   // DLLIMPORT stub.
68   if (GV->hasDLLImportStorageClass())
69     return X86II::MO_DLLIMPORT;
70
71   // Determine whether this is a reference to a definition or a declaration.
72   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
73   // load from stub.
74   bool isDecl = GV->hasAvailableExternallyLinkage();
75   if (GV->isDeclaration() && !GV->isMaterializable())
76     isDecl = true;
77
78   // X86-64 in PIC mode.
79   if (isPICStyleRIPRel()) {
80     // Large model never uses stubs.
81     if (TM.getCodeModel() == CodeModel::Large)
82       return X86II::MO_NO_FLAG;
83
84     if (isTargetDarwin()) {
85       // If symbol visibility is hidden, the extra load is not needed if
86       // target is x86-64 or the symbol is definitely defined in the current
87       // translation unit.
88       if (GV->hasDefaultVisibility() &&
89           (isDecl || GV->isWeakForLinker()))
90         return X86II::MO_GOTPCREL;
91     } else if (!isTargetWin64()) {
92       assert(isTargetELF() && "Unknown rip-relative target");
93
94       // Extra load is needed for all externally visible.
95       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
96         return X86II::MO_GOTPCREL;
97     }
98
99     return X86II::MO_NO_FLAG;
100   }
101
102   if (isPICStyleGOT()) {   // 32-bit ELF targets.
103     // Extra load is needed for all externally visible.
104     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
105       return X86II::MO_GOTOFF;
106     return X86II::MO_GOT;
107   }
108
109   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
110     // Determine whether we have a stub reference and/or whether the reference
111     // is relative to the PIC base or not.
112
113     // If this is a strong reference to a definition, it is definitely not
114     // through a stub.
115     if (!isDecl && !GV->isWeakForLinker())
116       return X86II::MO_PIC_BASE_OFFSET;
117
118     // Unless we have a symbol with hidden visibility, we have to go through a
119     // normal $non_lazy_ptr stub because this symbol might be resolved late.
120     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
121       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
122
123     // If symbol visibility is hidden, we have a stub for common symbol
124     // references and external declarations.
125     if (isDecl || GV->hasCommonLinkage()) {
126       // Hidden $non_lazy_ptr reference.
127       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
128     }
129
130     // Otherwise, no stub.
131     return X86II::MO_PIC_BASE_OFFSET;
132   }
133
134   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
135     // Determine whether we have a stub reference.
136
137     // If this is a strong reference to a definition, it is definitely not
138     // through a stub.
139     if (!isDecl && !GV->isWeakForLinker())
140       return X86II::MO_NO_FLAG;
141
142     // Unless we have a symbol with hidden visibility, we have to go through a
143     // normal $non_lazy_ptr stub because this symbol might be resolved late.
144     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
145       return X86II::MO_DARWIN_NONLAZY;
146
147     // Otherwise, no stub.
148     return X86II::MO_NO_FLAG;
149   }
150
151   // Direct static reference to global.
152   return X86II::MO_NO_FLAG;
153 }
154
155
156 /// getBZeroEntry - This function returns the name of a function which has an
157 /// interface like the non-standard bzero function, if such a function exists on
158 /// the current subtarget and it is considered prefereable over memset with zero
159 /// passed as the second argument. Otherwise it returns null.
160 const char *X86Subtarget::getBZeroEntry() const {
161   // Darwin 10 has a __bzero entry point for this purpose.
162   if (getTargetTriple().isMacOSX() &&
163       !getTargetTriple().isMacOSXVersionLT(10, 6))
164     return "__bzero";
165
166   return nullptr;
167 }
168
169 bool X86Subtarget::hasSinCos() const {
170   return getTargetTriple().isMacOSX() &&
171     !getTargetTriple().isMacOSXVersionLT(10, 9) &&
172     is64Bit();
173 }
174
175 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
176 /// to immediate address.
177 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
178   // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
179   // but WinCOFFObjectWriter::RecordRelocation cannot emit them.  Once it does,
180   // the following check for Win32 should be removed.
181   if (In64BitMode || isTargetWin32())
182     return false;
183   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
184 }
185
186 void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
187   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
188   Attribute CPUAttr =
189       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
190   Attribute FSAttr =
191       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
192   std::string CPU =
193       !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
194   std::string FS =
195       !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
196   if (!FS.empty()) {
197     initializeEnvironment();
198     resetSubtargetFeatures(CPU, FS);
199   }
200 }
201
202 void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
203   std::string CPUName = CPU;
204   if (CPUName.empty())
205     CPUName = "generic";
206
207   // Make sure 64-bit features are available in 64-bit mode. (But make sure
208   // SSE2 can be turned off explicitly.)
209   std::string FullFS = FS;
210   if (In64BitMode) {
211     if (!FullFS.empty())
212       FullFS = "+64bit,+sse2," + FullFS;
213     else
214       FullFS = "+64bit,+sse2";
215   }
216
217   // If feature string is not empty, parse features string.
218   ParseSubtargetFeatures(CPUName, FullFS);
219
220   // Make sure the right MCSchedModel is used.
221   InitCPUSchedModel(CPUName);
222
223   InstrItins = getInstrItineraryForCPU(CPUName);
224
225   // It's important to keep the MCSubtargetInfo feature bits in sync with
226   // target data structure which is shared with MC code emitter, etc.
227   if (In64BitMode)
228     ToggleFeature(X86::Mode64Bit);
229   else if (In32BitMode)
230     ToggleFeature(X86::Mode32Bit);
231   else if (In16BitMode)
232     ToggleFeature(X86::Mode16Bit);
233   else
234     llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
235
236   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
237                << ", 3DNowLevel " << X863DNowLevel
238                << ", 64bit " << HasX86_64 << "\n");
239   assert((!In64BitMode || HasX86_64) &&
240          "64-bit code requested on a subtarget that doesn't support it!");
241
242   // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
243   // 32 and 64 bit) and for all 64-bit targets.
244   if (StackAlignOverride)
245     stackAlignment = StackAlignOverride;
246   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
247            In64BitMode)
248     stackAlignment = 16;
249 }
250
251 void X86Subtarget::initializeEnvironment() {
252   X86SSELevel = NoMMXSSE;
253   X863DNowLevel = NoThreeDNow;
254   HasCMov = false;
255   HasX86_64 = false;
256   HasPOPCNT = false;
257   HasSSE4A = false;
258   HasAES = false;
259   HasPCLMUL = false;
260   HasFMA = false;
261   HasFMA4 = false;
262   HasXOP = false;
263   HasTBM = false;
264   HasMOVBE = false;
265   HasRDRAND = false;
266   HasF16C = false;
267   HasFSGSBase = false;
268   HasLZCNT = false;
269   HasBMI = false;
270   HasBMI2 = false;
271   HasRTM = false;
272   HasHLE = false;
273   HasERI = false;
274   HasCDI = false;
275   HasPFI = false;
276   HasDQI = false;
277   HasBWI = false;
278   HasVLX = false;
279   HasADX = false;
280   HasSHA = false;
281   HasSGX = false;
282   HasPRFCHW = false;
283   HasRDSEED = false;
284   IsBTMemSlow = false;
285   IsSHLDSlow = false;
286   IsUAMemFast = false;
287   HasVectorUAMem = false;
288   HasCmpxchg16b = false;
289   UseLeaForSP = false;
290   HasSlowDivide = false;
291   PadShortFunctions = false;
292   CallRegIndirect = false;
293   LEAUsesAG = false;
294   SlowLEA = false;
295   SlowIncDec = false;
296   stackAlignment = 4;
297   // FIXME: this is a known good value for Yonah. How about others?
298   MaxInlineSizeThreshold = 128;
299 }
300
301 static std::string computeDataLayout(const X86Subtarget &ST) {
302   // X86 is little endian
303   std::string Ret = "e";
304
305   Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
306   // X86 and x32 have 32 bit pointers.
307   if (ST.isTarget64BitILP32() || !ST.is64Bit())
308     Ret += "-p:32:32";
309
310   // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
311   if (ST.is64Bit() || ST.isOSWindows() || ST.isTargetNaCl())
312     Ret += "-i64:64";
313   else
314     Ret += "-f64:32:64";
315
316   // Some ABIs align long double to 128 bits, others to 32.
317   if (ST.isTargetNaCl())
318     ; // No f80
319   else if (ST.is64Bit() || ST.isTargetDarwin())
320     Ret += "-f80:128";
321   else
322     Ret += "-f80:32";
323
324   // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
325   if (ST.is64Bit())
326     Ret += "-n8:16:32:64";
327   else
328     Ret += "-n8:16:32";
329
330   // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
331   if (!ST.is64Bit() && ST.isOSWindows())  
332     Ret += "-S32";
333   else
334     Ret += "-S128";
335
336   return Ret;
337 }
338
339 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
340                                                             StringRef FS) {
341   initializeEnvironment();
342   resetSubtargetFeatures(CPU, FS);
343   return *this;
344 }
345
346 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
347                            const std::string &FS, X86TargetMachine &TM,
348                            unsigned StackAlignOverride)
349     : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
350       PICStyle(PICStyles::None), TargetTriple(TT),
351       StackAlignOverride(StackAlignOverride),
352       In64BitMode(TargetTriple.getArch() == Triple::x86_64),
353       In32BitMode(TargetTriple.getArch() == Triple::x86 &&
354                   TargetTriple.getEnvironment() != Triple::CODE16),
355       In16BitMode(TargetTriple.getArch() == Triple::x86 &&
356                   TargetTriple.getEnvironment() == Triple::CODE16),
357       DL(computeDataLayout(*this)), TSInfo(DL),
358       InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM),
359       FrameLowering(TargetFrameLowering::StackGrowsDown, getStackAlignment(),
360                     is64Bit() ? -8 : -4),
361       JITInfo(hasSSE1()) {
362   // Determine the PICStyle based on the target selected.
363   if (TM.getRelocationModel() == Reloc::Static) {
364     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
365     setPICStyle(PICStyles::None);
366   } else if (is64Bit()) {
367     // PIC in 64 bit mode is always rip-rel.
368     setPICStyle(PICStyles::RIPRel);
369   } else if (isTargetCOFF()) {
370     setPICStyle(PICStyles::None);
371   } else if (isTargetDarwin()) {
372     if (TM.getRelocationModel() == Reloc::PIC_)
373       setPICStyle(PICStyles::StubPIC);
374     else {
375       assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
376       setPICStyle(PICStyles::StubDynamicNoPIC);
377     }
378   } else if (isTargetELF()) {
379     setPICStyle(PICStyles::GOT);
380   }
381 }
382
383 bool X86Subtarget::enableEarlyIfConversion() const {
384   return hasCMov() && X86EarlyIfConv;
385 }
386