Unbreak x64 MSVC build. Patch by Nicolas Capens!
[oota-llvm.git] / lib / System / Host.cpp
1 //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file implements the operating system Host concept.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/Host.h"
15 #include "llvm/Config/config.h"
16 #include <string.h>
17
18 // Include the platform-specific parts of this class.
19 #ifdef LLVM_ON_UNIX
20 #include "Unix/Host.inc"
21 #endif
22 #ifdef LLVM_ON_WIN32
23 #include "Win32/Host.inc"
24 #endif
25 #ifdef _MSC_VER
26 #include <intrin.h>
27 #endif
28
29 //===----------------------------------------------------------------------===//
30 //
31 //  Implementations of the CPU detection routines
32 //
33 //===----------------------------------------------------------------------===//
34
35 using namespace llvm;
36
37 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
38  || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
39
40 /// GetX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
41 /// specified arguments.  If we can't run cpuid on the host, return true.
42 static bool GetX86CpuIDAndInfo(unsigned value, unsigned *rEAX,
43                             unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
44 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
45   #if defined(__GNUC__)
46     // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
47     asm ("movq\t%%rbx, %%rsi\n\t"
48          "cpuid\n\t"
49          "xchgq\t%%rbx, %%rsi\n\t"
50          : "=a" (*rEAX),
51            "=S" (*rEBX),
52            "=c" (*rECX),
53            "=d" (*rEDX)
54          :  "a" (value));
55     return false;
56   #elif defined(_MSC_VER)
57     int registers[4];
58     __cpuid(registers, value);
59     *rEAX = registers[0];
60     *rEBX = registers[1];
61     *rECX = registers[2];
62     *rEDX = registers[3];
63     return false;
64   #endif
65 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
66   #if defined(__GNUC__)
67     asm ("movl\t%%ebx, %%esi\n\t"
68          "cpuid\n\t"
69          "xchgl\t%%ebx, %%esi\n\t"
70          : "=a" (*rEAX),
71            "=S" (*rEBX),
72            "=c" (*rECX),
73            "=d" (*rEDX)
74          :  "a" (value));
75     return false;
76   #elif defined(_MSC_VER)
77     __asm {
78       mov   eax,value
79       cpuid
80       mov   esi,rEAX
81       mov   dword ptr [esi],eax
82       mov   esi,rEBX
83       mov   dword ptr [esi],ebx
84       mov   esi,rECX
85       mov   dword ptr [esi],ecx
86       mov   esi,rEDX
87       mov   dword ptr [esi],edx
88     }
89     return false;
90   #endif
91 #endif
92   return true;
93 }
94
95 static void DetectX86FamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
96   Family = (EAX >> 8) & 0xf; // Bits 8 - 11
97   Model  = (EAX >> 4) & 0xf; // Bits 4 - 7
98   if (Family == 6 || Family == 0xf) {
99     if (Family == 0xf)
100       // Examine extended family ID if family ID is F.
101       Family += (EAX >> 20) & 0xff;    // Bits 20 - 27
102     // Examine extended model ID if family ID is 6 or F.
103     Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
104   }
105 }
106 #endif
107
108
109 std::string sys::getHostCPUName() {
110 #if defined(__x86_64__) || defined(__i386__)
111   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
112   if (GetX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
113     return "generic";
114   unsigned Family = 0;
115   unsigned Model  = 0;
116   DetectX86FamilyModel(EAX, Family, Model);
117
118   GetX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
119   bool Em64T = (EDX >> 29) & 0x1;
120   bool HasSSE3 = (ECX & 0x1);
121
122   union {
123     unsigned u[3];
124     char     c[12];
125   } text;
126
127   GetX86CpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
128   if (memcmp(text.c, "GenuineIntel", 12) == 0) {
129     switch (Family) {
130     case 3:
131       return "i386";
132     case 4:
133       switch (Model) {
134       case 0: // Intel486TM DX processors
135       case 1: // Intel486TM DX processors
136       case 2: // Intel486 SX processors
137       case 3: // Intel487TM processors, IntelDX2 OverDrive® processors,
138               // IntelDX2TM processors
139       case 4: // Intel486 SL processor
140       case 5: // IntelSX2TM processors
141       case 7: // Write-Back Enhanced IntelDX2 processors
142       case 8: // IntelDX4 OverDrive processors, IntelDX4TM processors
143       default: return "i486";
144       }
145     case 5:
146       switch (Model) {
147       case  1: // Pentium OverDrive processor for Pentium processor (60, 66),
148                // Pentium® processors (60, 66)
149       case  2: // Pentium OverDrive processor for Pentium processor (75, 90,
150                // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133,
151                // 150, 166, 200)
152       case  3: // Pentium OverDrive processors for Intel486 processor-based
153                // systems
154         return "pentium";
155
156       case  4: // Pentium OverDrive processor with MMXTM technology for Pentium
157                // processor (75, 90, 100, 120, 133), Pentium processor with
158                // MMXTM technology (166, 200)
159         return "pentium-mmx";
160
161       default: return "pentium";
162       }
163     case 6:
164       switch (Model) {
165       case  1: // Pentium Pro processor
166         return "pentiumpro";
167
168       case  3: // Intel Pentium II OverDrive processor, Pentium II processor,
169                // model 03
170       case  5: // Pentium II processor, model 05, Pentium II Xeon processor,
171                // model 05, and Intel® Celeron® processor, model 05
172       case  6: // Celeron processor, model 06
173         return "pentium2";
174
175       case  7: // Pentium III processor, model 07, and Pentium III Xeon
176                // processor, model 07
177       case  8: // Pentium III processor, model 08, Pentium III Xeon processor,
178                // model 08, and Celeron processor, model 08
179       case 10: // Pentium III Xeon processor, model 0Ah
180       case 11: // Pentium III processor, model 0Bh
181         return "pentium3";
182
183       case  9: // Intel Pentium M processor, Intel Celeron M processor model 09.
184       case 13: // Intel Pentium M processor, Intel Celeron M processor, model
185                // 0Dh. All processors are manufactured using the 90 nm process.
186         return "pentium-m";
187
188       case 14: // Intel CoreTM Duo processor, Intel CoreTM Solo processor, model
189                // 0Eh. All processors are manufactured using the 65 nm process.
190         return "yonah";
191
192       case 15: // Intel CoreTM2 Duo processor, Intel CoreTM2 Duo mobile
193                // processor, Intel CoreTM2 Quad processor, Intel CoreTM2 Quad
194                // mobile processor, Intel CoreTM2 Extreme processor, Intel
195                // Pentium Dual-Core processor, Intel Xeon processor, model
196                // 0Fh. All processors are manufactured using the 65 nm process.
197       case 22: // Intel Celeron processor model 16h. All processors are
198                // manufactured using the 65 nm process
199         return "core2";
200
201       case 21: // Intel EP80579 Integrated Processor and Intel EP80579
202                // Integrated Processor with Intel QuickAssist Technology
203         return "i686"; // FIXME: ???
204
205       case 23: // Intel CoreTM2 Extreme processor, Intel Xeon processor, model
206                // 17h. All processors are manufactured using the 45 nm process.
207                //
208                // 45nm: Penryn , Wolfdale, Yorkfield (XE)
209         return "penryn";
210
211       case 26: // Intel Core i7 processor and Intel Xeon processor. All
212                // processors are manufactured using the 45 nm process.
213       case 29: // Intel Xeon processor MP. All processors are manufactured using
214                // the 45 nm process.
215         return "corei7";
216
217       case 28: // Intel Atom processor. All processors are manufactured using
218                // the 45 nm process
219         return "atom";
220
221       default: return "i686";
222       }
223     case 15: {
224       switch (Model) {
225       case  0: // Pentium 4 processor, Intel Xeon processor. All processors are
226                // model 00h and manufactured using the 0.18 micron process.
227       case  1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
228                // processor MP, and Intel Celeron processor. All processors are
229                // model 01h and manufactured using the 0.18 micron process.
230       case  2: // Pentium 4 processor, Mobile Intel Pentium 4 processor – M,
231                // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
232                // processor, and Mobile Intel Celeron processor. All processors
233                // are model 02h and manufactured using the 0.13 micron process.
234         return (Em64T) ? "x86-64" : "pentium4";
235
236       case  3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
237                // processor. All processors are model 03h and manufactured using
238                // the 90 nm process.
239       case  4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
240                // Pentium D processor, Intel Xeon processor, Intel Xeon
241                // processor MP, Intel Celeron D processor. All processors are
242                // model 04h and manufactured using the 90 nm process.
243       case  6: // Pentium 4 processor, Pentium D processor, Pentium processor
244                // Extreme Edition, Intel Xeon processor, Intel Xeon processor
245                // MP, Intel Celeron D processor. All processors are model 06h
246                // and manufactured using the 65 nm process.
247         return (Em64T) ? "nocona" : "prescott";
248
249       default:
250         return (Em64T) ? "x86-64" : "pentium4";
251       }
252     }
253
254     default:
255       return "generic";
256     }
257   } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
258     // FIXME: this poorly matches the generated SubtargetFeatureKV table.  There
259     // appears to be no way to generate the wide variety of AMD-specific targets
260     // from the information returned from CPUID.
261     switch (Family) {
262       case 4:
263         return "i486";
264       case 5:
265         switch (Model) {
266         case 6:
267         case 7:  return "k6";
268         case 8:  return "k6-2";
269         case 9:
270         case 13: return "k6-3";
271         default: return "pentium";
272         }
273       case 6:
274         switch (Model) {
275         case 4:  return "athlon-tbird";
276         case 6:
277         case 7:
278         case 8:  return "athlon-mp";
279         case 10: return "athlon-xp";
280         default: return "athlon";
281         }
282       case 15:
283         if (HasSSE3) {
284           return "k8-sse3";
285         } else {
286           switch (Model) {
287           case 1:  return "opteron";
288           case 5:  return "athlon-fx"; // also opteron
289           default: return "athlon64";
290           }
291         }
292       case 16:
293         return "amdfam10";
294     default:
295       return "generic";
296     }
297   }
298 #endif
299
300   return "generic";
301 }