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 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
178 /// specified arguments. If we can't run cpuid on the host, return true.
179 static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
180 unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
181 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
182 #if defined(__GNUC__)
183 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
184 asm ("movq\t%%rbx, %%rsi\n\t"
186 "xchgq\t%%rbx, %%rsi\n\t"
193 #elif defined(_MSC_VER)
195 __cpuid(registers, value);
196 *rEAX = registers[0];
197 *rEBX = registers[1];
198 *rECX = registers[2];
199 *rEDX = registers[3];
202 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
203 #if defined(__GNUC__)
204 asm ("movl\t%%ebx, %%esi\n\t"
206 "xchgl\t%%ebx, %%esi\n\t"
213 #elif defined(_MSC_VER)
218 mov dword ptr [esi],eax
220 mov dword ptr [esi],ebx
222 mov dword ptr [esi],ecx
224 mov dword ptr [esi],edx
232 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
233 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
234 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
235 if (Family == 6 || Family == 0xf) {
237 // Examine extended family ID if family ID is F.
238 Family += (EAX >> 20) & 0xff; // Bits 20 - 27
239 // Examine extended model ID if family ID is 6 or F.
240 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
244 void X86Subtarget::AutoDetectSubtargetFeatures() {
245 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
251 if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
254 GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
256 if ((EDX >> 15) & 1) HasCMov = true;
257 if ((EDX >> 23) & 1) X86SSELevel = MMX;
258 if ((EDX >> 25) & 1) X86SSELevel = SSE1;
259 if ((EDX >> 26) & 1) X86SSELevel = SSE2;
260 if (ECX & 0x1) X86SSELevel = SSE3;
261 if ((ECX >> 9) & 1) X86SSELevel = SSSE3;
262 if ((ECX >> 19) & 1) X86SSELevel = SSE41;
263 if ((ECX >> 20) & 1) X86SSELevel = SSE42;
264 // FIXME: AVX codegen support is not ready.
265 //if ((ECX >> 28) & 1) { HasAVX = true; X86SSELevel = NoMMXSSE; }
267 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
268 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
270 HasCLMUL = IsIntel && ((ECX >> 1) & 0x1);
271 HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
272 HasPOPCNT = IsIntel && ((ECX >> 23) & 0x1);
273 HasAES = IsIntel && ((ECX >> 25) & 0x1);
275 if (IsIntel || IsAMD) {
276 // Determine if bit test memory instructions are slow.
279 DetectFamilyModel(EAX, Family, Model);
280 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
281 // If it's Nehalem, unaligned memory access is fast.
282 if (Family == 15 && Model == 26)
285 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
286 HasX86_64 = (EDX >> 29) & 0x1;
287 HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
288 HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
292 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
293 const std::string &FS,
294 bool is64Bit, unsigned StackAlignOverride)
295 : X86GenSubtargetInfo()
296 , PICStyle(PICStyles::None)
297 , X86SSELevel(NoMMXSSE)
298 , X863DNowLevel(NoThreeDNow)
310 , HasVectorUAMem(false)
312 // FIXME: this is a known good value for Yonah. How about others?
313 , MaxInlineSizeThreshold(128)
317 // Determine default and user specified characteristics
318 if (!CPU.empty() || !FS.empty()) {
319 // If feature string is not empty, parse features string.
320 std::string CPUName = CPU;
322 CPUName = sys::getHostCPUName();
323 ParseSubtargetFeatures(FS, CPUName);
324 // All X86-64 CPUs also have SSE2, however user might request no SSE via
325 // -mattr, so don't force SSELevel here.
327 X86SSELevel = NoMMXSSE;
329 // Otherwise, use CPUID to auto-detect feature set.
330 AutoDetectSubtargetFeatures();
331 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
332 if (Is64Bit && !HasAVX && X86SSELevel < SSE2)
336 // If requesting codegen for X86-64, make sure that 64-bit features
341 // All 64-bit cpus have cmov support.
345 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
346 << ", 3DNowLevel " << X863DNowLevel
347 << ", 64bit " << HasX86_64 << "\n");
348 assert((!Is64Bit || HasX86_64) &&
349 "64-bit code requested on a subtarget that doesn't support it!");
351 // Stack alignment is 16 bytes on Darwin, FreeBSD, Linux and Solaris (both
352 // 32 and 64 bit) and for all 64-bit targets.
353 if (StackAlignOverride)
354 stackAlignment = StackAlignOverride;
355 else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
356 isTargetSolaris() || Is64Bit)