Move DataLayout back to the TargetMachine from TargetSubtargetInfo
[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   bool isDecl = GV->isDeclarationForLinker();
72
73   // X86-64 in PIC mode.
74   if (isPICStyleRIPRel()) {
75     // Large model never uses stubs.
76     if (TM.getCodeModel() == CodeModel::Large)
77       return X86II::MO_NO_FLAG;
78
79     if (isTargetDarwin()) {
80       // If symbol visibility is hidden, the extra load is not needed if
81       // target is x86-64 or the symbol is definitely defined in the current
82       // translation unit.
83       if (GV->hasDefaultVisibility() &&
84           (isDecl || GV->isWeakForLinker()))
85         return X86II::MO_GOTPCREL;
86     } else if (!isTargetWin64()) {
87       assert(isTargetELF() && "Unknown rip-relative target");
88
89       // Extra load is needed for all externally visible.
90       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
91         return X86II::MO_GOTPCREL;
92     }
93
94     return X86II::MO_NO_FLAG;
95   }
96
97   if (isPICStyleGOT()) {   // 32-bit ELF targets.
98     // Extra load is needed for all externally visible.
99     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
100       return X86II::MO_GOTOFF;
101     return X86II::MO_GOT;
102   }
103
104   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
105     // Determine whether we have a stub reference and/or whether the reference
106     // is relative to the PIC base or not.
107
108     // If this is a strong reference to a definition, it is definitely not
109     // through a stub.
110     if (!isDecl && !GV->isWeakForLinker())
111       return X86II::MO_PIC_BASE_OFFSET;
112
113     // Unless we have a symbol with hidden visibility, we have to go through a
114     // normal $non_lazy_ptr stub because this symbol might be resolved late.
115     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
116       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
117
118     // If symbol visibility is hidden, we have a stub for common symbol
119     // references and external declarations.
120     if (isDecl || GV->hasCommonLinkage()) {
121       // Hidden $non_lazy_ptr reference.
122       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
123     }
124
125     // Otherwise, no stub.
126     return X86II::MO_PIC_BASE_OFFSET;
127   }
128
129   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
130     // Determine whether we have a stub reference.
131
132     // If this is a strong reference to a definition, it is definitely not
133     // through a stub.
134     if (!isDecl && !GV->isWeakForLinker())
135       return X86II::MO_NO_FLAG;
136
137     // Unless we have a symbol with hidden visibility, we have to go through a
138     // normal $non_lazy_ptr stub because this symbol might be resolved late.
139     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
140       return X86II::MO_DARWIN_NONLAZY;
141
142     // Otherwise, no stub.
143     return X86II::MO_NO_FLAG;
144   }
145
146   // Direct static reference to global.
147   return X86II::MO_NO_FLAG;
148 }
149
150
151 /// getBZeroEntry - This function returns the name of a function which has an
152 /// interface like the non-standard bzero function, if such a function exists on
153 /// the current subtarget and it is considered prefereable over memset with zero
154 /// passed as the second argument. Otherwise it returns null.
155 const char *X86Subtarget::getBZeroEntry() const {
156   // Darwin 10 has a __bzero entry point for this purpose.
157   if (getTargetTriple().isMacOSX() &&
158       !getTargetTriple().isMacOSXVersionLT(10, 6))
159     return "__bzero";
160
161   return nullptr;
162 }
163
164 bool X86Subtarget::hasSinCos() const {
165   return getTargetTriple().isMacOSX() &&
166     !getTargetTriple().isMacOSXVersionLT(10, 9) &&
167     is64Bit();
168 }
169
170 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
171 /// to immediate address.
172 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
173   // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
174   // but WinCOFFObjectWriter::RecordRelocation cannot emit them.  Once it does,
175   // the following check for Win32 should be removed.
176   if (In64BitMode || isTargetWin32())
177     return false;
178   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
179 }
180
181 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
182   std::string CPUName = CPU;
183   if (CPUName.empty())
184     CPUName = "generic";
185
186   // Make sure 64-bit features are available in 64-bit mode. (But make sure
187   // SSE2 can be turned off explicitly.)
188   std::string FullFS = FS;
189   if (In64BitMode) {
190     if (!FullFS.empty())
191       FullFS = "+64bit,+sse2," + FullFS;
192     else
193       FullFS = "+64bit,+sse2";
194   }
195
196   // If feature string is not empty, parse features string.
197   ParseSubtargetFeatures(CPUName, FullFS);
198
199   // Make sure the right MCSchedModel is used.
200   InitCPUSchedModel(CPUName);
201
202   InstrItins = getInstrItineraryForCPU(CPUName);
203
204   // It's important to keep the MCSubtargetInfo feature bits in sync with
205   // target data structure which is shared with MC code emitter, etc.
206   if (In64BitMode)
207     ToggleFeature(X86::Mode64Bit);
208   else if (In32BitMode)
209     ToggleFeature(X86::Mode32Bit);
210   else if (In16BitMode)
211     ToggleFeature(X86::Mode16Bit);
212   else
213     llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
214
215   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
216                << ", 3DNowLevel " << X863DNowLevel
217                << ", 64bit " << HasX86_64 << "\n");
218   assert((!In64BitMode || HasX86_64) &&
219          "64-bit code requested on a subtarget that doesn't support it!");
220
221   // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
222   // 32 and 64 bit) and for all 64-bit targets.
223   if (StackAlignOverride)
224     stackAlignment = StackAlignOverride;
225   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
226            In64BitMode)
227     stackAlignment = 16;
228 }
229
230 void X86Subtarget::initializeEnvironment() {
231   X86SSELevel = NoMMXSSE;
232   X863DNowLevel = NoThreeDNow;
233   HasCMov = false;
234   HasX86_64 = false;
235   HasPOPCNT = false;
236   HasSSE4A = false;
237   HasAES = false;
238   HasPCLMUL = false;
239   HasFMA = false;
240   HasFMA4 = false;
241   HasXOP = false;
242   HasTBM = false;
243   HasMOVBE = false;
244   HasRDRAND = false;
245   HasF16C = false;
246   HasFSGSBase = false;
247   HasLZCNT = false;
248   HasBMI = false;
249   HasBMI2 = false;
250   HasRTM = false;
251   HasHLE = false;
252   HasERI = false;
253   HasCDI = false;
254   HasPFI = false;
255   HasDQI = false;
256   HasBWI = false;
257   HasVLX = false;
258   HasADX = false;
259   HasSHA = false;
260   HasSGX = false;
261   HasPRFCHW = false;
262   HasRDSEED = false;
263   HasSMAP = false;
264   IsBTMemSlow = false;
265   IsSHLDSlow = false;
266   IsUAMemFast = false;
267   IsUAMem32Slow = false;
268   HasVectorUAMem = false;
269   HasCmpxchg16b = false;
270   UseLeaForSP = false;
271   HasSlowDivide32 = false;
272   HasSlowDivide64 = false;
273   PadShortFunctions = false;
274   CallRegIndirect = false;
275   LEAUsesAG = false;
276   SlowLEA = false;
277   SlowIncDec = false;
278   UseSqrtEst = false;
279   UseReciprocalEst = false;
280   stackAlignment = 4;
281   // FIXME: this is a known good value for Yonah. How about others?
282   MaxInlineSizeThreshold = 128;
283 }
284
285 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
286                                                             StringRef FS) {
287   initializeEnvironment();
288   initSubtargetFeatures(CPU, FS);
289   return *this;
290 }
291
292 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
293                            const std::string &FS, const X86TargetMachine &TM,
294                            unsigned StackAlignOverride)
295     : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
296       PICStyle(PICStyles::None), TargetTriple(TT),
297       StackAlignOverride(StackAlignOverride),
298       In64BitMode(TargetTriple.getArch() == Triple::x86_64),
299       In32BitMode(TargetTriple.getArch() == Triple::x86 &&
300                   TargetTriple.getEnvironment() != Triple::CODE16),
301       In16BitMode(TargetTriple.getArch() == Triple::x86 &&
302                   TargetTriple.getEnvironment() == Triple::CODE16),
303       TSInfo(*TM.getDataLayout()),
304       InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM),
305       FrameLowering(TargetFrameLowering::StackGrowsDown, getStackAlignment(),
306                     is64Bit() ? -8 : -4) {
307   // Determine the PICStyle based on the target selected.
308   if (TM.getRelocationModel() == Reloc::Static) {
309     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
310     setPICStyle(PICStyles::None);
311   } else if (is64Bit()) {
312     // PIC in 64 bit mode is always rip-rel.
313     setPICStyle(PICStyles::RIPRel);
314   } else if (isTargetCOFF()) {
315     setPICStyle(PICStyles::None);
316   } else if (isTargetDarwin()) {
317     if (TM.getRelocationModel() == Reloc::PIC_)
318       setPICStyle(PICStyles::StubPIC);
319     else {
320       assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
321       setPICStyle(PICStyles::StubDynamicNoPIC);
322     }
323   } else if (isTargetELF()) {
324     setPICStyle(PICStyles::GOT);
325   }
326 }
327
328 bool X86Subtarget::enableEarlyIfConversion() const {
329   return hasCMov() && X86EarlyIfConv;
330 }
331