1 //===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the X86 specific subclass of TargetSubtargetInfo.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
27 #define GET_SUBTARGETINFO_TARGET_DESC
28 #define GET_SUBTARGETINFO_CTOR
29 #include "X86GenSubtargetInfo.inc"
37 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
38 /// current subtarget according to how we should reference it in a non-pcrel
40 unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
41 if (isPICStyleGOT()) // 32-bit ELF targets.
42 return X86II::MO_GOTOFF;
44 if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
45 return X86II::MO_PIC_BASE_OFFSET;
47 // Direct static reference to label.
48 return X86II::MO_NO_FLAG;
51 /// ClassifyGlobalReference - Classify a global variable reference for the
52 /// current subtarget according to how we should reference it in a non-pcrel
54 unsigned char X86Subtarget::
55 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
56 // DLLImport only exists on windows, it is implemented as a load from a
58 if (GV->hasDLLImportLinkage())
59 return X86II::MO_DLLIMPORT;
61 // 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
64 bool isDecl = GV->hasAvailableExternallyLinkage();
65 if (GV->isDeclaration() && !GV->isMaterializable())
68 // 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;
74 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
78 if (GV->hasDefaultVisibility() &&
79 (isDecl || GV->isWeakForLinker()))
80 return X86II::MO_GOTPCREL;
81 } else if (!isTargetWin64()) {
82 assert(isTargetELF() && "Unknown rip-relative target");
84 // Extra load is needed for all externally visible.
85 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
86 return X86II::MO_GOTPCREL;
89 return X86II::MO_NO_FLAG;
92 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;
99 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
100 // Determine whether we have a stub reference and/or whether the reference
101 // is relative to the PIC base or not.
103 // If this is a strong reference to a definition, it is definitely not
105 if (!isDecl && !GV->isWeakForLinker())
106 return X86II::MO_PIC_BASE_OFFSET;
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.
110 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
111 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
113 // If symbol visibility is hidden, we have a stub for common symbol
114 // references and external declarations.
115 if (isDecl || GV->hasCommonLinkage()) {
116 // Hidden $non_lazy_ptr reference.
117 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
120 // Otherwise, no stub.
121 return X86II::MO_PIC_BASE_OFFSET;
124 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
125 // Determine whether we have a stub reference.
127 // If this is a strong reference to a definition, it is definitely not
129 if (!isDecl && !GV->isWeakForLinker())
130 return X86II::MO_NO_FLAG;
132 // 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;
137 // Otherwise, no stub.
138 return X86II::MO_NO_FLAG;
141 // Direct static reference to global.
142 return X86II::MO_NO_FLAG;
146 /// 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.
150 const char *X86Subtarget::getBZeroEntry() const {
151 // Darwin 10 has a __bzero entry point for this purpose.
152 if (getTargetTriple().isMacOSX() &&
153 !getTargetTriple().isMacOSXVersionLT(10, 6))
159 bool X86Subtarget::hasSinCos() const {
160 return getTargetTriple().isMacOSX() &&
161 !getTargetTriple().isMacOSXVersionLT(10, 9) &&
165 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
166 /// to immediate address.
167 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
170 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
173 static bool OSHasAVXSupport() {
174 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
175 #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.
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);
184 int rEAX = 0; // Ensures we return false
186 return (rEAX & 6) == 6;
192 void X86Subtarget::AutoDetectSubtargetFeatures() {
193 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
200 if (X86_MC::GetCpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
204 X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
206 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);}
214 if (((ECX >> 27) & 1) && ((ECX >> 28) & 1) && OSHasAVXSupport()) {
215 X86SSELevel = AVX; ToggleFeature(X86::FeatureAVX);
218 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
219 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
221 if ((ECX >> 1) & 0x1) {
223 ToggleFeature(X86::FeaturePCLMUL);
225 if ((ECX >> 12) & 0x1) {
227 ToggleFeature(X86::FeatureFMA);
229 if (IsIntel && ((ECX >> 22) & 0x1)) {
231 ToggleFeature(X86::FeatureMOVBE);
233 if ((ECX >> 23) & 0x1) {
235 ToggleFeature(X86::FeaturePOPCNT);
237 if ((ECX >> 25) & 0x1) {
239 ToggleFeature(X86::FeatureAES);
241 if ((ECX >> 29) & 0x1) {
243 ToggleFeature(X86::FeatureF16C);
245 if (IsIntel && ((ECX >> 30) & 0x1)) {
247 ToggleFeature(X86::FeatureRDRAND);
250 if ((ECX >> 13) & 0x1) {
251 HasCmpxchg16b = true;
252 ToggleFeature(X86::FeatureCMPXCHG16B);
255 if (IsIntel || IsAMD) {
256 // Determine if bit test memory instructions are slow.
259 X86_MC::DetectFamilyModel(EAX, Family, Model);
260 if (IsAMD || (Family == 6 && Model >= 13)) {
262 ToggleFeature(X86::FeatureSlowBTMem);
265 // 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.
269 ((Family == 6 && Model == 0x1E) || // Nehalem: Clarksfield, Lynnfield,
271 (Family == 6 && Model == 0x1A) || // Nehalem: Bloomfield, Nehalem-EP
272 (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
280 ToggleFeature(X86::FeatureFastUAMem);
283 // Set processor type. Currently only Atom is detected.
285 (Model == 28 || Model == 38 || Model == 39
286 || Model == 53 || Model == 54)) {
287 X86ProcFamily = IntelAtom;
290 ToggleFeature(X86::FeatureLeaForSP);
293 unsigned MaxExtLevel;
294 X86_MC::GetCpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
296 if (MaxExtLevel >= 0x80000001) {
297 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
298 if ((EDX >> 29) & 0x1) {
300 ToggleFeature(X86::Feature64Bit);
302 if ((ECX >> 5) & 0x1) {
304 ToggleFeature(X86::FeatureLZCNT);
306 if (IsIntel && ((ECX >> 8) & 0x1)) {
308 ToggleFeature(X86::FeaturePRFCHW);
311 if ((ECX >> 6) & 0x1) {
313 ToggleFeature(X86::FeatureSSE4A);
315 if ((ECX >> 11) & 0x1) {
317 ToggleFeature(X86::FeatureXOP);
319 if ((ECX >> 16) & 0x1) {
321 ToggleFeature(X86::FeatureFMA4);
328 if (!X86_MC::GetCpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX)) {
329 if (IsIntel && (EBX & 0x1)) {
331 ToggleFeature(X86::FeatureFSGSBase);
333 if ((EBX >> 3) & 0x1) {
335 ToggleFeature(X86::FeatureBMI);
337 if ((EBX >> 4) & 0x1) {
339 ToggleFeature(X86::FeatureHLE);
341 if (IsIntel && ((EBX >> 5) & 0x1)) {
343 ToggleFeature(X86::FeatureAVX2);
345 if (IsIntel && ((EBX >> 8) & 0x1)) {
347 ToggleFeature(X86::FeatureBMI2);
349 if (IsIntel && ((EBX >> 11) & 0x1)) {
351 ToggleFeature(X86::FeatureRTM);
353 if (IsIntel && ((EBX >> 19) & 0x1)) {
355 ToggleFeature(X86::FeatureADX);
357 if (IsIntel && ((EBX >> 18) & 0x1)) {
359 ToggleFeature(X86::FeatureRDSEED);
365 void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
366 AttributeSet FnAttrs = MF->getFunction()->getAttributes();
367 Attribute CPUAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
369 Attribute FSAttr = FnAttrs.getAttribute(AttributeSet::FunctionIndex,
372 !CPUAttr.hasAttribute(Attribute::None) ?CPUAttr.getValueAsString() : "";
374 !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
376 initializeEnvironment();
377 resetSubtargetFeatures(CPU, FS);
381 void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
382 std::string CPUName = CPU;
383 if (!FS.empty() || !CPU.empty()) {
384 if (CPUName.empty()) {
385 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
386 || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
387 CPUName = sys::getHostCPUName();
393 // 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;
398 FullFS = "+64bit,+sse2," + FullFS;
400 FullFS = "+64bit,+sse2";
403 // If feature string is not empty, parse features string.
404 ParseSubtargetFeatures(CPUName, FullFS);
406 if (CPUName.empty()) {
407 #if defined (__x86_64__) || defined(__i386__)
408 CPUName = sys::getHostCPUName();
413 // Otherwise, use CPUID to auto-detect feature set.
414 AutoDetectSubtargetFeatures();
416 // Make sure 64-bit features are available in 64-bit mode.
418 HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
419 HasCMov = true; ToggleFeature(X86::FeatureCMOV);
421 if (X86SSELevel < SSE2) {
423 ToggleFeature(X86::FeatureSSE1);
424 ToggleFeature(X86::FeatureSSE2);
429 // CPUName may have been set by the CPU detection code. Make sure the
430 // new MCSchedModel is used.
431 InitMCProcessorInfo(CPUName, FS);
433 if (X86ProcFamily == IntelAtom)
434 PostRAScheduler = true;
436 InstrItins = getInstrItineraryForCPU(CPUName);
438 // It's important to keep the MCSubtargetInfo feature bits in sync with
439 // target data structure which is shared with MC code emitter, etc.
441 ToggleFeature(X86::Mode64Bit);
443 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
444 << ", 3DNowLevel " << X863DNowLevel
445 << ", 64bit " << HasX86_64 << "\n");
446 assert((!In64BitMode || HasX86_64) &&
447 "64-bit code requested on a subtarget that doesn't support it!");
449 // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
450 // 32 and 64 bit) and for all 64-bit targets.
451 if (StackAlignOverride)
452 stackAlignment = StackAlignOverride;
453 else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
458 void X86Subtarget::initializeEnvironment() {
459 X86SSELevel = NoMMXSSE;
460 X863DNowLevel = NoThreeDNow;
484 HasVectorUAMem = false;
485 HasCmpxchg16b = false;
487 HasSlowDivide = false;
488 PostRAScheduler = false;
489 PadShortFunctions = false;
490 CallRegIndirect = false;
493 // FIXME: this is a known good value for Yonah. How about others?
494 MaxInlineSizeThreshold = 128;
497 X86Subtarget::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)
502 , PICStyle(PICStyles::None)
504 , StackAlignOverride(StackAlignOverride)
505 , In64BitMode(is64Bit) {
506 initializeEnvironment();
507 resetSubtargetFeatures(CPU, FS);
510 bool X86Subtarget::enablePostRAScheduler(
511 CodeGenOpt::Level OptLevel,
512 TargetSubtargetInfo::AntiDepBreakMode& Mode,
513 RegClassVector& CriticalPathRCs) const {
514 Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
515 CriticalPathRCs.clear();
516 return PostRAScheduler && OptLevel >= CodeGenOpt::Default;