don't repeaat function names in comments; NFC
[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   InstrItins = getInstrItineraryForCPU(CPUName);
196
197   // It's important to keep the MCSubtargetInfo feature bits in sync with
198   // target data structure which is shared with MC code emitter, etc.
199   if (In64BitMode)
200     ToggleFeature(X86::Mode64Bit);
201   else if (In32BitMode)
202     ToggleFeature(X86::Mode32Bit);
203   else if (In16BitMode)
204     ToggleFeature(X86::Mode16Bit);
205   else
206     llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
207
208   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
209                << ", 3DNowLevel " << X863DNowLevel
210                << ", 64bit " << HasX86_64 << "\n");
211   assert((!In64BitMode || HasX86_64) &&
212          "64-bit code requested on a subtarget that doesn't support it!");
213
214   // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
215   // 32 and 64 bit) and for all 64-bit targets.
216   if (StackAlignOverride)
217     stackAlignment = StackAlignOverride;
218   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
219            In64BitMode)
220     stackAlignment = 16;
221 }
222
223 void X86Subtarget::initializeEnvironment() {
224   X86SSELevel = NoMMXSSE;
225   X863DNowLevel = NoThreeDNow;
226   HasCMov = false;
227   HasX86_64 = false;
228   HasPOPCNT = false;
229   HasSSE4A = false;
230   HasAES = false;
231   HasPCLMUL = false;
232   HasFMA = false;
233   HasFMA4 = false;
234   HasXOP = false;
235   HasTBM = false;
236   HasMOVBE = false;
237   HasRDRAND = false;
238   HasF16C = false;
239   HasFSGSBase = false;
240   HasLZCNT = false;
241   HasBMI = false;
242   HasBMI2 = false;
243   HasRTM = false;
244   HasHLE = false;
245   HasERI = false;
246   HasCDI = false;
247   HasPFI = false;
248   HasDQI = false;
249   HasBWI = false;
250   HasVLX = false;
251   HasADX = false;
252   HasSHA = false;
253   HasPRFCHW = false;
254   HasRDSEED = false;
255   HasMPX = false;
256   IsBTMemSlow = false;
257   IsSHLDSlow = false;
258   IsUAMemFast = false;
259   IsUAMem32Slow = false;
260   HasSSEUnalignedMem = false;
261   HasCmpxchg16b = false;
262   UseLeaForSP = false;
263   HasSlowDivide32 = false;
264   HasSlowDivide64 = false;
265   PadShortFunctions = false;
266   CallRegIndirect = false;
267   LEAUsesAG = false;
268   SlowLEA = false;
269   SlowIncDec = false;
270   stackAlignment = 4;
271   // FIXME: this is a known good value for Yonah. How about others?
272   MaxInlineSizeThreshold = 128;
273   UseSoftFloat = false;
274 }
275
276 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
277                                                             StringRef FS) {
278   initializeEnvironment();
279   initSubtargetFeatures(CPU, FS);
280   return *this;
281 }
282
283 X86Subtarget::X86Subtarget(const Triple &TT, const std::string &CPU,
284                            const std::string &FS, const X86TargetMachine &TM,
285                            unsigned StackAlignOverride)
286     : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
287       PICStyle(PICStyles::None), TargetTriple(TT),
288       StackAlignOverride(StackAlignOverride),
289       In64BitMode(TargetTriple.getArch() == Triple::x86_64),
290       In32BitMode(TargetTriple.getArch() == Triple::x86 &&
291                   TargetTriple.getEnvironment() != Triple::CODE16),
292       In16BitMode(TargetTriple.getArch() == Triple::x86 &&
293                   TargetTriple.getEnvironment() == Triple::CODE16),
294       TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
295       TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) {
296   // Determine the PICStyle based on the target selected.
297   if (TM.getRelocationModel() == Reloc::Static) {
298     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
299     setPICStyle(PICStyles::None);
300   } else if (is64Bit()) {
301     // PIC in 64 bit mode is always rip-rel.
302     setPICStyle(PICStyles::RIPRel);
303   } else if (isTargetCOFF()) {
304     setPICStyle(PICStyles::None);
305   } else if (isTargetDarwin()) {
306     if (TM.getRelocationModel() == Reloc::PIC_)
307       setPICStyle(PICStyles::StubPIC);
308     else {
309       assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
310       setPICStyle(PICStyles::StubDynamicNoPIC);
311     }
312   } else if (isTargetELF()) {
313     setPICStyle(PICStyles::GOT);
314   }
315 }
316
317 bool X86Subtarget::enableEarlyIfConversion() const {
318   return hasCMov() && X86EarlyIfConv;
319 }
320