blob: df1f7db85fcf71dea38f5c3468aae1aa61c685a9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the X86 specific subclass of TargetSubtarget.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng5211b422009-01-03 04:04:46 +000014#define DEBUG_TYPE "subtarget"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "X86Subtarget.h"
16#include "X86GenSubtarget.inc"
17#include "llvm/Module.h"
18#include "llvm/Support/CommandLine.h"
Evan Cheng5211b422009-01-03 04:04:46 +000019#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovb214a522008-04-23 18:18:10 +000021#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
Chris Lattner1d8091f2009-04-25 18:27:23 +000024#if defined(_MSC_VER)
25 #include <intrin.h>
26#endif
27
Dan Gohman089efff2008-05-13 00:00:25 +000028static cl::opt<X86Subtarget::AsmWriterFlavorTy>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::Unset),
30 cl::desc("Choose style of code to emit from X86 backend:"),
31 cl::values(
Dan Gohman669b9bf2008-10-14 20:25:08 +000032 clEnumValN(X86Subtarget::ATT, "att", "Emit AT&T-style assembly"),
33 clEnumValN(X86Subtarget::Intel, "intel", "Emit Intel-style assembly"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 clEnumValEnd));
35
36
37/// True if accessing the GV requires an extra load. For Windows, dllimported
38/// symbols are indirect, loading the value at address GV rather then the
39/// value of GV itself. This means that the GlobalAddress must be in the base
40/// or index register of the address, not the GV offset field.
41bool X86Subtarget::GVRequiresExtraLoad(const GlobalValue* GV,
42 const TargetMachine& TM,
43 bool isDirectCall) const
44{
45 // FIXME: PIC
Evan Cheng1f282202008-07-16 01:34:02 +000046 if (TM.getRelocationModel() != Reloc::Static &&
47 TM.getCodeModel() != CodeModel::Large) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 if (isTargetDarwin()) {
Evan Cheng17cc7952008-12-08 19:29:03 +000049 if (isDirectCall)
50 return false;
Evan Chenga65854f2008-12-05 01:06:39 +000051 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
52 if (GV->hasHiddenVisibility() &&
53 (Is64Bit || (!isDecl && !GV->hasCommonLinkage())))
54 // If symbol visibility is hidden, the extra load is not needed if
55 // target is x86-64 or the symbol is definitely defined in the current
56 // translation unit.
57 return false;
Duncan Sands19d161f2009-03-07 15:45:40 +000058 return !isDirectCall && (isDecl || GV->isWeakForLinker());
Anton Korobeynikov6835eb62008-01-20 13:58:16 +000059 } else if (isTargetELF()) {
Rafael Espindolaae289c12008-06-02 07:52:43 +000060 // Extra load is needed for all externally visible.
61 if (isDirectCall)
62 return false;
Rafael Espindolaa168fc92009-01-15 20:18:42 +000063 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Rafael Espindolaae289c12008-06-02 07:52:43 +000064 return false;
65 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 } else if (isTargetCygMing() || isTargetWindows()) {
67 return (GV->hasDLLImportLinkage());
68 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000069 }
Dale Johannesen64660e92008-12-05 21:47:27 +000070 return false;
71}
72
73/// True if accessing the GV requires a register. This is a superset of the
74/// cases where GVRequiresExtraLoad is true. Some variations of PIC require
75/// a register, but not an extra load.
76bool X86Subtarget::GVRequiresRegister(const GlobalValue *GV,
77 const TargetMachine& TM,
78 bool isDirectCall) const
79{
80 if (GVRequiresExtraLoad(GV, TM, isDirectCall))
81 return true;
82 // Code below here need only consider cases where GVRequiresExtraLoad
83 // returns false.
84 if (TM.getRelocationModel() == Reloc::PIC_)
85 return !isDirectCall &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +000086 (GV->hasLocalLinkage() || GV->hasExternalLinkage());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 return false;
88}
89
Bill Wendling5db7ffb2008-09-30 21:22:07 +000090/// getBZeroEntry - This function returns the name of a function which has an
91/// interface like the non-standard bzero function, if such a function exists on
92/// the current subtarget and it is considered prefereable over memset with zero
93/// passed as the second argument. Otherwise it returns null.
Bill Wendlingd3752032008-09-30 22:05:33 +000094const char *X86Subtarget::getBZeroEntry() const {
Dan Gohmanf95c2bf2008-04-01 20:38:36 +000095 // Darwin 10 has a __bzero entry point for this purpose.
96 if (getDarwinVers() >= 10)
Bill Wendlingd3752032008-09-30 22:05:33 +000097 return "__bzero";
Dan Gohmanf95c2bf2008-04-01 20:38:36 +000098
99 return 0;
100}
101
Dan Gohman47170992008-12-16 03:35:01 +0000102/// getSpecialAddressLatency - For targets where it is beneficial to
103/// backschedule instructions that compute addresses, return a value
104/// indicating the number of scheduling cycles of backscheduling that
105/// should be attempted.
106unsigned X86Subtarget::getSpecialAddressLatency() const {
107 // For x86 out-of-order targets, back-schedule address computations so
108 // that loads and stores aren't blocked.
109 // This value was chosen arbitrarily.
110 return 200;
111}
112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113/// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
114/// specified arguments. If we can't run cpuid on the host, return true.
115bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
116 unsigned *rECX, unsigned *rEDX) {
Chris Lattner1d8091f2009-04-25 18:27:23 +0000117#if defined(__x86_64__) || defined(_M_AMD64)
118 #if defined(__GNUC__)
119 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
120 asm ("movq\t%%rbx, %%rsi\n\t"
121 "cpuid\n\t"
122 "xchgq\t%%rbx, %%rsi\n\t"
123 : "=a" (*rEAX),
124 "=S" (*rEBX),
125 "=c" (*rECX),
126 "=d" (*rEDX)
127 : "a" (value));
128 return false;
129 #elif defined(_MSC_VER)
130 int registers[4];
131 __cpuid(registers, value);
132 *rEAX = registers[0];
133 *rEBX = registers[1];
134 *rECX = registers[2];
135 *rEDX = registers[3];
136 return false;
137 #endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138#elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Chris Lattner1d8091f2009-04-25 18:27:23 +0000139 #if defined(__GNUC__)
140 asm ("movl\t%%ebx, %%esi\n\t"
141 "cpuid\n\t"
142 "xchgl\t%%ebx, %%esi\n\t"
143 : "=a" (*rEAX),
144 "=S" (*rEBX),
145 "=c" (*rECX),
146 "=d" (*rEDX)
147 : "a" (value));
148 return false;
149 #elif defined(_MSC_VER)
150 __asm {
151 mov eax,value
152 cpuid
153 mov esi,rEAX
154 mov dword ptr [esi],eax
155 mov esi,rEBX
156 mov dword ptr [esi],ebx
157 mov esi,rECX
158 mov dword ptr [esi],ecx
159 mov esi,rEDX
160 mov dword ptr [esi],edx
161 }
162 return false;
163 #endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164#endif
165 return true;
166}
167
Evan Cheng95a77fd2009-01-02 05:35:45 +0000168static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
169 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
170 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
171 if (Family == 6 || Family == 0xf) {
172 if (Family == 0xf)
173 // Examine extended family ID if family ID is F.
174 Family += (EAX >> 20) & 0xff; // Bits 20 - 27
175 // Examine extended model ID if family ID is 6 or F.
176 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
177 }
178}
179
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180void X86Subtarget::AutoDetectSubtargetFeatures() {
181 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
182 union {
183 unsigned u[3];
184 char c[12];
185 } text;
186
187 if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
188 return;
189
190 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
191
192 if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
193 if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
194 if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
195 if (ECX & 0x1) X86SSELevel = SSE3;
196 if ((ECX >> 9) & 0x1) X86SSELevel = SSSE3;
Nate Begemanb2975562008-02-03 07:18:54 +0000197 if ((ECX >> 19) & 0x1) X86SSELevel = SSE41;
198 if ((ECX >> 20) & 0x1) X86SSELevel = SSE42;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199
Evan Cheng95a77fd2009-01-02 05:35:45 +0000200 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
201 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
202 if (IsIntel || IsAMD) {
203 // Determine if bit test memory instructions are slow.
204 unsigned Family = 0;
205 unsigned Model = 0;
206 DetectFamilyModel(EAX, Family, Model);
207 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
210 HasX86_64 = (EDX >> 29) & 0x1;
211 }
212}
213
214static const char *GetCurrentX86CPU() {
215 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
216 if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
217 return "generic";
Evan Cheng95a77fd2009-01-02 05:35:45 +0000218 unsigned Family = 0;
219 unsigned Model = 0;
220 DetectFamilyModel(EAX, Family, Model);
Evan Chengedde6842009-01-02 05:29:20 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
223 bool Em64T = (EDX >> 29) & 0x1;
224
225 union {
226 unsigned u[3];
227 char c[12];
228 } text;
229
230 X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
231 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
232 switch (Family) {
233 case 3:
234 return "i386";
235 case 4:
236 return "i486";
237 case 5:
238 switch (Model) {
239 case 4: return "pentium-mmx";
240 default: return "pentium";
241 }
242 case 6:
243 switch (Model) {
244 case 1: return "pentiumpro";
245 case 3:
246 case 5:
247 case 6: return "pentium2";
248 case 7:
249 case 8:
250 case 10:
251 case 11: return "pentium3";
252 case 9:
253 case 13: return "pentium-m";
254 case 14: return "yonah";
Evan Cheng5211b422009-01-03 04:04:46 +0000255 case 15:
256 case 22: // Celeron M 540
257 return "core2";
258 case 23: // 45nm: Penryn , Wolfdale, Yorkfield (XE)
259 return "penryn";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 default: return "i686";
261 }
262 case 15: {
263 switch (Model) {
264 case 3:
265 case 4:
Evan Cheng5211b422009-01-03 04:04:46 +0000266 case 6: // same as 4, but 65nm
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 return (Em64T) ? "nocona" : "prescott";
Evan Chengcfadd3b2009-01-05 08:45:01 +0000268 case 26:
269 return "corei7";
Evan Cheng5211b422009-01-03 04:04:46 +0000270 case 28:
Evan Chengcfadd3b2009-01-05 08:45:01 +0000271 return "atom";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 default:
273 return (Em64T) ? "x86-64" : "pentium4";
274 }
275 }
276
277 default:
278 return "generic";
279 }
280 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
281 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
282 // appears to be no way to generate the wide variety of AMD-specific targets
283 // from the information returned from CPUID.
284 switch (Family) {
285 case 4:
286 return "i486";
287 case 5:
288 switch (Model) {
289 case 6:
290 case 7: return "k6";
291 case 8: return "k6-2";
292 case 9:
293 case 13: return "k6-3";
294 default: return "pentium";
295 }
296 case 6:
297 switch (Model) {
298 case 4: return "athlon-tbird";
299 case 6:
300 case 7:
301 case 8: return "athlon-mp";
302 case 10: return "athlon-xp";
303 default: return "athlon";
304 }
305 case 15:
306 switch (Model) {
307 case 1: return "opteron";
308 case 5: return "athlon-fx"; // also opteron
309 default: return "athlon64";
310 }
311 default:
312 return "generic";
313 }
314 } else {
315 return "generic";
316 }
317}
318
319X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
320 : AsmFlavor(AsmWriterFlavor)
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000321 , PICStyle(PICStyles::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 , X86SSELevel(NoMMXSSE)
Evan Chengb6992de2008-04-16 19:03:02 +0000323 , X863DNowLevel(NoThreeDNow)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 , HasX86_64(false)
Evan Cheng95a77fd2009-01-02 05:35:45 +0000325 , IsBTMemSlow(false)
Chris Lattner93a2d432008-01-02 19:44:55 +0000326 , DarwinVers(0)
Dan Gohmande22f242008-05-05 18:43:07 +0000327 , IsLinux(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 , stackAlignment(8)
329 // FIXME: this is a known good value for Yonah. How about others?
Rafael Espindola7afa9b12007-10-31 11:52:06 +0000330 , MaxInlineSizeThreshold(128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 , Is64Bit(is64Bit)
332 , TargetType(isELF) { // Default to ELF unless otherwise specified.
Mon P Wang078a62d2008-05-05 19:05:59 +0000333
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 // Determine default and user specified characteristics
335 if (!FS.empty()) {
336 // If feature string is not empty, parse features string.
337 std::string CPU = GetCurrentX86CPU();
338 ParseSubtargetFeatures(FS, CPU);
Edwin Török4031b792009-02-02 21:57:34 +0000339 // All X86-64 CPUs also have SSE2, however user might request no SSE via
340 // -mattr, so don't force SSELevel here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 } else {
342 // Otherwise, use CPUID to auto-detect feature set.
343 AutoDetectSubtargetFeatures();
Dan Gohman4092bbc2009-02-03 00:04:43 +0000344 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
345 if (Is64Bit && X86SSELevel < SSE2)
346 X86SSELevel = SSE2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 }
Dan Gohman4092bbc2009-02-03 00:04:43 +0000348
Dan Gohmand3ef6c92009-02-03 18:53:21 +0000349 // If requesting codegen for X86-64, make sure that 64-bit features
350 // are enabled.
351 if (Is64Bit)
352 HasX86_64 = true;
353
Evan Cheng5211b422009-01-03 04:04:46 +0000354 DOUT << "Subtarget features: SSELevel " << X86SSELevel
355 << ", 3DNowLevel " << X863DNowLevel
356 << ", 64bit " << HasX86_64 << "\n";
Dan Gohman4092bbc2009-02-03 00:04:43 +0000357 assert((!Is64Bit || HasX86_64) &&
358 "64-bit code requested on a subtarget that doesn't support it!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359
360 // Set the boolean corresponding to the current target triple, or the default
361 // if one cannot be determined, to true.
362 const std::string& TT = M.getTargetTriple();
363 if (TT.length() > 5) {
Duncan Sandsdfd94582008-01-08 10:06:15 +0000364 size_t Pos;
Chris Lattner93a2d432008-01-02 19:44:55 +0000365 if ((Pos = TT.find("-darwin")) != std::string::npos) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 TargetType = isDarwin;
Chris Lattner93a2d432008-01-02 19:44:55 +0000367
368 // Compute the darwin version number.
369 if (isdigit(TT[Pos+7]))
370 DarwinVers = atoi(&TT[Pos+7]);
371 else
372 DarwinVers = 8; // Minimum supported darwin is Tiger.
Dan Gohmana65530a2008-05-05 00:28:39 +0000373 } else if (TT.find("linux") != std::string::npos) {
Dan Gohman2593e2b2008-05-05 16:11:31 +0000374 // Linux doesn't imply ELF, but we don't currently support anything else.
375 TargetType = isELF;
376 IsLinux = true;
Chris Lattner93a2d432008-01-02 19:44:55 +0000377 } else if (TT.find("cygwin") != std::string::npos) {
378 TargetType = isCygwin;
379 } else if (TT.find("mingw") != std::string::npos) {
380 TargetType = isMingw;
381 } else if (TT.find("win32") != std::string::npos) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 TargetType = isWindows;
Anton Korobeynikovf0ce64b2008-03-22 21:12:53 +0000383 } else if (TT.find("windows") != std::string::npos) {
384 TargetType = isWindows;
Chris Lattner93a2d432008-01-02 19:44:55 +0000385 }
Mon P Wang23bbfc32009-02-28 00:25:30 +0000386 else if (TT.find("-cl") != std::string::npos) {
387 TargetType = isDarwin;
388 DarwinVers = 9;
389 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 } else if (TT.empty()) {
391#if defined(__CYGWIN__)
392 TargetType = isCygwin;
Anton Korobeynikov62a51e42008-03-22 21:18:22 +0000393#elif defined(__MINGW32__) || defined(__MINGW64__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 TargetType = isMingw;
395#elif defined(__APPLE__)
396 TargetType = isDarwin;
Chris Lattner93a2d432008-01-02 19:44:55 +0000397#if __APPLE_CC__ > 5400
398 DarwinVers = 9; // GCC 5400+ is Leopard.
399#else
400 DarwinVers = 8; // Minimum supported darwin is Tiger.
401#endif
402
Anton Korobeynikov62a51e42008-03-22 21:18:22 +0000403#elif defined(_WIN32) || defined(_WIN64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 TargetType = isWindows;
Dan Gohmana65530a2008-05-05 00:28:39 +0000405#elif defined(__linux__)
406 // Linux doesn't imply ELF, but we don't currently support anything else.
Dan Gohman2593e2b2008-05-05 16:11:31 +0000407 TargetType = isELF;
408 IsLinux = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409#endif
410 }
411
412 // If the asm syntax hasn't been overridden on the command line, use whatever
413 // the target wants.
414 if (AsmFlavor == X86Subtarget::Unset) {
Chris Lattner93a2d432008-01-02 19:44:55 +0000415 AsmFlavor = (TargetType == isWindows)
416 ? X86Subtarget::Intel : X86Subtarget::ATT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 }
418
Anton Korobeynikovcdd93812008-04-23 18:16:16 +0000419 // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
420 // bit targets.
421 if (TargetType == isDarwin || Is64Bit)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 stackAlignment = 16;
Anton Korobeynikov06c42402008-04-12 22:12:22 +0000423
424 if (StackAlignment)
425 stackAlignment = StackAlignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426}