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 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
27 #define GET_SUBTARGETINFO_TARGET_DESC
28 #define GET_SUBTARGETINFO_CTOR
29 #include "X86GenSubtargetInfo.inc"
37 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
38 /// current subtarget according to how we should reference it in a non-pcrel
40 unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
41 if (isPICStyleGOT()) // 32-bit ELF targets.
42 return X86II::MO_GOTOFF;
44 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
45 return X86II::MO_PIC_BASE_OFFSET;
47 // Direct static reference to label.
48 return X86II::MO_NO_FLAG;
51 /// ClassifyGlobalReference - Classify a global variable reference for the
52 /// current subtarget according to how we should reference it in a non-pcrel
54 unsigned char X86Subtarget::
55 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
56 // DLLImport only exists on windows, it is implemented as a load from a
58 if (GV->hasDLLImportStorageClass())
59 return X86II::MO_DLLIMPORT;
61 // Determine whether this is a reference to a definition or a declaration.
62 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
64 bool isDecl = GV->hasAvailableExternallyLinkage();
65 if (GV->isDeclaration() && !GV->isMaterializable())
68 // X86-64 in PIC mode.
69 if (isPICStyleRIPRel()) {
70 // Large model never uses stubs.
71 if (TM.getCodeModel() == CodeModel::Large)
72 return X86II::MO_NO_FLAG;
74 if (isTargetDarwin()) {
75 // If symbol visibility is hidden, the extra load is not needed if
76 // target is x86-64 or the symbol is definitely defined in the current
78 if (GV->hasDefaultVisibility() &&
79 (isDecl || GV->isWeakForLinker()))
80 return X86II::MO_GOTPCREL;
81 } else if (!isTargetWin64()) {
82 assert(isTargetELF() && "Unknown rip-relative target");
84 // Extra load is needed for all externally visible.
85 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
86 return X86II::MO_GOTPCREL;
89 return X86II::MO_NO_FLAG;
92 if (isPICStyleGOT()) { // 32-bit ELF targets.
93 // Extra load is needed for all externally visible.
94 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
95 return X86II::MO_GOTOFF;
99 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
100 // Determine whether we have a stub reference and/or whether the reference
101 // is relative to the PIC base or not.
103 // If this is a strong reference to a definition, it is definitely not
105 if (!isDecl && !GV->isWeakForLinker())
106 return X86II::MO_PIC_BASE_OFFSET;
108 // Unless we have a symbol with hidden visibility, we have to go through a
109 // normal $non_lazy_ptr stub because this symbol might be resolved late.
110 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
111 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
113 // If symbol visibility is hidden, we have a stub for common symbol
114 // references and external declarations.
115 if (isDecl || GV->hasCommonLinkage()) {
116 // Hidden $non_lazy_ptr reference.
117 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
120 // Otherwise, no stub.
121 return X86II::MO_PIC_BASE_OFFSET;
124 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
125 // Determine whether we have a stub reference.
127 // If this is a strong reference to a definition, it is definitely not
129 if (!isDecl && !GV->isWeakForLinker())
130 return X86II::MO_NO_FLAG;
132 // Unless we have a symbol with hidden visibility, we have to go through a
133 // normal $non_lazy_ptr stub because this symbol might be resolved late.
134 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
135 return X86II::MO_DARWIN_NONLAZY;
137 // Otherwise, no stub.
138 return X86II::MO_NO_FLAG;
141 // Direct static reference to global.
142 return X86II::MO_NO_FLAG;
146 /// getBZeroEntry - This function returns the name of a function which has an
147 /// interface like the non-standard bzero function, if such a function exists on
148 /// the current subtarget and it is considered prefereable 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 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
166 /// to immediate address.
167 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
168 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
169 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
170 // the following check for Win32 should be removed.
171 if (In64BitMode || isTargetWin32())
173 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
176 void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
177 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
178 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
180 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
183 !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
185 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
187 initializeEnvironment();
188 resetSubtargetFeatures(CPU, FS);
192 void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
193 std::string CPUName = CPU;
197 // Make sure 64-bit features are available in 64-bit mode. (But make sure
198 // SSE2 can be turned off explicitly.)
199 std::string FullFS = FS;
202 FullFS = "+64bit,+sse2," + FullFS;
204 FullFS = "+64bit,+sse2";
207 // If feature string is not empty, parse features string.
208 ParseSubtargetFeatures(CPUName, FullFS);
210 // Make sure the right MCSchedModel is used.
211 InitCPUSchedModel(CPUName);
213 if (X86ProcFamily == IntelAtom || X86ProcFamily == IntelSLM)
214 PostRAScheduler = true;
216 InstrItins = getInstrItineraryForCPU(CPUName);
218 // It's important to keep the MCSubtargetInfo feature bits in sync with
219 // target data structure which is shared with MC code emitter, etc.
221 ToggleFeature(X86::Mode64Bit);
222 else if (In32BitMode)
223 ToggleFeature(X86::Mode32Bit);
224 else if (In16BitMode)
225 ToggleFeature(X86::Mode16Bit);
227 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
229 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
230 << ", 3DNowLevel " << X863DNowLevel
231 << ", 64bit " << HasX86_64 << "\n");
232 assert((!In64BitMode || HasX86_64) &&
233 "64-bit code requested on a subtarget that doesn't support it!");
235 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
236 // 32 and 64 bit) and for all 64-bit targets.
237 if (StackAlignOverride)
238 stackAlignment = StackAlignOverride;
239 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
244 void X86Subtarget::initializeEnvironment() {
245 X86SSELevel = NoMMXSSE;
246 X863DNowLevel = NoThreeDNow;
276 HasVectorUAMem = false;
277 HasCmpxchg16b = false;
279 HasSlowDivide = false;
280 PostRAScheduler = false;
281 PadShortFunctions = false;
282 CallRegIndirect = false;
285 // FIXME: this is a known good value for Yonah. How about others?
286 MaxInlineSizeThreshold = 128;
289 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
290 const std::string &FS,
291 unsigned StackAlignOverride)
292 : X86GenSubtargetInfo(TT, CPU, FS)
293 , X86ProcFamily(Others)
294 , PICStyle(PICStyles::None)
296 , StackAlignOverride(StackAlignOverride)
297 , In64BitMode(TargetTriple.getArch() == Triple::x86_64)
298 , In32BitMode(TargetTriple.getArch() == Triple::x86 &&
299 TargetTriple.getEnvironment() != Triple::CODE16)
300 , In16BitMode(TargetTriple.getArch() == Triple::x86 &&
301 TargetTriple.getEnvironment() == Triple::CODE16) {
302 initializeEnvironment();
303 resetSubtargetFeatures(CPU, FS);
306 bool X86Subtarget::enablePostRAScheduler(
307 CodeGenOpt::Level OptLevel,
308 TargetSubtargetInfo::AntiDepBreakMode& Mode,
309 RegClassVector& CriticalPathRCs) const {
310 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
311 CriticalPathRCs.clear();
312 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;