[multiversion] Remove the cached TargetMachine pointer from the
[oota-llvm.git] / include / llvm / CodeGen / BasicTTIImpl.h
1 //===- BasicTTIImpl.h -------------------------------------------*- 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 /// \file
10 /// This file provides a helper that implements much of the TTI interface in
11 /// terms of the target-independent code generator and TargetLowering
12 /// interfaces.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_BASICTTIIMPL_H
17 #define LLVM_CODEGEN_BASICTTIIMPL_H
18
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/TargetTransformInfoImpl.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Target/TargetLowering.h"
23 #include "llvm/Target/TargetSubtargetInfo.h"
24
25 namespace llvm {
26
27 extern cl::opt<unsigned> PartialUnrollingThreshold;
28
29 /// \brief Base class which can be used to help build a TTI implementation.
30 ///
31 /// This class provides as much implementation of the TTI interface as is
32 /// possible using the target independent parts of the code generator.
33 ///
34 /// In order to subclass it, your class must implement a getTM() method to
35 /// return the target machine, and a getTLI() method to return the target
36 /// lowering. We need these methods implemented in the derived class so that
37 /// this class doesn't have to duplicate storage for them.
38 template <typename T>
39 class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
40 private:
41   typedef TargetTransformInfoImplCRTPBase<T> BaseT;
42   typedef TargetTransformInfo TTI;
43
44   /// Estimate the overhead of scalarizing an instruction. Insert and Extract
45   /// are set if the result needs to be inserted and/or extracted from vectors.
46   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
47     assert(Ty->isVectorTy() && "Can only scalarize vectors");
48     unsigned Cost = 0;
49
50     for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
51       if (Insert)
52         Cost += static_cast<T *>(this)
53                     ->getVectorInstrCost(Instruction::InsertElement, Ty, i);
54       if (Extract)
55         Cost += static_cast<T *>(this)
56                     ->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
57     }
58
59     return Cost;
60   }
61
62   /// Estimate the cost overhead of SK_Alternate shuffle.
63   unsigned getAltShuffleOverhead(Type *Ty) {
64     assert(Ty->isVectorTy() && "Can only shuffle vectors");
65     unsigned Cost = 0;
66     // Shuffle cost is equal to the cost of extracting element from its argument
67     // plus the cost of inserting them onto the result vector.
68
69     // e.g. <4 x float> has a mask of <0,5,2,7> i.e we need to extract from
70     // index 0 of first vector, index 1 of second vector,index 2 of first
71     // vector and finally index 3 of second vector and insert them at index
72     // <0,1,2,3> of result vector.
73     for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
74       Cost += static_cast<T *>(this)
75                   ->getVectorInstrCost(Instruction::InsertElement, Ty, i);
76       Cost += static_cast<T *>(this)
77                   ->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
78     }
79     return Cost;
80   }
81
82   /// \brief Local query method delegates up to T which *must* implement this!
83   const TargetMachine *getTM() const {
84     return static_cast<const T *>(this)->getTM();
85   }
86
87   /// \brief Local query method delegates up to T which *must* implement this!
88   const TargetLoweringBase *getTLI() const {
89     return static_cast<const T *>(this)->getTLI();
90   }
91
92 protected:
93   explicit BasicTTIImplBase(const TargetMachine *TM)
94       : BaseT(TM->getDataLayout()) {}
95
96 public:
97   // Provide value semantics. MSVC requires that we spell all of these out.
98   BasicTTIImplBase(const BasicTTIImplBase &Arg)
99       : BaseT(static_cast<const BaseT &>(Arg)) {}
100   BasicTTIImplBase(BasicTTIImplBase &&Arg)
101       : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
102   BasicTTIImplBase &operator=(const BasicTTIImplBase &RHS) {
103     BaseT::operator=(static_cast<const BaseT &>(RHS));
104     return *this;
105   }
106   BasicTTIImplBase &operator=(BasicTTIImplBase &&RHS) {
107     BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
108     return *this;
109   }
110
111   /// \name Scalar TTI Implementations
112   /// @{
113
114   bool hasBranchDivergence() { return false; }
115
116   bool isLegalAddImmediate(int64_t imm) {
117     return getTLI()->isLegalAddImmediate(imm);
118   }
119
120   bool isLegalICmpImmediate(int64_t imm) {
121     return getTLI()->isLegalICmpImmediate(imm);
122   }
123
124   bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
125                              bool HasBaseReg, int64_t Scale) {
126     TargetLoweringBase::AddrMode AM;
127     AM.BaseGV = BaseGV;
128     AM.BaseOffs = BaseOffset;
129     AM.HasBaseReg = HasBaseReg;
130     AM.Scale = Scale;
131     return getTLI()->isLegalAddressingMode(AM, Ty);
132   }
133
134   int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
135                            bool HasBaseReg, int64_t Scale) {
136     TargetLoweringBase::AddrMode AM;
137     AM.BaseGV = BaseGV;
138     AM.BaseOffs = BaseOffset;
139     AM.HasBaseReg = HasBaseReg;
140     AM.Scale = Scale;
141     return getTLI()->getScalingFactorCost(AM, Ty);
142   }
143
144   bool isTruncateFree(Type *Ty1, Type *Ty2) {
145     return getTLI()->isTruncateFree(Ty1, Ty2);
146   }
147
148   bool isTypeLegal(Type *Ty) {
149     EVT VT = getTLI()->getValueType(Ty);
150     return getTLI()->isTypeLegal(VT);
151   }
152
153   unsigned getJumpBufAlignment() { return getTLI()->getJumpBufAlignment(); }
154
155   unsigned getJumpBufSize() { return getTLI()->getJumpBufSize(); }
156
157   bool shouldBuildLookupTables() {
158     const TargetLoweringBase *TLI = getTLI();
159     return TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
160            TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
161   }
162
163   bool haveFastSqrt(Type *Ty) {
164     const TargetLoweringBase *TLI = getTLI();
165     EVT VT = TLI->getValueType(Ty);
166     return TLI->isTypeLegal(VT) &&
167            TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
168   }
169
170   void getUnrollingPreferences(const Function *F, Loop *L,
171                                TTI::UnrollingPreferences &UP) {
172     // This unrolling functionality is target independent, but to provide some
173     // motivation for its intended use, for x86:
174
175     // According to the Intel 64 and IA-32 Architectures Optimization Reference
176     // Manual, Intel Core models and later have a loop stream detector (and
177     // associated uop queue) that can benefit from partial unrolling.
178     // The relevant requirements are:
179     //  - The loop must have no more than 4 (8 for Nehalem and later) branches
180     //    taken, and none of them may be calls.
181     //  - The loop can have no more than 18 (28 for Nehalem and later) uops.
182
183     // According to the Software Optimization Guide for AMD Family 15h
184     // Processors, models 30h-4fh (Steamroller and later) have a loop predictor
185     // and loop buffer which can benefit from partial unrolling.
186     // The relevant requirements are:
187     //  - The loop must have fewer than 16 branches
188     //  - The loop must have less than 40 uops in all executed loop branches
189
190     // The number of taken branches in a loop is hard to estimate here, and
191     // benchmarking has revealed that it is better not to be conservative when
192     // estimating the branch count. As a result, we'll ignore the branch limits
193     // until someone finds a case where it matters in practice.
194
195     unsigned MaxOps;
196     const TargetSubtargetInfo *ST = getTM()->getSubtargetImpl(*F);
197     if (PartialUnrollingThreshold.getNumOccurrences() > 0)
198       MaxOps = PartialUnrollingThreshold;
199     else if (ST->getSchedModel().LoopMicroOpBufferSize > 0)
200       MaxOps = ST->getSchedModel().LoopMicroOpBufferSize;
201     else
202       return;
203
204     // Scan the loop: don't unroll loops with calls.
205     for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); I != E;
206          ++I) {
207       BasicBlock *BB = *I;
208
209       for (BasicBlock::iterator J = BB->begin(), JE = BB->end(); J != JE; ++J)
210         if (isa<CallInst>(J) || isa<InvokeInst>(J)) {
211           ImmutableCallSite CS(J);
212           if (const Function *F = CS.getCalledFunction()) {
213             if (!static_cast<T *>(this)->isLoweredToCall(F))
214               continue;
215           }
216
217           return;
218         }
219     }
220
221     // Enable runtime and partial unrolling up to the specified size.
222     UP.Partial = UP.Runtime = true;
223     UP.PartialThreshold = UP.PartialOptSizeThreshold = MaxOps;
224   }
225
226   /// @}
227
228   /// \name Vector TTI Implementations
229   /// @{
230
231   unsigned getNumberOfRegisters(bool Vector) { return 1; }
232
233   unsigned getRegisterBitWidth(bool Vector) { return 32; }
234
235   unsigned getMaxInterleaveFactor() { return 1; }
236
237   unsigned getArithmeticInstrCost(
238       unsigned Opcode, Type *Ty,
239       TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue,
240       TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue,
241       TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None,
242       TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None) {
243     // Check if any of the operands are vector operands.
244     const TargetLoweringBase *TLI = getTLI();
245     int ISD = TLI->InstructionOpcodeToISD(Opcode);
246     assert(ISD && "Invalid opcode");
247
248     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
249
250     bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
251     // Assume that floating point arithmetic operations cost twice as much as
252     // integer operations.
253     unsigned OpCost = (IsFloat ? 2 : 1);
254
255     if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
256       // The operation is legal. Assume it costs 1.
257       // If the type is split to multiple registers, assume that there is some
258       // overhead to this.
259       // TODO: Once we have extract/insert subvector cost we need to use them.
260       if (LT.first > 1)
261         return LT.first * 2 * OpCost;
262       return LT.first * 1 * OpCost;
263     }
264
265     if (!TLI->isOperationExpand(ISD, LT.second)) {
266       // If the operation is custom lowered then assume
267       // thare the code is twice as expensive.
268       return LT.first * 2 * OpCost;
269     }
270
271     // Else, assume that we need to scalarize this op.
272     if (Ty->isVectorTy()) {
273       unsigned Num = Ty->getVectorNumElements();
274       unsigned Cost = static_cast<T *>(this)
275                           ->getArithmeticInstrCost(Opcode, Ty->getScalarType());
276       // return the cost of multiple scalar invocation plus the cost of
277       // inserting
278       // and extracting the values.
279       return getScalarizationOverhead(Ty, true, true) + Num * Cost;
280     }
281
282     // We don't know anything about this scalar instruction.
283     return OpCost;
284   }
285
286   unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
287                           Type *SubTp) {
288     if (Kind == TTI::SK_Alternate) {
289       return getAltShuffleOverhead(Tp);
290     }
291     return 1;
292   }
293
294   unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
295     const TargetLoweringBase *TLI = getTLI();
296     int ISD = TLI->InstructionOpcodeToISD(Opcode);
297     assert(ISD && "Invalid opcode");
298
299     std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
300     std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
301
302     // Check for NOOP conversions.
303     if (SrcLT.first == DstLT.first &&
304         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
305
306       // Bitcast between types that are legalized to the same type are free.
307       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
308         return 0;
309     }
310
311     if (Opcode == Instruction::Trunc &&
312         TLI->isTruncateFree(SrcLT.second, DstLT.second))
313       return 0;
314
315     if (Opcode == Instruction::ZExt &&
316         TLI->isZExtFree(SrcLT.second, DstLT.second))
317       return 0;
318
319     // If the cast is marked as legal (or promote) then assume low cost.
320     if (SrcLT.first == DstLT.first &&
321         TLI->isOperationLegalOrPromote(ISD, DstLT.second))
322       return 1;
323
324     // Handle scalar conversions.
325     if (!Src->isVectorTy() && !Dst->isVectorTy()) {
326
327       // Scalar bitcasts are usually free.
328       if (Opcode == Instruction::BitCast)
329         return 0;
330
331       // Just check the op cost. If the operation is legal then assume it costs
332       // 1.
333       if (!TLI->isOperationExpand(ISD, DstLT.second))
334         return 1;
335
336       // Assume that illegal scalar instruction are expensive.
337       return 4;
338     }
339
340     // Check vector-to-vector casts.
341     if (Dst->isVectorTy() && Src->isVectorTy()) {
342
343       // If the cast is between same-sized registers, then the check is simple.
344       if (SrcLT.first == DstLT.first &&
345           SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
346
347         // Assume that Zext is done using AND.
348         if (Opcode == Instruction::ZExt)
349           return 1;
350
351         // Assume that sext is done using SHL and SRA.
352         if (Opcode == Instruction::SExt)
353           return 2;
354
355         // Just check the op cost. If the operation is legal then assume it
356         // costs
357         // 1 and multiply by the type-legalization overhead.
358         if (!TLI->isOperationExpand(ISD, DstLT.second))
359           return SrcLT.first * 1;
360       }
361
362       // If we are converting vectors and the operation is illegal, or
363       // if the vectors are legalized to different types, estimate the
364       // scalarization costs.
365       unsigned Num = Dst->getVectorNumElements();
366       unsigned Cost = static_cast<T *>(this)->getCastInstrCost(
367           Opcode, Dst->getScalarType(), Src->getScalarType());
368
369       // Return the cost of multiple scalar invocation plus the cost of
370       // inserting and extracting the values.
371       return getScalarizationOverhead(Dst, true, true) + Num * Cost;
372     }
373
374     // We already handled vector-to-vector and scalar-to-scalar conversions.
375     // This
376     // is where we handle bitcast between vectors and scalars. We need to assume
377     //  that the conversion is scalarized in one way or another.
378     if (Opcode == Instruction::BitCast)
379       // Illegal bitcasts are done by storing and loading from a stack slot.
380       return (Src->isVectorTy() ? getScalarizationOverhead(Src, false, true)
381                                 : 0) +
382              (Dst->isVectorTy() ? getScalarizationOverhead(Dst, true, false)
383                                 : 0);
384
385     llvm_unreachable("Unhandled cast");
386   }
387
388   unsigned getCFInstrCost(unsigned Opcode) {
389     // Branches are assumed to be predicted.
390     return 0;
391   }
392
393   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
394     const TargetLoweringBase *TLI = getTLI();
395     int ISD = TLI->InstructionOpcodeToISD(Opcode);
396     assert(ISD && "Invalid opcode");
397
398     // Selects on vectors are actually vector selects.
399     if (ISD == ISD::SELECT) {
400       assert(CondTy && "CondTy must exist");
401       if (CondTy->isVectorTy())
402         ISD = ISD::VSELECT;
403     }
404
405     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
406
407     if (!(ValTy->isVectorTy() && !LT.second.isVector()) &&
408         !TLI->isOperationExpand(ISD, LT.second)) {
409       // The operation is legal. Assume it costs 1. Multiply
410       // by the type-legalization overhead.
411       return LT.first * 1;
412     }
413
414     // Otherwise, assume that the cast is scalarized.
415     if (ValTy->isVectorTy()) {
416       unsigned Num = ValTy->getVectorNumElements();
417       if (CondTy)
418         CondTy = CondTy->getScalarType();
419       unsigned Cost = static_cast<T *>(this)->getCmpSelInstrCost(
420           Opcode, ValTy->getScalarType(), CondTy);
421
422       // Return the cost of multiple scalar invocation plus the cost of
423       // inserting
424       // and extracting the values.
425       return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
426     }
427
428     // Unknown scalar opcode.
429     return 1;
430   }
431
432   unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
433     std::pair<unsigned, MVT> LT =
434         getTLI()->getTypeLegalizationCost(Val->getScalarType());
435
436     return LT.first;
437   }
438
439   unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
440                            unsigned AddressSpace) {
441     assert(!Src->isVoidTy() && "Invalid type");
442     std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
443
444     // Assuming that all loads of legal types cost 1.
445     unsigned Cost = LT.first;
446
447     if (Src->isVectorTy() &&
448         Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) {
449       // This is a vector load that legalizes to a larger type than the vector
450       // itself. Unless the corresponding extending load or truncating store is
451       // legal, then this will scalarize.
452       TargetLowering::LegalizeAction LA = TargetLowering::Expand;
453       EVT MemVT = getTLI()->getValueType(Src, true);
454       if (MemVT.isSimple() && MemVT != MVT::Other) {
455         if (Opcode == Instruction::Store)
456           LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT());
457         else
458           LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, LT.second, MemVT);
459       }
460
461       if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) {
462         // This is a vector load/store for some illegal type that is scalarized.
463         // We must account for the cost of building or decomposing the vector.
464         Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store,
465                                          Opcode == Instruction::Store);
466       }
467     }
468
469     return Cost;
470   }
471
472   unsigned getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
473                                  ArrayRef<Type *> Tys) {
474     unsigned ISD = 0;
475     switch (IID) {
476     default: {
477       // Assume that we need to scalarize this intrinsic.
478       unsigned ScalarizationCost = 0;
479       unsigned ScalarCalls = 1;
480       if (RetTy->isVectorTy()) {
481         ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
482         ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
483       }
484       for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
485         if (Tys[i]->isVectorTy()) {
486           ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
487           ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
488         }
489       }
490
491       return ScalarCalls + ScalarizationCost;
492     }
493     // Look for intrinsics that can be lowered directly or turned into a scalar
494     // intrinsic call.
495     case Intrinsic::sqrt:
496       ISD = ISD::FSQRT;
497       break;
498     case Intrinsic::sin:
499       ISD = ISD::FSIN;
500       break;
501     case Intrinsic::cos:
502       ISD = ISD::FCOS;
503       break;
504     case Intrinsic::exp:
505       ISD = ISD::FEXP;
506       break;
507     case Intrinsic::exp2:
508       ISD = ISD::FEXP2;
509       break;
510     case Intrinsic::log:
511       ISD = ISD::FLOG;
512       break;
513     case Intrinsic::log10:
514       ISD = ISD::FLOG10;
515       break;
516     case Intrinsic::log2:
517       ISD = ISD::FLOG2;
518       break;
519     case Intrinsic::fabs:
520       ISD = ISD::FABS;
521       break;
522     case Intrinsic::minnum:
523       ISD = ISD::FMINNUM;
524       break;
525     case Intrinsic::maxnum:
526       ISD = ISD::FMAXNUM;
527       break;
528     case Intrinsic::copysign:
529       ISD = ISD::FCOPYSIGN;
530       break;
531     case Intrinsic::floor:
532       ISD = ISD::FFLOOR;
533       break;
534     case Intrinsic::ceil:
535       ISD = ISD::FCEIL;
536       break;
537     case Intrinsic::trunc:
538       ISD = ISD::FTRUNC;
539       break;
540     case Intrinsic::nearbyint:
541       ISD = ISD::FNEARBYINT;
542       break;
543     case Intrinsic::rint:
544       ISD = ISD::FRINT;
545       break;
546     case Intrinsic::round:
547       ISD = ISD::FROUND;
548       break;
549     case Intrinsic::pow:
550       ISD = ISD::FPOW;
551       break;
552     case Intrinsic::fma:
553       ISD = ISD::FMA;
554       break;
555     case Intrinsic::fmuladd:
556       ISD = ISD::FMA;
557       break;
558     // FIXME: We should return 0 whenever getIntrinsicCost == TCC_Free.
559     case Intrinsic::lifetime_start:
560     case Intrinsic::lifetime_end:
561       return 0;
562     case Intrinsic::masked_store:
563       return static_cast<T *>(this)
564           ->getMaskedMemoryOpCost(Instruction::Store, Tys[0], 0, 0);
565     case Intrinsic::masked_load:
566       return static_cast<T *>(this)
567           ->getMaskedMemoryOpCost(Instruction::Load, RetTy, 0, 0);
568     }
569
570     const TargetLoweringBase *TLI = getTLI();
571     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
572
573     if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
574       // The operation is legal. Assume it costs 1.
575       // If the type is split to multiple registers, assume that there is some
576       // overhead to this.
577       // TODO: Once we have extract/insert subvector cost we need to use them.
578       if (LT.first > 1)
579         return LT.first * 2;
580       return LT.first * 1;
581     }
582
583     if (!TLI->isOperationExpand(ISD, LT.second)) {
584       // If the operation is custom lowered then assume
585       // thare the code is twice as expensive.
586       return LT.first * 2;
587     }
588
589     // If we can't lower fmuladd into an FMA estimate the cost as a floating
590     // point mul followed by an add.
591     if (IID == Intrinsic::fmuladd)
592       return static_cast<T *>(this)
593                  ->getArithmeticInstrCost(BinaryOperator::FMul, RetTy) +
594              static_cast<T *>(this)
595                  ->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy);
596
597     // Else, assume that we need to scalarize this intrinsic. For math builtins
598     // this will emit a costly libcall, adding call overhead and spills. Make it
599     // very expensive.
600     if (RetTy->isVectorTy()) {
601       unsigned Num = RetTy->getVectorNumElements();
602       unsigned Cost = static_cast<T *>(this)->getIntrinsicInstrCost(
603           IID, RetTy->getScalarType(), Tys);
604       return 10 * Cost * Num;
605     }
606
607     // This is going to be turned into a library call, make it expensive.
608     return 10;
609   }
610
611   unsigned getNumberOfParts(Type *Tp) {
612     std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
613     return LT.first;
614   }
615
616   unsigned getAddressComputationCost(Type *Ty, bool IsComplex) { return 0; }
617
618   unsigned getReductionCost(unsigned Opcode, Type *Ty, bool IsPairwise) {
619     assert(Ty->isVectorTy() && "Expect a vector type");
620     unsigned NumVecElts = Ty->getVectorNumElements();
621     unsigned NumReduxLevels = Log2_32(NumVecElts);
622     unsigned ArithCost =
623         NumReduxLevels *
624         static_cast<T *>(this)->getArithmeticInstrCost(Opcode, Ty);
625     // Assume the pairwise shuffles add a cost.
626     unsigned ShuffleCost =
627         NumReduxLevels * (IsPairwise + 1) *
628         static_cast<T *>(this)
629             ->getShuffleCost(TTI::SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
630     return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
631   }
632
633   /// @}
634 };
635
636 /// \brief Concrete BasicTTIImpl that can be used if no further customization
637 /// is needed.
638 class BasicTTIImpl : public BasicTTIImplBase<BasicTTIImpl> {
639   typedef BasicTTIImplBase<BasicTTIImpl> BaseT;
640   friend class BasicTTIImplBase<BasicTTIImpl>;
641
642   const TargetMachine *TM;
643   const TargetLoweringBase *TLI;
644
645   const TargetMachine *getTM() const { return TM; }
646   const TargetLoweringBase *getTLI() const { return TLI; }
647
648 public:
649   explicit BasicTTIImpl(const TargetMachine *TM);
650
651   // Provide value semantics. MSVC requires that we spell all of these out.
652   BasicTTIImpl(const BasicTTIImpl &Arg)
653       : BaseT(static_cast<const BaseT &>(Arg)), TM(Arg.TM), TLI(Arg.TLI) {}
654   BasicTTIImpl(BasicTTIImpl &&Arg)
655       : BaseT(std::move(static_cast<BaseT &>(Arg))), TM(std::move(Arg.TM)),
656         TLI(std::move(Arg.TLI)) {}
657   BasicTTIImpl &operator=(const BasicTTIImpl &RHS) {
658     BaseT::operator=(static_cast<const BaseT &>(RHS));
659     TM = RHS.TM;
660     TLI = RHS.TLI;
661     return *this;
662   }
663   BasicTTIImpl &operator=(BasicTTIImpl &&RHS) {
664     BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
665     TM = std::move(RHS.TM);
666     TLI = std::move(RHS.TLI);
667     return *this;
668   }
669 };
670
671 }
672
673 #endif