Moving Cost Tables up to share with other targets
[oota-llvm.git] / lib / Target / X86 / X86TargetTransformInfo.cpp
1 //===-- X86TargetTransformInfo.cpp - X86 specific TTI pass ----------------===//
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 /// \file
10 /// This file implements a TargetTransformInfo analysis pass specific to the
11 /// X86 target machine. It uses the target's detailed information to provide
12 /// more precise answers to certain TTI queries, while letting the target
13 /// independent and default TTI implementations handle the rest.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #define DEBUG_TYPE "x86tti"
18 #include "X86.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Target/TargetLowering.h"
23 #include "llvm/Target/CostTable.h"
24 using namespace llvm;
25
26 // Declare the pass initialization routine locally as target-specific passes
27 // don't havve a target-wide initialization entry point, and so we rely on the
28 // pass constructor initialization.
29 namespace llvm {
30 void initializeX86TTIPass(PassRegistry &);
31 }
32
33 namespace {
34
35 class X86TTI : public ImmutablePass, public TargetTransformInfo {
36   const X86TargetMachine *TM;
37   const X86Subtarget *ST;
38   const X86TargetLowering *TLI;
39
40   /// Estimate the overhead of scalarizing an instruction. Insert and Extract
41   /// are set if the result needs to be inserted and/or extracted from vectors.
42   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
43
44 public:
45   X86TTI() : ImmutablePass(ID), TM(0), ST(0), TLI(0) {
46     llvm_unreachable("This pass cannot be directly constructed");
47   }
48
49   X86TTI(const X86TargetMachine *TM)
50       : ImmutablePass(ID), TM(TM), ST(TM->getSubtargetImpl()),
51         TLI(TM->getTargetLowering()) {
52     initializeX86TTIPass(*PassRegistry::getPassRegistry());
53   }
54
55   virtual void initializePass() {
56     pushTTIStack(this);
57   }
58
59   virtual void finalizePass() {
60     popTTIStack();
61   }
62
63   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64     TargetTransformInfo::getAnalysisUsage(AU);
65   }
66
67   /// Pass identification.
68   static char ID;
69
70   /// Provide necessary pointer adjustments for the two base classes.
71   virtual void *getAdjustedAnalysisPointer(const void *ID) {
72     if (ID == &TargetTransformInfo::ID)
73       return (TargetTransformInfo*)this;
74     return this;
75   }
76
77   /// \name Scalar TTI Implementations
78   /// @{
79   virtual PopcntSupportKind getPopcntSupport(unsigned TyWidth) const;
80
81   /// @}
82
83   /// \name Vector TTI Implementations
84   /// @{
85
86   virtual unsigned getNumberOfRegisters(bool Vector) const;
87   virtual unsigned getRegisterBitWidth(bool Vector) const;
88   virtual unsigned getMaximumUnrollFactor() const;
89   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const;
90   virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
91                                   int Index, Type *SubTp) const;
92   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
93                                     Type *Src) const;
94   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
95                                       Type *CondTy) const;
96   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
97                                       unsigned Index) const;
98   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
99                                    unsigned Alignment,
100                                    unsigned AddressSpace) const;
101
102   /// @}
103 };
104
105 } // end anonymous namespace
106
107 INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti",
108                    "X86 Target Transform Info", true, true, false)
109 char X86TTI::ID = 0;
110
111 ImmutablePass *
112 llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
113   return new X86TTI(TM);
114 }
115
116
117 //===----------------------------------------------------------------------===//
118 //
119 // X86 cost model.
120 //
121 //===----------------------------------------------------------------------===//
122
123 X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
124   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
125   // TODO: Currently the __builtin_popcount() implementation using SSE3
126   //   instructions is inefficient. Once the problem is fixed, we should
127   //   call ST->hasSSE3() instead of ST->hasSSE4().
128   return ST->hasSSE41() ? PSK_FastHardware : PSK_Software;
129 }
130
131 unsigned X86TTI::getNumberOfRegisters(bool Vector) const {
132   if (Vector && !ST->hasSSE1())
133     return 0;
134
135   if (ST->is64Bit())
136     return 16;
137   return 8;
138 }
139
140 unsigned X86TTI::getRegisterBitWidth(bool Vector) const {
141   if (Vector) {
142     if (ST->hasAVX()) return 256;
143     if (ST->hasSSE1()) return 128;
144     return 0;
145   }
146
147   if (ST->is64Bit())
148     return 64;
149   return 32;
150
151 }
152
153 unsigned X86TTI::getMaximumUnrollFactor() const {
154   if (ST->isAtom())
155     return 1;
156
157   // Sandybridge and Haswell have multiple execution ports and pipelined
158   // vector units.
159   if (ST->hasAVX())
160     return 4;
161
162   return 2;
163 }
164
165 unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
166   // Legalize the type.
167   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
168
169   int ISD = TLI->InstructionOpcodeToISD(Opcode);
170   assert(ISD && "Invalid opcode");
171
172   static const CostTblEntry<MVT> AVX1CostTable[] = {
173     // We don't have to scalarize unsupported ops. We can issue two half-sized
174     // operations and we only need to extract the upper YMM half.
175     // Two ops + 1 extract + 1 insert = 4.
176     { ISD::MUL,     MVT::v8i32,    4 },
177     { ISD::SUB,     MVT::v8i32,    4 },
178     { ISD::ADD,     MVT::v8i32,    4 },
179     { ISD::MUL,     MVT::v4i64,    4 },
180     { ISD::SUB,     MVT::v4i64,    4 },
181     { ISD::ADD,     MVT::v4i64,    4 },
182     };
183
184   // Look for AVX1 lowering tricks.
185   if (ST->hasAVX()) {
186     int Idx = CostTableLookup<MVT>(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
187                           LT.second);
188     if (Idx != -1)
189       return LT.first * AVX1CostTable[Idx].Cost;
190   }
191   // Fallback to the default implementation.
192   return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty);
193 }
194
195 unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
196                                 Type *SubTp) const {
197   // We only estimate the cost of reverse shuffles.
198   if (Kind != SK_Reverse)
199     return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
200
201   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
202   unsigned Cost = 1;
203   if (LT.second.getSizeInBits() > 128)
204     Cost = 3; // Extract + insert + copy.
205
206   // Multiple by the number of parts.
207   return Cost * LT.first;
208 }
209
210 unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
211   int ISD = TLI->InstructionOpcodeToISD(Opcode);
212   assert(ISD && "Invalid opcode");
213
214   EVT SrcTy = TLI->getValueType(Src);
215   EVT DstTy = TLI->getValueType(Dst);
216
217   if (!SrcTy.isSimple() || !DstTy.isSimple())
218     return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
219
220   static const TypeConversionCostTblEntry<MVT> AVXConversionTbl[] = {
221     { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
222     { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
223     { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
224     { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
225     { ISD::TRUNCATE,    MVT::v4i32, MVT::v4i64, 1 },
226     { ISD::TRUNCATE,    MVT::v8i16, MVT::v8i32, 1 },
227     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i8,  1 },
228     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i8,  1 },
229     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i8,  1 },
230     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i8,  1 },
231     { ISD::FP_TO_SINT,  MVT::v8i8,  MVT::v8f32, 1 },
232     { ISD::FP_TO_SINT,  MVT::v4i8,  MVT::v4f32, 1 },
233     { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1,  6 },
234     { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1,  9 },
235     { ISD::TRUNCATE,    MVT::v8i32, MVT::v8i64, 3 },
236   };
237
238   if (ST->hasAVX()) {
239     int Idx = ConvertCostTableLookup<MVT>(AVXConversionTbl,
240                                  array_lengthof(AVXConversionTbl),
241                                  ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
242     if (Idx != -1)
243       return AVXConversionTbl[Idx].Cost;
244   }
245
246   return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
247 }
248
249 unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
250                                     Type *CondTy) const {
251   // Legalize the type.
252   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
253
254   MVT MTy = LT.second;
255
256   int ISD = TLI->InstructionOpcodeToISD(Opcode);
257   assert(ISD && "Invalid opcode");
258
259   static const CostTblEntry<MVT> SSE42CostTbl[] = {
260     { ISD::SETCC,   MVT::v2f64,   1 },
261     { ISD::SETCC,   MVT::v4f32,   1 },
262     { ISD::SETCC,   MVT::v2i64,   1 },
263     { ISD::SETCC,   MVT::v4i32,   1 },
264     { ISD::SETCC,   MVT::v8i16,   1 },
265     { ISD::SETCC,   MVT::v16i8,   1 },
266   };
267
268   static const CostTblEntry<MVT> AVX1CostTbl[] = {
269     { ISD::SETCC,   MVT::v4f64,   1 },
270     { ISD::SETCC,   MVT::v8f32,   1 },
271     // AVX1 does not support 8-wide integer compare.
272     { ISD::SETCC,   MVT::v4i64,   4 },
273     { ISD::SETCC,   MVT::v8i32,   4 },
274     { ISD::SETCC,   MVT::v16i16,  4 },
275     { ISD::SETCC,   MVT::v32i8,   4 },
276   };
277
278   static const CostTblEntry<MVT> AVX2CostTbl[] = {
279     { ISD::SETCC,   MVT::v4i64,   1 },
280     { ISD::SETCC,   MVT::v8i32,   1 },
281     { ISD::SETCC,   MVT::v16i16,  1 },
282     { ISD::SETCC,   MVT::v32i8,   1 },
283   };
284
285   if (ST->hasAVX2()) {
286     int Idx = CostTableLookup<MVT>(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
287     if (Idx != -1)
288       return LT.first * AVX2CostTbl[Idx].Cost;
289   }
290
291   if (ST->hasAVX()) {
292     int Idx = CostTableLookup<MVT>(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
293     if (Idx != -1)
294       return LT.first * AVX1CostTbl[Idx].Cost;
295   }
296
297   if (ST->hasSSE42()) {
298     int Idx = CostTableLookup<MVT>(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
299     if (Idx != -1)
300       return LT.first * SSE42CostTbl[Idx].Cost;
301   }
302
303   return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
304 }
305
306 unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val,
307                                     unsigned Index) const {
308   assert(Val->isVectorTy() && "This must be a vector type");
309
310   if (Index != -1U) {
311     // Legalize the type.
312     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val);
313
314     // This type is legalized to a scalar type.
315     if (!LT.second.isVector())
316       return 0;
317
318     // The type may be split. Normalize the index to the new type.
319     unsigned Width = LT.second.getVectorNumElements();
320     Index = Index % Width;
321
322     // Floating point scalars are already located in index #0.
323     if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
324       return 0;
325   }
326
327   return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
328 }
329
330 unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
331                                  unsigned AddressSpace) const {
332   // Legalize the type.
333   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
334   assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
335          "Invalid Opcode");
336
337   // Each load/store unit costs 1.
338   unsigned Cost = LT.first * 1;
339
340   // On Sandybridge 256bit load/stores are double pumped
341   // (but not on Haswell).
342   if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
343     Cost*=2;
344
345   return Cost;
346 }