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::initSubtargetFeatures(StringRef CPU, StringRef FS) {
187 std::string CPUName = CPU;
191 // Make sure 64-bit features are available in 64-bit mode. (But make sure
192 // SSE2 can be turned off explicitly.)
193 std::string FullFS = FS;
196 FullFS = "+64bit,+sse2," + FullFS;
198 FullFS = "+64bit,+sse2";
201 // If feature string is not empty, parse features string.
202 ParseSubtargetFeatures(CPUName, FullFS);
204 // Make sure the right MCSchedModel is used.
205 InitCPUSchedModel(CPUName);
207 InstrItins = getInstrItineraryForCPU(CPUName);
209 // It's important to keep the MCSubtargetInfo feature bits in sync with
210 // target data structure which is shared with MC code emitter, etc.
212 ToggleFeature(X86::Mode64Bit);
213 else if (In32BitMode)
214 ToggleFeature(X86::Mode32Bit);
215 else if (In16BitMode)
216 ToggleFeature(X86::Mode16Bit);
218 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
220 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
221 << ", 3DNowLevel " << X863DNowLevel
222 << ", 64bit " << HasX86_64 << "\n");
223 assert((!In64BitMode || HasX86_64) &&
224 "64-bit code requested on a subtarget that doesn't support it!");
226 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
227 // 32 and 64 bit) and for all 64-bit targets.
228 if (StackAlignOverride)
229 stackAlignment = StackAlignOverride;
230 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
235 void X86Subtarget::initializeEnvironment() {
236 X86SSELevel = NoMMXSSE;
237 X863DNowLevel = NoThreeDNow;
272 HasVectorUAMem = false;
273 HasCmpxchg16b = false;
275 HasSlowDivide = false;
276 PadShortFunctions = false;
277 CallRegIndirect = false;
282 // FIXME: this is a known good value for Yonah. How about others?
283 MaxInlineSizeThreshold = 128;
286 static std::string computeDataLayout(const Triple &TT) {
287 // X86 is little endian
288 std::string Ret = "e";
290 Ret += DataLayout::getManglingComponent(TT);
291 // X86 and x32 have 32 bit pointers.
292 if ((TT.isArch64Bit() &&
293 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
297 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
298 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
303 // Some ABIs align long double to 128 bits, others to 32.
306 else if (TT.isArch64Bit() || TT.isOSDarwin())
311 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
312 if (TT.isArch64Bit())
313 Ret += "-n8:16:32:64";
317 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
318 if (!TT.isArch64Bit() && TT.isOSWindows())
326 X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
328 initializeEnvironment();
329 initSubtargetFeatures(CPU, FS);
333 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
334 const std::string &FS, X86TargetMachine &TM,
335 unsigned StackAlignOverride)
336 : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
337 PICStyle(PICStyles::None), TargetTriple(TT),
338 DL(computeDataLayout(TargetTriple)),
339 StackAlignOverride(StackAlignOverride),
340 In64BitMode(TargetTriple.getArch() == Triple::x86_64),
341 In32BitMode(TargetTriple.getArch() == Triple::x86 &&
342 TargetTriple.getEnvironment() != Triple::CODE16),
343 In16BitMode(TargetTriple.getArch() == Triple::x86 &&
344 TargetTriple.getEnvironment() == Triple::CODE16),
345 TSInfo(DL), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
346 TLInfo(TM), FrameLowering(TargetFrameLowering::StackGrowsDown,
347 getStackAlignment(), is64Bit() ? -8 : -4) {
348 // Determine the PICStyle based on the target selected.
349 if (TM.getRelocationModel() == Reloc::Static) {
350 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
351 setPICStyle(PICStyles::None);
352 } else if (is64Bit()) {
353 // PIC in 64 bit mode is always rip-rel.
354 setPICStyle(PICStyles::RIPRel);
355 } else if (isTargetCOFF()) {
356 setPICStyle(PICStyles::None);
357 } else if (isTargetDarwin()) {
358 if (TM.getRelocationModel() == Reloc::PIC_)
359 setPICStyle(PICStyles::StubPIC);
361 assert(TM.getRelocationModel() == Reloc::DynamicNoPIC);
362 setPICStyle(PICStyles::StubDynamicNoPIC);
364 } else if (isTargetELF()) {
365 setPICStyle(PICStyles::GOT);
369 bool X86Subtarget::enableEarlyIfConversion() const {
370 return hasCMov() && X86EarlyIfConv;