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 // Parse features string and set the CPU.
193 ParseSubtargetFeatures(CPUName, FullFS);
195 // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of
196 // 16-bytes and under that are reasonably fast. These features were
197 // introduced with Intel's Nehalem/Silvermont and AMD's Family10h
198 // micro-architectures respectively.
199 if (hasSSE42() || hasSSE4A())
200 IsUAMem16Slow = false;
202 InstrItins = getInstrItineraryForCPU(CPUName);
204 // It's important to keep the MCSubtargetInfo feature bits in sync with
205 // target data structure which is shared with MC code emitter, etc.
207 ToggleFeature(X86::Mode64Bit);
208 else if (In32BitMode)
209 ToggleFeature(X86::Mode32Bit);
210 else if (In16BitMode)
211 ToggleFeature(X86::Mode16Bit);
213 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
215 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
216 << ", 3DNowLevel " << X863DNowLevel
217 << ", 64bit " << HasX86_64 << "\n");
218 assert((!In64BitMode || HasX86_64) &&
219 "64-bit code requested on a subtarget that doesn't support it!");
221 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
222 // 32 and 64 bit) and for all 64-bit targets.
223 if (StackAlignOverride)
224 stackAlignment = StackAlignOverride;
225 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
230 void X86Subtarget::initializeEnvironment() {
232 X863DNowLevel = NoThreeDNow;
270 IsUAMem16Slow = false;
271 IsUAMem32Slow = false;
272 HasSSEUnalignedMem = false;
273 HasCmpxchg16b = false;
275 HasSlowDivide32 = false;
276 HasSlowDivide64 = false;
277 PadShortFunctions = false;
278 CallRegIndirect = false;
283 // FIXME: this is a known good value for Yonah. How about others?
284 MaxInlineSizeThreshold = 128;
285 UseSoftFloat = false;
288 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
290 initializeEnvironment();
291 initSubtargetFeatures(CPU, FS);
295 X86Subtarget::X86Subtarget(const Triple &TT, const std::string &CPU,
296 const std::string &FS, const X86TargetMachine &TM,
297 unsigned StackAlignOverride)
298 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
299 PICStyle(PICStyles::None), TargetTriple(TT),
300 StackAlignOverride(StackAlignOverride),
301 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
302 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
303 TargetTriple.getEnvironment() != Triple::CODE16),
304 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
305 TargetTriple.getEnvironment() == Triple::CODE16),
306 TSInfo(), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
307 TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) {
308 // Determine the PICStyle based on the target selected.
309 if (TM.getRelocationModel() == Reloc::Static) {
310 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
311 setPICStyle(PICStyles::None);
312 } else if (is64Bit()) {
313 // PIC in 64 bit mode is always rip-rel.
314 setPICStyle(PICStyles::RIPRel);
315 } else if (isTargetCOFF()) {
316 setPICStyle(PICStyles::None);
317 } else if (isTargetDarwin()) {
318 if (TM.getRelocationModel() == Reloc::PIC_)
319 setPICStyle(PICStyles::StubPIC);
321 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
322 setPICStyle(PICStyles::StubDynamicNoPIC);
324 } else if (isTargetELF()) {
325 setPICStyle(PICStyles::GOT);
329 bool X86Subtarget::enableEarlyIfConversion() const {
330 return hasCMov() && X86EarlyIfConv;