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 virtual bool hasBranchDivergence() const;
70 /// \name Scalar TTI Implementations
73 virtual bool isLegalAddImmediate(int64_t imm) const;
74 virtual bool isLegalICmpImmediate(int64_t imm) const;
75 virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
76 int64_t BaseOffset, bool HasBaseReg,
78 virtual int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
79 int64_t BaseOffset, bool HasBaseReg,
81 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const;
82 virtual bool isTypeLegal(Type *Ty) const;
83 virtual unsigned getJumpBufAlignment() const;
84 virtual unsigned getJumpBufSize() const;
85 virtual bool shouldBuildLookupTables() const;
86 virtual bool haveFastSqrt(Type *Ty) const;
87 virtual void getUnrollingPreferences(Loop *L, UnrollingPreferences &UP) const;
91 /// \name Vector TTI Implementations
94 virtual unsigned getNumberOfRegisters(bool Vector) const;
95 virtual unsigned getMaximumUnrollFactor() const;
96 virtual unsigned getRegisterBitWidth(bool Vector) const;
97 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
99 OperandValueKind) const;
100 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
101 int Index, Type *SubTp) const;
102 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
104 virtual unsigned getCFInstrCost(unsigned Opcode) const;
105 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
107 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
108 unsigned Index) const;
109 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
111 unsigned AddressSpace) const;
112 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
113 ArrayRef<Type*> Tys) const;
114 virtual unsigned getNumberOfParts(Type *Tp) const;
115 virtual unsigned getAddressComputationCost(Type *Ty, bool IsComplex) const;
122 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
123 "Target independent code generator's TTI", true, true, false)
124 char BasicTTI::ID = 0;
127 llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
128 return new BasicTTI(TM);
131 bool BasicTTI::hasBranchDivergence() const { return false; }
133 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
134 return getTLI()->isLegalAddImmediate(imm);
137 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
138 return getTLI()->isLegalICmpImmediate(imm);
141 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
142 int64_t BaseOffset, bool HasBaseReg,
143 int64_t Scale) const {
144 TargetLoweringBase::AddrMode AM;
146 AM.BaseOffs = BaseOffset;
147 AM.HasBaseReg = HasBaseReg;
149 return getTLI()->isLegalAddressingMode(AM, Ty);
152 int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
153 int64_t BaseOffset, bool HasBaseReg,
154 int64_t Scale) const {
155 TargetLoweringBase::AddrMode AM;
157 AM.BaseOffs = BaseOffset;
158 AM.HasBaseReg = HasBaseReg;
160 return getTLI()->getScalingFactorCost(AM, Ty);
163 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
164 return getTLI()->isTruncateFree(Ty1, Ty2);
167 bool BasicTTI::isTypeLegal(Type *Ty) const {
168 EVT T = getTLI()->getValueType(Ty);
169 return getTLI()->isTypeLegal(T);
172 unsigned BasicTTI::getJumpBufAlignment() const {
173 return getTLI()->getJumpBufAlignment();
176 unsigned BasicTTI::getJumpBufSize() const {
177 return getTLI()->getJumpBufSize();
180 bool BasicTTI::shouldBuildLookupTables() const {
181 const TargetLoweringBase *TLI = getTLI();
182 return TLI->supportJumpTables() &&
183 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
184 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
187 bool BasicTTI::haveFastSqrt(Type *Ty) const {
188 const TargetLoweringBase *TLI = getTLI();
189 EVT VT = TLI->getValueType(Ty);
190 return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
193 void BasicTTI::getUnrollingPreferences(Loop *, UnrollingPreferences &) const { }
195 //===----------------------------------------------------------------------===//
197 // Calls used by the vectorizers.
199 //===----------------------------------------------------------------------===//
201 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
202 bool Extract) const {
203 assert (Ty->isVectorTy() && "Can only scalarize vectors");
206 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
208 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
210 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
216 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
220 unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
224 unsigned BasicTTI::getMaximumUnrollFactor() const {
228 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
230 OperandValueKind) const {
231 // Check if any of the operands are vector operands.
232 const TargetLoweringBase *TLI = getTLI();
233 int ISD = TLI->InstructionOpcodeToISD(Opcode);
234 assert(ISD && "Invalid opcode");
236 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
238 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
239 // Assume that floating point arithmetic operations cost twice as much as
240 // integer operations.
241 unsigned OpCost = (IsFloat ? 2 : 1);
243 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
244 // The operation is legal. Assume it costs 1.
245 // If the type is split to multiple registers, assume that there is some
247 // TODO: Once we have extract/insert subvector cost we need to use them.
249 return LT.first * 2 * OpCost;
250 return LT.first * 1 * OpCost;
253 if (!TLI->isOperationExpand(ISD, LT.second)) {
254 // If the operation is custom lowered then assume
255 // thare the code is twice as expensive.
256 return LT.first * 2 * OpCost;
259 // Else, assume that we need to scalarize this op.
260 if (Ty->isVectorTy()) {
261 unsigned Num = Ty->getVectorNumElements();
262 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
263 // return the cost of multiple scalar invocation plus the cost of inserting
264 // and extracting the values.
265 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
268 // We don't know anything about this scalar instruction.
272 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
277 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
279 const TargetLoweringBase *TLI = getTLI();
280 int ISD = TLI->InstructionOpcodeToISD(Opcode);
281 assert(ISD && "Invalid opcode");
283 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
284 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
286 // Check for NOOP conversions.
287 if (SrcLT.first == DstLT.first &&
288 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
290 // Bitcast between types that are legalized to the same type are free.
291 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
295 if (Opcode == Instruction::Trunc &&
296 TLI->isTruncateFree(SrcLT.second, DstLT.second))
299 if (Opcode == Instruction::ZExt &&
300 TLI->isZExtFree(SrcLT.second, DstLT.second))
303 // If the cast is marked as legal (or promote) then assume low cost.
304 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
307 // Handle scalar conversions.
308 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
310 // Scalar bitcasts are usually free.
311 if (Opcode == Instruction::BitCast)
314 // Just check the op cost. If the operation is legal then assume it costs 1.
315 if (!TLI->isOperationExpand(ISD, DstLT.second))
318 // Assume that illegal scalar instruction are expensive.
322 // Check vector-to-vector casts.
323 if (Dst->isVectorTy() && Src->isVectorTy()) {
325 // If the cast is between same-sized registers, then the check is simple.
326 if (SrcLT.first == DstLT.first &&
327 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
329 // Assume that Zext is done using AND.
330 if (Opcode == Instruction::ZExt)
333 // Assume that sext is done using SHL and SRA.
334 if (Opcode == Instruction::SExt)
337 // Just check the op cost. If the operation is legal then assume it costs
338 // 1 and multiply by the type-legalization overhead.
339 if (!TLI->isOperationExpand(ISD, DstLT.second))
340 return SrcLT.first * 1;
343 // If we are converting vectors and the operation is illegal, or
344 // if the vectors are legalized to different types, estimate the
345 // scalarization costs.
346 unsigned Num = Dst->getVectorNumElements();
347 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
348 Src->getScalarType());
350 // Return the cost of multiple scalar invocation plus the cost of
351 // inserting and extracting the values.
352 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
355 // We already handled vector-to-vector and scalar-to-scalar conversions. This
356 // is where we handle bitcast between vectors and scalars. We need to assume
357 // that the conversion is scalarized in one way or another.
358 if (Opcode == Instruction::BitCast)
359 // Illegal bitcasts are done by storing and loading from a stack slot.
360 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
361 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
363 llvm_unreachable("Unhandled cast");
366 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
367 // Branches are assumed to be predicted.
371 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
372 Type *CondTy) const {
373 const TargetLoweringBase *TLI = getTLI();
374 int ISD = TLI->InstructionOpcodeToISD(Opcode);
375 assert(ISD && "Invalid opcode");
377 // Selects on vectors are actually vector selects.
378 if (ISD == ISD::SELECT) {
379 assert(CondTy && "CondTy must exist");
380 if (CondTy->isVectorTy())
384 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
386 if (!TLI->isOperationExpand(ISD, LT.second)) {
387 // The operation is legal. Assume it costs 1. Multiply
388 // by the type-legalization overhead.
392 // Otherwise, assume that the cast is scalarized.
393 if (ValTy->isVectorTy()) {
394 unsigned Num = ValTy->getVectorNumElements();
396 CondTy = CondTy->getScalarType();
397 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
400 // Return the cost of multiple scalar invocation plus the cost of inserting
401 // and extracting the values.
402 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
405 // Unknown scalar opcode.
409 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
410 unsigned Index) const {
414 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
416 unsigned AddressSpace) const {
417 assert(!Src->isVoidTy() && "Invalid type");
418 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
420 // Assume that all loads of legal types cost 1.
424 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
425 ArrayRef<Type *> Tys) const {
429 // Assume that we need to scalarize this intrinsic.
430 unsigned ScalarizationCost = 0;
431 unsigned ScalarCalls = 1;
432 if (RetTy->isVectorTy()) {
433 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
434 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
436 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
437 if (Tys[i]->isVectorTy()) {
438 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
439 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
443 return ScalarCalls + ScalarizationCost;
445 // Look for intrinsics that can be lowered directly or turned into a scalar
447 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
448 case Intrinsic::sin: ISD = ISD::FSIN; break;
449 case Intrinsic::cos: ISD = ISD::FCOS; break;
450 case Intrinsic::exp: ISD = ISD::FEXP; break;
451 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
452 case Intrinsic::log: ISD = ISD::FLOG; break;
453 case Intrinsic::log10: ISD = ISD::FLOG10; break;
454 case Intrinsic::log2: ISD = ISD::FLOG2; break;
455 case Intrinsic::fabs: ISD = ISD::FABS; break;
456 case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
457 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
458 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
459 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
460 case Intrinsic::nearbyint:
461 ISD = ISD::FNEARBYINT; break;
462 case Intrinsic::rint: ISD = ISD::FRINT; break;
463 case Intrinsic::round: ISD = ISD::FROUND; break;
464 case Intrinsic::pow: ISD = ISD::FPOW; break;
465 case Intrinsic::fma: ISD = ISD::FMA; break;
466 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add?
467 case Intrinsic::lifetime_start:
468 case Intrinsic::lifetime_end:
472 const TargetLoweringBase *TLI = getTLI();
473 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
475 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
476 // The operation is legal. Assume it costs 1.
477 // If the type is split to multiple registers, assume that thre is some
479 // TODO: Once we have extract/insert subvector cost we need to use them.
485 if (!TLI->isOperationExpand(ISD, LT.second)) {
486 // If the operation is custom lowered then assume
487 // thare the code is twice as expensive.
491 // Else, assume that we need to scalarize this intrinsic. For math builtins
492 // this will emit a costly libcall, adding call overhead and spills. Make it
494 if (RetTy->isVectorTy()) {
495 unsigned Num = RetTy->getVectorNumElements();
496 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
498 return 10 * Cost * Num;
501 // This is going to be turned into a library call, make it expensive.
505 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
506 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
510 unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {