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) const;
89 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
90 int Index, Type *SubTp) const;
91 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
93 virtual unsigned getCFInstrCost(unsigned Opcode) const;
94 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
96 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
97 unsigned Index) const;
98 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
100 unsigned AddressSpace) const;
101 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
102 ArrayRef<Type*> Tys) const;
103 virtual unsigned getNumberOfParts(Type *Tp) const;
110 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
111 "Target independent code generator's TTI", true, true, false)
112 char BasicTTI::ID = 0;
115 llvm::createBasicTargetTransformInfoPass(const TargetLoweringBase *TLI) {
116 return new BasicTTI(TLI);
120 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
121 return TLI->isLegalAddImmediate(imm);
124 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
125 return TLI->isLegalICmpImmediate(imm);
128 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
129 int64_t BaseOffset, bool HasBaseReg,
130 int64_t Scale) const {
131 TargetLoweringBase::AddrMode AM;
133 AM.BaseOffs = BaseOffset;
134 AM.HasBaseReg = HasBaseReg;
136 return TLI->isLegalAddressingMode(AM, Ty);
139 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
140 return TLI->isTruncateFree(Ty1, Ty2);
143 bool BasicTTI::isTypeLegal(Type *Ty) const {
144 EVT T = TLI->getValueType(Ty);
145 return TLI->isTypeLegal(T);
148 unsigned BasicTTI::getJumpBufAlignment() const {
149 return TLI->getJumpBufAlignment();
152 unsigned BasicTTI::getJumpBufSize() const {
153 return TLI->getJumpBufSize();
156 bool BasicTTI::shouldBuildLookupTables() const {
157 return TLI->supportJumpTables() &&
158 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
159 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
162 //===----------------------------------------------------------------------===//
164 // Calls used by the vectorizers.
166 //===----------------------------------------------------------------------===//
168 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
169 bool Extract) const {
170 assert (Ty->isVectorTy() && "Can only scalarize vectors");
173 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
175 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
177 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
183 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
187 unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
191 unsigned BasicTTI::getMaximumUnrollFactor() const {
195 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
196 // Check if any of the operands are vector operands.
197 int ISD = TLI->InstructionOpcodeToISD(Opcode);
198 assert(ISD && "Invalid opcode");
200 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
202 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
203 // The operation is legal. Assume it costs 1.
204 // If the type is split to multiple registers, assume that thre is some
206 // TODO: Once we have extract/insert subvector cost we need to use them.
212 if (!TLI->isOperationExpand(ISD, LT.second)) {
213 // If the operation is custom lowered then assume
214 // thare the code is twice as expensive.
218 // Else, assume that we need to scalarize this op.
219 if (Ty->isVectorTy()) {
220 unsigned Num = Ty->getVectorNumElements();
221 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
222 // return the cost of multiple scalar invocation plus the cost of inserting
223 // and extracting the values.
224 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
227 // We don't know anything about this scalar instruction.
231 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
236 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
238 int ISD = TLI->InstructionOpcodeToISD(Opcode);
239 assert(ISD && "Invalid opcode");
241 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
242 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
244 // Check for NOOP conversions.
245 if (SrcLT.first == DstLT.first &&
246 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
248 // Bitcast between types that are legalized to the same type are free.
249 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
253 if (Opcode == Instruction::Trunc &&
254 TLI->isTruncateFree(SrcLT.second, DstLT.second))
257 if (Opcode == Instruction::ZExt &&
258 TLI->isZExtFree(SrcLT.second, DstLT.second))
261 // If the cast is marked as legal (or promote) then assume low cost.
262 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second))
265 // Handle scalar conversions.
266 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
268 // Scalar bitcasts are usually free.
269 if (Opcode == Instruction::BitCast)
272 // Just check the op cost. If the operation is legal then assume it costs 1.
273 if (!TLI->isOperationExpand(ISD, DstLT.second))
276 // Assume that illegal scalar instruction are expensive.
280 // Check vector-to-vector casts.
281 if (Dst->isVectorTy() && Src->isVectorTy()) {
283 // If the cast is between same-sized registers, then the check is simple.
284 if (SrcLT.first == DstLT.first &&
285 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
287 // Assume that Zext is done using AND.
288 if (Opcode == Instruction::ZExt)
291 // Assume that sext is done using SHL and SRA.
292 if (Opcode == Instruction::SExt)
295 // Just check the op cost. If the operation is legal then assume it costs
296 // 1 and multiply by the type-legalization overhead.
297 if (!TLI->isOperationExpand(ISD, DstLT.second))
298 return SrcLT.first * 1;
301 // If we are converting vectors and the operation is illegal, or
302 // if the vectors are legalized to different types, estimate the
303 // scalarization costs.
304 unsigned Num = Dst->getVectorNumElements();
305 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
306 Src->getScalarType());
308 // Return the cost of multiple scalar invocation plus the cost of
309 // inserting and extracting the values.
310 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
313 // We already handled vector-to-vector and scalar-to-scalar conversions. This
314 // is where we handle bitcast between vectors and scalars. We need to assume
315 // that the conversion is scalarized in one way or another.
316 if (Opcode == Instruction::BitCast)
317 // Illegal bitcasts are done by storing and loading from a stack slot.
318 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
319 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
321 llvm_unreachable("Unhandled cast");
324 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
325 // Branches are assumed to be predicted.
329 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
330 Type *CondTy) const {
331 int ISD = TLI->InstructionOpcodeToISD(Opcode);
332 assert(ISD && "Invalid opcode");
334 // Selects on vectors are actually vector selects.
335 if (ISD == ISD::SELECT) {
336 assert(CondTy && "CondTy must exist");
337 if (CondTy->isVectorTy())
341 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
343 if (!TLI->isOperationExpand(ISD, LT.second)) {
344 // The operation is legal. Assume it costs 1. Multiply
345 // by the type-legalization overhead.
349 // Otherwise, assume that the cast is scalarized.
350 if (ValTy->isVectorTy()) {
351 unsigned Num = ValTy->getVectorNumElements();
353 CondTy = CondTy->getScalarType();
354 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
357 // Return the cost of multiple scalar invocation plus the cost of inserting
358 // and extracting the values.
359 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
362 // Unknown scalar opcode.
366 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
367 unsigned Index) const {
371 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
373 unsigned AddressSpace) const {
374 assert(!Src->isVoidTy() && "Invalid type");
375 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
377 // Assume that all loads of legal types cost 1.
381 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
382 ArrayRef<Type *> Tys) const {
383 // assume that we need to scalarize this intrinsic.
384 unsigned ScalarizationCost = 0;
385 unsigned ScalarCalls = 1;
386 if (RetTy->isVectorTy()) {
387 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
388 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
390 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
391 if (Tys[i]->isVectorTy()) {
392 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
393 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
396 return ScalarCalls + ScalarizationCost;
399 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
400 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);