Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 1 | //===- CostModel.cpp ------ Cost Model Analysis ---------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the cost model analysis. It provides a very basic cost |
Nadav Rotem | 99868e4 | 2012-12-24 05:51:12 +0000 | [diff] [blame] | 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. |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Function.h" |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
Arnold Schwaighofer | cae8735 | 2013-09-17 18:06:50 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | using namespace llvm; |
Guozhi Wei | 62d6414 | 2017-09-08 22:29:17 +0000 | [diff] [blame] | 28 | |
| 29 | static cl::opt<TargetTransformInfo::TargetCostKind> CostKind( |
| 30 | "cost-kind", cl::desc("Target cost kind"), |
| 31 | cl::init(TargetTransformInfo::TCK_RecipThroughput), |
| 32 | cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, |
| 33 | "throughput", "Reciprocal throughput"), |
| 34 | clEnumValN(TargetTransformInfo::TCK_Latency, |
| 35 | "latency", "Instruction latency"), |
| 36 | clEnumValN(TargetTransformInfo::TCK_CodeSize, |
| 37 | "code-size", "Code size"))); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 38 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 39 | #define CM_NAME "cost-model" |
| 40 | #define DEBUG_TYPE CM_NAME |
| 41 | |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | class CostModelAnalysis : public FunctionPass { |
| 44 | |
| 45 | public: |
| 46 | static char ID; // Class identification, replacement for typeinfo |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 47 | CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) { |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 48 | initializeCostModelAnalysisPass( |
| 49 | *PassRegistry::getPassRegistry()); |
| 50 | } |
| 51 | |
| 52 | /// Returns the expected cost of the instruction. |
| 53 | /// Returns -1 if the cost is unknown. |
| 54 | /// Note, this method does not cache the cost calculation and it |
| 55 | /// can be expensive in some cases. |
Guozhi Wei | 62d6414 | 2017-09-08 22:29:17 +0000 | [diff] [blame] | 56 | unsigned getInstructionCost(const Instruction *I) const { |
| 57 | return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput); |
| 58 | } |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 59 | |
| 60 | private: |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 61 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 62 | bool runOnFunction(Function &F) override; |
| 63 | void print(raw_ostream &OS, const Module*) const override; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 64 | |
| 65 | /// The function that we analyze. |
| 66 | Function *F; |
Chandler Carruth | cf569a8 | 2013-01-05 10:09:33 +0000 | [diff] [blame] | 67 | /// Target information. |
| 68 | const TargetTransformInfo *TTI; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 69 | }; |
| 70 | } // End of anonymous namespace |
| 71 | |
| 72 | // Register this pass. |
| 73 | char CostModelAnalysis::ID = 0; |
| 74 | static const char cm_name[] = "Cost Model Analysis"; |
| 75 | INITIALIZE_PASS_BEGIN(CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 76 | INITIALIZE_PASS_END (CostModelAnalysis, CM_NAME, cm_name, false, true) |
| 77 | |
| 78 | FunctionPass *llvm::createCostModelAnalysisPass() { |
| 79 | return new CostModelAnalysis(); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 84 | AU.setPreservesAll(); |
| 85 | } |
| 86 | |
| 87 | bool |
| 88 | CostModelAnalysis::runOnFunction(Function &F) { |
| 89 | this->F = &F; |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 90 | auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>(); |
Chandler Carruth | fdb9c57 | 2015-02-01 12:01:35 +0000 | [diff] [blame] | 91 | TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 96 | void CostModelAnalysis::print(raw_ostream &OS, const Module*) const { |
| 97 | if (!F) |
| 98 | return; |
| 99 | |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 100 | for (BasicBlock &B : *F) { |
| 101 | for (Instruction &Inst : B) { |
Guozhi Wei | 62d6414 | 2017-09-08 22:29:17 +0000 | [diff] [blame] | 102 | unsigned Cost = TTI->getInstructionCost(&Inst, CostKind); |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 103 | if (Cost != (unsigned)-1) |
| 104 | OS << "Cost Model: Found an estimated cost of " << Cost; |
| 105 | else |
| 106 | OS << "Cost Model: Unknown cost"; |
| 107 | |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 108 | OS << " for instruction: " << Inst << "\n"; |
Nadav Rotem | a6b91ac | 2012-11-02 21:48:17 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |