1 //===- CostModel.cpp ------ Cost Model Analysis ---------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the cost model analysis. It provides a very basic cost 10 // estimation for LLVM-IR. This analysis uses the services of the codegen 11 // to approximate the cost of any IR instruction when lowered to machine 12 // instructions. The cost results are unit-less and the cost number represents 13 // the throughput of the machine assuming that all loads hit the cache, all 14 // branches are predicted, etc. The cost numbers can be added in order to 15 // compare two or more transformation alternatives. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/Analysis/Passes.h" 21 #include "llvm/Analysis/TargetTransformInfo.h" 22 #include "llvm/IR/Function.h" 23 #include "llvm/InitializePasses.h" 24 #include "llvm/Pass.h" 25 #include "llvm/Support/CommandLine.h" 26 #include "llvm/Support/Debug.h" 27 #include "llvm/Support/raw_ostream.h" 28 using namespace llvm; 29 30 static cl::opt<TargetTransformInfo::TargetCostKind> CostKind( 31 "cost-kind", cl::desc("Target cost kind"), 32 cl::init(TargetTransformInfo::TCK_RecipThroughput), 33 cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, 34 "throughput", "Reciprocal throughput"), 35 clEnumValN(TargetTransformInfo::TCK_Latency, 36 "latency", "Instruction latency"), 37 clEnumValN(TargetTransformInfo::TCK_CodeSize, 38 "code-size", "Code size"), 39 clEnumValN(TargetTransformInfo::TCK_SizeAndLatency, 40 "size-latency", "Code size and latency"))); 41 42 43 #define CM_NAME "cost-model" 44 #define DEBUG_TYPE CM_NAME 45 46 namespace { 47 class CostModelAnalysis : public FunctionPass { 48 49 public: 50 static char ID; // Class identification, replacement for typeinfo 51 CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) { 52 initializeCostModelAnalysisPass( 53 *PassRegistry::getPassRegistry()); 54 } 55 56 /// Returns the expected cost of the instruction. 57 /// Returns -1 if the cost is unknown. 58 /// Note, this method does not cache the cost calculation and it 59 /// can be expensive in some cases. 60 InstructionCost getInstructionCost(const Instruction *I) const { 61 return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput); 62 } 63 64 private: 65 void getAnalysisUsage(AnalysisUsage &AU) const override; 66 bool runOnFunction(Function &F) override; 67 void print(raw_ostream &OS, const Module*) const override; 68 69 /// The function that we analyze. 70 Function *F; 71 /// Target information. 72 const TargetTransformInfo *TTI; 73 }; 74 } // End of anonymous namespace 75 76 // Register this pass. 77 char CostModelAnalysis::ID = 0; 78 static const char cm_name[] = "Cost Model Analysis"; 79 INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true) 80 INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true) 81 82 FunctionPass *llvm::createCostModelAnalysisPass() { 83 return new CostModelAnalysis(); 84 } 85 86 void 87 CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 88 AU.setPreservesAll(); 89 } 90 91 bool 92 CostModelAnalysis::runOnFunction(Function &F) { 93 this->F = &F; 94 auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>(); 95 TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr; 96 97 return false; 98 } 99 100 void CostModelAnalysis::print(raw_ostream &OS, const Module*) const { 101 if (!F) 102 return; 103 104 for (BasicBlock &B : *F) { 105 for (Instruction &Inst : B) { 106 InstructionCost Cost = TTI->getInstructionCost(&Inst, CostKind); 107 if (auto CostVal = Cost.getValue()) 108 OS << "Cost Model: Found an estimated cost of " << *CostVal; 109 else 110 OS << "Cost Model: Unknown cost"; 111 112 OS << " for instruction: " << Inst << "\n"; 113 } 114 } 115 } 116