Remove resetSubtargetFeatures as it is unused.
[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::initSubtargetFeatures(StringRef CPU, StringRef FS) {
187   std::string CPUName = CPU;
188   if (CPUName.empty())
189     CPUName = "generic";
190
191   // Make sure 64-bit features are available in 64-bit mode. (But make sure
192   // SSE2 can be turned off explicitly.)
193   std::string FullFS = FS;
194   if (In64BitMode) {
195     if (!FullFS.empty())
196       FullFS = "+64bit,+sse2," + FullFS;
197     else
198       FullFS = "+64bit,+sse2";
199   }
200
201   // If feature string is not empty, parse features string.
202   ParseSubtargetFeatures(CPUName, FullFS);
203
204   // Make sure the right MCSchedModel is used.
205   InitCPUSchedModel(CPUName);
206
207   InstrItins = getInstrItineraryForCPU(CPUName);
208
209   // It's important to keep the MCSubtargetInfo feature bits in sync with
210   // target data structure which is shared with MC code emitter, etc.
211   if (In64BitMode)
212     ToggleFeature(X86::Mode64Bit);
213   else if (In32BitMode)
214     ToggleFeature(X86::Mode32Bit);
215   else if (In16BitMode)
216     ToggleFeature(X86::Mode16Bit);
217   else
218     llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
219
220   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
221                << ", 3DNowLevel " << X863DNowLevel
222                << ", 64bit " << HasX86_64 << "\n");
223   assert((!In64BitMode || HasX86_64) &&
224          "64-bit code requested on a subtarget that doesn't support it!");
225
226   // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
227   // 32 and 64 bit) and for all 64-bit targets.
228   if (StackAlignOverride)
229     stackAlignment = StackAlignOverride;
230   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
231            In64BitMode)
232     stackAlignment = 16;
233 }
234
235 void X86Subtarget::initializeEnvironment() {
236   X86SSELevel = NoMMXSSE;
237   X863DNowLevel = NoThreeDNow;
238   HasCMov = false;
239   HasX86_64 = false;
240   HasPOPCNT = false;
241   HasSSE4A = false;
242   HasAES = false;
243   HasPCLMUL = false;
244   HasFMA = false;
245   HasFMA4 = false;
246   HasXOP = false;
247   HasTBM = false;
248   HasMOVBE = false;
249   HasRDRAND = false;
250   HasF16C = false;
251   HasFSGSBase = false;
252   HasLZCNT = false;
253   HasBMI = false;
254   HasBMI2 = false;
255   HasRTM = false;
256   HasHLE = false;
257   HasERI = false;
258   HasCDI = false;
259   HasPFI = false;
260   HasDQI = false;
261   HasBWI = false;
262   HasVLX = false;
263   HasADX = false;
264   HasSHA = false;
265   HasSGX = false;
266   HasPRFCHW = false;
267   HasRDSEED = false;
268   HasSMAP = false;
269   IsBTMemSlow = false;
270   IsSHLDSlow = false;
271   IsUAMemFast = false;
272   HasVectorUAMem = false;
273   HasCmpxchg16b = false;
274   UseLeaForSP = false;
275   HasSlowDivide = false;
276   PadShortFunctions = false;
277   CallRegIndirect = false;
278   LEAUsesAG = false;
279   SlowLEA = false;
280   SlowIncDec = false;
281   stackAlignment = 4;
282   // FIXME: this is a known good value for Yonah. How about others?
283   MaxInlineSizeThreshold = 128;
284 }
285
286 static std::string computeDataLayout(const Triple &TT) {
287   // X86 is little endian
288   std::string Ret = "e";
289
290   Ret += DataLayout::getManglingComponent(TT);
291   // X86 and x32 have 32 bit pointers.
292   if ((TT.isArch64Bit() &&
293        (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
294       !TT.isArch64Bit())
295     Ret += "-p:32:32";
296
297   // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
298   if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
299     Ret += "-i64:64";
300   else
301     Ret += "-f64:32:64";
302
303   // Some ABIs align long double to 128 bits, others to 32.
304   if (TT.isOSNaCl())
305     ; // No f80
306   else if (TT.isArch64Bit() || TT.isOSDarwin())
307     Ret += "-f80:128";
308   else
309     Ret += "-f80:32";
310
311   // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
312   if (TT.isArch64Bit())
313     Ret += "-n8:16:32:64";
314   else
315     Ret += "-n8:16:32";
316
317   // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
318   if (!TT.isArch64Bit() && TT.isOSWindows())
319     Ret += "-S32";
320   else
321     Ret += "-S128";
322
323   return Ret;
324 }
325
326 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
327                                                             StringRef FS) {
328   initializeEnvironment();
329   initSubtargetFeatures(CPU, FS);
330   return *this;
331 }
332
333 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
334                            const std::string &FS, X86TargetMachine &TM,
335                            unsigned StackAlignOverride)
336     : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
337       PICStyle(PICStyles::None), TargetTriple(TT),
338       DL(computeDataLayout(TargetTriple)),
339       StackAlignOverride(StackAlignOverride),
340       In64BitMode(TargetTriple.getArch() == Triple::x86_64),
341       In32BitMode(TargetTriple.getArch() == Triple::x86 &&
342                   TargetTriple.getEnvironment() != Triple::CODE16),
343       In16BitMode(TargetTriple.getArch() == Triple::x86 &&
344                   TargetTriple.getEnvironment() == Triple::CODE16),
345       TSInfo(DL), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
346       TLInfo(TM), FrameLowering(TargetFrameLowering::StackGrowsDown,
347                                 getStackAlignment(), is64Bit() ? -8 : -4) {
348   // Determine the PICStyle based on the target selected.
349   if (TM.getRelocationModel() == Reloc::Static) {
350     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
351     setPICStyle(PICStyles::None);
352   } else if (is64Bit()) {
353     // PIC in 64 bit mode is always rip-rel.
354     setPICStyle(PICStyles::RIPRel);
355   } else if (isTargetCOFF()) {
356     setPICStyle(PICStyles::None);
357   } else if (isTargetDarwin()) {
358     if (TM.getRelocationModel() == Reloc::PIC_)
359       setPICStyle(PICStyles::StubPIC);
360     else {
361       assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
362       setPICStyle(PICStyles::StubDynamicNoPIC);
363     }
364   } else if (isTargetELF()) {
365     setPICStyle(PICStyles::GOT);
366   }
367 }
368
369 bool X86Subtarget::enableEarlyIfConversion() const {
370   return hasCMov() && X86EarlyIfConv;
371 }
372