1 //===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/BlockFrequencyInfo.h"
15 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16 #include "llvm/Analysis/BranchProbabilityInfo.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/IR/CFG.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/GraphWriter.h"
27 #define DEBUG_TYPE "block-freq"
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."),
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."),
52 struct GraphTraits<BlockFrequencyInfo *> {
53 typedef const BasicBlock NodeType;
54 typedef succ_const_iterator ChildIteratorType;
55 typedef Function::const_iterator nodes_iterator;
57 static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
58 return G->getFunction()->begin();
60 static ChildIteratorType child_begin(const NodeType *N) {
63 static ChildIteratorType child_end(const NodeType *N) {
66 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
67 return G->getFunction()->begin();
69 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
70 return G->getFunction()->end();
75 struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits {
76 explicit DOTGraphTraits(bool isSimple=false) :
77 DefaultDOTGraphTraits(isSimple) {}
79 static std::string getGraphName(const BlockFrequencyInfo *G) {
80 return G->getFunction()->getName();
83 std::string getNodeLabel(const BasicBlock *Node,
84 const BlockFrequencyInfo *Graph) {
86 raw_string_ostream OS(Result);
88 OS << Node->getName() << ":";
89 switch (ViewBlockFreqPropagationDAG) {
91 Graph->printBlockFreq(OS, Node);
94 OS << Graph->getBlockFreq(Node).getFrequency();
97 llvm_unreachable("If we are not supposed to render a graph we should "
98 "never reach this point.");
105 } // end namespace llvm
108 BlockFrequencyInfo::BlockFrequencyInfo() {}
110 BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
111 const BranchProbabilityInfo &BPI,
112 const LoopInfo &LI) {
113 calculate(F, BPI, LI);
116 void BlockFrequencyInfo::calculate(const Function &F,
117 const BranchProbabilityInfo &BPI,
118 const LoopInfo &LI) {
120 BFI.reset(new ImplType);
121 BFI->calculate(F, BPI, LI);
123 if (ViewBlockFreqPropagationDAG != GVDT_None)
128 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
129 return BFI ? BFI->getBlockFreq(BB) : 0;
132 /// Pop up a ghostview window with the current block frequency propagation
133 /// rendered using dot.
134 void BlockFrequencyInfo::view() const {
135 // This code is only for debugging.
137 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
139 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
140 "systems with Graphviz or gv!\n";
144 const Function *BlockFrequencyInfo::getFunction() const {
145 return BFI ? BFI->getFunction() : nullptr;
148 raw_ostream &BlockFrequencyInfo::
149 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
150 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
154 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
155 const BasicBlock *BB) const {
156 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
159 uint64_t BlockFrequencyInfo::getEntryFreq() const {
160 return BFI ? BFI->getEntryFreq() : 0;
163 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
165 void BlockFrequencyInfo::print(raw_ostream &OS) const {
171 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
172 "Block Frequency Analysis", true, true)
173 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
174 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
175 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
176 "Block Frequency Analysis", true, true)
178 char BlockFrequencyInfoWrapperPass::ID = 0;
181 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
183 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
186 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
188 void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
189 const Module *) const {
193 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
194 AU.addRequired<BranchProbabilityInfoWrapperPass>();
195 AU.addRequired<LoopInfoWrapperPass>();
196 AU.setPreservesAll();
199 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
201 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
202 BranchProbabilityInfo &BPI =
203 getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
204 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
205 BFI.calculate(F, BPI, LI);