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/Module.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetOptions.h"
29 static cl::opt<X86Subtarget::AsmWriterFlavorTy>
30 AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::Unset),
31 cl::desc("Choose style of code to emit from X86 backend:"),
33 clEnumValN(X86Subtarget::ATT, "att", "Emit AT&T-style assembly"),
34 clEnumValN(X86Subtarget::Intel, "intel", "Emit Intel-style assembly"),
37 bool X86Subtarget::isPICStyleStubPIC(const TargetMachine &TM) const {
38 return PICStyle == PICStyles::Stub &&
39 TM.getRelocationModel() == Reloc::PIC_;
42 bool X86Subtarget::isPICStyleStubNoDynamic(const TargetMachine &TM) const {
43 return PICStyle == PICStyles::Stub &&
44 TM.getRelocationModel() == Reloc::DynamicNoPIC;
49 /// ClassifyGlobalReference - Classify a global variable reference for the
50 /// current subtarget according to how we should reference it in a non-pcrel
52 unsigned char X86Subtarget::
53 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
54 // DLLImport only exists on windows, it is implemented as a load from a
56 if (GV->hasDLLImportLinkage())
57 return X86II::MO_DLLIMPORT;
59 // X86-64 in PIC mode.
60 if (isPICStyleRIPRel()) {
61 // Large model never uses stubs.
62 if (TM.getCodeModel() == CodeModel::Large)
63 return X86II::MO_NO_FLAG;
65 if (isTargetDarwin()) {
66 // If symbol visibility is hidden, the extra load is not needed if
67 // target is x86-64 or the symbol is definitely defined in the current
69 if (GV->hasDefaultVisibility() &&
70 (GV->isDeclaration() || GV->isWeakForLinker()))
71 return X86II::MO_GOTPCREL;
73 assert(isTargetELF() && "Unknown rip-relative target");
75 // Extra load is needed for all externally visible.
76 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
77 return X86II::MO_GOTPCREL;
80 return X86II::MO_NO_FLAG;
83 if (isPICStyleGOT()) { // 32-bit ELF targets.
84 // Extra load is needed for all externally visible.
85 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
86 return X86II::MO_GOTOFF;
90 if (isPICStyleStubPIC(TM)) { // Darwin/32 in PIC mode.
91 // Determine whether we have a stub reference and/or whether the reference
92 // is relative to the PIC base or not.
94 // If this is a strong reference to a definition, it is definitely not
96 if (!GV->isDeclaration() && !GV->isWeakForLinker())
97 return X86II::MO_PIC_BASE_OFFSET;
99 // Unless we have a symbol with hidden visibility, we have to go through a
100 // normal $non_lazy_ptr stub because this symbol might be resolved late.
101 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
102 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
104 // If symbol visibility is hidden, we have a stub for common symbol
105 // references and external declarations.
106 if (GV->isDeclaration() || GV->hasCommonLinkage()) {
107 // Hidden $non_lazy_ptr reference.
108 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
111 // Otherwise, no stub.
112 return X86II::MO_PIC_BASE_OFFSET;
115 if (isPICStyleStubNoDynamic(TM)) { // Darwin/32 in -mdynamic-no-pic mode.
116 // Determine whether we have a stub reference.
118 // If this is a strong reference to a definition, it is definitely not
120 if (!GV->isDeclaration() && !GV->isWeakForLinker())
121 return X86II::MO_NO_FLAG;
123 // Unless we have a symbol with hidden visibility, we have to go through a
124 // normal $non_lazy_ptr stub because this symbol might be resolved late.
125 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
126 return X86II::MO_DARWIN_NONLAZY;
128 // If symbol visibility is hidden, we have a stub for common symbol
129 // references and external declarations.
130 if (GV->isDeclaration() || GV->hasCommonLinkage()) {
131 // Hidden $non_lazy_ptr reference.
132 return X86II::MO_DARWIN_HIDDEN_NONLAZY;
135 // Otherwise, no stub.
136 return X86II::MO_NO_FLAG;
139 // Direct static reference to global.
140 return X86II::MO_NO_FLAG;
144 /// getBZeroEntry - This function returns the name of a function which has an
145 /// interface like the non-standard bzero function, if such a function exists on
146 /// the current subtarget and it is considered prefereable over memset with zero
147 /// passed as the second argument. Otherwise it returns null.
148 const char *X86Subtarget::getBZeroEntry() const {
149 // Darwin 10 has a __bzero entry point for this purpose.
150 if (getDarwinVers() >= 10)
156 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
157 /// to immediate address.
158 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
161 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
164 /// getSpecialAddressLatency - For targets where it is beneficial to
165 /// backschedule instructions that compute addresses, return a value
166 /// indicating the number of scheduling cycles of backscheduling that
167 /// should be attempted.
168 unsigned X86Subtarget::getSpecialAddressLatency() const {
169 // For x86 out-of-order targets, back-schedule address computations so
170 // that loads and stores aren't blocked.
171 // This value was chosen arbitrarily.
175 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
176 /// specified arguments. If we can't run cpuid on the host, return true.
177 bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
178 unsigned *rECX, unsigned *rEDX) {
179 #if defined(__x86_64__) || defined(_M_AMD64)
180 #if defined(__GNUC__)
181 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
182 asm ("movq\t%%rbx, %%rsi\n\t"
184 "xchgq\t%%rbx, %%rsi\n\t"
191 #elif defined(_MSC_VER)
193 __cpuid(registers, value);
194 *rEAX = registers[0];
195 *rEBX = registers[1];
196 *rECX = registers[2];
197 *rEDX = registers[3];
200 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
201 #if defined(__GNUC__)
202 asm ("movl\t%%ebx, %%esi\n\t"
204 "xchgl\t%%ebx, %%esi\n\t"
211 #elif defined(_MSC_VER)
216 mov dword ptr [esi],eax
218 mov dword ptr [esi],ebx
220 mov dword ptr [esi],ecx
222 mov dword ptr [esi],edx
230 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
231 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
232 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
233 if (Family == 6 || Family == 0xf) {
235 // Examine extended family ID if family ID is F.
236 Family += (EAX >> 20) & 0xff; // Bits 20 - 27
237 // Examine extended model ID if family ID is 6 or F.
238 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
242 void X86Subtarget::AutoDetectSubtargetFeatures() {
243 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
249 if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
252 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
254 if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
255 if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
256 if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
257 if (ECX & 0x1) X86SSELevel = SSE3;
258 if ((ECX >> 9) & 0x1) X86SSELevel = SSSE3;
259 if ((ECX >> 19) & 0x1) X86SSELevel = SSE41;
260 if ((ECX >> 20) & 0x1) X86SSELevel = SSE42;
262 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
263 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
265 HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
266 HasAVX = ((ECX >> 28) & 0x1);
268 if (IsIntel || IsAMD) {
269 // Determine if bit test memory instructions are slow.
272 DetectFamilyModel(EAX, Family, Model);
273 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
275 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
276 HasX86_64 = (EDX >> 29) & 0x1;
277 HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
278 HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
282 static const char *GetCurrentX86CPU() {
283 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
284 if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
288 DetectFamilyModel(EAX, Family, Model);
290 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
291 bool Em64T = (EDX >> 29) & 0x1;
292 bool HasSSE3 = (ECX & 0x1);
299 X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
300 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
308 case 4: return "pentium-mmx";
309 default: return "pentium";
313 case 1: return "pentiumpro";
316 case 6: return "pentium2";
320 case 11: return "pentium3";
322 case 13: return "pentium-m";
323 case 14: return "yonah";
325 case 22: // Celeron M 540
327 case 23: // 45nm: Penryn , Wolfdale, Yorkfield (XE)
329 default: return "i686";
335 case 6: // same as 4, but 65nm
336 return (Em64T) ? "nocona" : "prescott";
342 return (Em64T) ? "x86-64" : "pentium4";
349 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
350 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
351 // appears to be no way to generate the wide variety of AMD-specific targets
352 // from the information returned from CPUID.
360 case 8: return "k6-2";
362 case 13: return "k6-3";
363 default: return "pentium";
367 case 4: return "athlon-tbird";
370 case 8: return "athlon-mp";
371 case 10: return "athlon-xp";
372 default: return "athlon";
377 default: return "k8-sse3";
381 case 1: return "opteron";
382 case 5: return "athlon-fx"; // also opteron
383 default: return "athlon64";
388 default: return "amdfam10";
398 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
399 : AsmFlavor(AsmWriterFlavor)
400 , PICStyle(PICStyles::None)
401 , X86SSELevel(NoMMXSSE)
402 , X863DNowLevel(NoThreeDNow)
412 // FIXME: this is a known good value for Yonah. How about others?
413 , MaxInlineSizeThreshold(128)
415 , TargetType(isELF) { // Default to ELF unless otherwise specified.
417 // default to hard float ABI
418 if (FloatABIType == FloatABI::Default)
419 FloatABIType = FloatABI::Hard;
421 // Determine default and user specified characteristics
423 // If feature string is not empty, parse features string.
424 std::string CPU = GetCurrentX86CPU();
425 ParseSubtargetFeatures(FS, CPU);
426 // All X86-64 CPUs also have SSE2, however user might request no SSE via
427 // -mattr, so don't force SSELevel here.
429 // Otherwise, use CPUID to auto-detect feature set.
430 AutoDetectSubtargetFeatures();
431 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
432 if (Is64Bit && X86SSELevel < SSE2)
436 // If requesting codegen for X86-64, make sure that 64-bit features
441 DOUT << "Subtarget features: SSELevel " << X86SSELevel
442 << ", 3DNowLevel " << X863DNowLevel
443 << ", 64bit " << HasX86_64 << "\n";
444 assert((!Is64Bit || HasX86_64) &&
445 "64-bit code requested on a subtarget that doesn't support it!");
447 // Set the boolean corresponding to the current target triple, or the default
448 // if one cannot be determined, to true.
449 const std::string& TT = M.getTargetTriple();
450 if (TT.length() > 5) {
452 if ((Pos = TT.find("-darwin")) != std::string::npos) {
453 TargetType = isDarwin;
455 // Compute the darwin version number.
456 if (isdigit(TT[Pos+7]))
457 DarwinVers = atoi(&TT[Pos+7]);
459 DarwinVers = 8; // Minimum supported darwin is Tiger.
460 } else if (TT.find("linux") != std::string::npos) {
461 // Linux doesn't imply ELF, but we don't currently support anything else.
464 } else if (TT.find("cygwin") != std::string::npos) {
465 TargetType = isCygwin;
466 } else if (TT.find("mingw") != std::string::npos) {
467 TargetType = isMingw;
468 } else if (TT.find("win32") != std::string::npos) {
469 TargetType = isWindows;
470 } else if (TT.find("windows") != std::string::npos) {
471 TargetType = isWindows;
473 else if (TT.find("-cl") != std::string::npos) {
474 TargetType = isDarwin;
477 } else if (TT.empty()) {
478 #if defined(__CYGWIN__)
479 TargetType = isCygwin;
480 #elif defined(__MINGW32__) || defined(__MINGW64__)
481 TargetType = isMingw;
482 #elif defined(__APPLE__)
483 TargetType = isDarwin;
484 #if __APPLE_CC__ > 5400
485 DarwinVers = 9; // GCC 5400+ is Leopard.
487 DarwinVers = 8; // Minimum supported darwin is Tiger.
490 #elif defined(_WIN32) || defined(_WIN64)
491 TargetType = isWindows;
492 #elif defined(__linux__)
493 // Linux doesn't imply ELF, but we don't currently support anything else.
499 // If the asm syntax hasn't been overridden on the command line, use whatever
501 if (AsmFlavor == X86Subtarget::Unset) {
502 AsmFlavor = (TargetType == isWindows)
503 ? X86Subtarget::Intel : X86Subtarget::ATT;
506 // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
508 if (TargetType == isDarwin || Is64Bit)
512 stackAlignment = StackAlignment;