1 //===- MachineBlockFrequencyInfo.cpp - MBB 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/CodeGen/MachineBlockFrequencyInfo.h"
15 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineLoopInfo.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/GraphWriter.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "block-freq"
30 
31 #ifndef NDEBUG
32 
33 static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
34     "view-machine-block-freq-propagation-dags", cl::Hidden,
35     cl::desc("Pop up a window to show a dag displaying how machine block "
36              "frequencies propagate through the CFG."),
37     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
38                clEnumValN(GVDT_Fraction, "fraction",
39                           "display a graph using the "
40                           "fractional block frequency representation."),
41                clEnumValN(GVDT_Integer, "integer",
42                           "display a graph using the raw "
43                           "integer fractional block frequency representation."),
44                clEnumValN(GVDT_Count, "count", "display a graph using the real "
45                                                "profile count if available."),
46 
47                clEnumValEnd));
48 
49 extern cl::opt<std::string> ViewBlockFreqFuncName;
50 extern cl::opt<unsigned> ViewHotFreqPercent;
51 
52 namespace llvm {
53 
54 template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
55   typedef const MachineBasicBlock NodeType;
56   typedef const MachineBasicBlock *NodeRef;
57   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
58   typedef MachineFunction::const_iterator nodes_iterator;
59 
60   static inline const NodeType *
61   getEntryNode(const MachineBlockFrequencyInfo *G) {
62     return &G->getFunction()->front();
63   }
64 
65   static ChildIteratorType child_begin(const NodeType *N) {
66     return N->succ_begin();
67   }
68 
69   static ChildIteratorType child_end(const NodeType *N) {
70     return N->succ_end();
71   }
72 
73   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
74     return G->getFunction()->begin();
75   }
76 
77   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
78     return G->getFunction()->end();
79   }
80 };
81 
82 typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
83                               MachineBranchProbabilityInfo>
84     MBFIDOTGraphTraitsBase;
85 template <>
86 struct DOTGraphTraits<MachineBlockFrequencyInfo *>
87     : public MBFIDOTGraphTraitsBase {
88   explicit DOTGraphTraits(bool isSimple = false)
89       : MBFIDOTGraphTraitsBase(isSimple) {}
90 
91   std::string getNodeLabel(const MachineBasicBlock *Node,
92                            const MachineBlockFrequencyInfo *Graph) {
93     return MBFIDOTGraphTraitsBase::getNodeLabel(
94         Node, Graph, ViewMachineBlockFreqPropagationDAG);
95   }
96 
97   std::string getNodeAttributes(const MachineBasicBlock *Node,
98                                 const MachineBlockFrequencyInfo *Graph) {
99     return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
100                                                      ViewHotFreqPercent);
101   }
102 
103   std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
104                                 const MachineBlockFrequencyInfo *MBFI) {
105     return MBFIDOTGraphTraitsBase::getEdgeAttributes(
106         Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
107   }
108 };
109 
110 } // end namespace llvm
111 #endif
112 
113 INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
114                       "Machine Block Frequency Analysis", true, true)
115 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
116 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
117 INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
118                     "Machine Block Frequency Analysis", true, true)
119 
120 char MachineBlockFrequencyInfo::ID = 0;
121 
122 MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
123     : MachineFunctionPass(ID) {
124   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
125 }
126 
127 MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
128 
129 void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
130   AU.addRequired<MachineBranchProbabilityInfo>();
131   AU.addRequired<MachineLoopInfo>();
132   AU.setPreservesAll();
133   MachineFunctionPass::getAnalysisUsage(AU);
134 }
135 
136 bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
137   MachineBranchProbabilityInfo &MBPI =
138       getAnalysis<MachineBranchProbabilityInfo>();
139   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
140   if (!MBFI)
141     MBFI.reset(new ImplType);
142   MBFI->calculate(F, MBPI, MLI);
143 #ifndef NDEBUG
144   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
145       (ViewBlockFreqFuncName.empty() ||
146        F.getName().equals(ViewBlockFreqFuncName))) {
147     view();
148   }
149 #endif
150   return false;
151 }
152 
153 void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
154 
155 /// Pop up a ghostview window with the current block frequency propagation
156 /// rendered using dot.
157 void MachineBlockFrequencyInfo::view() const {
158 // This code is only for debugging.
159 #ifndef NDEBUG
160   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
161             "MachineBlockFrequencyDAGs");
162 #else
163   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
164             "on systems with Graphviz or gv!\n";
165 #endif // NDEBUG
166 }
167 
168 BlockFrequency
169 MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
170   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
171 }
172 
173 Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
174     const MachineBasicBlock *MBB) const {
175   const Function *F = MBFI->getFunction()->getFunction();
176   return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
177 }
178 
179 Optional<uint64_t>
180 MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
181   const Function *F = MBFI->getFunction()->getFunction();
182   return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
183 }
184 
185 const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
186   return MBFI ? MBFI->getFunction() : nullptr;
187 }
188 
189 const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
190   return MBFI ? &MBFI->getBPI() : nullptr;
191 }
192 
193 raw_ostream &
194 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
195                                           const BlockFrequency Freq) const {
196   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
197 }
198 
199 raw_ostream &
200 MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
201                                           const MachineBasicBlock *MBB) const {
202   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
203 }
204 
205 uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
206   return MBFI ? MBFI->getEntryFreq() : 0;
207 }
208