LLVM  10.0.0svn
CostModel.cpp
Go to the documentation of this file.
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"
22 #include "llvm/IR/Function.h"
23 #include "llvm/Pass.h"
25 #include "llvm/Support/Debug.h"
27 using namespace llvm;
28 
30  "cost-kind", cl::desc("Target cost kind"),
33  "throughput", "Reciprocal throughput"),
35  "latency", "Instruction latency"),
37  "code-size", "Code size")));
38 
39 #define CM_NAME "cost-model"
40 #define DEBUG_TYPE CM_NAME
41 
42 namespace {
43  class CostModelAnalysis : public FunctionPass {
44 
45  public:
46  static char ID; // Class identification, replacement for typeinfo
47  CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) {
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.
56  unsigned getInstructionCost(const Instruction *I) const {
57  return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput);
58  }
59 
60  private:
61  void getAnalysisUsage(AnalysisUsage &AU) const override;
62  bool runOnFunction(Function &F) override;
63  void print(raw_ostream &OS, const Module*) const override;
64 
65  /// The function that we analyze.
66  Function *F;
67  /// Target information.
68  const TargetTransformInfo *TTI;
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 
79  return new CostModelAnalysis();
80 }
81 
82 void
83 CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
84  AU.setPreservesAll();
85 }
86 
87 bool
89  this->F = &F;
90  auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
91  TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
92 
93  return false;
94 }
95 
96 void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
97  if (!F)
98  return;
99 
100  for (BasicBlock &B : *F) {
101  for (Instruction &Inst : B) {
102  unsigned Cost = TTI->getInstructionCost(&Inst, CostKind);
103  if (Cost != (unsigned)-1)
104  OS << "Cost Model: Found an estimated cost of " << Cost;
105  else
106  OS << "Cost Model: Unknown cost";
107 
108  OS << " for instruction: " << Inst << "\n";
109  }
110  }
111 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
void initializeCostModelAnalysisPass(PassRegistry &)
F(f)
block Block Frequency true
static cl::opt< TargetTransformInfo::TargetCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(TargetTransformInfo::TCK_RecipThroughput), cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(TargetTransformInfo::TCK_Latency, "latency", "Instruction latency"), clEnumValN(TargetTransformInfo::TCK_CodeSize, "code-size", "Code size")))
static const char cm_name[]
Definition: CostModel.cpp:74
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:652
#define CM_NAME
Definition: CostModel.cpp:39
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:47
void setPreservesAll()
Set by analyses that do not transform their input at all.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:627
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionPass * createCostModelAnalysisPass()
Definition: CostModel.cpp:78
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
This pass exposes codegen information to IR-level passes.