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