LLVM  3.7.0
BlockFrequencyInfo.cpp
Go to the documentation of this file.
1 //===- BlockFrequencyInfo.cpp - Block Frequency 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 // Loops should be simplified before this analysis.
11 //
12 //===----------------------------------------------------------------------===//
13 
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/IR/CFG.h"
20 #include "llvm/InitializePasses.h"
22 #include "llvm/Support/Debug.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "block-freq"
28 
29 #ifndef NDEBUG
30 enum GVDAGType {
34 };
35 
36 static cl::opt<GVDAGType>
37 ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden,
38  cl::desc("Pop up a window to show a dag displaying how block "
39  "frequencies propagation through the CFG."),
40  cl::values(
41  clEnumValN(GVDT_None, "none",
42  "do not display graphs."),
43  clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
44  "fractional block frequency representation."),
45  clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
46  "integer fractional block frequency representation."),
47  clEnumValEnd));
48 
49 namespace llvm {
50 
51 template <>
53  typedef const BasicBlock NodeType;
56 
57  static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
58  return G->getFunction()->begin();
59  }
60  static ChildIteratorType child_begin(const NodeType *N) {
61  return succ_begin(N);
62  }
63  static ChildIteratorType child_end(const NodeType *N) {
64  return succ_end(N);
65  }
66  static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
67  return G->getFunction()->begin();
68  }
69  static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
70  return G->getFunction()->end();
71  }
72 };
73 
74 template<>
76  explicit DOTGraphTraits(bool isSimple=false) :
77  DefaultDOTGraphTraits(isSimple) {}
78 
79  static std::string getGraphName(const BlockFrequencyInfo *G) {
80  return G->getFunction()->getName();
81  }
82 
83  std::string getNodeLabel(const BasicBlock *Node,
84  const BlockFrequencyInfo *Graph) {
85  std::string Result;
86  raw_string_ostream OS(Result);
87 
88  OS << Node->getName() << ":";
90  case GVDT_Fraction:
91  Graph->printBlockFreq(OS, Node);
92  break;
93  case GVDT_Integer:
94  OS << Graph->getBlockFreq(Node).getFrequency();
95  break;
96  case GVDT_None:
97  llvm_unreachable("If we are not supposed to render a graph we should "
98  "never reach this point.");
99  }
100 
101  return Result;
102  }
103 };
104 
105 } // end namespace llvm
106 #endif
107 
109  "Block Frequency Analysis", true, true)
113  "Block Frequency Analysis", true, true)
114 
115 char BlockFrequencyInfo::ID = 0;
116 
117 
118 BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
120 }
121 
123 
127  AU.setPreservesAll();
128 }
129 
131  BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
132  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
133  if (!BFI)
134  BFI.reset(new ImplType);
135  BFI->doFunction(&F, &BPI, &LI);
136 #ifndef NDEBUG
138  view();
139 #endif
140  return false;
141 }
142 
144 
146  if (BFI) BFI->print(O);
147 }
148 
150  return BFI ? BFI->getBlockFreq(BB) : 0;
151 }
152 
153 /// Pop up a ghostview window with the current block frequency propagation
154 /// rendered using dot.
156 // This code is only for debugging.
157 #ifndef NDEBUG
158  ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
159 #else
160  errs() << "BlockFrequencyInfo::view is only available in debug builds on "
161  "systems with Graphviz or gv!\n";
162 #endif // NDEBUG
163 }
164 
166  return BFI ? BFI->getFunction() : nullptr;
167 }
168 
170 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
171  return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
172 }
173 
174 raw_ostream &
176  const BasicBlock *BB) const {
177  return BFI ? BFI->printBlockFreq(OS, BB) : OS;
178 }
179 
181  return BFI ? BFI->getEntryFreq() : 0;
182 }
ValuesClass< DataType > LLVM_END_WITH_NULL values(const char *Arg, DataType Val, const char *Desc,...)
Definition: CommandLine.h:536
block Block Frequency true
std::string getNodeLabel(const BasicBlock *Node, const BlockFrequencyInfo *Graph)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
raw_ostream & printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: GraphTraits.h:27
iterator end()
Definition: Function.h:459
#define clEnumValEnd
Definition: CommandLine.h:498
F(f)
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
void print(raw_ostream &O, const Module *M) const override
print - Print out the internal state of the pass.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
static ChildIteratorType child_begin(const NodeType *N)
#define G(x, y, z)
Definition: MD5.cpp:52
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:104
static nodes_iterator nodes_begin(const BlockFrequencyInfo *G)
iterator begin()
Definition: Function.h:457
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
block freq
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
Definition: GraphWriter.h:348
static cl::opt< GVDAGType > ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden, cl::desc("Pop up a window to show a dag displaying how block ""frequencies propagation through the CFG."), cl::values(clEnumValN(GVDT_None,"none","do not display graphs."), clEnumValN(GVDT_Fraction,"fraction","display a graph using the ""fractional block frequency representation."), clEnumValN(GVDT_Integer,"integer","display a graph using the raw ""integer fractional block frequency representation."), clEnumValEnd))
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void initializeBlockFrequencyInfoPass(PassRegistry &)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
static ChildIteratorType child_end(const NodeType *N)
void setPreservesAll()
Set by analyses that do not transform their input at all.
INITIALIZE_PASS_BEGIN(BlockFrequencyInfo,"block-freq","Block Frequency Analysis", true, true) INITIALIZE_PASS_END(BlockFrequencyInfo
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:497
Analysis pass providing branch probability information.
block Block Frequency Analysis
const Function * getFunction() const
#define N
void view() const
Pop up a ghostview window with the current block frequency propagation rendered using dot...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:737
static const NodeType * getEntryNode(const BlockFrequencyInfo *G)
Shared implementation for block frequency analysis.
static std::string getGraphName(const BlockFrequencyInfo *G)
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
static nodes_iterator nodes_end(const BlockFrequencyInfo *G)