Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 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 |
Nadav Rotem | 99b7a99 | 2012-12-24 05:51:12 +0000 | [diff] [blame] | 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. |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | be04929 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Function.h" |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Arnold Schwaighofer | 65457b6 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | using namespace llvm; |
Guozhi Wei | 19969b8 | 2017-09-08 22:29:17 +0000 | [diff] [blame] | 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"))); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 39 | |
Chandler Carruth | 4da2537 | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 40 | #define CM_NAME "cost-model" |
| 41 | #define DEBUG_TYPE CM_NAME |
| 42 | |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | class CostModelAnalysis : public FunctionPass { |
| 45 | |
| 46 | public: |
| 47 | static char ID; // Class identification, replacement for typeinfo |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 48 | CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) { |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 49 | initializeCostModelAnalysisPass( |
| 50 | *PassRegistry::getPassRegistry()); |
| 51 | } |
| 52 | |
| 53 | /// Returns the expected cost of the instruction. |
| 54 | /// Returns -1 if the cost is unknown. |
| 55 | /// Note, this method does not cache the cost calculation and it |
| 56 | /// can be expensive in some cases. |
Guozhi Wei | 19969b8 | 2017-09-08 22:29:17 +0000 | [diff] [blame] | 57 | unsigned getInstructionCost(const Instruction *I) const { |
| 58 | return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput); |
| 59 | } |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 60 | |
| 61 | private: |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 62 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 63 | bool runOnFunction(Function &F) override; |
| 64 | void print(raw_ostream &OS, const Module*) const override; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 65 | |
| 66 | /// The function that we analyze. |
| 67 | Function *F; |
Chandler Carruth | 194bd71 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 68 | /// Target information. |
| 69 | const TargetTransformInfo *TTI; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 70 | }; |
| 71 | } // End of anonymous namespace |
| 72 | |
| 73 | // Register this pass. |
| 74 | char CostModelAnalysis::ID = 0; |
| 75 | static const char cm_name[] = "Cost Model Analysis"; |
| 76 | INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 77 | INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 78 | |
| 79 | FunctionPass *llvm::createCostModelAnalysisPass() { |
| 80 | return new CostModelAnalysis(); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 85 | AU.setPreservesAll(); |
| 86 | } |
| 87 | |
| 88 | bool |
| 89 | CostModelAnalysis::runOnFunction(Function &F) { |
| 90 | this->F = &F; |
Chandler Carruth | a6a87b5 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 91 | auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>(); |
Chandler Carruth | baceda7 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 92 | TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 97 | void CostModelAnalysis::print(raw_ostream &OS, const Module*) const { |
| 98 | if (!F) |
| 99 | return; |
| 100 | |
Benjamin Kramer | 8d0d2b6 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 101 | for (BasicBlock &B : *F) { |
| 102 | for (Instruction &Inst : B) { |
Guozhi Wei | 19969b8 | 2017-09-08 22:29:17 +0000 | [diff] [blame] | 103 | unsigned Cost = TTI->getInstructionCost(&Inst, CostKind); |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 104 | if (Cost != (unsigned)-1) |
| 105 | OS << "Cost Model: Found an estimated cost of " << Cost; |
| 106 | else |
| 107 | OS << "Cost Model: Unknown cost"; |
| 108 | |
Benjamin Kramer | 8d0d2b6 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 109 | OS << " for instruction: " << Inst << "\n"; |
Nadav Rotem | 6bed58e | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |