X86: Remove TargetMachine CPU auto-detection.
[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/IR/Attributes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26
27 #define GET_SUBTARGETINFO_TARGET_DESC
28 #define GET_SUBTARGETINFO_CTOR
29 #include "X86GenSubtargetInfo.inc"
30
31 using namespace llvm;
32
33 #if defined(_MSC_VER)
34 #include <intrin.h>
35 #endif
36
37 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
38 /// current subtarget according to how we should reference it in a non-pcrel
39 /// context.
40 unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
41   if (isPICStyleGOT())    // 32-bit ELF targets.
42     return X86II::MO_GOTOFF;
43
44   if (isPICStyleStubPIC())   // Darwin/32 in PIC mode.
45     return X86II::MO_PIC_BASE_OFFSET;
46
47   // Direct static reference to label.
48   return X86II::MO_NO_FLAG;
49 }
50
51 /// ClassifyGlobalReference - Classify a global variable reference for the
52 /// current subtarget according to how we should reference it in a non-pcrel
53 /// context.
54 unsigned char X86Subtarget::
55 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
56   // DLLImport only exists on windows, it is implemented as a load from a
57   // DLLIMPORT stub.
58   if (GV->hasDLLImportStorageClass())
59     return X86II::MO_DLLIMPORT;
60
61   // Determine whether this is a reference to a definition or a declaration.
62   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
63   // load from stub.
64   bool isDecl = GV->hasAvailableExternallyLinkage();
65   if (GV->isDeclaration() && !GV->isMaterializable())
66     isDecl = true;
67
68   // X86-64 in PIC mode.
69   if (isPICStyleRIPRel()) {
70     // Large model never uses stubs.
71     if (TM.getCodeModel() == CodeModel::Large)
72       return X86II::MO_NO_FLAG;
73
74     if (isTargetDarwin()) {
75       // If symbol visibility is hidden, the extra load is not needed if
76       // target is x86-64 or the symbol is definitely defined in the current
77       // translation unit.
78       if (GV->hasDefaultVisibility() &&
79           (isDecl || GV->isWeakForLinker()))
80         return X86II::MO_GOTPCREL;
81     } else if (!isTargetWin64()) {
82       assert(isTargetELF() && "Unknown rip-relative target");
83
84       // Extra load is needed for all externally visible.
85       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
86         return X86II::MO_GOTPCREL;
87     }
88
89     return X86II::MO_NO_FLAG;
90   }
91
92   if (isPICStyleGOT()) {   // 32-bit ELF targets.
93     // Extra load is needed for all externally visible.
94     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
95       return X86II::MO_GOTOFF;
96     return X86II::MO_GOT;
97   }
98
99   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
100     // Determine whether we have a stub reference and/or whether the reference
101     // is relative to the PIC base or not.
102
103     // If this is a strong reference to a definition, it is definitely not
104     // through a stub.
105     if (!isDecl && !GV->isWeakForLinker())
106       return X86II::MO_PIC_BASE_OFFSET;
107
108     // Unless we have a symbol with hidden visibility, we have to go through a
109     // normal $non_lazy_ptr stub because this symbol might be resolved late.
110     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
111       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
112
113     // If symbol visibility is hidden, we have a stub for common symbol
114     // references and external declarations.
115     if (isDecl || GV->hasCommonLinkage()) {
116       // Hidden $non_lazy_ptr reference.
117       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
118     }
119
120     // Otherwise, no stub.
121     return X86II::MO_PIC_BASE_OFFSET;
122   }
123
124   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
125     // Determine whether we have a stub reference.
126
127     // If this is a strong reference to a definition, it is definitely not
128     // through a stub.
129     if (!isDecl && !GV->isWeakForLinker())
130       return X86II::MO_NO_FLAG;
131
132     // Unless we have a symbol with hidden visibility, we have to go through a
133     // normal $non_lazy_ptr stub because this symbol might be resolved late.
134     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
135       return X86II::MO_DARWIN_NONLAZY;
136
137     // Otherwise, no stub.
138     return X86II::MO_NO_FLAG;
139   }
140
141   // Direct static reference to global.
142   return X86II::MO_NO_FLAG;
143 }
144
145
146 /// getBZeroEntry - This function returns the name of a function which has an
147 /// interface like the non-standard bzero function, if such a function exists on
148 /// the current subtarget and it is considered prefereable over memset with zero
149 /// passed as the second argument. Otherwise it returns null.
150 const char *X86Subtarget::getBZeroEntry() const {
151   // Darwin 10 has a __bzero entry point for this purpose.
152   if (getTargetTriple().isMacOSX() &&
153       !getTargetTriple().isMacOSXVersionLT(10, 6))
154     return "__bzero";
155
156   return 0;
157 }
158
159 bool X86Subtarget::hasSinCos() const {
160   return getTargetTriple().isMacOSX() &&
161     !getTargetTriple().isMacOSXVersionLT(10, 9) &&
162     is64Bit();
163 }
164
165 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
166 /// to immediate address.
167 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
168   // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
169   // but WinCOFFObjectWriter::RecordRelocation cannot emit them.  Once it does,
170   // the following check for Win32 should be removed.
171   if (In64BitMode || isTargetWin32())
172     return false;
173   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
174 }
175
176 void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
177   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
178   Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
179                                            "target-cpu");
180   Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
181                                           "target-features");
182   std::string CPU =
183     !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
184   std::string FS =
185     !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
186   if (!FS.empty()) {
187     initializeEnvironment();
188     resetSubtargetFeatures(CPU, FS);
189   }
190 }
191
192 void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
193   std::string CPUName = CPU;
194   if (CPUName.empty())
195     CPUName = "generic";
196
197   // Make sure 64-bit features are available in 64-bit mode. (But make sure
198   // SSE2 can be turned off explicitly.)
199   std::string FullFS = FS;
200   if (In64BitMode) {
201     if (!FullFS.empty())
202       FullFS = "+64bit,+sse2," + FullFS;
203     else
204       FullFS = "+64bit,+sse2";
205   }
206
207   // If feature string is not empty, parse features string.
208   ParseSubtargetFeatures(CPUName, FullFS);
209
210   // Make sure the right MCSchedModel is used.
211   InitCPUSchedModel(CPUName);
212
213   if (X86ProcFamily == IntelAtom || X86ProcFamily == IntelSLM)
214     PostRAScheduler = true;
215
216   InstrItins = getInstrItineraryForCPU(CPUName);
217
218   // It's important to keep the MCSubtargetInfo feature bits in sync with
219   // target data structure which is shared with MC code emitter, etc.
220   if (In64BitMode)
221     ToggleFeature(X86::Mode64Bit);
222   else if (In32BitMode)
223     ToggleFeature(X86::Mode32Bit);
224   else if (In16BitMode)
225     ToggleFeature(X86::Mode16Bit);
226   else
227     llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
228
229   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
230                << ", 3DNowLevel " << X863DNowLevel
231                << ", 64bit " << HasX86_64 << "\n");
232   assert((!In64BitMode || HasX86_64) &&
233          "64-bit code requested on a subtarget that doesn't support it!");
234
235   // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
236   // 32 and 64 bit) and for all 64-bit targets.
237   if (StackAlignOverride)
238     stackAlignment = StackAlignOverride;
239   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
240            In64BitMode)
241     stackAlignment = 16;
242 }
243
244 void X86Subtarget::initializeEnvironment() {
245   X86SSELevel = NoMMXSSE;
246   X863DNowLevel = NoThreeDNow;
247   HasCMov = false;
248   HasX86_64 = false;
249   HasPOPCNT = false;
250   HasSSE4A = false;
251   HasAES = false;
252   HasPCLMUL = false;
253   HasFMA = false;
254   HasFMA4 = false;
255   HasXOP = false;
256   HasTBM = false;
257   HasMOVBE = false;
258   HasRDRAND = false;
259   HasF16C = false;
260   HasFSGSBase = false;
261   HasLZCNT = false;
262   HasBMI = false;
263   HasBMI2 = false;
264   HasRTM = false;
265   HasHLE = false;
266   HasERI = false;
267   HasCDI = false;
268   HasPFI = false;
269   HasADX = false;
270   HasSHA = false;
271   HasPRFCHW = false;
272   HasRDSEED = false;
273   IsBTMemSlow = false;
274   IsSHLDSlow = false;
275   IsUAMemFast = false;
276   HasVectorUAMem = false;
277   HasCmpxchg16b = false;
278   UseLeaForSP = false;
279   HasSlowDivide = false;
280   PostRAScheduler = false;
281   PadShortFunctions = false;
282   CallRegIndirect = false;
283   LEAUsesAG = false;
284   stackAlignment = 4;
285   // FIXME: this is a known good value for Yonah. How about others?
286   MaxInlineSizeThreshold = 128;
287 }
288
289 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
290                            const std::string &FS,
291                            unsigned StackAlignOverride)
292   : X86GenSubtargetInfo(TT, CPU, FS)
293   , X86ProcFamily(Others)
294   , PICStyle(PICStyles::None)
295   , TargetTriple(TT)
296   , StackAlignOverride(StackAlignOverride)
297   , In64BitMode(TargetTriple.getArch() == Triple::x86_64)
298   , In32BitMode(TargetTriple.getArch() == Triple::x86 &&
299                 TargetTriple.getEnvironment() != Triple::CODE16)
300   , In16BitMode(TargetTriple.getArch() == Triple::x86 &&
301                 TargetTriple.getEnvironment() == Triple::CODE16) {
302   initializeEnvironment();
303   resetSubtargetFeatures(CPU, FS);
304 }
305
306 bool X86Subtarget::enablePostRAScheduler(
307            CodeGenOpt::Level OptLevel,
308            TargetSubtargetInfo::AntiDepBreakMode& Mode,
309            RegClassVector& CriticalPathRCs) const {
310   Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
311   CriticalPathRCs.clear();
312   return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
313 }