blob: 5e6c659e539348a3d58326e73f4529519a4ec9fc [file] [log] [blame]
Bill Wendlingd6434862010-12-04 23:57:24 +00001//===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
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//
Evan Cheng5b1b44892011-07-01 21:01:15 +000010// This file implements the X86 specific subclass of TargetSubtargetInfo.
Nate Begemanfb5792f2005-07-12 01:41:54 +000011//
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"
Daniel Dunbar3be03402009-08-02 22:11:08 +000017#include "llvm/GlobalValue.h"
Evan Cheng5b925c02009-01-03 04:04:46 +000018#include "llvm/Support/Debug.h"
Bill Wendling0ea8bf32009-08-03 00:11:34 +000019#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000020#include "llvm/Support/Host.h"
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +000021#include "llvm/Target/TargetMachine.h"
David Goodwinc2e8a7e2009-11-10 00:48:55 +000022#include "llvm/ADT/SmallVector.h"
Evan Cheng94214702011-07-01 20:45:01 +000023
Evan Cheng94214702011-07-01 20:45:01 +000024#define GET_SUBTARGETINFO_TARGET_DESC
Evan Chengebdeeab2011-07-08 01:53:10 +000025#define GET_SUBTARGETINFO_CTOR
Evan Cheng385e9302011-07-01 22:36:09 +000026#include "X86GenSubtargetInfo.inc"
Evan Cheng94214702011-07-01 20:45:01 +000027
Nate Begemanfb5792f2005-07-12 01:41:54 +000028using namespace llvm;
29
Chris Lattnerbc583222009-04-25 18:27:23 +000030#if defined(_MSC_VER)
Bill Wendling0ea8bf32009-08-03 00:11:34 +000031#include <intrin.h>
Chris Lattnerbc583222009-04-25 18:27:23 +000032#endif
33
Dan Gohman29cbade2009-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 Lattnerd392bd92009-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 Lattner6b601532010-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 Chenga82b22c2009-07-16 22:53:10 +000065
Chris Lattnerd392bd92009-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 Lattnerc7822322009-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 Chenga82b22c2009-07-16 22:53:10 +000077 (isDecl || GV->isWeakForLinker()))
Chris Lattnerc7822322009-07-10 21:01:59 +000078 return X86II::MO_GOTPCREL;
Anton Korobeynikov699647c2010-08-21 17:21:11 +000079 } else if (!isTargetWin64()) {
Chris Lattnerc7822322009-07-10 21:01:59 +000080 assert(isTargetELF() && "Unknown rip-relative target");
Chris Lattnerd392bd92009-07-10 07:20:05 +000081
Chris Lattnerc7822322009-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 Lattnerd392bd92009-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 Lattnere2c92082009-07-10 21:00:45 +000097 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
Chris Lattner84853a12009-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 Lattnerd392bd92009-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 Chenga82b22c2009-07-16 22:53:10 +0000103 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-07-10 20:53:38 +0000104 return X86II::MO_PIC_BASE_OFFSET;
Chris Lattnerd392bd92009-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 Lattner84853a12009-07-10 20:53:38 +0000108 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
109 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
Chris Lattnerd392bd92009-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 Chenga82b22c2009-07-16 22:53:10 +0000113 if (isDecl || GV->hasCommonLinkage()) {
Chris Lattnerd392bd92009-07-10 07:20:05 +0000114 // Hidden $non_lazy_ptr reference.
Chris Lattner84853a12009-07-10 20:53:38 +0000115 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000116 }
117
118 // Otherwise, no stub.
Chris Lattner84853a12009-07-10 20:53:38 +0000119 return X86II::MO_PIC_BASE_OFFSET;
120 }
121
Chris Lattnere2c92082009-07-10 21:00:45 +0000122 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
Chris Lattner84853a12009-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 Chenga82b22c2009-07-16 22:53:10 +0000127 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-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 Cheng63476a82009-09-03 07:04:02 +0000134
Chris Lattner84853a12009-07-10 20:53:38 +0000135 // Otherwise, no stub.
136 return X86II::MO_NO_FLAG;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000137 }
138
139 // Direct static reference to global.
140 return X86II::MO_NO_FLAG;
141}
142
Anton Korobeynikov7784ebc2006-11-30 22:42:55 +0000143
Bill Wendling6f287b22008-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 Wendling6e087382008-09-30 22:05:33 +0000148const char *X86Subtarget::getBZeroEntry() const {
Dan Gohman68d599d2008-04-01 20:38:36 +0000149 // Darwin 10 has a __bzero entry point for this purpose.
Daniel Dunbar558692f2011-04-20 00:14:25 +0000150 if (getTargetTriple().isMacOSX() &&
151 !getTargetTriple().isMacOSXVersionLT(10, 6))
Bill Wendling6e087382008-09-30 22:05:33 +0000152 return "__bzero";
Dan Gohman68d599d2008-04-01 20:38:36 +0000153
154 return 0;
155}
156
Evan Chengd7f666a2009-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 Cheng18fb1d32011-07-07 21:06:52 +0000160 if (In64BitMode)
Evan Chengd7f666a2009-05-20 04:53:57 +0000161 return false;
162 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
163}
164
Dan Gohman8749b612008-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 Chenga26eb5e2006-10-06 09:17:41 +0000176void X86Subtarget::AutoDetectSubtargetFeatures() {
Evan Chengb3a7e212006-01-27 19:30:30 +0000177 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Jeff Cohena3496402006-01-28 18:47:32 +0000178 union {
Jeff Cohen216d2812006-01-28 19:48:34 +0000179 unsigned u[3];
180 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +0000181 } text;
Chris Lattner3b6f4972006-11-20 18:16:05 +0000182
Evan Cheng18fb1d32011-07-07 21:06:52 +0000183 if (X86_MC::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
Evan Chengabc346c2006-10-06 08:21:07 +0000184 return;
Anton Korobeynikov3b5ee732007-03-23 23:46:48 +0000185
Evan Cheng18fb1d32011-07-07 21:06:52 +0000186 X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Chris Lattner3b6f4972006-11-20 18:16:05 +0000187
Evan Cheng59ee62d2011-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 Chengde7f9202010-12-13 04:23:53 +0000196 // FIXME: AVX codegen support is not ready.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000197 //if ((ECX >> 28) & 1) { HasAVX = true; } ToggleFeature(X86::FeatureAVX);
Anton Korobeynikov3b5ee732007-03-23 23:46:48 +0000198
Evan Chengccb69762009-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 Greene343dadb2009-06-26 22:46:54 +0000201
Evan Cheng59ee62d2011-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 Greene343dadb2009-06-26 22:46:54 +0000206
Evan Chengccb69762009-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 Cheng18fb1d32011-07-07 21:06:52 +0000211 X86_MC::DetectFamilyModel(EAX, Family, Model);
Evan Cheng59ee62d2011-07-11 03:57:24 +0000212 if (IsAMD || (Family == 6 && Model >= 13)) {
213 IsBTMemSlow = true;
214 ToggleFeature(X86::FeatureSlowBTMem);
215 }
Evan Cheng48c58bb2010-04-01 05:58:17 +0000216 // If it's Nehalem, unaligned memory access is fast.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000217 if (Family == 15 && Model == 26) {
Evan Cheng48c58bb2010-04-01 05:58:17 +0000218 IsUAMemFast = true;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000219 ToggleFeature(X86::FeatureFastUAMem);
220 }
Evan Chengccb69762009-01-02 05:35:45 +0000221
Evan Cheng18fb1d32011-07-07 21:06:52 +0000222 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Evan Cheng59ee62d2011-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 Cohenc3987092007-04-16 21:59:44 +0000235 }
Evan Cheng559806f2006-01-27 08:10:46 +0000236}
Evan Cheng97c7fc32006-01-26 09:53:06 +0000237
Evan Cheng276365d2011-06-30 01:53:36 +0000238X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
239 const std::string &FS,
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000240 unsigned StackAlignOverride, bool is64Bit)
Evan Cheng0ddff1b2011-07-07 07:07:08 +0000241 : X86GenSubtargetInfo(TT, CPU, FS)
Evan Cheng94214702011-07-01 20:45:01 +0000242 , PICStyle(PICStyles::None)
Evan Cheng25ab6902006-09-08 06:48:29 +0000243 , X86SSELevel(NoMMXSSE)
Evan Chengdc008582008-04-16 19:03:02 +0000244 , X863DNowLevel(NoThreeDNow)
Chris Lattner70084162009-09-02 05:53:04 +0000245 , HasCMov(false)
Evan Cheng25ab6902006-09-08 06:48:29 +0000246 , HasX86_64(false)
Bill Wendlingd6434862010-12-04 23:57:24 +0000247 , HasPOPCNT(false)
David Greene343dadb2009-06-26 22:46:54 +0000248 , HasSSE4A(false)
249 , HasAVX(false)
Eric Christopher6d1cd1c2010-04-02 21:54:27 +0000250 , HasAES(false)
Bruno Cardoso Lopescdae7e82010-07-23 01:17:51 +0000251 , HasCLMUL(false)
David Greene343dadb2009-06-26 22:46:54 +0000252 , HasFMA3(false)
253 , HasFMA4(false)
Evan Chengccb69762009-01-02 05:35:45 +0000254 , IsBTMemSlow(false)
Evan Cheng48c58bb2010-04-01 05:58:17 +0000255 , IsUAMemFast(false)
David Greene95eb2ee2010-01-11 16:29:42 +0000256 , HasVectorUAMem(false)
Evan Cheng25ab6902006-09-08 06:48:29 +0000257 , stackAlignment(8)
258 // FIXME: this is a known good value for Yonah. How about others?
Rafael Espindolafc05f402007-10-31 11:52:06 +0000259 , MaxInlineSizeThreshold(128)
Eric Christopher62f35a22010-07-05 19:26:33 +0000260 , TargetTriple(TT)
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000261 , In64BitMode(is64Bit) {
Evan Cheng97c7fc32006-01-26 09:53:06 +0000262 // Determine default and user specified characteristics
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000263 if (!FS.empty() || !CPU.empty()) {
Evan Chengcc0ddc72011-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 Friedman439d05d2011-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 Cheng4d1a8dd2011-07-08 22:30:25 +0000282
Eli Friedman439d05d2011-07-08 23:43:01 +0000283 // If feature string is not empty, parse features string.
284 ParseSubtargetFeatures(CPUName, FullFS);
Chris Lattner3b6f4972006-11-20 18:16:05 +0000285 } else {
286 // Otherwise, use CPUID to auto-detect feature set.
287 AutoDetectSubtargetFeatures();
Evan Cheng18fb1d32011-07-07 21:06:52 +0000288
Eli Friedman6dfef662011-07-08 23:07:42 +0000289 // Make sure 64-bit features are available in 64-bit mode.
290 if (In64BitMode) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000291 HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
292 HasCMov = true; ToggleFeature(X86::FeatureCMOV);
Eli Friedman6dfef662011-07-08 23:07:42 +0000293
Evan Cheng59ee62d2011-07-11 03:57:24 +0000294 if (!HasAVX && X86SSELevel < SSE2) {
Eli Friedman6dfef662011-07-08 23:07:42 +0000295 X86SSELevel = SSE2;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000296 ToggleFeature(X86::FeatureSSE1);
297 ToggleFeature(X86::FeatureSSE2);
298 }
Eli Friedman6dfef662011-07-08 23:07:42 +0000299 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000300 }
Evan Cheng59ee62d2011-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 Lattner1c436d02010-03-14 22:39:35 +0000309
David Greenebd7b8452010-01-05 01:29:13 +0000310 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000311 << ", 3DNowLevel " << X863DNowLevel
312 << ", 64bit " << HasX86_64 << "\n");
Evan Cheng18fb1d32011-07-07 21:06:52 +0000313 assert((!In64BitMode || HasX86_64) &&
Dan Gohmanf75e5b42009-02-03 00:04:43 +0000314 "64-bit code requested on a subtarget that doesn't support it!");
Evan Cheng25ab6902006-09-08 06:48:29 +0000315
Roman Divacky4c3ab582011-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 Chengef41ff62011-06-23 17:54:54 +0000318 if (StackAlignOverride)
319 stackAlignment = StackAlignOverride;
320 else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
Evan Cheng18fb1d32011-07-07 21:06:52 +0000321 isTargetSolaris() || In64BitMode)
Nate Begemanfb5792f2005-07-12 01:41:54 +0000322 stackAlignment = 16;
Dan Gohman4d3d6e12010-05-27 18:43:40 +0000323}