Revert r247692: Replace Triple with a new TargetTuple in MCTargetDesc/* and related...
[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 /// Classify a blockaddress reference for the current subtarget according to how
48 /// we should reference it in a non-pcrel context.
49 unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
50   if (isPICStyleGOT())    // 32-bit ELF targets.
51     return X86II::MO_GOTOFF;
52
53   if (isPICStyleStubPIC())   // Darwin/32 in PIC mode.
54     return X86II::MO_PIC_BASE_OFFSET;
55
56   // Direct static reference to label.
57   return X86II::MO_NO_FLAG;
58 }
59
60 /// Classify a global variable reference for the current subtarget according to
61 /// how we should reference it in a non-pcrel context.
62 unsigned char X86Subtarget::
63 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
64   // DLLImport only exists on windows, it is implemented as a load from a
65   // DLLIMPORT stub.
66   if (GV->hasDLLImportStorageClass())
67     return X86II::MO_DLLIMPORT;
68
69   bool isDef = GV->isStrongDefinitionForLinker();
70
71   // X86-64 in PIC mode.
72   if (isPICStyleRIPRel()) {
73     // Large model never uses stubs.
74     if (TM.getCodeModel() == CodeModel::Large)
75       return X86II::MO_NO_FLAG;
76
77     if (isTargetDarwin()) {
78       // If symbol visibility is hidden, the extra load is not needed if
79       // target is x86-64 or the symbol is definitely defined in the current
80       // translation unit.
81       if (GV->hasDefaultVisibility() && !isDef)
82         return X86II::MO_GOTPCREL;
83     } else if (!isTargetWin64()) {
84       assert(isTargetELF() && "Unknown rip-relative target");
85
86       // Extra load is needed for all externally visible.
87       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
88         return X86II::MO_GOTPCREL;
89     }
90
91     return X86II::MO_NO_FLAG;
92   }
93
94   if (isPICStyleGOT()) {   // 32-bit ELF targets.
95     // Extra load is needed for all externally visible.
96     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
97       return X86II::MO_GOTOFF;
98     return X86II::MO_GOT;
99   }
100
101   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
102     // Determine whether we have a stub reference and/or whether the reference
103     // is relative to the PIC base or not.
104
105     // If this is a strong reference to a definition, it is definitely not
106     // through a stub.
107     if (isDef)
108       return X86II::MO_PIC_BASE_OFFSET;
109
110     // Unless we have a symbol with hidden visibility, we have to go through a
111     // normal $non_lazy_ptr stub because this symbol might be resolved late.
112     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
113       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
114
115     // If symbol visibility is hidden, we have a stub for common symbol
116     // references and external declarations.
117     if (GV->isDeclarationForLinker() || GV->hasCommonLinkage()) {
118       // Hidden $non_lazy_ptr reference.
119       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
120     }
121
122     // Otherwise, no stub.
123     return X86II::MO_PIC_BASE_OFFSET;
124   }
125
126   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
127     // Determine whether we have a stub reference.
128
129     // If this is a strong reference to a definition, it is definitely not
130     // through a stub.
131     if (isDef)
132       return X86II::MO_NO_FLAG;
133
134     // Unless we have a symbol with hidden visibility, we have to go through a
135     // normal $non_lazy_ptr stub because this symbol might be resolved late.
136     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
137       return X86II::MO_DARWIN_NONLAZY;
138
139     // Otherwise, no stub.
140     return X86II::MO_NO_FLAG;
141   }
142
143   // Direct static reference to global.
144   return X86II::MO_NO_FLAG;
145 }
146
147
148 /// This function returns the name of a function which has an interface like
149 /// the non-standard bzero function, if such a function exists on the
150 /// current subtarget and it is considered preferable over memset with zero
151 /// passed as the second argument. Otherwise it returns null.
152 const char *X86Subtarget::getBZeroEntry() const {
153   // Darwin 10 has a __bzero entry point for this purpose.
154   if (getTargetTriple().isMacOSX() &&
155       !getTargetTriple().isMacOSXVersionLT(10, 6))
156     return "__bzero";
157
158   return nullptr;
159 }
160
161 bool X86Subtarget::hasSinCos() const {
162   return getTargetTriple().isMacOSX() &&
163     !getTargetTriple().isMacOSXVersionLT(10, 9) &&
164     is64Bit();
165 }
166
167 /// Return true if the subtarget allows calls to immediate address.
168 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
169   // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
170   // but WinCOFFObjectWriter::RecordRelocation cannot emit them.  Once it does,
171   // the following check for Win32 should be removed.
172   if (In64BitMode || isTargetWin32())
173     return false;
174   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
175 }
176
177 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
178   std::string CPUName = CPU;
179   if (CPUName.empty())
180     CPUName = "generic";
181
182   // Make sure 64-bit features are available in 64-bit mode. (But make sure
183   // SSE2 can be turned off explicitly.)
184   std::string FullFS = FS;
185   if (In64BitMode) {
186     if (!FullFS.empty())
187       FullFS = "+64bit,+sse2," + FullFS;
188     else
189       FullFS = "+64bit,+sse2";
190   }
191
192   // Parse features string and set the CPU.
193   ParseSubtargetFeatures(CPUName, FullFS);
194
195   // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of
196   // 16-bytes and under that are reasonably fast. These features were
197   // introduced with Intel's Nehalem/Silvermont and AMD's Family10h
198   // micro-architectures respectively.
199   if (hasSSE42() || hasSSE4A())
200     IsUAMem16Slow = false;
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   HasPRFCHW = false;
261   HasRDSEED = false;
262   HasMPX = false;
263   IsBTMemSlow = false;
264   IsSHLDSlow = false;
265   IsUAMem16Slow = false;
266   IsUAMem32Slow = false;
267   HasSSEUnalignedMem = false;
268   HasCmpxchg16b = false;
269   UseLeaForSP = false;
270   HasSlowDivide32 = false;
271   HasSlowDivide64 = false;
272   PadShortFunctions = false;
273   CallRegIndirect = false;
274   LEAUsesAG = false;
275   SlowLEA = false;
276   SlowIncDec = false;
277   stackAlignment = 4;
278   // FIXME: this is a known good value for Yonah. How about others?
279   MaxInlineSizeThreshold = 128;
280   UseSoftFloat = false;
281 }
282
283 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
284                                                             StringRef FS) {
285   initializeEnvironment();
286   initSubtargetFeatures(CPU, FS);
287   return *this;
288 }
289
290 X86Subtarget::X86Subtarget(const Triple &TT, const std::string &CPU,
291                            const std::string &FS, const X86TargetMachine &TM,
292                            unsigned StackAlignOverride)
293     : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
294       PICStyle(PICStyles::None), TargetTriple(TT),
295       StackAlignOverride(StackAlignOverride),
296       In64BitMode(TargetTriple.getArch() == Triple::x86_64),
297       In32BitMode(TargetTriple.getArch() == Triple::x86 &&
298                   TargetTriple.getEnvironment() != Triple::CODE16),
299       In16BitMode(TargetTriple.getArch() == Triple::x86 &&
300                   TargetTriple.getEnvironment() == Triple::CODE16),
301       TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
302       TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) {
303   // Determine the PICStyle based on the target selected.
304   if (TM.getRelocationModel() == Reloc::Static) {
305     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
306     setPICStyle(PICStyles::None);
307   } else if (is64Bit()) {
308     // PIC in 64 bit mode is always rip-rel.
309     setPICStyle(PICStyles::RIPRel);
310   } else if (isTargetCOFF()) {
311     setPICStyle(PICStyles::None);
312   } else if (isTargetDarwin()) {
313     if (TM.getRelocationModel() == Reloc::PIC_)
314       setPICStyle(PICStyles::StubPIC);
315     else {
316       assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
317       setPICStyle(PICStyles::StubDynamicNoPIC);
318     }
319   } else if (isTargetELF()) {
320     setPICStyle(PICStyles::GOT);
321   }
322 }
323
324 bool X86Subtarget::enableEarlyIfConversion() const {
325   return hasCMov() && X86EarlyIfConv;
326 }
327