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/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"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "block-freq"
28 
29 #ifndef NDEBUG
30 static cl::opt<GVDAGType> ViewBlockFreqPropagationDAG(
31     "view-block-freq-propagation-dags", cl::Hidden,
32     cl::desc("Pop up a window to show a dag displaying how block "
33              "frequencies propagation through the CFG."),
34     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
35                clEnumValN(GVDT_Fraction, "fraction",
36                           "display a graph using the "
37                           "fractional block frequency representation."),
38                clEnumValN(GVDT_Integer, "integer",
39                           "display a graph using the raw "
40                           "integer fractional block frequency representation."),
41                clEnumValN(GVDT_Count, "count", "display a graph using the real "
42                                                "profile count if available."),
43                clEnumValEnd));
44 
45 cl::opt<std::string>
46     ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden,
47                           cl::desc("The option to specify "
48                                    "the name of the function "
49                                    "whose CFG will be displayed."));
50 
51 cl::opt<unsigned>
52     ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden,
53                        cl::desc("An integer in percent used to specify "
54                                 "the hot blocks/edges to be displayed "
55                                 "in red: a block or edge whose frequency "
56                                 "is no less than the max frequency of the "
57                                 "function multiplied by this percent."));
58 
59 namespace llvm {
60 
61 template <>
62 struct GraphTraits<BlockFrequencyInfo *> {
63   typedef const BasicBlock NodeType;
64   typedef succ_const_iterator ChildIteratorType;
65   typedef Function::const_iterator nodes_iterator;
66 
67   static inline const NodeType *getEntryNode(const BlockFrequencyInfo *G) {
68     return &G->getFunction()->front();
69   }
70   static ChildIteratorType child_begin(const NodeType *N) {
71     return succ_begin(N);
72   }
73   static ChildIteratorType child_end(const NodeType *N) {
74     return succ_end(N);
75   }
76   static nodes_iterator nodes_begin(const BlockFrequencyInfo *G) {
77     return G->getFunction()->begin();
78   }
79   static nodes_iterator nodes_end(const BlockFrequencyInfo *G) {
80     return G->getFunction()->end();
81   }
82 };
83 
84 typedef BFIDOTGraphTraitsBase<BlockFrequencyInfo, BranchProbabilityInfo>
85     BFIDOTGTraitsBase;
86 
87 template <>
88 struct DOTGraphTraits<BlockFrequencyInfo *> : public BFIDOTGTraitsBase {
89   explicit DOTGraphTraits(bool isSimple = false)
90       : BFIDOTGTraitsBase(isSimple) {}
91 
92   std::string getNodeLabel(const BasicBlock *Node,
93                            const BlockFrequencyInfo *Graph) {
94 
95     return BFIDOTGTraitsBase::getNodeLabel(Node, Graph,
96                                            ViewBlockFreqPropagationDAG);
97   }
98 
99   std::string getNodeAttributes(const BasicBlock *Node,
100                                 const BlockFrequencyInfo *Graph) {
101     return BFIDOTGTraitsBase::getNodeAttributes(Node, Graph,
102                                                 ViewHotFreqPercent);
103   }
104 
105   std::string getEdgeAttributes(const BasicBlock *Node, EdgeIter EI,
106                                 const BlockFrequencyInfo *BFI) {
107     return BFIDOTGTraitsBase::getEdgeAttributes(Node, EI, BFI, BFI->getBPI(),
108                                                 ViewHotFreqPercent);
109   }
110 };
111 
112 } // end namespace llvm
113 #endif
114 
115 BlockFrequencyInfo::BlockFrequencyInfo() {}
116 
117 BlockFrequencyInfo::BlockFrequencyInfo(const Function &F,
118                                        const BranchProbabilityInfo &BPI,
119                                        const LoopInfo &LI) {
120   calculate(F, BPI, LI);
121 }
122 
123 BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo &&Arg)
124     : BFI(std::move(Arg.BFI)) {}
125 
126 BlockFrequencyInfo &BlockFrequencyInfo::operator=(BlockFrequencyInfo &&RHS) {
127   releaseMemory();
128   BFI = std::move(RHS.BFI);
129   return *this;
130 }
131 
132 // Explicitly define the default constructor otherwise it would be implicitly
133 // defined at the first ODR-use which is the BFI member in the
134 // LazyBlockFrequencyInfo header.  The dtor needs the BlockFrequencyInfoImpl
135 // template instantiated which is not available in the header.
136 BlockFrequencyInfo::~BlockFrequencyInfo() {}
137 
138 void BlockFrequencyInfo::calculate(const Function &F,
139                                    const BranchProbabilityInfo &BPI,
140                                    const LoopInfo &LI) {
141   if (!BFI)
142     BFI.reset(new ImplType);
143   BFI->calculate(F, BPI, LI);
144 #ifndef NDEBUG
145   if (ViewBlockFreqPropagationDAG != GVDT_None &&
146       (ViewBlockFreqFuncName.empty() ||
147        F.getName().equals(ViewBlockFreqFuncName))) {
148     view();
149   }
150 #endif
151 }
152 
153 BlockFrequency BlockFrequencyInfo::getBlockFreq(const BasicBlock *BB) const {
154   return BFI ? BFI->getBlockFreq(BB) : 0;
155 }
156 
157 Optional<uint64_t>
158 BlockFrequencyInfo::getBlockProfileCount(const BasicBlock *BB) const {
159   if (!BFI)
160     return None;
161 
162   return BFI->getBlockProfileCount(*getFunction(), BB);
163 }
164 
165 Optional<uint64_t>
166 BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
167   if (!BFI)
168     return None;
169   return BFI->getProfileCountFromFreq(*getFunction(), Freq);
170 }
171 
172 void BlockFrequencyInfo::setBlockFreq(const BasicBlock *BB, uint64_t Freq) {
173   assert(BFI && "Expected analysis to be available");
174   BFI->setBlockFreq(BB, Freq);
175 }
176 
177 /// Pop up a ghostview window with the current block frequency propagation
178 /// rendered using dot.
179 void BlockFrequencyInfo::view() const {
180 // This code is only for debugging.
181 #ifndef NDEBUG
182   ViewGraph(const_cast<BlockFrequencyInfo *>(this), "BlockFrequencyDAGs");
183 #else
184   errs() << "BlockFrequencyInfo::view is only available in debug builds on "
185             "systems with Graphviz or gv!\n";
186 #endif // NDEBUG
187 }
188 
189 const Function *BlockFrequencyInfo::getFunction() const {
190   return BFI ? BFI->getFunction() : nullptr;
191 }
192 
193 const BranchProbabilityInfo *BlockFrequencyInfo::getBPI() const {
194   return BFI ? &BFI->getBPI() : nullptr;
195 }
196 
197 raw_ostream &BlockFrequencyInfo::
198 printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const {
199   return BFI ? BFI->printBlockFreq(OS, Freq) : OS;
200 }
201 
202 raw_ostream &
203 BlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
204                                    const BasicBlock *BB) const {
205   return BFI ? BFI->printBlockFreq(OS, BB) : OS;
206 }
207 
208 uint64_t BlockFrequencyInfo::getEntryFreq() const {
209   return BFI ? BFI->getEntryFreq() : 0;
210 }
211 
212 void BlockFrequencyInfo::releaseMemory() { BFI.reset(); }
213 
214 void BlockFrequencyInfo::print(raw_ostream &OS) const {
215   if (BFI)
216     BFI->print(OS);
217 }
218 
219 
220 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass, "block-freq",
221                       "Block Frequency Analysis", true, true)
222 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
223 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
224 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass, "block-freq",
225                     "Block Frequency Analysis", true, true)
226 
227 char BlockFrequencyInfoWrapperPass::ID = 0;
228 
229 
230 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
231     : FunctionPass(ID) {
232   initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
233 }
234 
235 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() {}
236 
237 void BlockFrequencyInfoWrapperPass::print(raw_ostream &OS,
238                                           const Module *) const {
239   BFI.print(OS);
240 }
241 
242 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
243   AU.addRequired<BranchProbabilityInfoWrapperPass>();
244   AU.addRequired<LoopInfoWrapperPass>();
245   AU.setPreservesAll();
246 }
247 
248 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI.releaseMemory(); }
249 
250 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function &F) {
251   BranchProbabilityInfo &BPI =
252       getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
253   LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
254   BFI.calculate(F, BPI, LI);
255   return false;
256 }
257 
258 char BlockFrequencyAnalysis::PassID;
259 BlockFrequencyInfo BlockFrequencyAnalysis::run(Function &F,
260                                                AnalysisManager<Function> &AM) {
261   BlockFrequencyInfo BFI;
262   BFI.calculate(F, AM.getResult<BranchProbabilityAnalysis>(F),
263                 AM.getResult<LoopAnalysis>(F));
264   return BFI;
265 }
266 
267 PreservedAnalyses
268 BlockFrequencyPrinterPass::run(Function &F, AnalysisManager<Function> &AM) {
269   OS << "Printing analysis results of BFI for function "
270      << "'" << F.getName() << "':"
271      << "\n";
272   AM.getResult<BlockFrequencyAnalysis>(F).print(OS);
273   return PreservedAnalyses::all();
274 }
275