Fix a copy-paste bug.
[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 getST() method to
35 /// return the subtarget, and a getTLI() method to return the target lowering.
36 /// We need these methods implemented in the derived class so that this class
37 /// 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 TargetSubtargetInfo *getST() const {
84     return static_cast<const T *>(this)->getST();
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 isProfitableToHoist(Instruction *I) {
149     return getTLI()->isProfitableToHoist(I);
150   }
151
152   bool isTypeLegal(Type *Ty) {
153     EVT VT = getTLI()->getValueType(Ty);
154     return getTLI()->isTypeLegal(VT);
155   }
156
157   unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
158                             ArrayRef<const Value *> Arguments) {
159     return BaseT::getIntrinsicCost(IID, RetTy, Arguments);
160   }
161
162   unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
163                             ArrayRef<Type *> ParamTys) {
164     if (IID == Intrinsic::cttz) {
165       if (getTLI()->isCheapToSpeculateCttz())
166         return TargetTransformInfo::TCC_Basic;
167       return TargetTransformInfo::TCC_Expensive;
168     }
169
170     if (IID == Intrinsic::ctlz) {
171        if (getTLI()->isCheapToSpeculateCtlz())
172         return TargetTransformInfo::TCC_Basic;
173       return TargetTransformInfo::TCC_Expensive;
174     }
175
176     return BaseT::getIntrinsicCost(IID, RetTy, ParamTys);
177   }
178
179   unsigned getJumpBufAlignment() { return getTLI()->getJumpBufAlignment(); }
180
181   unsigned getJumpBufSize() { return getTLI()->getJumpBufSize(); }
182
183   bool shouldBuildLookupTables() {
184     const TargetLoweringBase *TLI = getTLI();
185     return TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
186            TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other);
187   }
188
189   bool haveFastSqrt(Type *Ty) {
190     const TargetLoweringBase *TLI = getTLI();
191     EVT VT = TLI->getValueType(Ty);
192     return TLI->isTypeLegal(VT) &&
193            TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
194   }
195
196   unsigned getFPOpCost(Type *Ty) {
197     // By default, FP instructions are no more expensive since they are
198     // implemented in HW.  Target specific TTI can override this.
199     return TargetTransformInfo::TCC_Basic;
200   }
201
202   unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
203     const TargetLoweringBase *TLI = getTLI();
204     switch (Opcode) {
205     default: break;
206     case Instruction::Trunc: {
207       if (TLI->isTruncateFree(OpTy, Ty))
208         return TargetTransformInfo::TCC_Free;
209       return TargetTransformInfo::TCC_Basic;
210     }
211     case Instruction::ZExt: {
212       if (TLI->isZExtFree(OpTy, Ty))
213         return TargetTransformInfo::TCC_Free;
214       return TargetTransformInfo::TCC_Basic;
215     }
216     }
217
218     return BaseT::getOperationCost(Opcode, Ty, OpTy);
219   }
220
221   void getUnrollingPreferences(Loop *L, TTI::UnrollingPreferences &UP) {
222     // This unrolling functionality is target independent, but to provide some
223     // motivation for its intended use, for x86:
224
225     // According to the Intel 64 and IA-32 Architectures Optimization Reference
226     // Manual, Intel Core models and later have a loop stream detector (and
227     // associated uop queue) that can benefit from partial unrolling.
228     // The relevant requirements are:
229     //  - The loop must have no more than 4 (8 for Nehalem and later) branches
230     //    taken, and none of them may be calls.
231     //  - The loop can have no more than 18 (28 for Nehalem and later) uops.
232
233     // According to the Software Optimization Guide for AMD Family 15h
234     // Processors, models 30h-4fh (Steamroller and later) have a loop predictor
235     // and loop buffer which can benefit from partial unrolling.
236     // The relevant requirements are:
237     //  - The loop must have fewer than 16 branches
238     //  - The loop must have less than 40 uops in all executed loop branches
239
240     // The number of taken branches in a loop is hard to estimate here, and
241     // benchmarking has revealed that it is better not to be conservative when
242     // estimating the branch count. As a result, we'll ignore the branch limits
243     // until someone finds a case where it matters in practice.
244
245     unsigned MaxOps;
246     const TargetSubtargetInfo *ST = getST();
247     if (PartialUnrollingThreshold.getNumOccurrences() > 0)
248       MaxOps = PartialUnrollingThreshold;
249     else if (ST->getSchedModel().LoopMicroOpBufferSize > 0)
250       MaxOps = ST->getSchedModel().LoopMicroOpBufferSize;
251     else
252       return;
253
254     // Scan the loop: don't unroll loops with calls.
255     for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); I != E;
256          ++I) {
257       BasicBlock *BB = *I;
258
259       for (BasicBlock::iterator J = BB->begin(), JE = BB->end(); J != JE; ++J)
260         if (isa<CallInst>(J) || isa<InvokeInst>(J)) {
261           ImmutableCallSite CS(J);
262           if (const Function *F = CS.getCalledFunction()) {
263             if (!static_cast<T *>(this)->isLoweredToCall(F))
264               continue;
265           }
266
267           return;
268         }
269     }
270
271     // Enable runtime and partial unrolling up to the specified size.
272     UP.Partial = UP.Runtime = true;
273     UP.PartialThreshold = UP.PartialOptSizeThreshold = MaxOps;
274   }
275
276   /// @}
277
278   /// \name Vector TTI Implementations
279   /// @{
280
281   unsigned getNumberOfRegisters(bool Vector) { return 1; }
282
283   unsigned getRegisterBitWidth(bool Vector) { return 32; }
284
285   unsigned getMaxInterleaveFactor() { return 1; }
286
287   unsigned getArithmeticInstrCost(
288       unsigned Opcode, Type *Ty,
289       TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue,
290       TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue,
291       TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None,
292       TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None) {
293     // Check if any of the operands are vector operands.
294     const TargetLoweringBase *TLI = getTLI();
295     int ISD = TLI->InstructionOpcodeToISD(Opcode);
296     assert(ISD && "Invalid opcode");
297
298     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
299
300     bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
301     // Assume that floating point arithmetic operations cost twice as much as
302     // integer operations.
303     unsigned OpCost = (IsFloat ? 2 : 1);
304
305     if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
306       // The operation is legal. Assume it costs 1.
307       // If the type is split to multiple registers, assume that there is some
308       // overhead to this.
309       // TODO: Once we have extract/insert subvector cost we need to use them.
310       if (LT.first > 1)
311         return LT.first * 2 * OpCost;
312       return LT.first * 1 * OpCost;
313     }
314
315     if (!TLI->isOperationExpand(ISD, LT.second)) {
316       // If the operation is custom lowered then assume
317       // thare the code is twice as expensive.
318       return LT.first * 2 * OpCost;
319     }
320
321     // Else, assume that we need to scalarize this op.
322     if (Ty->isVectorTy()) {
323       unsigned Num = Ty->getVectorNumElements();
324       unsigned Cost = static_cast<T *>(this)
325                           ->getArithmeticInstrCost(Opcode, Ty->getScalarType());
326       // return the cost of multiple scalar invocation plus the cost of
327       // inserting
328       // and extracting the values.
329       return getScalarizationOverhead(Ty, true, true) + Num * Cost;
330     }
331
332     // We don't know anything about this scalar instruction.
333     return OpCost;
334   }
335
336   unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
337                           Type *SubTp) {
338     if (Kind == TTI::SK_Alternate) {
339       return getAltShuffleOverhead(Tp);
340     }
341     return 1;
342   }
343
344   unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
345     const TargetLoweringBase *TLI = getTLI();
346     int ISD = TLI->InstructionOpcodeToISD(Opcode);
347     assert(ISD && "Invalid opcode");
348
349     std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
350     std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
351
352     // Check for NOOP conversions.
353     if (SrcLT.first == DstLT.first &&
354         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
355
356       // Bitcast between types that are legalized to the same type are free.
357       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
358         return 0;
359     }
360
361     if (Opcode == Instruction::Trunc &&
362         TLI->isTruncateFree(SrcLT.second, DstLT.second))
363       return 0;
364
365     if (Opcode == Instruction::ZExt &&
366         TLI->isZExtFree(SrcLT.second, DstLT.second))
367       return 0;
368
369     // If the cast is marked as legal (or promote) then assume low cost.
370     if (SrcLT.first == DstLT.first &&
371         TLI->isOperationLegalOrPromote(ISD, DstLT.second))
372       return 1;
373
374     // Handle scalar conversions.
375     if (!Src->isVectorTy() && !Dst->isVectorTy()) {
376
377       // Scalar bitcasts are usually free.
378       if (Opcode == Instruction::BitCast)
379         return 0;
380
381       // Just check the op cost. If the operation is legal then assume it costs
382       // 1.
383       if (!TLI->isOperationExpand(ISD, DstLT.second))
384         return 1;
385
386       // Assume that illegal scalar instruction are expensive.
387       return 4;
388     }
389
390     // Check vector-to-vector casts.
391     if (Dst->isVectorTy() && Src->isVectorTy()) {
392
393       // If the cast is between same-sized registers, then the check is simple.
394       if (SrcLT.first == DstLT.first &&
395           SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
396
397         // Assume that Zext is done using AND.
398         if (Opcode == Instruction::ZExt)
399           return 1;
400
401         // Assume that sext is done using SHL and SRA.
402         if (Opcode == Instruction::SExt)
403           return 2;
404
405         // Just check the op cost. If the operation is legal then assume it
406         // costs
407         // 1 and multiply by the type-legalization overhead.
408         if (!TLI->isOperationExpand(ISD, DstLT.second))
409           return SrcLT.first * 1;
410       }
411
412       // If we are converting vectors and the operation is illegal, or
413       // if the vectors are legalized to different types, estimate the
414       // scalarization costs.
415       unsigned Num = Dst->getVectorNumElements();
416       unsigned Cost = static_cast<T *>(this)->getCastInstrCost(
417           Opcode, Dst->getScalarType(), Src->getScalarType());
418
419       // Return the cost of multiple scalar invocation plus the cost of
420       // inserting and extracting the values.
421       return getScalarizationOverhead(Dst, true, true) + Num * Cost;
422     }
423
424     // We already handled vector-to-vector and scalar-to-scalar conversions.
425     // This
426     // is where we handle bitcast between vectors and scalars. We need to assume
427     //  that the conversion is scalarized in one way or another.
428     if (Opcode == Instruction::BitCast)
429       // Illegal bitcasts are done by storing and loading from a stack slot.
430       return (Src->isVectorTy() ? getScalarizationOverhead(Src, false, true)
431                                 : 0) +
432              (Dst->isVectorTy() ? getScalarizationOverhead(Dst, true, false)
433                                 : 0);
434
435     llvm_unreachable("Unhandled cast");
436   }
437
438   unsigned getCFInstrCost(unsigned Opcode) {
439     // Branches are assumed to be predicted.
440     return 0;
441   }
442
443   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
444     const TargetLoweringBase *TLI = getTLI();
445     int ISD = TLI->InstructionOpcodeToISD(Opcode);
446     assert(ISD && "Invalid opcode");
447
448     // Selects on vectors are actually vector selects.
449     if (ISD == ISD::SELECT) {
450       assert(CondTy && "CondTy must exist");
451       if (CondTy->isVectorTy())
452         ISD = ISD::VSELECT;
453     }
454
455     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
456
457     if (!(ValTy->isVectorTy() && !LT.second.isVector()) &&
458         !TLI->isOperationExpand(ISD, LT.second)) {
459       // The operation is legal. Assume it costs 1. Multiply
460       // by the type-legalization overhead.
461       return LT.first * 1;
462     }
463
464     // Otherwise, assume that the cast is scalarized.
465     if (ValTy->isVectorTy()) {
466       unsigned Num = ValTy->getVectorNumElements();
467       if (CondTy)
468         CondTy = CondTy->getScalarType();
469       unsigned Cost = static_cast<T *>(this)->getCmpSelInstrCost(
470           Opcode, ValTy->getScalarType(), CondTy);
471
472       // Return the cost of multiple scalar invocation plus the cost of
473       // inserting
474       // and extracting the values.
475       return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
476     }
477
478     // Unknown scalar opcode.
479     return 1;
480   }
481
482   unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
483     std::pair<unsigned, MVT> LT =
484         getTLI()->getTypeLegalizationCost(Val->getScalarType());
485
486     return LT.first;
487   }
488
489   unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
490                            unsigned AddressSpace) {
491     assert(!Src->isVoidTy() && "Invalid type");
492     std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
493
494     // Assuming that all loads of legal types cost 1.
495     unsigned Cost = LT.first;
496
497     if (Src->isVectorTy() &&
498         Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) {
499       // This is a vector load that legalizes to a larger type than the vector
500       // itself. Unless the corresponding extending load or truncating store is
501       // legal, then this will scalarize.
502       TargetLowering::LegalizeAction LA = TargetLowering::Expand;
503       EVT MemVT = getTLI()->getValueType(Src, true);
504       if (MemVT.isSimple() && MemVT != MVT::Other) {
505         if (Opcode == Instruction::Store)
506           LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT());
507         else
508           LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, LT.second, MemVT);
509       }
510
511       if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) {
512         // This is a vector load/store for some illegal type that is scalarized.
513         // We must account for the cost of building or decomposing the vector.
514         Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store,
515                                          Opcode == Instruction::Store);
516       }
517     }
518
519     return Cost;
520   }
521
522   unsigned getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
523                                  ArrayRef<Type *> Tys) {
524     unsigned ISD = 0;
525     switch (IID) {
526     default: {
527       // Assume that we need to scalarize this intrinsic.
528       unsigned ScalarizationCost = 0;
529       unsigned ScalarCalls = 1;
530       if (RetTy->isVectorTy()) {
531         ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
532         ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
533       }
534       for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
535         if (Tys[i]->isVectorTy()) {
536           ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
537           ScalarCalls = std::max(ScalarCalls, Tys[i]->getVectorNumElements());
538         }
539       }
540
541       return ScalarCalls + ScalarizationCost;
542     }
543     // Look for intrinsics that can be lowered directly or turned into a scalar
544     // intrinsic call.
545     case Intrinsic::sqrt:
546       ISD = ISD::FSQRT;
547       break;
548     case Intrinsic::sin:
549       ISD = ISD::FSIN;
550       break;
551     case Intrinsic::cos:
552       ISD = ISD::FCOS;
553       break;
554     case Intrinsic::exp:
555       ISD = ISD::FEXP;
556       break;
557     case Intrinsic::exp2:
558       ISD = ISD::FEXP2;
559       break;
560     case Intrinsic::log:
561       ISD = ISD::FLOG;
562       break;
563     case Intrinsic::log10:
564       ISD = ISD::FLOG10;
565       break;
566     case Intrinsic::log2:
567       ISD = ISD::FLOG2;
568       break;
569     case Intrinsic::fabs:
570       ISD = ISD::FABS;
571       break;
572     case Intrinsic::minnum:
573       ISD = ISD::FMINNUM;
574       break;
575     case Intrinsic::maxnum:
576       ISD = ISD::FMAXNUM;
577       break;
578     case Intrinsic::copysign:
579       ISD = ISD::FCOPYSIGN;
580       break;
581     case Intrinsic::floor:
582       ISD = ISD::FFLOOR;
583       break;
584     case Intrinsic::ceil:
585       ISD = ISD::FCEIL;
586       break;
587     case Intrinsic::trunc:
588       ISD = ISD::FTRUNC;
589       break;
590     case Intrinsic::nearbyint:
591       ISD = ISD::FNEARBYINT;
592       break;
593     case Intrinsic::rint:
594       ISD = ISD::FRINT;
595       break;
596     case Intrinsic::round:
597       ISD = ISD::FROUND;
598       break;
599     case Intrinsic::pow:
600       ISD = ISD::FPOW;
601       break;
602     case Intrinsic::fma:
603       ISD = ISD::FMA;
604       break;
605     case Intrinsic::fmuladd:
606       ISD = ISD::FMA;
607       break;
608     // FIXME: We should return 0 whenever getIntrinsicCost == TCC_Free.
609     case Intrinsic::lifetime_start:
610     case Intrinsic::lifetime_end:
611       return 0;
612     case Intrinsic::masked_store:
613       return static_cast<T *>(this)
614           ->getMaskedMemoryOpCost(Instruction::Store, Tys[0], 0, 0);
615     case Intrinsic::masked_load:
616       return static_cast<T *>(this)
617           ->getMaskedMemoryOpCost(Instruction::Load, RetTy, 0, 0);
618     }
619
620     const TargetLoweringBase *TLI = getTLI();
621     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
622
623     if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
624       // The operation is legal. Assume it costs 1.
625       // If the type is split to multiple registers, assume that there is some
626       // overhead to this.
627       // TODO: Once we have extract/insert subvector cost we need to use them.
628       if (LT.first > 1)
629         return LT.first * 2;
630       return LT.first * 1;
631     }
632
633     if (!TLI->isOperationExpand(ISD, LT.second)) {
634       // If the operation is custom lowered then assume
635       // thare the code is twice as expensive.
636       return LT.first * 2;
637     }
638
639     // If we can't lower fmuladd into an FMA estimate the cost as a floating
640     // point mul followed by an add.
641     if (IID == Intrinsic::fmuladd)
642       return static_cast<T *>(this)
643                  ->getArithmeticInstrCost(BinaryOperator::FMul, RetTy) +
644              static_cast<T *>(this)
645                  ->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy);
646
647     // Else, assume that we need to scalarize this intrinsic. For math builtins
648     // this will emit a costly libcall, adding call overhead and spills. Make it
649     // very expensive.
650     if (RetTy->isVectorTy()) {
651       unsigned Num = RetTy->getVectorNumElements();
652       unsigned Cost = static_cast<T *>(this)->getIntrinsicInstrCost(
653           IID, RetTy->getScalarType(), Tys);
654       return 10 * Cost * Num;
655     }
656
657     // This is going to be turned into a library call, make it expensive.
658     return 10;
659   }
660
661   unsigned getNumberOfParts(Type *Tp) {
662     std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
663     return LT.first;
664   }
665
666   unsigned getAddressComputationCost(Type *Ty, bool IsComplex) { return 0; }
667
668   unsigned getReductionCost(unsigned Opcode, Type *Ty, bool IsPairwise) {
669     assert(Ty->isVectorTy() && "Expect a vector type");
670     unsigned NumVecElts = Ty->getVectorNumElements();
671     unsigned NumReduxLevels = Log2_32(NumVecElts);
672     unsigned ArithCost =
673         NumReduxLevels *
674         static_cast<T *>(this)->getArithmeticInstrCost(Opcode, Ty);
675     // Assume the pairwise shuffles add a cost.
676     unsigned ShuffleCost =
677         NumReduxLevels * (IsPairwise + 1) *
678         static_cast<T *>(this)
679             ->getShuffleCost(TTI::SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
680     return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
681   }
682
683   /// @}
684 };
685
686 /// \brief Concrete BasicTTIImpl that can be used if no further customization
687 /// is needed.
688 class BasicTTIImpl : public BasicTTIImplBase<BasicTTIImpl> {
689   typedef BasicTTIImplBase<BasicTTIImpl> BaseT;
690   friend class BasicTTIImplBase<BasicTTIImpl>;
691
692   const TargetSubtargetInfo *ST;
693   const TargetLoweringBase *TLI;
694
695   const TargetSubtargetInfo *getST() const { return ST; }
696   const TargetLoweringBase *getTLI() const { return TLI; }
697
698 public:
699   explicit BasicTTIImpl(const TargetMachine *ST, Function &F);
700
701   // Provide value semantics. MSVC requires that we spell all of these out.
702   BasicTTIImpl(const BasicTTIImpl &Arg)
703       : BaseT(static_cast<const BaseT &>(Arg)), ST(Arg.ST), TLI(Arg.TLI) {}
704   BasicTTIImpl(BasicTTIImpl &&Arg)
705       : BaseT(std::move(static_cast<BaseT &>(Arg))), ST(std::move(Arg.ST)),
706         TLI(std::move(Arg.TLI)) {}
707   BasicTTIImpl &operator=(const BasicTTIImpl &RHS) {
708     BaseT::operator=(static_cast<const BaseT &>(RHS));
709     ST = RHS.ST;
710     TLI = RHS.TLI;
711     return *this;
712   }
713   BasicTTIImpl &operator=(BasicTTIImpl &&RHS) {
714     BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
715     ST = std::move(RHS.ST);
716     TLI = std::move(RHS.TLI);
717     return *this;
718   }
719 };
720
721 }
722
723 #endif