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 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
48 /// current subtarget according to how we should reference it in a non-pcrel
50 unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
51 if (isPICStyleGOT()) // 32-bit ELF targets.
52 return X86II::MO_GOTOFF;
54 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
55 return X86II::MO_PIC_BASE_OFFSET;
57 // Direct static reference to label.
58 return X86II::MO_NO_FLAG;
61 /// ClassifyGlobalReference - Classify a global variable reference for the
62 /// current subtarget according to how we should reference it in a non-pcrel
64 unsigned char X86Subtarget::
65 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
66 // DLLImport only exists on windows, it is implemented as a load from a
68 if (GV->hasDLLImportStorageClass())
69 return X86II::MO_DLLIMPORT;
71 // Determine whether this is a reference to a definition or a declaration.
72 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
74 bool isDecl = GV->hasAvailableExternallyLinkage();
75 if (GV->isDeclaration() && !GV->isMaterializable())
78 // X86-64 in PIC mode.
79 if (isPICStyleRIPRel()) {
80 // Large model never uses stubs.
81 if (TM.getCodeModel() == CodeModel::Large)
82 return X86II::MO_NO_FLAG;
84 if (isTargetDarwin()) {
85 // If symbol visibility is hidden, the extra load is not needed if
86 // target is x86-64 or the symbol is definitely defined in the current
88 if (GV->hasDefaultVisibility() &&
89 (isDecl || GV->isWeakForLinker()))
90 return X86II::MO_GOTPCREL;
91 } else if (!isTargetWin64()) {
92 assert(isTargetELF() && "Unknown rip-relative target");
94 // Extra load is needed for all externally visible.
95 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
96 return X86II::MO_GOTPCREL;
99 return X86II::MO_NO_FLAG;
102 if (isPICStyleGOT()) { // 32-bit ELF targets.
103 // Extra load is needed for all externally visible.
104 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
105 return X86II::MO_GOTOFF;
106 return X86II::MO_GOT;
109 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
110 // Determine whether we have a stub reference and/or whether the reference
111 // is relative to the PIC base or not.
113 // If this is a strong reference to a definition, it is definitely not
115 if (!isDecl && !GV->isWeakForLinker())
116 return X86II::MO_PIC_BASE_OFFSET;
118 // Unless we have a symbol with hidden visibility, we have to go through a
119 // normal $non_lazy_ptr stub because this symbol might be resolved late.
120 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
121 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
123 // If symbol visibility is hidden, we have a stub for common symbol
124 // references and external declarations.
125 if (isDecl || GV->hasCommonLinkage()) {
126 // Hidden $non_lazy_ptr reference.
127 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
130 // Otherwise, no stub.
131 return X86II::MO_PIC_BASE_OFFSET;
134 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
135 // Determine whether we have a stub reference.
137 // If this is a strong reference to a definition, it is definitely not
139 if (!isDecl && !GV->isWeakForLinker())
140 return X86II::MO_NO_FLAG;
142 // Unless we have a symbol with hidden visibility, we have to go through a
143 // normal $non_lazy_ptr stub because this symbol might be resolved late.
144 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
145 return X86II::MO_DARWIN_NONLAZY;
147 // Otherwise, no stub.
148 return X86II::MO_NO_FLAG;
151 // Direct static reference to global.
152 return X86II::MO_NO_FLAG;
156 /// getBZeroEntry - This function returns the name of a function which has an
157 /// interface like the non-standard bzero function, if such a function exists on
158 /// the current subtarget and it is considered prefereable over memset with zero
159 /// passed as the second argument. Otherwise it returns null.
160 const char *X86Subtarget::getBZeroEntry() const {
161 // Darwin 10 has a __bzero entry point for this purpose.
162 if (getTargetTriple().isMacOSX() &&
163 !getTargetTriple().isMacOSXVersionLT(10, 6))
169 bool X86Subtarget::hasSinCos() const {
170 return getTargetTriple().isMacOSX() &&
171 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
175 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
176 /// to immediate address.
177 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
178 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
179 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
180 // the following check for Win32 should be removed.
181 if (In64BitMode || isTargetWin32())
183 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
186 void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
187 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
189 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
191 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
193 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
195 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
197 initializeEnvironment();
198 resetSubtargetFeatures(CPU, FS);
202 void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
203 std::string CPUName = CPU;
207 // Make sure 64-bit features are available in 64-bit mode. (But make sure
208 // SSE2 can be turned off explicitly.)
209 std::string FullFS = FS;
212 FullFS = "+64bit,+sse2," + FullFS;
214 FullFS = "+64bit,+sse2";
217 // If feature string is not empty, parse features string.
218 ParseSubtargetFeatures(CPUName, FullFS);
220 // Make sure the right MCSchedModel is used.
221 InitCPUSchedModel(CPUName);
223 InstrItins = getInstrItineraryForCPU(CPUName);
225 // It's important to keep the MCSubtargetInfo feature bits in sync with
226 // target data structure which is shared with MC code emitter, etc.
228 ToggleFeature(X86::Mode64Bit);
229 else if (In32BitMode)
230 ToggleFeature(X86::Mode32Bit);
231 else if (In16BitMode)
232 ToggleFeature(X86::Mode16Bit);
234 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
236 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
237 << ", 3DNowLevel " << X863DNowLevel
238 << ", 64bit " << HasX86_64 << "\n");
239 assert((!In64BitMode || HasX86_64) &&
240 "64-bit code requested on a subtarget that doesn't support it!");
242 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
243 // 32 and 64 bit) and for all 64-bit targets.
244 if (StackAlignOverride)
245 stackAlignment = StackAlignOverride;
246 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
251 void X86Subtarget::initializeEnvironment() {
252 X86SSELevel = NoMMXSSE;
253 X863DNowLevel = NoThreeDNow;
288 HasVectorUAMem = false;
289 HasCmpxchg16b = false;
291 HasSlowDivide = false;
292 PadShortFunctions = false;
293 CallRegIndirect = false;
298 // FIXME: this is a known good value for Yonah. How about others?
299 MaxInlineSizeThreshold = 128;
302 static std::string computeDataLayout(const Triple &TT) {
303 // X86 is little endian
304 std::string Ret = "e";
306 Ret += DataLayout::getManglingComponent(TT);
307 // X86 and x32 have 32 bit pointers.
308 if ((TT.isArch64Bit() &&
309 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
313 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
314 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
319 // Some ABIs align long double to 128 bits, others to 32.
322 else if (TT.isArch64Bit() || TT.isOSDarwin())
327 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
328 if (TT.isArch64Bit())
329 Ret += "-n8:16:32:64";
333 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
334 if (!TT.isArch64Bit() && TT.isOSWindows())
342 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
344 initializeEnvironment();
345 resetSubtargetFeatures(CPU, FS);
349 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
350 const std::string &FS, X86TargetMachine &TM,
351 unsigned StackAlignOverride)
352 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
353 PICStyle(PICStyles::None), TargetTriple(TT),
354 DL(computeDataLayout(TargetTriple)),
355 StackAlignOverride(StackAlignOverride),
356 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
357 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
358 TargetTriple.getEnvironment() != Triple::CODE16),
359 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
360 TargetTriple.getEnvironment() == Triple::CODE16),
361 TSInfo(DL), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
362 TLInfo(TM), FrameLowering(TargetFrameLowering::StackGrowsDown,
363 getStackAlignment(), is64Bit() ? -8 : -4),
365 // Determine the PICStyle based on the target selected.
366 if (TM.getRelocationModel() == Reloc::Static) {
367 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
368 setPICStyle(PICStyles::None);
369 } else if (is64Bit()) {
370 // PIC in 64 bit mode is always rip-rel.
371 setPICStyle(PICStyles::RIPRel);
372 } else if (isTargetCOFF()) {
373 setPICStyle(PICStyles::None);
374 } else if (isTargetDarwin()) {
375 if (TM.getRelocationModel() == Reloc::PIC_)
376 setPICStyle(PICStyles::StubPIC);
378 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
379 setPICStyle(PICStyles::StubDynamicNoPIC);
381 } else if (isTargetELF()) {
382 setPICStyle(PICStyles::GOT);
386 bool X86Subtarget::enableEarlyIfConversion() const {
387 return hasCMov() && X86EarlyIfConv;