1 //===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the X86 specific subclass of TargetSubtargetInfo.
12 //===----------------------------------------------------------------------===//
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"
34 #define DEBUG_TYPE "subtarget"
36 #define GET_SUBTARGETINFO_TARGET_DESC
37 #define GET_SUBTARGETINFO_CTOR
38 #include "X86GenSubtargetInfo.inc"
40 // Temporary option to control early if-conversion for x86 while adding machine
43 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
44 cl::desc("Enable early if-conversion on X86"));
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);
53 /// Classify a global variable reference for the current subtarget according to
54 /// how we should reference it in a non-pcrel context.
56 X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const {
57 return classifyGlobalReference(GV, *GV->getParent());
61 X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
62 // 64 bits can use %rip addressing for anything local.
64 return X86II::MO_NO_FLAG;
66 // If this is for a position dependent executable, the static linker can
68 if (!isPositionIndependent())
69 return X86II::MO_NO_FLAG;
71 // The COFF dynamic linker just patches the executable sections.
73 return X86II::MO_NO_FLAG;
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
80 if (GV && (GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
81 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
83 return X86II::MO_PIC_BASE_OFFSET;
86 return X86II::MO_GOTOFF;
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;
95 if (TM.shouldAssumeDSOLocal(M, GV))
96 return classifyLocalReference(GV);
99 return X86II::MO_DLLIMPORT;
102 return X86II::MO_GOTPCREL;
104 if (isTargetDarwin()) {
105 if (!isPositionIndependent())
106 return X86II::MO_DARWIN_NONLAZY;
107 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
110 return X86II::MO_GOT;
114 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const {
115 return classifyGlobalFunctionReference(GV, *GV->getParent());
119 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV,
120 const Module &M) const {
121 if (TM.shouldAssumeDSOLocal(M, GV))
122 return X86II::MO_NO_FLAG;
124 assert(!isTargetCOFF());
127 return X86II::MO_PLT;
130 auto *F = dyn_cast_or_null<Function>(GV);
131 if (F && F->hasFnAttribute(Attribute::NonLazyBind))
132 // If the function is marked as non-lazy, generate an indirect call
133 // which loads from the GOT directly. This avoids runtime overhead
134 // at the cost of eager binding (and one extra byte of encoding).
135 return X86II::MO_GOTPCREL;
136 return X86II::MO_NO_FLAG;
139 return X86II::MO_NO_FLAG;
142 /// This function returns the name of a function which has an interface like
143 /// the non-standard bzero function, if such a function exists on the
144 /// current subtarget and it is considered preferable over memset with zero
145 /// passed as the second argument. Otherwise it returns null.
146 const char *X86Subtarget::getBZeroEntry() const {
147 // Darwin 10 has a __bzero entry point for this purpose.
148 if (getTargetTriple().isMacOSX() &&
149 !getTargetTriple().isMacOSXVersionLT(10, 6))
155 bool X86Subtarget::hasSinCos() const {
156 return getTargetTriple().isMacOSX() &&
157 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
161 /// Return true if the subtarget allows calls to immediate address.
162 bool X86Subtarget::isLegalToCallImmediateAddr() const {
163 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
164 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
165 // the following check for Win32 should be removed.
166 if (In64BitMode || isTargetWin32())
168 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
171 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
172 std::string CPUName = CPU;
176 // Make sure 64-bit features are available in 64-bit mode. (But make sure
177 // SSE2 can be turned off explicitly.)
178 std::string FullFS = FS;
181 FullFS = "+64bit,+sse2," + FullFS;
183 FullFS = "+64bit,+sse2";
186 // LAHF/SAHF are always supported in non-64-bit mode.
189 FullFS = "+sahf," + FullFS;
195 // Parse features string and set the CPU.
196 ParseSubtargetFeatures(CPUName, FullFS);
198 // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of
199 // 16-bytes and under that are reasonably fast. These features were
200 // introduced with Intel's Nehalem/Silvermont and AMD's Family10h
201 // micro-architectures respectively.
202 if (hasSSE42() || hasSSE4A())
203 IsUAMem16Slow = false;
205 InstrItins = getInstrItineraryForCPU(CPUName);
207 // It's important to keep the MCSubtargetInfo feature bits in sync with
208 // target data structure which is shared with MC code emitter, etc.
210 ToggleFeature(X86::Mode64Bit);
211 else if (In32BitMode)
212 ToggleFeature(X86::Mode32Bit);
213 else if (In16BitMode)
214 ToggleFeature(X86::Mode16Bit);
216 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
218 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
219 << ", 3DNowLevel " << X863DNowLevel
220 << ", 64bit " << HasX86_64 << "\n");
221 assert((!In64BitMode || HasX86_64) &&
222 "64-bit code requested on a subtarget that doesn't support it!");
224 // Stack alignment is 16 bytes on Darwin, Linux, kFreeBSD and Solaris (both
225 // 32 and 64 bit) and for all 64-bit targets.
226 if (StackAlignOverride)
227 stackAlignment = StackAlignOverride;
228 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
229 isTargetKFreeBSD() || In64BitMode)
233 void X86Subtarget::initializeEnvironment() {
235 X863DNowLevel = NoThreeDNow;
279 IsUAMem16Slow = false;
280 IsUAMem32Slow = false;
281 HasSSEUnalignedMem = false;
282 HasCmpxchg16b = false;
284 HasFastPartialYMMWrite = false;
285 HasSlowDivide32 = false;
286 HasSlowDivide64 = false;
287 PadShortFunctions = false;
288 CallRegIndirect = false;
293 // FIXME: this is a known good value for Yonah. How about others?
294 MaxInlineSizeThreshold = 128;
295 UseSoftFloat = false;
298 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
300 initializeEnvironment();
301 initSubtargetFeatures(CPU, FS);
305 X86Subtarget::X86Subtarget(const Triple &TT, StringRef CPU, StringRef FS,
306 const X86TargetMachine &TM,
307 unsigned StackAlignOverride)
308 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
309 PICStyle(PICStyles::None), TM(TM), TargetTriple(TT),
310 StackAlignOverride(StackAlignOverride),
311 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
312 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
313 TargetTriple.getEnvironment() != Triple::CODE16),
314 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
315 TargetTriple.getEnvironment() == Triple::CODE16),
316 TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
317 TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) {
318 // Determine the PICStyle based on the target selected.
319 if (!isPositionIndependent())
320 setPICStyle(PICStyles::None);
322 setPICStyle(PICStyles::RIPRel);
323 else if (isTargetCOFF())
324 setPICStyle(PICStyles::None);
325 else if (isTargetDarwin())
326 setPICStyle(PICStyles::StubPIC);
327 else if (isTargetELF())
328 setPICStyle(PICStyles::GOT);
331 bool X86Subtarget::enableEarlyIfConversion() const {
332 return hasCMov() && X86EarlyIfConv;