blob: 5ceafa4c593d2fd3af02b22c3817d1c5f6e5c9fb [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
Dan Gohman089efff2008-05-13 00:00:25 +000024static cl::opt<X86Subtarget::AsmWriterFlavorTy>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025AsmWriterFlavor("x86-asm-syntax", cl::init(X86Subtarget::Unset),
26 cl::desc("Choose style of code to emit from X86 backend:"),
27 cl::values(
Dan Gohman669b9bf2008-10-14 20:25:08 +000028 clEnumValN(X86Subtarget::ATT, "att", "Emit AT&T-style assembly"),
29 clEnumValN(X86Subtarget::Intel, "intel", "Emit Intel-style assembly"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 clEnumValEnd));
31
32
33/// True if accessing the GV requires an extra load. For Windows, dllimported
34/// symbols are indirect, loading the value at address GV rather then the
35/// value of GV itself. This means that the GlobalAddress must be in the base
36/// or index register of the address, not the GV offset field.
37bool X86Subtarget::GVRequiresExtraLoad(const GlobalValue* GV,
38 const TargetMachine& TM,
39 bool isDirectCall) const
40{
41 // FIXME: PIC
Evan Cheng1f282202008-07-16 01:34:02 +000042 if (TM.getRelocationModel() != Reloc::Static &&
43 TM.getCodeModel() != CodeModel::Large) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 if (isTargetDarwin()) {
Evan Cheng17cc7952008-12-08 19:29:03 +000045 if (isDirectCall)
46 return false;
Evan Chenga65854f2008-12-05 01:06:39 +000047 bool isDecl = GV->isDeclaration() && !GV->hasNotBeenReadFromBitcode();
48 if (GV->hasHiddenVisibility() &&
49 (Is64Bit || (!isDecl && !GV->hasCommonLinkage())))
50 // If symbol visibility is hidden, the extra load is not needed if
51 // target is x86-64 or the symbol is definitely defined in the current
52 // translation unit.
53 return false;
Dan Gohman653cc452008-12-08 17:38:02 +000054 return !isDirectCall && (isDecl || GV->mayBeOverridden());
Anton Korobeynikov6835eb62008-01-20 13:58:16 +000055 } else if (isTargetELF()) {
Rafael Espindolaae289c12008-06-02 07:52:43 +000056 // Extra load is needed for all externally visible.
57 if (isDirectCall)
58 return false;
Rafael Espindolaa168fc92009-01-15 20:18:42 +000059 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
Rafael Espindolaae289c12008-06-02 07:52:43 +000060 return false;
61 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 } else if (isTargetCygMing() || isTargetWindows()) {
63 return (GV->hasDLLImportLinkage());
64 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +000065 }
Dale Johannesen64660e92008-12-05 21:47:27 +000066 return false;
67}
68
69/// True if accessing the GV requires a register. This is a superset of the
70/// cases where GVRequiresExtraLoad is true. Some variations of PIC require
71/// a register, but not an extra load.
72bool X86Subtarget::GVRequiresRegister(const GlobalValue *GV,
73 const TargetMachine& TM,
74 bool isDirectCall) const
75{
76 if (GVRequiresExtraLoad(GV, TM, isDirectCall))
77 return true;
78 // Code below here need only consider cases where GVRequiresExtraLoad
79 // returns false.
80 if (TM.getRelocationModel() == Reloc::PIC_)
81 return !isDirectCall &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +000082 (GV->hasLocalLinkage() || GV->hasExternalLinkage());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 return false;
84}
85
Bill Wendling5db7ffb2008-09-30 21:22:07 +000086/// getBZeroEntry - This function returns the name of a function which has an
87/// interface like the non-standard bzero function, if such a function exists on
88/// the current subtarget and it is considered prefereable over memset with zero
89/// passed as the second argument. Otherwise it returns null.
Bill Wendlingd3752032008-09-30 22:05:33 +000090const char *X86Subtarget::getBZeroEntry() const {
Dan Gohmanf95c2bf2008-04-01 20:38:36 +000091 // Darwin 10 has a __bzero entry point for this purpose.
92 if (getDarwinVers() >= 10)
Bill Wendlingd3752032008-09-30 22:05:33 +000093 return "__bzero";
Dan Gohmanf95c2bf2008-04-01 20:38:36 +000094
95 return 0;
96}
97
Dan Gohman47170992008-12-16 03:35:01 +000098/// getSpecialAddressLatency - For targets where it is beneficial to
99/// backschedule instructions that compute addresses, return a value
100/// indicating the number of scheduling cycles of backscheduling that
101/// should be attempted.
102unsigned X86Subtarget::getSpecialAddressLatency() const {
103 // For x86 out-of-order targets, back-schedule address computations so
104 // that loads and stores aren't blocked.
105 // This value was chosen arbitrarily.
106 return 200;
107}
108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109/// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
110/// specified arguments. If we can't run cpuid on the host, return true.
111bool X86::GetCpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
112 unsigned *rECX, unsigned *rEDX) {
113#if defined(__x86_64__)
114 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
115 asm ("movq\t%%rbx, %%rsi\n\t"
116 "cpuid\n\t"
117 "xchgq\t%%rbx, %%rsi\n\t"
118 : "=a" (*rEAX),
119 "=S" (*rEBX),
120 "=c" (*rECX),
121 "=d" (*rEDX)
122 : "a" (value));
123 return false;
124#elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
125#if defined(__GNUC__)
126 asm ("movl\t%%ebx, %%esi\n\t"
127 "cpuid\n\t"
128 "xchgl\t%%ebx, %%esi\n\t"
129 : "=a" (*rEAX),
130 "=S" (*rEBX),
131 "=c" (*rECX),
132 "=d" (*rEDX)
133 : "a" (value));
134 return false;
135#elif defined(_MSC_VER)
136 __asm {
137 mov eax,value
138 cpuid
139 mov esi,rEAX
140 mov dword ptr [esi],eax
141 mov esi,rEBX
142 mov dword ptr [esi],ebx
143 mov esi,rECX
144 mov dword ptr [esi],ecx
145 mov esi,rEDX
146 mov dword ptr [esi],edx
147 }
148 return false;
149#endif
150#endif
151 return true;
152}
153
Evan Cheng95a77fd2009-01-02 05:35:45 +0000154static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
155 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
156 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
157 if (Family == 6 || Family == 0xf) {
158 if (Family == 0xf)
159 // Examine extended family ID if family ID is F.
160 Family += (EAX >> 20) & 0xff; // Bits 20 - 27
161 // Examine extended model ID if family ID is 6 or F.
162 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
163 }
164}
165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166void X86Subtarget::AutoDetectSubtargetFeatures() {
167 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
168 union {
169 unsigned u[3];
170 char c[12];
171 } text;
172
173 if (X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
174 return;
175
176 X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
177
178 if ((EDX >> 23) & 0x1) X86SSELevel = MMX;
179 if ((EDX >> 25) & 0x1) X86SSELevel = SSE1;
180 if ((EDX >> 26) & 0x1) X86SSELevel = SSE2;
181 if (ECX & 0x1) X86SSELevel = SSE3;
182 if ((ECX >> 9) & 0x1) X86SSELevel = SSSE3;
Nate Begemanb2975562008-02-03 07:18:54 +0000183 if ((ECX >> 19) & 0x1) X86SSELevel = SSE41;
184 if ((ECX >> 20) & 0x1) X86SSELevel = SSE42;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Evan Cheng95a77fd2009-01-02 05:35:45 +0000186 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
187 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
188 if (IsIntel || IsAMD) {
189 // Determine if bit test memory instructions are slow.
190 unsigned Family = 0;
191 unsigned Model = 0;
192 DetectFamilyModel(EAX, Family, Model);
193 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
196 HasX86_64 = (EDX >> 29) & 0x1;
197 }
198}
199
200static const char *GetCurrentX86CPU() {
201 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
202 if (X86::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
203 return "generic";
Evan Cheng95a77fd2009-01-02 05:35:45 +0000204 unsigned Family = 0;
205 unsigned Model = 0;
206 DetectFamilyModel(EAX, Family, Model);
Evan Chengedde6842009-01-02 05:29:20 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 X86::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
209 bool Em64T = (EDX >> 29) & 0x1;
210
211 union {
212 unsigned u[3];
213 char c[12];
214 } text;
215
216 X86::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1);
217 if (memcmp(text.c, "GenuineIntel", 12) == 0) {
218 switch (Family) {
219 case 3:
220 return "i386";
221 case 4:
222 return "i486";
223 case 5:
224 switch (Model) {
225 case 4: return "pentium-mmx";
226 default: return "pentium";
227 }
228 case 6:
229 switch (Model) {
230 case 1: return "pentiumpro";
231 case 3:
232 case 5:
233 case 6: return "pentium2";
234 case 7:
235 case 8:
236 case 10:
237 case 11: return "pentium3";
238 case 9:
239 case 13: return "pentium-m";
240 case 14: return "yonah";
Evan Cheng5211b422009-01-03 04:04:46 +0000241 case 15:
242 case 22: // Celeron M 540
243 return "core2";
244 case 23: // 45nm: Penryn , Wolfdale, Yorkfield (XE)
245 return "penryn";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 default: return "i686";
247 }
248 case 15: {
249 switch (Model) {
250 case 3:
251 case 4:
Evan Cheng5211b422009-01-03 04:04:46 +0000252 case 6: // same as 4, but 65nm
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 return (Em64T) ? "nocona" : "prescott";
Evan Chengcfadd3b2009-01-05 08:45:01 +0000254 case 26:
255 return "corei7";
Evan Cheng5211b422009-01-03 04:04:46 +0000256 case 28:
Evan Chengcfadd3b2009-01-05 08:45:01 +0000257 return "atom";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 default:
259 return (Em64T) ? "x86-64" : "pentium4";
260 }
261 }
262
263 default:
264 return "generic";
265 }
266 } else if (memcmp(text.c, "AuthenticAMD", 12) == 0) {
267 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
268 // appears to be no way to generate the wide variety of AMD-specific targets
269 // from the information returned from CPUID.
270 switch (Family) {
271 case 4:
272 return "i486";
273 case 5:
274 switch (Model) {
275 case 6:
276 case 7: return "k6";
277 case 8: return "k6-2";
278 case 9:
279 case 13: return "k6-3";
280 default: return "pentium";
281 }
282 case 6:
283 switch (Model) {
284 case 4: return "athlon-tbird";
285 case 6:
286 case 7:
287 case 8: return "athlon-mp";
288 case 10: return "athlon-xp";
289 default: return "athlon";
290 }
291 case 15:
292 switch (Model) {
293 case 1: return "opteron";
294 case 5: return "athlon-fx"; // also opteron
295 default: return "athlon64";
296 }
297 default:
298 return "generic";
299 }
300 } else {
301 return "generic";
302 }
303}
304
305X86Subtarget::X86Subtarget(const Module &M, const std::string &FS, bool is64Bit)
306 : AsmFlavor(AsmWriterFlavor)
Duncan Sandsde5f95f2008-11-28 09:29:37 +0000307 , PICStyle(PICStyles::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 , X86SSELevel(NoMMXSSE)
Evan Chengb6992de2008-04-16 19:03:02 +0000309 , X863DNowLevel(NoThreeDNow)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 , HasX86_64(false)
Evan Cheng95a77fd2009-01-02 05:35:45 +0000311 , IsBTMemSlow(false)
Chris Lattner93a2d432008-01-02 19:44:55 +0000312 , DarwinVers(0)
Dan Gohmande22f242008-05-05 18:43:07 +0000313 , IsLinux(false)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 , stackAlignment(8)
315 // FIXME: this is a known good value for Yonah. How about others?
Rafael Espindola7afa9b12007-10-31 11:52:06 +0000316 , MaxInlineSizeThreshold(128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 , Is64Bit(is64Bit)
318 , TargetType(isELF) { // Default to ELF unless otherwise specified.
Mon P Wang078a62d2008-05-05 19:05:59 +0000319
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 // Determine default and user specified characteristics
321 if (!FS.empty()) {
322 // If feature string is not empty, parse features string.
323 std::string CPU = GetCurrentX86CPU();
324 ParseSubtargetFeatures(FS, CPU);
Edwin Török4031b792009-02-02 21:57:34 +0000325 // All X86-64 CPUs also have SSE2, however user might request no SSE via
326 // -mattr, so don't force SSELevel here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 } else {
328 // Otherwise, use CPUID to auto-detect feature set.
329 AutoDetectSubtargetFeatures();
Dan Gohman4092bbc2009-02-03 00:04:43 +0000330 // If requesting codegen for X86-64, make sure that 64-bit features
331 // are enabled.
332 if (Is64Bit)
Edwin Török4031b792009-02-02 21:57:34 +0000333 HasX86_64 = true;
Dan Gohman4092bbc2009-02-03 00:04:43 +0000334 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
335 if (Is64Bit && X86SSELevel < SSE2)
336 X86SSELevel = SSE2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 }
Dan Gohman4092bbc2009-02-03 00:04:43 +0000338
Evan Cheng5211b422009-01-03 04:04:46 +0000339 DOUT << "Subtarget features: SSELevel " << X86SSELevel
340 << ", 3DNowLevel " << X863DNowLevel
341 << ", 64bit " << HasX86_64 << "\n";
Dan Gohman4092bbc2009-02-03 00:04:43 +0000342 assert((!Is64Bit || HasX86_64) &&
343 "64-bit code requested on a subtarget that doesn't support it!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
345 // Set the boolean corresponding to the current target triple, or the default
346 // if one cannot be determined, to true.
347 const std::string& TT = M.getTargetTriple();
348 if (TT.length() > 5) {
Duncan Sandsdfd94582008-01-08 10:06:15 +0000349 size_t Pos;
Chris Lattner93a2d432008-01-02 19:44:55 +0000350 if ((Pos = TT.find("-darwin")) != std::string::npos) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 TargetType = isDarwin;
Chris Lattner93a2d432008-01-02 19:44:55 +0000352
353 // Compute the darwin version number.
354 if (isdigit(TT[Pos+7]))
355 DarwinVers = atoi(&TT[Pos+7]);
356 else
357 DarwinVers = 8; // Minimum supported darwin is Tiger.
Dan Gohmana65530a2008-05-05 00:28:39 +0000358 } else if (TT.find("linux") != std::string::npos) {
Dan Gohman2593e2b2008-05-05 16:11:31 +0000359 // Linux doesn't imply ELF, but we don't currently support anything else.
360 TargetType = isELF;
361 IsLinux = true;
Chris Lattner93a2d432008-01-02 19:44:55 +0000362 } else if (TT.find("cygwin") != std::string::npos) {
363 TargetType = isCygwin;
364 } else if (TT.find("mingw") != std::string::npos) {
365 TargetType = isMingw;
366 } else if (TT.find("win32") != std::string::npos) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 TargetType = isWindows;
Anton Korobeynikovf0ce64b2008-03-22 21:12:53 +0000368 } else if (TT.find("windows") != std::string::npos) {
369 TargetType = isWindows;
Chris Lattner93a2d432008-01-02 19:44:55 +0000370 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 } else if (TT.empty()) {
372#if defined(__CYGWIN__)
373 TargetType = isCygwin;
Anton Korobeynikov62a51e42008-03-22 21:18:22 +0000374#elif defined(__MINGW32__) || defined(__MINGW64__)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 TargetType = isMingw;
376#elif defined(__APPLE__)
377 TargetType = isDarwin;
Chris Lattner93a2d432008-01-02 19:44:55 +0000378#if __APPLE_CC__ > 5400
379 DarwinVers = 9; // GCC 5400+ is Leopard.
380#else
381 DarwinVers = 8; // Minimum supported darwin is Tiger.
382#endif
383
Anton Korobeynikov62a51e42008-03-22 21:18:22 +0000384#elif defined(_WIN32) || defined(_WIN64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 TargetType = isWindows;
Dan Gohmana65530a2008-05-05 00:28:39 +0000386#elif defined(__linux__)
387 // Linux doesn't imply ELF, but we don't currently support anything else.
Dan Gohman2593e2b2008-05-05 16:11:31 +0000388 TargetType = isELF;
389 IsLinux = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390#endif
391 }
392
393 // If the asm syntax hasn't been overridden on the command line, use whatever
394 // the target wants.
395 if (AsmFlavor == X86Subtarget::Unset) {
Chris Lattner93a2d432008-01-02 19:44:55 +0000396 AsmFlavor = (TargetType == isWindows)
397 ? X86Subtarget::Intel : X86Subtarget::ATT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 }
399
Anton Korobeynikovcdd93812008-04-23 18:16:16 +0000400 // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
401 // bit targets.
402 if (TargetType == isDarwin || Is64Bit)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 stackAlignment = 16;
Anton Korobeynikov06c42402008-04-12 22:12:22 +0000404
405 if (StackAlignment)
406 stackAlignment = StackAlignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407}