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/GlobalValue.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/ADT/SmallVector.h"
26 #define GET_SUBTARGETINFO_TARGET_DESC
27 #define GET_SUBTARGETINFO_CTOR
28 #include "X86GenSubtargetInfo.inc"
36 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
37 /// current subtarget according to how we should reference it in a non-pcrel
39 unsigned char X86Subtarget::
40 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 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
160 /// to immediate address.
161 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
164 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
167 /// getSpecialAddressLatency - For targets where it is beneficial to
168 /// backschedule instructions that compute addresses, return a value
169 /// indicating the number of scheduling cycles of backscheduling that
170 /// should be attempted.
171 unsigned X86Subtarget::getSpecialAddressLatency() const {
172 // For x86 out-of-order targets, back-schedule address computations so
173 // that loads and stores aren't blocked.
174 // This value was chosen arbitrarily.
178 void X86Subtarget::AutoDetectSubtargetFeatures() {
179 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
185 if (X86_MC::GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
188 X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
190 if ((EDX >> 15) & 1) { HasCMov = true; ToggleFeature(X86::FeatureCMOV); }
191 if ((EDX >> 23) & 1) { X86SSELevel = MMX; ToggleFeature(X86::FeatureMMX); }
192 if ((EDX >> 25) & 1) { X86SSELevel = SSE1; ToggleFeature(X86::FeatureSSE1); }
193 if ((EDX >> 26) & 1) { X86SSELevel = SSE2; ToggleFeature(X86::FeatureSSE2); }
194 if (ECX & 0x1) { X86SSELevel = SSE3; ToggleFeature(X86::FeatureSSE3); }
195 if ((ECX >> 9) & 1) { X86SSELevel = SSSE3; ToggleFeature(X86::FeatureSSSE3);}
196 if ((ECX >> 19) & 1) { X86SSELevel = SSE41; ToggleFeature(X86::FeatureSSE41);}
197 if ((ECX >> 20) & 1) { X86SSELevel = SSE42; ToggleFeature(X86::FeatureSSE42);}
198 // FIXME: AVX codegen support is not ready.
199 //if ((ECX >> 28) & 1) { HasAVX = true; ToggleFeature(X86::FeatureAVX); }
201 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
202 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
204 if (IsIntel && ((ECX >> 1) & 0x1)) {
206 ToggleFeature(X86::FeatureCLMUL);
208 if (IsIntel && ((ECX >> 12) & 0x1)) {
210 ToggleFeature(X86::FeatureFMA3);
212 if (IsIntel && ((ECX >> 22) & 0x1)) {
214 ToggleFeature(X86::FeatureMOVBE);
216 if (IsIntel && ((ECX >> 23) & 0x1)) {
218 ToggleFeature(X86::FeaturePOPCNT);
220 if (IsIntel && ((ECX >> 25) & 0x1)) {
222 ToggleFeature(X86::FeatureAES);
224 if (IsIntel && ((ECX >> 29) & 0x1)) {
226 ToggleFeature(X86::FeatureF16C);
228 if (IsIntel && ((ECX >> 30) & 0x1)) {
230 ToggleFeature(X86::FeatureRDRAND);
233 if ((ECX >> 13) & 0x1) {
234 HasCmpxchg16b = true;
235 ToggleFeature(X86::FeatureCMPXCHG16B);
238 if (IsIntel || IsAMD) {
239 // Determine if bit test memory instructions are slow.
242 X86_MC::DetectFamilyModel(EAX, Family, Model);
243 if (IsAMD || (Family == 6 && Model >= 13)) {
245 ToggleFeature(X86::FeatureSlowBTMem);
247 // If it's Nehalem, unaligned memory access is fast.
248 if (Family == 15 && Model == 26) {
250 ToggleFeature(X86::FeatureFastUAMem);
253 X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
254 if ((EDX >> 29) & 0x1) {
256 ToggleFeature(X86::Feature64Bit);
258 if ((ECX >> 5) & 0x1) {
260 ToggleFeature(X86::FeatureLZCNT);
262 if (IsAMD && ((ECX >> 6) & 0x1)) {
264 ToggleFeature(X86::FeatureSSE4A);
266 if (IsAMD && ((ECX >> 16) & 0x1)) {
268 ToggleFeature(X86::FeatureFMA4);
273 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
274 const std::string &FS,
275 unsigned StackAlignOverride, bool is64Bit)
276 : X86GenSubtargetInfo(TT, CPU, FS)
277 , PICStyle(PICStyles::None)
278 , X86SSELevel(NoMMXSSE)
279 , X863DNowLevel(NoThreeDNow)
295 , HasVectorUAMem(false)
296 , HasCmpxchg16b(false)
298 // FIXME: this is a known good value for Yonah. How about others?
299 , MaxInlineSizeThreshold(128)
301 , In64BitMode(is64Bit)
302 , InNaClMode(false) {
303 // Determine default and user specified characteristics
304 if (!FS.empty() || !CPU.empty()) {
305 std::string CPUName = CPU;
306 if (CPUName.empty()) {
307 #if defined (__x86_64__) || defined(__i386__)
308 CPUName = sys::getHostCPUName();
314 // Make sure 64-bit features are available in 64-bit mode. (But make sure
315 // SSE2 can be turned off explicitly.)
316 std::string FullFS = FS;
319 FullFS = "+64bit,+sse2," + FullFS;
321 FullFS = "+64bit,+sse2";
324 // If feature string is not empty, parse features string.
325 ParseSubtargetFeatures(CPUName, FullFS);
327 // Otherwise, use CPUID to auto-detect feature set.
328 AutoDetectSubtargetFeatures();
330 // Make sure 64-bit features are available in 64-bit mode.
332 HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
333 HasCMov = true; ToggleFeature(X86::FeatureCMOV);
335 if (!HasAVX && X86SSELevel < SSE2) {
337 ToggleFeature(X86::FeatureSSE1);
338 ToggleFeature(X86::FeatureSSE2);
343 // It's important to keep the MCSubtargetInfo feature bits in sync with
344 // target data structure which is shared with MC code emitter, etc.
346 ToggleFeature(X86::Mode64Bit);
348 if (isTargetNaCl()) {
350 ToggleFeature(X86::ModeNaCl);
354 X86SSELevel = NoMMXSSE;
356 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
357 << ", 3DNowLevel " << X863DNowLevel
358 << ", 64bit " << HasX86_64 << "\n");
359 assert((!In64BitMode || HasX86_64) &&
360 "64-bit code requested on a subtarget that doesn't support it!");
362 if(EnableSegmentedStacks && !isTargetELF())
363 report_fatal_error("Segmented stacks are only implemented on ELF.");
365 // Stack alignment is 16 bytes on Darwin, FreeBSD, Linux and Solaris (both
366 // 32 and 64 bit) and for all 64-bit targets.
367 if (StackAlignOverride)
368 stackAlignment = StackAlignOverride;
369 else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
370 isTargetSolaris() || In64BitMode)