Switch all uses of LLVM_OVERRIDE to just use 'override' directly.
[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/IR/IntrinsicInst.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Target/CostTable.h"
24 #include "llvm/Target/TargetLowering.h"
25 using namespace llvm;
26
27 // Declare the pass initialization routine locally as target-specific passes
28 // don't havve a target-wide initialization entry point, and so we rely on the
29 // pass constructor initialization.
30 namespace llvm {
31 void initializeX86TTIPass(PassRegistry &);
32 }
33
34 namespace {
35
36 class X86TTI final : public ImmutablePass, public TargetTransformInfo {
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), ST(0), TLI(0) {
46     llvm_unreachable("This pass cannot be directly constructed");
47   }
48
49   X86TTI(const X86TargetMachine *TM)
50     : ImmutablePass(ID), ST(TM->getSubtargetImpl()),
51       TLI(TM->getTargetLowering()) {
52     initializeX86TTIPass(*PassRegistry::getPassRegistry());
53   }
54
55   virtual void initializePass() override {
56     pushTTIStack(this);
57   }
58
59   virtual void finalizePass() {
60     popTTIStack();
61   }
62
63   virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
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) override {
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 override;
80
81   /// @}
82
83   /// \name Vector TTI Implementations
84   /// @{
85
86   virtual unsigned getNumberOfRegisters(bool Vector) const override;
87   virtual unsigned getRegisterBitWidth(bool Vector) const override;
88   virtual unsigned getMaximumUnrollFactor() const override;
89   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
90                                           OperandValueKind,
91                                           OperandValueKind) const override;
92   virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
93                                   int Index, Type *SubTp) const override;
94   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
95                                     Type *Src) const override;
96   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
97                                       Type *CondTy) const override;
98   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
99                                       unsigned Index) const override;
100   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
101                                    unsigned Alignment,
102                                    unsigned AddressSpace) const override;
103
104   virtual unsigned
105   getAddressComputationCost(Type *PtrTy, bool IsComplex) const override;
106
107   virtual unsigned getReductionCost(unsigned Opcode, Type *Ty,
108                                     bool IsPairwiseForm) const override;
109
110   virtual unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
111
112   virtual unsigned getIntImmCost(unsigned Opcode, const APInt &Imm,
113                                  Type *Ty) const override;
114   virtual unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
115                                  Type *Ty) const override;
116
117   /// @}
118 };
119
120 } // end anonymous namespace
121
122 INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti",
123                    "X86 Target Transform Info", true, true, false)
124 char X86TTI::ID = 0;
125
126 ImmutablePass *
127 llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
128   return new X86TTI(TM);
129 }
130
131
132 //===----------------------------------------------------------------------===//
133 //
134 // X86 cost model.
135 //
136 //===----------------------------------------------------------------------===//
137
138 X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
139   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
140   // TODO: Currently the __builtin_popcount() implementation using SSE3
141   //   instructions is inefficient. Once the problem is fixed, we should
142   //   call ST->hasSSE3() instead of ST->hasPOPCNT().
143   return ST->hasPOPCNT() ? PSK_FastHardware : PSK_Software;
144 }
145
146 unsigned X86TTI::getNumberOfRegisters(bool Vector) const {
147   if (Vector && !ST->hasSSE1())
148     return 0;
149
150   if (ST->is64Bit())
151     return 16;
152   return 8;
153 }
154
155 unsigned X86TTI::getRegisterBitWidth(bool Vector) const {
156   if (Vector) {
157     if (ST->hasAVX()) return 256;
158     if (ST->hasSSE1()) return 128;
159     return 0;
160   }
161
162   if (ST->is64Bit())
163     return 64;
164   return 32;
165
166 }
167
168 unsigned X86TTI::getMaximumUnrollFactor() const {
169   if (ST->isAtom())
170     return 1;
171
172   // Sandybridge and Haswell have multiple execution ports and pipelined
173   // vector units.
174   if (ST->hasAVX())
175     return 4;
176
177   return 2;
178 }
179
180 unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
181                                         OperandValueKind Op1Info,
182                                         OperandValueKind Op2Info) const {
183   // Legalize the type.
184   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
185
186   int ISD = TLI->InstructionOpcodeToISD(Opcode);
187   assert(ISD && "Invalid opcode");
188
189   static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
190     // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
191     // customize them to detect the cases where shift amount is a scalar one.
192     { ISD::SHL,     MVT::v4i32,    1 },
193     { ISD::SRL,     MVT::v4i32,    1 },
194     { ISD::SRA,     MVT::v4i32,    1 },
195     { ISD::SHL,     MVT::v8i32,    1 },
196     { ISD::SRL,     MVT::v8i32,    1 },
197     { ISD::SRA,     MVT::v8i32,    1 },
198     { ISD::SHL,     MVT::v2i64,    1 },
199     { ISD::SRL,     MVT::v2i64,    1 },
200     { ISD::SHL,     MVT::v4i64,    1 },
201     { ISD::SRL,     MVT::v4i64,    1 },
202
203     { ISD::SHL,  MVT::v32i8,  42 }, // cmpeqb sequence.
204     { ISD::SHL,  MVT::v16i16,  16*10 }, // Scalarized.
205
206     { ISD::SRL,  MVT::v32i8,  32*10 }, // Scalarized.
207     { ISD::SRL,  MVT::v16i16,  8*10 }, // Scalarized.
208
209     { ISD::SRA,  MVT::v32i8,  32*10 }, // Scalarized.
210     { ISD::SRA,  MVT::v16i16,  16*10 }, // Scalarized.
211     { ISD::SRA,  MVT::v4i64,  4*10 }, // Scalarized.
212
213     // Vectorizing division is a bad idea. See the SSE2 table for more comments.
214     { ISD::SDIV,  MVT::v32i8,  32*20 },
215     { ISD::SDIV,  MVT::v16i16, 16*20 },
216     { ISD::SDIV,  MVT::v8i32,  8*20 },
217     { ISD::SDIV,  MVT::v4i64,  4*20 },
218     { ISD::UDIV,  MVT::v32i8,  32*20 },
219     { ISD::UDIV,  MVT::v16i16, 16*20 },
220     { ISD::UDIV,  MVT::v8i32,  8*20 },
221     { ISD::UDIV,  MVT::v4i64,  4*20 },
222   };
223
224   // Look for AVX2 lowering tricks.
225   if (ST->hasAVX2()) {
226     if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
227         (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
228          Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
229       // On AVX2, a packed v16i16 shift left by a constant build_vector
230       // is lowered into a vector multiply (vpmullw).
231       return LT.first;
232
233     int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
234     if (Idx != -1)
235       return LT.first * AVX2CostTable[Idx].Cost;
236   }
237
238   static const CostTblEntry<MVT::SimpleValueType>
239   SSE2UniformConstCostTable[] = {
240     // We don't correctly identify costs of casts because they are marked as
241     // custom.
242     // Constant splats are cheaper for the following instructions.
243     { ISD::SHL,  MVT::v16i8,  1 }, // psllw.
244     { ISD::SHL,  MVT::v8i16,  1 }, // psllw.
245     { ISD::SHL,  MVT::v4i32,  1 }, // pslld
246     { ISD::SHL,  MVT::v2i64,  1 }, // psllq.
247
248     { ISD::SRL,  MVT::v16i8,  1 }, // psrlw.
249     { ISD::SRL,  MVT::v8i16,  1 }, // psrlw.
250     { ISD::SRL,  MVT::v4i32,  1 }, // psrld.
251     { ISD::SRL,  MVT::v2i64,  1 }, // psrlq.
252
253     { ISD::SRA,  MVT::v16i8,  4 }, // psrlw, pand, pxor, psubb.
254     { ISD::SRA,  MVT::v8i16,  1 }, // psraw.
255     { ISD::SRA,  MVT::v4i32,  1 }, // psrad.
256   };
257
258   if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
259       ST->hasSSE2()) {
260     int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
261     if (Idx != -1)
262       return LT.first * SSE2UniformConstCostTable[Idx].Cost;
263   }
264
265   if (ISD == ISD::SHL &&
266       Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
267     EVT VT = LT.second;
268     if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
269         (VT == MVT::v4i32 && ST->hasSSE41()))
270       // Vector shift left by non uniform constant can be lowered
271       // into vector multiply (pmullw/pmulld).
272       return LT.first;
273     if (VT == MVT::v4i32 && ST->hasSSE2())
274       // A vector shift left by non uniform constant is converted
275       // into a vector multiply; the new multiply is eventually
276       // lowered into a sequence of shuffles and 2 x pmuludq.
277       ISD = ISD::MUL;
278   }
279
280   static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
281     // We don't correctly identify costs of casts because they are marked as
282     // custom.
283     // For some cases, where the shift amount is a scalar we would be able
284     // to generate better code. Unfortunately, when this is the case the value
285     // (the splat) will get hoisted out of the loop, thereby making it invisible
286     // to ISel. The cost model must return worst case assumptions because it is
287     // used for vectorization and we don't want to make vectorized code worse
288     // than scalar code.
289     { ISD::SHL,  MVT::v16i8,  30 }, // cmpeqb sequence.
290     { ISD::SHL,  MVT::v8i16,  8*10 }, // Scalarized.
291     { ISD::SHL,  MVT::v4i32,  2*5 }, // We optimized this using mul.
292     { ISD::SHL,  MVT::v2i64,  2*10 }, // Scalarized.
293     { ISD::SHL,  MVT::v4i64,  4*10 }, // Scalarized. 
294
295     { ISD::SRL,  MVT::v16i8,  16*10 }, // Scalarized.
296     { ISD::SRL,  MVT::v8i16,  8*10 }, // Scalarized.
297     { ISD::SRL,  MVT::v4i32,  4*10 }, // Scalarized.
298     { ISD::SRL,  MVT::v2i64,  2*10 }, // Scalarized.
299
300     { ISD::SRA,  MVT::v16i8,  16*10 }, // Scalarized.
301     { ISD::SRA,  MVT::v8i16,  8*10 }, // Scalarized.
302     { ISD::SRA,  MVT::v4i32,  4*10 }, // Scalarized.
303     { ISD::SRA,  MVT::v2i64,  2*10 }, // Scalarized.
304
305     // It is not a good idea to vectorize division. We have to scalarize it and
306     // in the process we will often end up having to spilling regular
307     // registers. The overhead of division is going to dominate most kernels
308     // anyways so try hard to prevent vectorization of division - it is
309     // generally a bad idea. Assume somewhat arbitrarily that we have to be able
310     // to hide "20 cycles" for each lane.
311     { ISD::SDIV,  MVT::v16i8,  16*20 },
312     { ISD::SDIV,  MVT::v8i16,  8*20 },
313     { ISD::SDIV,  MVT::v4i32,  4*20 },
314     { ISD::SDIV,  MVT::v2i64,  2*20 },
315     { ISD::UDIV,  MVT::v16i8,  16*20 },
316     { ISD::UDIV,  MVT::v8i16,  8*20 },
317     { ISD::UDIV,  MVT::v4i32,  4*20 },
318     { ISD::UDIV,  MVT::v2i64,  2*20 },
319   };
320
321   if (ST->hasSSE2()) {
322     int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
323     if (Idx != -1)
324       return LT.first * SSE2CostTable[Idx].Cost;
325   }
326
327   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
328     // We don't have to scalarize unsupported ops. We can issue two half-sized
329     // operations and we only need to extract the upper YMM half.
330     // Two ops + 1 extract + 1 insert = 4.
331     { ISD::MUL,     MVT::v16i16,   4 },
332     { ISD::MUL,     MVT::v8i32,    4 },
333     { ISD::SUB,     MVT::v8i32,    4 },
334     { ISD::ADD,     MVT::v8i32,    4 },
335     { ISD::SUB,     MVT::v4i64,    4 },
336     { ISD::ADD,     MVT::v4i64,    4 },
337     // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
338     // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
339     // Because we believe v4i64 to be a legal type, we must also include the
340     // split factor of two in the cost table. Therefore, the cost here is 18
341     // instead of 9.
342     { ISD::MUL,     MVT::v4i64,    18 },
343   };
344
345   // Look for AVX1 lowering tricks.
346   if (ST->hasAVX() && !ST->hasAVX2()) {
347     EVT VT = LT.second;
348
349     // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
350     // sequence of extract + two vector multiply + insert.
351     if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
352         Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
353       ISD = ISD::MUL;
354
355     int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
356     if (Idx != -1)
357       return LT.first * AVX1CostTable[Idx].Cost;
358   }
359
360   // Custom lowering of vectors.
361   static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
362     // A v2i64/v4i64 and multiply is custom lowered as a series of long
363     // multiplies(3), shifts(4) and adds(2).
364     { ISD::MUL,     MVT::v2i64,    9 },
365     { ISD::MUL,     MVT::v4i64,    9 },
366   };
367   int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
368   if (Idx != -1)
369     return LT.first * CustomLowered[Idx].Cost;
370
371   // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
372   // 2x pmuludq, 2x shuffle.
373   if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
374       !ST->hasSSE41())
375     return LT.first * 6;
376
377   // Fallback to the default implementation.
378   return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info,
379                                                      Op2Info);
380 }
381
382 unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
383                                 Type *SubTp) const {
384   // We only estimate the cost of reverse shuffles.
385   if (Kind != SK_Reverse)
386     return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
387
388   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
389   unsigned Cost = 1;
390   if (LT.second.getSizeInBits() > 128)
391     Cost = 3; // Extract + insert + copy.
392
393   // Multiple by the number of parts.
394   return Cost * LT.first;
395 }
396
397 unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
398   int ISD = TLI->InstructionOpcodeToISD(Opcode);
399   assert(ISD && "Invalid opcode");
400
401   std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src);
402   std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst);
403
404   static const TypeConversionCostTblEntry<MVT::SimpleValueType>
405   SSE2ConvTbl[] = {
406     // These are somewhat magic numbers justified by looking at the output of
407     // Intel's IACA, running some kernels and making sure when we take
408     // legalization into account the throughput will be overestimated.
409     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
410     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
411     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
412     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
413     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
414     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
415     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
416     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
417     // There are faster sequences for float conversions.
418     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
419     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
420     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
421     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
422     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
423     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
424     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
425     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
426   };
427
428   if (ST->hasSSE2() && !ST->hasAVX()) {
429     int Idx =
430         ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
431     if (Idx != -1)
432       return LTSrc.first * SSE2ConvTbl[Idx].Cost;
433   }
434
435   EVT SrcTy = TLI->getValueType(Src);
436   EVT DstTy = TLI->getValueType(Dst);
437
438   // The function getSimpleVT only handles simple value types.
439   if (!SrcTy.isSimple() || !DstTy.isSimple())
440     return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
441
442   static const TypeConversionCostTblEntry<MVT::SimpleValueType>
443   AVX2ConversionTbl[] = {
444     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8,  1 },
445     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8,  1 },
446     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,   3 },
447     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,   3 },
448     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,   3 },
449     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,   3 },
450     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16,  1 },
451     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16,  1 },
452     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,   3 },
453     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,   3 },
454     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i8,   3 },
455     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i8,   3 },
456     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16,  3 },
457     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16,  3 },
458     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32,  1 },
459     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32,  1 },
460
461     { ISD::TRUNCATE,    MVT::v4i8,   MVT::v4i64,  2 },
462     { ISD::TRUNCATE,    MVT::v4i16,  MVT::v4i64,  2 },
463     { ISD::TRUNCATE,    MVT::v4i32,  MVT::v4i64,  2 },
464     { ISD::TRUNCATE,    MVT::v8i8,   MVT::v8i32,  2 },
465     { ISD::TRUNCATE,    MVT::v8i16,  MVT::v8i32,  2 },
466     { ISD::TRUNCATE,    MVT::v8i32,  MVT::v8i64,  4 },
467   };
468
469   static const TypeConversionCostTblEntry<MVT::SimpleValueType>
470   AVXConversionTbl[] = {
471     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
472     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
473     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,  7 },
474     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,  4 },
475     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,  7 },
476     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,  4 },
477     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16, 4 },
478     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16, 4 },
479     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,  6 },
480     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,  4 },
481     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i8,  6 },
482     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i8,  4 },
483     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16, 6 },
484     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16, 3 },
485     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32, 4 },
486     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32, 4 },
487
488     { ISD::TRUNCATE,    MVT::v4i8,  MVT::v4i64,  4 },
489     { ISD::TRUNCATE,    MVT::v4i16, MVT::v4i64,  4 },
490     { ISD::TRUNCATE,    MVT::v4i32, MVT::v4i64,  4 },
491     { ISD::TRUNCATE,    MVT::v8i8,  MVT::v8i32,  4 },
492     { ISD::TRUNCATE,    MVT::v8i16, MVT::v8i32,  5 },
493     { ISD::TRUNCATE,    MVT::v16i8, MVT::v16i16, 4 },
494     { ISD::TRUNCATE,    MVT::v8i32, MVT::v8i64,  9 },
495
496     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i1,  8 },
497     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i8,  8 },
498     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i16, 5 },
499     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i32, 1 },
500     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i1,  3 },
501     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i8,  3 },
502     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i16, 3 },
503     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i32, 1 },
504     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i1,  3 },
505     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i8,  3 },
506     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i16, 3 },
507     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i32, 1 },
508
509     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i1,  6 },
510     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i8,  5 },
511     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i16, 5 },
512     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i32, 9 },
513     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i1,  7 },
514     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i8,  2 },
515     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i16, 2 },
516     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i32, 6 },
517     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i1,  7 },
518     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i8,  2 },
519     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i16, 2 },
520     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i32, 6 },
521
522     { ISD::FP_TO_SINT,  MVT::v8i8,  MVT::v8f32, 1 },
523     { ISD::FP_TO_SINT,  MVT::v4i8,  MVT::v4f32, 1 },
524   };
525
526   if (ST->hasAVX2()) {
527     int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
528                                      DstTy.getSimpleVT(), SrcTy.getSimpleVT());
529     if (Idx != -1)
530       return AVX2ConversionTbl[Idx].Cost;
531   }
532
533   if (ST->hasAVX()) {
534     int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
535                                      SrcTy.getSimpleVT());
536     if (Idx != -1)
537       return AVXConversionTbl[Idx].Cost;
538   }
539
540   return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
541 }
542
543 unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
544                                     Type *CondTy) const {
545   // Legalize the type.
546   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
547
548   MVT MTy = LT.second;
549
550   int ISD = TLI->InstructionOpcodeToISD(Opcode);
551   assert(ISD && "Invalid opcode");
552
553   static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
554     { ISD::SETCC,   MVT::v2f64,   1 },
555     { ISD::SETCC,   MVT::v4f32,   1 },
556     { ISD::SETCC,   MVT::v2i64,   1 },
557     { ISD::SETCC,   MVT::v4i32,   1 },
558     { ISD::SETCC,   MVT::v8i16,   1 },
559     { ISD::SETCC,   MVT::v16i8,   1 },
560   };
561
562   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
563     { ISD::SETCC,   MVT::v4f64,   1 },
564     { ISD::SETCC,   MVT::v8f32,   1 },
565     // AVX1 does not support 8-wide integer compare.
566     { ISD::SETCC,   MVT::v4i64,   4 },
567     { ISD::SETCC,   MVT::v8i32,   4 },
568     { ISD::SETCC,   MVT::v16i16,  4 },
569     { ISD::SETCC,   MVT::v32i8,   4 },
570   };
571
572   static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
573     { ISD::SETCC,   MVT::v4i64,   1 },
574     { ISD::SETCC,   MVT::v8i32,   1 },
575     { ISD::SETCC,   MVT::v16i16,  1 },
576     { ISD::SETCC,   MVT::v32i8,   1 },
577   };
578
579   if (ST->hasAVX2()) {
580     int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
581     if (Idx != -1)
582       return LT.first * AVX2CostTbl[Idx].Cost;
583   }
584
585   if (ST->hasAVX()) {
586     int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
587     if (Idx != -1)
588       return LT.first * AVX1CostTbl[Idx].Cost;
589   }
590
591   if (ST->hasSSE42()) {
592     int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
593     if (Idx != -1)
594       return LT.first * SSE42CostTbl[Idx].Cost;
595   }
596
597   return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
598 }
599
600 unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val,
601                                     unsigned Index) const {
602   assert(Val->isVectorTy() && "This must be a vector type");
603
604   if (Index != -1U) {
605     // Legalize the type.
606     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val);
607
608     // This type is legalized to a scalar type.
609     if (!LT.second.isVector())
610       return 0;
611
612     // The type may be split. Normalize the index to the new type.
613     unsigned Width = LT.second.getVectorNumElements();
614     Index = Index % Width;
615
616     // Floating point scalars are already located in index #0.
617     if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
618       return 0;
619   }
620
621   return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
622 }
623
624 unsigned X86TTI::getScalarizationOverhead(Type *Ty, bool Insert,
625                                             bool Extract) const {
626   assert (Ty->isVectorTy() && "Can only scalarize vectors");
627   unsigned Cost = 0;
628
629   for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
630     if (Insert)
631       Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
632     if (Extract)
633       Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
634   }
635
636   return Cost;
637 }
638
639 unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
640                                  unsigned AddressSpace) const {
641   // Handle non-power-of-two vectors such as <3 x float>
642   if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
643     unsigned NumElem = VTy->getVectorNumElements();
644
645     // Handle a few common cases:
646     // <3 x float>
647     if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
648       // Cost = 64 bit store + extract + 32 bit store.
649       return 3;
650
651     // <3 x double>
652     if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
653       // Cost = 128 bit store + unpack + 64 bit store.
654       return 3;
655
656     // Assume that all other non-power-of-two numbers are scalarized.
657     if (!isPowerOf2_32(NumElem)) {
658       unsigned Cost = TargetTransformInfo::getMemoryOpCost(Opcode,
659                                                            VTy->getScalarType(),
660                                                            Alignment,
661                                                            AddressSpace);
662       unsigned SplitCost = getScalarizationOverhead(Src,
663                                                     Opcode == Instruction::Load,
664                                                     Opcode==Instruction::Store);
665       return NumElem * Cost + SplitCost;
666     }
667   }
668
669   // Legalize the type.
670   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
671   assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
672          "Invalid Opcode");
673
674   // Each load/store unit costs 1.
675   unsigned Cost = LT.first * 1;
676
677   // On Sandybridge 256bit load/stores are double pumped
678   // (but not on Haswell).
679   if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
680     Cost*=2;
681
682   return Cost;
683 }
684
685 unsigned X86TTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
686   // Address computations in vectorized code with non-consecutive addresses will
687   // likely result in more instructions compared to scalar code where the
688   // computation can more often be merged into the index mode. The resulting
689   // extra micro-ops can significantly decrease throughput.
690   unsigned NumVectorInstToHideOverhead = 10;
691
692   if (Ty->isVectorTy() && IsComplex)
693     return NumVectorInstToHideOverhead;
694
695   return TargetTransformInfo::getAddressComputationCost(Ty, IsComplex);
696 }
697
698 unsigned X86TTI::getReductionCost(unsigned Opcode, Type *ValTy,
699                                   bool IsPairwise) const {
700   
701   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
702   
703   MVT MTy = LT.second;
704   
705   int ISD = TLI->InstructionOpcodeToISD(Opcode);
706   assert(ISD && "Invalid opcode");
707  
708   // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput 
709   // and make it as the cost. 
710  
711   static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
712     { ISD::FADD,  MVT::v2f64,   2 },
713     { ISD::FADD,  MVT::v4f32,   4 },
714     { ISD::ADD,   MVT::v2i64,   2 },      // The data reported by the IACA tool is "1.6".
715     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "3.5".
716     { ISD::ADD,   MVT::v8i16,   5 },
717   };
718  
719   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
720     { ISD::FADD,  MVT::v4f32,   4 },
721     { ISD::FADD,  MVT::v4f64,   5 },
722     { ISD::FADD,  MVT::v8f32,   7 },
723     { ISD::ADD,   MVT::v2i64,   1 },      // The data reported by the IACA tool is "1.5".
724     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "3.5".
725     { ISD::ADD,   MVT::v4i64,   5 },      // The data reported by the IACA tool is "4.8".
726     { ISD::ADD,   MVT::v8i16,   5 },
727     { ISD::ADD,   MVT::v8i32,   5 },
728   };
729
730   static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
731     { ISD::FADD,  MVT::v2f64,   2 },
732     { ISD::FADD,  MVT::v4f32,   4 },
733     { ISD::ADD,   MVT::v2i64,   2 },      // The data reported by the IACA tool is "1.6".
734     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "3.3".
735     { ISD::ADD,   MVT::v8i16,   4 },      // The data reported by the IACA tool is "4.3".
736   };
737   
738   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
739     { ISD::FADD,  MVT::v4f32,   3 },
740     { ISD::FADD,  MVT::v4f64,   3 },
741     { ISD::FADD,  MVT::v8f32,   4 },
742     { ISD::ADD,   MVT::v2i64,   1 },      // The data reported by the IACA tool is "1.5".
743     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "2.8".
744     { ISD::ADD,   MVT::v4i64,   3 },
745     { ISD::ADD,   MVT::v8i16,   4 },
746     { ISD::ADD,   MVT::v8i32,   5 },
747   };
748   
749   if (IsPairwise) {
750     if (ST->hasAVX()) {
751       int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
752       if (Idx != -1)
753         return LT.first * AVX1CostTblPairWise[Idx].Cost;
754     }
755   
756     if (ST->hasSSE42()) {
757       int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
758       if (Idx != -1)
759         return LT.first * SSE42CostTblPairWise[Idx].Cost;
760     }
761   } else {
762     if (ST->hasAVX()) {
763       int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
764       if (Idx != -1)
765         return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
766     }
767     
768     if (ST->hasSSE42()) {
769       int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
770       if (Idx != -1)
771         return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
772     }
773   }
774
775   return TargetTransformInfo::getReductionCost(Opcode, ValTy, IsPairwise);
776 }
777
778 unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
779   assert(Ty->isIntegerTy());
780
781   unsigned BitSize = Ty->getPrimitiveSizeInBits();
782   if (BitSize == 0)
783     return ~0U;
784
785   if (Imm.getBitWidth() <= 64 &&
786       (isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue())))
787     return TCC_Basic;
788   else
789     return 2 * TCC_Basic;
790 }
791
792 unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm,
793                                Type *Ty) const {
794   assert(Ty->isIntegerTy());
795
796   unsigned BitSize = Ty->getPrimitiveSizeInBits();
797   if (BitSize == 0)
798     return ~0U;
799
800   switch (Opcode) {
801   case Instruction::Add:
802   case Instruction::Sub:
803   case Instruction::Mul:
804   case Instruction::UDiv:
805   case Instruction::SDiv:
806   case Instruction::URem:
807   case Instruction::SRem:
808   case Instruction::Shl:
809   case Instruction::LShr:
810   case Instruction::AShr:
811   case Instruction::And:
812   case Instruction::Or:
813   case Instruction::Xor:
814   case Instruction::ICmp:
815     if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
816       return TCC_Free;
817     else
818       return X86TTI::getIntImmCost(Imm, Ty);
819   case Instruction::Trunc:
820   case Instruction::ZExt:
821   case Instruction::SExt:
822   case Instruction::IntToPtr:
823   case Instruction::PtrToInt:
824   case Instruction::BitCast:
825   case Instruction::Call:
826   case Instruction::Select:
827   case Instruction::Ret:
828   case Instruction::Load:
829   case Instruction::Store:
830     return X86TTI::getIntImmCost(Imm, Ty);
831   }
832   return TargetTransformInfo::getIntImmCost(Opcode, Imm, Ty);
833 }
834
835 unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
836                                Type *Ty) const {
837   assert(Ty->isIntegerTy());
838
839   unsigned BitSize = Ty->getPrimitiveSizeInBits();
840   if (BitSize == 0)
841     return ~0U;
842
843   switch (IID) {
844   default: return TargetTransformInfo::getIntImmCost(IID, Imm, Ty);
845   case Intrinsic::sadd_with_overflow:
846   case Intrinsic::uadd_with_overflow:
847   case Intrinsic::ssub_with_overflow:
848   case Intrinsic::usub_with_overflow:
849   case Intrinsic::smul_with_overflow:
850   case Intrinsic::umul_with_overflow:
851     if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
852       return TCC_Free;
853     else
854       return X86TTI::getIntImmCost(Imm, Ty);
855   case Intrinsic::experimental_stackmap:
856   case Intrinsic::experimental_patchpoint_void:
857   case Intrinsic::experimental_patchpoint_i64:
858     if (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))
859       return TCC_Free;
860     else
861       return X86TTI::getIntImmCost(Imm, Ty);
862   }
863 }