blob: 58e0d06dfb474fa5d1c4a4ec820c7d63eb18606b [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.
40unsigned char X86Subtarget::
41ClassifyBlockAddressReference() const {
42 if (isPICStyleGOT()) // 32-bit ELF targets.
43 return X86II::MO_GOTOFF;
Chad Rosiera20e1e72012-08-01 18:39:17 +000044
Dan Gohman29cbade2009-11-20 23:18:13 +000045 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
46 return X86II::MO_PIC_BASE_OFFSET;
Chad Rosiera20e1e72012-08-01 18:39:17 +000047
Dan Gohman29cbade2009-11-20 23:18:13 +000048 // Direct static reference to label.
49 return X86II::MO_NO_FLAG;
50}
51
Chris Lattnerd392bd92009-07-10 07:20:05 +000052/// ClassifyGlobalReference - Classify a global variable reference for the
53/// current subtarget according to how we should reference it in a non-pcrel
54/// context.
55unsigned char X86Subtarget::
56ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
57 // DLLImport only exists on windows, it is implemented as a load from a
58 // DLLIMPORT stub.
59 if (GV->hasDLLImportLinkage())
60 return X86II::MO_DLLIMPORT;
61
Chris Lattner6b601532010-06-14 20:11:56 +000062 // Determine whether this is a reference to a definition or a declaration.
63 // Materializable GVs (in JIT lazy compilation mode) do not require an extra
64 // load from stub.
65 bool isDecl = GV->hasAvailableExternallyLinkage();
66 if (GV->isDeclaration() && !GV->isMaterializable())
67 isDecl = true;
Evan Chenga82b22c2009-07-16 22:53:10 +000068
Chris Lattnerd392bd92009-07-10 07:20:05 +000069 // X86-64 in PIC mode.
70 if (isPICStyleRIPRel()) {
71 // Large model never uses stubs.
72 if (TM.getCodeModel() == CodeModel::Large)
73 return X86II::MO_NO_FLAG;
Chad Rosiera20e1e72012-08-01 18:39:17 +000074
Chris Lattnerc7822322009-07-10 21:01:59 +000075 if (isTargetDarwin()) {
76 // If symbol visibility is hidden, the extra load is not needed if
77 // target is x86-64 or the symbol is definitely defined in the current
78 // translation unit.
79 if (GV->hasDefaultVisibility() &&
Evan Chenga82b22c2009-07-16 22:53:10 +000080 (isDecl || GV->isWeakForLinker()))
Chris Lattnerc7822322009-07-10 21:01:59 +000081 return X86II::MO_GOTPCREL;
Anton Korobeynikov699647c2010-08-21 17:21:11 +000082 } else if (!isTargetWin64()) {
Chris Lattnerc7822322009-07-10 21:01:59 +000083 assert(isTargetELF() && "Unknown rip-relative target");
Chris Lattnerd392bd92009-07-10 07:20:05 +000084
Chris Lattnerc7822322009-07-10 21:01:59 +000085 // Extra load is needed for all externally visible.
86 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
87 return X86II::MO_GOTPCREL;
88 }
Chris Lattnerd392bd92009-07-10 07:20:05 +000089
90 return X86II::MO_NO_FLAG;
91 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000092
Chris Lattnerd392bd92009-07-10 07:20:05 +000093 if (isPICStyleGOT()) { // 32-bit ELF targets.
94 // Extra load is needed for all externally visible.
95 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
96 return X86II::MO_GOTOFF;
97 return X86II::MO_GOT;
98 }
Chad Rosiera20e1e72012-08-01 18:39:17 +000099
Chris Lattnere2c92082009-07-10 21:00:45 +0000100 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
Chris Lattner84853a12009-07-10 20:53:38 +0000101 // Determine whether we have a stub reference and/or whether the reference
102 // is relative to the PIC base or not.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000103
Chris Lattnerd392bd92009-07-10 07:20:05 +0000104 // If this is a strong reference to a definition, it is definitely not
105 // through a stub.
Evan Chenga82b22c2009-07-16 22:53:10 +0000106 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-07-10 20:53:38 +0000107 return X86II::MO_PIC_BASE_OFFSET;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000108
109 // Unless we have a symbol with hidden visibility, we have to go through a
110 // normal $non_lazy_ptr stub because this symbol might be resolved late.
Chris Lattner84853a12009-07-10 20:53:38 +0000111 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
112 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
Chad Rosiera20e1e72012-08-01 18:39:17 +0000113
Chris Lattnerd392bd92009-07-10 07:20:05 +0000114 // If symbol visibility is hidden, we have a stub for common symbol
115 // references and external declarations.
Evan Chenga82b22c2009-07-16 22:53:10 +0000116 if (isDecl || GV->hasCommonLinkage()) {
Chris Lattnerd392bd92009-07-10 07:20:05 +0000117 // Hidden $non_lazy_ptr reference.
Chris Lattner84853a12009-07-10 20:53:38 +0000118 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000119 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000120
Chris Lattnerd392bd92009-07-10 07:20:05 +0000121 // Otherwise, no stub.
Chris Lattner84853a12009-07-10 20:53:38 +0000122 return X86II::MO_PIC_BASE_OFFSET;
123 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000124
Chris Lattnere2c92082009-07-10 21:00:45 +0000125 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
Chris Lattner84853a12009-07-10 20:53:38 +0000126 // Determine whether we have a stub reference.
Chad Rosiera20e1e72012-08-01 18:39:17 +0000127
Chris Lattner84853a12009-07-10 20:53:38 +0000128 // If this is a strong reference to a definition, it is definitely not
129 // through a stub.
Evan Chenga82b22c2009-07-16 22:53:10 +0000130 if (!isDecl && !GV->isWeakForLinker())
Chris Lattner84853a12009-07-10 20:53:38 +0000131 return X86II::MO_NO_FLAG;
Chad Rosiera20e1e72012-08-01 18:39:17 +0000132
Chris Lattner84853a12009-07-10 20:53:38 +0000133 // Unless we have a symbol with hidden visibility, we have to go through a
134 // normal $non_lazy_ptr stub because this symbol might be resolved late.
135 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
136 return X86II::MO_DARWIN_NONLAZY;
Evan Cheng63476a82009-09-03 07:04:02 +0000137
Chris Lattner84853a12009-07-10 20:53:38 +0000138 // Otherwise, no stub.
139 return X86II::MO_NO_FLAG;
Chris Lattnerd392bd92009-07-10 07:20:05 +0000140 }
Chad Rosiera20e1e72012-08-01 18:39:17 +0000141
Chris Lattnerd392bd92009-07-10 07:20:05 +0000142 // Direct static reference to global.
143 return X86II::MO_NO_FLAG;
144}
145
Anton Korobeynikov7784ebc2006-11-30 22:42:55 +0000146
Bill Wendling6f287b22008-09-30 21:22:07 +0000147/// getBZeroEntry - This function returns the name of a function which has an
148/// interface like the non-standard bzero function, if such a function exists on
149/// the current subtarget and it is considered prefereable over memset with zero
150/// passed as the second argument. Otherwise it returns null.
Bill Wendling6e087382008-09-30 22:05:33 +0000151const char *X86Subtarget::getBZeroEntry() const {
Dan Gohman68d599d2008-04-01 20:38:36 +0000152 // Darwin 10 has a __bzero entry point for this purpose.
Daniel Dunbar558692f2011-04-20 00:14:25 +0000153 if (getTargetTriple().isMacOSX() &&
154 !getTargetTriple().isMacOSXVersionLT(10, 6))
Bill Wendling6e087382008-09-30 22:05:33 +0000155 return "__bzero";
Dan Gohman68d599d2008-04-01 20:38:36 +0000156
157 return 0;
158}
159
Evan Cheng8688a582013-01-29 02:32:37 +0000160bool X86Subtarget::hasSinCos() const {
161 return getTargetTriple().isMacOSX() &&
Evan Chenga66f40a2013-01-30 22:56:35 +0000162 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
163 is64Bit();
Evan Cheng8688a582013-01-29 02:32:37 +0000164}
165
Evan Chengd7f666a2009-05-20 04:53:57 +0000166/// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
167/// to immediate address.
168bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
Evan Cheng18fb1d32011-07-07 21:06:52 +0000169 if (In64BitMode)
Evan Chengd7f666a2009-05-20 04:53:57 +0000170 return false;
171 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
172}
173
Evan Chenga26eb5e2006-10-06 09:17:41 +0000174void X86Subtarget::AutoDetectSubtargetFeatures() {
Evan Chengb3a7e212006-01-27 19:30:30 +0000175 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Craig Topper4145c492011-10-16 00:21:51 +0000176 unsigned MaxLevel;
Jeff Cohena3496402006-01-28 18:47:32 +0000177 union {
Jeff Cohen216d2812006-01-28 19:48:34 +0000178 unsigned u[3];
179 char c[12];
Jeff Cohena3496402006-01-28 18:47:32 +0000180 } text;
Craig Topper4145c492011-10-16 00:21:51 +0000181
182 if (X86_MC::GetCpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
183 MaxLevel < 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);
Craig Topper4145c492011-10-16 00:21:51 +0000187
Craig Topper1f104802011-10-10 05:34:02 +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);}
Craig Topper1203f2f2012-04-26 06:40:15 +0000196 if ((ECX >> 28) & 1) { X86SSELevel = AVX; ToggleFeature(X86::FeatureAVX); }
Anton Korobeynikov3b5ee732007-03-23 23:46:48 +0000197
Evan Chengccb69762009-01-02 05:35:45 +0000198 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
199 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
David Greene343dadb2009-06-26 22:46:54 +0000200
Craig Topperc80e7d22012-05-01 07:10:32 +0000201 if ((ECX >> 1) & 0x1) {
Benjamin Kramerc8e340d2012-05-31 14:34:17 +0000202 HasPCLMUL = true;
203 ToggleFeature(X86::FeaturePCLMUL);
Craig Topper1f104802011-10-10 05:34:02 +0000204 }
Craig Topper14f094b2012-06-01 06:10:14 +0000205 if ((ECX >> 12) & 0x1) {
Craig Toppera15f9d52012-06-03 18:58:46 +0000206 HasFMA = true;
207 ToggleFeature(X86::FeatureFMA);
Craig Topper14f094b2012-06-01 06:10:14 +0000208 }
Craig Topper1f104802011-10-10 05:34:02 +0000209 if (IsIntel && ((ECX >> 22) & 0x1)) {
210 HasMOVBE = true;
211 ToggleFeature(X86::FeatureMOVBE);
212 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000213 if ((ECX >> 23) & 0x1) {
Craig Topper1f104802011-10-10 05:34:02 +0000214 HasPOPCNT = true;
215 ToggleFeature(X86::FeaturePOPCNT);
216 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000217 if ((ECX >> 25) & 0x1) {
Craig Topper1f104802011-10-10 05:34:02 +0000218 HasAES = true;
219 ToggleFeature(X86::FeatureAES);
220 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000221 if ((ECX >> 29) & 0x1) {
Craig Topper1f104802011-10-10 05:34:02 +0000222 HasF16C = true;
223 ToggleFeature(X86::FeatureF16C);
224 }
225 if (IsIntel && ((ECX >> 30) & 0x1)) {
226 HasRDRAND = true;
227 ToggleFeature(X86::FeatureRDRAND);
228 }
229
230 if ((ECX >> 13) & 0x1) {
231 HasCmpxchg16b = true;
232 ToggleFeature(X86::FeatureCMPXCHG16B);
233 }
David Greene343dadb2009-06-26 22:46:54 +0000234
Evan Chengccb69762009-01-02 05:35:45 +0000235 if (IsIntel || IsAMD) {
236 // Determine if bit test memory instructions are slow.
237 unsigned Family = 0;
238 unsigned Model = 0;
Evan Cheng18fb1d32011-07-07 21:06:52 +0000239 X86_MC::DetectFamilyModel(EAX, Family, Model);
Evan Cheng59ee62d2011-07-11 03:57:24 +0000240 if (IsAMD || (Family == 6 && Model >= 13)) {
241 IsBTMemSlow = true;
242 ToggleFeature(X86::FeatureSlowBTMem);
243 }
Andrew Trick922d3142012-02-01 23:20:51 +0000244
Chandler Carruth9f3f40f2012-12-10 09:18:44 +0000245 // If it's an Intel chip since Nehalem and not an Atom chip, unaligned
246 // memory access is fast. We hard code model numbers here because they
247 // aren't strictly increasing for Intel chips it seems.
248 if (IsIntel &&
249 ((Family == 6 && Model == 0x1E) || // Nehalem: Clarksfield, Lynnfield,
250 // Jasper Froest
Chandler Carruth2c0575f2012-12-10 18:22:40 +0000251 (Family == 6 && Model == 0x1A) || // Nehalem: Bloomfield, Nehalem-EP
Chandler Carruth9f3f40f2012-12-10 09:18:44 +0000252 (Family == 6 && Model == 0x2E) || // Nehalem: Nehalem-EX
253 (Family == 6 && Model == 0x25) || // Westmere: Arrandale, Clarksdale
254 (Family == 6 && Model == 0x2C) || // Westmere: Gulftown, Westmere-EP
255 (Family == 6 && Model == 0x2F) || // Westmere: Westmere-EX
256 (Family == 6 && Model == 0x2A) || // SandyBridge
257 (Family == 6 && Model == 0x2D) || // SandyBridge: SandyBridge-E*
258 (Family == 6 && Model == 0x3A))) {// IvyBridge
Evan Cheng48c58bb2010-04-01 05:58:17 +0000259 IsUAMemFast = true;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000260 ToggleFeature(X86::FeatureFastUAMem);
261 }
Evan Chengccb69762009-01-02 05:35:45 +0000262
Andrew Trick922d3142012-02-01 23:20:51 +0000263 // Set processor type. Currently only Atom is detected.
Preston Gurd79bbe852012-05-02 21:38:46 +0000264 if (Family == 6 &&
Preston Gurdd4d96162012-07-18 20:49:17 +0000265 (Model == 28 || Model == 38 || Model == 39
Preston Gurdfd012b22012-07-19 19:05:37 +0000266 || Model == 53 || Model == 54)) {
Andrew Trick922d3142012-02-01 23:20:51 +0000267 X86ProcFamily = IntelAtom;
Preston Gurdc573b1f2012-04-26 19:52:27 +0000268
269 UseLeaForSP = true;
Evan Chengde1df102012-02-07 22:50:41 +0000270 ToggleFeature(X86::FeatureLeaForSP);
Andrew Trick922d3142012-02-01 23:20:51 +0000271 }
272
Craig Topper4145c492011-10-16 00:21:51 +0000273 unsigned MaxExtLevel;
274 X86_MC::GetCpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
275
276 if (MaxExtLevel >= 0x80000001) {
277 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
278 if ((EDX >> 29) & 0x1) {
279 HasX86_64 = true;
280 ToggleFeature(X86::Feature64Bit);
281 }
282 if ((ECX >> 5) & 0x1) {
283 HasLZCNT = true;
284 ToggleFeature(X86::FeatureLZCNT);
285 }
Craig Topper5ebee442011-12-29 19:25:56 +0000286 if (IsAMD) {
287 if ((ECX >> 6) & 0x1) {
288 HasSSE4A = true;
289 ToggleFeature(X86::FeatureSSE4A);
290 }
291 if ((ECX >> 11) & 0x1) {
292 HasXOP = true;
293 ToggleFeature(X86::FeatureXOP);
294 }
295 if ((ECX >> 16) & 0x1) {
296 HasFMA4 = true;
297 ToggleFeature(X86::FeatureFMA4);
298 }
Craig Topper4145c492011-10-16 00:21:51 +0000299 }
Evan Cheng59ee62d2011-07-11 03:57:24 +0000300 }
Craig Topper4145c492011-10-16 00:21:51 +0000301 }
302
Craig Topperc80e7d22012-05-01 07:10:32 +0000303 if (MaxLevel >= 7) {
Craig Topper4b2dc742011-10-17 05:33:10 +0000304 if (!X86_MC::GetCpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX)) {
Craig Topperc80e7d22012-05-01 07:10:32 +0000305 if (IsIntel && (EBX & 0x1)) {
Craig Toppere7b05502011-10-30 19:57:21 +0000306 HasFSGSBase = true;
307 ToggleFeature(X86::FeatureFSGSBase);
308 }
Craig Topper4b2dc742011-10-17 05:33:10 +0000309 if ((EBX >> 3) & 0x1) {
310 HasBMI = true;
311 ToggleFeature(X86::FeatureBMI);
312 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000313 if (IsIntel && ((EBX >> 5) & 0x1)) {
Craig Topper1203f2f2012-04-26 06:40:15 +0000314 X86SSELevel = AVX2;
315 ToggleFeature(X86::FeatureAVX2);
316 }
Craig Topperc80e7d22012-05-01 07:10:32 +0000317 if (IsIntel && ((EBX >> 8) & 0x1)) {
Craig Topper4b2dc742011-10-17 05:33:10 +0000318 HasBMI2 = true;
319 ToggleFeature(X86::FeatureBMI2);
320 }
Michael Liaobe02a902012-11-08 07:28:54 +0000321 if (IsIntel && ((EBX >> 11) & 0x1)) {
322 HasRTM = true;
323 ToggleFeature(X86::FeatureRTM);
324 }
Craig Topperb53fa8b2011-10-16 07:55:05 +0000325 }
Jeff Cohenc3987092007-04-16 21:59:44 +0000326 }
Evan Cheng559806f2006-01-27 08:10:46 +0000327}
Evan Cheng97c7fc32006-01-26 09:53:06 +0000328
Bill Wendling789cb5d2013-02-15 22:31:27 +0000329void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
330 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
331 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
332 "target-cpu");
333 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
334 "target-features");
335 std::string CPU =
336 !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
337 std::string FS =
338 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
339 if (!FS.empty())
340 resetSubtargetFeatures(CPU, FS);
341}
342
343void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
Andrew Trick922d3142012-02-01 23:20:51 +0000344 std::string CPUName = CPU;
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000345 if (!FS.empty() || !CPU.empty()) {
Evan Chengcc0ddc72011-07-08 21:14:14 +0000346 if (CPUName.empty()) {
Evan Cheng893a0452012-01-30 23:10:32 +0000347#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
348 || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
Evan Chengcc0ddc72011-07-08 21:14:14 +0000349 CPUName = sys::getHostCPUName();
350#else
351 CPUName = "generic";
352#endif
353 }
354
Eli Friedman439d05d2011-07-08 23:43:01 +0000355 // Make sure 64-bit features are available in 64-bit mode. (But make sure
356 // SSE2 can be turned off explicitly.)
357 std::string FullFS = FS;
358 if (In64BitMode) {
359 if (!FullFS.empty())
360 FullFS = "+64bit,+sse2," + FullFS;
361 else
362 FullFS = "+64bit,+sse2";
363 }
Evan Cheng4d1a8dd2011-07-08 22:30:25 +0000364
Eli Friedman439d05d2011-07-08 23:43:01 +0000365 // If feature string is not empty, parse features string.
366 ParseSubtargetFeatures(CPUName, FullFS);
Chris Lattner3b6f4972006-11-20 18:16:05 +0000367 } else {
Andrew Trick922d3142012-02-01 23:20:51 +0000368 if (CPUName.empty()) {
369#if defined (__x86_64__) || defined(__i386__)
370 CPUName = sys::getHostCPUName();
371#else
372 CPUName = "generic";
373#endif
374 }
Chris Lattner3b6f4972006-11-20 18:16:05 +0000375 // Otherwise, use CPUID to auto-detect feature set.
376 AutoDetectSubtargetFeatures();
Evan Cheng18fb1d32011-07-07 21:06:52 +0000377
Eli Friedman6dfef662011-07-08 23:07:42 +0000378 // Make sure 64-bit features are available in 64-bit mode.
379 if (In64BitMode) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000380 HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
381 HasCMov = true; ToggleFeature(X86::FeatureCMOV);
Eli Friedman6dfef662011-07-08 23:07:42 +0000382
Craig Topper16de4632012-01-09 09:02:13 +0000383 if (X86SSELevel < SSE2) {
Eli Friedman6dfef662011-07-08 23:07:42 +0000384 X86SSELevel = SSE2;
Evan Cheng59ee62d2011-07-11 03:57:24 +0000385 ToggleFeature(X86::FeatureSSE1);
386 ToggleFeature(X86::FeatureSSE2);
387 }
Eli Friedman6dfef662011-07-08 23:07:42 +0000388 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000389 }
Evan Cheng59ee62d2011-07-11 03:57:24 +0000390
Preston Gurdbfcb4aa2012-10-03 15:55:13 +0000391 // CPUName may have been set by the CPU detection code. Make sure the
392 // new MCSchedModel is used.
393 InitMCProcessorInfo(CPUName, FS);
394
Andrew Trickc42a7012012-08-07 00:25:30 +0000395 if (X86ProcFamily == IntelAtom)
Andrew Trick922d3142012-02-01 23:20:51 +0000396 PostRAScheduler = true;
Andrew Trickc42a7012012-08-07 00:25:30 +0000397
398 InstrItins = getInstrItineraryForCPU(CPUName);
Andrew Trick922d3142012-02-01 23:20:51 +0000399
Evan Cheng59ee62d2011-07-11 03:57:24 +0000400 // It's important to keep the MCSubtargetInfo feature bits in sync with
401 // target data structure which is shared with MC code emitter, etc.
402 if (In64BitMode)
403 ToggleFeature(X86::Mode64Bit);
404
David Greenebd7b8452010-01-05 01:29:13 +0000405 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000406 << ", 3DNowLevel " << X863DNowLevel
407 << ", 64bit " << HasX86_64 << "\n");
Evan Cheng18fb1d32011-07-07 21:06:52 +0000408 assert((!In64BitMode || HasX86_64) &&
Dan Gohmanf75e5b42009-02-03 00:04:43 +0000409 "64-bit code requested on a subtarget that doesn't support it!");
Evan Cheng25ab6902006-09-08 06:48:29 +0000410
Roman Divacky64f03672012-11-09 20:10:44 +0000411 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
Roman Divacky4c3ab582011-02-22 17:30:05 +0000412 // 32 and 64 bit) and for all 64-bit targets.
Evan Chengef41ff62011-06-23 17:54:54 +0000413 if (StackAlignOverride)
414 stackAlignment = StackAlignOverride;
Roman Divacky64f03672012-11-09 20:10:44 +0000415 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
416 In64BitMode)
Nate Begemanfb5792f2005-07-12 01:41:54 +0000417 stackAlignment = 16;
Dan Gohman4d3d6e12010-05-27 18:43:40 +0000418}
Andrew Trick922d3142012-02-01 23:20:51 +0000419
Bill Wendling789cb5d2013-02-15 22:31:27 +0000420X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
421 const std::string &FS,
422 unsigned StackAlignOverride, bool is64Bit)
423 : X86GenSubtargetInfo(TT, CPU, FS)
424 , X86ProcFamily(Others)
425 , PICStyle(PICStyles::None)
426 , X86SSELevel(NoMMXSSE)
427 , X863DNowLevel(NoThreeDNow)
428 , HasCMov(false)
429 , HasX86_64(false)
430 , HasPOPCNT(false)
431 , HasSSE4A(false)
432 , HasAES(false)
433 , HasPCLMUL(false)
434 , HasFMA(false)
435 , HasFMA4(false)
436 , HasXOP(false)
437 , HasMOVBE(false)
438 , HasRDRAND(false)
439 , HasF16C(false)
440 , HasFSGSBase(false)
441 , HasLZCNT(false)
442 , HasBMI(false)
443 , HasBMI2(false)
444 , HasRTM(false)
445 , HasADX(false)
446 , IsBTMemSlow(false)
447 , IsUAMemFast(false)
448 , HasVectorUAMem(false)
449 , HasCmpxchg16b(false)
450 , UseLeaForSP(false)
451 , HasSlowDivide(false)
452 , PostRAScheduler(false)
453 , PadShortFunctions(false)
454 , stackAlignment(4)
455 // FIXME: this is a known good value for Yonah. How about others?
456 , MaxInlineSizeThreshold(128)
457 , TargetTriple(TT)
458 , StackAlignOverride(StackAlignOverride)
459 , In64BitMode(is64Bit) {
460 resetSubtargetFeatures(CPU, FS);
461}
462
Andrew Trick922d3142012-02-01 23:20:51 +0000463bool X86Subtarget::enablePostRAScheduler(
464 CodeGenOpt::Level OptLevel,
465 TargetSubtargetInfo::AntiDepBreakMode& Mode,
466 RegClassVector& CriticalPathRCs) const {
Preston Gurd6a8c7bf2012-04-23 21:39:35 +0000467 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
Andrew Trick922d3142012-02-01 23:20:51 +0000468 CriticalPathRCs.clear();
469 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
470}