]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/X86/X86Subtarget.cpp
Merge ^/head r312624 through r312719.
[FreeBSD/FreeBSD.git] / contrib / llvm / 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   return classifyLocalReference(nullptr);
51 }
52
53 /// Classify a global variable reference for the current subtarget according to
54 /// how we should reference it in a non-pcrel context.
55 unsigned char
56 X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const {
57   return classifyGlobalReference(GV, *GV->getParent());
58 }
59
60 unsigned char
61 X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
62   // 64 bits can use %rip addressing for anything local.
63   if (is64Bit())
64     return X86II::MO_NO_FLAG;
65
66   // If this is for a position dependent executable, the static linker can
67   // figure it out.
68   if (!isPositionIndependent())
69     return X86II::MO_NO_FLAG;
70
71   // The COFF dynamic linker just patches the executable sections.
72   if (isTargetCOFF())
73     return X86II::MO_NO_FLAG;
74
75   if (isTargetDarwin()) {
76     // 32 bit macho has no relocation for a-b if a is undefined, even if
77     // b is in the section that is being relocated.
78     // This means we have to use o load even for GVs that are known to be
79     // local to the dso.
80     if (GV && (GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
81       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
82
83     return X86II::MO_PIC_BASE_OFFSET;
84   }
85
86   return X86II::MO_GOTOFF;
87 }
88
89 unsigned char X86Subtarget::classifyGlobalReference(const GlobalValue *GV,
90                                                     const Module &M) const {
91   // Large model never uses stubs.
92   if (TM.getCodeModel() == CodeModel::Large)
93     return X86II::MO_NO_FLAG;
94
95   // Absolute symbols can be referenced directly.
96   if (GV && GV->isAbsoluteSymbolRef())
97     return X86II::MO_NO_FLAG;
98
99   if (TM.shouldAssumeDSOLocal(M, GV))
100     return classifyLocalReference(GV);
101
102   if (isTargetCOFF())
103     return X86II::MO_DLLIMPORT;
104
105   if (is64Bit())
106     return X86II::MO_GOTPCREL;
107
108   if (isTargetDarwin()) {
109     if (!isPositionIndependent())
110       return X86II::MO_DARWIN_NONLAZY;
111     return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
112   }
113
114   return X86II::MO_GOT;
115 }
116
117 unsigned char
118 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const {
119   return classifyGlobalFunctionReference(GV, *GV->getParent());
120 }
121
122 unsigned char
123 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV,
124                                               const Module &M) const {
125   if (TM.shouldAssumeDSOLocal(M, GV))
126     return X86II::MO_NO_FLAG;
127
128   assert(!isTargetCOFF());
129
130   if (isTargetELF())
131     return X86II::MO_PLT;
132
133   if (is64Bit()) {
134     auto *F = dyn_cast_or_null<Function>(GV);
135     if (F && F->hasFnAttribute(Attribute::NonLazyBind))
136       // If the function is marked as non-lazy, generate an indirect call
137       // which loads from the GOT directly. This avoids runtime overhead
138       // at the cost of eager binding (and one extra byte of encoding).
139       return X86II::MO_GOTPCREL;
140     return X86II::MO_NO_FLAG;
141   }
142
143   return X86II::MO_NO_FLAG;
144 }
145
146 /// This function returns the name of a function which has an interface like
147 /// the non-standard bzero function, if such a function exists on the
148 /// current subtarget and it is considered preferable 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 nullptr;
157 }
158
159 bool X86Subtarget::hasSinCos() const {
160   return getTargetTriple().isMacOSX() &&
161     !getTargetTriple().isMacOSXVersionLT(10, 9) &&
162     is64Bit();
163 }
164
165 /// Return true if the subtarget allows calls to immediate address.
166 bool X86Subtarget::isLegalToCallImmediateAddr() const {
167   // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
168   // but WinCOFFObjectWriter::RecordRelocation cannot emit them.  Once it does,
169   // the following check for Win32 should be removed.
170   if (In64BitMode || isTargetWin32())
171     return false;
172   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
173 }
174
175 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
176   std::string CPUName = CPU;
177   if (CPUName.empty())
178     CPUName = "generic";
179
180   // Make sure 64-bit features are available in 64-bit mode. (But make sure
181   // SSE2 can be turned off explicitly.)
182   std::string FullFS = FS;
183   if (In64BitMode) {
184     if (!FullFS.empty())
185       FullFS = "+64bit,+sse2," + FullFS;
186     else
187       FullFS = "+64bit,+sse2";
188   }
189
190   // LAHF/SAHF are always supported in non-64-bit mode.
191   if (!In64BitMode) {
192     if (!FullFS.empty())
193       FullFS = "+sahf," + FullFS;
194     else
195       FullFS = "+sahf";
196   }
197
198
199   // Parse features string and set the CPU.
200   ParseSubtargetFeatures(CPUName, FullFS);
201
202   // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of
203   // 16-bytes and under that are reasonably fast. These features were
204   // introduced with Intel's Nehalem/Silvermont and AMD's Family10h
205   // micro-architectures respectively.
206   if (hasSSE42() || hasSSE4A())
207     IsUAMem16Slow = false;
208   
209   InstrItins = getInstrItineraryForCPU(CPUName);
210
211   // It's important to keep the MCSubtargetInfo feature bits in sync with
212   // target data structure which is shared with MC code emitter, etc.
213   if (In64BitMode)
214     ToggleFeature(X86::Mode64Bit);
215   else if (In32BitMode)
216     ToggleFeature(X86::Mode32Bit);
217   else if (In16BitMode)
218     ToggleFeature(X86::Mode16Bit);
219   else
220     llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
221
222   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
223                << ", 3DNowLevel " << X863DNowLevel
224                << ", 64bit " << HasX86_64 << "\n");
225   assert((!In64BitMode || HasX86_64) &&
226          "64-bit code requested on a subtarget that doesn't support it!");
227
228   // Stack alignment is 16 bytes on Darwin, Linux, kFreeBSD and Solaris (both
229   // 32 and 64 bit) and for all 64-bit targets.
230   if (StackAlignOverride)
231     stackAlignment = StackAlignOverride;
232   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
233            isTargetKFreeBSD() || In64BitMode)
234     stackAlignment = 16;
235 }
236
237 void X86Subtarget::initializeEnvironment() {
238   X86SSELevel = NoSSE;
239   X863DNowLevel = NoThreeDNow;
240   HasX87 = false;
241   HasCMov = false;
242   HasX86_64 = false;
243   HasPOPCNT = false;
244   HasSSE4A = false;
245   HasAES = false;
246   HasFXSR = false;
247   HasXSAVE = false;
248   HasXSAVEOPT = false;
249   HasXSAVEC = false;
250   HasXSAVES = false;
251   HasPCLMUL = false;
252   HasFMA = false;
253   HasFMA4 = false;
254   HasXOP = false;
255   HasTBM = false;
256   HasMOVBE = false;
257   HasRDRAND = false;
258   HasF16C = false;
259   HasFSGSBase = false;
260   HasLZCNT = false;
261   HasBMI = false;
262   HasBMI2 = false;
263   HasVBMI = false;
264   HasIFMA = false;
265   HasRTM = false;
266   HasHLE = false;
267   HasERI = false;
268   HasCDI = false;
269   HasPFI = false;
270   HasDQI = false;
271   HasBWI = false;
272   HasVLX = false;
273   HasADX = false;
274   HasPKU = false;
275   HasSHA = false;
276   HasPRFCHW = false;
277   HasRDSEED = false;
278   HasLAHFSAHF = false;
279   HasMWAITX = false;
280   HasMPX = false;
281   IsBTMemSlow = false;
282   IsPMULLDSlow = false;
283   IsSHLDSlow = false;
284   IsUAMem16Slow = false;
285   IsUAMem32Slow = false;
286   HasSSEUnalignedMem = false;
287   HasCmpxchg16b = false;
288   UseLeaForSP = false;
289   HasFastPartialYMMWrite = false;
290   HasFastScalarFSQRT = false;
291   HasFastVectorFSQRT = false;
292   HasFastLZCNT = false;
293   HasSlowDivide32 = false;
294   HasSlowDivide64 = false;
295   PadShortFunctions = false;
296   CallRegIndirect = false;
297   LEAUsesAG = false;
298   SlowLEA = false;
299   SlowIncDec = false;
300   stackAlignment = 4;
301   // FIXME: this is a known good value for Yonah. How about others?
302   MaxInlineSizeThreshold = 128;
303   UseSoftFloat = false;
304 }
305
306 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
307                                                             StringRef FS) {
308   initializeEnvironment();
309   initSubtargetFeatures(CPU, FS);
310   return *this;
311 }
312
313 X86Subtarget::X86Subtarget(const Triple &TT, StringRef CPU, StringRef FS,
314                            const X86TargetMachine &TM,
315                            unsigned StackAlignOverride)
316     : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
317       PICStyle(PICStyles::None), TM(TM), TargetTriple(TT),
318       StackAlignOverride(StackAlignOverride),
319       In64BitMode(TargetTriple.getArch() == Triple::x86_64),
320       In32BitMode(TargetTriple.getArch() == Triple::x86 &&
321                   TargetTriple.getEnvironment() != Triple::CODE16),
322       In16BitMode(TargetTriple.getArch() == Triple::x86 &&
323                   TargetTriple.getEnvironment() == Triple::CODE16),
324       TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
325       TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) {
326   // Determine the PICStyle based on the target selected.
327   if (!isPositionIndependent())
328     setPICStyle(PICStyles::None);
329   else if (is64Bit())
330     setPICStyle(PICStyles::RIPRel);
331   else if (isTargetCOFF())
332     setPICStyle(PICStyles::None);
333   else if (isTargetDarwin())
334     setPICStyle(PICStyles::StubPIC);
335   else if (isTargetELF())
336     setPICStyle(PICStyles::GOT);
337 }
338
339 const CallLowering *X86Subtarget::getCallLowering() const {
340   assert(GISel && "Access to GlobalISel APIs not set");
341   return GISel->getCallLowering();
342 }
343
344 const InstructionSelector *X86Subtarget::getInstructionSelector() const {
345   assert(GISel && "Access to GlobalISel APIs not set");
346   return GISel->getInstructionSelector();
347 }
348
349 const LegalizerInfo *X86Subtarget::getLegalizerInfo() const {
350   assert(GISel && "Access to GlobalISel APIs not set");
351   return GISel->getLegalizerInfo();
352 }
353
354 const RegisterBankInfo *X86Subtarget::getRegBankInfo() const {
355   assert(GISel && "Access to GlobalISel APIs not set");
356   return GISel->getRegBankInfo();
357 }
358
359 bool X86Subtarget::enableEarlyIfConversion() const {
360   return hasCMov() && X86EarlyIfConv;
361 }
362