blob: 4ab87134274ddce125bcc013a65cbcd53f544be1 [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"
Evan Chenga26eb5e2006-10-06 09:17:41 +000015#include "X86GenSubtarget.inc"
Nate Begemanfb5792f2005-07-12 01:41:54 +000016#include "llvm/Module.h"
Jim Laskey05a059d2006-09-07 12:23:47 +000017#include "llvm/Support/CommandLine.h"
Nate Begemanfb5792f2005-07-12 01:41:54 +000018using namespace llvm;
19
Jim Laskey05a059d2006-09-07 12:23:47 +000020cl::opt<X86Subtarget::AsmWriterFlavorTy>
Chris Lattnercdb341d2006-09-07 22:29:41 +000021AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::unset),
Jim Laskey05a059d2006-09-07 12:23:47 +000022 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"),
Chris Lattnercdb341d2006-09-07 22:29:41 +000026 clEnumValEnd));
Jim Laskey05a059d2006-09-07 12:23:47 +000027
Evan Cheng751c0e12006-10-16 21:00:37 +000028
Anton Korobeynikov7784ebc2006-11-30 22:42:55 +000029/// 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.
Anton Korobeynikov48c8e3d2006-12-20 20:40:30 +000033bool X86Subtarget::GVRequiresExtraLoad(const GlobalValue* GV,
34 bool isDirectCall) const
Anton Korobeynikov7784ebc2006-11-30 22:42:55 +000035{
Anton Korobeynikov15fccf12006-12-20 01:03:20 +000036 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 }
Anton Korobeynikov7784ebc2006-11-30 22:42:55 +000044
45 return false;
46}
47
Chris Lattner1e39a152006-01-28 06:05:41 +000048/// 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.
Evan Cheng751c0e12006-10-16 21:00:37 +000050bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
51 unsigned *rECX, unsigned *rEDX) {
Evan Cheng25ab6902006-09-08 06:48:29 +000052#if defined(__x86_64__)
Evan Chengf896d1e2006-10-17 00:24:49 +000053 // 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"
Evan Cheng25ab6902006-09-08 06:48:29 +000057 : "=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)
Evan Cheng559806f2006-01-27 08:10:46 +000064#if defined(__GNUC__)
Evan Chengaacf9992006-11-08 20:35:37 +000065 asm ("movl\t%%ebx, %%esi\n\t"
Evan Cheng559806f2006-01-27 08:10:46 +000066 "cpuid\n\t"
Evan Chengaacf9992006-11-08 20:35:37 +000067 "xchgl\t%%ebx, %%esi\n\t"
Jeff Cohen41adb0d2006-01-28 18:09:06 +000068 : "=a" (*rEAX),
69 "=S" (*rEBX),
70 "=c" (*rECX),
71 "=d" (*rEDX)
Evan Cheng559806f2006-01-27 08:10:46 +000072 : "a" (value));
Chris Lattner1e39a152006-01-28 06:05:41 +000073 return false;
Jeff Cohen41adb0d2006-01-28 18:09:06 +000074#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;
Evan Cheng559806f2006-01-27 08:10:46 +000088#endif
89#endif
Chris Lattner1e39a152006-01-28 06:05:41 +000090 return true;
Evan Cheng559806f2006-01-27 08:10:46 +000091}
92
Evan Chenga26eb5e2006-10-06 09:17:41 +000093void X86Subtarget::AutoDetectSubtargetFeatures() {
Evan Chengb3a7e212006-01-27 19:30:30 +000094 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Jeff Cohena3496402006-01-28 18:47:32 +000095 union {
Jeff Cohen216d2812006-01-28 19:48:34 +000096 unsigned u[3];
97 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +000098 } text;
Chris Lattner3b6f4972006-11-20 18:16:05 +000099
Evan Cheng751c0e12006-10-16 21:00:37 +0000100 if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
Evan Chengabc346c2006-10-06 08:21:07 +0000101 return;
Chris Lattner3b6f4972006-11-20 18:16:05 +0000102
Evan Chengabc346c2006-10-06 08:21:07 +0000103 // FIXME: support for AMD family of processors.
104 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
Evan Cheng751c0e12006-10-16 21:00:37 +0000105 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Evan Chengabc346c2006-10-06 08:21:07 +0000106
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
Evan Cheng751c0e12006-10-16 21:00:37 +0000112 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Evan Chengabc346c2006-10-06 08:21:07 +0000113 HasX86_64 = (EDX >> 29) & 0x1;
Evan Cheng559806f2006-01-27 08:10:46 +0000114 }
115}
Evan Cheng97c7fc32006-01-26 09:53:06 +0000116
Evan Chenga26eb5e2006-10-06 09:17:41 +0000117static const char *GetCurrentX86CPU() {
118 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Evan Cheng751c0e12006-10-16 21:00:37 +0000119 if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
Evan Chenga26eb5e2006-10-06 09:17:41 +0000120 return "generic";
121 unsigned Family = (EAX >> 8) & 0xf; // Bits 8 - 11
122 unsigned Model = (EAX >> 4) & 0xf; // Bits 4 - 7
Evan Cheng751c0e12006-10-16 21:00:37 +0000123 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Evan Cheng3cff9f82006-10-06 18:57:51 +0000124 bool Em64T = (EDX >> 29) & 0x1;
Evan Chenga26eb5e2006-10-06 09:17:41 +0000125
126 union {
127 unsigned u[3];
128 char c[12];
129 } text;
130
Evan Cheng751c0e12006-10-16 21:00:37 +0000131 X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
Evan Chenga26eb5e2006-10-06 09:17:41 +0000132 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
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000211/// SetJITMode - This is called to inform the subtarget info that we are
212/// producing code for the JIT.
Anton Korobeynikov48c8e3d2006-12-20 20:40:30 +0000213void X86Subtarget::SetJITMode()
214{
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000215 // 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
Evan Cheng25ab6902006-09-08 06:48:29 +0000221X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
Evan Cheng8e0055d2006-10-04 18:33:00 +0000222 : AsmFlavor(AsmWriterFlavor)
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 , X86SSELevel(NoMMXSSE)
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 , 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)
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000229 , GenerateExtraLoadsForGVs(true)
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 , TargetType(isELF) { // Default to ELF unless otherwise specified.
Chris Lattner104988a2006-01-27 22:37:09 +0000231
Evan Cheng97c7fc32006-01-26 09:53:06 +0000232 // Determine default and user specified characteristics
Evan Chenga26eb5e2006-10-06 09:17:41 +0000233 if (!FS.empty()) {
234 // If feature string is not empty, parse features string.
235 std::string CPU = GetCurrentX86CPU();
236 ParseSubtargetFeatures(FS, CPU);
Chris Lattner3b6f4972006-11-20 18:16:05 +0000237
238 if (Is64Bit && !HasX86_64)
Bill Wendlingf5da1332006-12-07 22:21:48 +0000239 cerr << "Warning: Generation of 64-bit code for a 32-bit processor "
240 << "requested.\n";
Chris Lattner3b6f4972006-11-20 18:16:05 +0000241 if (Is64Bit && X86SSELevel < SSE2)
Bill Wendlingf5da1332006-12-07 22:21:48 +0000242 cerr << "Warning: 64-bit processors all have at least SSE2.\n";
Chris Lattner3b6f4972006-11-20 18:16:05 +0000243 } 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;
Evan Cheng25ab6902006-09-08 06:48:29 +0000254 }
255
Nate Begemanfb5792f2005-07-12 01:41:54 +0000256 // 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) {
Chris Lattnere5600e52005-11-21 22:31:58 +0000260 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;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000267 } else if (TT.empty()) {
268#if defined(__CYGWIN__) || defined(__MINGW32__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000269 TargetType = isCygwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000270#elif defined(__APPLE__)
Chris Lattnere5600e52005-11-21 22:31:58 +0000271 TargetType = isDarwin;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000272#elif defined(_WIN32)
Chris Lattnere5600e52005-11-21 22:31:58 +0000273 TargetType = isWindows;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000274#endif
275 }
276
Chris Lattnercdb341d2006-09-07 22:29:41 +0000277 // 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
Evan Chengb4809b22006-11-29 02:00:40 +0000287 if (TargetType == isDarwin ||
288 TargetType == isCygwin ||
289 (TargetType == isELF && Is64Bit))
Nate Begemanfb5792f2005-07-12 01:41:54 +0000290 stackAlignment = 16;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000291}