Fixed 80 cols & style violation
[oota-llvm.git] / lib / Target / X86 / X86Subtarget.cpp
1 //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the X86 specific subclass of TargetSubtarget.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86Subtarget.h"
15 #include "X86GenSubtarget.inc"
16 #include "llvm/Module.h"
17 #include "llvm/Support/CommandLine.h"
18 using namespace llvm;
19
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:"),
23   cl::values(
24     clEnumValN(X86Subtarget::att,   "att",   "  Emit AT&T-style assembly"),
25     clEnumValN(X86Subtarget::intel, "intel", "  Emit Intel-style assembly"),
26     clEnumValEnd));
27
28
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
35 {
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());
43     }
44   
45   return false;
46 }
47
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"
55        "cpuid\n\t"
56        "xchgq\t%%rbx, %%rsi\n\t"
57        : "=a" (*rEAX),
58          "=S" (*rEBX),
59          "=c" (*rECX),
60          "=d" (*rEDX)
61        :  "a" (value));
62   return false;
63 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
64 #if defined(__GNUC__)
65   asm ("movl\t%%ebx, %%esi\n\t"
66        "cpuid\n\t"
67        "xchgl\t%%ebx, %%esi\n\t"
68        : "=a" (*rEAX),
69          "=S" (*rEBX),
70          "=c" (*rECX),
71          "=d" (*rEDX)
72        :  "a" (value));
73   return false;
74 #elif defined(_MSC_VER)
75   __asm {
76     mov   eax,value
77     cpuid
78     mov   esi,rEAX
79     mov   dword ptr [esi],eax
80     mov   esi,rEBX
81     mov   dword ptr [esi],ebx
82     mov   esi,rECX
83     mov   dword ptr [esi],ecx
84     mov   esi,rEDX
85     mov   dword ptr [esi],edx
86   }
87   return false;
88 #endif
89 #endif
90   return true;
91 }
92
93 void X86Subtarget::AutoDetectSubtargetFeatures() {
94   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
95   union {
96     unsigned u[3];
97     char     c[12];
98   } text;
99   
100   if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
101     return;
102   
103   // FIXME: support for AMD family of processors.
104   if (memcmp(text.c, "GenuineIntel", 12) == 0) {
105     X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
106
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;
111
112     X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
113     HasX86_64 = (EDX >> 29) & 0x1;
114   }
115 }
116
117 static const char *GetCurrentX86CPU() {
118   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
119   if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
120     return "generic";
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;
125
126   union {
127     unsigned u[3];
128     char     c[12];
129   } text;
130
131   X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
132   if (memcmp(text.c, "GenuineIntel", 12) == 0) {
133     switch (Family) {
134       case 3:
135         return "i386";
136       case 4:
137         return "i486";
138       case 5:
139         switch (Model) {
140         case 4:  return "pentium-mmx";
141         default: return "pentium";
142         }
143       case 6:
144         switch (Model) {
145         case 1:  return "pentiumpro";
146         case 3:
147         case 5:
148         case 6:  return "pentium2";
149         case 7:
150         case 8:
151         case 10:
152         case 11: return "pentium3";
153         case 9:
154         case 13: return "pentium-m";
155         case 14: return "yonah";
156         case 15: return "core2";
157         default: return "i686";
158         }
159       case 15: {
160         switch (Model) {
161         case 3:  
162         case 4:
163           return (Em64T) ? "nocona" : "prescott";
164         default:
165           return (Em64T) ? "x86-64" : "pentium4";
166         }
167       }
168         
169     default:
170       return "generic";
171     }
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.
176     switch (Family) {
177       case 4:
178         return "i486";
179       case 5:
180         switch (Model) {
181         case 6:
182         case 7:  return "k6";
183         case 8:  return "k6-2";
184         case 9:
185         case 13: return "k6-3";
186         default: return "pentium";
187         }
188       case 6:
189         switch (Model) {
190         case 4:  return "athlon-tbird";
191         case 6:
192         case 7:
193         case 8:  return "athlon-mp";
194         case 10: return "athlon-xp";
195         default: return "athlon";
196         }
197       case 15:
198         switch (Model) {
199         case 5:  return "athlon-fx"; // also opteron
200         default: return "athlon64";
201         }
202
203     default:
204       return "generic";
205     }
206   } else {
207     return "generic";
208   }
209 }
210
211 /// SetJITMode - This is called to inform the subtarget info that we are
212 /// producing code for the JIT.
213 void X86Subtarget::SetJITMode()
214 {
215   // JIT mode doesn't want extra loads for dllimported symbols, it knows exactly
216   // where everything is.
217   if (isTargetCygwin())
218     GenerateExtraLoadsForGVs = false;
219 }
220
221 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
222   : AsmFlavor(AsmWriterFlavor)
223   , X86SSELevel(NoMMXSSE)
224   , HasX86_64(false)
225   , stackAlignment(8)
226   // FIXME: this is a known good value for Yonah. How about others?
227   , MinRepStrSizeThreshold(128)
228   , Is64Bit(is64Bit)
229   , GenerateExtraLoadsForGVs(true)
230   , TargetType(isELF) { // Default to ELF unless otherwise specified.
231
232   // Determine default and user specified characteristics
233   if (!FS.empty()) {
234     // If feature string is not empty, parse features string.
235     std::string CPU = GetCurrentX86CPU();
236     ParseSubtargetFeatures(FS, CPU);
237     
238     if (Is64Bit && !HasX86_64)
239       cerr << "Warning: Generation of 64-bit code for a 32-bit processor "
240            << "requested.\n";
241     if (Is64Bit && X86SSELevel < SSE2)
242       cerr << "Warning: 64-bit processors all have at least SSE2.\n";
243   } else {
244     // Otherwise, use CPUID to auto-detect feature set.
245     AutoDetectSubtargetFeatures();
246   }
247     
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.
250   if (Is64Bit) {
251     HasX86_64 = true;
252     if (X86SSELevel < SSE2)
253       X86SSELevel = SSE2;
254   }
255
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;
274 #endif
275   }
276
277   // If the asm syntax hasn't been overridden on the command line, use whatever
278   // the target wants.
279   if (AsmFlavor == X86Subtarget::unset) {
280     if (TargetType == isWindows) {
281       AsmFlavor = X86Subtarget::intel;
282     } else {
283       AsmFlavor = X86Subtarget::att;
284     }
285   }
286
287   if (TargetType == isDarwin ||
288       TargetType == isCygwin ||
289       (TargetType == isELF && Is64Bit))
290     stackAlignment = 16;
291 }