blob: 5e6c659e539348a3d58326e73f4529519a4ec9fc [file] [log] [blame]
Bill Wendling2bce78e2010-12-04 23:57:24 +00001//===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
Nate Begemanf26625e2005-07-12 01:41:54 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Begemanf26625e2005-07-12 01:41:54 +00007//
8//===----------------------------------------------------------------------===//
9//
Evan Cheng0d639a22011-07-01 21:01:15 +000010// This file implements the X86 specific subclass of TargetSubtargetInfo.
Nate Begemanf26625e2005-07-12 01:41:54 +000011//
12//===----------------------------------------------------------------------===//
13
Evan Cheng9a3ec1b2009-01-03 04:04:46 +000014#define DEBUG_TYPE "subtarget"
Nate Begemanf26625e2005-07-12 01:41:54 +000015#include "X86Subtarget.h"
Chris Lattnerdc842c02009-07-10 07:20:05 +000016#include "X86InstrInfo.h"
Daniel Dunbar31b44e82009-08-02 22:11:08 +000017#include "llvm/GlobalValue.h"
Evan Cheng9a3ec1b2009-01-03 04:04:46 +000018#include "llvm/Support/Debug.h"
Bill Wendling6eecd562009-08-03 00:11:34 +000019#include "llvm/Support/raw_ostream.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000020#include "llvm/Support/Host.h"
Anton Korobeynikov430e68a12006-12-22 22:29:05 +000021#include "llvm/Target/TargetMachine.h"
David Goodwin0d412c22009-11-10 00:48:55 +000022#include "llvm/ADT/SmallVector.h"
Evan Cheng54b68e32011-07-01 20:45:01 +000023
Evan Cheng54b68e32011-07-01 20:45:01 +000024#define GET_SUBTARGETINFO_TARGET_DESC
Evan Cheng4d1ca962011-07-08 01:53:10 +000025#define GET_SUBTARGETINFO_CTOR
Evan Chengc9c090d2011-07-01 22:36:09 +000026#include "X86GenSubtargetInfo.inc"
Evan Cheng54b68e32011-07-01 20:45:01 +000027
Nate Begemanf26625e2005-07-12 01:41:54 +000028using namespace llvm;
29
Chris Lattner3ad60b12009-04-25 18:27:23 +000030#if defined(_MSC_VER)
Bill Wendling6eecd562009-08-03 00:11:34 +000031#include <intrin.h>
Chris Lattner3ad60b12009-04-25 18:27:23 +000032#endif
33
Dan Gohman7a6611792009-11-20 23:18:13 +000034/// ClassifyBlockAddressReference - Classify a blockaddress reference for the
35/// current subtarget according to how we should reference it in a non-pcrel
36/// context.
37unsigned char X86Subtarget::
38ClassifyBlockAddressReference() const {
39 if (isPICStyleGOT()) // 32-bit ELF targets.
40 return X86II::MO_GOTOFF;
41
42 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
43 return X86II::MO_PIC_BASE_OFFSET;
44
45 // Direct static reference to label.
46 return X86II::MO_NO_FLAG;
47}
48
Chris Lattnerdc842c02009-07-10 07:20:05 +000049/// ClassifyGlobalReference - Classify a global variable reference for the
50/// current subtarget according to how we should reference it in a non-pcrel
51/// context.
52unsigned char X86Subtarget::
53ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
54 // DLLImport only exists on windows, it is implemented as a load from a
55 // DLLIMPORT stub.
56 if (GV->hasDLLImportLinkage())
57 return X86II::MO_DLLIMPORT;
58
Chris Lattnerfaa7bdc2010-06-14 20:11:56 +000059 // Determine whether this is a reference to a definition or a declaration.
60 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
61 // load from stub.
62 bool isDecl = GV->hasAvailableExternallyLinkage();
63 if (GV->isDeclaration() && !GV->isMaterializable())
64 isDecl = true;
Evan Cheng02a76522009-07-16 22:53:10 +000065
Chris Lattnerdc842c02009-07-10 07:20:05 +000066 // X86-64 in PIC mode.
67 if (isPICStyleRIPRel()) {
68 // Large model never uses stubs.
69 if (TM.getCodeModel() == CodeModel::Large)
70 return X86II::MO_NO_FLAG;
71
Chris Lattner7dce9912009-07-10 21:01:59 +000072 if (isTargetDarwin()) {
73 // If symbol visibility is hidden, the extra load is not needed if
74 // target is x86-64 or the symbol is definitely defined in the current
75 // translation unit.
76 if (GV->hasDefaultVisibility() &&
Evan Cheng02a76522009-07-16 22:53:10 +000077 (isDecl || GV->isWeakForLinker()))
Chris Lattner7dce9912009-07-10 21:01:59 +000078 return X86II::MO_GOTPCREL;
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000079 } else if (!isTargetWin64()) {
Chris Lattner7dce9912009-07-10 21:01:59 +000080 assert(isTargetELF() && "Unknown rip-relative target");
Chris Lattnerdc842c02009-07-10 07:20:05 +000081
Chris Lattner7dce9912009-07-10 21:01:59 +000082 // Extra load is needed for all externally visible.
83 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
84 return X86II::MO_GOTPCREL;
85 }
Chris Lattnerdc842c02009-07-10 07:20:05 +000086
87 return X86II::MO_NO_FLAG;
88 }
89
90 if (isPICStyleGOT()) { // 32-bit ELF targets.
91 // Extra load is needed for all externally visible.
92 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
93 return X86II::MO_GOTOFF;
94 return X86II::MO_GOT;
95 }
96
Chris Lattner21c29402009-07-10 21:00:45 +000097 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
Chris Lattnerbd3e5602009-07-10 20:53:38 +000098 // Determine whether we have a stub reference and/or whether the reference
99 // is relative to the PIC base or not.
Chris Lattnerdc842c02009-07-10 07:20:05 +0000100
101 // If this is a strong reference to a definition, it is definitely not
102 // through a stub.
Evan Cheng02a76522009-07-16 22:53:10 +0000103 if (!isDecl && !GV->isWeakForLinker())
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000104 return X86II::MO_PIC_BASE_OFFSET;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000105
106 // Unless we have a symbol with hidden visibility, we have to go through a
107 // normal $non_lazy_ptr stub because this symbol might be resolved late.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000108 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
109 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000110
111 // If symbol visibility is hidden, we have a stub for common symbol
112 // references and external declarations.
Evan Cheng02a76522009-07-16 22:53:10 +0000113 if (isDecl || GV->hasCommonLinkage()) {
Chris Lattnerdc842c02009-07-10 07:20:05 +0000114 // Hidden $non_lazy_ptr reference.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000115 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000116 }
117
118 // Otherwise, no stub.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000119 return X86II::MO_PIC_BASE_OFFSET;
120 }
121
Chris Lattner21c29402009-07-10 21:00:45 +0000122 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000123 // Determine whether we have a stub reference.
124
125 // If this is a strong reference to a definition, it is definitely not
126 // through a stub.
Evan Cheng02a76522009-07-16 22:53:10 +0000127 if (!isDecl && !GV->isWeakForLinker())
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000128 return X86II::MO_NO_FLAG;
129
130 // Unless we have a symbol with hidden visibility, we have to go through a
131 // normal $non_lazy_ptr stub because this symbol might be resolved late.
132 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
133 return X86II::MO_DARWIN_NONLAZY;
Evan Cheng1b389522009-09-03 07:04:02 +0000134
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000135 // Otherwise, no stub.
136 return X86II::MO_NO_FLAG;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000137 }
138
139 // Direct static reference to global.
140 return X86II::MO_NO_FLAG;
141}
142
Anton Korobeynikov6dbdfe22006-11-30 22:42:55 +0000143
Bill Wendlingbd092622008-09-30 21:22:07 +0000144/// getBZeroEntry - This function returns the name of a function which has an
145/// interface like the non-standard bzero function, if such a function exists on
146/// the current subtarget and it is considered prefereable over memset with zero
147/// passed as the second argument. Otherwise it returns null.
Bill Wendling17825842008-09-30 22:05:33 +0000148const char *X86Subtarget::getBZeroEntry() const {
Dan Gohman980d7202008-04-01 20:38:36 +0000149 // Darwin 10 has a __bzero entry point for this purpose.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000150 if (getTargetTriple().isMacOSX() &&
151 !getTargetTriple().isMacOSXVersionLT(10, 6))
Bill Wendling17825842008-09-30 22:05:33 +0000152 return "__bzero";
Dan Gohman980d7202008-04-01 20:38:36 +0000153
154 return 0;
155}
156
Evan Cheng96098332009-05-20 04:53:57 +0000157/// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
158/// to immediate address.
159bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000160 if (In64BitMode)
Evan Cheng96098332009-05-20 04:53:57 +0000161 return false;
162 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
163}
164
Dan Gohmanb9a01212008-12-16 03:35:01 +0000165/// getSpecialAddressLatency - For targets where it is beneficial to
166/// backschedule instructions that compute addresses, return a value
167/// indicating the number of scheduling cycles of backscheduling that
168/// should be attempted.
169unsigned X86Subtarget::getSpecialAddressLatency() const {
170 // For x86 out-of-order targets, back-schedule address computations so
171 // that loads and stores aren't blocked.
172 // This value was chosen arbitrarily.
173 return 200;
174}
175
Evan Chengff1beda2006-10-06 09:17:41 +0000176void X86Subtarget::AutoDetectSubtargetFeatures() {
Evan Chengafab7aa2006-01-27 19:30:30 +0000177 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Jeff Cohen71287082006-01-28 18:47:32 +0000178 union {
Jeff Cohen58ca0be92006-01-28 19:48:34 +0000179 unsigned u[3];
180 char c[12];
Jeff Cohen71287082006-01-28 18:47:32 +0000181 } text;
Chris Lattner3e962112006-11-20 18:16:05 +0000182
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000183 if (X86_MC::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
Evan Cheng9274f722006-10-06 08:21:07 +0000184 return;
Anton Korobeynikov8aae2d72007-03-23 23:46:48 +0000185
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000186 X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Chris Lattner3e962112006-11-20 18:16:05 +0000187
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000188 if ((EDX >> 15) & 1) HasCMov = true; ToggleFeature(X86::FeatureCMOV);
189 if ((EDX >> 23) & 1) X86SSELevel = MMX; ToggleFeature(X86::FeatureMMX);
190 if ((EDX >> 25) & 1) X86SSELevel = SSE1; ToggleFeature(X86::FeatureSSE1);
191 if ((EDX >> 26) & 1) X86SSELevel = SSE2; ToggleFeature(X86::FeatureSSE2);
192 if (ECX & 0x1) X86SSELevel = SSE3; ToggleFeature(X86::FeatureSSE3);
193 if ((ECX >> 9) & 1) X86SSELevel = SSSE3; ToggleFeature(X86::FeatureSSSE3);
194 if ((ECX >> 19) & 1) X86SSELevel = SSE41; ToggleFeature(X86::FeatureSSE41);
195 if ((ECX >> 20) & 1) X86SSELevel = SSE42; ToggleFeature(X86::FeatureSSE42);
Evan Chengf8b4c002010-12-13 04:23:53 +0000196 // FIXME: AVX codegen support is not ready.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000197 //if ((ECX >> 28) & 1) { HasAVX = true; } ToggleFeature(X86::FeatureAVX);
Anton Korobeynikov8aae2d72007-03-23 23:46:48 +0000198
Evan Cheng4c91aa32009-01-02 05:35:45 +0000199 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
200 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
David Greene8f6f72c2009-06-26 22:46:54 +0000201
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000202 HasCLMUL = IsIntel && ((ECX >> 1) & 0x1); ToggleFeature(X86::FeatureCLMUL);
203 HasFMA3 = IsIntel && ((ECX >> 12) & 0x1); ToggleFeature(X86::FeatureFMA3);
204 HasPOPCNT = IsIntel && ((ECX >> 23) & 0x1); ToggleFeature(X86::FeaturePOPCNT);
205 HasAES = IsIntel && ((ECX >> 25) & 0x1); ToggleFeature(X86::FeatureAES);
David Greene8f6f72c2009-06-26 22:46:54 +0000206
Evan Cheng4c91aa32009-01-02 05:35:45 +0000207 if (IsIntel || IsAMD) {
208 // Determine if bit test memory instructions are slow.
209 unsigned Family = 0;
210 unsigned Model = 0;
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000211 X86_MC::DetectFamilyModel(EAX, Family, Model);
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000212 if (IsAMD || (Family == 6 && Model >= 13)) {
213 IsBTMemSlow = true;
214 ToggleFeature(X86::FeatureSlowBTMem);
215 }
Evan Cheng738b0f92010-04-01 05:58:17 +0000216 // If it's Nehalem, unaligned memory access is fast.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000217 if (Family == 15 && Model == 26) {
Evan Cheng738b0f92010-04-01 05:58:17 +0000218 IsUAMemFast = true;
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000219 ToggleFeature(X86::FeatureFastUAMem);
220 }
Evan Cheng4c91aa32009-01-02 05:35:45 +0000221
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000222 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000223 if ((EDX >> 29) & 0x1) {
224 HasX86_64 = true;
225 ToggleFeature(X86::Feature64Bit);
226 }
227 if (IsAMD && ((ECX >> 6) & 0x1)) {
228 HasSSE4A = true;
229 ToggleFeature(X86::FeatureSSE4A);
230 }
231 if (IsAMD && ((ECX >> 16) & 0x1)) {
232 HasFMA4 = true;
233 ToggleFeature(X86::FeatureFMA4);
234 }
Jeff Cohen6f3a5482007-04-16 21:59:44 +0000235 }
Evan Chengcde9e302006-01-27 08:10:46 +0000236}
Evan Cheng54c13da2006-01-26 09:53:06 +0000237
Evan Chengfe6e4052011-06-30 01:53:36 +0000238X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
239 const std::string &FS,
Evan Cheng60fc0fc2011-07-08 22:30:25 +0000240 unsigned StackAlignOverride, bool is64Bit)
Evan Cheng1a72add62011-07-07 07:07:08 +0000241 : X86GenSubtargetInfo(TT, CPU, FS)
Evan Cheng54b68e32011-07-01 20:45:01 +0000242 , PICStyle(PICStyles::None)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000243 , X86SSELevel(NoMMXSSE)
Evan Chenga15cee12008-04-16 19:03:02 +0000244 , X863DNowLevel(NoThreeDNow)
Chris Lattnercc8c5812009-09-02 05:53:04 +0000245 , HasCMov(false)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000246 , HasX86_64(false)
Bill Wendling2bce78e2010-12-04 23:57:24 +0000247 , HasPOPCNT(false)
David Greene8f6f72c2009-06-26 22:46:54 +0000248 , HasSSE4A(false)
249 , HasAVX(false)
Eric Christopher2ef63182010-04-02 21:54:27 +0000250 , HasAES(false)
Bruno Cardoso Lopes09dc24b2010-07-23 01:17:51 +0000251 , HasCLMUL(false)
David Greene8f6f72c2009-06-26 22:46:54 +0000252 , HasFMA3(false)
253 , HasFMA4(false)
Evan Cheng4c91aa32009-01-02 05:35:45 +0000254 , IsBTMemSlow(false)
Evan Cheng738b0f92010-04-01 05:58:17 +0000255 , IsUAMemFast(false)
David Greene206351a2010-01-11 16:29:42 +0000256 , HasVectorUAMem(false)
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000257 , stackAlignment(8)
258 // FIXME: this is a known good value for Yonah. How about others?
Rafael Espindola063f1772007-10-31 11:52:06 +0000259 , MaxInlineSizeThreshold(128)
Eric Christopherd4298462010-07-05 19:26:33 +0000260 , TargetTriple(TT)
Evan Cheng60fc0fc2011-07-08 22:30:25 +0000261 , In64BitMode(is64Bit) {
Evan Cheng54c13da2006-01-26 09:53:06 +0000262 // Determine default and user specified characteristics
Evan Cheng60fc0fc2011-07-08 22:30:25 +0000263 if (!FS.empty() || !CPU.empty()) {
Evan Cheng964cb5f2011-07-08 21:14:14 +0000264 std::string CPUName = CPU;
265 if (CPUName.empty()) {
266#if defined (__x86_64__) || defined(__i386__)
267 CPUName = sys::getHostCPUName();
268#else
269 CPUName = "generic";
270#endif
271 }
272
Eli Friedmanfe2088b2011-07-08 23:43:01 +0000273 // Make sure 64-bit features are available in 64-bit mode. (But make sure
274 // SSE2 can be turned off explicitly.)
275 std::string FullFS = FS;
276 if (In64BitMode) {
277 if (!FullFS.empty())
278 FullFS = "+64bit,+sse2," + FullFS;
279 else
280 FullFS = "+64bit,+sse2";
281 }
Evan Cheng60fc0fc2011-07-08 22:30:25 +0000282
Eli Friedmanfe2088b2011-07-08 23:43:01 +0000283 // If feature string is not empty, parse features string.
284 ParseSubtargetFeatures(CPUName, FullFS);
Chris Lattner3e962112006-11-20 18:16:05 +0000285 } else {
286 // Otherwise, use CPUID to auto-detect feature set.
287 AutoDetectSubtargetFeatures();
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000288
Eli Friedman52868332011-07-08 23:07:42 +0000289 // Make sure 64-bit features are available in 64-bit mode.
290 if (In64BitMode) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000291 HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
292 HasCMov = true; ToggleFeature(X86::FeatureCMOV);
Eli Friedman52868332011-07-08 23:07:42 +0000293
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000294 if (!HasAVX && X86SSELevel < SSE2) {
Eli Friedman52868332011-07-08 23:07:42 +0000295 X86SSELevel = SSE2;
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000296 ToggleFeature(X86::FeatureSSE1);
297 ToggleFeature(X86::FeatureSSE2);
298 }
Eli Friedman52868332011-07-08 23:07:42 +0000299 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000300 }
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000301
302 // It's important to keep the MCSubtargetInfo feature bits in sync with
303 // target data structure which is shared with MC code emitter, etc.
304 if (In64BitMode)
305 ToggleFeature(X86::Mode64Bit);
306
307 if (HasAVX)
308 X86SSELevel = NoMMXSSE;
Chris Lattner402d6442010-03-14 22:39:35 +0000309
David Greene00411812010-01-05 01:29:13 +0000310 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
Bill Wendling6eecd562009-08-03 00:11:34 +0000311 << ", 3DNowLevel " << X863DNowLevel
312 << ", 64bit " << HasX86_64 << "\n");
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000313 assert((!In64BitMode || HasX86_64) &&
Dan Gohman74037512009-02-03 00:04:43 +0000314 "64-bit code requested on a subtarget that doesn't support it!");
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000315
Roman Divackye8a93fe82011-02-22 17:30:05 +0000316 // Stack alignment is 16 bytes on Darwin, FreeBSD, Linux and Solaris (both
317 // 32 and 64 bit) and for all 64-bit targets.
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000318 if (StackAlignOverride)
319 stackAlignment = StackAlignOverride;
320 else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000321 isTargetSolaris() || In64BitMode)
Nate Begemanf26625e2005-07-12 01:41:54 +0000322 stackAlignment = 16;
Dan Gohmandc53f1c2010-05-27 18:43:40 +0000323}