blob: 0d0a8933b35c61aff5b979bd597cff033e6a4178 [file] [log] [blame]
Nadav Rotem6bed58e2012-11-02 21:48:17 +00001//===- CostModel.cpp ------ Cost Model Analysis ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
13// alternatives.
14//
15//===----------------------------------------------------------------------===//
16
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"
Nadav Rotem6bed58e2012-11-02 21:48:17 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/TargetTransformInfo.h"
26#include "llvm/Value.h"
Nadav Rotem6bed58e2012-11-02 21:48:17 +000027using namespace llvm;
28
29namespace {
30 class CostModelAnalysis : public FunctionPass {
31
32 public:
33 static char ID; // Class identification, replacement for typeinfo
34 CostModelAnalysis() : FunctionPass(ID), F(0), VTTI(0) {
35 initializeCostModelAnalysisPass(
36 *PassRegistry::getPassRegistry());
37 }
38
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.
Nadav Roteme70b2682012-12-03 22:47:12 +000043 unsigned getInstructionCost(const Instruction *I) const;
Nadav Rotem6bed58e2012-11-02 21:48:17 +000044
45 private:
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
47 virtual bool runOnFunction(Function &F);
48 virtual void print(raw_ostream &OS, const Module*) const;
49
50 /// The function that we analyze.
51 Function *F;
52 /// Vector target information.
53 const VectorTargetTransformInfo *VTTI;
54 };
55} // End of anonymous namespace
56
57// Register this pass.
58char CostModelAnalysis::ID = 0;
59static const char cm_name[] = "Cost Model Analysis";
60INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
61INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
62
63FunctionPass *llvm::createCostModelAnalysisPass() {
64 return new CostModelAnalysis();
65}
66
67void
68CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.setPreservesAll();
70}
71
72bool
73CostModelAnalysis::runOnFunction(Function &F) {
74 this->F = &F;
75
76 // Target information.
77 TargetTransformInfo *TTI;
78 TTI = getAnalysisIfAvailable<TargetTransformInfo>();
79 if (TTI)
80 VTTI = TTI->getVectorTargetTransformInfo();
81
82 return false;
83}
84
Nadav Roteme70b2682012-12-03 22:47:12 +000085unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const {
Nadav Rotem6bed58e2012-11-02 21:48:17 +000086 if (!VTTI)
87 return -1;
88
89 switch (I->getOpcode()) {
90 case Instruction::Ret:
91 case Instruction::PHI:
92 case Instruction::Br: {
93 return VTTI->getCFInstrCost(I->getOpcode());
94 }
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());
114 }
115 case Instruction::Select: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000116 const SelectInst *SI = cast<SelectInst>(I);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000117 Type *CondTy = SI->getCondition()->getType();
118 return VTTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
119 }
120 case Instruction::ICmp:
121 case Instruction::FCmp: {
122 Type *ValTy = I->getOperand(0)->getType();
123 return VTTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
124 }
125 case Instruction::Store: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000126 const StoreInst *SI = cast<StoreInst>(I);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000127 Type *ValTy = SI->getValueOperand()->getType();
128 return VTTI->getMemoryOpCost(I->getOpcode(), ValTy,
129 SI->getAlignment(),
130 SI->getPointerAddressSpace());
131 }
132 case Instruction::Load: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000133 const LoadInst *LI = cast<LoadInst>(I);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000134 return VTTI->getMemoryOpCost(I->getOpcode(), I->getType(),
135 LI->getAlignment(),
136 LI->getPointerAddressSpace());
137 }
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);
152 }
Nadav Rotemf1495672012-11-02 22:31:56 +0000153 case Instruction::ExtractElement: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000154 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
Nadav Rotemf1495672012-11-02 22:31:56 +0000155 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
156 unsigned Idx = -1;
157 if (CI)
158 Idx = CI->getZExtValue();
159 return VTTI->getVectorInstrCost(I->getOpcode(),
160 EEI->getOperand(0)->getType(), Idx);
161 }
162 case Instruction::InsertElement: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000163 const InsertElementInst * IE = cast<InsertElementInst>(I);
Nadav Rotemf1495672012-11-02 22:31:56 +0000164 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
165 unsigned Idx = -1;
166 if (CI)
167 Idx = CI->getZExtValue();
168 return VTTI->getVectorInstrCost(I->getOpcode(),
169 IE->getType(), Idx);
170 }
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000171 default:
172 // We don't have any information on this instruction.
173 return -1;
174 }
175}
176
177void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
178 if (!F)
179 return;
180
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;
187 else
188 OS << "Cost Model: Unknown cost";
189
190 OS << " for instruction: "<< *Inst << "\n";
191 }
192 }
193}