blob: 13ab29a94d3f294034c6f9e9b7aff0b8a03fc218 [file] [log] [blame]
Duncan P. N. Exon Smith689a5072014-04-11 23:20:58 +00001//===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
Jakub Staszak668c6fa2011-06-23 21:56:59 +00002//
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
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +000014#define DEBUG_TYPE "block-freq"
Jakub Staszak875ebd52011-07-25 19:25:40 +000015#include "llvm/Analysis/BlockFrequencyInfo.h"
Duncan P. N. Exon Smith689a5072014-04-11 23:20:58 +000016#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Analysis/BranchProbabilityInfo.h"
Jakub Staszak668c6fa2011-06-23 21:56:59 +000018#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/Passes.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000020#include "llvm/IR/CFG.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/InitializePasses.h"
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000022#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/GraphWriter.h"
Jakub Staszak668c6fa2011-06-23 21:56:59 +000025
26using namespace llvm;
27
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000028#ifndef NDEBUG
29enum GVDAGType {
30 GVDT_None,
31 GVDT_Fraction,
32 GVDT_Integer
33};
34
35static cl::opt<GVDAGType>
36ViewBlockFreqPropagationDAG("view-block-freq-propagation-dags", cl::Hidden,
37 cl::desc("Pop up a window to show a dag displaying how block "
38 "frequencies propagation through the CFG."),
39 cl::values(
40 clEnumValN(GVDT_None, "none",
41 "do not display graphs."),
42 clEnumValN(GVDT_Fraction, "fraction", "display a graph using the "
43 "fractional block frequency representation."),
44 clEnumValN(GVDT_Integer, "integer", "display a graph using the raw "
45 "integer fractional block frequency representation."),
46 clEnumValEnd));
47
48namespace llvm {
49
50template <>
51struct GraphTraits<BlockFrequencyInfo *> {
52 typedef const BasicBlock NodeType;
53 typedef succ_const_iterator ChildIteratorType;
54 typedef Function::const_iterator nodes_iterator;
55
56 static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
57 return G->getFunction()->begin();
58 }
59 static ChildIteratorType child_begin(const NodeType *N) {
60 return succ_begin(N);
61 }
62 static ChildIteratorType child_end(const NodeType *N) {
63 return succ_end(N);
64 }
65 static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
66 return G->getFunction()->begin();
67 }
68 static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
69 return G->getFunction()->end();
70 }
71};
72
73template<>
74struct DOTGraphTraits<BlockFrequencyInfo*> : public DefaultDOTGraphTraits {
75 explicit DOTGraphTraits(bool isSimple=false) :
76 DefaultDOTGraphTraits(isSimple) {}
77
78 static std::string getGraphName(const BlockFrequencyInfo *G) {
79 return G->getFunction()->getName();
80 }
81
82 std::string getNodeLabel(const BasicBlock *Node,
83 const BlockFrequencyInfo *Graph) {
84 std::string Result;
85 raw_string_ostream OS(Result);
86
87 OS << Node->getName().str() << ":";
88 switch (ViewBlockFreqPropagationDAG) {
89 case GVDT_Fraction:
Michael Gottesmanb0c1ed82013-12-14 00:25:42 +000090 Graph->printBlockFreq(OS, Node);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +000091 break;
92 case GVDT_Integer:
93 OS << Graph->getBlockFreq(Node).getFrequency();
94 break;
95 case GVDT_None:
96 llvm_unreachable("If we are not supposed to render a graph we should "
97 "never reach this point.");
98 }
99
100 return Result;
101 }
102};
103
104} // end namespace llvm
105#endif
106
Michael Gottesman857d6e32013-11-14 00:05:07 +0000107INITIALIZE_PASS_BEGIN(BlockFrequencyInfo, "block-freq",
108 "Block Frequency Analysis", true, true)
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000109INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfo)
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000110INITIALIZE_PASS_DEPENDENCY(LoopInfo)
Michael Gottesman857d6e32013-11-14 00:05:07 +0000111INITIALIZE_PASS_END(BlockFrequencyInfo, "block-freq",
112 "Block Frequency Analysis", true, true)
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000113
Jakub Staszak875ebd52011-07-25 19:25:40 +0000114char BlockFrequencyInfo::ID = 0;
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000115
116
Jakub Staszak875ebd52011-07-25 19:25:40 +0000117BlockFrequencyInfo::BlockFrequencyInfo() : FunctionPass(ID) {
118 initializeBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000119}
120
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000121BlockFrequencyInfo::~BlockFrequencyInfo() {}
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000122
Jakub Staszak875ebd52011-07-25 19:25:40 +0000123void BlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000124 AU.addRequired<BranchProbabilityInfo>();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000125 AU.addRequired<LoopInfo>();
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000126 AU.setPreservesAll();
127}
128
Jakub Staszak875ebd52011-07-25 19:25:40 +0000129bool BlockFrequencyInfo::runOnFunction(Function &F) {
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000130 BranchProbabilityInfo &BPI = getAnalysis<BranchProbabilityInfo>();
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000131 LoopInfo &LI = getAnalysis<LoopInfo>();
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000132 if (!BFI)
133 BFI.reset(new ImplType);
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000134 BFI->doFunction(&F, &BPI, &LI);
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000135#ifndef NDEBUG
136 if (ViewBlockFreqPropagationDAG != GVDT_None)
137 view();
138#endif
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000139 return false;
140}
141
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000142void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
143
Chandler Carruth343fad42011-10-19 10:12:41 +0000144void BlockFrequencyInfo::print(raw_ostream &O, const Module *) const {
145 if (BFI) BFI->print(O);
146}
147
Jakub Staszak96f8c552011-12-20 20:03:10 +0000148BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000149 return BFI ? BFI->getBlockFreq(BB) : 0;
Jakub Staszak668c6fa2011-06-23 21:56:59 +0000150}
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000151
152/// Pop up a ghostview window with the current block frequency propagation
153/// rendered using dot.
154void BlockFrequencyInfo::view() const {
155// This code is only for debugging.
156#ifndef NDEBUG
157 ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
158#else
159 errs() << "BlockFrequencyInfo::view is only available in debug builds on "
160 "systems with Graphviz or gv!\n";
161#endif // NDEBUG
162}
163
164const Function *BlockFrequencyInfo::getFunction() const {
Duncan P. N. Exon Smith10be9a82014-04-21 17:57:07 +0000165 return BFI ? BFI->getFunction() : nullptr;
Michael Gottesmanfd8aee72013-11-14 02:27:46 +0000166}
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000167
168raw_ostream &BlockFrequencyInfo::
169printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000170 return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000171}
172
173raw_ostream &
174BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
175 const BasicBlock *BB) const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000176 return BFI ? BFI->printBlockFreq(OS, BB) : OS;
Michael Gottesmanfd5c4b22013-12-14 00:06:03 +0000177}
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000178
179uint64_t BlockFrequencyInfo::getEntryFreq() const {
Duncan P. N. Exon Smith3dbe1052014-03-25 18:01:38 +0000180 return BFI ? BFI->getEntryFreq() : 0;
Yuchen Wu5947c8f2013-12-20 22:11:11 +0000181}