CostModel.cpp revision 243789
1//===- 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" 23#include "llvm/TargetTransformInfo.h" 24#include "llvm/Value.h" 25#include "llvm/Support/Debug.h" 26#include "llvm/Support/raw_ostream.h" 27using 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. 43 unsigned getInstructionCost(Instruction *I) const; 44 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 85unsigned CostModelAnalysis::getInstructionCost(Instruction *I) const { 86 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: { 116 SelectInst *SI = cast<SelectInst>(I); 117 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: { 126 StoreInst *SI = cast<StoreInst>(I); 127 Type *ValTy = SI->getValueOperand()->getType(); 128 return VTTI->getMemoryOpCost(I->getOpcode(), ValTy, 129 SI->getAlignment(), 130 SI->getPointerAddressSpace()); 131 } 132 case Instruction::Load: { 133 LoadInst *LI = cast<LoadInst>(I); 134 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 } 153 case Instruction::ExtractElement: { 154 ExtractElementInst * EEI = cast<ExtractElementInst>(I); 155 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: { 163 InsertElementInst * IE = cast<InsertElementInst>(I); 164 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 } 171 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} 194