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 TargetLowering *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 TargetLowering *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 getArithmeticInstrCost(unsigned Opcode, Type *Ty) const;
87 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
88 int Index, Type *SubTp) const;
89 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
91 virtual unsigned getCFInstrCost(unsigned Opcode) const;
92 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
94 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
95 unsigned Index) const;
96 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
98 unsigned AddressSpace) const;
99 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
100 ArrayRef<Type*> Tys) const;
101 virtual unsigned getNumberOfParts(Type *Tp) const;
108 INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
109 "Target independent code generator's TTI", true, true, false)
110 char BasicTTI::ID = 0;
113 llvm::createBasicTargetTransformInfoPass(const TargetLowering *TLI) {
114 return new BasicTTI(TLI);
118 bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
119 return TLI->isLegalAddImmediate(imm);
122 bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
123 return TLI->isLegalICmpImmediate(imm);
126 bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
127 int64_t BaseOffset, bool HasBaseReg,
128 int64_t Scale) const {
131 AM.BaseOffs = BaseOffset;
132 AM.HasBaseReg = HasBaseReg;
134 return TLI->isLegalAddressingMode(AM, Ty);
137 bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
138 return TLI->isTruncateFree(Ty1, Ty2);
141 bool BasicTTI::isTypeLegal(Type *Ty) const {
142 EVT T = TLI->getValueType(Ty);
143 return TLI->isTypeLegal(T);
146 unsigned BasicTTI::getJumpBufAlignment() const {
147 return TLI->getJumpBufAlignment();
150 unsigned BasicTTI::getJumpBufSize() const {
151 return TLI->getJumpBufSize();
154 bool BasicTTI::shouldBuildLookupTables() const {
155 return TLI->supportJumpTables() &&
156 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
157 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
160 //===----------------------------------------------------------------------===//
162 // Calls used by the vectorizers.
164 //===----------------------------------------------------------------------===//
166 unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
167 bool Extract) const {
168 assert (Ty->isVectorTy() && "Can only scalarize vectors");
171 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
173 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
175 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
181 unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
185 unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
186 // Check if any of the operands are vector operands.
187 int ISD = TLI->InstructionOpcodeToISD(Opcode);
188 assert(ISD && "Invalid opcode");
190 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
192 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
193 // The operation is legal. Assume it costs 1.
194 // If the type is split to multiple registers, assume that thre is some
196 // TODO: Once we have extract/insert subvector cost we need to use them.
202 if (!TLI->isOperationExpand(ISD, LT.second)) {
203 // If the operation is custom lowered then assume
204 // thare the code is twice as expensive.
208 // Else, assume that we need to scalarize this op.
209 if (Ty->isVectorTy()) {
210 unsigned Num = Ty->getVectorNumElements();
211 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
212 // return the cost of multiple scalar invocation plus the cost of inserting
213 // and extracting the values.
214 return getScalarizationOverhead(Ty, true, true) + Num * Cost;
217 // We don't know anything about this scalar instruction.
221 unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
226 unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
228 int ISD = TLI->InstructionOpcodeToISD(Opcode);
229 assert(ISD && "Invalid opcode");
231 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
232 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
234 // Handle scalar conversions.
235 if (!Src->isVectorTy() && !Dst->isVectorTy()) {
237 // Scalar bitcasts are usually free.
238 if (Opcode == Instruction::BitCast)
241 if (Opcode == Instruction::Trunc &&
242 TLI->isTruncateFree(SrcLT.second, DstLT.second))
245 if (Opcode == Instruction::ZExt &&
246 TLI->isZExtFree(SrcLT.second, DstLT.second))
249 // Just check the op cost. If the operation is legal then assume it costs 1.
250 if (!TLI->isOperationExpand(ISD, DstLT.second))
253 // Assume that illegal scalar instruction are expensive.
257 // Check vector-to-vector casts.
258 if (Dst->isVectorTy() && Src->isVectorTy()) {
260 // If the cast is between same-sized registers, then the check is simple.
261 if (SrcLT.first == DstLT.first &&
262 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
264 // Bitcast between types that are legalized to the same type are free.
265 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
268 // Assume that Zext is done using AND.
269 if (Opcode == Instruction::ZExt)
272 // Assume that sext is done using SHL and SRA.
273 if (Opcode == Instruction::SExt)
276 // Just check the op cost. If the operation is legal then assume it costs
277 // 1 and multiply by the type-legalization overhead.
278 if (!TLI->isOperationExpand(ISD, DstLT.second))
279 return SrcLT.first * 1;
282 // If we are converting vectors and the operation is illegal, or
283 // if the vectors are legalized to different types, estimate the
284 // scalarization costs.
285 unsigned Num = Dst->getVectorNumElements();
286 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
287 Src->getScalarType());
289 // Return the cost of multiple scalar invocation plus the cost of
290 // inserting and extracting the values.
291 return getScalarizationOverhead(Dst, true, true) + Num * Cost;
294 // We already handled vector-to-vector and scalar-to-scalar conversions. This
295 // is where we handle bitcast between vectors and scalars. We need to assume
296 // that the conversion is scalarized in one way or another.
297 if (Opcode == Instruction::BitCast)
298 // Illegal bitcasts are done by storing and loading from a stack slot.
299 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
300 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
302 llvm_unreachable("Unhandled cast");
305 unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
306 // Branches are assumed to be predicted.
310 unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
311 Type *CondTy) const {
312 int ISD = TLI->InstructionOpcodeToISD(Opcode);
313 assert(ISD && "Invalid opcode");
315 // Selects on vectors are actually vector selects.
316 if (ISD == ISD::SELECT) {
317 assert(CondTy && "CondTy must exist");
318 if (CondTy->isVectorTy())
322 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
324 if (!TLI->isOperationExpand(ISD, LT.second)) {
325 // The operation is legal. Assume it costs 1. Multiply
326 // by the type-legalization overhead.
330 // Otherwise, assume that the cast is scalarized.
331 if (ValTy->isVectorTy()) {
332 unsigned Num = ValTy->getVectorNumElements();
334 CondTy = CondTy->getScalarType();
335 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(),
338 // Return the cost of multiple scalar invocation plus the cost of inserting
339 // and extracting the values.
340 return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
343 // Unknown scalar opcode.
347 unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
348 unsigned Index) const {
352 unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
354 unsigned AddressSpace) const {
355 assert(!Src->isVoidTy() && "Invalid type");
356 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
358 // Assume that all loads of legal types cost 1.
362 unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
363 ArrayRef<Type *> Tys) const {
364 // assume that we need to scalarize this intrinsic.
365 unsigned ScalarizationCost = 0;
366 unsigned ScalarCalls = 1;
367 if (RetTy->isVectorTy()) {
368 ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
369 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
371 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
372 if (Tys[i]->isVectorTy()) {
373 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
374 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
377 return ScalarCalls + ScalarizationCost;
380 unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
381 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);