blob: 2a65013c2313b53efc259f696089a102c433b2ac [file] [log] [blame]
Nate Begeman8c00f8c2005-08-04 07:12:09 +00001//===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
Nate Begemanfb5792f2005-07-12 01:41:54 +00002//
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"
Evan Cheng97c7fc32006-01-26 09:53:06 +000016#include "X86GenSubtarget.inc"
Nate Begemanfb5792f2005-07-12 01:41:54 +000017using namespace llvm;
18
Evan Chengdbd38d72006-01-27 21:49:34 +000019// FIXME: temporary.
20#include "llvm/Support/CommandLine.h"
21namespace {
22 cl::opt<bool> EnableSSE("enable-x86-sse", cl::Hidden,
23 cl::desc("Enable sse on X86"));
24}
25
Chris Lattner1e39a152006-01-28 06:05:41 +000026/// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
27/// specified arguments. If we can't run cpuid on the host, return true.
Jeff Cohen41adb0d2006-01-28 18:09:06 +000028static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
29 unsigned *rECX, unsigned *rEDX) {
Evan Cheng559806f2006-01-27 08:10:46 +000030#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
31#if defined(__GNUC__)
32 asm ("pushl\t%%ebx\n\t"
33 "cpuid\n\t"
Evan Chengb3a7e212006-01-27 19:30:30 +000034 "movl\t%%ebx, %%esi\n\t"
Evan Cheng559806f2006-01-27 08:10:46 +000035 "popl\t%%ebx"
Jeff Cohen41adb0d2006-01-28 18:09:06 +000036 : "=a" (*rEAX),
37 "=S" (*rEBX),
38 "=c" (*rECX),
39 "=d" (*rEDX)
Evan Cheng559806f2006-01-27 08:10:46 +000040 : "a" (value));
Chris Lattner1e39a152006-01-28 06:05:41 +000041 return false;
Jeff Cohen41adb0d2006-01-28 18:09:06 +000042#elif defined(_MSC_VER)
43 __asm {
44 mov eax,value
45 cpuid
46 mov esi,rEAX
47 mov dword ptr [esi],eax
48 mov esi,rEBX
49 mov dword ptr [esi],ebx
50 mov esi,rECX
51 mov dword ptr [esi],ecx
52 mov esi,rEDX
53 mov dword ptr [esi],edx
54 }
55 return false;
Evan Cheng559806f2006-01-27 08:10:46 +000056#endif
57#endif
Chris Lattner1e39a152006-01-28 06:05:41 +000058 return true;
Evan Cheng559806f2006-01-27 08:10:46 +000059}
60
61static const char *GetCurrentX86CPU() {
Evan Chengb3a7e212006-01-27 19:30:30 +000062 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Chris Lattner1e39a152006-01-28 06:05:41 +000063 if (GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
64 return "generic";
Evan Cheng559806f2006-01-27 08:10:46 +000065 unsigned Family = (EAX & (0xffffffff >> (32 - 4)) << 8) >> 8; // Bits 8 - 11
66 unsigned Model = (EAX & (0xffffffff >> (32 - 4)) << 4) >> 4; // Bits 4 - 7
Evan Chengb3a7e212006-01-27 19:30:30 +000067 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Evan Cheng559806f2006-01-27 08:10:46 +000068 bool Em64T = EDX & (1 << 29);
69
Jeff Cohena3496402006-01-28 18:47:32 +000070 union {
Jeff Cohen216d2812006-01-28 19:48:34 +000071 unsigned u[3];
72 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +000073 } text;
74
Jeff Cohen216d2812006-01-28 19:48:34 +000075 GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
76 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
Jeff Cohen41adb0d2006-01-28 18:09:06 +000077 switch (Family) {
Evan Cheng559806f2006-01-27 08:10:46 +000078 case 3:
Jeff Cohen41adb0d2006-01-28 18:09:06 +000079 return "i386";
Evan Cheng559806f2006-01-27 08:10:46 +000080 case 4:
Jeff Cohen41adb0d2006-01-28 18:09:06 +000081 return "i486";
82 case 5:
83 switch (Model) {
84 case 4: return "pentium-mmx";
85 default: return "pentium";
86 }
87 case 6:
88 switch (Model) {
89 case 1: return "pentiumpro";
90 case 3:
91 case 5:
92 case 6: return "pentium2";
93 case 7:
94 case 8:
95 case 10:
96 case 11: return "pentium3";
97 case 9:
98 case 13: return "pentium-m";
99 case 14: return "yonah";
100 default: return "i686";
101 }
102 case 15: {
103 switch (Model) {
104 case 3:
105 case 4:
106 return (Em64T) ? "nocona" : "prescott";
107 default:
108 return (Em64T) ? "x86-64" : "pentium4";
109 }
Evan Cheng559806f2006-01-27 08:10:46 +0000110 }
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000111
112 default:
113 return "generic";
Evan Cheng559806f2006-01-27 08:10:46 +0000114 }
Jeff Cohen216d2812006-01-28 19:48:34 +0000115 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
Jeff Cohenc4013d62006-01-28 20:30:18 +0000116 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
117 // appears to be no way to generate the wide variety of AMD-specific targets
118 // from the information returned from CPUID.
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000119 switch (Family) {
Jeff Cohenc4013d62006-01-28 20:30:18 +0000120 case 4:
121 return "i486";
122 case 5:
123 switch (Model) {
124 case 6:
125 case 7: return "k6";
126 case 8: return "k6-2";
127 case 9:
128 case 13: return "k6-3";
129 default: return "pentium";
130 }
131 case 6:
132 switch (Model) {
133 case 4: return "athlon-tbird";
134 case 6:
135 case 7:
136 case 8: return "athlon-mp";
137 case 10: return "athlon-xp";
138 default: return "athlon";
139 }
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000140 case 15:
Jeff Cohenc4013d62006-01-28 20:30:18 +0000141 switch (Model) {
142 case 5: return "athlon-fx"; // also opteron
143 default: return "athlon64";
144 }
Jeff Cohen41adb0d2006-01-28 18:09:06 +0000145
146 default:
147 return "generic";
148 }
149 } else {
Evan Cheng559806f2006-01-27 08:10:46 +0000150 return "generic";
151 }
152}
Evan Cheng97c7fc32006-01-26 09:53:06 +0000153
Chris Lattner104988a2006-01-27 22:37:09 +0000154X86Subtarget::X86Subtarget(const Module &M, const std::string &FS) {
155 stackAlignment = 8;
156 indirectExternAndWeakGlobals = false;
157 X86SSELevel = NoMMXSSE;
158 X863DNowLevel = NoThreeDNow;
159 Is64Bit = false;
160
Evan Cheng97c7fc32006-01-26 09:53:06 +0000161 // Determine default and user specified characteristics
Evan Cheng559806f2006-01-27 08:10:46 +0000162 std::string CPU = GetCurrentX86CPU();
Evan Cheng97c7fc32006-01-26 09:53:06 +0000163
164 // Parse features string.
165 ParseSubtargetFeatures(FS, CPU);
166
Chris Lattnere5600e52005-11-21 22:31:58 +0000167 // Default to ELF unless otherwise specified.
168 TargetType = isELF;
Chris Lattner9f96a322006-01-27 18:30:50 +0000169
170 // FIXME: Force these off until they work. An llc-beta option should turn
171 // them back on.
Evan Chengdbd38d72006-01-27 21:49:34 +0000172 if (!EnableSSE) {
173 X86SSELevel = NoMMXSSE;
174 X863DNowLevel = NoThreeDNow;
175 }
Chris Lattnere5600e52005-11-21 22:31:58 +0000176
Nate Begemanfb5792f2005-07-12 01:41:54 +0000177 // Set the boolean corresponding to the current target triple, or the default
178 // if one cannot be determined, to true.
179 const std::string& TT = M.getTargetTriple();
180 if (TT.length() > 5) {
Chris Lattnere5600e52005-11-21 22:31:58 +0000181 if (TT.find("cygwin") != std::string::npos ||
182 TT.find("mingw") != std::string::npos)
183 TargetType = isCygwin;
184 else if (TT.find("darwin") != std::string::npos)
185 TargetType = isDarwin;
186 else if (TT.find("win32") != std::string::npos)
187 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000188 } else if (TT.empty()) {
189#if defined(__CYGWIN__) || defined(__MINGW32__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000190 TargetType = isCygwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000191#elif defined(__APPLE__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000192 TargetType = isDarwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000193#elif defined(_WIN32)
Chris Lattnere5600e52005-11-21 22:31:58 +0000194 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000195#endif
196 }
197
Chris Lattnerd460f572005-11-21 22:43:58 +0000198 if (TargetType == isDarwin) {
Nate Begemanfb5792f2005-07-12 01:41:54 +0000199 stackAlignment = 16;
200 indirectExternAndWeakGlobals = true;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000201 }
202}