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. This analysis uses the services of the codegen
12 // to approximate the cost of any IR instruction when lowered to machine
13 // instructions. The cost results are unit-less and the cost number represents
14 // the throughput of the machine assuming that all loads hit the cache, all
15 // branches are predicted, etc. The cost numbers can be added in order to
16 // compare two or more transformation alternatives.
18 //===----------------------------------------------------------------------===//
20 #define CM_NAME "cost-model"
21 #define DEBUG_TYPE CM_NAME
22 #include "llvm/Analysis/Passes.h"
23 #include "llvm/Function.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/TargetTransformInfo.h"
29 #include "llvm/Value.h"
33 class CostModelAnalysis : public FunctionPass {
36 static char ID; // Class identification, replacement for typeinfo
37 CostModelAnalysis() : FunctionPass(ID), F(0), VTTI(0) {
38 initializeCostModelAnalysisPass(
39 *PassRegistry::getPassRegistry());
42 /// Returns the expected cost of the instruction.
43 /// Returns -1 if the cost is unknown.
44 /// Note, this method does not cache the cost calculation and it
45 /// can be expensive in some cases.
46 unsigned getInstructionCost(const Instruction *I) const;
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
50 virtual bool runOnFunction(Function &F);
51 virtual void print(raw_ostream &OS, const Module*) const;
53 /// The function that we analyze.
55 /// Vector target information.
56 const VectorTargetTransformInfo *VTTI;
58 } // End of anonymous namespace
60 // Register this pass.
61 char CostModelAnalysis::ID = 0;
62 static const char cm_name[] = "Cost Model Analysis";
63 INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
64 INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
66 FunctionPass *llvm::createCostModelAnalysisPass() {
67 return new CostModelAnalysis();
71 CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
76 CostModelAnalysis::runOnFunction(Function &F) {
79 // Target information.
80 TargetTransformInfo *TTI;
81 TTI = getAnalysisIfAvailable<TargetTransformInfo>();
83 VTTI = TTI->getVectorTargetTransformInfo();
88 unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const {
92 switch (I->getOpcode()) {
93 case Instruction::Ret:
94 case Instruction::PHI:
95 case Instruction::Br: {
96 return VTTI->getCFInstrCost(I->getOpcode());
98 case Instruction::Add:
99 case Instruction::FAdd:
100 case Instruction::Sub:
101 case Instruction::FSub:
102 case Instruction::Mul:
103 case Instruction::FMul:
104 case Instruction::UDiv:
105 case Instruction::SDiv:
106 case Instruction::FDiv:
107 case Instruction::URem:
108 case Instruction::SRem:
109 case Instruction::FRem:
110 case Instruction::Shl:
111 case Instruction::LShr:
112 case Instruction::AShr:
113 case Instruction::And:
114 case Instruction::Or:
115 case Instruction::Xor: {
116 return VTTI->getArithmeticInstrCost(I->getOpcode(), I->getType());
118 case Instruction::Select: {
119 const SelectInst *SI = cast<SelectInst>(I);
120 Type *CondTy = SI->getCondition()->getType();
121 return VTTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
123 case Instruction::ICmp:
124 case Instruction::FCmp: {
125 Type *ValTy = I->getOperand(0)->getType();
126 return VTTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
128 case Instruction::Store: {
129 const StoreInst *SI = cast<StoreInst>(I);
130 Type *ValTy = SI->getValueOperand()->getType();
131 return VTTI->getMemoryOpCost(I->getOpcode(), ValTy,
133 SI->getPointerAddressSpace());
135 case Instruction::Load: {
136 const LoadInst *LI = cast<LoadInst>(I);
137 return VTTI->getMemoryOpCost(I->getOpcode(), I->getType(),
139 LI->getPointerAddressSpace());
141 case Instruction::ZExt:
142 case Instruction::SExt:
143 case Instruction::FPToUI:
144 case Instruction::FPToSI:
145 case Instruction::FPExt:
146 case Instruction::PtrToInt:
147 case Instruction::IntToPtr:
148 case Instruction::SIToFP:
149 case Instruction::UIToFP:
150 case Instruction::Trunc:
151 case Instruction::FPTrunc:
152 case Instruction::BitCast: {
153 Type *SrcTy = I->getOperand(0)->getType();
154 return VTTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy);
156 case Instruction::ExtractElement: {
157 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
158 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
161 Idx = CI->getZExtValue();
162 return VTTI->getVectorInstrCost(I->getOpcode(),
163 EEI->getOperand(0)->getType(), Idx);
165 case Instruction::InsertElement: {
166 const InsertElementInst * IE = cast<InsertElementInst>(I);
167 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
170 Idx = CI->getZExtValue();
171 return VTTI->getVectorInstrCost(I->getOpcode(),
175 // We don't have any information on this instruction.
180 void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
184 for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
185 for (BasicBlock::iterator it = B->begin(), e = B->end(); it != e; ++it) {
186 Instruction *Inst = it;
187 unsigned Cost = getInstructionCost(Inst);
188 if (Cost != (unsigned)-1)
189 OS << "Cost Model: Found an estimated cost of " << Cost;
191 OS << "Cost Model: Unknown cost";
193 OS << " for instruction: "<< *Inst << "\n";