The current Intel Atom microarchitecture has a feature whereby when a function
[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 /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
159 /// to immediate address.
160 bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
161   if (In64BitMode)
162     return false;
163   return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
164 }
165
166 void X86Subtarget::AutoDetectSubtargetFeatures() {
167   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
168   unsigned MaxLevel;
169   union {
170     unsigned u[3];
171     char     c[12];
172   } text;
173
174   if (X86_MC::GetCpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
175       MaxLevel < 1)
176     return;
177
178   X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
179
180   if ((EDX >> 15) & 1) { HasCMov = true;      ToggleFeature(X86::FeatureCMOV); }
181   if ((EDX >> 23) & 1) { X86SSELevel = MMX;   ToggleFeature(X86::FeatureMMX);  }
182   if ((EDX >> 25) & 1) { X86SSELevel = SSE1;  ToggleFeature(X86::FeatureSSE1); }
183   if ((EDX >> 26) & 1) { X86SSELevel = SSE2;  ToggleFeature(X86::FeatureSSE2); }
184   if (ECX & 0x1)       { X86SSELevel = SSE3;  ToggleFeature(X86::FeatureSSE3); }
185   if ((ECX >> 9)  & 1) { X86SSELevel = SSSE3; ToggleFeature(X86::FeatureSSSE3);}
186   if ((ECX >> 19) & 1) { X86SSELevel = SSE41; ToggleFeature(X86::FeatureSSE41);}
187   if ((ECX >> 20) & 1) { X86SSELevel = SSE42; ToggleFeature(X86::FeatureSSE42);}
188   if ((ECX >> 28) & 1) { X86SSELevel = AVX;   ToggleFeature(X86::FeatureAVX); }
189
190   bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
191   bool IsAMD   = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
192
193   if ((ECX >> 1) & 0x1) {
194     HasPCLMUL = true;
195     ToggleFeature(X86::FeaturePCLMUL);
196   }
197   if ((ECX >> 12) & 0x1) {
198     HasFMA = true;
199     ToggleFeature(X86::FeatureFMA);
200   }
201   if (IsIntel && ((ECX >> 22) & 0x1)) {
202     HasMOVBE = true;
203     ToggleFeature(X86::FeatureMOVBE);
204   }
205   if ((ECX >> 23) & 0x1) {
206     HasPOPCNT = true;
207     ToggleFeature(X86::FeaturePOPCNT);
208   }
209   if ((ECX >> 25) & 0x1) {
210     HasAES = true;
211     ToggleFeature(X86::FeatureAES);
212   }
213   if ((ECX >> 29) & 0x1) {
214     HasF16C = true;
215     ToggleFeature(X86::FeatureF16C);
216   }
217   if (IsIntel && ((ECX >> 30) & 0x1)) {
218     HasRDRAND = true;
219     ToggleFeature(X86::FeatureRDRAND);
220   }
221
222   if ((ECX >> 13) & 0x1) {
223     HasCmpxchg16b = true;
224     ToggleFeature(X86::FeatureCMPXCHG16B);
225   }
226
227   if (IsIntel || IsAMD) {
228     // Determine if bit test memory instructions are slow.
229     unsigned Family = 0;
230     unsigned Model  = 0;
231     X86_MC::DetectFamilyModel(EAX, Family, Model);
232     if (IsAMD || (Family == 6 && Model >= 13)) {
233       IsBTMemSlow = true;
234       ToggleFeature(X86::FeatureSlowBTMem);
235     }
236
237     // If it's an Intel chip since Nehalem and not an Atom chip, unaligned
238     // memory access is fast. We hard code model numbers here because they
239     // aren't strictly increasing for Intel chips it seems.
240     if (IsIntel &&
241         ((Family == 6 && Model == 0x1E) || // Nehalem: Clarksfield, Lynnfield,
242                                            //          Jasper Froest
243          (Family == 6 && Model == 0x1A) || // Nehalem: Bloomfield, Nehalem-EP
244          (Family == 6 && Model == 0x2E) || // Nehalem: Nehalem-EX
245          (Family == 6 && Model == 0x25) || // Westmere: Arrandale, Clarksdale
246          (Family == 6 && Model == 0x2C) || // Westmere: Gulftown, Westmere-EP
247          (Family == 6 && Model == 0x2F) || // Westmere: Westmere-EX
248          (Family == 6 && Model == 0x2A) || // SandyBridge
249          (Family == 6 && Model == 0x2D) || // SandyBridge: SandyBridge-E*
250          (Family == 6 && Model == 0x3A))) {// IvyBridge
251       IsUAMemFast = true;
252       ToggleFeature(X86::FeatureFastUAMem);
253     }
254
255     // Set processor type. Currently only Atom is detected.
256     if (Family == 6 &&
257         (Model == 28 || Model == 38 || Model == 39
258          || Model == 53 || Model == 54)) {
259       X86ProcFamily = IntelAtom;
260
261       UseLeaForSP = true;
262       ToggleFeature(X86::FeatureLeaForSP);
263     }
264
265     unsigned MaxExtLevel;
266     X86_MC::GetCpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
267
268     if (MaxExtLevel >= 0x80000001) {
269       X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
270       if ((EDX >> 29) & 0x1) {
271         HasX86_64 = true;
272         ToggleFeature(X86::Feature64Bit);
273       }
274       if ((ECX >> 5) & 0x1) {
275         HasLZCNT = true;
276         ToggleFeature(X86::FeatureLZCNT);
277       }
278       if (IsAMD) {
279         if ((ECX >> 6) & 0x1) {
280           HasSSE4A = true;
281           ToggleFeature(X86::FeatureSSE4A);
282         }
283         if ((ECX >> 11) & 0x1) {
284           HasXOP = true;
285           ToggleFeature(X86::FeatureXOP);
286         }
287         if ((ECX >> 16) & 0x1) {
288           HasFMA4 = true;
289           ToggleFeature(X86::FeatureFMA4);
290         }
291       }
292     }
293   }
294
295   if (MaxLevel >= 7) {
296     if (!X86_MC::GetCpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX)) {
297       if (IsIntel && (EBX & 0x1)) {
298         HasFSGSBase = true;
299         ToggleFeature(X86::FeatureFSGSBase);
300       }
301       if ((EBX >> 3) & 0x1) {
302         HasBMI = true;
303         ToggleFeature(X86::FeatureBMI);
304       }
305       if (IsIntel && ((EBX >> 5) & 0x1)) {
306         X86SSELevel = AVX2;
307         ToggleFeature(X86::FeatureAVX2);
308       }
309       if (IsIntel && ((EBX >> 8) & 0x1)) {
310         HasBMI2 = true;
311         ToggleFeature(X86::FeatureBMI2);
312       }
313       if (IsIntel && ((EBX >> 11) & 0x1)) {
314         HasRTM = true;
315         ToggleFeature(X86::FeatureRTM);
316       }
317     }
318   }
319 }
320
321 X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
322                            const std::string &FS,
323                            unsigned StackAlignOverride, bool is64Bit)
324   : X86GenSubtargetInfo(TT, CPU, FS)
325   , X86ProcFamily(Others)
326   , PICStyle(PICStyles::None)
327   , X86SSELevel(NoMMXSSE)
328   , X863DNowLevel(NoThreeDNow)
329   , HasCMov(false)
330   , HasX86_64(false)
331   , HasPOPCNT(false)
332   , HasSSE4A(false)
333   , HasAES(false)
334   , HasPCLMUL(false)
335   , HasFMA(false)
336   , HasFMA4(false)
337   , HasXOP(false)
338   , HasMOVBE(false)
339   , HasRDRAND(false)
340   , HasF16C(false)
341   , HasFSGSBase(false)
342   , HasLZCNT(false)
343   , HasBMI(false)
344   , HasBMI2(false)
345   , HasRTM(false)
346   , IsBTMemSlow(false)
347   , IsUAMemFast(false)
348   , HasVectorUAMem(false)
349   , HasCmpxchg16b(false)
350   , UseLeaForSP(false)
351   , HasSlowDivide(false)
352   , PostRAScheduler(false)
353   , PadShortFunctions(false)
354   , stackAlignment(4)
355   // FIXME: this is a known good value for Yonah. How about others?
356   , MaxInlineSizeThreshold(128)
357   , TargetTriple(TT)
358   , In64BitMode(is64Bit) {
359   // Determine default and user specified characteristics
360   std::string CPUName = CPU;
361   if (!FS.empty() || !CPU.empty()) {
362     if (CPUName.empty()) {
363 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
364     || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
365       CPUName = sys::getHostCPUName();
366 #else
367       CPUName = "generic";
368 #endif
369     }
370
371     // Make sure 64-bit features are available in 64-bit mode. (But make sure
372     // SSE2 can be turned off explicitly.)
373     std::string FullFS = FS;
374     if (In64BitMode) {
375       if (!FullFS.empty())
376         FullFS = "+64bit,+sse2," + FullFS;
377       else
378         FullFS = "+64bit,+sse2";
379     }
380
381     // If feature string is not empty, parse features string.
382     ParseSubtargetFeatures(CPUName, FullFS);
383   } else {
384     if (CPUName.empty()) {
385 #if defined (__x86_64__) || defined(__i386__)
386       CPUName = sys::getHostCPUName();
387 #else
388       CPUName = "generic";
389 #endif
390     }
391     // Otherwise, use CPUID to auto-detect feature set.
392     AutoDetectSubtargetFeatures();
393
394     // Make sure 64-bit features are available in 64-bit mode.
395     if (In64BitMode) {
396       HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
397       HasCMov = true;   ToggleFeature(X86::FeatureCMOV);
398
399       if (X86SSELevel < SSE2) {
400         X86SSELevel = SSE2;
401         ToggleFeature(X86::FeatureSSE1);
402         ToggleFeature(X86::FeatureSSE2);
403       }
404     }
405   }
406
407   // CPUName may have been set by the CPU detection code. Make sure the
408   // new MCSchedModel is used.
409   InitMCProcessorInfo(CPUName, FS);
410
411   if (X86ProcFamily == IntelAtom)
412     PostRAScheduler = true;
413
414   InstrItins = getInstrItineraryForCPU(CPUName);
415
416   // It's important to keep the MCSubtargetInfo feature bits in sync with
417   // target data structure which is shared with MC code emitter, etc.
418   if (In64BitMode)
419     ToggleFeature(X86::Mode64Bit);
420
421   DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
422                << ", 3DNowLevel " << X863DNowLevel
423                << ", 64bit " << HasX86_64 << "\n");
424   assert((!In64BitMode || HasX86_64) &&
425          "64-bit code requested on a subtarget that doesn't support it!");
426
427   // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
428   // 32 and 64 bit) and for all 64-bit targets.
429   if (StackAlignOverride)
430     stackAlignment = StackAlignOverride;
431   else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
432            In64BitMode)
433     stackAlignment = 16;
434 }
435
436 bool X86Subtarget::enablePostRAScheduler(
437            CodeGenOpt::Level OptLevel,
438            TargetSubtargetInfo::AntiDepBreakMode& Mode,
439            RegClassVector& CriticalPathRCs) const {
440   Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
441   CriticalPathRCs.clear();
442   return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
443 }