Make helpers static. Add missing include so LLVMInitializeObjCARCOpts gets C linkage.
[oota-llvm.git] / lib / Target / X86 / X86Subtarget.cpp
1 //===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the X86 specific subclass of TargetSubtargetInfo.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "subtarget"
15 #include "X86Subtarget.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetOptions.h"
24
25 #define GET_SUBTARGETINFO_TARGET_DESC
26 #define GET_SUBTARGETINFO_CTOR
27 #include "X86GenSubtargetInfo.inc"
28
29 using namespace llvm;
30
31 #if defined(_MSC_VER)
32 #include <intrin.h>
33 #endif
34
35 /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
36 /// current subtarget according to how we should reference it in a non-pcrel
37 /// context.
38 unsigned char X86Subtarget::
39 ClassifyBlockAddressReference() const {
40   if (isPICStyleGOT())    // 32-bit ELF targets.
41     return X86II::MO_GOTOFF;
42
43   if (isPICStyleStubPIC())   // Darwin/32 in PIC mode.
44     return X86II::MO_PIC_BASE_OFFSET;
45
46   // Direct static reference to label.
47   return X86II::MO_NO_FLAG;
48 }
49
50 /// ClassifyGlobalReference - Classify a global variable reference for the
51 /// current subtarget according to how we should reference it in a non-pcrel
52 /// context.
53 unsigned char X86Subtarget::
54 ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
55   // DLLImport only exists on windows, it is implemented as a load from a
56   // DLLIMPORT stub.
57   if (GV->hasDLLImportLinkage())
58     return X86II::MO_DLLIMPORT;
59
60   // Determine whether this is a reference to a definition or a declaration.
61   // Materializable GVs (in JIT lazy compilation mode) do not require an extra
62   // load from stub.
63   bool isDecl = GV->hasAvailableExternallyLinkage();
64   if (GV->isDeclaration() && !GV->isMaterializable())
65     isDecl = true;
66
67   // X86-64 in PIC mode.
68   if (isPICStyleRIPRel()) {
69     // Large model never uses stubs.
70     if (TM.getCodeModel() == CodeModel::Large)
71       return X86II::MO_NO_FLAG;
72
73     if (isTargetDarwin()) {
74       // If symbol visibility is hidden, the extra load is not needed if
75       // target is x86-64 or the symbol is definitely defined in the current
76       // translation unit.
77       if (GV->hasDefaultVisibility() &&
78           (isDecl || GV->isWeakForLinker()))
79         return X86II::MO_GOTPCREL;
80     } else if (!isTargetWin64()) {
81       assert(isTargetELF() && "Unknown rip-relative target");
82
83       // Extra load is needed for all externally visible.
84       if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
85         return X86II::MO_GOTPCREL;
86     }
87
88     return X86II::MO_NO_FLAG;
89   }
90
91   if (isPICStyleGOT()) {   // 32-bit ELF targets.
92     // Extra load is needed for all externally visible.
93     if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
94       return X86II::MO_GOTOFF;
95     return X86II::MO_GOT;
96   }
97
98   if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
99     // Determine whether we have a stub reference and/or whether the reference
100     // is relative to the PIC base or not.
101
102     // If this is a strong reference to a definition, it is definitely not
103     // through a stub.
104     if (!isDecl && !GV->isWeakForLinker())
105       return X86II::MO_PIC_BASE_OFFSET;
106
107     // Unless we have a symbol with hidden visibility, we have to go through a
108     // normal $non_lazy_ptr stub because this symbol might be resolved late.
109     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
110       return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
111
112     // If symbol visibility is hidden, we have a stub for common symbol
113     // references and external declarations.
114     if (isDecl || GV->hasCommonLinkage()) {
115       // Hidden $non_lazy_ptr reference.
116       return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
117     }
118
119     // Otherwise, no stub.
120     return X86II::MO_PIC_BASE_OFFSET;
121   }
122
123   if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
124     // Determine whether we have a stub reference.
125
126     // If this is a strong reference to a definition, it is definitely not
127     // through a stub.
128     if (!isDecl && !GV->isWeakForLinker())
129       return X86II::MO_NO_FLAG;
130
131     // Unless we have a symbol with hidden visibility, we have to go through a
132     // normal $non_lazy_ptr stub because this symbol might be resolved late.
133     if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
134       return X86II::MO_DARWIN_NONLAZY;
135
136     // Otherwise, no stub.
137     return X86II::MO_NO_FLAG;
138   }
139
140   // Direct static reference to global.
141   return X86II::MO_NO_FLAG;
142 }
143
144
145 /// getBZeroEntry - This function returns the name of a function which has an
146 /// interface like the non-standard bzero function, if such a function exists on
147 /// the current subtarget and it is considered prefereable over memset with zero
148 /// passed as the second argument. Otherwise it returns null.
149 const char *X86Subtarget::getBZeroEntry() const {
150   // Darwin 10 has a __bzero entry point for this purpose.
151   if (getTargetTriple().isMacOSX() &&
152       !getTargetTriple().isMacOSXVersionLT(10, 6))
153     return "__bzero";
154
155   return 0;
156 }
157
158 bool X86Subtarget::hasSinCos() const {
159   return getTargetTriple().isMacOSX() &&
160     !getTargetTriple().isMacOSXVersionLT(10, 9) &&
161     is64Bit();
162 }
163
164 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
165 /// to immediate address.
166 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
167   if (In64BitMode)
168     return false;
169   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
170 }
171
172 void X86Subtarget::AutoDetectSubtargetFeatures() {
173   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
174   unsigned MaxLevel;
175   union {
176     unsigned u[3];
177     char     c[12];
178   } text;
179
180   if (X86_MC::GetCpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
181       MaxLevel < 1)
182     return;
183
184   X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
185
186   if ((EDX >> 15) & 1) { HasCMov = true;      ToggleFeature(X86::FeatureCMOV); }
187   if ((EDX >> 23) & 1) { X86SSELevel = MMX;   ToggleFeature(X86::FeatureMMX);  }
188   if ((EDX >> 25) & 1) { X86SSELevel = SSE1;  ToggleFeature(X86::FeatureSSE1); }
189   if ((EDX >> 26) & 1) { X86SSELevel = SSE2;  ToggleFeature(X86::FeatureSSE2); }
190   if (ECX & 0x1)       { X86SSELevel = SSE3;  ToggleFeature(X86::FeatureSSE3); }
191   if ((ECX >> 9)  & 1) { X86SSELevel = SSSE3; ToggleFeature(X86::FeatureSSSE3);}
192   if ((ECX >> 19) & 1) { X86SSELevel = SSE41; ToggleFeature(X86::FeatureSSE41);}
193   if ((ECX >> 20) & 1) { X86SSELevel = SSE42; ToggleFeature(X86::FeatureSSE42);}
194   if ((ECX >> 28) & 1) { X86SSELevel = AVX;   ToggleFeature(X86::FeatureAVX); }
195
196   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
197   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
198
199   if ((ECX >> 1) & 0x1) {
200     HasPCLMUL = true;
201     ToggleFeature(X86::FeaturePCLMUL);
202   }
203   if ((ECX >> 12) & 0x1) {
204     HasFMA = true;
205     ToggleFeature(X86::FeatureFMA);
206   }
207   if (IsIntel && ((ECX >> 22) & 0x1)) {
208     HasMOVBE = true;
209     ToggleFeature(X86::FeatureMOVBE);
210   }
211   if ((ECX >> 23) & 0x1) {
212     HasPOPCNT = true;
213     ToggleFeature(X86::FeaturePOPCNT);
214   }
215   if ((ECX >> 25) & 0x1) {
216     HasAES = true;
217     ToggleFeature(X86::FeatureAES);
218   }
219   if ((ECX >> 29) & 0x1) {
220     HasF16C = true;
221     ToggleFeature(X86::FeatureF16C);
222   }
223   if (IsIntel && ((ECX >> 30) & 0x1)) {
224     HasRDRAND = true;
225     ToggleFeature(X86::FeatureRDRAND);
226   }
227
228   if ((ECX >> 13) & 0x1) {
229     HasCmpxchg16b = true;
230     ToggleFeature(X86::FeatureCMPXCHG16B);
231   }
232
233   if (IsIntel || IsAMD) {
234     // Determine if bit test memory instructions are slow.
235     unsigned Family = 0;
236     unsigned Model  = 0;
237     X86_MC::DetectFamilyModel(EAX, Family, Model);
238     if (IsAMD || (Family == 6 && Model >= 13)) {
239       IsBTMemSlow = true;
240       ToggleFeature(X86::FeatureSlowBTMem);
241     }
242
243     // If it's an Intel chip since Nehalem and not an Atom chip, unaligned
244     // memory access is fast. We hard code model numbers here because they
245     // aren't strictly increasing for Intel chips it seems.
246     if (IsIntel &&
247         ((Family == 6 && Model == 0x1E) || // Nehalem: Clarksfield, Lynnfield,
248                                            //          Jasper Froest
249          (Family == 6 && Model == 0x1A) || // Nehalem: Bloomfield, Nehalem-EP
250          (Family == 6 && Model == 0x2E) || // Nehalem: Nehalem-EX
251          (Family == 6 && Model == 0x25) || // Westmere: Arrandale, Clarksdale
252          (Family == 6 && Model == 0x2C) || // Westmere: Gulftown, Westmere-EP
253          (Family == 6 && Model == 0x2F) || // Westmere: Westmere-EX
254          (Family == 6 && Model == 0x2A) || // SandyBridge
255          (Family == 6 && Model == 0x2D) || // SandyBridge: SandyBridge-E*
256          (Family == 6 && Model == 0x3A))) {// IvyBridge
257       IsUAMemFast = true;
258       ToggleFeature(X86::FeatureFastUAMem);
259     }
260
261     // Set processor type. Currently only Atom is detected.
262     if (Family == 6 &&
263         (Model == 28 || Model == 38 || Model == 39
264          || Model == 53 || Model == 54)) {
265       X86ProcFamily = IntelAtom;
266
267       UseLeaForSP = true;
268       ToggleFeature(X86::FeatureLeaForSP);
269     }
270
271     unsigned MaxExtLevel;
272     X86_MC::GetCpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
273
274     if (MaxExtLevel >= 0x80000001) {
275       X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
276       if ((EDX >> 29) & 0x1) {
277         HasX86_64 = true;
278         ToggleFeature(X86::Feature64Bit);
279       }
280       if ((ECX >> 5) & 0x1) {
281         HasLZCNT = true;
282         ToggleFeature(X86::FeatureLZCNT);
283       }
284       if (IsAMD) {
285         if ((ECX >> 6) & 0x1) {
286           HasSSE4A = true;
287           ToggleFeature(X86::FeatureSSE4A);
288         }
289         if ((ECX >> 11) & 0x1) {
290           HasXOP = true;
291           ToggleFeature(X86::FeatureXOP);
292         }
293         if ((ECX >> 16) & 0x1) {
294           HasFMA4 = true;
295           ToggleFeature(X86::FeatureFMA4);
296         }
297       }
298     }
299   }
300
301   if (MaxLevel >= 7) {
302     if (!X86_MC::GetCpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX)) {
303       if (IsIntel && (EBX & 0x1)) {
304         HasFSGSBase = true;
305         ToggleFeature(X86::FeatureFSGSBase);
306       }
307       if ((EBX >> 3) & 0x1) {
308         HasBMI = true;
309         ToggleFeature(X86::FeatureBMI);
310       }
311       if (IsIntel && ((EBX >> 5) & 0x1)) {
312         X86SSELevel = AVX2;
313         ToggleFeature(X86::FeatureAVX2);
314       }
315       if (IsIntel && ((EBX >> 8) & 0x1)) {
316         HasBMI2 = true;
317         ToggleFeature(X86::FeatureBMI2);
318       }
319       if (IsIntel && ((EBX >> 11) & 0x1)) {
320         HasRTM = true;
321         ToggleFeature(X86::FeatureRTM);
322       }
323     }
324   }
325 }
326
327 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
328                            const std::string &FS,
329                            unsigned StackAlignOverride, bool is64Bit)
330   : X86GenSubtargetInfo(TT, CPU, FS)
331   , X86ProcFamily(Others)
332   , PICStyle(PICStyles::None)
333   , X86SSELevel(NoMMXSSE)
334   , X863DNowLevel(NoThreeDNow)
335   , HasCMov(false)
336   , HasX86_64(false)
337   , HasPOPCNT(false)
338   , HasSSE4A(false)
339   , HasAES(false)
340   , HasPCLMUL(false)
341   , HasFMA(false)
342   , HasFMA4(false)
343   , HasXOP(false)
344   , HasMOVBE(false)
345   , HasRDRAND(false)
346   , HasF16C(false)
347   , HasFSGSBase(false)
348   , HasLZCNT(false)
349   , HasBMI(false)
350   , HasBMI2(false)
351   , HasRTM(false)
352   , HasADX(false)
353   , IsBTMemSlow(false)
354   , IsUAMemFast(false)
355   , HasVectorUAMem(false)
356   , HasCmpxchg16b(false)
357   , UseLeaForSP(false)
358   , HasSlowDivide(false)
359   , PostRAScheduler(false)
360   , PadShortFunctions(false)
361   , stackAlignment(4)
362   // FIXME: this is a known good value for Yonah. How about others?
363   , MaxInlineSizeThreshold(128)
364   , TargetTriple(TT)
365   , In64BitMode(is64Bit) {
366   // Determine default and user specified characteristics
367   std::string CPUName = CPU;
368   if (!FS.empty() || !CPU.empty()) {
369     if (CPUName.empty()) {
370 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
371     || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
372       CPUName = sys::getHostCPUName();
373 #else
374       CPUName = "generic";
375 #endif
376     }
377
378     // Make sure 64-bit features are available in 64-bit mode. (But make sure
379     // SSE2 can be turned off explicitly.)
380     std::string FullFS = FS;
381     if (In64BitMode) {
382       if (!FullFS.empty())
383         FullFS = "+64bit,+sse2," + FullFS;
384       else
385         FullFS = "+64bit,+sse2";
386     }
387
388     // If feature string is not empty, parse features string.
389     ParseSubtargetFeatures(CPUName, FullFS);
390   } else {
391     if (CPUName.empty()) {
392 #if defined (__x86_64__) || defined(__i386__)
393       CPUName = sys::getHostCPUName();
394 #else
395       CPUName = "generic";
396 #endif
397     }
398     // Otherwise, use CPUID to auto-detect feature set.
399     AutoDetectSubtargetFeatures();
400
401     // Make sure 64-bit features are available in 64-bit mode.
402     if (In64BitMode) {
403       HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
404       HasCMov = true;   ToggleFeature(X86::FeatureCMOV);
405
406       if (X86SSELevel < SSE2) {
407         X86SSELevel = SSE2;
408         ToggleFeature(X86::FeatureSSE1);
409         ToggleFeature(X86::FeatureSSE2);
410       }
411     }
412   }
413
414   // CPUName may have been set by the CPU detection code. Make sure the
415   // new MCSchedModel is used.
416   InitMCProcessorInfo(CPUName, FS);
417
418   if (X86ProcFamily == IntelAtom)
419     PostRAScheduler = true;
420
421   InstrItins = getInstrItineraryForCPU(CPUName);
422
423   // It's important to keep the MCSubtargetInfo feature bits in sync with
424   // target data structure which is shared with MC code emitter, etc.
425   if (In64BitMode)
426     ToggleFeature(X86::Mode64Bit);
427
428   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
429                << ", 3DNowLevel " << X863DNowLevel
430                << ", 64bit " << HasX86_64 << "\n");
431   assert((!In64BitMode || HasX86_64) &&
432          "64-bit code requested on a subtarget that doesn't support it!");
433
434   // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
435   // 32 and 64 bit) and for all 64-bit targets.
436   if (StackAlignOverride)
437     stackAlignment = StackAlignOverride;
438   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
439            In64BitMode)
440     stackAlignment = 16;
441 }
442
443 bool X86Subtarget::enablePostRAScheduler(
444            CodeGenOpt::Level OptLevel,
445            TargetSubtargetInfo::AntiDepBreakMode& Mode,
446            RegClassVector& CriticalPathRCs) const {
447   Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
448   CriticalPathRCs.clear();
449   return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
450 }