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 
28664e354dSChandler Carruth class BasicTTI : public ImmutablePass, public TargetTransformInfo {
2956b31bd9SBenjamin Kramer   const TargetLoweringBase *TLI;
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 
35664e354dSChandler Carruth public:
36664e354dSChandler Carruth   BasicTTI() : ImmutablePass(ID), TLI(0) {
37664e354dSChandler Carruth     llvm_unreachable("This pass cannot be directly constructed");
38664e354dSChandler Carruth   }
39664e354dSChandler Carruth 
4056b31bd9SBenjamin Kramer   BasicTTI(const TargetLoweringBase *TLI) : ImmutablePass(ID), TLI(TLI) {
41664e354dSChandler Carruth     initializeBasicTTIPass(*PassRegistry::getPassRegistry());
42664e354dSChandler Carruth   }
43664e354dSChandler Carruth 
44664e354dSChandler Carruth   virtual void initializePass() {
45664e354dSChandler Carruth     pushTTIStack(this);
46664e354dSChandler Carruth   }
47664e354dSChandler Carruth 
48664e354dSChandler Carruth   virtual void finalizePass() {
49664e354dSChandler Carruth     popTTIStack();
50664e354dSChandler Carruth   }
51664e354dSChandler Carruth 
52664e354dSChandler Carruth   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53664e354dSChandler Carruth     TargetTransformInfo::getAnalysisUsage(AU);
54664e354dSChandler Carruth   }
55664e354dSChandler Carruth 
56664e354dSChandler Carruth   /// Pass identification.
57664e354dSChandler Carruth   static char ID;
58664e354dSChandler Carruth 
59664e354dSChandler Carruth   /// Provide necessary pointer adjustments for the two base classes.
60664e354dSChandler Carruth   virtual void *getAdjustedAnalysisPointer(const void *ID) {
61664e354dSChandler Carruth     if (ID == &TargetTransformInfo::ID)
62664e354dSChandler Carruth       return (TargetTransformInfo*)this;
63664e354dSChandler Carruth     return this;
64664e354dSChandler Carruth   }
65664e354dSChandler Carruth 
66664e354dSChandler Carruth   /// \name Scalar TTI Implementations
67664e354dSChandler Carruth   /// @{
68664e354dSChandler Carruth 
69664e354dSChandler Carruth   virtual bool isLegalAddImmediate(int64_t imm) const;
70664e354dSChandler Carruth   virtual bool isLegalICmpImmediate(int64_t imm) const;
71664e354dSChandler Carruth   virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
72664e354dSChandler Carruth                                      int64_t BaseOffset, bool HasBaseReg,
73664e354dSChandler Carruth                                      int64_t Scale) const;
74*bf490d4aSQuentin Colombet   virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
75*bf490d4aSQuentin Colombet                                    int64_t BaseOffset, bool HasBaseReg,
76*bf490d4aSQuentin Colombet                                    int64_t Scale) const;
77664e354dSChandler Carruth   virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
78664e354dSChandler Carruth   virtual bool isTypeLegal(Type *Ty) const;
79664e354dSChandler Carruth   virtual unsigned getJumpBufAlignment() const;
80664e354dSChandler Carruth   virtual unsigned getJumpBufSize() const;
81664e354dSChandler Carruth   virtual bool shouldBuildLookupTables() const;
82664e354dSChandler Carruth 
83664e354dSChandler Carruth   /// @}
84664e354dSChandler Carruth 
85664e354dSChandler Carruth   /// \name Vector TTI Implementations
86664e354dSChandler Carruth   /// @{
87664e354dSChandler Carruth 
88664e354dSChandler Carruth   virtual unsigned getNumberOfRegisters(bool Vector) const;
89b696c36fSNadav Rotem   virtual unsigned getMaximumUnrollFactor() const;
90b1791a75SNadav Rotem   virtual unsigned getRegisterBitWidth(bool Vector) const;
91b9773871SArnold Schwaighofer   virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
92b9773871SArnold Schwaighofer                                           OperandValueKind,
93b9773871SArnold Schwaighofer                                           OperandValueKind) const;
94664e354dSChandler Carruth   virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
95664e354dSChandler Carruth                                   int Index, Type *SubTp) const;
96664e354dSChandler Carruth   virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
97664e354dSChandler Carruth                                     Type *Src) const;
98664e354dSChandler Carruth   virtual unsigned getCFInstrCost(unsigned Opcode) const;
99664e354dSChandler Carruth   virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
100664e354dSChandler Carruth                                       Type *CondTy) const;
101664e354dSChandler Carruth   virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
102664e354dSChandler Carruth                                       unsigned Index) const;
103664e354dSChandler Carruth   virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
104664e354dSChandler Carruth                                    unsigned Alignment,
105664e354dSChandler Carruth                                    unsigned AddressSpace) const;
106664e354dSChandler Carruth   virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
107664e354dSChandler Carruth                                          ArrayRef<Type*> Tys) const;
108664e354dSChandler Carruth   virtual unsigned getNumberOfParts(Type *Tp) const;
109594fa2dcSArnold Schwaighofer   virtual unsigned getAddressComputationCost(Type *Ty) const;
110664e354dSChandler Carruth 
111664e354dSChandler Carruth   /// @}
112664e354dSChandler Carruth };
113664e354dSChandler Carruth 
114664e354dSChandler Carruth }
115664e354dSChandler Carruth 
116664e354dSChandler Carruth INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
117664e354dSChandler Carruth                    "Target independent code generator's TTI", true, true, false)
118664e354dSChandler Carruth char BasicTTI::ID = 0;
119664e354dSChandler Carruth 
120664e354dSChandler Carruth ImmutablePass *
12156b31bd9SBenjamin Kramer llvm::createBasicTargetTransformInfoPass(const TargetLoweringBase *TLI) {
122664e354dSChandler Carruth   return new BasicTTI(TLI);
123664e354dSChandler Carruth }
124664e354dSChandler Carruth 
125664e354dSChandler Carruth 
126664e354dSChandler Carruth bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
127664e354dSChandler Carruth   return TLI->isLegalAddImmediate(imm);
128664e354dSChandler Carruth }
129664e354dSChandler Carruth 
130664e354dSChandler Carruth bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
131664e354dSChandler Carruth   return TLI->isLegalICmpImmediate(imm);
132664e354dSChandler Carruth }
133664e354dSChandler Carruth 
134664e354dSChandler Carruth bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
135664e354dSChandler Carruth                                      int64_t BaseOffset, bool HasBaseReg,
136664e354dSChandler Carruth                                      int64_t Scale) const {
13756b31bd9SBenjamin Kramer   TargetLoweringBase::AddrMode AM;
138664e354dSChandler Carruth   AM.BaseGV = BaseGV;
139664e354dSChandler Carruth   AM.BaseOffs = BaseOffset;
140664e354dSChandler Carruth   AM.HasBaseReg = HasBaseReg;
141664e354dSChandler Carruth   AM.Scale = Scale;
142664e354dSChandler Carruth   return TLI->isLegalAddressingMode(AM, Ty);
143664e354dSChandler Carruth }
144664e354dSChandler Carruth 
145*bf490d4aSQuentin Colombet int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
146*bf490d4aSQuentin Colombet                                    int64_t BaseOffset, bool HasBaseReg,
147*bf490d4aSQuentin Colombet                                    int64_t Scale) const {
148*bf490d4aSQuentin Colombet   TargetLoweringBase::AddrMode AM;
149*bf490d4aSQuentin Colombet   AM.BaseGV = BaseGV;
150*bf490d4aSQuentin Colombet   AM.BaseOffs = BaseOffset;
151*bf490d4aSQuentin Colombet   AM.HasBaseReg = HasBaseReg;
152*bf490d4aSQuentin Colombet   AM.Scale = Scale;
153*bf490d4aSQuentin Colombet   return TLI->getScalingFactorCost(AM, Ty);
154*bf490d4aSQuentin Colombet }
155*bf490d4aSQuentin Colombet 
156664e354dSChandler Carruth bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
157664e354dSChandler Carruth   return TLI->isTruncateFree(Ty1, Ty2);
158664e354dSChandler Carruth }
159664e354dSChandler Carruth 
160664e354dSChandler Carruth bool BasicTTI::isTypeLegal(Type *Ty) const {
161664e354dSChandler Carruth   EVT T = TLI->getValueType(Ty);
162664e354dSChandler Carruth   return TLI->isTypeLegal(T);
163664e354dSChandler Carruth }
164664e354dSChandler Carruth 
165664e354dSChandler Carruth unsigned BasicTTI::getJumpBufAlignment() const {
166664e354dSChandler Carruth   return TLI->getJumpBufAlignment();
167664e354dSChandler Carruth }
168664e354dSChandler Carruth 
169664e354dSChandler Carruth unsigned BasicTTI::getJumpBufSize() const {
170664e354dSChandler Carruth   return TLI->getJumpBufSize();
171664e354dSChandler Carruth }
172664e354dSChandler Carruth 
173664e354dSChandler Carruth bool BasicTTI::shouldBuildLookupTables() const {
174664e354dSChandler Carruth   return TLI->supportJumpTables() &&
175664e354dSChandler Carruth       (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
176664e354dSChandler Carruth        TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
177664e354dSChandler Carruth }
178664e354dSChandler Carruth 
179664e354dSChandler Carruth //===----------------------------------------------------------------------===//
180664e354dSChandler Carruth //
181664e354dSChandler Carruth // Calls used by the vectorizers.
182664e354dSChandler Carruth //
183664e354dSChandler Carruth //===----------------------------------------------------------------------===//
184664e354dSChandler Carruth 
185664e354dSChandler Carruth unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
186664e354dSChandler Carruth                                             bool Extract) const {
187664e354dSChandler Carruth   assert (Ty->isVectorTy() && "Can only scalarize vectors");
188664e354dSChandler Carruth   unsigned Cost = 0;
189664e354dSChandler Carruth 
190664e354dSChandler Carruth   for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
191664e354dSChandler Carruth     if (Insert)
192664e354dSChandler Carruth       Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
193664e354dSChandler Carruth     if (Extract)
194664e354dSChandler Carruth       Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
195664e354dSChandler Carruth   }
196664e354dSChandler Carruth 
197664e354dSChandler Carruth   return Cost;
198664e354dSChandler Carruth }
199664e354dSChandler Carruth 
200664e354dSChandler Carruth unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
201664e354dSChandler Carruth   return 1;
202664e354dSChandler Carruth }
203664e354dSChandler Carruth 
204b1791a75SNadav Rotem unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
205b1791a75SNadav Rotem   return 32;
206b1791a75SNadav Rotem }
207b1791a75SNadav Rotem 
208b696c36fSNadav Rotem unsigned BasicTTI::getMaximumUnrollFactor() const {
209b696c36fSNadav Rotem   return 1;
210b696c36fSNadav Rotem }
211b696c36fSNadav Rotem 
212b9773871SArnold Schwaighofer unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
213b9773871SArnold Schwaighofer                                           OperandValueKind,
214b9773871SArnold Schwaighofer                                           OperandValueKind) const {
215664e354dSChandler Carruth   // Check if any of the operands are vector operands.
216664e354dSChandler Carruth   int ISD = TLI->InstructionOpcodeToISD(Opcode);
217664e354dSChandler Carruth   assert(ISD && "Invalid opcode");
218664e354dSChandler Carruth 
219664e354dSChandler Carruth   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
220664e354dSChandler Carruth 
22187a0af6eSNadav Rotem   bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
2220db0690aSNadav Rotem   // Assume that floating point arithmetic operations cost twice as much as
2230db0690aSNadav Rotem   // integer operations.
22487a0af6eSNadav Rotem   unsigned OpCost = (IsFloat ? 2 : 1);
22587a0af6eSNadav Rotem 
226664e354dSChandler Carruth   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
227664e354dSChandler Carruth     // The operation is legal. Assume it costs 1.
2280db0690aSNadav Rotem     // If the type is split to multiple registers, assume that there is some
229664e354dSChandler Carruth     // overhead to this.
230664e354dSChandler Carruth     // TODO: Once we have extract/insert subvector cost we need to use them.
231664e354dSChandler Carruth     if (LT.first > 1)
23287a0af6eSNadav Rotem       return LT.first * 2 * OpCost;
23387a0af6eSNadav Rotem     return LT.first * 1 * OpCost;
234664e354dSChandler Carruth   }
235664e354dSChandler Carruth 
236664e354dSChandler Carruth   if (!TLI->isOperationExpand(ISD, LT.second)) {
237664e354dSChandler Carruth     // If the operation is custom lowered then assume
238664e354dSChandler Carruth     // thare the code is twice as expensive.
23987a0af6eSNadav Rotem     return LT.first * 2 * OpCost;
240664e354dSChandler Carruth   }
241664e354dSChandler Carruth 
242664e354dSChandler Carruth   // Else, assume that we need to scalarize this op.
243664e354dSChandler Carruth   if (Ty->isVectorTy()) {
244664e354dSChandler Carruth     unsigned Num = Ty->getVectorNumElements();
245664e354dSChandler Carruth     unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
246664e354dSChandler Carruth     // return the cost of multiple scalar invocation plus the cost of inserting
247664e354dSChandler Carruth     // and extracting the values.
248664e354dSChandler Carruth     return getScalarizationOverhead(Ty, true, true) + Num * Cost;
249664e354dSChandler Carruth   }
250664e354dSChandler Carruth 
251664e354dSChandler Carruth   // We don't know anything about this scalar instruction.
25287a0af6eSNadav Rotem   return OpCost;
253664e354dSChandler Carruth }
254664e354dSChandler Carruth 
255664e354dSChandler Carruth unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
256664e354dSChandler Carruth                                   Type *SubTp) const {
257664e354dSChandler Carruth   return 1;
258664e354dSChandler Carruth }
259664e354dSChandler Carruth 
260664e354dSChandler Carruth unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
261664e354dSChandler Carruth                                     Type *Src) const {
262664e354dSChandler Carruth   int ISD = TLI->InstructionOpcodeToISD(Opcode);
263664e354dSChandler Carruth   assert(ISD && "Invalid opcode");
264664e354dSChandler Carruth 
265664e354dSChandler Carruth   std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
266664e354dSChandler Carruth   std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
267664e354dSChandler Carruth 
268e55aa3c8SNadav Rotem   // Check for NOOP conversions.
269e55aa3c8SNadav Rotem   if (SrcLT.first == DstLT.first &&
270e55aa3c8SNadav Rotem       SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
271664e354dSChandler Carruth 
272e55aa3c8SNadav Rotem       // Bitcast between types that are legalized to the same type are free.
273e55aa3c8SNadav Rotem       if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
274664e354dSChandler Carruth         return 0;
275e55aa3c8SNadav Rotem   }
276664e354dSChandler Carruth 
277664e354dSChandler Carruth   if (Opcode == Instruction::Trunc &&
278664e354dSChandler Carruth       TLI->isTruncateFree(SrcLT.second, DstLT.second))
279664e354dSChandler Carruth     return 0;
280664e354dSChandler Carruth 
281664e354dSChandler Carruth   if (Opcode == Instruction::ZExt &&
282664e354dSChandler Carruth       TLI->isZExtFree(SrcLT.second, DstLT.second))
283664e354dSChandler Carruth     return 0;
284664e354dSChandler Carruth 
285e55aa3c8SNadav Rotem   // If the cast is marked as legal (or promote) then assume low cost.
286e55aa3c8SNadav Rotem   if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
287e55aa3c8SNadav Rotem     return 1;
288e55aa3c8SNadav Rotem 
289e55aa3c8SNadav Rotem   // Handle scalar conversions.
290e55aa3c8SNadav Rotem   if (!Src->isVectorTy() && !Dst->isVectorTy()) {
291e55aa3c8SNadav Rotem 
292e55aa3c8SNadav Rotem     // Scalar bitcasts are usually free.
293e55aa3c8SNadav Rotem     if (Opcode == Instruction::BitCast)
294e55aa3c8SNadav Rotem       return 0;
295e55aa3c8SNadav Rotem 
296664e354dSChandler Carruth     // Just check the op cost. If the operation is legal then assume it costs 1.
297664e354dSChandler Carruth     if (!TLI->isOperationExpand(ISD, DstLT.second))
298664e354dSChandler Carruth       return  1;
299664e354dSChandler Carruth 
300664e354dSChandler Carruth     // Assume that illegal scalar instruction are expensive.
301664e354dSChandler Carruth     return 4;
302664e354dSChandler Carruth   }
303664e354dSChandler Carruth 
304664e354dSChandler Carruth   // Check vector-to-vector casts.
305664e354dSChandler Carruth   if (Dst->isVectorTy() && Src->isVectorTy()) {
306664e354dSChandler Carruth 
307664e354dSChandler Carruth     // If the cast is between same-sized registers, then the check is simple.
308664e354dSChandler Carruth     if (SrcLT.first == DstLT.first &&
309664e354dSChandler Carruth         SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
310664e354dSChandler Carruth 
311664e354dSChandler Carruth       // Assume that Zext is done using AND.
312664e354dSChandler Carruth       if (Opcode == Instruction::ZExt)
313664e354dSChandler Carruth         return 1;
314664e354dSChandler Carruth 
315664e354dSChandler Carruth       // Assume that sext is done using SHL and SRA.
316664e354dSChandler Carruth       if (Opcode == Instruction::SExt)
317664e354dSChandler Carruth         return 2;
318664e354dSChandler Carruth 
319664e354dSChandler Carruth       // Just check the op cost. If the operation is legal then assume it costs
320664e354dSChandler Carruth       // 1 and multiply by the type-legalization overhead.
321664e354dSChandler Carruth       if (!TLI->isOperationExpand(ISD, DstLT.second))
322664e354dSChandler Carruth         return SrcLT.first * 1;
323664e354dSChandler Carruth     }
324664e354dSChandler Carruth 
325664e354dSChandler Carruth     // If we are converting vectors and the operation is illegal, or
326664e354dSChandler Carruth     // if the vectors are legalized to different types, estimate the
327664e354dSChandler Carruth     // scalarization costs.
328664e354dSChandler Carruth     unsigned Num = Dst->getVectorNumElements();
329664e354dSChandler Carruth     unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
330664e354dSChandler Carruth                                              Src->getScalarType());
331664e354dSChandler Carruth 
332664e354dSChandler Carruth     // Return the cost of multiple scalar invocation plus the cost of
333664e354dSChandler Carruth     // inserting and extracting the values.
334664e354dSChandler Carruth     return getScalarizationOverhead(Dst, true, true) + Num * Cost;
335664e354dSChandler Carruth   }
336664e354dSChandler Carruth 
337664e354dSChandler Carruth   // We already handled vector-to-vector and scalar-to-scalar conversions. This
338664e354dSChandler Carruth   // is where we handle bitcast between vectors and scalars. We need to assume
339664e354dSChandler Carruth   //  that the conversion is scalarized in one way or another.
340664e354dSChandler Carruth   if (Opcode == Instruction::BitCast)
341664e354dSChandler Carruth     // Illegal bitcasts are done by storing and loading from a stack slot.
342664e354dSChandler Carruth     return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
343664e354dSChandler Carruth            (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
344664e354dSChandler Carruth 
345664e354dSChandler Carruth   llvm_unreachable("Unhandled cast");
346664e354dSChandler Carruth  }
347664e354dSChandler Carruth 
348664e354dSChandler Carruth unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
349664e354dSChandler Carruth   // Branches are assumed to be predicted.
350664e354dSChandler Carruth   return 0;
351664e354dSChandler Carruth }
352664e354dSChandler Carruth 
353664e354dSChandler Carruth unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
354664e354dSChandler Carruth                                       Type *CondTy) const {
355664e354dSChandler Carruth   int ISD = TLI->InstructionOpcodeToISD(Opcode);
356664e354dSChandler Carruth   assert(ISD && "Invalid opcode");
357664e354dSChandler Carruth 
358664e354dSChandler Carruth   // Selects on vectors are actually vector selects.
359664e354dSChandler Carruth   if (ISD == ISD::SELECT) {
360664e354dSChandler Carruth     assert(CondTy && "CondTy must exist");
361664e354dSChandler Carruth     if (CondTy->isVectorTy())
362664e354dSChandler Carruth       ISD = ISD::VSELECT;
363664e354dSChandler Carruth   }
364664e354dSChandler Carruth 
365664e354dSChandler Carruth   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
366664e354dSChandler Carruth 
367664e354dSChandler Carruth   if (!TLI->isOperationExpand(ISD, LT.second)) {
368664e354dSChandler Carruth     // The operation is legal. Assume it costs 1. Multiply
369664e354dSChandler Carruth     // by the type-legalization overhead.
370664e354dSChandler Carruth     return LT.first * 1;
371664e354dSChandler Carruth   }
372664e354dSChandler Carruth 
373664e354dSChandler Carruth   // Otherwise, assume that the cast is scalarized.
374664e354dSChandler Carruth   if (ValTy->isVectorTy()) {
375664e354dSChandler Carruth     unsigned Num = ValTy->getVectorNumElements();
376664e354dSChandler Carruth     if (CondTy)
377664e354dSChandler Carruth       CondTy = CondTy->getScalarType();
378664e354dSChandler Carruth     unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
379664e354dSChandler Carruth                                                CondTy);
380664e354dSChandler Carruth 
381664e354dSChandler Carruth     // Return the cost of multiple scalar invocation plus the cost of inserting
382664e354dSChandler Carruth     // and extracting the values.
383664e354dSChandler Carruth     return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
384664e354dSChandler Carruth   }
385664e354dSChandler Carruth 
386664e354dSChandler Carruth   // Unknown scalar opcode.
387664e354dSChandler Carruth   return 1;
388664e354dSChandler Carruth }
389664e354dSChandler Carruth 
390664e354dSChandler Carruth unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
391664e354dSChandler Carruth                                       unsigned Index) const {
392664e354dSChandler Carruth   return 1;
393664e354dSChandler Carruth }
394664e354dSChandler Carruth 
395664e354dSChandler Carruth unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
396664e354dSChandler Carruth                                    unsigned Alignment,
397664e354dSChandler Carruth                                    unsigned AddressSpace) const {
398664e354dSChandler Carruth   assert(!Src->isVoidTy() && "Invalid type");
399664e354dSChandler Carruth   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
400664e354dSChandler Carruth 
401664e354dSChandler Carruth   // Assume that all loads of legal types cost 1.
402664e354dSChandler Carruth   return LT.first;
403664e354dSChandler Carruth }
404664e354dSChandler Carruth 
405f7cfac7aSBenjamin Kramer unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
406664e354dSChandler Carruth                                          ArrayRef<Type *> Tys) const {
407f7cfac7aSBenjamin Kramer   unsigned ISD = 0;
408f7cfac7aSBenjamin Kramer   switch (IID) {
409f7cfac7aSBenjamin Kramer   default: {
410f7cfac7aSBenjamin Kramer     // Assume that we need to scalarize this intrinsic.
411664e354dSChandler Carruth     unsigned ScalarizationCost = 0;
412664e354dSChandler Carruth     unsigned ScalarCalls = 1;
413664e354dSChandler Carruth     if (RetTy->isVectorTy()) {
414664e354dSChandler Carruth       ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
415664e354dSChandler Carruth       ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
416664e354dSChandler Carruth     }
417664e354dSChandler Carruth     for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
418664e354dSChandler Carruth       if (Tys[i]->isVectorTy()) {
419664e354dSChandler Carruth         ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
420664e354dSChandler Carruth         ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
421664e354dSChandler Carruth       }
422664e354dSChandler Carruth     }
423f7cfac7aSBenjamin Kramer 
424664e354dSChandler Carruth     return ScalarCalls + ScalarizationCost;
425664e354dSChandler Carruth   }
426f7cfac7aSBenjamin Kramer   // Look for intrinsics that can be lowered directly or turned into a scalar
427f7cfac7aSBenjamin Kramer   // intrinsic call.
428f7cfac7aSBenjamin Kramer   case Intrinsic::sqrt:    ISD = ISD::FSQRT;  break;
429f7cfac7aSBenjamin Kramer   case Intrinsic::sin:     ISD = ISD::FSIN;   break;
430f7cfac7aSBenjamin Kramer   case Intrinsic::cos:     ISD = ISD::FCOS;   break;
431f7cfac7aSBenjamin Kramer   case Intrinsic::exp:     ISD = ISD::FEXP;   break;
432f7cfac7aSBenjamin Kramer   case Intrinsic::exp2:    ISD = ISD::FEXP2;  break;
433f7cfac7aSBenjamin Kramer   case Intrinsic::log:     ISD = ISD::FLOG;   break;
434f7cfac7aSBenjamin Kramer   case Intrinsic::log10:   ISD = ISD::FLOG10; break;
435f7cfac7aSBenjamin Kramer   case Intrinsic::log2:    ISD = ISD::FLOG2;  break;
436f7cfac7aSBenjamin Kramer   case Intrinsic::fabs:    ISD = ISD::FABS;   break;
437f7cfac7aSBenjamin Kramer   case Intrinsic::floor:   ISD = ISD::FFLOOR; break;
438f7cfac7aSBenjamin Kramer   case Intrinsic::ceil:    ISD = ISD::FCEIL;  break;
439f7cfac7aSBenjamin Kramer   case Intrinsic::trunc:   ISD = ISD::FTRUNC; break;
440f7cfac7aSBenjamin Kramer   case Intrinsic::rint:    ISD = ISD::FRINT;  break;
441f7cfac7aSBenjamin Kramer   case Intrinsic::pow:     ISD = ISD::FPOW;   break;
442f7cfac7aSBenjamin Kramer   case Intrinsic::fma:     ISD = ISD::FMA;    break;
443f7cfac7aSBenjamin Kramer   case Intrinsic::fmuladd: ISD = ISD::FMA;    break; // FIXME: mul + add?
444f7cfac7aSBenjamin Kramer   }
445f7cfac7aSBenjamin Kramer 
446f7cfac7aSBenjamin Kramer   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
447f7cfac7aSBenjamin Kramer 
448f7cfac7aSBenjamin Kramer   if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
449f7cfac7aSBenjamin Kramer     // The operation is legal. Assume it costs 1.
450f7cfac7aSBenjamin Kramer     // If the type is split to multiple registers, assume that thre is some
451f7cfac7aSBenjamin Kramer     // overhead to this.
452f7cfac7aSBenjamin Kramer     // TODO: Once we have extract/insert subvector cost we need to use them.
453f7cfac7aSBenjamin Kramer     if (LT.first > 1)
454f7cfac7aSBenjamin Kramer       return LT.first * 2;
455f7cfac7aSBenjamin Kramer     return LT.first * 1;
456f7cfac7aSBenjamin Kramer   }
457f7cfac7aSBenjamin Kramer 
458f7cfac7aSBenjamin Kramer   if (!TLI->isOperationExpand(ISD, LT.second)) {
459f7cfac7aSBenjamin Kramer     // If the operation is custom lowered then assume
460f7cfac7aSBenjamin Kramer     // thare the code is twice as expensive.
461f7cfac7aSBenjamin Kramer     return LT.first * 2;
462f7cfac7aSBenjamin Kramer   }
463f7cfac7aSBenjamin Kramer 
464f7cfac7aSBenjamin Kramer   // Else, assume that we need to scalarize this intrinsic. For math builtins
465f7cfac7aSBenjamin Kramer   // this will emit a costly libcall, adding call overhead and spills. Make it
466f7cfac7aSBenjamin Kramer   // very expensive.
467f7cfac7aSBenjamin Kramer   if (RetTy->isVectorTy()) {
468f7cfac7aSBenjamin Kramer     unsigned Num = RetTy->getVectorNumElements();
469f7cfac7aSBenjamin Kramer     unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
470f7cfac7aSBenjamin Kramer                                                   Tys);
471f7cfac7aSBenjamin Kramer     return 10 * Cost * Num;
472f7cfac7aSBenjamin Kramer   }
473f7cfac7aSBenjamin Kramer 
474f7cfac7aSBenjamin Kramer   // This is going to be turned into a library call, make it expensive.
475f7cfac7aSBenjamin Kramer   return 10;
476f7cfac7aSBenjamin Kramer }
477664e354dSChandler Carruth 
478664e354dSChandler Carruth unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
479664e354dSChandler Carruth   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
480664e354dSChandler Carruth   return LT.first;
481664e354dSChandler Carruth }
482594fa2dcSArnold Schwaighofer 
483594fa2dcSArnold Schwaighofer unsigned BasicTTI::getAddressComputationCost(Type *Ty) const {
484594fa2dcSArnold Schwaighofer   return 0;
485594fa2dcSArnold Schwaighofer }
486