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 "X86GenSubtarget.inc"
16 #include "llvm/Module.h"
17 #include "llvm/Support/CommandLine.h"
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"),
29 /// True if accessing the GV requires an extra load. For Windows, dllimported
30 /// symbols are indirect, loading the value at address GV rather then the
31 /// value of GV itself. This means that the GlobalAddress must be in the base
32 /// or index register of the address, not the GV offset field.
33 bool X86Subtarget::GVRequiresExtraLoad(const GlobalValue* GV,
34 bool isDirectCall) const
36 if (GenerateExtraLoadsForGVs)
37 if (isTargetDarwin()) {
38 return (!isDirectCall &&
39 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
40 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())));
41 } else if (isTargetCygwin() || isTargetWindows()) {
42 return (GV->hasDLLImportLinkage());
48 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
49 /// specified arguments. If we can't run cpuid on the host, return true.
50 bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
51 unsigned *rECX, unsigned *rEDX) {
52 #if defined(__x86_64__)
53 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
54 asm ("movq\t%%rbx, %%rsi\n\t"
56 "xchgq\t%%rbx, %%rsi\n\t"
63 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
65 asm ("movl\t%%ebx, %%esi\n\t"
67 "xchgl\t%%ebx, %%esi\n\t"
74 #elif defined(_MSC_VER)
79 mov dword ptr [esi],eax
81 mov dword ptr [esi],ebx
83 mov dword ptr [esi],ecx
85 mov dword ptr [esi],edx
93 void X86Subtarget::AutoDetectSubtargetFeatures() {
94 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
100 if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
103 // FIXME: support for AMD family of processors.
104 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
105 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
107 if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
108 if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
109 if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
110 if (ECX & 0x1) X86SSELevel = SSE3;
112 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
113 HasX86_64 = (EDX >> 29) & 0x1;
117 static const char *GetCurrentX86CPU() {
118 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
119 if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
121 unsigned Family = (EAX >> 8) & 0xf; // Bits 8 - 11
122 unsigned Model = (EAX >> 4) & 0xf; // Bits 4 - 7
123 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
124 bool Em64T = (EDX >> 29) & 0x1;
131 X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
132 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
140 case 4: return "pentium-mmx";
141 default: return "pentium";
145 case 1: return "pentiumpro";
148 case 6: return "pentium2";
152 case 11: return "pentium3";
154 case 13: return "pentium-m";
155 case 14: return "yonah";
156 case 15: return "core2";
157 default: return "i686";
163 return (Em64T) ? "nocona" : "prescott";
165 return (Em64T) ? "x86-64" : "pentium4";
172 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
173 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
174 // appears to be no way to generate the wide variety of AMD-specific targets
175 // from the information returned from CPUID.
183 case 8: return "k6-2";
185 case 13: return "k6-3";
186 default: return "pentium";
190 case 4: return "athlon-tbird";
193 case 8: return "athlon-mp";
194 case 10: return "athlon-xp";
195 default: return "athlon";
199 case 5: return "athlon-fx"; // also opteron
200 default: return "athlon64";
211 /// SetJITMode - This is called to inform the subtarget info that we are
212 /// producing code for the JIT.
213 void X86Subtarget::SetJITMode()
215 // JIT mode doesn't want extra loads for dllimported symbols, it knows exactly
216 // where everything is.
217 if (isTargetCygwin())
218 GenerateExtraLoadsForGVs = false;
221 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
222 : AsmFlavor(AsmWriterFlavor)
223 , X86SSELevel(NoMMXSSE)
226 // FIXME: this is a known good value for Yonah. How about others?
227 , MinRepStrSizeThreshold(128)
229 , GenerateExtraLoadsForGVs(true)
230 , TargetType(isELF) { // Default to ELF unless otherwise specified.
232 // Determine default and user specified characteristics
234 // If feature string is not empty, parse features string.
235 std::string CPU = GetCurrentX86CPU();
236 ParseSubtargetFeatures(FS, CPU);
238 if (Is64Bit && !HasX86_64)
239 cerr << "Warning: Generation of 64-bit code for a 32-bit processor "
241 if (Is64Bit && X86SSELevel < SSE2)
242 cerr << "Warning: 64-bit processors all have at least SSE2.\n";
244 // Otherwise, use CPUID to auto-detect feature set.
245 AutoDetectSubtargetFeatures();
248 // If requesting codegen for X86-64, make sure that 64-bit and SSE2 features
249 // are enabled. These are available on all x86-64 CPUs.
252 if (X86SSELevel < SSE2)
256 // Set the boolean corresponding to the current target triple, or the default
257 // if one cannot be determined, to true.
258 const std::string& TT = M.getTargetTriple();
259 if (TT.length() > 5) {
260 if (TT.find("cygwin") != std::string::npos ||
261 TT.find("mingw") != std::string::npos)
262 TargetType = isCygwin;
263 else if (TT.find("darwin") != std::string::npos)
264 TargetType = isDarwin;
265 else if (TT.find("win32") != std::string::npos)
266 TargetType = isWindows;
267 } else if (TT.empty()) {
268 #if defined(__CYGWIN__) || defined(__MINGW32__)
269 TargetType = isCygwin;
270 #elif defined(__APPLE__)
271 TargetType = isDarwin;
272 #elif defined(_WIN32)
273 TargetType = isWindows;
277 // If the asm syntax hasn't been overridden on the command line, use whatever
279 if (AsmFlavor == X86Subtarget::unset) {
280 if (TargetType == isWindows) {
281 AsmFlavor = X86Subtarget::intel;
283 AsmFlavor = X86Subtarget::att;
287 if (TargetType == isDarwin ||
288 TargetType == isCygwin ||
289 (TargetType == isELF && Is64Bit))