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/BlockFrequencyImpl.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"
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."),
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."),
50 struct GraphTraits<BlockFrequencyInfo *> {
51 typedef const BasicBlock NodeType;
52 typedef succ_const_iterator ChildIteratorType;
53 typedef Function::const_iterator nodes_iterator;
55 static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
56 return G->getFunction()->begin();
58 static ChildIteratorType child_begin(const NodeType *N) {
61 static ChildIteratorType child_end(const NodeType *N) {
64 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
65 return G->getFunction()->begin();
67 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
68 return G->getFunction()->end();
73 struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits {
74 explicit DOTGraphTraits(bool isSimple=false) :
75 DefaultDOTGraphTraits(isSimple) {}
77 static std::string getGraphName(const BlockFrequencyInfo *G) {
78 return G->getFunction()->getName();
81 std::string getNodeLabel(const BasicBlock *Node,
82 const BlockFrequencyInfo *Graph) {
84 raw_string_ostream OS(Result);
86 OS << Node->getName().str() << ":";
87 switch (ViewBlockFreqPropagationDAG) {
89 Graph->printBlockFreq(OS, Node);
92 OS << Graph->getBlockFreq(Node).getFrequency();
95 llvm_unreachable("If we are not supposed to render a graph we should "
96 "never reach this point.");
103 } // end namespace llvm
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)
112 char BlockFrequencyInfo::ID = 0;
115 BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
116 initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
119 BlockFrequencyInfo::~BlockFrequencyInfo() {}
121 void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
122 AU.addRequired<BranchProbabilityInfo>();
123 AU.setPreservesAll();
126 bool BlockFrequencyInfo::runOnFunction(Function &F) {
127 BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
129 BFI.reset(new ImplType);
130 BFI->doFunction(&F, &BPI);
132 if (ViewBlockFreqPropagationDAG != GVDT_None)
138 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
140 void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
141 if (BFI) BFI->print(O);
144 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
145 return BFI ? BFI->getBlockFreq(BB) : 0;
148 /// Pop up a ghostview window with the current block frequency propagation
149 /// rendered using dot.
150 void BlockFrequencyInfo::view() const {
151 // This code is only for debugging.
153 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
155 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
156 "systems with Graphviz or gv!\n";
160 const Function *BlockFrequencyInfo::getFunction() const {
161 return BFI ? BFI->Fn : nullptr;
164 raw_ostream &BlockFrequencyInfo::
165 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
166 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
170 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
171 const BasicBlock *BB) const {
172 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
175 uint64_t BlockFrequencyInfo::getEntryFreq() const {
176 return BFI ? BFI->getEntryFreq() : 0;