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