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/GlobalValue.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/SmallVector.h"
24 #define GET_SUBTARGETINFO_CTOR
25 #define GET_SUBTARGETINFO_MC_DESC
26 #define GET_SUBTARGETINFO_TARGET_DESC
27 #include "X86GenSubtargetInfo.inc"
35 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
36 /// current subtarget according to how we should reference it in a non-pcrel
38 unsigned char X86Subtarget::
39 ClassifyBlockAddressReference() const {
40 if (isPICStyleGOT()) // 32-bit ELF targets.
41 return X86II::MO_GOTOFF;
43 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
44 return X86II::MO_PIC_BASE_OFFSET;
46 // Direct static reference to label.
47 return X86II::MO_NO_FLAG;
50 /// ClassifyGlobalReference - Classify a global variable reference for the
51 /// current subtarget according to how we should reference it in a non-pcrel
53 unsigned char X86Subtarget::
54 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
55 // DLLImport only exists on windows, it is implemented as a load from a
57 if (GV->hasDLLImportLinkage())
58 return X86II::MO_DLLIMPORT;
60 // Determine whether this is a reference to a definition or a declaration.
61 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
63 bool isDecl = GV->hasAvailableExternallyLinkage();
64 if (GV->isDeclaration() && !GV->isMaterializable())
67 // X86-64 in PIC mode.
68 if (isPICStyleRIPRel()) {
69 // Large model never uses stubs.
70 if (TM.getCodeModel() == CodeModel::Large)
71 return X86II::MO_NO_FLAG;
73 if (isTargetDarwin()) {
74 // If symbol visibility is hidden, the extra load is not needed if
75 // target is x86-64 or the symbol is definitely defined in the current
77 if (GV->hasDefaultVisibility() &&
78 (isDecl || GV->isWeakForLinker()))
79 return X86II::MO_GOTPCREL;
80 } else if (!isTargetWin64()) {
81 assert(isTargetELF() && "Unknown rip-relative target");
83 // Extra load is needed for all externally visible.
84 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
85 return X86II::MO_GOTPCREL;
88 return X86II::MO_NO_FLAG;
91 if (isPICStyleGOT()) { // 32-bit ELF targets.
92 // Extra load is needed for all externally visible.
93 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
94 return X86II::MO_GOTOFF;
98 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
99 // Determine whether we have a stub reference and/or whether the reference
100 // is relative to the PIC base or not.
102 // If this is a strong reference to a definition, it is definitely not
104 if (!isDecl && !GV->isWeakForLinker())
105 return X86II::MO_PIC_BASE_OFFSET;
107 // Unless we have a symbol with hidden visibility, we have to go through a
108 // normal $non_lazy_ptr stub because this symbol might be resolved late.
109 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
110 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
112 // If symbol visibility is hidden, we have a stub for common symbol
113 // references and external declarations.
114 if (isDecl || GV->hasCommonLinkage()) {
115 // Hidden $non_lazy_ptr reference.
116 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
119 // Otherwise, no stub.
120 return X86II::MO_PIC_BASE_OFFSET;
123 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
124 // Determine whether we have a stub reference.
126 // If this is a strong reference to a definition, it is definitely not
128 if (!isDecl && !GV->isWeakForLinker())
129 return X86II::MO_NO_FLAG;
131 // Unless we have a symbol with hidden visibility, we have to go through a
132 // normal $non_lazy_ptr stub because this symbol might be resolved late.
133 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
134 return X86II::MO_DARWIN_NONLAZY;
136 // Otherwise, no stub.
137 return X86II::MO_NO_FLAG;
140 // Direct static reference to global.
141 return X86II::MO_NO_FLAG;
145 /// getBZeroEntry - This function returns the name of a function which has an
146 /// interface like the non-standard bzero function, if such a function exists on
147 /// the current subtarget and it is considered prefereable over memset with zero
148 /// passed as the second argument. Otherwise it returns null.
149 const char *X86Subtarget::getBZeroEntry() const {
150 // Darwin 10 has a __bzero entry point for this purpose.
151 if (getTargetTriple().isMacOSX() &&
152 !getTargetTriple().isMacOSXVersionLT(10, 6))
158 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
159 /// to immediate address.
160 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
163 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
166 /// getSpecialAddressLatency - For targets where it is beneficial to
167 /// backschedule instructions that compute addresses, return a value
168 /// indicating the number of scheduling cycles of backscheduling that
169 /// should be attempted.
170 unsigned X86Subtarget::getSpecialAddressLatency() const {
171 // For x86 out-of-order targets, back-schedule address computations so
172 // that loads and stores aren't blocked.
173 // This value was chosen arbitrarily.
177 void X86Subtarget::AutoDetectSubtargetFeatures() {
178 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
184 if (X86_MC::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
187 X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
189 if ((EDX >> 15) & 1) HasCMov = true;
190 if ((EDX >> 23) & 1) X86SSELevel = MMX;
191 if ((EDX >> 25) & 1) X86SSELevel = SSE1;
192 if ((EDX >> 26) & 1) X86SSELevel = SSE2;
193 if (ECX & 0x1) X86SSELevel = SSE3;
194 if ((ECX >> 9) & 1) X86SSELevel = SSSE3;
195 if ((ECX >> 19) & 1) X86SSELevel = SSE41;
196 if ((ECX >> 20) & 1) X86SSELevel = SSE42;
197 // FIXME: AVX codegen support is not ready.
198 //if ((ECX >> 28) & 1) { HasAVX = true; X86SSELevel = NoMMXSSE; }
200 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
201 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
203 HasCLMUL = IsIntel && ((ECX >> 1) & 0x1);
204 HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
205 HasPOPCNT = IsIntel && ((ECX >> 23) & 0x1);
206 HasAES = IsIntel && ((ECX >> 25) & 0x1);
208 if (IsIntel || IsAMD) {
209 // Determine if bit test memory instructions are slow.
212 X86_MC::DetectFamilyModel(EAX, Family, Model);
213 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
214 // If it's Nehalem, unaligned memory access is fast.
215 if (Family == 15 && Model == 26)
218 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
219 HasX86_64 = (EDX >> 29) & 0x1;
220 HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
221 HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
225 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
226 const std::string &FS,
227 unsigned StackAlignOverride)
228 : X86GenSubtargetInfo(TT, CPU, FS)
229 , PICStyle(PICStyles::None)
230 , X86SSELevel(NoMMXSSE)
231 , X863DNowLevel(NoThreeDNow)
243 , HasVectorUAMem(false)
245 // FIXME: this is a known good value for Yonah. How about others?
246 , MaxInlineSizeThreshold(128)
248 , In64BitMode(false) {
249 // Insert the architecture feature derived from the target triple into the
250 // feature string. This is important for setting features that are implied
251 // based on the architecture version.
252 std::string ArchFS = X86_MC::ParseX86Triple(TT);
255 ArchFS = ArchFS + "," + FS;
260 std::string CPUName = CPU;
262 CPUName = sys::getHostCPUName();
264 // Determine default and user specified characteristics
265 if (!CPUName.empty() || !ArchFS.empty()) {
266 // If feature string is not empty, parse features string.
267 ParseSubtargetFeatures(CPUName, ArchFS);
268 // All X86-64 CPUs also have SSE2, however user might request no SSE via
269 // -mattr, so don't force SSELevel here.
271 X86SSELevel = NoMMXSSE;
273 // Otherwise, use CPUID to auto-detect feature set.
274 AutoDetectSubtargetFeatures();
276 // If CPU is 64-bit capable, default to 64-bit mode if not specified.
277 In64BitMode = HasX86_64;
279 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
280 if (In64BitMode && !HasAVX && X86SSELevel < SSE2)
284 // If requesting codegen for X86-64, make sure that 64-bit features
286 // FIXME: Remove this feature since it's not actually being used.
290 // All 64-bit cpus have cmov support.
294 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
295 << ", 3DNowLevel " << X863DNowLevel
296 << ", 64bit " << HasX86_64 << "\n");
297 assert((!In64BitMode || HasX86_64) &&
298 "64-bit code requested on a subtarget that doesn't support it!");
300 // Stack alignment is 16 bytes on Darwin, FreeBSD, Linux and Solaris (both
301 // 32 and 64 bit) and for all 64-bit targets.
302 if (StackAlignOverride)
303 stackAlignment = StackAlignOverride;
304 else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
305 isTargetSolaris() || In64BitMode)