1689a5073SDuncan P. N. Exon Smith //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
2875ebd5fSJakub Staszak //
3875ebd5fSJakub Staszak //                     The LLVM Compiler Infrastructure
4875ebd5fSJakub Staszak //
5875ebd5fSJakub Staszak // This file is distributed under the University of Illinois Open Source
6875ebd5fSJakub Staszak // License. See LICENSE.TXT for details.
7875ebd5fSJakub Staszak //
8875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
9875ebd5fSJakub Staszak //
10875ebd5fSJakub Staszak // Loops should be simplified before this analysis.
11875ebd5fSJakub Staszak //
12875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
13875ebd5fSJakub Staszak 
14875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
15689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
16875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
1710be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineFunction.h"
1810be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineLoopInfo.h"
19ed0881b2SChandler Carruth #include "llvm/CodeGen/Passes.h"
20ed0881b2SChandler Carruth #include "llvm/InitializePasses.h"
2165bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h"
2265bbcdfaSMichael Gottesman #include "llvm/Support/Debug.h"
2369317f2eSXinliang David Li #include "llvm/Support/Format.h"
2465bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h"
2569317f2eSXinliang David Li #include "llvm/Support/raw_ostream.h"
26875ebd5fSJakub Staszak 
27875ebd5fSJakub Staszak using namespace llvm;
28875ebd5fSJakub Staszak 
291b9dde08SChandler Carruth #define DEBUG_TYPE "block-freq"
301b9dde08SChandler Carruth 
3165bbcdfaSMichael Gottesman #ifndef NDEBUG
3230c50f3cSXinliang David Li enum GVDAGType { GVDT_None, GVDT_Fraction, GVDT_Integer, GVDT_Count };
3365bbcdfaSMichael Gottesman 
34bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
35bc157084SXinliang David Li     "view-machine-block-freq-propagation-dags", cl::Hidden,
3665bbcdfaSMichael Gottesman     cl::desc("Pop up a window to show a dag displaying how machine block "
37748fe483SMichael Gottesman              "frequencies propagate through the CFG."),
38bc157084SXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
39bc157084SXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
40bc157084SXinliang David Li                           "display a graph using the "
4165bbcdfaSMichael Gottesman                           "fractional block frequency representation."),
42bc157084SXinliang David Li                clEnumValN(GVDT_Integer, "integer",
43bc157084SXinliang David Li                           "display a graph using the raw "
4465bbcdfaSMichael Gottesman                           "integer fractional block frequency representation."),
4530c50f3cSXinliang David Li                clEnumValN(GVDT_Count, "count", "display a graph using the real "
4630c50f3cSXinliang David Li                                                "profile count if available."),
4730c50f3cSXinliang David Li 
4865bbcdfaSMichael Gottesman                clEnumValEnd));
4965bbcdfaSMichael Gottesman 
5080457ce5SXinliang David Li static cl::opt<std::string> ViewMachineBlockFreqFuncName("view-mbfi-func-name",
5180457ce5SXinliang David Li                                                          cl::Hidden);
5280457ce5SXinliang David Li 
5365bbcdfaSMichael Gottesman namespace llvm {
5465bbcdfaSMichael Gottesman 
55bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
5665bbcdfaSMichael Gottesman   typedef const MachineBasicBlock NodeType;
5765bbcdfaSMichael Gottesman   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
5865bbcdfaSMichael Gottesman   typedef MachineFunction::const_iterator nodes_iterator;
5965bbcdfaSMichael Gottesman 
60bc157084SXinliang David Li   static inline const NodeType *
61bc157084SXinliang David Li   getEntryNode(const MachineBlockFrequencyInfo *G) {
620ac8eb91SDuncan P. N. Exon Smith     return &G->getFunction()->front();
6365bbcdfaSMichael Gottesman   }
64748fe483SMichael Gottesman 
6565bbcdfaSMichael Gottesman   static ChildIteratorType child_begin(const NodeType *N) {
6665bbcdfaSMichael Gottesman     return N->succ_begin();
6765bbcdfaSMichael Gottesman   }
68748fe483SMichael Gottesman 
6965bbcdfaSMichael Gottesman   static ChildIteratorType child_end(const NodeType *N) {
7065bbcdfaSMichael Gottesman     return N->succ_end();
7165bbcdfaSMichael Gottesman   }
72748fe483SMichael Gottesman 
7365bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
7465bbcdfaSMichael Gottesman     return G->getFunction()->begin();
7565bbcdfaSMichael Gottesman   }
76748fe483SMichael Gottesman 
7765bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
7865bbcdfaSMichael Gottesman     return G->getFunction()->end();
7965bbcdfaSMichael Gottesman   }
8065bbcdfaSMichael Gottesman };
8165bbcdfaSMichael Gottesman 
8265bbcdfaSMichael Gottesman template <>
83bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *>
84bc157084SXinliang David Li     : public DefaultDOTGraphTraits {
85bc157084SXinliang David Li   explicit DOTGraphTraits(bool isSimple = false)
86bc157084SXinliang David Li       : DefaultDOTGraphTraits(isSimple) {}
8765bbcdfaSMichael Gottesman 
8869317f2eSXinliang David Li   typedef MachineBasicBlock::const_succ_iterator EdgeIter;
8965bbcdfaSMichael Gottesman   static std::string getGraphName(const MachineBlockFrequencyInfo *G) {
9065bbcdfaSMichael Gottesman     return G->getFunction()->getName();
9165bbcdfaSMichael Gottesman   }
9265bbcdfaSMichael Gottesman 
9365bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
9465bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
95e69170a1SAlp Toker     std::string Result;
96e69170a1SAlp Toker     raw_string_ostream OS(Result);
9765bbcdfaSMichael Gottesman 
98e69170a1SAlp Toker     OS << Node->getName().str() << " : ";
9965bbcdfaSMichael Gottesman     switch (ViewMachineBlockFreqPropagationDAG) {
10065bbcdfaSMichael Gottesman     case GVDT_Fraction:
101b0c1ed8fSMichael Gottesman       Graph->printBlockFreq(OS, Node);
10265bbcdfaSMichael Gottesman       break;
10365bbcdfaSMichael Gottesman     case GVDT_Integer:
10465bbcdfaSMichael Gottesman       OS << Graph->getBlockFreq(Node).getFrequency();
10565bbcdfaSMichael Gottesman       break;
10630c50f3cSXinliang David Li     case GVDT_Count: {
10730c50f3cSXinliang David Li       auto Count = Graph->getBlockProfileCount(Node);
10830c50f3cSXinliang David Li       if (Count)
10930c50f3cSXinliang David Li         OS << Count.getValue();
11030c50f3cSXinliang David Li       else
11130c50f3cSXinliang David Li         OS << "Unknown";
11230c50f3cSXinliang David Li       break;
11330c50f3cSXinliang David Li     }
11465bbcdfaSMichael Gottesman     case GVDT_None:
11565bbcdfaSMichael Gottesman       llvm_unreachable("If we are not supposed to render a graph we should "
11665bbcdfaSMichael Gottesman                        "never reach this point.");
11765bbcdfaSMichael Gottesman     }
118e69170a1SAlp Toker     return Result;
11965bbcdfaSMichael Gottesman   }
12069317f2eSXinliang David Li   static std::string getEdgeAttributes(const MachineBasicBlock *Node,
12169317f2eSXinliang David Li                                        EdgeIter EI,
12269317f2eSXinliang David Li                                        const MachineBlockFrequencyInfo *MBFI) {
123*3264fdd3SXinliang David Li     std::string Str;
124*3264fdd3SXinliang David Li     const MachineBranchProbabilityInfo *MBPI = MBFI->getMBPI();
125*3264fdd3SXinliang David Li     if (!MBPI)
126*3264fdd3SXinliang David Li       return Str;
127*3264fdd3SXinliang David Li     BranchProbability BP = MBPI->getEdgeProbability(Node, EI);
12869317f2eSXinliang David Li     uint32_t N = BP.getNumerator();
12969317f2eSXinliang David Li     uint32_t D = BP.getDenominator();
13069317f2eSXinliang David Li     double Percent = 100.0 * N / D;
13169317f2eSXinliang David Li     raw_string_ostream OS(Str);
13269317f2eSXinliang David Li     OS << format("label=\"%.1f%%\"", Percent);
13369317f2eSXinliang David Li     OS.flush();
13469317f2eSXinliang David Li     return Str;
13569317f2eSXinliang David Li   }
13665bbcdfaSMichael Gottesman };
13765bbcdfaSMichael Gottesman 
13865bbcdfaSMichael Gottesman } // end namespace llvm
13965bbcdfaSMichael Gottesman #endif
14065bbcdfaSMichael Gottesman 
141875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
142875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
143875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
14410be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
145875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
146875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
147875ebd5fSJakub Staszak 
148875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
149875ebd5fSJakub Staszak 
150bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
151bc157084SXinliang David Li     : MachineFunctionPass(ID) {
152875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
153875ebd5fSJakub Staszak }
154875ebd5fSJakub Staszak 
1553dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
156875ebd5fSJakub Staszak 
157875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
158875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
15910be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
160875ebd5fSJakub Staszak   AU.setPreservesAll();
161875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
162875ebd5fSJakub Staszak }
163875ebd5fSJakub Staszak 
164875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
165748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
166748fe483SMichael Gottesman       getAnalysis<MachineBranchProbabilityInfo>();
16710be9a88SDuncan P. N. Exon Smith   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
1683dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1693dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
1705e67b666SCong Hou   MBFI->calculate(F, MBPI, MLI);
17165bbcdfaSMichael Gottesman #ifndef NDEBUG
17280457ce5SXinliang David Li   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
17380457ce5SXinliang David Li       (ViewMachineBlockFreqFuncName.empty() ||
17480457ce5SXinliang David Li        F.getName().equals(ViewMachineBlockFreqFuncName))) {
17565bbcdfaSMichael Gottesman     view();
17665bbcdfaSMichael Gottesman   }
17765bbcdfaSMichael Gottesman #endif
178875ebd5fSJakub Staszak   return false;
179875ebd5fSJakub Staszak }
180875ebd5fSJakub Staszak 
1813dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1823dbe1050SDuncan P. N. Exon Smith 
18365bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
18465bbcdfaSMichael Gottesman /// rendered using dot.
18565bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const {
18665bbcdfaSMichael Gottesman // This code is only for debugging.
18765bbcdfaSMichael Gottesman #ifndef NDEBUG
18865bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
18965bbcdfaSMichael Gottesman             "MachineBlockFrequencyDAGs");
19065bbcdfaSMichael Gottesman #else
191748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
192748fe483SMichael Gottesman             "on systems with Graphviz or gv!\n";
19365bbcdfaSMichael Gottesman #endif // NDEBUG
19465bbcdfaSMichael Gottesman }
19565bbcdfaSMichael Gottesman 
196bc157084SXinliang David Li BlockFrequency
197bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
1983dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
199875ebd5fSJakub Staszak }
20065bbcdfaSMichael Gottesman 
20130c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
20230c50f3cSXinliang David Li     const MachineBasicBlock *MBB) const {
20330c50f3cSXinliang David Li   const Function *F = MBFI->getFunction()->getFunction();
20430c50f3cSXinliang David Li   return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
20530c50f3cSXinliang David Li }
20630c50f3cSXinliang David Li 
207936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
20810be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
20965bbcdfaSMichael Gottesman }
210fd5c4b2cSMichael Gottesman 
211*3264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
212*3264fdd3SXinliang David Li   return MBFI ? &MBFI->getBPI() : nullptr;
213*3264fdd3SXinliang David Li }
214*3264fdd3SXinliang David Li 
215fd5c4b2cSMichael Gottesman raw_ostream &
216fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
217fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
2183dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
219fd5c4b2cSMichael Gottesman }
220fd5c4b2cSMichael Gottesman 
221fd5c4b2cSMichael Gottesman raw_ostream &
222fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
223fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
2243dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
225fd5c4b2cSMichael Gottesman }
226fd5c4b2cSMichael Gottesman 
2275e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
2283dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
229fd5c4b2cSMichael Gottesman }
230