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 // Absolute symbols can be referenced directly.
96 if (GV && GV->isAbsoluteSymbolRef())
97 return X86II::MO_NO_FLAG;
99 if (TM.shouldAssumeDSOLocal(M, GV))
100 return classifyLocalReference(GV);
103 return X86II::MO_DLLIMPORT;
106 return X86II::MO_GOTPCREL;
108 if (isTargetDarwin()) {
109 if (!isPositionIndependent())
110 return X86II::MO_DARWIN_NONLAZY;
111 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
114 return X86II::MO_GOT;
118 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const {
119 return classifyGlobalFunctionReference(GV, *GV->getParent());
123 X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV,
124 const Module &M) const {
125 if (TM.shouldAssumeDSOLocal(M, GV))
126 return X86II::MO_NO_FLAG;
128 assert(!isTargetCOFF());
131 return X86II::MO_PLT;
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;
143 return X86II::MO_NO_FLAG;
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))
159 bool X86Subtarget::hasSinCos() const {
160 return getTargetTriple().isMacOSX() &&
161 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
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())
172 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
175 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
176 std::string CPUName = CPU;
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;
185 FullFS = "+64bit,+sse2," + FullFS;
187 FullFS = "+64bit,+sse2";
190 // LAHF/SAHF are always supported in non-64-bit mode.
193 FullFS = "+sahf," + FullFS;
199 // Parse features string and set the CPU.
200 ParseSubtargetFeatures(CPUName, FullFS);
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;
209 InstrItins = getInstrItineraryForCPU(CPUName);
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.
214 ToggleFeature(X86::Mode64Bit);
215 else if (In32BitMode)
216 ToggleFeature(X86::Mode32Bit);
217 else if (In16BitMode)
218 ToggleFeature(X86::Mode16Bit);
220 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
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!");
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)
237 void X86Subtarget::initializeEnvironment() {
239 X863DNowLevel = NoThreeDNow;
282 IsPMULLDSlow = false;
284 IsUAMem16Slow = false;
285 IsUAMem32Slow = false;
286 HasSSEUnalignedMem = false;
287 HasCmpxchg16b = 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;
301 // FIXME: this is a known good value for Yonah. How about others?
302 MaxInlineSizeThreshold = 128;
303 UseSoftFloat = false;
306 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
308 initializeEnvironment();
309 initSubtargetFeatures(CPU, FS);
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);
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);
339 const CallLowering *X86Subtarget::getCallLowering() const {
340 assert(GISel && "Access to GlobalISel APIs not set");
341 return GISel->getCallLowering();
344 const InstructionSelector *X86Subtarget::getInstructionSelector() const {
345 assert(GISel && "Access to GlobalISel APIs not set");
346 return GISel->getInstructionSelector();
349 const LegalizerInfo *X86Subtarget::getLegalizerInfo() const {
350 assert(GISel && "Access to GlobalISel APIs not set");
351 return GISel->getLegalizerInfo();
354 const RegisterBankInfo *X86Subtarget::getRegBankInfo() const {
355 assert(GISel && "Access to GlobalISel APIs not set");
356 return GISel->getRegBankInfo();
359 bool X86Subtarget::enableEarlyIfConversion() const {
360 return hasCMov() && X86EarlyIfConv;