1 //===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file provides the implementation of a basic TargetTransformInfo pass
11 /// predicated on the target abstractions present in the target independent
12 /// code generator. It uses these (primarily TargetLowering) to model as much
13 /// of the TTI query interface as possible. It is included by most targets so
14 /// that they can specialize only a small subset of the query space.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "basictti"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Target/TargetLowering.h"
28 class BasicTTI : public ImmutablePass, public TargetTransformInfo {
29 const TargetLoweringBase *TLI;
31 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
32 /// are set if the result needs to be inserted and/or extracted from vectors.
33 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
36 BasicTTI() : ImmutablePass(ID), TLI(0) {
37 llvm_unreachable("This pass cannot be directly constructed");
40 BasicTTI(const TargetLoweringBase *TLI) : ImmutablePass(ID), TLI(TLI) {
41 initializeBasicTTIPass(*PassRegistry::getPassRegistry());
44 virtual void initializePass() {
48 virtual void finalizePass() {
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 TargetTransformInfo::getAnalysisUsage(AU);
56 /// Pass identification.
59 /// Provide necessary pointer adjustments for the two base classes.
60 virtual void *getAdjustedAnalysisPointer(const void *ID) {
61 if (ID == &TargetTransformInfo::ID)
62 return (TargetTransformInfo*)this;
66 /// \name Scalar TTI Implementations
69 virtual bool isLegalAddImmediate(int64_t imm) const;
70 virtual bool isLegalICmpImmediate(int64_t imm) const;
71 virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
72 int64_t BaseOffset, bool HasBaseReg,
74 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
75 virtual bool isTypeLegal(Type *Ty) const;
76 virtual unsigned getJumpBufAlignment() const;
77 virtual unsigned getJumpBufSize() const;
78 virtual bool shouldBuildLookupTables() const;
82 /// \name Vector TTI Implementations
85 virtual unsigned getNumberOfRegisters(bool Vector) const;
86 virtual unsigned getMaximumUnrollFactor() const;
87 virtual unsigned getRegisterBitWidth(bool Vector) const;
88 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
90 OperandValueKind) const;
91 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
92 int Index, Type *SubTp) const;
93 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
95 virtual unsigned getCFInstrCost(unsigned Opcode) const;
96 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
98 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
99 unsigned Index) const;
100 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
102 unsigned AddressSpace) const;
103 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
104 ArrayRef<Type*> Tys) const;
105 virtual unsigned getNumberOfParts(Type *Tp) const;
106 virtual unsigned getAddressComputationCost(Type *Ty) const;
113 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
114 "Target independent code generator's TTI", true, true, false)
115 char BasicTTI::ID = 0;
118 llvm::createBasicTargetTransformInfoPass(const TargetLoweringBase *TLI) {
119 return new BasicTTI(TLI);
123 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
124 return TLI->isLegalAddImmediate(imm);
127 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
128 return TLI->isLegalICmpImmediate(imm);
131 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
132 int64_t BaseOffset, bool HasBaseReg,
133 int64_t Scale) const {
134 TargetLoweringBase::AddrMode AM;
136 AM.BaseOffs = BaseOffset;
137 AM.HasBaseReg = HasBaseReg;
139 return TLI->isLegalAddressingMode(AM, Ty);
142 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
143 return TLI->isTruncateFree(Ty1, Ty2);
146 bool BasicTTI::isTypeLegal(Type *Ty) const {
147 EVT T = TLI->getValueType(Ty);
148 return TLI->isTypeLegal(T);
151 unsigned BasicTTI::getJumpBufAlignment() const {
152 return TLI->getJumpBufAlignment();
155 unsigned BasicTTI::getJumpBufSize() const {
156 return TLI->getJumpBufSize();
159 bool BasicTTI::shouldBuildLookupTables() const {
160 return TLI->supportJumpTables() &&
161 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
162 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
165 //===----------------------------------------------------------------------===//
167 // Calls used by the vectorizers.
169 //===----------------------------------------------------------------------===//
171 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
172 bool Extract) const {
173 assert (Ty->isVectorTy() && "Can only scalarize vectors");
176 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
178 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
180 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
186 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
190 unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
194 unsigned BasicTTI::getMaximumUnrollFactor() const {
198 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
200 OperandValueKind) const {
201 // Check if any of the operands are vector operands.
202 int ISD = TLI->InstructionOpcodeToISD(Opcode);
203 assert(ISD && "Invalid opcode");
205 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
207 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
208 // The operation is legal. Assume it costs 1.
209 // If the type is split to multiple registers, assume that thre is some
211 // TODO: Once we have extract/insert subvector cost we need to use them.
217 if (!TLI->isOperationExpand(ISD, LT.second)) {
218 // If the operation is custom lowered then assume
219 // thare the code is twice as expensive.
223 // Else, assume that we need to scalarize this op.
224 if (Ty->isVectorTy()) {
225 unsigned Num = Ty->getVectorNumElements();
226 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
227 // return the cost of multiple scalar invocation plus the cost of inserting
228 // and extracting the values.
229 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
232 // We don't know anything about this scalar instruction.
236 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
241 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
243 int ISD = TLI->InstructionOpcodeToISD(Opcode);
244 assert(ISD && "Invalid opcode");
246 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
247 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
249 // Check for NOOP conversions.
250 if (SrcLT.first == DstLT.first &&
251 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
253 // Bitcast between types that are legalized to the same type are free.
254 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
258 if (Opcode == Instruction::Trunc &&
259 TLI->isTruncateFree(SrcLT.second, DstLT.second))
262 if (Opcode == Instruction::ZExt &&
263 TLI->isZExtFree(SrcLT.second, DstLT.second))
266 // If the cast is marked as legal (or promote) then assume low cost.
267 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
270 // Handle scalar conversions.
271 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
273 // Scalar bitcasts are usually free.
274 if (Opcode == Instruction::BitCast)
277 // Just check the op cost. If the operation is legal then assume it costs 1.
278 if (!TLI->isOperationExpand(ISD, DstLT.second))
281 // Assume that illegal scalar instruction are expensive.
285 // Check vector-to-vector casts.
286 if (Dst->isVectorTy() && Src->isVectorTy()) {
288 // If the cast is between same-sized registers, then the check is simple.
289 if (SrcLT.first == DstLT.first &&
290 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
292 // Assume that Zext is done using AND.
293 if (Opcode == Instruction::ZExt)
296 // Assume that sext is done using SHL and SRA.
297 if (Opcode == Instruction::SExt)
300 // Just check the op cost. If the operation is legal then assume it costs
301 // 1 and multiply by the type-legalization overhead.
302 if (!TLI->isOperationExpand(ISD, DstLT.second))
303 return SrcLT.first * 1;
306 // If we are converting vectors and the operation is illegal, or
307 // if the vectors are legalized to different types, estimate the
308 // scalarization costs.
309 unsigned Num = Dst->getVectorNumElements();
310 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
311 Src->getScalarType());
313 // Return the cost of multiple scalar invocation plus the cost of
314 // inserting and extracting the values.
315 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
318 // We already handled vector-to-vector and scalar-to-scalar conversions. This
319 // is where we handle bitcast between vectors and scalars. We need to assume
320 // that the conversion is scalarized in one way or another.
321 if (Opcode == Instruction::BitCast)
322 // Illegal bitcasts are done by storing and loading from a stack slot.
323 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
324 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
326 llvm_unreachable("Unhandled cast");
329 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
330 // Branches are assumed to be predicted.
334 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
335 Type *CondTy) const {
336 int ISD = TLI->InstructionOpcodeToISD(Opcode);
337 assert(ISD && "Invalid opcode");
339 // Selects on vectors are actually vector selects.
340 if (ISD == ISD::SELECT) {
341 assert(CondTy && "CondTy must exist");
342 if (CondTy->isVectorTy())
346 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
348 if (!TLI->isOperationExpand(ISD, LT.second)) {
349 // The operation is legal. Assume it costs 1. Multiply
350 // by the type-legalization overhead.
354 // Otherwise, assume that the cast is scalarized.
355 if (ValTy->isVectorTy()) {
356 unsigned Num = ValTy->getVectorNumElements();
358 CondTy = CondTy->getScalarType();
359 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
362 // Return the cost of multiple scalar invocation plus the cost of inserting
363 // and extracting the values.
364 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
367 // Unknown scalar opcode.
371 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
372 unsigned Index) const {
376 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
378 unsigned AddressSpace) const {
379 assert(!Src->isVoidTy() && "Invalid type");
380 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
382 // Assume that all loads of legal types cost 1.
386 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
387 ArrayRef<Type *> Tys) const {
391 // Assume that we need to scalarize this intrinsic.
392 unsigned ScalarizationCost = 0;
393 unsigned ScalarCalls = 1;
394 if (RetTy->isVectorTy()) {
395 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
396 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
398 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
399 if (Tys[i]->isVectorTy()) {
400 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
401 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
405 return ScalarCalls + ScalarizationCost;
407 // Look for intrinsics that can be lowered directly or turned into a scalar
409 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
410 case Intrinsic::sin: ISD = ISD::FSIN; break;
411 case Intrinsic::cos: ISD = ISD::FCOS; break;
412 case Intrinsic::exp: ISD = ISD::FEXP; break;
413 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
414 case Intrinsic::log: ISD = ISD::FLOG; break;
415 case Intrinsic::log10: ISD = ISD::FLOG10; break;
416 case Intrinsic::log2: ISD = ISD::FLOG2; break;
417 case Intrinsic::fabs: ISD = ISD::FABS; break;
418 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
419 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
420 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
421 case Intrinsic::rint: ISD = ISD::FRINT; break;
422 case Intrinsic::pow: ISD = ISD::FPOW; break;
423 case Intrinsic::fma: ISD = ISD::FMA; break;
424 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add?
427 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
429 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
430 // The operation is legal. Assume it costs 1.
431 // If the type is split to multiple registers, assume that thre is some
433 // TODO: Once we have extract/insert subvector cost we need to use them.
439 if (!TLI->isOperationExpand(ISD, LT.second)) {
440 // If the operation is custom lowered then assume
441 // thare the code is twice as expensive.
445 // Else, assume that we need to scalarize this intrinsic. For math builtins
446 // this will emit a costly libcall, adding call overhead and spills. Make it
448 if (RetTy->isVectorTy()) {
449 unsigned Num = RetTy->getVectorNumElements();
450 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
452 return 10 * Cost * Num;
455 // This is going to be turned into a library call, make it expensive.
459 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
460 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
464 unsigned BasicTTI::getAddressComputationCost(Type *Ty) const {