1664e354dSChandler Carruth //===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===//
2664e354dSChandler Carruth //
3664e354dSChandler Carruth //                     The LLVM Compiler Infrastructure
4664e354dSChandler Carruth //
5664e354dSChandler Carruth // This file is distributed under the University of Illinois Open Source
6664e354dSChandler Carruth // License. See LICENSE.TXT for details.
7664e354dSChandler Carruth //
8664e354dSChandler Carruth //===----------------------------------------------------------------------===//
9664e354dSChandler Carruth /// \file
10664e354dSChandler Carruth /// This file provides the implementation of a basic TargetTransformInfo pass
11664e354dSChandler Carruth /// predicated on the target abstractions present in the target independent
12664e354dSChandler Carruth /// code generator. It uses these (primarily TargetLowering) to model as much
13664e354dSChandler Carruth /// of the TTI query interface as possible. It is included by most targets so
14664e354dSChandler Carruth /// that they can specialize only a small subset of the query space.
15664e354dSChandler Carruth ///
16664e354dSChandler Carruth //===----------------------------------------------------------------------===//
17664e354dSChandler Carruth 
18664e354dSChandler Carruth #define DEBUG_TYPE "basictti"
19664e354dSChandler Carruth #include "llvm/CodeGen/Passes.h"
20d3e73556SChandler Carruth #include "llvm/Analysis/TargetTransformInfo.h"
21664e354dSChandler Carruth #include "llvm/Target/TargetLowering.h"
22664e354dSChandler Carruth #include <utility>
23664e354dSChandler Carruth 
24664e354dSChandler Carruth using namespace llvm;
25664e354dSChandler Carruth 
26664e354dSChandler Carruth namespace {
27664e354dSChandler Carruth 
28*3e752e7aSJuergen Ributzka class BasicTTI LLVM_FINAL : public ImmutablePass, public TargetTransformInfo {
29afc1036fSBill Wendling   const TargetMachine *TM;
30664e354dSChandler Carruth 
31664e354dSChandler Carruth   /// Estimate the overhead of scalarizing an instruction. Insert and Extract
32664e354dSChandler Carruth   /// are set if the result needs to be inserted and/or extracted from vectors.
33664e354dSChandler Carruth   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
34664e354dSChandler Carruth 
35afc1036fSBill Wendling   const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); }
36afc1036fSBill Wendling 
37664e354dSChandler Carruth public:
38afc1036fSBill Wendling   BasicTTI() : ImmutablePass(ID), TM(0) {
39664e354dSChandler Carruth     llvm_unreachable("This pass cannot be directly constructed");
40664e354dSChandler Carruth   }
41664e354dSChandler Carruth 
42afc1036fSBill Wendling   BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) {
43664e354dSChandler Carruth     initializeBasicTTIPass(*PassRegistry::getPassRegistry());
44664e354dSChandler Carruth   }
45664e354dSChandler Carruth 
46*3e752e7aSJuergen Ributzka   virtual void initializePass() LLVM_OVERRIDE {
47664e354dSChandler Carruth     pushTTIStack(this);
48664e354dSChandler Carruth   }
49664e354dSChandler Carruth 
50664e354dSChandler Carruth   virtual void finalizePass() {
51664e354dSChandler Carruth     popTTIStack();
52664e354dSChandler Carruth   }
53664e354dSChandler Carruth 
54*3e752e7aSJuergen Ributzka   virtual void getAnalysisUsage(AnalysisUsage &AU) const LLVM_OVERRIDE {
55664e354dSChandler Carruth     TargetTransformInfo::getAnalysisUsage(AU);
56664e354dSChandler Carruth   }
57664e354dSChandler Carruth 
58664e354dSChandler Carruth   /// Pass identification.
59664e354dSChandler Carruth   static char ID;
60664e354dSChandler Carruth 
61664e354dSChandler Carruth   /// Provide necessary pointer adjustments for the two base classes.
62*3e752e7aSJuergen Ributzka   virtual void *getAdjustedAnalysisPointer(const void *ID) LLVM_OVERRIDE {
63664e354dSChandler Carruth     if (ID == &TargetTransformInfo::ID)
64664e354dSChandler Carruth       return (TargetTransformInfo*)this;
65664e354dSChandler Carruth     return this;
66664e354dSChandler Carruth   }
67664e354dSChandler Carruth 
68*3e752e7aSJuergen Ributzka   virtual bool hasBranchDivergence() const LLVM_OVERRIDE;
698b1e021eSTom Stellard 
70664e354dSChandler Carruth   /// \name Scalar TTI Implementations
71664e354dSChandler Carruth   /// @{
72664e354dSChandler Carruth 
73*3e752e7aSJuergen Ributzka   virtual bool isLegalAddImmediate(int64_t imm) const LLVM_OVERRIDE;
74*3e752e7aSJuergen Ributzka   virtual bool isLegalICmpImmediate(int64_t imm) const LLVM_OVERRIDE;
75664e354dSChandler Carruth   virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
76664e354dSChandler Carruth                                      int64_t BaseOffset, bool HasBaseReg,
77*3e752e7aSJuergen Ributzka                                      int64_t Scale) const LLVM_OVERRIDE;
78bf490d4aSQuentin Colombet   virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
79bf490d4aSQuentin Colombet                                    int64_t BaseOffset, bool HasBaseReg,
80*3e752e7aSJuergen Ributzka                                    int64_t Scale) const LLVM_OVERRIDE;
81*3e752e7aSJuergen Ributzka   virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const LLVM_OVERRIDE;
82*3e752e7aSJuergen Ributzka   virtual bool isTypeLegal(Type *Ty) const LLVM_OVERRIDE;
83*3e752e7aSJuergen Ributzka   virtual unsigned getJumpBufAlignment() const LLVM_OVERRIDE;
84*3e752e7aSJuergen Ributzka   virtual unsigned getJumpBufSize() const LLVM_OVERRIDE;
85*3e752e7aSJuergen Ributzka   virtual bool shouldBuildLookupTables() const LLVM_OVERRIDE;
86*3e752e7aSJuergen Ributzka   virtual bool haveFastSqrt(Type *Ty) const LLVM_OVERRIDE;
87*3e752e7aSJuergen Ributzka   virtual void getUnrollingPreferences(
88*3e752e7aSJuergen Ributzka     Loop *L, UnrollingPreferences &UP) const LLVM_OVERRIDE;
89664e354dSChandler Carruth 
90664e354dSChandler Carruth   /// @}
91664e354dSChandler Carruth 
92664e354dSChandler Carruth   /// \name Vector TTI Implementations
93664e354dSChandler Carruth   /// @{
94664e354dSChandler Carruth 
95*3e752e7aSJuergen Ributzka   virtual unsigned getNumberOfRegisters(bool Vector) const LLVM_OVERRIDE;
96*3e752e7aSJuergen Ributzka   virtual unsigned getMaximumUnrollFactor() const LLVM_OVERRIDE;
97*3e752e7aSJuergen Ributzka   virtual unsigned getRegisterBitWidth(bool Vector) const LLVM_OVERRIDE;
98b9773871SArnold Schwaighofer   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
99b9773871SArnold Schwaighofer                                           OperandValueKind,
100*3e752e7aSJuergen Ributzka                                           OperandValueKind) const LLVM_OVERRIDE;
101664e354dSChandler Carruth   virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
102*3e752e7aSJuergen Ributzka                                   int Index, Type *SubTp) const LLVM_OVERRIDE;
103664e354dSChandler Carruth   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
104*3e752e7aSJuergen Ributzka                                     Type *Src) const LLVM_OVERRIDE;
105*3e752e7aSJuergen Ributzka   virtual unsigned getCFInstrCost(unsigned Opcode) const LLVM_OVERRIDE;
106664e354dSChandler Carruth   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
107*3e752e7aSJuergen Ributzka                                       Type *CondTy) const LLVM_OVERRIDE;
108664e354dSChandler Carruth   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
109*3e752e7aSJuergen Ributzka                                       unsigned Index) const LLVM_OVERRIDE;
110664e354dSChandler Carruth   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
111664e354dSChandler Carruth                                    unsigned Alignment,
112*3e752e7aSJuergen Ributzka                                    unsigned AddressSpace) const LLVM_OVERRIDE;
113*3e752e7aSJuergen Ributzka   virtual unsigned getIntrinsicInstrCost(
114*3e752e7aSJuergen Ributzka     Intrinsic::ID, Type *RetTy, ArrayRef<Type*> Tys) const LLVM_OVERRIDE;
115*3e752e7aSJuergen Ributzka   virtual unsigned getNumberOfParts(Type *Tp) const LLVM_OVERRIDE;
116*3e752e7aSJuergen Ributzka   virtual unsigned getAddressComputationCost(
117*3e752e7aSJuergen Ributzka     Type *Ty, bool IsComplex) const LLVM_OVERRIDE;
118*3e752e7aSJuergen Ributzka   virtual unsigned getReductionCost(unsigned Opcode, Type *Ty,
119*3e752e7aSJuergen Ributzka                                     bool IsPairwise) const LLVM_OVERRIDE;
120664e354dSChandler Carruth 
121664e354dSChandler Carruth   /// @}
122664e354dSChandler Carruth };
123664e354dSChandler Carruth 
124664e354dSChandler Carruth }
125664e354dSChandler Carruth 
126664e354dSChandler Carruth INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
127664e354dSChandler Carruth                    "Target independent code generator's TTI", true, true, false)
128664e354dSChandler Carruth char BasicTTI::ID = 0;
129664e354dSChandler Carruth 
130664e354dSChandler Carruth ImmutablePass *
131afc1036fSBill Wendling llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
132afc1036fSBill Wendling   return new BasicTTI(TM);
133664e354dSChandler Carruth }
134664e354dSChandler Carruth 
1358b1e021eSTom Stellard bool BasicTTI::hasBranchDivergence() const { return false; }
136664e354dSChandler Carruth 
137664e354dSChandler Carruth bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
138afc1036fSBill Wendling   return getTLI()->isLegalAddImmediate(imm);
139664e354dSChandler Carruth }
140664e354dSChandler Carruth 
141664e354dSChandler Carruth bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
142afc1036fSBill Wendling   return getTLI()->isLegalICmpImmediate(imm);
143664e354dSChandler Carruth }
144664e354dSChandler Carruth 
145664e354dSChandler Carruth bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
146664e354dSChandler Carruth                                      int64_t BaseOffset, bool HasBaseReg,
147664e354dSChandler Carruth                                      int64_t Scale) const {
14856b31bd9SBenjamin Kramer   TargetLoweringBase::AddrMode AM;
149664e354dSChandler Carruth   AM.BaseGV = BaseGV;
150664e354dSChandler Carruth   AM.BaseOffs = BaseOffset;
151664e354dSChandler Carruth   AM.HasBaseReg = HasBaseReg;
152664e354dSChandler Carruth   AM.Scale = Scale;
153afc1036fSBill Wendling   return getTLI()->isLegalAddressingMode(AM, Ty);
154664e354dSChandler Carruth }
155664e354dSChandler Carruth 
156bf490d4aSQuentin Colombet int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
157bf490d4aSQuentin Colombet                                    int64_t BaseOffset, bool HasBaseReg,
158bf490d4aSQuentin Colombet                                    int64_t Scale) const {
159bf490d4aSQuentin Colombet   TargetLoweringBase::AddrMode AM;
160bf490d4aSQuentin Colombet   AM.BaseGV = BaseGV;
161bf490d4aSQuentin Colombet   AM.BaseOffs = BaseOffset;
162bf490d4aSQuentin Colombet   AM.HasBaseReg = HasBaseReg;
163bf490d4aSQuentin Colombet   AM.Scale = Scale;
164afc1036fSBill Wendling   return getTLI()->getScalingFactorCost(AM, Ty);
165bf490d4aSQuentin Colombet }
166bf490d4aSQuentin Colombet 
167664e354dSChandler Carruth bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
168afc1036fSBill Wendling   return getTLI()->isTruncateFree(Ty1, Ty2);
169664e354dSChandler Carruth }
170664e354dSChandler Carruth 
171664e354dSChandler Carruth bool BasicTTI::isTypeLegal(Type *Ty) const {
172afc1036fSBill Wendling   EVT T = getTLI()->getValueType(Ty);
173afc1036fSBill Wendling   return getTLI()->isTypeLegal(T);
174664e354dSChandler Carruth }
175664e354dSChandler Carruth 
176664e354dSChandler Carruth unsigned BasicTTI::getJumpBufAlignment() const {
177afc1036fSBill Wendling   return getTLI()->getJumpBufAlignment();
178664e354dSChandler Carruth }
179664e354dSChandler Carruth 
180664e354dSChandler Carruth unsigned BasicTTI::getJumpBufSize() const {
181afc1036fSBill Wendling   return getTLI()->getJumpBufSize();
182664e354dSChandler Carruth }
183664e354dSChandler Carruth 
184664e354dSChandler Carruth bool BasicTTI::shouldBuildLookupTables() const {
185afc1036fSBill Wendling   const TargetLoweringBase *TLI = getTLI();
186664e354dSChandler Carruth   return TLI->supportJumpTables() &&
187664e354dSChandler Carruth       (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
188664e354dSChandler Carruth        TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
189664e354dSChandler Carruth }
190664e354dSChandler Carruth 
19137cd6cfbSRichard Sandiford bool BasicTTI::haveFastSqrt(Type *Ty) const {
19237cd6cfbSRichard Sandiford   const TargetLoweringBase *TLI = getTLI();
19337cd6cfbSRichard Sandiford   EVT VT = TLI->getValueType(Ty);
19437cd6cfbSRichard Sandiford   return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
19537cd6cfbSRichard Sandiford }
19637cd6cfbSRichard Sandiford 
1978f2e7005SHal Finkel void BasicTTI::getUnrollingPreferences(Loop *, UnrollingPreferences &) const { }
1988f2e7005SHal Finkel 
199664e354dSChandler Carruth //===----------------------------------------------------------------------===//
200664e354dSChandler Carruth //
201664e354dSChandler Carruth // Calls used by the vectorizers.
202664e354dSChandler Carruth //
203664e354dSChandler Carruth //===----------------------------------------------------------------------===//
204664e354dSChandler Carruth 
205664e354dSChandler Carruth unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
206664e354dSChandler Carruth                                             bool Extract) const {
207664e354dSChandler Carruth   assert (Ty->isVectorTy() && "Can only scalarize vectors");
208664e354dSChandler Carruth   unsigned Cost = 0;
209664e354dSChandler Carruth 
210664e354dSChandler Carruth   for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
211664e354dSChandler Carruth     if (Insert)
212664e354dSChandler Carruth       Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
213664e354dSChandler Carruth     if (Extract)
214664e354dSChandler Carruth       Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
215664e354dSChandler Carruth   }
216664e354dSChandler Carruth 
217664e354dSChandler Carruth   return Cost;
218664e354dSChandler Carruth }
219664e354dSChandler Carruth 
220664e354dSChandler Carruth unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
221664e354dSChandler Carruth   return 1;
222664e354dSChandler Carruth }
223664e354dSChandler Carruth 
224b1791a75SNadav Rotem unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
225b1791a75SNadav Rotem   return 32;
226b1791a75SNadav Rotem }
227b1791a75SNadav Rotem 
228b696c36fSNadav Rotem unsigned BasicTTI::getMaximumUnrollFactor() const {
229b696c36fSNadav Rotem   return 1;
230b696c36fSNadav Rotem }
231b696c36fSNadav Rotem 
232b9773871SArnold Schwaighofer unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
233b9773871SArnold Schwaighofer                                           OperandValueKind,
234b9773871SArnold Schwaighofer                                           OperandValueKind) const {
235664e354dSChandler Carruth   // Check if any of the operands are vector operands.
236afc1036fSBill Wendling   const TargetLoweringBase *TLI = getTLI();
237664e354dSChandler Carruth   int ISD = TLI->InstructionOpcodeToISD(Opcode);
238664e354dSChandler Carruth   assert(ISD && "Invalid opcode");
239664e354dSChandler Carruth 
240664e354dSChandler Carruth   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
241664e354dSChandler Carruth 
24287a0af6eSNadav Rotem   bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
2430db0690aSNadav Rotem   // Assume that floating point arithmetic operations cost twice as much as
2440db0690aSNadav Rotem   // integer operations.
24587a0af6eSNadav Rotem   unsigned OpCost = (IsFloat ? 2 : 1);
24687a0af6eSNadav Rotem 
247664e354dSChandler Carruth   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
248664e354dSChandler Carruth     // The operation is legal. Assume it costs 1.
2490db0690aSNadav Rotem     // If the type is split to multiple registers, assume that there is some
250664e354dSChandler Carruth     // overhead to this.
251664e354dSChandler Carruth     // TODO: Once we have extract/insert subvector cost we need to use them.
252664e354dSChandler Carruth     if (LT.first > 1)
25387a0af6eSNadav Rotem       return LT.first * 2 * OpCost;
25487a0af6eSNadav Rotem     return LT.first * 1 * OpCost;
255664e354dSChandler Carruth   }
256664e354dSChandler Carruth 
257664e354dSChandler Carruth   if (!TLI->isOperationExpand(ISD, LT.second)) {
258664e354dSChandler Carruth     // If the operation is custom lowered then assume
259664e354dSChandler Carruth     // thare the code is twice as expensive.
26087a0af6eSNadav Rotem     return LT.first * 2 * OpCost;
261664e354dSChandler Carruth   }
262664e354dSChandler Carruth 
263664e354dSChandler Carruth   // Else, assume that we need to scalarize this op.
264664e354dSChandler Carruth   if (Ty->isVectorTy()) {
265664e354dSChandler Carruth     unsigned Num = Ty->getVectorNumElements();
266664e354dSChandler Carruth     unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
267664e354dSChandler Carruth     // return the cost of multiple scalar invocation plus the cost of inserting
268664e354dSChandler Carruth     // and extracting the values.
269664e354dSChandler Carruth     return getScalarizationOverhead(Ty, true, true) + Num * Cost;
270664e354dSChandler Carruth   }
271664e354dSChandler Carruth 
272664e354dSChandler Carruth   // We don't know anything about this scalar instruction.
27387a0af6eSNadav Rotem   return OpCost;
274664e354dSChandler Carruth }
275664e354dSChandler Carruth 
276664e354dSChandler Carruth unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
277664e354dSChandler Carruth                                   Type *SubTp) const {
278664e354dSChandler Carruth   return 1;
279664e354dSChandler Carruth }
280664e354dSChandler Carruth 
281664e354dSChandler Carruth unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
282664e354dSChandler Carruth                                     Type *Src) const {
283afc1036fSBill Wendling   const TargetLoweringBase *TLI = getTLI();
284664e354dSChandler Carruth   int ISD = TLI->InstructionOpcodeToISD(Opcode);
285664e354dSChandler Carruth   assert(ISD && "Invalid opcode");
286664e354dSChandler Carruth 
287664e354dSChandler Carruth   std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
288664e354dSChandler Carruth   std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
289664e354dSChandler Carruth 
290e55aa3c8SNadav Rotem   // Check for NOOP conversions.
291e55aa3c8SNadav Rotem   if (SrcLT.first == DstLT.first &&
292e55aa3c8SNadav Rotem       SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
293664e354dSChandler Carruth 
294e55aa3c8SNadav Rotem       // Bitcast between types that are legalized to the same type are free.
295e55aa3c8SNadav Rotem       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
296664e354dSChandler Carruth         return 0;
297e55aa3c8SNadav Rotem   }
298664e354dSChandler Carruth 
299664e354dSChandler Carruth   if (Opcode == Instruction::Trunc &&
300664e354dSChandler Carruth       TLI->isTruncateFree(SrcLT.second, DstLT.second))
301664e354dSChandler Carruth     return 0;
302664e354dSChandler Carruth 
303664e354dSChandler Carruth   if (Opcode == Instruction::ZExt &&
304664e354dSChandler Carruth       TLI->isZExtFree(SrcLT.second, DstLT.second))
305664e354dSChandler Carruth     return 0;
306664e354dSChandler Carruth 
307e55aa3c8SNadav Rotem   // If the cast is marked as legal (or promote) then assume low cost.
308e55aa3c8SNadav Rotem   if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
309e55aa3c8SNadav Rotem     return 1;
310e55aa3c8SNadav Rotem 
311e55aa3c8SNadav Rotem   // Handle scalar conversions.
312e55aa3c8SNadav Rotem   if (!Src->isVectorTy() && !Dst->isVectorTy()) {
313e55aa3c8SNadav Rotem 
314e55aa3c8SNadav Rotem     // Scalar bitcasts are usually free.
315e55aa3c8SNadav Rotem     if (Opcode == Instruction::BitCast)
316e55aa3c8SNadav Rotem       return 0;
317e55aa3c8SNadav Rotem 
318664e354dSChandler Carruth     // Just check the op cost. If the operation is legal then assume it costs 1.
319664e354dSChandler Carruth     if (!TLI->isOperationExpand(ISD, DstLT.second))
320664e354dSChandler Carruth       return  1;
321664e354dSChandler Carruth 
322664e354dSChandler Carruth     // Assume that illegal scalar instruction are expensive.
323664e354dSChandler Carruth     return 4;
324664e354dSChandler Carruth   }
325664e354dSChandler Carruth 
326664e354dSChandler Carruth   // Check vector-to-vector casts.
327664e354dSChandler Carruth   if (Dst->isVectorTy() && Src->isVectorTy()) {
328664e354dSChandler Carruth 
329664e354dSChandler Carruth     // If the cast is between same-sized registers, then the check is simple.
330664e354dSChandler Carruth     if (SrcLT.first == DstLT.first &&
331664e354dSChandler Carruth         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
332664e354dSChandler Carruth 
333664e354dSChandler Carruth       // Assume that Zext is done using AND.
334664e354dSChandler Carruth       if (Opcode == Instruction::ZExt)
335664e354dSChandler Carruth         return 1;
336664e354dSChandler Carruth 
337664e354dSChandler Carruth       // Assume that sext is done using SHL and SRA.
338664e354dSChandler Carruth       if (Opcode == Instruction::SExt)
339664e354dSChandler Carruth         return 2;
340664e354dSChandler Carruth 
341664e354dSChandler Carruth       // Just check the op cost. If the operation is legal then assume it costs
342664e354dSChandler Carruth       // 1 and multiply by the type-legalization overhead.
343664e354dSChandler Carruth       if (!TLI->isOperationExpand(ISD, DstLT.second))
344664e354dSChandler Carruth         return SrcLT.first * 1;
345664e354dSChandler Carruth     }
346664e354dSChandler Carruth 
347664e354dSChandler Carruth     // If we are converting vectors and the operation is illegal, or
348664e354dSChandler Carruth     // if the vectors are legalized to different types, estimate the
349664e354dSChandler Carruth     // scalarization costs.
350664e354dSChandler Carruth     unsigned Num = Dst->getVectorNumElements();
351664e354dSChandler Carruth     unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
352664e354dSChandler Carruth                                              Src->getScalarType());
353664e354dSChandler Carruth 
354664e354dSChandler Carruth     // Return the cost of multiple scalar invocation plus the cost of
355664e354dSChandler Carruth     // inserting and extracting the values.
356664e354dSChandler Carruth     return getScalarizationOverhead(Dst, true, true) + Num * Cost;
357664e354dSChandler Carruth   }
358664e354dSChandler Carruth 
359664e354dSChandler Carruth   // We already handled vector-to-vector and scalar-to-scalar conversions. This
360664e354dSChandler Carruth   // is where we handle bitcast between vectors and scalars. We need to assume
361664e354dSChandler Carruth   //  that the conversion is scalarized in one way or another.
362664e354dSChandler Carruth   if (Opcode == Instruction::BitCast)
363664e354dSChandler Carruth     // Illegal bitcasts are done by storing and loading from a stack slot.
364664e354dSChandler Carruth     return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
365664e354dSChandler Carruth            (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
366664e354dSChandler Carruth 
367664e354dSChandler Carruth   llvm_unreachable("Unhandled cast");
368664e354dSChandler Carruth  }
369664e354dSChandler Carruth 
370664e354dSChandler Carruth unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
371664e354dSChandler Carruth   // Branches are assumed to be predicted.
372664e354dSChandler Carruth   return 0;
373664e354dSChandler Carruth }
374664e354dSChandler Carruth 
375664e354dSChandler Carruth unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
376664e354dSChandler Carruth                                       Type *CondTy) const {
377afc1036fSBill Wendling   const TargetLoweringBase *TLI = getTLI();
378664e354dSChandler Carruth   int ISD = TLI->InstructionOpcodeToISD(Opcode);
379664e354dSChandler Carruth   assert(ISD && "Invalid opcode");
380664e354dSChandler Carruth 
381664e354dSChandler Carruth   // Selects on vectors are actually vector selects.
382664e354dSChandler Carruth   if (ISD == ISD::SELECT) {
383664e354dSChandler Carruth     assert(CondTy && "CondTy must exist");
384664e354dSChandler Carruth     if (CondTy->isVectorTy())
385664e354dSChandler Carruth       ISD = ISD::VSELECT;
386664e354dSChandler Carruth   }
387664e354dSChandler Carruth 
388664e354dSChandler Carruth   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
389664e354dSChandler Carruth 
390664e354dSChandler Carruth   if (!TLI->isOperationExpand(ISD, LT.second)) {
391664e354dSChandler Carruth     // The operation is legal. Assume it costs 1. Multiply
392664e354dSChandler Carruth     // by the type-legalization overhead.
393664e354dSChandler Carruth     return LT.first * 1;
394664e354dSChandler Carruth   }
395664e354dSChandler Carruth 
396664e354dSChandler Carruth   // Otherwise, assume that the cast is scalarized.
397664e354dSChandler Carruth   if (ValTy->isVectorTy()) {
398664e354dSChandler Carruth     unsigned Num = ValTy->getVectorNumElements();
399664e354dSChandler Carruth     if (CondTy)
400664e354dSChandler Carruth       CondTy = CondTy->getScalarType();
401664e354dSChandler Carruth     unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
402664e354dSChandler Carruth                                                CondTy);
403664e354dSChandler Carruth 
404664e354dSChandler Carruth     // Return the cost of multiple scalar invocation plus the cost of inserting
405664e354dSChandler Carruth     // and extracting the values.
406664e354dSChandler Carruth     return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
407664e354dSChandler Carruth   }
408664e354dSChandler Carruth 
409664e354dSChandler Carruth   // Unknown scalar opcode.
410664e354dSChandler Carruth   return 1;
411664e354dSChandler Carruth }
412664e354dSChandler Carruth 
413664e354dSChandler Carruth unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
414664e354dSChandler Carruth                                       unsigned Index) const {
415664e354dSChandler Carruth   return 1;
416664e354dSChandler Carruth }
417664e354dSChandler Carruth 
418664e354dSChandler Carruth unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
419664e354dSChandler Carruth                                    unsigned Alignment,
420664e354dSChandler Carruth                                    unsigned AddressSpace) const {
421664e354dSChandler Carruth   assert(!Src->isVoidTy() && "Invalid type");
422afc1036fSBill Wendling   std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
423664e354dSChandler Carruth 
424664e354dSChandler Carruth   // Assume that all loads of legal types cost 1.
425664e354dSChandler Carruth   return LT.first;
426664e354dSChandler Carruth }
427664e354dSChandler Carruth 
428f7cfac7aSBenjamin Kramer unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
429664e354dSChandler Carruth                                          ArrayRef<Type *> Tys) const {
430f7cfac7aSBenjamin Kramer   unsigned ISD = 0;
431f7cfac7aSBenjamin Kramer   switch (IID) {
432f7cfac7aSBenjamin Kramer   default: {
433f7cfac7aSBenjamin Kramer     // Assume that we need to scalarize this intrinsic.
434664e354dSChandler Carruth     unsigned ScalarizationCost = 0;
435664e354dSChandler Carruth     unsigned ScalarCalls = 1;
436664e354dSChandler Carruth     if (RetTy->isVectorTy()) {
437664e354dSChandler Carruth       ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
438664e354dSChandler Carruth       ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
439664e354dSChandler Carruth     }
440664e354dSChandler Carruth     for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
441664e354dSChandler Carruth       if (Tys[i]->isVectorTy()) {
442664e354dSChandler Carruth         ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
443664e354dSChandler Carruth         ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
444664e354dSChandler Carruth       }
445664e354dSChandler Carruth     }
446f7cfac7aSBenjamin Kramer 
447664e354dSChandler Carruth     return ScalarCalls + ScalarizationCost;
448664e354dSChandler Carruth   }
449f7cfac7aSBenjamin Kramer   // Look for intrinsics that can be lowered directly or turned into a scalar
450f7cfac7aSBenjamin Kramer   // intrinsic call.
451f7cfac7aSBenjamin Kramer   case Intrinsic::sqrt:    ISD = ISD::FSQRT;  break;
452f7cfac7aSBenjamin Kramer   case Intrinsic::sin:     ISD = ISD::FSIN;   break;
453f7cfac7aSBenjamin Kramer   case Intrinsic::cos:     ISD = ISD::FCOS;   break;
454f7cfac7aSBenjamin Kramer   case Intrinsic::exp:     ISD = ISD::FEXP;   break;
455f7cfac7aSBenjamin Kramer   case Intrinsic::exp2:    ISD = ISD::FEXP2;  break;
456f7cfac7aSBenjamin Kramer   case Intrinsic::log:     ISD = ISD::FLOG;   break;
457f7cfac7aSBenjamin Kramer   case Intrinsic::log10:   ISD = ISD::FLOG10; break;
458f7cfac7aSBenjamin Kramer   case Intrinsic::log2:    ISD = ISD::FLOG2;  break;
459f7cfac7aSBenjamin Kramer   case Intrinsic::fabs:    ISD = ISD::FABS;   break;
4600c5c01aaSHal Finkel   case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
461f7cfac7aSBenjamin Kramer   case Intrinsic::floor:   ISD = ISD::FFLOOR; break;
462f7cfac7aSBenjamin Kramer   case Intrinsic::ceil:    ISD = ISD::FCEIL;  break;
463f7cfac7aSBenjamin Kramer   case Intrinsic::trunc:   ISD = ISD::FTRUNC; break;
464ec474f28SHal Finkel   case Intrinsic::nearbyint:
465ec474f28SHal Finkel                            ISD = ISD::FNEARBYINT; break;
466f7cfac7aSBenjamin Kramer   case Intrinsic::rint:    ISD = ISD::FRINT;  break;
467171817eeSHal Finkel   case Intrinsic::round:   ISD = ISD::FROUND; break;
468f7cfac7aSBenjamin Kramer   case Intrinsic::pow:     ISD = ISD::FPOW;   break;
469f7cfac7aSBenjamin Kramer   case Intrinsic::fma:     ISD = ISD::FMA;    break;
470f7cfac7aSBenjamin Kramer   case Intrinsic::fmuladd: ISD = ISD::FMA;    break; // FIXME: mul + add?
471a7cd6bf3SArnold Schwaighofer   case Intrinsic::lifetime_start:
472a7cd6bf3SArnold Schwaighofer   case Intrinsic::lifetime_end:
473a7cd6bf3SArnold Schwaighofer     return 0;
474f7cfac7aSBenjamin Kramer   }
475f7cfac7aSBenjamin Kramer 
476afc1036fSBill Wendling   const TargetLoweringBase *TLI = getTLI();
477f7cfac7aSBenjamin Kramer   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
478f7cfac7aSBenjamin Kramer 
479f7cfac7aSBenjamin Kramer   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
480f7cfac7aSBenjamin Kramer     // The operation is legal. Assume it costs 1.
481f7cfac7aSBenjamin Kramer     // If the type is split to multiple registers, assume that thre is some
482f7cfac7aSBenjamin Kramer     // overhead to this.
483f7cfac7aSBenjamin Kramer     // TODO: Once we have extract/insert subvector cost we need to use them.
484f7cfac7aSBenjamin Kramer     if (LT.first > 1)
485f7cfac7aSBenjamin Kramer       return LT.first * 2;
486f7cfac7aSBenjamin Kramer     return LT.first * 1;
487f7cfac7aSBenjamin Kramer   }
488f7cfac7aSBenjamin Kramer 
489f7cfac7aSBenjamin Kramer   if (!TLI->isOperationExpand(ISD, LT.second)) {
490f7cfac7aSBenjamin Kramer     // If the operation is custom lowered then assume
491f7cfac7aSBenjamin Kramer     // thare the code is twice as expensive.
492f7cfac7aSBenjamin Kramer     return LT.first * 2;
493f7cfac7aSBenjamin Kramer   }
494f7cfac7aSBenjamin Kramer 
495f7cfac7aSBenjamin Kramer   // Else, assume that we need to scalarize this intrinsic. For math builtins
496f7cfac7aSBenjamin Kramer   // this will emit a costly libcall, adding call overhead and spills. Make it
497f7cfac7aSBenjamin Kramer   // very expensive.
498f7cfac7aSBenjamin Kramer   if (RetTy->isVectorTy()) {
499f7cfac7aSBenjamin Kramer     unsigned Num = RetTy->getVectorNumElements();
500f7cfac7aSBenjamin Kramer     unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
501f7cfac7aSBenjamin Kramer                                                   Tys);
502f7cfac7aSBenjamin Kramer     return 10 * Cost * Num;
503f7cfac7aSBenjamin Kramer   }
504f7cfac7aSBenjamin Kramer 
505f7cfac7aSBenjamin Kramer   // This is going to be turned into a library call, make it expensive.
506f7cfac7aSBenjamin Kramer   return 10;
507f7cfac7aSBenjamin Kramer }
508664e354dSChandler Carruth 
509664e354dSChandler Carruth unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
510afc1036fSBill Wendling   std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
511664e354dSChandler Carruth   return LT.first;
512664e354dSChandler Carruth }
513594fa2dcSArnold Schwaighofer 
5149da9a43aSArnold Schwaighofer unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
515594fa2dcSArnold Schwaighofer   return 0;
516594fa2dcSArnold Schwaighofer }
517cae8735aSArnold Schwaighofer 
518cae8735aSArnold Schwaighofer unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty,
519cae8735aSArnold Schwaighofer                                     bool IsPairwise) const {
520cae8735aSArnold Schwaighofer   assert(Ty->isVectorTy() && "Expect a vector type");
521cae8735aSArnold Schwaighofer   unsigned NumVecElts = Ty->getVectorNumElements();
522cae8735aSArnold Schwaighofer   unsigned NumReduxLevels = Log2_32(NumVecElts);
523cae8735aSArnold Schwaighofer   unsigned ArithCost = NumReduxLevels *
524cae8735aSArnold Schwaighofer     TopTTI->getArithmeticInstrCost(Opcode, Ty);
525cae8735aSArnold Schwaighofer   // Assume the pairwise shuffles add a cost.
526cae8735aSArnold Schwaighofer   unsigned ShuffleCost =
527cae8735aSArnold Schwaighofer       NumReduxLevels * (IsPairwise + 1) *
528cae8735aSArnold Schwaighofer       TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
529cae8735aSArnold Schwaighofer   return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
530cae8735aSArnold Schwaighofer }
531