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