blob: 6f724b42d977483c9ff2420dc4837c1a84adcd85 [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"
Bill Wendlingaef9c372013-02-15 22:31:27 +000017#include "llvm/IR/Attributes.h"
18#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/GlobalValue.h"
Evan Cheng9a3ec1b2009-01-03 04:04:46 +000020#include "llvm/Support/Debug.h"
Rafael Espindola65596562011-09-07 16:10:57 +000021#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000022#include "llvm/Support/Host.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov430e68a12006-12-22 22:29:05 +000024#include "llvm/Target/TargetMachine.h"
Rafael Espindola65596562011-09-07 16:10:57 +000025#include "llvm/Target/TargetOptions.h"
Evan Cheng54b68e32011-07-01 20:45:01 +000026
Evan Cheng54b68e32011-07-01 20:45:01 +000027#define GET_SUBTARGETINFO_TARGET_DESC
Evan Cheng4d1ca962011-07-08 01:53:10 +000028#define GET_SUBTARGETINFO_CTOR
Evan Chengc9c090d2011-07-01 22:36:09 +000029#include "X86GenSubtargetInfo.inc"
Evan Cheng54b68e32011-07-01 20:45:01 +000030
Nate Begemanf26625e2005-07-12 01:41:54 +000031using namespace llvm;
32
Chris Lattner3ad60b12009-04-25 18:27:23 +000033#if defined(_MSC_VER)
Bill Wendling6eecd562009-08-03 00:11:34 +000034#include <intrin.h>
Chris Lattner3ad60b12009-04-25 18:27:23 +000035#endif
36
Dan Gohman7a6611792009-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 Christophere2fbc672013-04-02 23:06:40 +000040unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
Dan Gohman7a6611792009-11-20 23:18:13 +000041 if (isPICStyleGOT()) // 32-bit ELF targets.
42 return X86II::MO_GOTOFF;
Chad Rosier24c19d22012-08-01 18:39:17 +000043
Dan Gohman7a6611792009-11-20 23:18:13 +000044 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
45 return X86II::MO_PIC_BASE_OFFSET;
Chad Rosier24c19d22012-08-01 18:39:17 +000046
Dan Gohman7a6611792009-11-20 23:18:13 +000047 // Direct static reference to label.
48 return X86II::MO_NO_FLAG;
49}
50
Chris Lattnerdc842c02009-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.
Nico Rieck7157bb72014-01-14 15:22:47 +000058 if (GV->hasDLLImportStorageClass())
Chris Lattnerdc842c02009-07-10 07:20:05 +000059 return X86II::MO_DLLIMPORT;
60
Chris Lattnerfaa7bdc2010-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 Cheng02a76522009-07-16 22:53:10 +000067
Chris Lattnerdc842c02009-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 Rosier24c19d22012-08-01 18:39:17 +000073
Chris Lattner7dce9912009-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 Cheng02a76522009-07-16 22:53:10 +000079 (isDecl || GV->isWeakForLinker()))
Chris Lattner7dce9912009-07-10 21:01:59 +000080 return X86II::MO_GOTPCREL;
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000081 } else if (!isTargetWin64()) {
Chris Lattner7dce9912009-07-10 21:01:59 +000082 assert(isTargetELF() && "Unknown rip-relative target");
Chris Lattnerdc842c02009-07-10 07:20:05 +000083
Chris Lattner7dce9912009-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 Lattnerdc842c02009-07-10 07:20:05 +000088
89 return X86II::MO_NO_FLAG;
90 }
Chad Rosier24c19d22012-08-01 18:39:17 +000091
Chris Lattnerdc842c02009-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 Rosier24c19d22012-08-01 18:39:17 +000098
Chris Lattner21c29402009-07-10 21:00:45 +000099 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
Chris Lattnerbd3e5602009-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 Rosier24c19d22012-08-01 18:39:17 +0000102
Chris Lattnerdc842c02009-07-10 07:20:05 +0000103 // If this is a strong reference to a definition, it is definitely not
104 // through a stub.
Evan Cheng02a76522009-07-16 22:53:10 +0000105 if (!isDecl && !GV->isWeakForLinker())
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000106 return X86II::MO_PIC_BASE_OFFSET;
Chris Lattnerdc842c02009-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 Lattnerbd3e5602009-07-10 20:53:38 +0000110 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
111 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
Chad Rosier24c19d22012-08-01 18:39:17 +0000112
Chris Lattnerdc842c02009-07-10 07:20:05 +0000113 // If symbol visibility is hidden, we have a stub for common symbol
114 // references and external declarations.
Evan Cheng02a76522009-07-16 22:53:10 +0000115 if (isDecl || GV->hasCommonLinkage()) {
Chris Lattnerdc842c02009-07-10 07:20:05 +0000116 // Hidden $non_lazy_ptr reference.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000117 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000118 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000119
Chris Lattnerdc842c02009-07-10 07:20:05 +0000120 // Otherwise, no stub.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000121 return X86II::MO_PIC_BASE_OFFSET;
122 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000123
Chris Lattner21c29402009-07-10 21:00:45 +0000124 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000125 // Determine whether we have a stub reference.
Chad Rosier24c19d22012-08-01 18:39:17 +0000126
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000127 // If this is a strong reference to a definition, it is definitely not
128 // through a stub.
Evan Cheng02a76522009-07-16 22:53:10 +0000129 if (!isDecl && !GV->isWeakForLinker())
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000130 return X86II::MO_NO_FLAG;
Chad Rosier24c19d22012-08-01 18:39:17 +0000131
Chris Lattnerbd3e5602009-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 Cheng1b389522009-09-03 07:04:02 +0000136
Chris Lattnerbd3e5602009-07-10 20:53:38 +0000137 // Otherwise, no stub.
138 return X86II::MO_NO_FLAG;
Chris Lattnerdc842c02009-07-10 07:20:05 +0000139 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000140
Chris Lattnerdc842c02009-07-10 07:20:05 +0000141 // Direct static reference to global.
142 return X86II::MO_NO_FLAG;
143}
144
Anton Korobeynikov6dbdfe22006-11-30 22:42:55 +0000145
Bill Wendlingbd092622008-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 Wendling17825842008-09-30 22:05:33 +0000150const char *X86Subtarget::getBZeroEntry() const {
Dan Gohman980d7202008-04-01 20:38:36 +0000151 // Darwin 10 has a __bzero entry point for this purpose.
Daniel Dunbarcd01ed52011-04-20 00:14:25 +0000152 if (getTargetTriple().isMacOSX() &&
153 !getTargetTriple().isMacOSXVersionLT(10, 6))
Bill Wendling17825842008-09-30 22:05:33 +0000154 return "__bzero";
Dan Gohman980d7202008-04-01 20:38:36 +0000155
156 return 0;
157}
158
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000159bool X86Subtarget::hasSinCos() const {
160 return getTargetTriple().isMacOSX() &&
Evan Chengd2ca4e22013-01-30 22:56:35 +0000161 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
162 is64Bit();
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000163}
164
Evan Cheng96098332009-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 Cheng13bcc6c2011-07-07 21:06:52 +0000168 if (In64BitMode)
Evan Cheng96098332009-05-20 04:53:57 +0000169 return false;
170 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
171}
172
Aaron Ballman63fe0142013-05-03 02:39:21 +0000173static bool OSHasAVXSupport() {
Michael Kuperstein1a0c91f2013-05-07 14:05:33 +0000174#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
175 || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
Aaron Ballman63fe0142013-05-03 02:39:21 +0000176#if defined(__GNUC__)
177 // Check xgetbv; this uses a .byte sequence instead of the instruction
178 // directly because older assemblers do not include support for xgetbv and
179 // there is no easy way to conditionally compile based on the assembler used.
180 int rEAX, rEDX;
181 __asm__ (".byte 0x0f, 0x01, 0xd0" : "=a" (rEAX), "=d" (rEDX) : "c" (0));
182#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
183 unsigned long long rEAX = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
184#else
185 int rEAX = 0; // Ensures we return false
186#endif
187 return (rEAX & 6) == 6;
Aaron Ballmancc958f02013-05-03 02:52:21 +0000188#else
189 return false;
190#endif
Aaron Ballman63fe0142013-05-03 02:39:21 +0000191}
192
Evan Chengff1beda2006-10-06 09:17:41 +0000193void X86Subtarget::AutoDetectSubtargetFeatures() {
Evan Chengafab7aa2006-01-27 19:30:30 +0000194 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
Craig Topper6c8879e2011-10-16 00:21:51 +0000195 unsigned MaxLevel;
Jeff Cohen71287082006-01-28 18:47:32 +0000196 union {
Jeff Cohen58ca0be92006-01-28 19:48:34 +0000197 unsigned u[3];
198 char c[12];
Jeff Cohen71287082006-01-28 18:47:32 +0000199 } text;
Craig Topper6c8879e2011-10-16 00:21:51 +0000200
201 if (X86_MC::GetCpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
202 MaxLevel < 1)
Evan Cheng9274f722006-10-06 08:21:07 +0000203 return;
Anton Korobeynikov8aae2d72007-03-23 23:46:48 +0000204
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000205 X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Craig Topper6c8879e2011-10-16 00:21:51 +0000206
Craig Toppera14c5722011-10-10 05:34:02 +0000207 if ((EDX >> 15) & 1) { HasCMov = true; ToggleFeature(X86::FeatureCMOV); }
208 if ((EDX >> 23) & 1) { X86SSELevel = MMX; ToggleFeature(X86::FeatureMMX); }
209 if ((EDX >> 25) & 1) { X86SSELevel = SSE1; ToggleFeature(X86::FeatureSSE1); }
210 if ((EDX >> 26) & 1) { X86SSELevel = SSE2; ToggleFeature(X86::FeatureSSE2); }
211 if (ECX & 0x1) { X86SSELevel = SSE3; ToggleFeature(X86::FeatureSSE3); }
212 if ((ECX >> 9) & 1) { X86SSELevel = SSSE3; ToggleFeature(X86::FeatureSSSE3);}
213 if ((ECX >> 19) & 1) { X86SSELevel = SSE41; ToggleFeature(X86::FeatureSSE41);}
214 if ((ECX >> 20) & 1) { X86SSELevel = SSE42; ToggleFeature(X86::FeatureSSE42);}
Aaron Ballman63fe0142013-05-03 02:39:21 +0000215 if (((ECX >> 27) & 1) && ((ECX >> 28) & 1) && OSHasAVXSupport()) {
216 X86SSELevel = AVX; ToggleFeature(X86::FeatureAVX);
217 }
Anton Korobeynikov8aae2d72007-03-23 23:46:48 +0000218
Evan Cheng4c91aa32009-01-02 05:35:45 +0000219 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
220 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
David Greene8f6f72c2009-06-26 22:46:54 +0000221
Craig Topper05eb6e02012-05-01 07:10:32 +0000222 if ((ECX >> 1) & 0x1) {
Benjamin Kramera0396e42012-05-31 14:34:17 +0000223 HasPCLMUL = true;
224 ToggleFeature(X86::FeaturePCLMUL);
Craig Toppera14c5722011-10-10 05:34:02 +0000225 }
Craig Topper1d4d62d2012-06-01 06:10:14 +0000226 if ((ECX >> 12) & 0x1) {
Craig Topper79dbb0c2012-06-03 18:58:46 +0000227 HasFMA = true;
228 ToggleFeature(X86::FeatureFMA);
Craig Topper1d4d62d2012-06-01 06:10:14 +0000229 }
Craig Toppera14c5722011-10-10 05:34:02 +0000230 if (IsIntel && ((ECX >> 22) & 0x1)) {
231 HasMOVBE = true;
232 ToggleFeature(X86::FeatureMOVBE);
233 }
Craig Topper05eb6e02012-05-01 07:10:32 +0000234 if ((ECX >> 23) & 0x1) {
Craig Toppera14c5722011-10-10 05:34:02 +0000235 HasPOPCNT = true;
236 ToggleFeature(X86::FeaturePOPCNT);
237 }
Craig Topper05eb6e02012-05-01 07:10:32 +0000238 if ((ECX >> 25) & 0x1) {
Craig Toppera14c5722011-10-10 05:34:02 +0000239 HasAES = true;
240 ToggleFeature(X86::FeatureAES);
241 }
Craig Topper05eb6e02012-05-01 07:10:32 +0000242 if ((ECX >> 29) & 0x1) {
Craig Toppera14c5722011-10-10 05:34:02 +0000243 HasF16C = true;
244 ToggleFeature(X86::FeatureF16C);
245 }
246 if (IsIntel && ((ECX >> 30) & 0x1)) {
247 HasRDRAND = true;
248 ToggleFeature(X86::FeatureRDRAND);
249 }
250
251 if ((ECX >> 13) & 0x1) {
252 HasCmpxchg16b = true;
253 ToggleFeature(X86::FeatureCMPXCHG16B);
254 }
David Greene8f6f72c2009-06-26 22:46:54 +0000255
Evan Cheng4c91aa32009-01-02 05:35:45 +0000256 if (IsIntel || IsAMD) {
257 // Determine if bit test memory instructions are slow.
258 unsigned Family = 0;
259 unsigned Model = 0;
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000260 X86_MC::DetectFamilyModel(EAX, Family, Model);
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000261 if (IsAMD || (Family == 6 && Model >= 13)) {
262 IsBTMemSlow = true;
263 ToggleFeature(X86::FeatureSlowBTMem);
264 }
Andrew Trick8523b162012-02-01 23:20:51 +0000265
Ekaterina Romanovad5fa5542013-11-21 23:21:26 +0000266 // Determine if SHLD/SHRD instructions have higher latency then the
267 // equivalent series of shifts/or instructions.
268 // FIXME: Add Intel's processors that have SHLD instructions with very
269 // poor latency.
270 if (IsAMD) {
271 IsSHLDSlow = true;
272 ToggleFeature(X86::FeatureSlowSHLD);
273 }
274
Chandler Carruth0f585582012-12-10 09:18:44 +0000275 // If it's an Intel chip since Nehalem and not an Atom chip, unaligned
276 // memory access is fast. We hard code model numbers here because they
277 // aren't strictly increasing for Intel chips it seems.
278 if (IsIntel &&
279 ((Family == 6 && Model == 0x1E) || // Nehalem: Clarksfield, Lynnfield,
280 // Jasper Froest
Chandler Carruth17f25c4e2012-12-10 18:22:40 +0000281 (Family == 6 && Model == 0x1A) || // Nehalem: Bloomfield, Nehalem-EP
Chandler Carruth0f585582012-12-10 09:18:44 +0000282 (Family == 6 && Model == 0x2E) || // Nehalem: Nehalem-EX
283 (Family == 6 && Model == 0x25) || // Westmere: Arrandale, Clarksdale
284 (Family == 6 && Model == 0x2C) || // Westmere: Gulftown, Westmere-EP
285 (Family == 6 && Model == 0x2F) || // Westmere: Westmere-EX
286 (Family == 6 && Model == 0x2A) || // SandyBridge
287 (Family == 6 && Model == 0x2D) || // SandyBridge: SandyBridge-E*
Tim Northover89ccb612013-11-25 09:52:59 +0000288 (Family == 6 && Model == 0x3A) || // IvyBridge
289 (Family == 6 && Model == 0x3E) || // IvyBridge EP
290 (Family == 6 && Model == 0x3C) || // Haswell
291 (Family == 6 && Model == 0x3F) || // ...
292 (Family == 6 && Model == 0x45) || // ...
293 (Family == 6 && Model == 0x46))) { // ...
Evan Cheng738b0f92010-04-01 05:58:17 +0000294 IsUAMemFast = true;
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000295 ToggleFeature(X86::FeatureFastUAMem);
296 }
Evan Cheng4c91aa32009-01-02 05:35:45 +0000297
Preston Gurd3fe264d2013-09-13 19:23:28 +0000298 // Set processor type. Currently only Atom or Silvermont (SLM) is detected.
Preston Gurdc0b976c2012-05-02 21:38:46 +0000299 if (Family == 6 &&
Craig Topper21a916b2013-09-13 04:41:06 +0000300 (Model == 28 || Model == 38 || Model == 39 ||
301 Model == 53 || Model == 54)) {
Andrew Trick8523b162012-02-01 23:20:51 +0000302 X86ProcFamily = IntelAtom;
Preston Gurd81290f42012-04-26 19:52:27 +0000303
304 UseLeaForSP = true;
Evan Cheng1b81fdd2012-02-07 22:50:41 +0000305 ToggleFeature(X86::FeatureLeaForSP);
Andrew Trick8523b162012-02-01 23:20:51 +0000306 }
Preston Gurd3fe264d2013-09-13 19:23:28 +0000307 else if (Family == 6 &&
308 (Model == 55 || Model == 74 || Model == 77)) {
309 X86ProcFamily = IntelSLM;
310 }
Andrew Trick8523b162012-02-01 23:20:51 +0000311
Craig Topper6c8879e2011-10-16 00:21:51 +0000312 unsigned MaxExtLevel;
313 X86_MC::GetCpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
314
315 if (MaxExtLevel >= 0x80000001) {
316 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
317 if ((EDX >> 29) & 0x1) {
318 HasX86_64 = true;
319 ToggleFeature(X86::Feature64Bit);
320 }
321 if ((ECX >> 5) & 0x1) {
322 HasLZCNT = true;
323 ToggleFeature(X86::FeatureLZCNT);
324 }
Michael Liao5173ee02013-03-26 17:47:11 +0000325 if (IsIntel && ((ECX >> 8) & 0x1)) {
326 HasPRFCHW = true;
327 ToggleFeature(X86::FeaturePRFCHW);
328 }
Craig Topperdd286a52011-12-29 19:25:56 +0000329 if (IsAMD) {
330 if ((ECX >> 6) & 0x1) {
331 HasSSE4A = true;
332 ToggleFeature(X86::FeatureSSE4A);
333 }
334 if ((ECX >> 11) & 0x1) {
335 HasXOP = true;
336 ToggleFeature(X86::FeatureXOP);
337 }
338 if ((ECX >> 16) & 0x1) {
339 HasFMA4 = true;
340 ToggleFeature(X86::FeatureFMA4);
341 }
Craig Topper6c8879e2011-10-16 00:21:51 +0000342 }
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000343 }
Craig Topper6c8879e2011-10-16 00:21:51 +0000344 }
345
Craig Topper05eb6e02012-05-01 07:10:32 +0000346 if (MaxLevel >= 7) {
Craig Toppere20793a2011-10-17 05:33:10 +0000347 if (!X86_MC::GetCpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX)) {
Craig Topper05eb6e02012-05-01 07:10:32 +0000348 if (IsIntel && (EBX & 0x1)) {
Craig Topper228d9132011-10-30 19:57:21 +0000349 HasFSGSBase = true;
350 ToggleFeature(X86::FeatureFSGSBase);
351 }
Craig Toppere20793a2011-10-17 05:33:10 +0000352 if ((EBX >> 3) & 0x1) {
353 HasBMI = true;
354 ToggleFeature(X86::FeatureBMI);
355 }
Michael Liaoe344ec92013-03-26 22:46:02 +0000356 if ((EBX >> 4) & 0x1) {
357 HasHLE = true;
358 ToggleFeature(X86::FeatureHLE);
359 }
Craig Topper05eb6e02012-05-01 07:10:32 +0000360 if (IsIntel && ((EBX >> 5) & 0x1)) {
Craig Topper08ccfbe2012-04-26 06:40:15 +0000361 X86SSELevel = AVX2;
362 ToggleFeature(X86::FeatureAVX2);
363 }
Craig Topper05eb6e02012-05-01 07:10:32 +0000364 if (IsIntel && ((EBX >> 8) & 0x1)) {
Craig Toppere20793a2011-10-17 05:33:10 +0000365 HasBMI2 = true;
366 ToggleFeature(X86::FeatureBMI2);
367 }
Michael Liao73cffdd2012-11-08 07:28:54 +0000368 if (IsIntel && ((EBX >> 11) & 0x1)) {
369 HasRTM = true;
370 ToggleFeature(X86::FeatureRTM);
371 }
Craig Toppere13a0662013-08-20 05:22:42 +0000372 if (IsIntel && ((EBX >> 16) & 0x1)) {
Craig Topper5c94bb82013-08-21 03:57:57 +0000373 X86SSELevel = AVX512F;
Craig Toppere13a0662013-08-20 05:22:42 +0000374 ToggleFeature(X86::FeatureAVX512);
Michael Liaoc93fe7f2013-03-28 22:29:53 +0000375 }
Michael Liaoa486a112013-03-28 23:41:26 +0000376 if (IsIntel && ((EBX >> 18) & 0x1)) {
377 HasRDSEED = true;
378 ToggleFeature(X86::FeatureRDSEED);
379 }
Craig Toppere13a0662013-08-20 05:22:42 +0000380 if (IsIntel && ((EBX >> 19) & 0x1)) {
381 HasADX = true;
382 ToggleFeature(X86::FeatureADX);
383 }
384 if (IsIntel && ((EBX >> 26) & 0x1)) {
385 HasPFI = true;
386 ToggleFeature(X86::FeaturePFI);
387 }
388 if (IsIntel && ((EBX >> 27) & 0x1)) {
389 HasERI = true;
390 ToggleFeature(X86::FeatureERI);
391 }
392 if (IsIntel && ((EBX >> 28) & 0x1)) {
393 HasCDI = true;
394 ToggleFeature(X86::FeatureCDI);
395 }
Ben Langmuir16501752013-09-12 15:51:31 +0000396 if (IsIntel && ((EBX >> 29) & 0x1)) {
397 HasSHA = true;
398 ToggleFeature(X86::FeatureSHA);
399 }
Craig Topperaea148c2011-10-16 07:55:05 +0000400 }
Yunzhong Gaodd36e932013-09-24 18:21:52 +0000401 if (IsAMD && ((ECX >> 21) & 0x1)) {
402 HasTBM = true;
403 ToggleFeature(X86::FeatureTBM);
404 }
Jeff Cohen6f3a5482007-04-16 21:59:44 +0000405 }
Evan Chengcde9e302006-01-27 08:10:46 +0000406}
Evan Cheng54c13da2006-01-26 09:53:06 +0000407
Bill Wendlingaef9c372013-02-15 22:31:27 +0000408void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
409 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
410 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
411 "target-cpu");
412 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
413 "target-features");
Nadav Rotem08ab8772013-02-27 05:56:20 +0000414 std::string CPU =
415 !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
416 std::string FS =
Bill Wendlingaef9c372013-02-15 22:31:27 +0000417 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
Bill Wendling61375d82013-02-16 01:36:26 +0000418 if (!FS.empty()) {
419 initializeEnvironment();
Bill Wendlingaef9c372013-02-15 22:31:27 +0000420 resetSubtargetFeatures(CPU, FS);
Bill Wendling61375d82013-02-16 01:36:26 +0000421 }
Bill Wendlingaef9c372013-02-15 22:31:27 +0000422}
423
424void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
Nadav Rotem08ab8772013-02-27 05:56:20 +0000425 std::string CPUName = CPU;
Evan Cheng60fc0fc2011-07-08 22:30:25 +0000426 if (!FS.empty() || !CPU.empty()) {
Evan Cheng964cb5f2011-07-08 21:14:14 +0000427 if (CPUName.empty()) {
Evan Cheng4e7992e2012-01-30 23:10:32 +0000428#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
429 || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
Evan Cheng964cb5f2011-07-08 21:14:14 +0000430 CPUName = sys::getHostCPUName();
431#else
432 CPUName = "generic";
433#endif
434 }
435
Eli Friedmanfe2088b2011-07-08 23:43:01 +0000436 // Make sure 64-bit features are available in 64-bit mode. (But make sure
437 // SSE2 can be turned off explicitly.)
438 std::string FullFS = FS;
439 if (In64BitMode) {
440 if (!FullFS.empty())
441 FullFS = "+64bit,+sse2," + FullFS;
442 else
443 FullFS = "+64bit,+sse2";
444 }
Evan Cheng60fc0fc2011-07-08 22:30:25 +0000445
Eli Friedmanfe2088b2011-07-08 23:43:01 +0000446 // If feature string is not empty, parse features string.
447 ParseSubtargetFeatures(CPUName, FullFS);
Chris Lattner3e962112006-11-20 18:16:05 +0000448 } else {
Andrew Trick8523b162012-02-01 23:20:51 +0000449 if (CPUName.empty()) {
450#if defined (__x86_64__) || defined(__i386__)
451 CPUName = sys::getHostCPUName();
452#else
453 CPUName = "generic";
454#endif
455 }
Chris Lattner3e962112006-11-20 18:16:05 +0000456 // Otherwise, use CPUID to auto-detect feature set.
457 AutoDetectSubtargetFeatures();
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000458
Eli Friedman52868332011-07-08 23:07:42 +0000459 // Make sure 64-bit features are available in 64-bit mode.
460 if (In64BitMode) {
Craig Topper358c7982013-09-18 06:01:53 +0000461 if (!HasX86_64) { HasX86_64 = true; ToggleFeature(X86::Feature64Bit); }
462 if (!HasCMov) { HasCMov = true; ToggleFeature(X86::FeatureCMOV); }
Eli Friedman52868332011-07-08 23:07:42 +0000463
Craig Topperf287a452012-01-09 09:02:13 +0000464 if (X86SSELevel < SSE2) {
Eli Friedman52868332011-07-08 23:07:42 +0000465 X86SSELevel = SSE2;
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000466 ToggleFeature(X86::FeatureSSE1);
467 ToggleFeature(X86::FeatureSSE2);
468 }
Eli Friedman52868332011-07-08 23:07:42 +0000469 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000470 }
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000471
Preston Gurd35fcb542012-10-03 15:55:13 +0000472 // CPUName may have been set by the CPU detection code. Make sure the
473 // new MCSchedModel is used.
Craig Toppera8442342013-09-18 05:54:09 +0000474 InitCPUSchedModel(CPUName);
Preston Gurd35fcb542012-10-03 15:55:13 +0000475
Preston Gurd3fe264d2013-09-13 19:23:28 +0000476 if (X86ProcFamily == IntelAtom || X86ProcFamily == IntelSLM)
Andrew Trick8523b162012-02-01 23:20:51 +0000477 PostRAScheduler = true;
Andrew Tricke0c83b12012-08-07 00:25:30 +0000478
479 InstrItins = getInstrItineraryForCPU(CPUName);
Andrew Trick8523b162012-02-01 23:20:51 +0000480
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000481 // It's important to keep the MCSubtargetInfo feature bits in sync with
482 // target data structure which is shared with MC code emitter, etc.
483 if (In64BitMode)
484 ToggleFeature(X86::Mode64Bit);
Craig Topper3c80d622014-01-06 04:55:54 +0000485 else if (In32BitMode)
486 ToggleFeature(X86::Mode32Bit);
487 else if (In16BitMode)
488 ToggleFeature(X86::Mode16Bit);
489 else
490 llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000491
David Greene00411812010-01-05 01:29:13 +0000492 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
Bill Wendling6eecd562009-08-03 00:11:34 +0000493 << ", 3DNowLevel " << X863DNowLevel
494 << ", 64bit " << HasX86_64 << "\n");
Evan Cheng13bcc6c2011-07-07 21:06:52 +0000495 assert((!In64BitMode || HasX86_64) &&
Dan Gohman74037512009-02-03 00:04:43 +0000496 "64-bit code requested on a subtarget that doesn't support it!");
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000497
Roman Divacky22135672012-11-09 20:10:44 +0000498 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
Roman Divackye8a93fe82011-02-22 17:30:05 +0000499 // 32 and 64 bit) and for all 64-bit targets.
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000500 if (StackAlignOverride)
501 stackAlignment = StackAlignOverride;
Roman Divacky22135672012-11-09 20:10:44 +0000502 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
503 In64BitMode)
Nate Begemanf26625e2005-07-12 01:41:54 +0000504 stackAlignment = 16;
Dan Gohmandc53f1c2010-05-27 18:43:40 +0000505}
Andrew Trick8523b162012-02-01 23:20:51 +0000506
Bill Wendling61375d82013-02-16 01:36:26 +0000507void X86Subtarget::initializeEnvironment() {
508 X86SSELevel = NoMMXSSE;
509 X863DNowLevel = NoThreeDNow;
510 HasCMov = false;
511 HasX86_64 = false;
512 HasPOPCNT = false;
513 HasSSE4A = false;
514 HasAES = false;
515 HasPCLMUL = false;
516 HasFMA = false;
517 HasFMA4 = false;
518 HasXOP = false;
Yunzhong Gaodd36e932013-09-24 18:21:52 +0000519 HasTBM = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000520 HasMOVBE = false;
521 HasRDRAND = false;
522 HasF16C = false;
523 HasFSGSBase = false;
524 HasLZCNT = false;
525 HasBMI = false;
526 HasBMI2 = false;
527 HasRTM = false;
Michael Liaoe344ec92013-03-26 22:46:02 +0000528 HasHLE = false;
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000529 HasERI = false;
530 HasCDI = false;
Craig Topper7a8cf012013-08-20 05:23:59 +0000531 HasPFI = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000532 HasADX = false;
Ben Langmuir16501752013-09-12 15:51:31 +0000533 HasSHA = false;
Michael Liao5173ee02013-03-26 17:47:11 +0000534 HasPRFCHW = false;
Michael Liaoa486a112013-03-28 23:41:26 +0000535 HasRDSEED = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000536 IsBTMemSlow = false;
Ekaterina Romanovad5fa5542013-11-21 23:21:26 +0000537 IsSHLDSlow = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000538 IsUAMemFast = false;
539 HasVectorUAMem = false;
540 HasCmpxchg16b = false;
541 UseLeaForSP = false;
542 HasSlowDivide = false;
543 PostRAScheduler = false;
544 PadShortFunctions = false;
Preston Gurd663e6f92013-03-27 19:14:02 +0000545 CallRegIndirect = false;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000546 LEAUsesAG = false;
Bill Wendling61375d82013-02-16 01:36:26 +0000547 stackAlignment = 4;
548 // FIXME: this is a known good value for Yonah. How about others?
549 MaxInlineSizeThreshold = 128;
550}
551
Bill Wendlingaef9c372013-02-15 22:31:27 +0000552X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
553 const std::string &FS,
David Woodhouse1c3996a2014-01-08 00:08:50 +0000554 unsigned StackAlignOverride)
Bill Wendlingaef9c372013-02-15 22:31:27 +0000555 : X86GenSubtargetInfo(TT, CPU, FS)
556 , X86ProcFamily(Others)
Bill Wendlinge9434772013-02-15 23:22:32 +0000557 , PICStyle(PICStyles::None)
Bill Wendlingaef9c372013-02-15 22:31:27 +0000558 , TargetTriple(TT)
559 , StackAlignOverride(StackAlignOverride)
David Woodhouse1c3996a2014-01-08 00:08:50 +0000560 , In64BitMode(TargetTriple.getArch() == Triple::x86_64)
561 , In32BitMode(TargetTriple.getArch() == Triple::x86)
Craig Topper3c80d622014-01-06 04:55:54 +0000562 , In16BitMode(false) {
Bill Wendling61375d82013-02-16 01:36:26 +0000563 initializeEnvironment();
Bill Wendlingaef9c372013-02-15 22:31:27 +0000564 resetSubtargetFeatures(CPU, FS);
565}
566
Andrew Trick8523b162012-02-01 23:20:51 +0000567bool X86Subtarget::enablePostRAScheduler(
568 CodeGenOpt::Level OptLevel,
569 TargetSubtargetInfo::AntiDepBreakMode& Mode,
570 RegClassVector& CriticalPathRCs) const {
Preston Gurd9a091472012-04-23 21:39:35 +0000571 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
Andrew Trick8523b162012-02-01 23:20:51 +0000572 CriticalPathRCs.clear();
573 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
574}