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 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
75 int64_t BaseOffset, bool HasBaseReg,
77 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
78 virtual bool isTypeLegal(Type *Ty) const;
79 virtual unsigned getJumpBufAlignment() const;
80 virtual unsigned getJumpBufSize() const;
81 virtual bool shouldBuildLookupTables() const;
85 /// \name Vector TTI Implementations
88 virtual unsigned getNumberOfRegisters(bool Vector) const;
89 virtual unsigned getMaximumUnrollFactor() const;
90 virtual unsigned getRegisterBitWidth(bool Vector) const;
91 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
93 OperandValueKind) const;
94 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
95 int Index, Type *SubTp) const;
96 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
98 virtual unsigned getCFInstrCost(unsigned Opcode) const;
99 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
101 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
102 unsigned Index) const;
103 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
105 unsigned AddressSpace) const;
106 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
107 ArrayRef<Type*> Tys) const;
108 virtual unsigned getNumberOfParts(Type *Tp) const;
109 virtual unsigned getAddressComputationCost(Type *Ty) const;
116 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
117 "Target independent code generator's TTI", true, true, false)
118 char BasicTTI::ID = 0;
121 llvm::createBasicTargetTransformInfoPass(const TargetLoweringBase *TLI) {
122 return new BasicTTI(TLI);
126 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
127 return TLI->isLegalAddImmediate(imm);
130 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
131 return TLI->isLegalICmpImmediate(imm);
134 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
135 int64_t BaseOffset, bool HasBaseReg,
136 int64_t Scale) const {
137 TargetLoweringBase::AddrMode AM;
139 AM.BaseOffs = BaseOffset;
140 AM.HasBaseReg = HasBaseReg;
142 return TLI->isLegalAddressingMode(AM, Ty);
145 int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
146 int64_t BaseOffset, bool HasBaseReg,
147 int64_t Scale) const {
148 TargetLoweringBase::AddrMode AM;
150 AM.BaseOffs = BaseOffset;
151 AM.HasBaseReg = HasBaseReg;
153 return TLI->getScalingFactorCost(AM, Ty);
156 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
157 return TLI->isTruncateFree(Ty1, Ty2);
160 bool BasicTTI::isTypeLegal(Type *Ty) const {
161 EVT T = TLI->getValueType(Ty);
162 return TLI->isTypeLegal(T);
165 unsigned BasicTTI::getJumpBufAlignment() const {
166 return TLI->getJumpBufAlignment();
169 unsigned BasicTTI::getJumpBufSize() const {
170 return TLI->getJumpBufSize();
173 bool BasicTTI::shouldBuildLookupTables() const {
174 return TLI->supportJumpTables() &&
175 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
176 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
179 //===----------------------------------------------------------------------===//
181 // Calls used by the vectorizers.
183 //===----------------------------------------------------------------------===//
185 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
186 bool Extract) const {
187 assert (Ty->isVectorTy() && "Can only scalarize vectors");
190 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
192 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
194 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
200 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
204 unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
208 unsigned BasicTTI::getMaximumUnrollFactor() const {
212 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
214 OperandValueKind) const {
215 // Check if any of the operands are vector operands.
216 int ISD = TLI->InstructionOpcodeToISD(Opcode);
217 assert(ISD && "Invalid opcode");
219 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
221 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
222 // Assume that floating point arithmetic operations cost twice as much as
223 // integer operations.
224 unsigned OpCost = (IsFloat ? 2 : 1);
226 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
227 // The operation is legal. Assume it costs 1.
228 // If the type is split to multiple registers, assume that there is some
230 // TODO: Once we have extract/insert subvector cost we need to use them.
232 return LT.first * 2 * OpCost;
233 return LT.first * 1 * OpCost;
236 if (!TLI->isOperationExpand(ISD, LT.second)) {
237 // If the operation is custom lowered then assume
238 // thare the code is twice as expensive.
239 return LT.first * 2 * OpCost;
242 // Else, assume that we need to scalarize this op.
243 if (Ty->isVectorTy()) {
244 unsigned Num = Ty->getVectorNumElements();
245 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
246 // return the cost of multiple scalar invocation plus the cost of inserting
247 // and extracting the values.
248 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
251 // We don't know anything about this scalar instruction.
255 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
260 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
262 int ISD = TLI->InstructionOpcodeToISD(Opcode);
263 assert(ISD && "Invalid opcode");
265 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
266 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
268 // Check for NOOP conversions.
269 if (SrcLT.first == DstLT.first &&
270 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
272 // Bitcast between types that are legalized to the same type are free.
273 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
277 if (Opcode == Instruction::Trunc &&
278 TLI->isTruncateFree(SrcLT.second, DstLT.second))
281 if (Opcode == Instruction::ZExt &&
282 TLI->isZExtFree(SrcLT.second, DstLT.second))
285 // If the cast is marked as legal (or promote) then assume low cost.
286 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
289 // Handle scalar conversions.
290 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
292 // Scalar bitcasts are usually free.
293 if (Opcode == Instruction::BitCast)
296 // Just check the op cost. If the operation is legal then assume it costs 1.
297 if (!TLI->isOperationExpand(ISD, DstLT.second))
300 // Assume that illegal scalar instruction are expensive.
304 // Check vector-to-vector casts.
305 if (Dst->isVectorTy() && Src->isVectorTy()) {
307 // If the cast is between same-sized registers, then the check is simple.
308 if (SrcLT.first == DstLT.first &&
309 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
311 // Assume that Zext is done using AND.
312 if (Opcode == Instruction::ZExt)
315 // Assume that sext is done using SHL and SRA.
316 if (Opcode == Instruction::SExt)
319 // Just check the op cost. If the operation is legal then assume it costs
320 // 1 and multiply by the type-legalization overhead.
321 if (!TLI->isOperationExpand(ISD, DstLT.second))
322 return SrcLT.first * 1;
325 // If we are converting vectors and the operation is illegal, or
326 // if the vectors are legalized to different types, estimate the
327 // scalarization costs.
328 unsigned Num = Dst->getVectorNumElements();
329 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
330 Src->getScalarType());
332 // Return the cost of multiple scalar invocation plus the cost of
333 // inserting and extracting the values.
334 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
337 // We already handled vector-to-vector and scalar-to-scalar conversions. This
338 // is where we handle bitcast between vectors and scalars. We need to assume
339 // that the conversion is scalarized in one way or another.
340 if (Opcode == Instruction::BitCast)
341 // Illegal bitcasts are done by storing and loading from a stack slot.
342 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
343 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
345 llvm_unreachable("Unhandled cast");
348 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
349 // Branches are assumed to be predicted.
353 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
354 Type *CondTy) const {
355 int ISD = TLI->InstructionOpcodeToISD(Opcode);
356 assert(ISD && "Invalid opcode");
358 // Selects on vectors are actually vector selects.
359 if (ISD == ISD::SELECT) {
360 assert(CondTy && "CondTy must exist");
361 if (CondTy->isVectorTy())
365 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
367 if (!TLI->isOperationExpand(ISD, LT.second)) {
368 // The operation is legal. Assume it costs 1. Multiply
369 // by the type-legalization overhead.
373 // Otherwise, assume that the cast is scalarized.
374 if (ValTy->isVectorTy()) {
375 unsigned Num = ValTy->getVectorNumElements();
377 CondTy = CondTy->getScalarType();
378 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
381 // Return the cost of multiple scalar invocation plus the cost of inserting
382 // and extracting the values.
383 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
386 // Unknown scalar opcode.
390 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
391 unsigned Index) const {
395 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
397 unsigned AddressSpace) const {
398 assert(!Src->isVoidTy() && "Invalid type");
399 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
401 // Assume that all loads of legal types cost 1.
405 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
406 ArrayRef<Type *> Tys) const {
410 // Assume that we need to scalarize this intrinsic.
411 unsigned ScalarizationCost = 0;
412 unsigned ScalarCalls = 1;
413 if (RetTy->isVectorTy()) {
414 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
415 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
417 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
418 if (Tys[i]->isVectorTy()) {
419 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
420 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
424 return ScalarCalls + ScalarizationCost;
426 // Look for intrinsics that can be lowered directly or turned into a scalar
428 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
429 case Intrinsic::sin: ISD = ISD::FSIN; break;
430 case Intrinsic::cos: ISD = ISD::FCOS; break;
431 case Intrinsic::exp: ISD = ISD::FEXP; break;
432 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
433 case Intrinsic::log: ISD = ISD::FLOG; break;
434 case Intrinsic::log10: ISD = ISD::FLOG10; break;
435 case Intrinsic::log2: ISD = ISD::FLOG2; break;
436 case Intrinsic::fabs: ISD = ISD::FABS; break;
437 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
438 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
439 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
440 case Intrinsic::rint: ISD = ISD::FRINT; break;
441 case Intrinsic::pow: ISD = ISD::FPOW; break;
442 case Intrinsic::fma: ISD = ISD::FMA; break;
443 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add?
446 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
448 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
449 // The operation is legal. Assume it costs 1.
450 // If the type is split to multiple registers, assume that thre is some
452 // TODO: Once we have extract/insert subvector cost we need to use them.
458 if (!TLI->isOperationExpand(ISD, LT.second)) {
459 // If the operation is custom lowered then assume
460 // thare the code is twice as expensive.
464 // Else, assume that we need to scalarize this intrinsic. For math builtins
465 // this will emit a costly libcall, adding call overhead and spills. Make it
467 if (RetTy->isVectorTy()) {
468 unsigned Num = RetTy->getVectorNumElements();
469 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
471 return 10 * Cost * Num;
474 // This is going to be turned into a library call, make it expensive.
478 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
479 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
483 unsigned BasicTTI::getAddressComputationCost(Type *Ty) const {