27429d050bd5924cfab85acbc1dfce8279ffde9c
[oota-llvm.git] / lib / Target / X86 / X86Subtarget.h
1 //===-- X86Subtarget.h - Define Subtarget for the X86 ----------*- C++ -*--===//
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 declares the X86 specific subclass of TargetSubtargetInfo.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_TARGET_X86_X86SUBTARGET_H
15 #define LLVM_LIB_TARGET_X86_X86SUBTARGET_H
16
17 #include "X86FrameLowering.h"
18 #include "X86ISelLowering.h"
19 #include "X86InstrInfo.h"
20 #include "X86SelectionDAGInfo.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/Target/TargetSubtargetInfo.h"
24 #include <string>
25
26 #define GET_SUBTARGETINFO_HEADER
27 #include "X86GenSubtargetInfo.inc"
28
29 namespace llvm {
30 class GlobalValue;
31 class StringRef;
32 class TargetMachine;
33
34 /// The X86 backend supports a number of different styles of PIC.
35 ///
36 namespace PICStyles {
37 enum Style {
38   StubPIC,          // Used on i386-darwin in -fPIC mode.
39   StubDynamicNoPIC, // Used on i386-darwin in -mdynamic-no-pic mode.
40   GOT,              // Used on many 32-bit unices in -fPIC mode.
41   RIPRel,           // Used on X86-64 when not in -static mode.
42   None              // Set when in -static mode (not PIC or DynamicNoPIC mode).
43 };
44 }
45
46 class X86Subtarget final : public X86GenSubtargetInfo {
47
48 protected:
49   enum X86SSEEnum {
50     NoMMXSSE, MMX, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2, AVX512F
51   };
52
53   enum X863DNowEnum {
54     NoThreeDNow, ThreeDNow, ThreeDNowA
55   };
56
57   enum X86ProcFamilyEnum {
58     Others, IntelAtom, IntelSLM
59   };
60
61   /// X86 processor family: Intel Atom, and others
62   X86ProcFamilyEnum X86ProcFamily;
63
64   /// Which PIC style to use
65   PICStyles::Style PICStyle;
66
67   /// MMX, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, or none supported.
68   X86SSEEnum X86SSELevel;
69
70   /// 3DNow, 3DNow Athlon, or none supported.
71   X863DNowEnum X863DNowLevel;
72
73   /// True if this processor has conditional move instructions
74   /// (generally pentium pro+).
75   bool HasCMov;
76
77   /// True if the processor supports X86-64 instructions.
78   bool HasX86_64;
79
80   /// True if the processor supports POPCNT.
81   bool HasPOPCNT;
82
83   /// True if the processor supports SSE4A instructions.
84   bool HasSSE4A;
85
86   /// Target has AES instructions
87   bool HasAES;
88
89   /// Target has carry-less multiplication
90   bool HasPCLMUL;
91
92   /// Target has 3-operand fused multiply-add
93   bool HasFMA;
94
95   /// Target has 4-operand fused multiply-add
96   bool HasFMA4;
97
98   /// Target has XOP instructions
99   bool HasXOP;
100
101   /// Target has TBM instructions.
102   bool HasTBM;
103
104   /// True if the processor has the MOVBE instruction.
105   bool HasMOVBE;
106
107   /// True if the processor has the RDRAND instruction.
108   bool HasRDRAND;
109
110   /// Processor has 16-bit floating point conversion instructions.
111   bool HasF16C;
112
113   /// Processor has FS/GS base insturctions.
114   bool HasFSGSBase;
115
116   /// Processor has LZCNT instruction.
117   bool HasLZCNT;
118
119   /// Processor has BMI1 instructions.
120   bool HasBMI;
121
122   /// Processor has BMI2 instructions.
123   bool HasBMI2;
124
125   /// Processor has RTM instructions.
126   bool HasRTM;
127
128   /// Processor has HLE.
129   bool HasHLE;
130
131   /// Processor has ADX instructions.
132   bool HasADX;
133
134   /// Processor has SHA instructions.
135   bool HasSHA;
136
137   /// Processor has PRFCHW instructions.
138   bool HasPRFCHW;
139
140   /// Processor has RDSEED instructions.
141   bool HasRDSEED;
142
143   /// True if BT (bit test) of memory instructions are slow.
144   bool IsBTMemSlow;
145
146   /// True if SHLD instructions are slow.
147   bool IsSHLDSlow;
148
149   /// True if unaligned memory access is fast.
150   bool IsUAMemFast;
151
152   /// True if unaligned 32-byte memory accesses are slow.
153   bool IsUAMem32Slow;
154
155   /// True if SSE operations can have unaligned memory operands.
156   /// This may require setting a configuration bit in the processor.
157   bool HasSSEUnalignedMem;
158
159   /// True if this processor has the CMPXCHG16B instruction;
160   /// this is true for most x86-64 chips, but not the first AMD chips.
161   bool HasCmpxchg16b;
162
163   /// True if the LEA instruction should be used for adjusting
164   /// the stack pointer. This is an optimization for Intel Atom processors.
165   bool UseLeaForSP;
166
167   /// True if 8-bit divisions are significantly faster than
168   /// 32-bit divisions and should be used when possible.
169   bool HasSlowDivide32;
170
171   /// True if 16-bit divides are significantly faster than
172   /// 64-bit divisions and should be used when possible.
173   bool HasSlowDivide64;
174
175   /// True if the short functions should be padded to prevent
176   /// a stall when returning too early.
177   bool PadShortFunctions;
178
179   /// True if the Calls with memory reference should be converted
180   /// to a register-based indirect call.
181   bool CallRegIndirect;
182
183   /// True if the LEA instruction inputs have to be ready at address generation
184   /// (AG) time.
185   bool LEAUsesAG;
186
187   /// True if the LEA instruction with certain arguments is slow
188   bool SlowLEA;
189
190   /// True if INC and DEC instructions are slow when writing to flags
191   bool SlowIncDec;
192
193   /// Processor has AVX-512 PreFetch Instructions
194   bool HasPFI;
195
196   /// Processor has AVX-512 Exponential and Reciprocal Instructions
197   bool HasERI;
198
199   /// Processor has AVX-512 Conflict Detection Instructions
200   bool HasCDI;
201
202   /// Processor has AVX-512 Doubleword and Quadword instructions
203   bool HasDQI;
204
205   /// Processor has AVX-512 Byte and Word instructions
206   bool HasBWI;
207
208   /// Processor has AVX-512 Vector Length eXtenstions
209   bool HasVLX;
210
211   /// Use software floating point for code generation.
212   bool UseSoftFloat;
213
214   /// The minimum alignment known to hold of the stack frame on
215   /// entry to the function and which must be maintained by every function.
216   unsigned stackAlignment;
217
218   /// Max. memset / memcpy size that is turned into rep/movs, rep/stos ops.
219   ///
220   unsigned MaxInlineSizeThreshold;
221
222   /// What processor and OS we're targeting.
223   Triple TargetTriple;
224
225   /// Instruction itineraries for scheduling
226   InstrItineraryData InstrItins;
227
228 private:
229
230   /// Override the stack alignment.
231   unsigned StackAlignOverride;
232
233   /// True if compiling for 64-bit, false for 16-bit or 32-bit.
234   bool In64BitMode;
235
236   /// True if compiling for 32-bit, false for 16-bit or 64-bit.
237   bool In32BitMode;
238
239   /// True if compiling for 16-bit, false for 32-bit or 64-bit.
240   bool In16BitMode;
241
242   X86SelectionDAGInfo TSInfo;
243   // Ordering here is important. X86InstrInfo initializes X86RegisterInfo which
244   // X86TargetLowering needs.
245   X86InstrInfo InstrInfo;
246   X86TargetLowering TLInfo;
247   X86FrameLowering FrameLowering;
248
249 public:
250   /// This constructor initializes the data members to match that
251   /// of the specified triple.
252   ///
253   X86Subtarget(const std::string &TT, const std::string &CPU,
254                const std::string &FS, const X86TargetMachine &TM,
255                unsigned StackAlignOverride);
256
257   const X86TargetLowering *getTargetLowering() const override {
258     return &TLInfo;
259   }
260   const X86InstrInfo *getInstrInfo() const override { return &InstrInfo; }
261   const X86FrameLowering *getFrameLowering() const override {
262     return &FrameLowering;
263   }
264   const X86SelectionDAGInfo *getSelectionDAGInfo() const override {
265     return &TSInfo;
266   }
267   const X86RegisterInfo *getRegisterInfo() const override {
268     return &getInstrInfo()->getRegisterInfo();
269   }
270
271   /// Returns the minimum alignment known to hold of the
272   /// stack frame on entry to the function and which must be maintained by every
273   /// function for this subtarget.
274   unsigned getStackAlignment() const { return stackAlignment; }
275
276   /// Returns the maximum memset / memcpy size
277   /// that still makes it profitable to inline the call.
278   unsigned getMaxInlineSizeThreshold() const { return MaxInlineSizeThreshold; }
279
280   /// ParseSubtargetFeatures - Parses features string setting specified
281   /// subtarget options.  Definition of function is auto generated by tblgen.
282   void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
283
284 private:
285   /// Initialize the full set of dependencies so we can use an initializer
286   /// list for X86Subtarget.
287   X86Subtarget &initializeSubtargetDependencies(StringRef CPU, StringRef FS);
288   void initializeEnvironment();
289   void initSubtargetFeatures(StringRef CPU, StringRef FS);
290 public:
291   /// Is this x86_64? (disregarding specific ABI / programming model)
292   bool is64Bit() const {
293     return In64BitMode;
294   }
295
296   bool is32Bit() const {
297     return In32BitMode;
298   }
299
300   bool is16Bit() const {
301     return In16BitMode;
302   }
303
304   /// Is this x86_64 with the ILP32 programming model (x32 ABI)?
305   bool isTarget64BitILP32() const {
306     return In64BitMode && (TargetTriple.getEnvironment() == Triple::GNUX32 ||
307                            TargetTriple.isOSNaCl());
308   }
309
310   /// Is this x86_64 with the LP64 programming model (standard AMD64, no x32)?
311   bool isTarget64BitLP64() const {
312     return In64BitMode && (TargetTriple.getEnvironment() != Triple::GNUX32 &&
313                            !TargetTriple.isOSNaCl());
314   }
315
316   PICStyles::Style getPICStyle() const { return PICStyle; }
317   void setPICStyle(PICStyles::Style Style)  { PICStyle = Style; }
318
319   bool hasCMov() const { return HasCMov; }
320   bool hasMMX() const { return X86SSELevel >= MMX; }
321   bool hasSSE1() const { return X86SSELevel >= SSE1; }
322   bool hasSSE2() const { return X86SSELevel >= SSE2; }
323   bool hasSSE3() const { return X86SSELevel >= SSE3; }
324   bool hasSSSE3() const { return X86SSELevel >= SSSE3; }
325   bool hasSSE41() const { return X86SSELevel >= SSE41; }
326   bool hasSSE42() const { return X86SSELevel >= SSE42; }
327   bool hasAVX() const { return X86SSELevel >= AVX; }
328   bool hasAVX2() const { return X86SSELevel >= AVX2; }
329   bool hasAVX512() const { return X86SSELevel >= AVX512F; }
330   bool hasFp256() const { return hasAVX(); }
331   bool hasInt256() const { return hasAVX2(); }
332   bool hasSSE4A() const { return HasSSE4A; }
333   bool has3DNow() const { return X863DNowLevel >= ThreeDNow; }
334   bool has3DNowA() const { return X863DNowLevel >= ThreeDNowA; }
335   bool hasPOPCNT() const { return HasPOPCNT; }
336   bool hasAES() const { return HasAES; }
337   bool hasPCLMUL() const { return HasPCLMUL; }
338   bool hasFMA() const { return HasFMA; }
339   // FIXME: Favor FMA when both are enabled. Is this the right thing to do?
340   bool hasFMA4() const { return HasFMA4 && !HasFMA; }
341   bool hasXOP() const { return HasXOP; }
342   bool hasTBM() const { return HasTBM; }
343   bool hasMOVBE() const { return HasMOVBE; }
344   bool hasRDRAND() const { return HasRDRAND; }
345   bool hasF16C() const { return HasF16C; }
346   bool hasFSGSBase() const { return HasFSGSBase; }
347   bool hasLZCNT() const { return HasLZCNT; }
348   bool hasBMI() const { return HasBMI; }
349   bool hasBMI2() const { return HasBMI2; }
350   bool hasRTM() const { return HasRTM; }
351   bool hasHLE() const { return HasHLE; }
352   bool hasADX() const { return HasADX; }
353   bool hasSHA() const { return HasSHA; }
354   bool hasPRFCHW() const { return HasPRFCHW; }
355   bool hasRDSEED() const { return HasRDSEED; }
356   bool isBTMemSlow() const { return IsBTMemSlow; }
357   bool isSHLDSlow() const { return IsSHLDSlow; }
358   bool isUnalignedMemAccessFast() const { return IsUAMemFast; }
359   bool isUnalignedMem32Slow() const { return IsUAMem32Slow; }
360   bool hasSSEUnalignedMem() const { return HasSSEUnalignedMem; }
361   bool hasCmpxchg16b() const { return HasCmpxchg16b; }
362   bool useLeaForSP() const { return UseLeaForSP; }
363   bool hasSlowDivide32() const { return HasSlowDivide32; }
364   bool hasSlowDivide64() const { return HasSlowDivide64; }
365   bool padShortFunctions() const { return PadShortFunctions; }
366   bool callRegIndirect() const { return CallRegIndirect; }
367   bool LEAusesAG() const { return LEAUsesAG; }
368   bool slowLEA() const { return SlowLEA; }
369   bool slowIncDec() const { return SlowIncDec; }
370   bool hasCDI() const { return HasCDI; }
371   bool hasPFI() const { return HasPFI; }
372   bool hasERI() const { return HasERI; }
373   bool hasDQI() const { return HasDQI; }
374   bool hasBWI() const { return HasBWI; }
375   bool hasVLX() const { return HasVLX; }
376
377   bool isAtom() const { return X86ProcFamily == IntelAtom; }
378   bool isSLM() const { return X86ProcFamily == IntelSLM; }
379   bool useSoftFloat() const { return UseSoftFloat; }
380
381   const Triple &getTargetTriple() const { return TargetTriple; }
382
383   bool isTargetDarwin() const { return TargetTriple.isOSDarwin(); }
384   bool isTargetFreeBSD() const { return TargetTriple.isOSFreeBSD(); }
385   bool isTargetDragonFly() const { return TargetTriple.isOSDragonFly(); }
386   bool isTargetSolaris() const { return TargetTriple.isOSSolaris(); }
387   bool isTargetPS4() const { return TargetTriple.isPS4(); }
388
389   bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
390   bool isTargetCOFF() const { return TargetTriple.isOSBinFormatCOFF(); }
391   bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
392
393   bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
394   bool isTargetNaCl() const { return TargetTriple.isOSNaCl(); }
395   bool isTargetNaCl32() const { return isTargetNaCl() && !is64Bit(); }
396   bool isTargetNaCl64() const { return isTargetNaCl() && is64Bit(); }
397
398   bool isTargetWindowsMSVC() const {
399     return TargetTriple.isWindowsMSVCEnvironment();
400   }
401
402   bool isTargetKnownWindowsMSVC() const {
403     return TargetTriple.isKnownWindowsMSVCEnvironment();
404   }
405
406   bool isTargetWindowsCygwin() const {
407     return TargetTriple.isWindowsCygwinEnvironment();
408   }
409
410   bool isTargetWindowsGNU() const {
411     return TargetTriple.isWindowsGNUEnvironment();
412   }
413
414   bool isTargetWindowsItanium() const {
415     return TargetTriple.isWindowsItaniumEnvironment();
416   }
417
418   bool isTargetCygMing() const { return TargetTriple.isOSCygMing(); }
419
420   bool isOSWindows() const { return TargetTriple.isOSWindows(); }
421
422   bool isTargetWin64() const {
423     return In64BitMode && TargetTriple.isOSWindows();
424   }
425
426   bool isTargetWin32() const {
427     return !In64BitMode && (isTargetCygMing() || isTargetKnownWindowsMSVC());
428   }
429
430   bool isPICStyleSet() const { return PICStyle != PICStyles::None; }
431   bool isPICStyleGOT() const { return PICStyle == PICStyles::GOT; }
432   bool isPICStyleRIPRel() const { return PICStyle == PICStyles::RIPRel; }
433
434   bool isPICStyleStubPIC() const {
435     return PICStyle == PICStyles::StubPIC;
436   }
437
438   bool isPICStyleStubNoDynamic() const {
439     return PICStyle == PICStyles::StubDynamicNoPIC;
440   }
441   bool isPICStyleStubAny() const {
442     return PICStyle == PICStyles::StubDynamicNoPIC ||
443            PICStyle == PICStyles::StubPIC;
444   }
445
446   bool isCallingConvWin64(CallingConv::ID CC) const {
447     return (isTargetWin64() && CC != CallingConv::X86_64_SysV) ||
448            CC == CallingConv::X86_64_Win64;
449   }
450
451   /// ClassifyGlobalReference - Classify a global variable reference for the
452   /// current subtarget according to how we should reference it in a non-pcrel
453   /// context.
454   unsigned char ClassifyGlobalReference(const GlobalValue *GV,
455                                         const TargetMachine &TM)const;
456
457   /// Classify a blockaddress reference for the current subtarget according to
458   /// how we should reference it in a non-pcrel context.
459   unsigned char ClassifyBlockAddressReference() const;
460
461   /// Return true if the subtarget allows calls to immediate address.
462   bool IsLegalToCallImmediateAddr(const TargetMachine &TM) const;
463
464   /// This function returns the name of a function which has an interface
465   /// like the non-standard bzero function, if such a function exists on
466   /// the current subtarget and it is considered prefereable over
467   /// memset with zero passed as the second argument. Otherwise it
468   /// returns null.
469   const char *getBZeroEntry() const;
470
471   /// This function returns true if the target has sincos() routine in its
472   /// compiler runtime or math libraries.
473   bool hasSinCos() const;
474
475   /// Enable the MachineScheduler pass for all X86 subtargets.
476   bool enableMachineScheduler() const override { return true; }
477
478   bool enableEarlyIfConversion() const override;
479
480   /// Return the instruction itineraries based on the subtarget selection.
481   const InstrItineraryData *getInstrItineraryData() const override {
482     return &InstrItins;
483   }
484
485   AntiDepBreakMode getAntiDepBreakMode() const override {
486     return TargetSubtargetInfo::ANTIDEP_CRITICAL;
487   }
488 };
489
490 } // End llvm namespace
491
492 #endif