1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
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 #define DEBUG_TYPE "tti"
11 #include "llvm/Analysis/TargetTransformInfo.h"
12 #include "llvm/Support/ErrorHandling.h"
16 // Setup the analysis group to manage the TargetTransformInfo passes.
17 INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI)
18 char TargetTransformInfo::ID = 0;
20 TargetTransformInfo::~TargetTransformInfo() {
23 void TargetTransformInfo::pushTTIStack(Pass *P) {
25 PrevTTI = &P->getAnalysis<TargetTransformInfo>();
27 // Walk up the chain and update the top TTI pointer.
28 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
32 void TargetTransformInfo::popTTIStack() {
35 // Walk up the chain and update the top TTI pointer.
36 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
37 PTTI->TopTTI = PrevTTI;
42 void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired<TargetTransformInfo>();
46 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
47 return PrevTTI->isLegalAddImmediate(Imm);
50 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
51 return PrevTTI->isLegalICmpImmediate(Imm);
54 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
57 int64_t Scale) const {
58 return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
62 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
63 return PrevTTI->isTruncateFree(Ty1, Ty2);
66 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
67 return PrevTTI->isTypeLegal(Ty);
70 unsigned TargetTransformInfo::getJumpBufAlignment() const {
71 return PrevTTI->getJumpBufAlignment();
74 unsigned TargetTransformInfo::getJumpBufSize() const {
75 return PrevTTI->getJumpBufSize();
78 bool TargetTransformInfo::shouldBuildLookupTables() const {
79 return PrevTTI->shouldBuildLookupTables();
82 TargetTransformInfo::PopcntSupportKind
83 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
84 return PrevTTI->getPopcntSupport(IntTyWidthInBit);
87 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
88 return PrevTTI->getIntImmCost(Imm, Ty);
91 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
92 return PrevTTI->getNumberOfRegisters(Vector);
95 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
96 return PrevTTI->getRegisterBitWidth(Vector);
99 unsigned TargetTransformInfo::getMaximumUnrollFactor() const {
100 return PrevTTI->getMaximumUnrollFactor();
103 unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
105 return PrevTTI->getArithmeticInstrCost(Opcode, Ty);
108 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
109 int Index, Type *SubTp) const {
110 return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
113 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
115 return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
118 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
119 return PrevTTI->getCFInstrCost(Opcode);
122 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
123 Type *CondTy) const {
124 return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
127 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
128 unsigned Index) const {
129 return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
132 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
134 unsigned AddressSpace) const {
135 return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
140 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
142 ArrayRef<Type *> Tys) const {
143 return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
146 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
147 return PrevTTI->getNumberOfParts(Tp);
153 struct NoTTI : ImmutablePass, TargetTransformInfo {
154 NoTTI() : ImmutablePass(ID) {
155 initializeNoTTIPass(*PassRegistry::getPassRegistry());
158 virtual void initializePass() {
159 // Note that this subclass is special, and must *not* call initializeTTI as
160 // it does not chain.
164 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
165 // Note that this subclass is special, and must *not* call
166 // TTI::getAnalysisUsage as it breaks the recursion.
169 /// Pass identification.
172 /// Provide necessary pointer adjustments for the two base classes.
173 virtual void *getAdjustedAnalysisPointer(const void *ID) {
174 if (ID == &TargetTransformInfo::ID)
175 return (TargetTransformInfo*)this;
180 bool isLegalAddImmediate(int64_t Imm) const {
184 bool isLegalICmpImmediate(int64_t Imm) const {
188 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
189 bool HasBaseReg, int64_t Scale) const {
190 // Guess that reg+reg addressing is allowed. This heuristic is taken from
191 // the implementation of LSR.
192 return !BaseGV && BaseOffset == 0 && Scale <= 1;
195 bool isTruncateFree(Type *Ty1, Type *Ty2) const {
199 bool isTypeLegal(Type *Ty) const {
203 unsigned getJumpBufAlignment() const {
207 unsigned getJumpBufSize() const {
211 bool shouldBuildLookupTables() const {
215 PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const {
219 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const {
223 unsigned getNumberOfRegisters(bool Vector) const {
227 unsigned getRegisterBitWidth(bool Vector) const {
231 unsigned getMaximumUnrollFactor() const {
235 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
239 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
240 int Index = 0, Type *SubTp = 0) const {
244 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
249 unsigned getCFInstrCost(unsigned Opcode) const {
253 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
254 Type *CondTy = 0) const {
258 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
259 unsigned Index = -1) const {
263 unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
265 unsigned AddressSpace) const {
269 unsigned getIntrinsicInstrCost(Intrinsic::ID ID,
271 ArrayRef<Type*> Tys) const {
275 unsigned getNumberOfParts(Type *Tp) const {
280 } // end anonymous namespace
282 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
283 "No target information", true, true, true)
286 ImmutablePass *llvm::createNoTargetTransformInfoPass() {
290 //======================================= COST TABLES ==
292 CostTable::CostTable(const CostTableEntry *table,
295 : table(table), size(size), numTypes(numTypes) {
296 assert(table && "missing cost table");
297 assert(size > 0 && "empty cost table");
300 unsigned CostTable::_findCost(int ISD, MVT *Types) const {
301 for (unsigned i = 0; i < size; ++i) {
302 if (table[i].ISD != ISD)
305 for (unsigned t=0; t<numTypes; t++) {
306 if (table[i].Types[t] != Types[t]) {
312 return table[i].Cost;
314 return COST_NOT_FOUND;
317 UnaryCostTable::UnaryCostTable(const CostTableEntry *table,
319 : CostTable(table, size, 1) { }
321 unsigned UnaryCostTable::findCost(int ISD, MVT Type) const {
322 MVT tys[1] = { Type };
323 return _findCost(ISD, tys);
326 BinaryCostTable::BinaryCostTable(const CostTableEntry *table,
328 : CostTable(table, size, 2) { }
330 unsigned BinaryCostTable::findCost(int ISD, MVT Type, MVT SrcType) const {
331 MVT tys[2] = { Type, SrcType };
332 return _findCost(ISD, tys);