blob: b2eff5e5591bd20250dcc53df624429c70be842a [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"
Bill Wendling789cb5d2013-02-15 22:31:27 +000017#include "llvm/IR/Attributes.h"
18#include "llvm/IR/Function.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/GlobalValue.h"
Evan Cheng5b925c02009-01-03 04:04:46 +000020#include "llvm/Support/Debug.h"
Rafael Espindolaca592212011-09-07 16:10:57 +000021#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000022#include "llvm/Support/Host.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +000024#include "llvm/Target/TargetMachine.h"
Rafael Espindolaca592212011-09-07 16:10:57 +000025#include "llvm/Target/TargetOptions.h"
Evan Cheng94214702011-07-01 20:45:01 +000026
Evan Cheng94214702011-07-01 20:45:01 +000027#define GET_SUBTARGETINFO_TARGET_DESC
Evan Chengebdeeab2011-07-08 01:53:10 +000028#define GET_SUBTARGETINFO_CTOR
Evan Cheng385e9302011-07-01 22:36:09 +000029#include "X86GenSubtargetInfo.inc"
Evan Cheng94214702011-07-01 20:45:01 +000030
Nate Begemanfb5792f2005-07-12 01:41:54 +000031using namespace llvm;
32
Chris Lattnerbc583222009-04-25 18:27:23 +000033#if defined(_MSC_VER)
Bill Wendling0ea8bf32009-08-03 00:11:34 +000034#include <intrin.h>
Chris Lattnerbc583222009-04-25 18:27:23 +000035#endif
36
Dan Gohman29cbade2009-11-20 23:18:13 +000037/// ClassifyBlockAddressReference - Classify a blockaddress reference for the
38/// current subtarget according to how we should reference it in a non-pcrel
39/// context.
Eric Christopherf5ebc392013-04-02 23:06:40 +000040unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
Dan Gohman29cbade2009-11-20 23:18:13 +000041 if (isPICStyleGOT()) // 32-bit ELF targets.
42 return X86II::MO_GOTOFF;
Chad Rosiera20e1e72012-08-01 18:39:17 +000043
Dan Gohman29cbade2009-11-20 23:18:13 +000044 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
45 return X86II::MO_PIC_BASE_OFFSET;
Chad Rosiera20e1e72012-08-01 18:39:17 +000046
Dan Gohman29cbade2009-11-20 23:18:13 +000047 // Direct static reference to label.
48 return X86II::MO_NO_FLAG;
49}
50
Chris Lattnerd392bd92009-07-10 07:20:05 +000051/// ClassifyGlobalReference - Classify a global variable reference for the
52/// current subtarget according to how we should reference it in a non-pcrel
53/// context.
54unsigned char X86Subtarget::
55ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
56 // DLLImport only exists on windows, it is implemented as a load from a
57 // DLLIMPORT stub.
58 if (GV->hasDLLImportLinkage())
59 return X86II::MO_DLLIMPORT;
60
Chris Lattner6b601532010-06-14 20:11:56 +000061 // Determine whether this is a reference to a definition or a declaration.
62 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
63 // load from stub.
64 bool isDecl = GV->hasAvailableExternallyLinkage();
65 if (GV->isDeclaration() && !GV->isMaterializable())
66 isDecl = true;
Evan Chenga82b22c2009-07-16 22:53:10 +000067
Chris Lattnerd392bd92009-07-10 07:20:05 +000068 // X86-64 in PIC mode.
69 if (isPICStyleRIPRel()) {
70 // Large model never uses stubs.
71 if (TM.getCodeModel() == CodeModel::Large)
72 return X86II::MO_NO_FLAG;
Chad Rosiera20e1e72012-08-01 18:39:17 +000073
Chris Lattnerc7822322009-07-10 21:01:59 +000074 if (isTargetDarwin()) {
75 // If symbol visibility is hidden, the extra load is not needed if
76 // target is x86-64 or the symbol is definitely defined in the current
77 // translation unit.
78 if (GV->hasDefaultVisibility() &&
Evan Chenga82b22c2009-07-16 22:53:10 +000079 (isDecl || GV->isWeakForLinker()))
Chris Lattnerc7822322009-07-10 21:01:59 +000080 return X86II::MO_GOTPCREL;
Anton Korobeynikov699647c2010-08-21 17:21:11 +000081 } else if (!isTargetWin64()) {
Chris Lattnerc7822322009-07-10 21:01:59 +000082 assert(isTargetELF() && "Unknown rip-relative target");
Chris Lattnerd392bd92009-07-10 07:20:05 +000083
Chris Lattnerc7822322009-07-10 21:01:59 +000084 // Extra load is needed for all externally visible.
85 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
86 return X86II::MO_GOTPCREL;
87 }
Chris Lattnerd392bd92009-07-10 07:20:05 +000088
89 return X86II::MO_NO_FLAG;
90 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000091
Chris Lattnerd392bd92009-07-10 07:20:05 +000092 if (isPICStyleGOT()) { // 32-bit ELF targets.
93 // Extra load is needed for all externally visible.
94 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
95 return X86II::MO_GOTOFF;
96 return X86II::MO_GOT;
97 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000098
Chris Lattnere2c92082009-07-10 21:00:45 +000099 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
Chris Lattner84853a12009-07-10 20:53:38 +0000100 // Determine whether we have a stub reference and/or whether the reference
101 // is relative to the PIC base or not.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000102
Chris Lattnerd392bd92009-07-10 07:20:05 +0000103 // If this is a strong reference to a definition, it is definitely not
104 // through a stub.
Evan Chenga82b22c2009-07-16 22:53:10 +0000105 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-07-10 20:53:38 +0000106 return X86II::MO_PIC_BASE_OFFSET;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000107
108 // Unless we have a symbol with hidden visibility, we have to go through a
109 // normal $non_lazy_ptr stub because this symbol might be resolved late.
Chris Lattner84853a12009-07-10 20:53:38 +0000110 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
111 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
Chad Rosiera20e1e72012-08-01 18:39:17 +0000112
Chris Lattnerd392bd92009-07-10 07:20:05 +0000113 // If symbol visibility is hidden, we have a stub for common symbol
114 // references and external declarations.
Evan Chenga82b22c2009-07-16 22:53:10 +0000115 if (isDecl || GV->hasCommonLinkage()) {
Chris Lattnerd392bd92009-07-10 07:20:05 +0000116 // Hidden $non_lazy_ptr reference.
Chris Lattner84853a12009-07-10 20:53:38 +0000117 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000118 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000119
Chris Lattnerd392bd92009-07-10 07:20:05 +0000120 // Otherwise, no stub.
Chris Lattner84853a12009-07-10 20:53:38 +0000121 return X86II::MO_PIC_BASE_OFFSET;
122 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000123
Chris Lattnere2c92082009-07-10 21:00:45 +0000124 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
Chris Lattner84853a12009-07-10 20:53:38 +0000125 // Determine whether we have a stub reference.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000126
Chris Lattner84853a12009-07-10 20:53:38 +0000127 // If this is a strong reference to a definition, it is definitely not
128 // through a stub.
Evan Chenga82b22c2009-07-16 22:53:10 +0000129 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-07-10 20:53:38 +0000130 return X86II::MO_NO_FLAG;
Chad Rosiera20e1e72012-08-01 18:39:17 +0000131
Chris Lattner84853a12009-07-10 20:53:38 +0000132 // Unless we have a symbol with hidden visibility, we have to go through a
133 // normal $non_lazy_ptr stub because this symbol might be resolved late.
134 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
135 return X86II::MO_DARWIN_NONLAZY;
Evan Cheng63476a82009-09-03 07:04:02 +0000136
Chris Lattner84853a12009-07-10 20:53:38 +0000137 // Otherwise, no stub.
138 return X86II::MO_NO_FLAG;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000139 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000140
Chris Lattnerd392bd92009-07-10 07:20:05 +0000141 // Direct static reference to global.
142 return X86II::MO_NO_FLAG;
143}
144
Anton Korobeynikov7784ebc2006-11-30 22:42:55 +0000145
Bill Wendling6f287b22008-09-30 21:22:07 +0000146/// getBZeroEntry - This function returns the name of a function which has an
147/// interface like the non-standard bzero function, if such a function exists on
148/// the current subtarget and it is considered prefereable over memset with zero
149/// passed as the second argument. Otherwise it returns null.
Bill Wendling6e087382008-09-30 22:05:33 +0000150const char *X86Subtarget::getBZeroEntry() const {
Dan Gohman68d599d2008-04-01 20:38:36 +0000151 // Darwin 10 has a __bzero entry point for this purpose.
Daniel Dunbar558692f2011-04-20 00:14:25 +0000152 if (getTargetTriple().isMacOSX() &&
153 !getTargetTriple().isMacOSXVersionLT(10, 6))
Bill Wendling6e087382008-09-30 22:05:33 +0000154 return "__bzero";
Dan Gohman68d599d2008-04-01 20:38:36 +0000155
156 return 0;
157}
158
Evan Cheng8688a582013-01-29 02:32:37 +0000159bool X86Subtarget::hasSinCos() const {
160 return getTargetTriple().isMacOSX() &&
Evan Chenga66f40a2013-01-30 22:56:35 +0000161 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
162 is64Bit();
Evan Cheng8688a582013-01-29 02:32:37 +0000163}
164
Evan Chengd7f666a2009-05-20 04:53:57 +0000165/// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
166/// to immediate address.
167bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
Evan Cheng18fb1d32011-07-07 21:06:52 +0000168 if (In64BitMode)
Evan Chengd7f666a2009-05-20 04:53:57 +0000169 return false;
170 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
171}
172
Aaron Ballmanf57563b2013-05-03 02:39:21 +0000173static bool OSHasAVXSupport() {
Aaron Ballman3ff641f2013-05-03 02:52:21 +0000174#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Aaron Ballmanf57563b2013-05-03 02:39:21 +0000175#if defined(__GNUC__)
176 // Check xgetbv; this uses a .byte sequence instead of the instruction
177 // directly because older assemblers do not include support for xgetbv and
178 // there is no easy way to conditionally compile based on the assembler used.
179 int rEAX, rEDX;
180 __asm__ (".byte 0x0f, 0x01, 0xd0" : "=a" (rEAX), "=d" (rEDX) : "c" (0));
181#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
182 unsigned long long rEAX = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
183#else
184 int rEAX = 0; // Ensures we return false
185#endif
186 return (rEAX & 6) == 6;
Aaron Ballman3ff641f2013-05-03 02:52:21 +0000187#else
188 return false;
189#endif
Aaron Ballmanf57563b2013-05-03 02:39:21 +0000190}
191
Evan Chenga26eb5e2006-10-06 09:17:41 +0000192void X86Subtarget::AutoDetectSubtargetFeatures() {
Evan Chengb3a7e212006-01-27 19:30:30 +0000193 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Craig Topper4145c492011-10-16 00:21:51 +0000194 unsigned MaxLevel;
Jeff Cohena3496402006-01-28 18:47:32 +0000195 union {
Jeff Cohen216d2812006-01-28 19:48:34 +0000196 unsigned u[3];
197 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +0000198 } text;
Craig Topper4145c492011-10-16 00:21:51 +0000199
200 if (X86_MC::GetCpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
201 MaxLevel < 1)
Evan Chengabc346c2006-10-06 08:21:07 +0000202 return;
Anton Korobeynikov3b5ee732007-03-23 23:46:48 +0000203
Evan Cheng18fb1d32011-07-07 21:06:52 +0000204 X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Craig Topper4145c492011-10-16 00:21:51 +0000205
Craig Topper1f104802011-10-10 05:34:02 +0000206 if ((EDX >> 15) & 1) { HasCMov = true; ToggleFeature(X86::FeatureCMOV); }
207 if ((EDX >> 23) & 1) { X86SSELevel = MMX; ToggleFeature(X86::FeatureMMX); }
208 if ((EDX >> 25) & 1) { X86SSELevel = SSE1; ToggleFeature(X86::FeatureSSE1); }
209 if ((EDX >> 26) & 1) { X86SSELevel = SSE2; ToggleFeature(X86::FeatureSSE2); }
210 if (ECX & 0x1) { X86SSELevel = SSE3; ToggleFeature(X86::FeatureSSE3); }
211 if ((ECX >> 9) & 1) { X86SSELevel = SSSE3; ToggleFeature(X86::FeatureSSSE3);}
212 if ((ECX >> 19) & 1) { X86SSELevel = SSE41; ToggleFeature(X86::FeatureSSE41);}
213 if ((ECX >> 20) & 1) { X86SSELevel = SSE42; ToggleFeature(X86::FeatureSSE42);}
Aaron Ballmanf57563b2013-05-03 02:39:21 +0000214 if (((ECX >> 27) & 1) && ((ECX >> 28) & 1) && OSHasAVXSupport()) {
215 X86SSELevel = AVX; ToggleFeature(X86::FeatureAVX);
216 }
Anton Korobeynikov3b5ee732007-03-23 23:46:48 +0000217
Evan Chengccb69762009-01-02 05:35:45 +0000218 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
219 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
David Greene343dadb2009-06-26 22:46:54 +0000220
Craig Topperc80e7d22012-05-01 07:10:32 +0000221 if ((ECX >> 1) & 0x1) {
Benjamin Kramerc8e340d2012-05-31 14:34:17 +0000222 HasPCLMUL = true;
223 ToggleFeature(X86::FeaturePCLMUL);
Craig Topper1f104802011-10-10 05:34:02 +0000224 }
Craig Topper14f094b2012-06-01 06:10:14 +0000225 if ((ECX >> 12) & 0x1) {
Craig Toppera15f9d52012-06-03 18:58:46 +0000226 HasFMA = true;
227 ToggleFeature(X86::FeatureFMA);
Craig Topper14f094b2012-06-01 06:10:14 +0000228 }
Craig Topper1f104802011-10-10 05:34:02 +0000229 if (IsIntel && ((ECX >> 22) & 0x1)) {
230 HasMOVBE = true;
231 ToggleFeature(X86::FeatureMOVBE);
232 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000233 if ((ECX >> 23) & 0x1) {
Craig Topper1f104802011-10-10 05:34:02 +0000234 HasPOPCNT = true;
235 ToggleFeature(X86::FeaturePOPCNT);
236 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000237 if ((ECX >> 25) & 0x1) {
Craig Topper1f104802011-10-10 05:34:02 +0000238 HasAES = true;
239 ToggleFeature(X86::FeatureAES);
240 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000241 if ((ECX >> 29) & 0x1) {
Craig Topper1f104802011-10-10 05:34:02 +0000242 HasF16C = true;
243 ToggleFeature(X86::FeatureF16C);
244 }
245 if (IsIntel && ((ECX >> 30) & 0x1)) {
246 HasRDRAND = true;
247 ToggleFeature(X86::FeatureRDRAND);
248 }
249
250 if ((ECX >> 13) & 0x1) {
251 HasCmpxchg16b = true;
252 ToggleFeature(X86::FeatureCMPXCHG16B);
253 }
David Greene343dadb2009-06-26 22:46:54 +0000254
Evan Chengccb69762009-01-02 05:35:45 +0000255 if (IsIntel || IsAMD) {
256 // Determine if bit test memory instructions are slow.
257 unsigned Family = 0;
258 unsigned Model = 0;
Evan Cheng18fb1d32011-07-07 21:06:52 +0000259 X86_MC::DetectFamilyModel(EAX, Family, Model);
Evan Cheng59ee62d2011-07-11 03:57:24 +0000260 if (IsAMD || (Family == 6 && Model >= 13)) {
261 IsBTMemSlow = true;
262 ToggleFeature(X86::FeatureSlowBTMem);
263 }
Andrew Trick922d3142012-02-01 23:20:51 +0000264
Chandler Carruth9f3f40f2012-12-10 09:18:44 +0000265 // If it's an Intel chip since Nehalem and not an Atom chip, unaligned
266 // memory access is fast. We hard code model numbers here because they
267 // aren't strictly increasing for Intel chips it seems.
268 if (IsIntel &&
269 ((Family == 6 && Model == 0x1E) || // Nehalem: Clarksfield, Lynnfield,
270 // Jasper Froest
Chandler Carruth2c0575f2012-12-10 18:22:40 +0000271 (Family == 6 && Model == 0x1A) || // Nehalem: Bloomfield, Nehalem-EP
Chandler Carruth9f3f40f2012-12-10 09:18:44 +0000272 (Family == 6 && Model == 0x2E) || // Nehalem: Nehalem-EX
273 (Family == 6 && Model == 0x25) || // Westmere: Arrandale, Clarksdale
274 (Family == 6 && Model == 0x2C) || // Westmere: Gulftown, Westmere-EP
275 (Family == 6 && Model == 0x2F) || // Westmere: Westmere-EX
276 (Family == 6 && Model == 0x2A) || // SandyBridge
277 (Family == 6 && Model == 0x2D) || // SandyBridge: SandyBridge-E*
278 (Family == 6 && Model == 0x3A))) {// IvyBridge
Evan Cheng48c58bb2010-04-01 05:58:17 +0000279 IsUAMemFast = true;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000280 ToggleFeature(X86::FeatureFastUAMem);
281 }
Evan Chengccb69762009-01-02 05:35:45 +0000282
Andrew Trick922d3142012-02-01 23:20:51 +0000283 // Set processor type. Currently only Atom is detected.
Preston Gurd79bbe852012-05-02 21:38:46 +0000284 if (Family == 6 &&
Preston Gurdd4d96162012-07-18 20:49:17 +0000285 (Model == 28 || Model == 38 || Model == 39
Preston Gurdfd012b22012-07-19 19:05:37 +0000286 || Model == 53 || Model == 54)) {
Andrew Trick922d3142012-02-01 23:20:51 +0000287 X86ProcFamily = IntelAtom;
Preston Gurdc573b1f2012-04-26 19:52:27 +0000288
289 UseLeaForSP = true;
Evan Chengde1df102012-02-07 22:50:41 +0000290 ToggleFeature(X86::FeatureLeaForSP);
Andrew Trick922d3142012-02-01 23:20:51 +0000291 }
292
Craig Topper4145c492011-10-16 00:21:51 +0000293 unsigned MaxExtLevel;
294 X86_MC::GetCpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
295
296 if (MaxExtLevel >= 0x80000001) {
297 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
298 if ((EDX >> 29) & 0x1) {
299 HasX86_64 = true;
300 ToggleFeature(X86::Feature64Bit);
301 }
302 if ((ECX >> 5) & 0x1) {
303 HasLZCNT = true;
304 ToggleFeature(X86::FeatureLZCNT);
305 }
Michael Liao675eb3b2013-03-26 17:47:11 +0000306 if (IsIntel && ((ECX >> 8) & 0x1)) {
307 HasPRFCHW = true;
308 ToggleFeature(X86::FeaturePRFCHW);
309 }
Craig Topper5ebee442011-12-29 19:25:56 +0000310 if (IsAMD) {
311 if ((ECX >> 6) & 0x1) {
312 HasSSE4A = true;
313 ToggleFeature(X86::FeatureSSE4A);
314 }
315 if ((ECX >> 11) & 0x1) {
316 HasXOP = true;
317 ToggleFeature(X86::FeatureXOP);
318 }
319 if ((ECX >> 16) & 0x1) {
320 HasFMA4 = true;
321 ToggleFeature(X86::FeatureFMA4);
322 }
Craig Topper4145c492011-10-16 00:21:51 +0000323 }
Evan Cheng59ee62d2011-07-11 03:57:24 +0000324 }
Craig Topper4145c492011-10-16 00:21:51 +0000325 }
326
Craig Topperc80e7d22012-05-01 07:10:32 +0000327 if (MaxLevel >= 7) {
Craig Topper4b2dc742011-10-17 05:33:10 +0000328 if (!X86_MC::GetCpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX)) {
Craig Topperc80e7d22012-05-01 07:10:32 +0000329 if (IsIntel && (EBX & 0x1)) {
Craig Toppere7b05502011-10-30 19:57:21 +0000330 HasFSGSBase = true;
331 ToggleFeature(X86::FeatureFSGSBase);
332 }
Craig Topper4b2dc742011-10-17 05:33:10 +0000333 if ((EBX >> 3) & 0x1) {
334 HasBMI = true;
335 ToggleFeature(X86::FeatureBMI);
336 }
Michael Liao0ca1a7f2013-03-26 22:46:02 +0000337 if ((EBX >> 4) & 0x1) {
338 HasHLE = true;
339 ToggleFeature(X86::FeatureHLE);
340 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000341 if (IsIntel && ((EBX >> 5) & 0x1)) {
Craig Topper1203f2f2012-04-26 06:40:15 +0000342 X86SSELevel = AVX2;
343 ToggleFeature(X86::FeatureAVX2);
344 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000345 if (IsIntel && ((EBX >> 8) & 0x1)) {
Craig Topper4b2dc742011-10-17 05:33:10 +0000346 HasBMI2 = true;
347 ToggleFeature(X86::FeatureBMI2);
348 }
Michael Liaobe02a902012-11-08 07:28:54 +0000349 if (IsIntel && ((EBX >> 11) & 0x1)) {
350 HasRTM = true;
351 ToggleFeature(X86::FeatureRTM);
352 }
Michael Liao5a8386e2013-03-28 22:29:53 +0000353 if (IsIntel && ((EBX >> 19) & 0x1)) {
354 HasADX = true;
355 ToggleFeature(X86::FeatureADX);
356 }
Michael Liaoc26392a2013-03-28 23:41:26 +0000357 if (IsIntel && ((EBX >> 18) & 0x1)) {
358 HasRDSEED = true;
359 ToggleFeature(X86::FeatureRDSEED);
360 }
Craig Topperb53fa8b2011-10-16 07:55:05 +0000361 }
Jeff Cohenc3987092007-04-16 21:59:44 +0000362 }
Evan Cheng559806f2006-01-27 08:10:46 +0000363}
Evan Cheng97c7fc32006-01-26 09:53:06 +0000364
Bill Wendling789cb5d2013-02-15 22:31:27 +0000365void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
366 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
367 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
368 "target-cpu");
369 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
370 "target-features");
Nadav Rotemf8b80de2013-02-27 05:56:20 +0000371 std::string CPU =
372 !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
373 std::string FS =
Bill Wendling789cb5d2013-02-15 22:31:27 +0000374 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
Bill Wendling901d8002013-02-16 01:36:26 +0000375 if (!FS.empty()) {
376 initializeEnvironment();
Bill Wendling789cb5d2013-02-15 22:31:27 +0000377 resetSubtargetFeatures(CPU, FS);
Bill Wendling901d8002013-02-16 01:36:26 +0000378 }
Bill Wendling789cb5d2013-02-15 22:31:27 +0000379}
380
381void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
Nadav Rotemf8b80de2013-02-27 05:56:20 +0000382 std::string CPUName = CPU;
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000383 if (!FS.empty() || !CPU.empty()) {
Evan Chengcc0ddc72011-07-08 21:14:14 +0000384 if (CPUName.empty()) {
Evan Cheng893a0452012-01-30 23:10:32 +0000385#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
386 || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
Evan Chengcc0ddc72011-07-08 21:14:14 +0000387 CPUName = sys::getHostCPUName();
388#else
389 CPUName = "generic";
390#endif
391 }
392
Eli Friedman439d05d2011-07-08 23:43:01 +0000393 // Make sure 64-bit features are available in 64-bit mode. (But make sure
394 // SSE2 can be turned off explicitly.)
395 std::string FullFS = FS;
396 if (In64BitMode) {
397 if (!FullFS.empty())
398 FullFS = "+64bit,+sse2," + FullFS;
399 else
400 FullFS = "+64bit,+sse2";
401 }
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000402
Eli Friedman439d05d2011-07-08 23:43:01 +0000403 // If feature string is not empty, parse features string.
404 ParseSubtargetFeatures(CPUName, FullFS);
Chris Lattner3b6f4972006-11-20 18:16:05 +0000405 } else {
Andrew Trick922d3142012-02-01 23:20:51 +0000406 if (CPUName.empty()) {
407#if defined (__x86_64__) || defined(__i386__)
408 CPUName = sys::getHostCPUName();
409#else
410 CPUName = "generic";
411#endif
412 }
Chris Lattner3b6f4972006-11-20 18:16:05 +0000413 // Otherwise, use CPUID to auto-detect feature set.
414 AutoDetectSubtargetFeatures();
Evan Cheng18fb1d32011-07-07 21:06:52 +0000415
Eli Friedman6dfef662011-07-08 23:07:42 +0000416 // Make sure 64-bit features are available in 64-bit mode.
417 if (In64BitMode) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000418 HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
419 HasCMov = true; ToggleFeature(X86::FeatureCMOV);
Eli Friedman6dfef662011-07-08 23:07:42 +0000420
Craig Topper16de4632012-01-09 09:02:13 +0000421 if (X86SSELevel < SSE2) {
Eli Friedman6dfef662011-07-08 23:07:42 +0000422 X86SSELevel = SSE2;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000423 ToggleFeature(X86::FeatureSSE1);
424 ToggleFeature(X86::FeatureSSE2);
425 }
Eli Friedman6dfef662011-07-08 23:07:42 +0000426 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000427 }
Evan Cheng59ee62d2011-07-11 03:57:24 +0000428
Preston Gurdbfcb4aa2012-10-03 15:55:13 +0000429 // CPUName may have been set by the CPU detection code. Make sure the
430 // new MCSchedModel is used.
431 InitMCProcessorInfo(CPUName, FS);
432
Andrew Trickc42a7012012-08-07 00:25:30 +0000433 if (X86ProcFamily == IntelAtom)
Andrew Trick922d3142012-02-01 23:20:51 +0000434 PostRAScheduler = true;
Andrew Trickc42a7012012-08-07 00:25:30 +0000435
436 InstrItins = getInstrItineraryForCPU(CPUName);
Andrew Trick922d3142012-02-01 23:20:51 +0000437
Evan Cheng59ee62d2011-07-11 03:57:24 +0000438 // It's important to keep the MCSubtargetInfo feature bits in sync with
439 // target data structure which is shared with MC code emitter, etc.
440 if (In64BitMode)
441 ToggleFeature(X86::Mode64Bit);
442
David Greenebd7b8452010-01-05 01:29:13 +0000443 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000444 << ", 3DNowLevel " << X863DNowLevel
445 << ", 64bit " << HasX86_64 << "\n");
Evan Cheng18fb1d32011-07-07 21:06:52 +0000446 assert((!In64BitMode || HasX86_64) &&
Dan Gohmanf75e5b42009-02-03 00:04:43 +0000447 "64-bit code requested on a subtarget that doesn't support it!");
Evan Cheng25ab6902006-09-08 06:48:29 +0000448
Roman Divacky64f03672012-11-09 20:10:44 +0000449 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
Roman Divacky4c3ab582011-02-22 17:30:05 +0000450 // 32 and 64 bit) and for all 64-bit targets.
Evan Chengef41ff62011-06-23 17:54:54 +0000451 if (StackAlignOverride)
452 stackAlignment = StackAlignOverride;
Roman Divacky64f03672012-11-09 20:10:44 +0000453 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
454 In64BitMode)
Nate Begemanfb5792f2005-07-12 01:41:54 +0000455 stackAlignment = 16;
Dan Gohman4d3d6e12010-05-27 18:43:40 +0000456}
Andrew Trick922d3142012-02-01 23:20:51 +0000457
Bill Wendling901d8002013-02-16 01:36:26 +0000458void X86Subtarget::initializeEnvironment() {
459 X86SSELevel = NoMMXSSE;
460 X863DNowLevel = NoThreeDNow;
461 HasCMov = false;
462 HasX86_64 = false;
463 HasPOPCNT = false;
464 HasSSE4A = false;
465 HasAES = false;
466 HasPCLMUL = false;
467 HasFMA = false;
468 HasFMA4 = false;
469 HasXOP = false;
470 HasMOVBE = false;
471 HasRDRAND = false;
472 HasF16C = false;
473 HasFSGSBase = false;
474 HasLZCNT = false;
475 HasBMI = false;
476 HasBMI2 = false;
477 HasRTM = false;
Michael Liao0ca1a7f2013-03-26 22:46:02 +0000478 HasHLE = false;
Bill Wendling901d8002013-02-16 01:36:26 +0000479 HasADX = false;
Michael Liao675eb3b2013-03-26 17:47:11 +0000480 HasPRFCHW = false;
Michael Liaoc26392a2013-03-28 23:41:26 +0000481 HasRDSEED = false;
Bill Wendling901d8002013-02-16 01:36:26 +0000482 IsBTMemSlow = false;
483 IsUAMemFast = false;
484 HasVectorUAMem = false;
485 HasCmpxchg16b = false;
486 UseLeaForSP = false;
487 HasSlowDivide = false;
488 PostRAScheduler = false;
489 PadShortFunctions = false;
Preston Gurd1edadea2013-03-27 19:14:02 +0000490 CallRegIndirect = false;
Preston Gurdd6ac8e92013-04-25 20:29:37 +0000491 LEAUsesAG = false;
Bill Wendling901d8002013-02-16 01:36:26 +0000492 stackAlignment = 4;
493 // FIXME: this is a known good value for Yonah. How about others?
494 MaxInlineSizeThreshold = 128;
495}
496
Bill Wendling789cb5d2013-02-15 22:31:27 +0000497X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
498 const std::string &FS,
499 unsigned StackAlignOverride, bool is64Bit)
500 : X86GenSubtargetInfo(TT, CPU, FS)
501 , X86ProcFamily(Others)
Bill Wendlingba6867d2013-02-15 23:22:32 +0000502 , PICStyle(PICStyles::None)
Bill Wendling789cb5d2013-02-15 22:31:27 +0000503 , TargetTriple(TT)
504 , StackAlignOverride(StackAlignOverride)
505 , In64BitMode(is64Bit) {
Bill Wendling901d8002013-02-16 01:36:26 +0000506 initializeEnvironment();
Bill Wendling789cb5d2013-02-15 22:31:27 +0000507 resetSubtargetFeatures(CPU, FS);
508}
509
Andrew Trick922d3142012-02-01 23:20:51 +0000510bool X86Subtarget::enablePostRAScheduler(
511 CodeGenOpt::Level OptLevel,
512 TargetSubtargetInfo::AntiDepBreakMode& Mode,
513 RegClassVector& CriticalPathRCs) const {
Preston Gurd6a8c7bf2012-04-23 21:39:35 +0000514 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
Andrew Trick922d3142012-02-01 23:20:51 +0000515 CriticalPathRCs.clear();
516 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
517}