A better workaround
[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 "llvm/Module.h"
16 #include "X86GenSubtarget.inc"
17 using namespace llvm;
18
19 static void GetCpuIDAndInfo(unsigned value, unsigned *EAX, unsigned *EBX,
20                             unsigned *ECX, unsigned *EDX) {
21 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
22 #if defined(__GNUC__)
23   asm ("pushl\t%%ebx\n\t"
24        "cpuid\n\t"
25        "movl\t%%ebx, %%esi\n\t"
26        "popl\t%%ebx"
27        : "=a" (*EAX),
28          "=S" (*EBX),
29          "=c" (*ECX),
30          "=d" (*EDX)
31        :  "a" (value));
32 #endif
33 #endif
34 }
35
36 static const char *GetCurrentX86CPU() {
37   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
38   GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
39   unsigned Family  = (EAX & (0xffffffff >> (32 - 4)) << 8) >> 8; // Bits 8 - 11
40   unsigned Model   = (EAX & (0xffffffff >> (32 - 4)) << 4) >> 4; // Bits 4 - 7
41   GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
42   bool Em64T = EDX & (1 << 29);
43
44   switch (Family) {
45     case 3:
46       return "i386";
47     case 4:
48       return "i486";
49     case 5:
50       switch (Model) {
51       case 4:  return "pentium-mmx";
52       default: return "pentium";
53       }
54       break;
55     case 6:
56       switch (Model) {
57       case 1:  return "pentiumpro";
58       case 3:
59       case 5:
60       case 6:  return "pentium2";
61       case 7:
62       case 8:
63       case 10:
64       case 11: return "pentium3";
65       case 9:
66       case 13: return "pentium-m";
67       case 14: return "yonah";
68       default: return "i686";
69       }
70     case 15: {
71       switch (Model) {
72       case 3:  
73       case 4:
74         return (Em64T) ? "nocona" : "prescott";
75       default:
76         return (Em64T) ? "x86-64" : "pentium4";
77       }
78     }
79       
80   default:
81     return "generic";
82   }
83 }
84
85 X86Subtarget::X86Subtarget(const Module &M, const std::string &FS)
86   : stackAlignment(8), indirectExternAndWeakGlobals(false) {
87       
88   // Determine default and user specified characteristics
89   std::string CPU = GetCurrentX86CPU();
90
91   // Parse features string.
92   ParseSubtargetFeatures(FS, CPU);
93
94   // Default to ELF unless otherwise specified.
95   TargetType = isELF;
96   
97   // FIXME: Force these off until they work.  An llc-beta option should turn
98   // them back on.
99   X86SSELevel = NoMMXSSE;
100   X863DNowLevel = NoThreeDNow;
101       
102   // Set the boolean corresponding to the current target triple, or the default
103   // if one cannot be determined, to true.
104   const std::string& TT = M.getTargetTriple();
105   if (TT.length() > 5) {
106     if (TT.find("cygwin") != std::string::npos ||
107         TT.find("mingw")  != std::string::npos)
108       TargetType = isCygwin;
109     else if (TT.find("darwin") != std::string::npos)
110       TargetType = isDarwin;
111     else if (TT.find("win32") != std::string::npos)
112       TargetType = isWindows;
113   } else if (TT.empty()) {
114 #if defined(__CYGWIN__) || defined(__MINGW32__)
115     TargetType = isCygwin;
116 #elif defined(__APPLE__)
117     TargetType = isDarwin;
118 #elif defined(_WIN32)
119     TargetType = isWindows;
120 #endif
121   }
122
123   if (TargetType == isDarwin) {
124     stackAlignment = 16;
125     indirectExternAndWeakGlobals = true;
126   }
127 }