1 //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
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 TargetSubtarget.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "X86GenSubtarget.inc"
18 #include "llvm/GlobalValue.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/System/Host.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/ADT/SmallVector.h"
31 /// ClassifyGlobalReference - Classify a global variable reference for the
32 /// current subtarget according to how we should reference it in a non-pcrel
34 unsigned char X86Subtarget::
35 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
36 // DLLImport only exists on windows, it is implemented as a load from a
38 if (GV->hasDLLImportLinkage())
39 return X86II::MO_DLLIMPORT;
41 // GV with ghost linkage (in JIT lazy compilation mode) do not require an
42 // extra load from stub.
43 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
45 // X86-64 in PIC mode.
46 if (isPICStyleRIPRel()) {
47 // Large model never uses stubs.
48 if (TM.getCodeModel() == CodeModel::Large)
49 return X86II::MO_NO_FLAG;
51 if (isTargetDarwin()) {
52 // If symbol visibility is hidden, the extra load is not needed if
53 // target is x86-64 or the symbol is definitely defined in the current
55 if (GV->hasDefaultVisibility() &&
56 (isDecl || GV->isWeakForLinker()))
57 return X86II::MO_GOTPCREL;
59 assert(isTargetELF() && "Unknown rip-relative target");
61 // Extra load is needed for all externally visible.
62 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
63 return X86II::MO_GOTPCREL;
66 return X86II::MO_NO_FLAG;
69 if (isPICStyleGOT()) { // 32-bit ELF targets.
70 // Extra load is needed for all externally visible.
71 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
72 return X86II::MO_GOTOFF;
76 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
77 // Determine whether we have a stub reference and/or whether the reference
78 // is relative to the PIC base or not.
80 // If this is a strong reference to a definition, it is definitely not
82 if (!isDecl && !GV->isWeakForLinker())
83 return X86II::MO_PIC_BASE_OFFSET;
85 // Unless we have a symbol with hidden visibility, we have to go through a
86 // normal $non_lazy_ptr stub because this symbol might be resolved late.
87 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
88 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
90 // If symbol visibility is hidden, we have a stub for common symbol
91 // references and external declarations.
92 if (isDecl || GV->hasCommonLinkage()) {
93 // Hidden $non_lazy_ptr reference.
94 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
97 // Otherwise, no stub.
98 return X86II::MO_PIC_BASE_OFFSET;
101 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
102 // Determine whether we have a stub reference.
104 // If this is a strong reference to a definition, it is definitely not
106 if (!isDecl && !GV->isWeakForLinker())
107 return X86II::MO_NO_FLAG;
109 // Unless we have a symbol with hidden visibility, we have to go through a
110 // normal $non_lazy_ptr stub because this symbol might be resolved late.
111 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
112 return X86II::MO_DARWIN_NONLAZY;
114 // Otherwise, no stub.
115 return X86II::MO_NO_FLAG;
118 // Direct static reference to global.
119 return X86II::MO_NO_FLAG;
123 /// getBZeroEntry - This function returns the name of a function which has an
124 /// interface like the non-standard bzero function, if such a function exists on
125 /// the current subtarget and it is considered prefereable over memset with zero
126 /// passed as the second argument. Otherwise it returns null.
127 const char *X86Subtarget::getBZeroEntry() const {
128 // Darwin 10 has a __bzero entry point for this purpose.
129 if (getDarwinVers() >= 10)
135 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
136 /// to immediate address.
137 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
140 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
143 /// getSpecialAddressLatency - For targets where it is beneficial to
144 /// backschedule instructions that compute addresses, return a value
145 /// indicating the number of scheduling cycles of backscheduling that
146 /// should be attempted.
147 unsigned X86Subtarget::getSpecialAddressLatency() const {
148 // For x86 out-of-order targets, back-schedule address computations so
149 // that loads and stores aren't blocked.
150 // This value was chosen arbitrarily.
154 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
155 /// specified arguments. If we can't run cpuid on the host, return true.
156 static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
157 unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
158 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
159 #if defined(__GNUC__)
160 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
161 asm ("movq\t%%rbx, %%rsi\n\t"
163 "xchgq\t%%rbx, %%rsi\n\t"
170 #elif defined(_MSC_VER)
172 __cpuid(registers, value);
173 *rEAX = registers[0];
174 *rEBX = registers[1];
175 *rECX = registers[2];
176 *rEDX = registers[3];
179 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
180 #if defined(__GNUC__)
181 asm ("movl\t%%ebx, %%esi\n\t"
183 "xchgl\t%%ebx, %%esi\n\t"
190 #elif defined(_MSC_VER)
195 mov dword ptr [esi],eax
197 mov dword ptr [esi],ebx
199 mov dword ptr [esi],ecx
201 mov dword ptr [esi],edx
209 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
210 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
211 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
212 if (Family == 6 || Family == 0xf) {
214 // Examine extended family ID if family ID is F.
215 Family += (EAX >> 20) & 0xff; // Bits 20 - 27
216 // Examine extended model ID if family ID is 6 or F.
217 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
221 void X86Subtarget::AutoDetectSubtargetFeatures() {
222 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
228 if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
231 GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
233 if ((EDX >> 15) & 1) HasCMov = true;
234 if ((EDX >> 23) & 1) X86SSELevel = MMX;
235 if ((EDX >> 25) & 1) X86SSELevel = SSE1;
236 if ((EDX >> 26) & 1) X86SSELevel = SSE2;
237 if (ECX & 0x1) X86SSELevel = SSE3;
238 if ((ECX >> 9) & 1) X86SSELevel = SSSE3;
239 if ((ECX >> 19) & 1) X86SSELevel = SSE41;
240 if ((ECX >> 20) & 1) X86SSELevel = SSE42;
242 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
243 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
245 HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
246 HasAVX = ((ECX >> 28) & 0x1);
248 if (IsIntel || IsAMD) {
249 // Determine if bit test memory instructions are slow.
252 DetectFamilyModel(EAX, Family, Model);
253 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
255 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
256 HasX86_64 = (EDX >> 29) & 0x1;
257 HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
258 HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
262 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &FS,
264 : PICStyle(PICStyles::None)
265 , X86SSELevel(NoMMXSSE)
266 , X863DNowLevel(NoThreeDNow)
276 // FIXME: this is a known good value for Yonah. How about others?
277 , MaxInlineSizeThreshold(128)
279 , TargetType(isELF) { // Default to ELF unless otherwise specified.
281 // default to hard float ABI
282 if (FloatABIType == FloatABI::Default)
283 FloatABIType = FloatABI::Hard;
285 // Determine default and user specified characteristics
287 // If feature string is not empty, parse features string.
288 std::string CPU = sys::getHostCPUName();
289 ParseSubtargetFeatures(FS, CPU);
290 // All X86-64 CPUs also have SSE2, however user might request no SSE via
291 // -mattr, so don't force SSELevel here.
293 // Otherwise, use CPUID to auto-detect feature set.
294 AutoDetectSubtargetFeatures();
295 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
296 if (Is64Bit && X86SSELevel < SSE2)
300 // If requesting codegen for X86-64, make sure that 64-bit features
305 DEBUG(errs() << "Subtarget features: SSELevel " << X86SSELevel
306 << ", 3DNowLevel " << X863DNowLevel
307 << ", 64bit " << HasX86_64 << "\n");
308 assert((!Is64Bit || HasX86_64) &&
309 "64-bit code requested on a subtarget that doesn't support it!");
311 // Set the boolean corresponding to the current target triple, or the default
312 // if one cannot be determined, to true.
313 if (TT.length() > 5) {
315 if ((Pos = TT.find("-darwin")) != std::string::npos) {
316 TargetType = isDarwin;
318 // Compute the darwin version number.
319 if (isdigit(TT[Pos+7]))
320 DarwinVers = atoi(&TT[Pos+7]);
322 DarwinVers = 8; // Minimum supported darwin is Tiger.
323 } else if (TT.find("linux") != std::string::npos) {
324 // Linux doesn't imply ELF, but we don't currently support anything else.
326 } else if (TT.find("cygwin") != std::string::npos) {
327 TargetType = isCygwin;
328 } else if (TT.find("mingw") != std::string::npos) {
329 TargetType = isMingw;
330 } else if (TT.find("win32") != std::string::npos) {
331 TargetType = isWindows;
332 } else if (TT.find("windows") != std::string::npos) {
333 TargetType = isWindows;
334 } else if (TT.find("-cl") != std::string::npos) {
335 TargetType = isDarwin;
340 // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
342 if (TargetType == isDarwin || Is64Bit)
346 stackAlignment = StackAlignment;
349 bool X86Subtarget::enablePostRAScheduler(
350 CodeGenOpt::Level OptLevel,
351 TargetSubtarget::AntiDepBreakMode& Mode,
352 RegClassVector& CriticalPathRCs) const {
353 Mode = TargetSubtarget::ANTIDEP_CRITICAL;
354 CriticalPathRCs.clear();
355 return OptLevel >= CodeGenOpt::Default;