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 if (isPICStyleGOT()) // 32-bit ELF targets.
51 return X86II::MO_GOTOFF;
53 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
54 return X86II::MO_PIC_BASE_OFFSET;
56 // Direct static reference to label.
57 return X86II::MO_NO_FLAG;
60 /// Classify a global variable reference for the current subtarget according to
61 /// how we should reference it in a non-pcrel context.
62 unsigned char X86Subtarget::
63 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
64 // DLLImport only exists on windows, it is implemented as a load from a
66 if (GV->hasDLLImportStorageClass())
67 return X86II::MO_DLLIMPORT;
69 bool isDef = GV->isStrongDefinitionForLinker();
71 // X86-64 in PIC mode.
72 if (isPICStyleRIPRel()) {
73 // Large model never uses stubs.
74 if (TM.getCodeModel() == CodeModel::Large)
75 return X86II::MO_NO_FLAG;
77 if (isTargetDarwin()) {
78 // If symbol visibility is hidden, the extra load is not needed if
79 // target is x86-64 or the symbol is definitely defined in the current
81 if (GV->hasDefaultVisibility() && !isDef)
82 return X86II::MO_GOTPCREL;
83 } else if (!isTargetWin64()) {
84 assert(isTargetELF() && "Unknown rip-relative target");
86 // Extra load is needed for all externally visible.
87 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
88 return X86II::MO_GOTPCREL;
91 return X86II::MO_NO_FLAG;
94 if (isPICStyleGOT()) { // 32-bit ELF targets.
95 // Extra load is needed for all externally visible.
96 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
97 return X86II::MO_GOTOFF;
101 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
102 // Determine whether we have a stub reference and/or whether the reference
103 // is relative to the PIC base or not.
105 // If this is a strong reference to a definition, it is definitely not
108 return X86II::MO_PIC_BASE_OFFSET;
110 // Unless we have a symbol with hidden visibility, we have to go through a
111 // normal $non_lazy_ptr stub because this symbol might be resolved late.
112 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
113 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
115 // If symbol visibility is hidden, we have a stub for common symbol
116 // references and external declarations.
117 if (GV->isDeclarationForLinker() || GV->hasCommonLinkage()) {
118 // Hidden $non_lazy_ptr reference.
119 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
122 // Otherwise, no stub.
123 return X86II::MO_PIC_BASE_OFFSET;
126 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
127 // Determine whether we have a stub reference.
129 // If this is a strong reference to a definition, it is definitely not
132 return X86II::MO_NO_FLAG;
134 // Unless we have a symbol with hidden visibility, we have to go through a
135 // normal $non_lazy_ptr stub because this symbol might be resolved late.
136 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
137 return X86II::MO_DARWIN_NONLAZY;
139 // Otherwise, no stub.
140 return X86II::MO_NO_FLAG;
143 // Direct static reference to global.
144 return X86II::MO_NO_FLAG;
148 /// This function returns the name of a function which has an interface like
149 /// the non-standard bzero function, if such a function exists on the
150 /// current subtarget and it is considered preferable over memset with zero
151 /// passed as the second argument. Otherwise it returns null.
152 const char *X86Subtarget::getBZeroEntry() const {
153 // Darwin 10 has a __bzero entry point for this purpose.
154 if (getTargetTriple().isMacOSX() &&
155 !getTargetTriple().isMacOSXVersionLT(10, 6))
161 bool X86Subtarget::hasSinCos() const {
162 return getTargetTriple().isMacOSX() &&
163 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
167 /// Return true if the subtarget allows calls to immediate address.
168 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
169 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
170 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
171 // the following check for Win32 should be removed.
172 if (In64BitMode || isTargetWin32())
174 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
177 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
178 std::string CPUName = CPU;
182 // Make sure 64-bit features are available in 64-bit mode. (But make sure
183 // SSE2 can be turned off explicitly.)
184 std::string FullFS = FS;
187 FullFS = "+64bit,+sse2," + FullFS;
189 FullFS = "+64bit,+sse2";
192 // LAHF/SAHF are always supported in non-64-bit mode.
195 FullFS = "+sahf," + FullFS;
201 // Parse features string and set the CPU.
202 ParseSubtargetFeatures(CPUName, FullFS);
204 // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of
205 // 16-bytes and under that are reasonably fast. These features were
206 // introduced with Intel's Nehalem/Silvermont and AMD's Family10h
207 // micro-architectures respectively.
208 if (hasSSE42() || hasSSE4A())
209 IsUAMem16Slow = false;
211 InstrItins = getInstrItineraryForCPU(CPUName);
213 // It's important to keep the MCSubtargetInfo feature bits in sync with
214 // target data structure which is shared with MC code emitter, etc.
216 ToggleFeature(X86::Mode64Bit);
217 else if (In32BitMode)
218 ToggleFeature(X86::Mode32Bit);
219 else if (In16BitMode)
220 ToggleFeature(X86::Mode16Bit);
222 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
224 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
225 << ", 3DNowLevel " << X863DNowLevel
226 << ", 64bit " << HasX86_64 << "\n");
227 assert((!In64BitMode || HasX86_64) &&
228 "64-bit code requested on a subtarget that doesn't support it!");
230 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
231 // 32 and 64 bit) and for all 64-bit targets.
232 if (StackAlignOverride)
233 stackAlignment = StackAlignOverride;
234 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
239 void X86Subtarget::initializeEnvironment() {
241 X863DNowLevel = NoThreeDNow;
281 IsUAMem16Slow = false;
282 IsUAMem32Slow = false;
283 HasSSEUnalignedMem = false;
284 HasCmpxchg16b = false;
286 HasSlowDivide32 = false;
287 HasSlowDivide64 = false;
288 PadShortFunctions = false;
289 CallRegIndirect = false;
294 // FIXME: this is a known good value for Yonah. How about others?
295 MaxInlineSizeThreshold = 128;
296 UseSoftFloat = false;
299 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
301 initializeEnvironment();
302 initSubtargetFeatures(CPU, FS);
306 X86Subtarget::X86Subtarget(const Triple &TT, const std::string &CPU,
307 const std::string &FS, const X86TargetMachine &TM,
308 unsigned StackAlignOverride)
309 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
310 PICStyle(PICStyles::None), TargetTriple(TT),
311 StackAlignOverride(StackAlignOverride),
312 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
313 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
314 TargetTriple.getEnvironment() != Triple::CODE16),
315 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
316 TargetTriple.getEnvironment() == Triple::CODE16),
317 TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
318 TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) {
319 // Determine the PICStyle based on the target selected.
320 if (TM.getRelocationModel() == Reloc::Static) {
321 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
322 setPICStyle(PICStyles::None);
323 } else if (is64Bit()) {
324 // PIC in 64 bit mode is always rip-rel.
325 setPICStyle(PICStyles::RIPRel);
326 } else if (isTargetCOFF()) {
327 setPICStyle(PICStyles::None);
328 } else if (isTargetDarwin()) {
329 if (TM.getRelocationModel() == Reloc::PIC_)
330 setPICStyle(PICStyles::StubPIC);
332 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
333 setPICStyle(PICStyles::StubDynamicNoPIC);
335 } else if (isTargetELF()) {
336 setPICStyle(PICStyles::GOT);
340 bool X86Subtarget::enableEarlyIfConversion() const {
341 return hasCMov() && X86EarlyIfConv;