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 bool isDecl = GV->isDeclarationForLinker();
73 // X86-64 in PIC mode.
74 if (isPICStyleRIPRel()) {
75 // Large model never uses stubs.
76 if (TM.getCodeModel() == CodeModel::Large)
77 return X86II::MO_NO_FLAG;
79 if (isTargetDarwin()) {
80 // If symbol visibility is hidden, the extra load is not needed if
81 // target is x86-64 or the symbol is definitely defined in the current
83 if (GV->hasDefaultVisibility() &&
84 (isDecl || GV->isWeakForLinker()))
85 return X86II::MO_GOTPCREL;
86 } else if (!isTargetWin64()) {
87 assert(isTargetELF() && "Unknown rip-relative target");
89 // Extra load is needed for all externally visible.
90 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
91 return X86II::MO_GOTPCREL;
94 return X86II::MO_NO_FLAG;
97 if (isPICStyleGOT()) { // 32-bit ELF targets.
98 // Extra load is needed for all externally visible.
99 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
100 return X86II::MO_GOTOFF;
101 return X86II::MO_GOT;
104 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
105 // Determine whether we have a stub reference and/or whether the reference
106 // is relative to the PIC base or not.
108 // If this is a strong reference to a definition, it is definitely not
110 if (!isDecl && !GV->isWeakForLinker())
111 return X86II::MO_PIC_BASE_OFFSET;
113 // Unless we have a symbol with hidden visibility, we have to go through a
114 // normal $non_lazy_ptr stub because this symbol might be resolved late.
115 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
116 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
118 // If symbol visibility is hidden, we have a stub for common symbol
119 // references and external declarations.
120 if (isDecl || GV->hasCommonLinkage()) {
121 // Hidden $non_lazy_ptr reference.
122 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
125 // Otherwise, no stub.
126 return X86II::MO_PIC_BASE_OFFSET;
129 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
130 // Determine whether we have a stub reference.
132 // If this is a strong reference to a definition, it is definitely not
134 if (!isDecl && !GV->isWeakForLinker())
135 return X86II::MO_NO_FLAG;
137 // Unless we have a symbol with hidden visibility, we have to go through a
138 // normal $non_lazy_ptr stub because this symbol might be resolved late.
139 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
140 return X86II::MO_DARWIN_NONLAZY;
142 // Otherwise, no stub.
143 return X86II::MO_NO_FLAG;
146 // Direct static reference to global.
147 return X86II::MO_NO_FLAG;
151 /// getBZeroEntry - This function returns the name of a function which has an
152 /// interface like the non-standard bzero function, if such a function exists on
153 /// the current subtarget and it is considered prefereable over memset with zero
154 /// passed as the second argument. Otherwise it returns null.
155 const char *X86Subtarget::getBZeroEntry() const {
156 // Darwin 10 has a __bzero entry point for this purpose.
157 if (getTargetTriple().isMacOSX() &&
158 !getTargetTriple().isMacOSXVersionLT(10, 6))
164 bool X86Subtarget::hasSinCos() const {
165 return getTargetTriple().isMacOSX() &&
166 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
170 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
171 /// to immediate address.
172 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
173 // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
174 // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
175 // the following check for Win32 should be removed.
176 if (In64BitMode || isTargetWin32())
178 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
181 void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
182 std::string CPUName = CPU;
186 // Make sure 64-bit features are available in 64-bit mode. (But make sure
187 // SSE2 can be turned off explicitly.)
188 std::string FullFS = FS;
191 FullFS = "+64bit,+sse2," + FullFS;
193 FullFS = "+64bit,+sse2";
196 // If feature string is not empty, parse features string.
197 ParseSubtargetFeatures(CPUName, FullFS);
199 // Make sure the right MCSchedModel is used.
200 InitCPUSchedModel(CPUName);
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() {
231 X86SSELevel = NoMMXSSE;
232 X863DNowLevel = NoThreeDNow;
265 IsUAMem32Slow = false;
266 HasSSEUnalignedMem = false;
267 HasCmpxchg16b = false;
269 HasSlowDivide32 = false;
270 HasSlowDivide64 = false;
271 PadShortFunctions = false;
272 CallRegIndirect = false;
277 UseReciprocalEst = false;
279 // FIXME: this is a known good value for Yonah. How about others?
280 MaxInlineSizeThreshold = 128;
283 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
285 initializeEnvironment();
286 initSubtargetFeatures(CPU, FS);
290 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
291 const std::string &FS, const X86TargetMachine &TM,
292 unsigned StackAlignOverride)
293 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
294 PICStyle(PICStyles::None), TargetTriple(TT),
295 StackAlignOverride(StackAlignOverride),
296 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
297 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
298 TargetTriple.getEnvironment() != Triple::CODE16),
299 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
300 TargetTriple.getEnvironment() == Triple::CODE16),
301 TSInfo(*TM.getDataLayout()),
302 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
303 FrameLowering(TargetFrameLowering::StackGrowsDown, getStackAlignment(),
304 is64Bit() ? -8 : -4) {
305 // Determine the PICStyle based on the target selected.
306 if (TM.getRelocationModel() == Reloc::Static) {
307 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
308 setPICStyle(PICStyles::None);
309 } else if (is64Bit()) {
310 // PIC in 64 bit mode is always rip-rel.
311 setPICStyle(PICStyles::RIPRel);
312 } else if (isTargetCOFF()) {
313 setPICStyle(PICStyles::None);
314 } else if (isTargetDarwin()) {
315 if (TM.getRelocationModel() == Reloc::PIC_)
316 setPICStyle(PICStyles::StubPIC);
318 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
319 setPICStyle(PICStyles::StubDynamicNoPIC);
321 } else if (isTargetELF()) {
322 setPICStyle(PICStyles::GOT);
326 bool X86Subtarget::enableEarlyIfConversion() const {
327 return hasCMov() && X86EarlyIfConv;