LLVM  7.0.0svn
CostModel.cpp
Go to the documentation of this file.
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. 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.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Analysis/Passes.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/Pass.h"
26 #include "llvm/Support/Debug.h"
28 using namespace llvm;
29 
31  "cost-kind", cl::desc("Target cost kind"),
34  "throughput", "Reciprocal throughput"),
36  "latency", "Instruction latency"),
38  "code-size", "Code size")));
39 
40 #define CM_NAME "cost-model"
41 #define DEBUG_TYPE CM_NAME
42 
43 namespace {
44  class CostModelAnalysis : public FunctionPass {
45 
46  public:
47  static char ID; // Class identification, replacement for typeinfo
48  CostModelAnalysis() : FunctionPass(ID), F(nullptr), TTI(nullptr) {
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.
57  unsigned getInstructionCost(const Instruction *I) const {
58  return TTI->getInstructionCost(I, TargetTransformInfo::TCK_RecipThroughput);
59  }
60 
61  private:
62  void getAnalysisUsage(AnalysisUsage &AU) const override;
63  bool runOnFunction(Function &F) override;
64  void print(raw_ostream &OS, const Module*) const override;
65 
66  /// The function that we analyze.
67  Function *F;
68  /// Target information.
69  const TargetTransformInfo *TTI;
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 
80  return new CostModelAnalysis();
81 }
82 
83 void
84 CostModelAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
85  AU.setPreservesAll();
86 }
87 
88 bool
90  this->F = &F;
91  auto *TTIWP = getAnalysisIfAvailable<TargetTransformInfoWrapperPass>();
92  TTI = TTIWP ? &TTIWP->getTTI(F) : nullptr;
93 
94  return false;
95 }
96 
97 void CostModelAnalysis::print(raw_ostream &OS, const Module*) const {
98  if (!F)
99  return;
100 
101  for (BasicBlock &B : *F) {
102  for (Instruction &Inst : B) {
103  unsigned Cost = TTI->getInstructionCost(&Inst, CostKind);
104  if (Cost != (unsigned)-1)
105  OS << "Cost Model: Found an estimated cost of " << Cost;
106  else
107  OS << "Cost Model: Unknown cost";
108 
109  OS << " for instruction: " << Inst << "\n";
110  }
111  }
112 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void initializeCostModelAnalysisPass(PassRegistry &)
F(f)
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:75
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:630
#define CM_NAME
Definition: CostModel.cpp:40
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
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:48
void setPreservesAll()
Set by analyses that do not transform their input at all.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:605
Basic Alias true
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionPass * createCostModelAnalysisPass()
Definition: CostModel.cpp:79
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
This pass exposes codegen information to IR-level passes.