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 "llvm/IR/Attributes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/Support/CommandLine.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"
33 #define DEBUG_TYPE "subtarget"
35 #define GET_SUBTARGETINFO_TARGET_DESC
36 #define GET_SUBTARGETINFO_CTOR
37 #include "X86GenSubtargetInfo.inc"
39 // Temporary option to control early if-conversion for x86 while adding machine
42 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
43 cl::desc("Enable early if-conversion on X86"));
46 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
47 /// current subtarget according to how we should reference it in a non-pcrel
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 /// ClassifyGlobalReference - Classify a global variable reference for the
61 /// current subtarget according to how we should reference it in a non-pcrel
63 unsigned char X86Subtarget::
64 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
65 // DLLImport only exists on windows, it is implemented as a load from a
67 if (GV->hasDLLImportStorageClass())
68 return X86II::MO_DLLIMPORT;
70 // Determine whether this is a reference to a definition or a declaration.
71 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
73 bool isDecl = GV->hasAvailableExternallyLinkage();
74 if (GV->isDeclaration() && !GV->isMaterializable())
77 // X86-64 in PIC mode.
78 if (isPICStyleRIPRel()) {
79 // Large model never uses stubs.
80 if (TM.getCodeModel() == CodeModel::Large)
81 return X86II::MO_NO_FLAG;
83 if (isTargetDarwin()) {
84 // If symbol visibility is hidden, the extra load is not needed if
85 // target is x86-64 or the symbol is definitely defined in the current
87 if (GV->hasDefaultVisibility() &&
88 (isDecl || GV->isWeakForLinker()))
89 return X86II::MO_GOTPCREL;
90 } else if (!isTargetWin64()) {
91 assert(isTargetELF() && "Unknown rip-relative target");
93 // Extra load is needed for all externally visible.
94 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
95 return X86II::MO_GOTPCREL;
98 return X86II::MO_NO_FLAG;
101 if (isPICStyleGOT()) { // 32-bit ELF targets.
102 // Extra load is needed for all externally visible.
103 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
104 return X86II::MO_GOTOFF;
105 return X86II::MO_GOT;
108 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
109 // Determine whether we have a stub reference and/or whether the reference
110 // is relative to the PIC base or not.
112 // If this is a strong reference to a definition, it is definitely not
114 if (!isDecl && !GV->isWeakForLinker())
115 return X86II::MO_PIC_BASE_OFFSET;
117 // Unless we have a symbol with hidden visibility, we have to go through a
118 // normal $non_lazy_ptr stub because this symbol might be resolved late.
119 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
120 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
122 // If symbol visibility is hidden, we have a stub for common symbol
123 // references and external declarations.
124 if (isDecl || GV->hasCommonLinkage()) {
125 // Hidden $non_lazy_ptr reference.
126 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
129 // Otherwise, no stub.
130 return X86II::MO_PIC_BASE_OFFSET;
133 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
134 // Determine whether we have a stub reference.
136 // If this is a strong reference to a definition, it is definitely not
138 if (!isDecl && !GV->isWeakForLinker())
139 return X86II::MO_NO_FLAG;
141 // Unless we have a symbol with hidden visibility, we have to go through a
142 // normal $non_lazy_ptr stub because this symbol might be resolved late.
143 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
144 return X86II::MO_DARWIN_NONLAZY;
146 // Otherwise, no stub.
147 return X86II::MO_NO_FLAG;
150 // Direct static reference to global.
151 return X86II::MO_NO_FLAG;
155 /// getBZeroEntry - This function returns the name of a function which has an
156 /// interface like the non-standard bzero function, if such a function exists on
157 /// the current subtarget and it is considered prefereable over memset with zero
158 /// passed as the second argument. Otherwise it returns null.
159 const char *X86Subtarget::getBZeroEntry() const {
160 // Darwin 10 has a __bzero entry point for this purpose.
161 if (getTargetTriple().isMacOSX() &&
162 !getTargetTriple().isMacOSXVersionLT(10, 6))
168 bool X86Subtarget::hasSinCos() const {
169 return getTargetTriple().isMacOSX() &&
170 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
174 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
175 /// to immediate address.
176 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
177 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
178 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
179 // the following check for Win32 should be removed.
180 if (In64BitMode || isTargetWin32())
182 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
185 void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
186 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
188 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
190 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
192 !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
194 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
196 initializeEnvironment();
197 resetSubtargetFeatures(CPU, FS);
201 void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
202 std::string CPUName = CPU;
206 // Make sure 64-bit features are available in 64-bit mode. (But make sure
207 // SSE2 can be turned off explicitly.)
208 std::string FullFS = FS;
211 FullFS = "+64bit,+sse2," + FullFS;
213 FullFS = "+64bit,+sse2";
216 // If feature string is not empty, parse features string.
217 ParseSubtargetFeatures(CPUName, FullFS);
219 // Make sure the right MCSchedModel is used.
220 InitCPUSchedModel(CPUName);
222 if (X86ProcFamily == IntelAtom || X86ProcFamily == IntelSLM)
223 PostRAScheduler = true;
225 InstrItins = getInstrItineraryForCPU(CPUName);
227 // It's important to keep the MCSubtargetInfo feature bits in sync with
228 // target data structure which is shared with MC code emitter, etc.
230 ToggleFeature(X86::Mode64Bit);
231 else if (In32BitMode)
232 ToggleFeature(X86::Mode32Bit);
233 else if (In16BitMode)
234 ToggleFeature(X86::Mode16Bit);
236 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
238 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
239 << ", 3DNowLevel " << X863DNowLevel
240 << ", 64bit " << HasX86_64 << "\n");
241 assert((!In64BitMode || HasX86_64) &&
242 "64-bit code requested on a subtarget that doesn't support it!");
244 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
245 // 32 and 64 bit) and for all 64-bit targets.
246 if (StackAlignOverride)
247 stackAlignment = StackAlignOverride;
248 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
253 void X86Subtarget::initializeEnvironment() {
254 X86SSELevel = NoMMXSSE;
255 X863DNowLevel = NoThreeDNow;
285 HasVectorUAMem = false;
286 HasCmpxchg16b = false;
288 HasSlowDivide = false;
289 PostRAScheduler = false;
290 PadShortFunctions = false;
291 CallRegIndirect = false;
295 // FIXME: this is a known good value for Yonah. How about others?
296 MaxInlineSizeThreshold = 128;
299 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
300 const std::string &FS, unsigned StackAlignOverride)
301 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
302 PICStyle(PICStyles::None), TargetTriple(TT),
303 StackAlignOverride(StackAlignOverride),
304 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
305 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
306 TargetTriple.getEnvironment() != Triple::CODE16),
307 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
308 TargetTriple.getEnvironment() == Triple::CODE16) {
309 initializeEnvironment();
310 resetSubtargetFeatures(CPU, FS);
314 X86Subtarget::enablePostRAScheduler(CodeGenOpt::Level OptLevel,
315 TargetSubtargetInfo::AntiDepBreakMode &Mode,
316 RegClassVector &CriticalPathRCs) const {
317 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
318 CriticalPathRCs.clear();
319 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
323 X86Subtarget::enableEarlyIfConversion() const {
324 return hasCMov() && X86EarlyIfConv;