1 //===- CostModel.cpp ------ Cost Model Analysis ---------------------------===//
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 defines the cost model analysis. It provides a very basic cost
11 // estimation for LLVM-IR. The cost result can be thought of as cycles, but it
12 // is really unit-less. The estimated cost is ment to be used for comparing
15 //===----------------------------------------------------------------------===//
17 #define CM_NAME "cost-model"
18 #define DEBUG_TYPE CM_NAME
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Function.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Pass.h"
23 #include "llvm/TargetTransformInfo.h"
24 #include "llvm/Value.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
30 class CostModelAnalysis : public FunctionPass {
33 static char ID; // Class identification, replacement for typeinfo
34 CostModelAnalysis() : FunctionPass(ID), F(0), VTTI(0) {
35 initializeCostModelAnalysisPass(
36 *PassRegistry::getPassRegistry());
39 /// Returns the expected cost of the instruction.
40 /// Returns -1 if the cost is unknown.
41 /// Note, this method does not cache the cost calculation and it
42 /// can be expensive in some cases.
43 unsigned getInstructionCost(Instruction *I) const;
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
47 virtual bool runOnFunction(Function &F);
48 virtual void print(raw_ostream &OS, const Module*) const;
50 /// The function that we analyze.
52 /// Vector target information.
53 const VectorTargetTransformInfo *VTTI;
55 } // End of anonymous namespace
57 // Register this pass.
58 char CostModelAnalysis::ID = 0;
59 static const char cm_name[] = "Cost Model Analysis";
60 INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
61 INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
63 FunctionPass *llvm::createCostModelAnalysisPass() {
64 return new CostModelAnalysis();
68 CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
73 CostModelAnalysis::runOnFunction(Function &F) {
76 // Target information.
77 TargetTransformInfo *TTI;
78 TTI = getAnalysisIfAvailable<TargetTransformInfo>();
80 VTTI = TTI->getVectorTargetTransformInfo();
85 unsigned CostModelAnalysis::getInstructionCost(Instruction *I) const {
89 switch (I->getOpcode()) {
90 case Instruction::Ret:
91 case Instruction::PHI:
92 case Instruction::Br: {
93 return VTTI->getCFInstrCost(I->getOpcode());
95 case Instruction::Add:
96 case Instruction::FAdd:
97 case Instruction::Sub:
98 case Instruction::FSub:
99 case Instruction::Mul:
100 case Instruction::FMul:
101 case Instruction::UDiv:
102 case Instruction::SDiv:
103 case Instruction::FDiv:
104 case Instruction::URem:
105 case Instruction::SRem:
106 case Instruction::FRem:
107 case Instruction::Shl:
108 case Instruction::LShr:
109 case Instruction::AShr:
110 case Instruction::And:
111 case Instruction::Or:
112 case Instruction::Xor: {
113 return VTTI->getArithmeticInstrCost(I->getOpcode(), I->getType());
115 case Instruction::Select: {
116 SelectInst *SI = cast<SelectInst>(I);
117 Type *CondTy = SI->getCondition()->getType();
118 return VTTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
120 case Instruction::ICmp:
121 case Instruction::FCmp: {
122 Type *ValTy = I->getOperand(0)->getType();
123 return VTTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
125 case Instruction::Store: {
126 StoreInst *SI = cast<StoreInst>(I);
127 Type *ValTy = SI->getValueOperand()->getType();
128 return VTTI->getMemoryOpCost(I->getOpcode(), ValTy,
130 SI->getPointerAddressSpace());
132 case Instruction::Load: {
133 LoadInst *LI = cast<LoadInst>(I);
134 return VTTI->getMemoryOpCost(I->getOpcode(), I->getType(),
136 LI->getPointerAddressSpace());
138 case Instruction::ZExt:
139 case Instruction::SExt:
140 case Instruction::FPToUI:
141 case Instruction::FPToSI:
142 case Instruction::FPExt:
143 case Instruction::PtrToInt:
144 case Instruction::IntToPtr:
145 case Instruction::SIToFP:
146 case Instruction::UIToFP:
147 case Instruction::Trunc:
148 case Instruction::FPTrunc:
149 case Instruction::BitCast: {
150 Type *SrcTy = I->getOperand(0)->getType();
151 return VTTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy);
153 case Instruction::ExtractElement: {
154 ExtractElementInst * EEI = cast<ExtractElementInst>(I);
155 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
158 Idx = CI->getZExtValue();
159 return VTTI->getVectorInstrCost(I->getOpcode(),
160 EEI->getOperand(0)->getType(), Idx);
162 case Instruction::InsertElement: {
163 InsertElementInst * IE = cast<InsertElementInst>(I);
164 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
167 Idx = CI->getZExtValue();
168 return VTTI->getVectorInstrCost(I->getOpcode(),
172 // We don't have any information on this instruction.
177 void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
181 for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
182 for (BasicBlock::iterator it = B->begin(), e = B->end(); it != e; ++it) {
183 Instruction *Inst = it;
184 unsigned Cost = getInstructionCost(Inst);
185 if (Cost != (unsigned)-1)
186 OS << "Cost Model: Found an estimated cost of " << Cost;
188 OS << "Cost Model: Unknown cost";
190 OS << " for instruction: "<< *Inst << "\n";