1 //===-- X86Subtarget.cpp - X86 Subtarget Information ------------*- C++ -*-===//
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 TargetSubtarget.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "X86GenSubtarget.inc"
18 #include "llvm/GlobalValue.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/System/Host.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/ADT/SmallVector.h"
29 DoPromote16Bit("promote-16bit", cl::Hidden,
30 cl::desc("Promote 16-bit instructions"));
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 // Materializable GVs (in JIT lazy compilation mode) do not require an
62 // extra load from stub.
63 bool isDecl = GV->isDeclaration() && !GV->isMaterializable();
65 // X86-64 in PIC mode.
66 if (isPICStyleRIPRel()) {
67 // Large model never uses stubs.
68 if (TM.getCodeModel() == CodeModel::Large)
69 return X86II::MO_NO_FLAG;
71 if (isTargetDarwin()) {
72 // If symbol visibility is hidden, the extra load is not needed if
73 // target is x86-64 or the symbol is definitely defined in the current
75 if (GV->hasDefaultVisibility() &&
76 (isDecl || GV->isWeakForLinker()))
77 return X86II::MO_GOTPCREL;
79 assert(isTargetELF() && "Unknown rip-relative target");
81 // Extra load is needed for all externally visible.
82 if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
83 return X86II::MO_GOTPCREL;
86 return X86II::MO_NO_FLAG;
89 if (isPICStyleGOT()) { // 32-bit ELF targets.
90 // Extra load is needed for all externally visible.
91 if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
92 return X86II::MO_GOTOFF;
96 if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
97 // Determine whether we have a stub reference and/or whether the reference
98 // is relative to the PIC base or not.
100 // If this is a strong reference to a definition, it is definitely not
102 if (!isDecl && !GV->isWeakForLinker())
103 return X86II::MO_PIC_BASE_OFFSET;
105 // Unless we have a symbol with hidden visibility, we have to go through a
106 // normal $non_lazy_ptr stub because this symbol might be resolved late.
107 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
108 return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
110 // If symbol visibility is hidden, we have a stub for common symbol
111 // references and external declarations.
112 if (isDecl || GV->hasCommonLinkage()) {
113 // Hidden $non_lazy_ptr reference.
114 return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
117 // Otherwise, no stub.
118 return X86II::MO_PIC_BASE_OFFSET;
121 if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
122 // Determine whether we have a stub reference.
124 // If this is a strong reference to a definition, it is definitely not
126 if (!isDecl && !GV->isWeakForLinker())
127 return X86II::MO_NO_FLAG;
129 // Unless we have a symbol with hidden visibility, we have to go through a
130 // normal $non_lazy_ptr stub because this symbol might be resolved late.
131 if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
132 return X86II::MO_DARWIN_NONLAZY;
134 // Otherwise, no stub.
135 return X86II::MO_NO_FLAG;
138 // Direct static reference to global.
139 return X86II::MO_NO_FLAG;
143 /// getBZeroEntry - This function returns the name of a function which has an
144 /// interface like the non-standard bzero function, if such a function exists on
145 /// the current subtarget and it is considered prefereable over memset with zero
146 /// passed as the second argument. Otherwise it returns null.
147 const char *X86Subtarget::getBZeroEntry() const {
148 // Darwin 10 has a __bzero entry point for this purpose.
149 if (getDarwinVers() >= 10)
155 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
156 /// to immediate address.
157 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
160 return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
163 /// getSpecialAddressLatency - For targets where it is beneficial to
164 /// backschedule instructions that compute addresses, return a value
165 /// indicating the number of scheduling cycles of backscheduling that
166 /// should be attempted.
167 unsigned X86Subtarget::getSpecialAddressLatency() const {
168 // For x86 out-of-order targets, back-schedule address computations so
169 // that loads and stores aren't blocked.
170 // This value was chosen arbitrarily.
174 /// GetCpuIDAndInfo - Execute the specified cpuid and return the 4 values in the
175 /// specified arguments. If we can't run cpuid on the host, return true.
176 static bool GetCpuIDAndInfo(unsigned value, unsigned *rEAX,
177 unsigned *rEBX, unsigned *rECX, unsigned *rEDX) {
178 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
179 #if defined(__GNUC__)
180 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
181 asm ("movq\t%%rbx, %%rsi\n\t"
183 "xchgq\t%%rbx, %%rsi\n\t"
190 #elif defined(_MSC_VER)
192 __cpuid(registers, value);
193 *rEAX = registers[0];
194 *rEBX = registers[1];
195 *rECX = registers[2];
196 *rEDX = registers[3];
199 #elif defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
200 #if defined(__GNUC__)
201 asm ("movl\t%%ebx, %%esi\n\t"
203 "xchgl\t%%ebx, %%esi\n\t"
210 #elif defined(_MSC_VER)
215 mov dword ptr [esi],eax
217 mov dword ptr [esi],ebx
219 mov dword ptr [esi],ecx
221 mov dword ptr [esi],edx
229 static void DetectFamilyModel(unsigned EAX, unsigned &Family, unsigned &Model) {
230 Family = (EAX >> 8) & 0xf; // Bits 8 - 11
231 Model = (EAX >> 4) & 0xf; // Bits 4 - 7
232 if (Family == 6 || Family == 0xf) {
234 // Examine extended family ID if family ID is F.
235 Family += (EAX >> 20) & 0xff; // Bits 20 - 27
236 // Examine extended model ID if family ID is 6 or F.
237 Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
241 void X86Subtarget::AutoDetectSubtargetFeatures() {
242 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
248 if (GetCpuIDAndInfo(0, &EAX, text.u+0, text.u+2, text.u+1))
251 GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
253 if ((EDX >> 15) & 1) HasCMov = true;
254 if ((EDX >> 23) & 1) X86SSELevel = MMX;
255 if ((EDX >> 25) & 1) X86SSELevel = SSE1;
256 if ((EDX >> 26) & 1) X86SSELevel = SSE2;
257 if (ECX & 0x1) X86SSELevel = SSE3;
258 if ((ECX >> 9) & 1) X86SSELevel = SSSE3;
259 if ((ECX >> 19) & 1) X86SSELevel = SSE41;
260 if ((ECX >> 20) & 1) X86SSELevel = SSE42;
262 bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
263 bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
265 HasFMA3 = IsIntel && ((ECX >> 12) & 0x1);
266 HasAVX = ((ECX >> 28) & 0x1);
267 HasAES = IsIntel && ((ECX >> 25) & 0x1);
269 if (IsIntel || IsAMD) {
270 // Determine if bit test memory instructions are slow.
273 DetectFamilyModel(EAX, Family, Model);
274 IsBTMemSlow = IsAMD || (Family == 6 && Model >= 13);
275 // If it's Nehalem, unaligned memory access is fast.
276 if (Family == 15 && Model == 26)
279 GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
280 HasX86_64 = (EDX >> 29) & 0x1;
281 HasSSE4A = IsAMD && ((ECX >> 6) & 0x1);
282 HasFMA4 = IsAMD && ((ECX >> 16) & 0x1);
286 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &FS,
288 : PICStyle(PICStyles::None)
289 , X86SSELevel(NoMMXSSE)
290 , X863DNowLevel(NoThreeDNow)
300 , HasVectorUAMem(false)
301 , Promote16Bit(DoPromote16Bit)
304 // FIXME: this is a known good value for Yonah. How about others?
305 , MaxInlineSizeThreshold(128)
307 , TargetType(isELF) { // Default to ELF unless otherwise specified.
309 // default to hard float ABI
310 if (FloatABIType == FloatABI::Default)
311 FloatABIType = FloatABI::Hard;
313 // Determine default and user specified characteristics
315 // If feature string is not empty, parse features string.
316 std::string CPU = sys::getHostCPUName();
317 ParseSubtargetFeatures(FS, CPU);
318 // All X86-64 CPUs also have SSE2, however user might request no SSE via
319 // -mattr, so don't force SSELevel here.
321 // Otherwise, use CPUID to auto-detect feature set.
322 AutoDetectSubtargetFeatures();
323 // Make sure SSE2 is enabled; it is available on all X86-64 CPUs.
324 if (Is64Bit && X86SSELevel < SSE2)
328 // If requesting codegen for X86-64, make sure that 64-bit features
333 // All 64-bit cpus have cmov support.
338 DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
339 << ", 3DNowLevel " << X863DNowLevel
340 << ", 64bit " << HasX86_64 << "\n");
341 assert((!Is64Bit || HasX86_64) &&
342 "64-bit code requested on a subtarget that doesn't support it!");
344 // Set the boolean corresponding to the current target triple, or the default
345 // if one cannot be determined, to true.
346 if (TT.length() > 5) {
348 if ((Pos = TT.find("-darwin")) != std::string::npos) {
349 TargetType = isDarwin;
351 // Compute the darwin version number.
352 if (isdigit(TT[Pos+7]))
353 DarwinVers = atoi(&TT[Pos+7]);
355 DarwinVers = 8; // Minimum supported darwin is Tiger.
356 } else if (TT.find("linux") != std::string::npos) {
357 // Linux doesn't imply ELF, but we don't currently support anything else.
359 } else if (TT.find("cygwin") != std::string::npos) {
360 TargetType = isCygwin;
361 } else if (TT.find("mingw") != std::string::npos) {
362 TargetType = isMingw;
363 } else if (TT.find("win32") != std::string::npos) {
364 TargetType = isWindows;
365 } else if (TT.find("windows") != std::string::npos) {
366 TargetType = isWindows;
367 } else if (TT.find("-cl") != std::string::npos) {
368 TargetType = isDarwin;
373 // Stack alignment is 16 bytes on Darwin (both 32 and 64 bit) and for all 64
375 if (TargetType == isDarwin || Is64Bit)
379 stackAlignment = StackAlignment;