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
14 #include "llvm/Analysis/BlockFrequencyInfo.h"
15 #include "llvm/Analysis/BlockFrequencyImpl.h"
16 #include "llvm/Analysis/BranchProbabilityInfo.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/InitializePasses.h"
20 #include "llvm/Support/CFG.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/GraphWriter.h"
24
25 using namespace llvm;
26
27 #ifndef NDEBUG
28 enum GVDAGType {
29 GVDT_None,
30 GVDT_Fraction,
31 GVDT_Integer
32 };
33
34 static cl::opt<GVDAGType>
35 ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden,
36 cl::desc("Pop up a window to show a dag displaying how block "
37 "frequencies propagation through the CFG."),
38 cl::values(
39 clEnumValN(GVDT_None, "none",
40 "do not display graphs."),
41 clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
42 "fractional block frequency representation."),
43 clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
44 "integer fractional block frequency representation."),
45 clEnumValEnd));
46
47 namespace llvm {
48
49 template <>
50 struct GraphTraits<BlockFrequencyInfo *> {
51 typedef const BasicBlock NodeType;
52 typedef succ_const_iterator ChildIteratorType;
53 typedef Function::const_iterator nodes_iterator;
54
getEntryNodellvm::GraphTraits55 static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
56 return G->getFunction()->begin();
57 }
child_beginllvm::GraphTraits58 static ChildIteratorType child_begin(const NodeType *N) {
59 return succ_begin(N);
60 }
child_endllvm::GraphTraits61 static ChildIteratorType child_end(const NodeType *N) {
62 return succ_end(N);
63 }
nodes_beginllvm::GraphTraits64 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
65 return G->getFunction()->begin();
66 }
nodes_endllvm::GraphTraits67 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
68 return G->getFunction()->end();
69 }
70 };
71
72 template<>
73 struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits74 explicit DOTGraphTraits(bool isSimple=false) :
75 DefaultDOTGraphTraits(isSimple) {}
76
getGraphNamellvm::DOTGraphTraits77 static std::string getGraphName(const BlockFrequencyInfo *G) {
78 return G->getFunction()->getName();
79 }
80
getNodeLabelllvm::DOTGraphTraits81 std::string getNodeLabel(const BasicBlock *Node,
82 const BlockFrequencyInfo *Graph) {
83 std::string Result;
84 raw_string_ostream OS(Result);
85
86 OS << Node->getName().str() << ":";
87 switch (ViewBlockFreqPropagationDAG) {
88 case GVDT_Fraction:
89 Graph->getBlockFreq(Node).print(OS);
90 break;
91 case GVDT_Integer:
92 OS << Graph->getBlockFreq(Node).getFrequency();
93 break;
94 case GVDT_None:
95 llvm_unreachable("If we are not supposed to render a graph we should "
96 "never reach this point.");
97 }
98
99 return Result;
100 }
101 };
102
103 } // end namespace llvm
104 #endif
105
106 INITIALIZE_PASS_BEGIN(BlockFrequencyInfo, "block-freq",
107 "Block Frequency Analysis", true, true)
108 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo)
109 INITIALIZE_PASS_END(BlockFrequencyInfo, "block-freq",
110 "Block Frequency Analysis", true, true)
111
112 char BlockFrequencyInfo::ID = 0;
113
114
BlockFrequencyInfo()115 BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
116 initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
117 BFI = new BlockFrequencyImpl<BasicBlock, Function, BranchProbabilityInfo>();
118 }
119
~BlockFrequencyInfo()120 BlockFrequencyInfo::~BlockFrequencyInfo() {
121 delete BFI;
122 }
123
getAnalysisUsage(AnalysisUsage & AU) const124 void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
125 AU.addRequired<BranchProbabilityInfo>();
126 AU.setPreservesAll();
127 }
128
runOnFunction(Function & F)129 bool BlockFrequencyInfo::runOnFunction(Function &F) {
130 BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
131 BFI->doFunction(&F, &BPI);
132 #ifndef NDEBUG
133 if (ViewBlockFreqPropagationDAG != GVDT_None)
134 view();
135 #endif
136 return false;
137 }
138
print(raw_ostream & O,const Module *) const139 void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
140 if (BFI) BFI->print(O);
141 }
142
getBlockFreq(const BasicBlock * BB) const143 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
144 return BFI->getBlockFreq(BB);
145 }
146
147 /// Pop up a ghostview window with the current block frequency propagation
148 /// rendered using dot.
view() const149 void BlockFrequencyInfo::view() const {
150 // This code is only for debugging.
151 #ifndef NDEBUG
152 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
153 #else
154 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
155 "systems with Graphviz or gv!\n";
156 #endif // NDEBUG
157 }
158
getFunction() const159 const Function *BlockFrequencyInfo::getFunction() const {
160 return BFI->Fn;
161 }
162