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 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
208 unsigned OpCost = (IsFloat ? 2 : 1);
210 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
211 // The operation is legal. Assume it costs 1.
212 // If the type is split to multiple registers, assume that thre is some
214 // TODO: Once we have extract/insert subvector cost we need to use them.
216 return LT.first * 2 * OpCost;
217 return LT.first * 1 * OpCost;
220 if (!TLI->isOperationExpand(ISD, LT.second)) {
221 // If the operation is custom lowered then assume
222 // thare the code is twice as expensive.
223 return LT.first * 2 * OpCost;
226 // Else, assume that we need to scalarize this op.
227 if (Ty->isVectorTy()) {
228 unsigned Num = Ty->getVectorNumElements();
229 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
230 // return the cost of multiple scalar invocation plus the cost of inserting
231 // and extracting the values.
232 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
235 // We don't know anything about this scalar instruction.
239 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
244 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
246 int ISD = TLI->InstructionOpcodeToISD(Opcode);
247 assert(ISD && "Invalid opcode");
249 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
250 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
252 // Check for NOOP conversions.
253 if (SrcLT.first == DstLT.first &&
254 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
256 // Bitcast between types that are legalized to the same type are free.
257 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
261 if (Opcode == Instruction::Trunc &&
262 TLI->isTruncateFree(SrcLT.second, DstLT.second))
265 if (Opcode == Instruction::ZExt &&
266 TLI->isZExtFree(SrcLT.second, DstLT.second))
269 // If the cast is marked as legal (or promote) then assume low cost.
270 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
273 // Handle scalar conversions.
274 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
276 // Scalar bitcasts are usually free.
277 if (Opcode == Instruction::BitCast)
280 // Just check the op cost. If the operation is legal then assume it costs 1.
281 if (!TLI->isOperationExpand(ISD, DstLT.second))
284 // Assume that illegal scalar instruction are expensive.
288 // Check vector-to-vector casts.
289 if (Dst->isVectorTy() && Src->isVectorTy()) {
291 // If the cast is between same-sized registers, then the check is simple.
292 if (SrcLT.first == DstLT.first &&
293 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
295 // Assume that Zext is done using AND.
296 if (Opcode == Instruction::ZExt)
299 // Assume that sext is done using SHL and SRA.
300 if (Opcode == Instruction::SExt)
303 // Just check the op cost. If the operation is legal then assume it costs
304 // 1 and multiply by the type-legalization overhead.
305 if (!TLI->isOperationExpand(ISD, DstLT.second))
306 return SrcLT.first * 1;
309 // If we are converting vectors and the operation is illegal, or
310 // if the vectors are legalized to different types, estimate the
311 // scalarization costs.
312 unsigned Num = Dst->getVectorNumElements();
313 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
314 Src->getScalarType());
316 // Return the cost of multiple scalar invocation plus the cost of
317 // inserting and extracting the values.
318 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
321 // We already handled vector-to-vector and scalar-to-scalar conversions. This
322 // is where we handle bitcast between vectors and scalars. We need to assume
323 // that the conversion is scalarized in one way or another.
324 if (Opcode == Instruction::BitCast)
325 // Illegal bitcasts are done by storing and loading from a stack slot.
326 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
327 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
329 llvm_unreachable("Unhandled cast");
332 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
333 // Branches are assumed to be predicted.
337 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
338 Type *CondTy) const {
339 int ISD = TLI->InstructionOpcodeToISD(Opcode);
340 assert(ISD && "Invalid opcode");
342 // Selects on vectors are actually vector selects.
343 if (ISD == ISD::SELECT) {
344 assert(CondTy && "CondTy must exist");
345 if (CondTy->isVectorTy())
349 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
351 if (!TLI->isOperationExpand(ISD, LT.second)) {
352 // The operation is legal. Assume it costs 1. Multiply
353 // by the type-legalization overhead.
357 // Otherwise, assume that the cast is scalarized.
358 if (ValTy->isVectorTy()) {
359 unsigned Num = ValTy->getVectorNumElements();
361 CondTy = CondTy->getScalarType();
362 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
365 // Return the cost of multiple scalar invocation plus the cost of inserting
366 // and extracting the values.
367 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
370 // Unknown scalar opcode.
374 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
375 unsigned Index) const {
379 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
381 unsigned AddressSpace) const {
382 assert(!Src->isVoidTy() && "Invalid type");
383 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
385 // Assume that all loads of legal types cost 1.
389 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
390 ArrayRef<Type *> Tys) const {
394 // Assume that we need to scalarize this intrinsic.
395 unsigned ScalarizationCost = 0;
396 unsigned ScalarCalls = 1;
397 if (RetTy->isVectorTy()) {
398 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
399 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
401 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
402 if (Tys[i]->isVectorTy()) {
403 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
404 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
408 return ScalarCalls + ScalarizationCost;
410 // Look for intrinsics that can be lowered directly or turned into a scalar
412 case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
413 case Intrinsic::sin: ISD = ISD::FSIN; break;
414 case Intrinsic::cos: ISD = ISD::FCOS; break;
415 case Intrinsic::exp: ISD = ISD::FEXP; break;
416 case Intrinsic::exp2: ISD = ISD::FEXP2; break;
417 case Intrinsic::log: ISD = ISD::FLOG; break;
418 case Intrinsic::log10: ISD = ISD::FLOG10; break;
419 case Intrinsic::log2: ISD = ISD::FLOG2; break;
420 case Intrinsic::fabs: ISD = ISD::FABS; break;
421 case Intrinsic::floor: ISD = ISD::FFLOOR; break;
422 case Intrinsic::ceil: ISD = ISD::FCEIL; break;
423 case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
424 case Intrinsic::rint: ISD = ISD::FRINT; break;
425 case Intrinsic::pow: ISD = ISD::FPOW; break;
426 case Intrinsic::fma: ISD = ISD::FMA; break;
427 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add?
430 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
432 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
433 // The operation is legal. Assume it costs 1.
434 // If the type is split to multiple registers, assume that thre is some
436 // TODO: Once we have extract/insert subvector cost we need to use them.
442 if (!TLI->isOperationExpand(ISD, LT.second)) {
443 // If the operation is custom lowered then assume
444 // thare the code is twice as expensive.
448 // Else, assume that we need to scalarize this intrinsic. For math builtins
449 // this will emit a costly libcall, adding call overhead and spills. Make it
451 if (RetTy->isVectorTy()) {
452 unsigned Num = RetTy->getVectorNumElements();
453 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
455 return 10 * Cost * Num;
458 // This is going to be turned into a library call, make it expensive.
462 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
463 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
467 unsigned BasicTTI::getAddressComputationCost(Type *Ty) const {