blob: 3ad84b98c831614f0feaa8e8cc05fe7244bf368f [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begemanfb5792f2005-07-12 01:41:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the X86 specific subclass of TargetSubtarget.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng5b925c02009-01-03 04:04:46 +000014#define DEBUG_TYPE "subtarget"
Nate Begemanfb5792f2005-07-12 01:41:54 +000015#include "X86Subtarget.h"
Chris Lattnerd392bd92009-07-10 07:20:05 +000016#include "X86InstrInfo.h"
Evan Chenga26eb5e2006-10-06 09:17:41 +000017#include "X86GenSubtarget.inc"
Daniel Dunbar3be03402009-08-02 22:11:08 +000018#include "llvm/GlobalValue.h"
Evan Cheng5b925c02009-01-03 04:04:46 +000019#include "llvm/Support/Debug.h"
Bill Wendling0ea8bf32009-08-03 00:11:34 +000020#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Host.h"
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +000022#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov45709ae2008-04-23 18:18:10 +000023#include "llvm/Target/TargetOptions.h"
David Goodwinc2e8a7e2009-11-10 00:48:55 +000024#include "llvm/ADT/SmallVector.h"
Nate Begemanfb5792f2005-07-12 01:41:54 +000025using namespace llvm;
26
Chris Lattnerbc583222009-04-25 18:27:23 +000027#if defined(_MSC_VER)
Bill Wendling0ea8bf32009-08-03 00:11:34 +000028#include <intrin.h>
Chris Lattnerbc583222009-04-25 18:27:23 +000029#endif
30
Dan Gohman29cbade2009-11-20 23:18:13 +000031/// ClassifyBlockAddressReference - Classify a blockaddress reference for the
32/// current subtarget according to how we should reference it in a non-pcrel
33/// context.
34unsigned char X86Subtarget::
35ClassifyBlockAddressReference() const {
36 if (isPICStyleGOT()) // 32-bit ELF targets.
37 return X86II::MO_GOTOFF;
38
39 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
40 return X86II::MO_PIC_BASE_OFFSET;
41
42 // Direct static reference to label.
43 return X86II::MO_NO_FLAG;
44}
45
Chris Lattnerd392bd92009-07-10 07:20:05 +000046/// ClassifyGlobalReference - Classify a global variable reference for the
47/// current subtarget according to how we should reference it in a non-pcrel
48/// context.
49unsigned char X86Subtarget::
50ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
51 // DLLImport only exists on windows, it is implemented as a load from a
52 // DLLIMPORT stub.
53 if (GV->hasDLLImportLinkage())
54 return X86II::MO_DLLIMPORT;
55
Chris Lattner6b601532010-06-14 20:11:56 +000056 // Determine whether this is a reference to a definition or a declaration.
57 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
58 // load from stub.
59 bool isDecl = GV->hasAvailableExternallyLinkage();
60 if (GV->isDeclaration() && !GV->isMaterializable())
61 isDecl = true;
Evan Chenga82b22c2009-07-16 22:53:10 +000062
Chris Lattnerd392bd92009-07-10 07:20:05 +000063 // X86-64 in PIC mode.
64 if (isPICStyleRIPRel()) {
65 // Large model never uses stubs.
66 if (TM.getCodeModel() == CodeModel::Large)
67 return X86II::MO_NO_FLAG;
68
Chris Lattnerc7822322009-07-10 21:01:59 +000069 if (isTargetDarwin()) {
70 // If symbol visibility is hidden, the extra load is not needed if
71 // target is x86-64 or the symbol is definitely defined in the current
72 // translation unit.
73 if (GV->hasDefaultVisibility() &&
Evan Chenga82b22c2009-07-16 22:53:10 +000074 (isDecl || GV->isWeakForLinker()))
Chris Lattnerc7822322009-07-10 21:01:59 +000075 return X86II::MO_GOTPCREL;
Anton Korobeynikov699647c2010-08-21 17:21:11 +000076 } else if (!isTargetWin64()) {
Chris Lattnerc7822322009-07-10 21:01:59 +000077 assert(isTargetELF() && "Unknown rip-relative target");
Chris Lattnerd392bd92009-07-10 07:20:05 +000078
Chris Lattnerc7822322009-07-10 21:01:59 +000079 // Extra load is needed for all externally visible.
80 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
81 return X86II::MO_GOTPCREL;
82 }
Chris Lattnerd392bd92009-07-10 07:20:05 +000083
84 return X86II::MO_NO_FLAG;
85 }
86
87 if (isPICStyleGOT()) { // 32-bit ELF targets.
88 // Extra load is needed for all externally visible.
89 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
90 return X86II::MO_GOTOFF;
91 return X86II::MO_GOT;
92 }
93
Chris Lattnere2c92082009-07-10 21:00:45 +000094 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
Chris Lattner84853a12009-07-10 20:53:38 +000095 // Determine whether we have a stub reference and/or whether the reference
96 // is relative to the PIC base or not.
Chris Lattnerd392bd92009-07-10 07:20:05 +000097
98 // If this is a strong reference to a definition, it is definitely not
99 // through a stub.
Evan Chenga82b22c2009-07-16 22:53:10 +0000100 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-07-10 20:53:38 +0000101 return X86II::MO_PIC_BASE_OFFSET;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000102
103 // Unless we have a symbol with hidden visibility, we have to go through a
104 // normal $non_lazy_ptr stub because this symbol might be resolved late.
Chris Lattner84853a12009-07-10 20:53:38 +0000105 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
106 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000107
108 // If symbol visibility is hidden, we have a stub for common symbol
109 // references and external declarations.
Evan Chenga82b22c2009-07-16 22:53:10 +0000110 if (isDecl || GV->hasCommonLinkage()) {
Chris Lattnerd392bd92009-07-10 07:20:05 +0000111 // Hidden $non_lazy_ptr reference.
Chris Lattner84853a12009-07-10 20:53:38 +0000112 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000113 }
114
115 // Otherwise, no stub.
Chris Lattner84853a12009-07-10 20:53:38 +0000116 return X86II::MO_PIC_BASE_OFFSET;
117 }
118
Chris Lattnere2c92082009-07-10 21:00:45 +0000119 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
Chris Lattner84853a12009-07-10 20:53:38 +0000120 // Determine whether we have a stub reference.
121
122 // If this is a strong reference to a definition, it is definitely not
123 // through a stub.
Evan Chenga82b22c2009-07-16 22:53:10 +0000124 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-07-10 20:53:38 +0000125 return X86II::MO_NO_FLAG;
126
127 // Unless we have a symbol with hidden visibility, we have to go through a
128 // normal $non_lazy_ptr stub because this symbol might be resolved late.
129 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
130 return X86II::MO_DARWIN_NONLAZY;
Evan Cheng63476a82009-09-03 07:04:02 +0000131
Chris Lattner84853a12009-07-10 20:53:38 +0000132 // Otherwise, no stub.
133 return X86II::MO_NO_FLAG;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000134 }
135
136 // Direct static reference to global.
137 return X86II::MO_NO_FLAG;
138}
139
Anton Korobeynikov7784ebc2006-11-30 22:42:55 +0000140
Bill Wendling6f287b22008-09-30 21:22:07 +0000141/// getBZeroEntry - This function returns the name of a function which has an
142/// interface like the non-standard bzero function, if such a function exists on
143/// the current subtarget and it is considered prefereable over memset with zero
144/// passed as the second argument. Otherwise it returns null.
Bill Wendling6e087382008-09-30 22:05:33 +0000145const char *X86Subtarget::getBZeroEntry() const {
Dan Gohman68d599d2008-04-01 20:38:36 +0000146 // Darwin 10 has a __bzero entry point for this purpose.
147 if (getDarwinVers() >= 10)
Bill Wendling6e087382008-09-30 22:05:33 +0000148 return "__bzero";
Dan Gohman68d599d2008-04-01 20:38:36 +0000149
150 return 0;
151}
152
Evan Chengd7f666a2009-05-20 04:53:57 +0000153/// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
154/// to immediate address.
155bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
156 if (Is64Bit)
157 return false;
158 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
159}
160
Dan Gohman8749b612008-12-16 03:35:01 +0000161/// getSpecialAddressLatency - For targets where it is beneficial to
162/// backschedule instructions that compute addresses, return a value
163/// indicating the number of scheduling cycles of backscheduling that
164/// should be attempted.
165unsigned X86Subtarget::getSpecialAddressLatency() const {
166 // For x86 out-of-order targets, back-schedule address computations so
167 // that loads and stores aren't blocked.
168 // This value was chosen arbitrarily.
169 return 200;
170}
171
Chris Lattner1e39a152006-01-28 06:05:41 +0000172/// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
173/// specified arguments. If we can't run cpuid on the host, return true.
Daniel Dunbar84811382009-09-03 05:47:34 +0000174static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
175 unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
Anton Korobeynikov6f9bb6f2009-08-28 16:06:41 +0000176#if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
Chris Lattnerbc583222009-04-25 18:27:23 +0000177 #if defined(__GNUC__)
178 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
179 asm ("movq\t%%rbx, %%rsi\n\t"
180 "cpuid\n\t"
181 "xchgq\t%%rbx, %%rsi\n\t"
182 : "=a" (*rEAX),
183 "=S" (*rEBX),
184 "=c" (*rECX),
185 "=d" (*rEDX)
186 : "a" (value));
187 return false;
188 #elif defined(_MSC_VER)
189 int registers[4];
190 __cpuid(registers, value);
191 *rEAX = registers[0];
192 *rEBX = registers[1];
193 *rECX = registers[2];
194 *rEDX = registers[3];
195 return false;
196 #endif
Evan Cheng25ab6902006-09-08 06:48:29 +0000197#elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Chris Lattnerbc583222009-04-25 18:27:23 +0000198 #if defined(__GNUC__)
199 asm ("movl\t%%ebx, %%esi\n\t"
200 "cpuid\n\t"
201 "xchgl\t%%ebx, %%esi\n\t"
202 : "=a" (*rEAX),
203 "=S" (*rEBX),
204 "=c" (*rECX),
205 "=d" (*rEDX)
206 : "a" (value));
207 return false;
208 #elif defined(_MSC_VER)
209 __asm {
210 mov eax,value
211 cpuid
212 mov esi,rEAX
213 mov dword ptr [esi],eax
214 mov esi,rEBX
215 mov dword ptr [esi],ebx
216 mov esi,rECX
217 mov dword ptr [esi],ecx
218 mov esi,rEDX
219 mov dword ptr [esi],edx
220 }
221 return false;
222 #endif
Evan Cheng559806f2006-01-27 08:10:46 +0000223#endif
Chris Lattner1e39a152006-01-28 06:05:41 +0000224 return true;
Evan Cheng559806f2006-01-27 08:10:46 +0000225}
226
Evan Chengccb69762009-01-02 05:35:45 +0000227static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
228 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
229 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
230 if (Family == 6 || Family == 0xf) {
231 if (Family == 0xf)
232 // Examine extended family ID if family ID is F.
233 Family += (EAX >> 20) & 0xff; // Bits 20 - 27
234 // Examine extended model ID if family ID is 6 or F.
235 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
236 }
237}
238
Evan Chenga26eb5e2006-10-06 09:17:41 +0000239void X86Subtarget::AutoDetectSubtargetFeatures() {
Evan Chengb3a7e212006-01-27 19:30:30 +0000240 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Jeff Cohena3496402006-01-28 18:47:32 +0000241 union {
Jeff Cohen216d2812006-01-28 19:48:34 +0000242 unsigned u[3];
243 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +0000244 } text;
Chris Lattner3b6f4972006-11-20 18:16:05 +0000245
Evan Chengd0da6ff2009-09-03 04:37:05 +0000246 if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
Evan Chengabc346c2006-10-06 08:21:07 +0000247 return;
Anton Korobeynikov3b5ee732007-03-23 23:46:48 +0000248
Evan Chengd0da6ff2009-09-03 04:37:05 +0000249 GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Chris Lattner3b6f4972006-11-20 18:16:05 +0000250
Chris Lattner70084162009-09-02 05:53:04 +0000251 if ((EDX >> 15) & 1) HasCMov = true;
252 if ((EDX >> 23) & 1) X86SSELevel = MMX;
253 if ((EDX >> 25) & 1) X86SSELevel = SSE1;
254 if ((EDX >> 26) & 1) X86SSELevel = SSE2;
Daniel Dunbar84811382009-09-03 05:47:34 +0000255 if (ECX & 0x1) X86SSELevel = SSE3;
Chris Lattner70084162009-09-02 05:53:04 +0000256 if ((ECX >> 9) & 1) X86SSELevel = SSSE3;
257 if ((ECX >> 19) & 1) X86SSELevel = SSE41;
258 if ((ECX >> 20) & 1) X86SSELevel = SSE42;
Anton Korobeynikov3b5ee732007-03-23 23:46:48 +0000259
Evan Chengccb69762009-01-02 05:35:45 +0000260 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
261 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
David Greene343dadb2009-06-26 22:46:54 +0000262
Bruno Cardoso Lopescdae7e82010-07-23 01:17:51 +0000263 HasCLMUL = IsIntel && ((ECX >> 1) & 0x1);
264 HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
265 HasAVX = ((ECX >> 28) & 0x1);
266 HasAES = IsIntel && ((ECX >> 25) & 0x1);
David Greene343dadb2009-06-26 22:46:54 +0000267
Evan Chengccb69762009-01-02 05:35:45 +0000268 if (IsIntel || IsAMD) {
269 // Determine if bit test memory instructions are slow.
270 unsigned Family = 0;
271 unsigned Model = 0;
272 DetectFamilyModel(EAX, Family, Model);
273 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
Evan Cheng48c58bb2010-04-01 05:58:17 +0000274 // If it's Nehalem, unaligned memory access is fast.
275 if (Family == 15 && Model == 26)
276 IsUAMemFast = true;
Evan Chengccb69762009-01-02 05:35:45 +0000277
Evan Chengd0da6ff2009-09-03 04:37:05 +0000278 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Jeff Cohenc3987092007-04-16 21:59:44 +0000279 HasX86_64 = (EDX >> 29) & 0x1;
Stefanus Du Toit8cf5ab12009-05-26 21:04:35 +0000280 HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
David Greene343dadb2009-06-26 22:46:54 +0000281 HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
Jeff Cohenc3987092007-04-16 21:59:44 +0000282 }
Evan Cheng559806f2006-01-27 08:10:46 +0000283}
Evan Cheng97c7fc32006-01-26 09:53:06 +0000284
Daniel Dunbar3be03402009-08-02 22:11:08 +0000285X86Subtarget::X86Subtarget(const std::string &TT, const std::string &FS,
286 bool is64Bit)
Chris Lattnerce914b82009-08-11 23:01:09 +0000287 : PICStyle(PICStyles::None)
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 , X86SSELevel(NoMMXSSE)
Evan Chengdc008582008-04-16 19:03:02 +0000289 , X863DNowLevel(NoThreeDNow)
Chris Lattner70084162009-09-02 05:53:04 +0000290 , HasCMov(false)
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 , HasX86_64(false)
David Greene343dadb2009-06-26 22:46:54 +0000292 , HasSSE4A(false)
293 , HasAVX(false)
Eric Christopher6d1cd1c2010-04-02 21:54:27 +0000294 , HasAES(false)
Bruno Cardoso Lopescdae7e82010-07-23 01:17:51 +0000295 , HasCLMUL(false)
David Greene343dadb2009-06-26 22:46:54 +0000296 , HasFMA3(false)
297 , HasFMA4(false)
Evan Chengccb69762009-01-02 05:35:45 +0000298 , IsBTMemSlow(false)
Evan Cheng48c58bb2010-04-01 05:58:17 +0000299 , IsUAMemFast(false)
David Greene95eb2ee2010-01-11 16:29:42 +0000300 , HasVectorUAMem(false)
Evan Cheng25ab6902006-09-08 06:48:29 +0000301 , stackAlignment(8)
302 // FIXME: this is a known good value for Yonah. How about others?
Rafael Espindolafc05f402007-10-31 11:52:06 +0000303 , MaxInlineSizeThreshold(128)
Eric Christopher62f35a22010-07-05 19:26:33 +0000304 , TargetTriple(TT)
305 , Is64Bit(is64Bit) {
Anton Korobeynikov0eebf652009-06-08 22:53:56 +0000306
307 // default to hard float ABI
308 if (FloatABIType == FloatABI::Default)
309 FloatABIType = FloatABI::Hard;
Mon P Wang63307c32008-05-05 19:05:59 +0000310
Evan Cheng97c7fc32006-01-26 09:53:06 +0000311 // Determine default and user specified characteristics
Evan Chenga26eb5e2006-10-06 09:17:41 +0000312 if (!FS.empty()) {
313 // If feature string is not empty, parse features string.
Daniel Dunbar067d0242009-11-14 10:09:12 +0000314 std::string CPU = sys::getHostCPUName();
Evan Chenga26eb5e2006-10-06 09:17:41 +0000315 ParseSubtargetFeatures(FS, CPU);
Torok Edwinb68a88b2009-02-02 21:57:34 +0000316 // All X86-64 CPUs also have SSE2, however user might request no SSE via
317 // -mattr, so don't force SSELevel here.
Chris Lattner3b6f4972006-11-20 18:16:05 +0000318 } else {
319 // Otherwise, use CPUID to auto-detect feature set.
320 AutoDetectSubtargetFeatures();
Dan Gohmanf75e5b42009-02-03 00:04:43 +0000321 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
322 if (Is64Bit && X86SSELevel < SSE2)
323 X86SSELevel = SSE2;
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Dan Gohmanf75e5b42009-02-03 00:04:43 +0000325
Dan Gohman605679f2009-02-03 18:53:21 +0000326 // If requesting codegen for X86-64, make sure that 64-bit features
327 // are enabled.
Chris Lattner1c436d02010-03-14 22:39:35 +0000328 if (Is64Bit) {
Dan Gohman605679f2009-02-03 18:53:21 +0000329 HasX86_64 = true;
330
Chris Lattner1c436d02010-03-14 22:39:35 +0000331 // All 64-bit cpus have cmov support.
332 HasCMov = true;
333 }
334
David Greenebd7b8452010-01-05 01:29:13 +0000335 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000336 << ", 3DNowLevel " << X863DNowLevel
337 << ", 64bit " << HasX86_64 << "\n");
Dan Gohmanf75e5b42009-02-03 00:04:43 +0000338 assert((!Is64Bit || HasX86_64) &&
339 "64-bit code requested on a subtarget that doesn't support it!");
Evan Cheng25ab6902006-09-08 06:48:29 +0000340
Anton Korobeynikov890fe882008-04-23 18:16:16 +0000341 // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
342 // bit targets.
Eric Christopher62f35a22010-07-05 19:26:33 +0000343 if (isTargetDarwin() || Is64Bit)
Nate Begemanfb5792f2005-07-12 01:41:54 +0000344 stackAlignment = 16;
Anton Korobeynikov78c80fd2008-04-12 22:12:22 +0000345
346 if (StackAlignment)
347 stackAlignment = StackAlignment;
Nate Begemanfb5792f2005-07-12 01:41:54 +0000348}
Dan Gohman4d3d6e12010-05-27 18:43:40 +0000349
350/// IsCalleePop - Determines whether the callee is required to pop its
351/// own arguments. Callee pop is necessary to support tail calls.
352bool X86Subtarget::IsCalleePop(bool IsVarArg,
353 CallingConv::ID CallingConv) const {
354 if (IsVarArg)
355 return false;
356
357 switch (CallingConv) {
358 default:
359 return false;
360 case CallingConv::X86_StdCall:
361 return !is64Bit();
362 case CallingConv::X86_FastCall:
363 return !is64Bit();
364 case CallingConv::X86_ThisCall:
365 return !is64Bit();
366 case CallingConv::Fast:
367 return GuaranteedTailCallOpt;
368 case CallingConv::GHC:
369 return GuaranteedTailCallOpt;
370 }
371}