1 //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Nate Begeman and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the X86 specific subclass of TargetSubtarget.
12 //===----------------------------------------------------------------------===//
14 #include "X86Subtarget.h"
15 #include "llvm/Module.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "X86GenSubtarget.inc"
20 cl::opt<X86Subtarget::AsmWriterFlavorTy>
21 AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::unset),
22 cl::desc("Choose style of code to emit from X86 backend:"),
24 clEnumValN(X86Subtarget::att, "att", " Emit AT&T-style assembly"),
25 clEnumValN(X86Subtarget::intel, "intel", " Emit Intel-style assembly"),
28 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
29 /// specified arguments. If we can't run cpuid on the host, return true.
30 static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
31 unsigned *rECX, unsigned *rEDX) {
32 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
34 asm ("pushl\t%%ebx\n\t"
36 "movl\t%%ebx, %%esi\n\t"
44 #elif defined(_MSC_VER)
49 mov dword ptr [esi],eax
51 mov dword ptr [esi],ebx
53 mov dword ptr [esi],ecx
55 mov dword ptr [esi],edx
63 static const char *GetCurrentX86CPU() {
64 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
65 if (GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
67 unsigned Family = (EAX & (0xffffffff >> (32 - 4)) << 8) >> 8; // Bits 8 - 11
68 unsigned Model = (EAX & (0xffffffff >> (32 - 4)) << 4) >> 4; // Bits 4 - 7
69 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
70 bool Em64T = EDX & (1 << 29);
77 GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
78 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
86 case 4: return "pentium-mmx";
87 default: return "pentium";
91 case 1: return "pentiumpro";
94 case 6: return "pentium2";
98 case 11: return "pentium3";
100 case 13: return "pentium-m";
101 case 14: return "yonah";
103 return (Model > 14) ? "yonah" : "i686";
109 return (Em64T) ? "nocona" : "prescott";
111 return (Em64T) ? "x86-64" : "pentium4";
118 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
119 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
120 // appears to be no way to generate the wide variety of AMD-specific targets
121 // from the information returned from CPUID.
129 case 8: return "k6-2";
131 case 13: return "k6-3";
132 default: return "pentium";
136 case 4: return "athlon-tbird";
139 case 8: return "athlon-mp";
140 case 10: return "athlon-xp";
141 default: return "athlon";
145 case 5: return "athlon-fx"; // also opteron
146 default: return "athlon64";
157 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS) {
159 // FIXME: this is a known good value for Yonah. Not sure about others.
160 MinRepStrSizeThreshold = 128;
161 X86SSELevel = NoMMXSSE;
162 X863DNowLevel = NoThreeDNow;
163 AsmFlavor = AsmWriterFlavor;
166 // Determine default and user specified characteristics
167 std::string CPU = GetCurrentX86CPU();
169 // Parse features string.
170 ParseSubtargetFeatures(FS, CPU);
172 // Default to ELF unless otherwise specified.
175 // Set the boolean corresponding to the current target triple, or the default
176 // if one cannot be determined, to true.
177 const std::string& TT = M.getTargetTriple();
178 if (TT.length() > 5) {
179 if (TT.find("cygwin") != std::string::npos ||
180 TT.find("mingw") != std::string::npos)
181 TargetType = isCygwin;
182 else if (TT.find("darwin") != std::string::npos)
183 TargetType = isDarwin;
184 else if (TT.find("win32") != std::string::npos)
185 TargetType = isWindows;
186 } else if (TT.empty()) {
187 #if defined(__CYGWIN__) || defined(__MINGW32__)
188 TargetType = isCygwin;
189 #elif defined(__APPLE__)
190 TargetType = isDarwin;
191 #elif defined(_WIN32)
192 TargetType = isWindows;
196 // If the asm syntax hasn't been overridden on the command line, use whatever
198 if (AsmFlavor == X86Subtarget::unset) {
199 if (TargetType == isWindows) {
200 AsmFlavor = X86Subtarget::intel;
202 AsmFlavor = X86Subtarget::att;
206 if (TargetType == isDarwin || TargetType == isCygwin)