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 TargetMachine *TM;
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;
35 const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); }
38 BasicTTI() : ImmutablePass(ID), TM(0) {
39 llvm_unreachable("This pass cannot be directly constructed");
42 BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) {
43 initializeBasicTTIPass(*PassRegistry::getPassRegistry());
46 virtual void initializePass() {
50 virtual void finalizePass() {
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 TargetTransformInfo::getAnalysisUsage(AU);
58 /// Pass identification.
61 /// Provide necessary pointer adjustments for the two base classes.
62 virtual void *getAdjustedAnalysisPointer(const void *ID) {
63 if (ID == &TargetTransformInfo::ID)
64 return (TargetTransformInfo*)this;
68 /// \name Scalar TTI Implementations
71 virtual bool isLegalAddImmediate(int64_t imm) const;
72 virtual bool isLegalICmpImmediate(int64_t imm) const;
73 virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
74 int64_t BaseOffset, bool HasBaseReg,
76 virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
77 int64_t BaseOffset, bool HasBaseReg,
79 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
80 virtual bool isTypeLegal(Type *Ty) const;
81 virtual unsigned getJumpBufAlignment() const;
82 virtual unsigned getJumpBufSize() const;
83 virtual bool shouldBuildLookupTables() const;
87 /// \name Vector TTI Implementations
90 virtual unsigned getNumberOfRegisters(bool Vector) const;
91 virtual unsigned getMaximumUnrollFactor() const;
92 virtual unsigned getRegisterBitWidth(bool Vector) const;
93 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
95 OperandValueKind) const;
96 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
97 int Index, Type *SubTp) const;
98 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
100 virtual unsigned getCFInstrCost(unsigned Opcode) const;
101 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
103 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
104 unsigned Index) const;
105 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
107 unsigned AddressSpace) const;
108 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
109 ArrayRef<Type*> Tys) const;
110 virtual unsigned getNumberOfParts(Type *Tp) const;
111 virtual unsigned getAddressComputationCost(Type *Ty) const;
118 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
119 "Target independent code generator's TTI", true, true, false)
120 char BasicTTI::ID = 0;
123 llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
124 return new BasicTTI(TM);
128 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
129 return getTLI()->isLegalAddImmediate(imm);
132 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
133 return getTLI()->isLegalICmpImmediate(imm);
136 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
137 int64_t BaseOffset, bool HasBaseReg,
138 int64_t Scale) const {
139 TargetLoweringBase::AddrMode AM;
141 AM.BaseOffs = BaseOffset;
142 AM.HasBaseReg = HasBaseReg;
144 return getTLI()->isLegalAddressingMode(AM, Ty);
147 int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
148 int64_t BaseOffset, bool HasBaseReg,
149 int64_t Scale) const {
150 TargetLoweringBase::AddrMode AM;
152 AM.BaseOffs = BaseOffset;
153 AM.HasBaseReg = HasBaseReg;
155 return getTLI()->getScalingFactorCost(AM, Ty);
158 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
159 return getTLI()->isTruncateFree(Ty1, Ty2);
162 bool BasicTTI::isTypeLegal(Type *Ty) const {
163 EVT T = getTLI()->getValueType(Ty);
164 return getTLI()->isTypeLegal(T);
167 unsigned BasicTTI::getJumpBufAlignment() const {
168 return getTLI()->getJumpBufAlignment();
171 unsigned BasicTTI::getJumpBufSize() const {
172 return getTLI()->getJumpBufSize();
175 bool BasicTTI::shouldBuildLookupTables() const {
176 const TargetLoweringBase *TLI = getTLI();
177 return TLI->supportJumpTables() &&
178 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
179 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
182 //===----------------------------------------------------------------------===//
184 // Calls used by the vectorizers.
186 //===----------------------------------------------------------------------===//
188 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
189 bool Extract) const {
190 assert (Ty->isVectorTy() && "Can only scalarize vectors");
193 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
195 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
197 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
203 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
207 unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
211 unsigned BasicTTI::getMaximumUnrollFactor() const {
215 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
217 OperandValueKind) const {
218 // Check if any of the operands are vector operands.
219 const TargetLoweringBase *TLI = getTLI();
220 int ISD = TLI->InstructionOpcodeToISD(Opcode);
221 assert(ISD && "Invalid opcode");
223 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
225 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
226 // Assume that floating point arithmetic operations cost twice as much as
227 // integer operations.
228 unsigned OpCost = (IsFloat ? 2 : 1);
230 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
231 // The operation is legal. Assume it costs 1.
232 // If the type is split to multiple registers, assume that there is some
234 // TODO: Once we have extract/insert subvector cost we need to use them.
236 return LT.first * 2 * OpCost;
237 return LT.first * 1 * OpCost;
240 if (!TLI->isOperationExpand(ISD, LT.second)) {
241 // If the operation is custom lowered then assume
242 // thare the code is twice as expensive.
243 return LT.first * 2 * OpCost;
246 // Else, assume that we need to scalarize this op.
247 if (Ty->isVectorTy()) {
248 unsigned Num = Ty->getVectorNumElements();
249 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
250 // return the cost of multiple scalar invocation plus the cost of inserting
251 // and extracting the values.
252 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
255 // We don't know anything about this scalar instruction.
259 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
264 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
266 const TargetLoweringBase *TLI = getTLI();
267 int ISD = TLI->InstructionOpcodeToISD(Opcode);
268 assert(ISD && "Invalid opcode");
270 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
271 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
273 // Check for NOOP conversions.
274 if (SrcLT.first == DstLT.first &&
275 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
277 // Bitcast between types that are legalized to the same type are free.
278 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
282 if (Opcode == Instruction::Trunc &&
283 TLI->isTruncateFree(SrcLT.second, DstLT.second))
286 if (Opcode == Instruction::ZExt &&
287 TLI->isZExtFree(SrcLT.second, DstLT.second))
290 // If the cast is marked as legal (or promote) then assume low cost.
291 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
294 // Handle scalar conversions.
295 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
297 // Scalar bitcasts are usually free.
298 if (Opcode == Instruction::BitCast)
301 // Just check the op cost. If the operation is legal then assume it costs 1.
302 if (!TLI->isOperationExpand(ISD, DstLT.second))
305 // Assume that illegal scalar instruction are expensive.
309 // Check vector-to-vector casts.
310 if (Dst->isVectorTy() && Src->isVectorTy()) {
312 // If the cast is between same-sized registers, then the check is simple.
313 if (SrcLT.first == DstLT.first &&
314 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
316 // Assume that Zext is done using AND.
317 if (Opcode == Instruction::ZExt)
320 // Assume that sext is done using SHL and SRA.
321 if (Opcode == Instruction::SExt)
324 // Just check the op cost. If the operation is legal then assume it costs
325 // 1 and multiply by the type-legalization overhead.
326 if (!TLI->isOperationExpand(ISD, DstLT.second))
327 return SrcLT.first * 1;
330 // If we are converting vectors and the operation is illegal, or
331 // if the vectors are legalized to different types, estimate the
332 // scalarization costs.
333 unsigned Num = Dst->getVectorNumElements();
334 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
335 Src->getScalarType());
337 // Return the cost of multiple scalar invocation plus the cost of
338 // inserting and extracting the values.
339 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
342 // We already handled vector-to-vector and scalar-to-scalar conversions. This
343 // is where we handle bitcast between vectors and scalars. We need to assume
344 // that the conversion is scalarized in one way or another.
345 if (Opcode == Instruction::BitCast)
346 // Illegal bitcasts are done by storing and loading from a stack slot.
347 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
348 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
350 llvm_unreachable("Unhandled cast");
353 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
354 // Branches are assumed to be predicted.
358 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
359 Type *CondTy) const {
360 const TargetLoweringBase *TLI = getTLI();
361 int ISD = TLI->InstructionOpcodeToISD(Opcode);
362 assert(ISD && "Invalid opcode");
364 // Selects on vectors are actually vector selects.
365 if (ISD == ISD::SELECT) {
366 assert(CondTy && "CondTy must exist");
367 if (CondTy->isVectorTy())
371 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
373 if (!TLI->isOperationExpand(ISD, LT.second)) {
374 // The operation is legal. Assume it costs 1. Multiply
375 // by the type-legalization overhead.
379 // Otherwise, assume that the cast is scalarized.
380 if (ValTy->isVectorTy()) {
381 unsigned Num = ValTy->getVectorNumElements();
383 CondTy = CondTy->getScalarType();
384 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
387 // Return the cost of multiple scalar invocation plus the cost of inserting
388 // and extracting the values.
389 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
392 // Unknown scalar opcode.
396 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
397 unsigned Index) const {
401 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
403 unsigned AddressSpace) const {
404 assert(!Src->isVoidTy() && "Invalid type");
405 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
407 // Assume that all loads of legal types cost 1.
411 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
412 ArrayRef<Type *> Tys) const {
416 // Assume that we need to scalarize this intrinsic.
417 unsigned ScalarizationCost = 0;
418 unsigned ScalarCalls = 1;
419 if (RetTy->isVectorTy()) {
420 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
421 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
423 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
424 if (Tys[i]->isVectorTy()) {
425 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
426 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
430 return ScalarCalls + ScalarizationCost;
432 // Look for intrinsics that can be lowered directly or turned into a scalar
434 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
435 case Intrinsic::sin: ISD = ISD::FSIN; break;
436 case Intrinsic::cos: ISD = ISD::FCOS; break;
437 case Intrinsic::exp: ISD = ISD::FEXP; break;
438 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
439 case Intrinsic::log: ISD = ISD::FLOG; break;
440 case Intrinsic::log10: ISD = ISD::FLOG10; break;
441 case Intrinsic::log2: ISD = ISD::FLOG2; break;
442 case Intrinsic::fabs: ISD = ISD::FABS; break;
443 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
444 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
445 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
446 case Intrinsic::nearbyint:
447 ISD = ISD::FNEARBYINT; break;
448 case Intrinsic::rint: ISD = ISD::FRINT; break;
449 case Intrinsic::pow: ISD = ISD::FPOW; break;
450 case Intrinsic::fma: ISD = ISD::FMA; break;
451 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add?
454 const TargetLoweringBase *TLI = getTLI();
455 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
457 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
458 // The operation is legal. Assume it costs 1.
459 // If the type is split to multiple registers, assume that thre is some
461 // TODO: Once we have extract/insert subvector cost we need to use them.
467 if (!TLI->isOperationExpand(ISD, LT.second)) {
468 // If the operation is custom lowered then assume
469 // thare the code is twice as expensive.
473 // Else, assume that we need to scalarize this intrinsic. For math builtins
474 // this will emit a costly libcall, adding call overhead and spills. Make it
476 if (RetTy->isVectorTy()) {
477 unsigned Num = RetTy->getVectorNumElements();
478 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
480 return 10 * Cost * Num;
483 // This is going to be turned into a library call, make it expensive.
487 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
488 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
492 unsigned BasicTTI::getAddressComputationCost(Type *Ty) const {