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"
18 #include "llvm/Target/TargetMachine.h"
21 cl::opt<X86Subtarget::AsmWriterFlavorTy>
22 AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::Unset),
23 cl::desc("Choose style of code to emit from X86 backend:"),
25 clEnumValN(X86Subtarget::ATT, "att", " Emit AT&T-style assembly"),
26 clEnumValN(X86Subtarget::Intel, "intel", " Emit Intel-style assembly"),
30 /// True if accessing the GV requires an extra load. For Windows, dllimported
31 /// symbols are indirect, loading the value at address GV rather then the
32 /// value of GV itself. This means that the GlobalAddress must be in the base
33 /// or index register of the address, not the GV offset field.
34 bool X86Subtarget::GVRequiresExtraLoad(const GlobalValue* GV,
35 const TargetMachine& TM,
36 bool isDirectCall) const
38 if (TM.getRelocationModel() != Reloc::Static)
39 if (isTargetDarwin()) {
40 return (!isDirectCall &&
41 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
42 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())));
43 } else if (TM.getRelocationModel() == Reloc::PIC_ && isPICStyleGOT()) {
44 // Extra load is needed for all non-statics.
45 return (!isDirectCall &&
46 (GV->isExternal() || !GV->hasInternalLinkage()));
47 } else if (isTargetCygMing() || isTargetWindows()) {
48 return (GV->hasDLLImportLinkage());
54 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
55 /// specified arguments. If we can't run cpuid on the host, return true.
56 bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
57 unsigned *rECX, unsigned *rEDX) {
58 #if defined(__x86_64__)
59 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
60 asm ("movq\t%%rbx, %%rsi\n\t"
62 "xchgq\t%%rbx, %%rsi\n\t"
69 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
71 asm ("movl\t%%ebx, %%esi\n\t"
73 "xchgl\t%%ebx, %%esi\n\t"
80 #elif defined(_MSC_VER)
85 mov dword ptr [esi],eax
87 mov dword ptr [esi],ebx
89 mov dword ptr [esi],ecx
91 mov dword ptr [esi],edx
99 void X86Subtarget::AutoDetectSubtargetFeatures() {
100 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
106 if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
109 // FIXME: support for AMD family of processors.
110 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
111 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
113 if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
114 if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
115 if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
116 if (ECX & 0x1) X86SSELevel = SSE3;
118 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
119 HasX86_64 = (EDX >> 29) & 0x1;
123 static const char *GetCurrentX86CPU() {
124 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
125 if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
127 unsigned Family = (EAX >> 8) & 0xf; // Bits 8 - 11
128 unsigned Model = (EAX >> 4) & 0xf; // Bits 4 - 7
129 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
130 bool Em64T = (EDX >> 29) & 0x1;
137 X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
138 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
146 case 4: return "pentium-mmx";
147 default: return "pentium";
151 case 1: return "pentiumpro";
154 case 6: return "pentium2";
158 case 11: return "pentium3";
160 case 13: return "pentium-m";
161 case 14: return "yonah";
162 case 15: return "core2";
163 default: return "i686";
169 return (Em64T) ? "nocona" : "prescott";
171 return (Em64T) ? "x86-64" : "pentium4";
178 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
179 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
180 // appears to be no way to generate the wide variety of AMD-specific targets
181 // from the information returned from CPUID.
189 case 8: return "k6-2";
191 case 13: return "k6-3";
192 default: return "pentium";
196 case 4: return "athlon-tbird";
199 case 8: return "athlon-mp";
200 case 10: return "athlon-xp";
201 default: return "athlon";
205 case 5: return "athlon-fx"; // also opteron
206 default: return "athlon64";
217 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
218 : AsmFlavor(AsmWriterFlavor)
219 , PICStyle(PICStyle::None)
220 , X86SSELevel(NoMMXSSE)
223 // FIXME: this is a known good value for Yonah. How about others?
224 , MinRepStrSizeThreshold(128)
226 , TargetType(isELF) { // Default to ELF unless otherwise specified.
228 // Determine default and user specified characteristics
230 // If feature string is not empty, parse features string.
231 std::string CPU = GetCurrentX86CPU();
232 ParseSubtargetFeatures(FS, CPU);
234 if (Is64Bit && !HasX86_64)
235 cerr << "Warning: Generation of 64-bit code for a 32-bit processor "
237 if (Is64Bit && X86SSELevel < SSE2)
238 cerr << "Warning: 64-bit processors all have at least SSE2.\n";
240 // Otherwise, use CPUID to auto-detect feature set.
241 AutoDetectSubtargetFeatures();
244 // If requesting codegen for X86-64, make sure that 64-bit and SSE2 features
245 // are enabled. These are available on all x86-64 CPUs.
248 if (X86SSELevel < SSE2)
252 // Set the boolean corresponding to the current target triple, or the default
253 // if one cannot be determined, to true.
254 const std::string& TT = M.getTargetTriple();
255 if (TT.length() > 5) {
256 if (TT.find("cygwin") != std::string::npos)
257 TargetType = isCygwin;
258 else if (TT.find("mingw") != std::string::npos)
259 TargetType = isMingw;
260 else if (TT.find("darwin") != std::string::npos)
261 TargetType = isDarwin;
262 else if (TT.find("win32") != std::string::npos)
263 TargetType = isWindows;
264 } else if (TT.empty()) {
265 #if defined(__CYGWIN__)
266 TargetType = isCygwin;
267 #elif defined(__MINGW32__)
268 TargetType = isMingw;
269 #elif defined(__APPLE__)
270 TargetType = isDarwin;
271 #elif defined(_WIN32)
272 TargetType = isWindows;
276 // If the asm syntax hasn't been overridden on the command line, use whatever
278 if (AsmFlavor == X86Subtarget::Unset) {
279 if (TargetType == isWindows) {
280 AsmFlavor = X86Subtarget::Intel;
282 AsmFlavor = X86Subtarget::ATT;
286 if (TargetType == isDarwin ||
287 TargetType == isCygwin ||
288 TargetType == isMingw ||
289 (TargetType == isELF && Is64Bit))