blob: 8435e397077f48f2857f2b24034f66c2ba1d7cbc [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
Nadav Rotem99b7a992012-12-24 05:51:12 +000011// 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.
Nadav Rotem6bed58e2012-11-02 21:48:17 +000017//
18//===----------------------------------------------------------------------===//
19
20#define CM_NAME "cost-model"
21#define DEBUG_TYPE CM_NAME
22#include "llvm/Analysis/Passes.h"
Chandler Carruthbe049292013-01-07 03:08:10 +000023#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/Value.h"
Nadav Rotem6bed58e2012-11-02 21:48:17 +000027#include "llvm/Pass.h"
Nadav Rotem6bed58e2012-11-02 21:48:17 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace llvm;
31
32namespace {
33 class CostModelAnalysis : public FunctionPass {
34
35 public:
36 static char ID; // Class identification, replacement for typeinfo
Chandler Carruth194bd712013-01-05 10:09:33 +000037 CostModelAnalysis() : FunctionPass(ID), F(0), TTI(0) {
Nadav Rotem6bed58e2012-11-02 21:48:17 +000038 initializeCostModelAnalysisPass(
39 *PassRegistry::getPassRegistry());
40 }
41
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.
Nadav Roteme70b2682012-12-03 22:47:12 +000046 unsigned getInstructionCost(const Instruction *I) const;
Nadav Rotem6bed58e2012-11-02 21:48:17 +000047
48 private:
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
50 virtual bool runOnFunction(Function &F);
51 virtual void print(raw_ostream &OS, const Module*) const;
52
53 /// The function that we analyze.
54 Function *F;
Chandler Carruth194bd712013-01-05 10:09:33 +000055 /// Target information.
56 const TargetTransformInfo *TTI;
Nadav Rotem6bed58e2012-11-02 21:48:17 +000057 };
58} // End of anonymous namespace
59
60// Register this pass.
61char CostModelAnalysis::ID = 0;
62static const char cm_name[] = "Cost Model Analysis";
63INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true)
64INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true)
65
66FunctionPass *llvm::createCostModelAnalysisPass() {
67 return new CostModelAnalysis();
68}
69
70void
71CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.setPreservesAll();
73}
74
75bool
76CostModelAnalysis::runOnFunction(Function &F) {
77 this->F = &F;
Nadav Rotem6bed58e2012-11-02 21:48:17 +000078 TTI = getAnalysisIfAvailable<TargetTransformInfo>();
Nadav Rotem6bed58e2012-11-02 21:48:17 +000079
80 return false;
81}
82
Nadav Roteme70b2682012-12-03 22:47:12 +000083unsigned CostModelAnalysis::getInstructionCost(const Instruction *I) const {
Chandler Carruth194bd712013-01-05 10:09:33 +000084 if (!TTI)
Nadav Rotem6bed58e2012-11-02 21:48:17 +000085 return -1;
86
87 switch (I->getOpcode()) {
Arnold Schwaighoferfb55a8f2013-02-08 14:50:48 +000088 case Instruction::GetElementPtr:{
89 Type *ValTy = I->getOperand(0)->getType()->getPointerElementType();
90 return TTI->getAddressComputationCost(ValTy);
91 }
92
Nadav Rotem6bed58e2012-11-02 21:48:17 +000093 case Instruction::Ret:
94 case Instruction::PHI:
95 case Instruction::Br: {
Chandler Carruth194bd712013-01-05 10:09:33 +000096 return TTI->getCFInstrCost(I->getOpcode());
Nadav Rotem6bed58e2012-11-02 21:48:17 +000097 }
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: {
Chandler Carruth194bd712013-01-05 10:09:33 +0000116 return TTI->getArithmeticInstrCost(I->getOpcode(), I->getType());
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000117 }
118 case Instruction::Select: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000119 const SelectInst *SI = cast<SelectInst>(I);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000120 Type *CondTy = SI->getCondition()->getType();
Chandler Carruth194bd712013-01-05 10:09:33 +0000121 return TTI->getCmpSelInstrCost(I->getOpcode(), I->getType(), CondTy);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000122 }
123 case Instruction::ICmp:
124 case Instruction::FCmp: {
125 Type *ValTy = I->getOperand(0)->getType();
Chandler Carruth194bd712013-01-05 10:09:33 +0000126 return TTI->getCmpSelInstrCost(I->getOpcode(), ValTy);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000127 }
128 case Instruction::Store: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000129 const StoreInst *SI = cast<StoreInst>(I);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000130 Type *ValTy = SI->getValueOperand()->getType();
Chandler Carruth194bd712013-01-05 10:09:33 +0000131 return TTI->getMemoryOpCost(I->getOpcode(), ValTy,
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000132 SI->getAlignment(),
133 SI->getPointerAddressSpace());
134 }
135 case Instruction::Load: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000136 const LoadInst *LI = cast<LoadInst>(I);
Chandler Carruth194bd712013-01-05 10:09:33 +0000137 return TTI->getMemoryOpCost(I->getOpcode(), I->getType(),
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000138 LI->getAlignment(),
139 LI->getPointerAddressSpace());
140 }
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();
Chandler Carruth194bd712013-01-05 10:09:33 +0000154 return TTI->getCastInstrCost(I->getOpcode(), I->getType(), SrcTy);
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000155 }
Nadav Rotemf1495672012-11-02 22:31:56 +0000156 case Instruction::ExtractElement: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000157 const ExtractElementInst * EEI = cast<ExtractElementInst>(I);
Nadav Rotemf1495672012-11-02 22:31:56 +0000158 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
159 unsigned Idx = -1;
160 if (CI)
161 Idx = CI->getZExtValue();
Chandler Carruth194bd712013-01-05 10:09:33 +0000162 return TTI->getVectorInstrCost(I->getOpcode(),
163 EEI->getOperand(0)->getType(), Idx);
Nadav Rotemf1495672012-11-02 22:31:56 +0000164 }
165 case Instruction::InsertElement: {
Nadav Roteme70b2682012-12-03 22:47:12 +0000166 const InsertElementInst * IE = cast<InsertElementInst>(I);
Nadav Rotemf1495672012-11-02 22:31:56 +0000167 ConstantInt *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
168 unsigned Idx = -1;
169 if (CI)
170 Idx = CI->getZExtValue();
Chandler Carruth194bd712013-01-05 10:09:33 +0000171 return TTI->getVectorInstrCost(I->getOpcode(),
172 IE->getType(), Idx);
Nadav Rotemf1495672012-11-02 22:31:56 +0000173 }
Nadav Rotem6bed58e2012-11-02 21:48:17 +0000174 default:
175 // We don't have any information on this instruction.
176 return -1;
177 }
178}
179
180void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
181 if (!F)
182 return;
183
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;
190 else
191 OS << "Cost Model: Unknown cost";
192
193 OS << " for instruction: "<< *Inst << "\n";
194 }
195 }
196}