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
3265bbcdfaSMichael Gottesman 
33bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
34bc157084SXinliang David Li     "view-machine-block-freq-propagation-dags", cl::Hidden,
3565bbcdfaSMichael Gottesman     cl::desc("Pop up a window to show a dag displaying how machine block "
36748fe483SMichael Gottesman              "frequencies propagate through the CFG."),
37bc157084SXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
38bc157084SXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
39bc157084SXinliang David Li                           "display a graph using the "
4065bbcdfaSMichael Gottesman                           "fractional block frequency representation."),
41bc157084SXinliang David Li                clEnumValN(GVDT_Integer, "integer",
42bc157084SXinliang David Li                           "display a graph using the raw "
4365bbcdfaSMichael Gottesman                           "integer fractional block frequency representation."),
4430c50f3cSXinliang David Li                clEnumValN(GVDT_Count, "count", "display a graph using the real "
45*732afdd0SMehdi Amini                                                "profile count if available.")));
4665bbcdfaSMichael Gottesman 
478dd5ce97SXinliang David Li extern cl::opt<std::string> ViewBlockFreqFuncName;
482d531580SSimon Pilgrim extern cl::opt<unsigned> ViewHotFreqPercent;
4980457ce5SXinliang David Li 
5065bbcdfaSMichael Gottesman namespace llvm {
5165bbcdfaSMichael Gottesman 
52bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
53eb3958faSTim Shen   typedef const MachineBasicBlock *NodeRef;
5465bbcdfaSMichael Gottesman   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
55b5e0f5acSTim Shen   typedef pointer_iterator<MachineFunction::const_iterator> nodes_iterator;
5665bbcdfaSMichael Gottesman 
5748f814e8STim Shen   static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
580ac8eb91SDuncan P. N. Exon Smith     return &G->getFunction()->front();
5965bbcdfaSMichael Gottesman   }
60748fe483SMichael Gottesman 
61f2187ed3STim Shen   static ChildIteratorType child_begin(const NodeRef N) {
6265bbcdfaSMichael Gottesman     return N->succ_begin();
6365bbcdfaSMichael Gottesman   }
64748fe483SMichael Gottesman 
65f2187ed3STim Shen   static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
66748fe483SMichael Gottesman 
6765bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
68b5e0f5acSTim Shen     return nodes_iterator(G->getFunction()->begin());
6965bbcdfaSMichael Gottesman   }
70748fe483SMichael Gottesman 
7165bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
72b5e0f5acSTim Shen     return nodes_iterator(G->getFunction()->end());
7365bbcdfaSMichael Gottesman   }
7465bbcdfaSMichael Gottesman };
7565bbcdfaSMichael Gottesman 
7655415f25SXinliang David Li typedef BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
7755415f25SXinliang David Li                               MachineBranchProbabilityInfo>
7855415f25SXinliang David Li     MBFIDOTGraphTraitsBase;
7965bbcdfaSMichael Gottesman template <>
80bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *>
8155415f25SXinliang David Li     : public MBFIDOTGraphTraitsBase {
82bc157084SXinliang David Li   explicit DOTGraphTraits(bool isSimple = false)
8355415f25SXinliang David Li       : MBFIDOTGraphTraitsBase(isSimple) {}
8465bbcdfaSMichael Gottesman 
8565bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
8665bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
8755415f25SXinliang David Li     return MBFIDOTGraphTraitsBase::getNodeLabel(
8855415f25SXinliang David Li         Node, Graph, ViewMachineBlockFreqPropagationDAG);
8955415f25SXinliang David Li   }
9065bbcdfaSMichael Gottesman 
913e176c77SXinliang David Li   std::string getNodeAttributes(const MachineBasicBlock *Node,
923e176c77SXinliang David Li                                 const MachineBlockFrequencyInfo *Graph) {
933e176c77SXinliang David Li     return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
943e176c77SXinliang David Li                                                      ViewHotFreqPercent);
953e176c77SXinliang David Li   }
963e176c77SXinliang David Li 
9755415f25SXinliang David Li   std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
9869317f2eSXinliang David Li                                 const MachineBlockFrequencyInfo *MBFI) {
993e176c77SXinliang David Li     return MBFIDOTGraphTraitsBase::getEdgeAttributes(
1003e176c77SXinliang David Li         Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
10169317f2eSXinliang David Li   }
10265bbcdfaSMichael Gottesman };
10365bbcdfaSMichael Gottesman 
10465bbcdfaSMichael Gottesman } // end namespace llvm
10565bbcdfaSMichael Gottesman #endif
10665bbcdfaSMichael Gottesman 
107875ebd5fSJakub Staszak INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, "machine-block-freq",
108875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
109875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
11010be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
111875ebd5fSJakub Staszak INITIALIZE_PASS_END(MachineBlockFrequencyInfo, "machine-block-freq",
112875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
113875ebd5fSJakub Staszak 
114875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
115875ebd5fSJakub Staszak 
116bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
117bc157084SXinliang David Li     : MachineFunctionPass(ID) {
118875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
119875ebd5fSJakub Staszak }
120875ebd5fSJakub Staszak 
1213dbe1050SDuncan P. N. Exon Smith MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() {}
122875ebd5fSJakub Staszak 
123875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
124875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
12510be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
126875ebd5fSJakub Staszak   AU.setPreservesAll();
127875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
128875ebd5fSJakub Staszak }
129875ebd5fSJakub Staszak 
130875ebd5fSJakub Staszak bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
131748fe483SMichael Gottesman   MachineBranchProbabilityInfo &MBPI =
132748fe483SMichael Gottesman       getAnalysis<MachineBranchProbabilityInfo>();
13310be9a88SDuncan P. N. Exon Smith   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
1343dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1353dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
1365e67b666SCong Hou   MBFI->calculate(F, MBPI, MLI);
13765bbcdfaSMichael Gottesman #ifndef NDEBUG
13880457ce5SXinliang David Li   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
1398dd5ce97SXinliang David Li       (ViewBlockFreqFuncName.empty() ||
1408dd5ce97SXinliang David Li        F.getName().equals(ViewBlockFreqFuncName))) {
14165bbcdfaSMichael Gottesman     view();
14265bbcdfaSMichael Gottesman   }
14365bbcdfaSMichael Gottesman #endif
144875ebd5fSJakub Staszak   return false;
145875ebd5fSJakub Staszak }
146875ebd5fSJakub Staszak 
1473dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
1483dbe1050SDuncan P. N. Exon Smith 
14965bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
15065bbcdfaSMichael Gottesman /// rendered using dot.
15165bbcdfaSMichael Gottesman void MachineBlockFrequencyInfo::view() const {
15265bbcdfaSMichael Gottesman // This code is only for debugging.
15365bbcdfaSMichael Gottesman #ifndef NDEBUG
15465bbcdfaSMichael Gottesman   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this),
15565bbcdfaSMichael Gottesman             "MachineBlockFrequencyDAGs");
15665bbcdfaSMichael Gottesman #else
157748fe483SMichael Gottesman   errs() << "MachineBlockFrequencyInfo::view is only available in debug builds "
158748fe483SMichael Gottesman             "on systems with Graphviz or gv!\n";
15965bbcdfaSMichael Gottesman #endif // NDEBUG
16065bbcdfaSMichael Gottesman }
16165bbcdfaSMichael Gottesman 
162bc157084SXinliang David Li BlockFrequency
163bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
1643dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
165875ebd5fSJakub Staszak }
16665bbcdfaSMichael Gottesman 
16730c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
16830c50f3cSXinliang David Li     const MachineBasicBlock *MBB) const {
16930c50f3cSXinliang David Li   const Function *F = MBFI->getFunction()->getFunction();
17030c50f3cSXinliang David Li   return MBFI ? MBFI->getBlockProfileCount(*F, MBB) : None;
17130c50f3cSXinliang David Li }
17230c50f3cSXinliang David Li 
173f801575fSSean Silva Optional<uint64_t>
174f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
175f801575fSSean Silva   const Function *F = MBFI->getFunction()->getFunction();
176f801575fSSean Silva   return MBFI ? MBFI->getProfileCountFromFreq(*F, Freq) : None;
177f801575fSSean Silva }
178f801575fSSean Silva 
179936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
18010be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
18165bbcdfaSMichael Gottesman }
182fd5c4b2cSMichael Gottesman 
1833264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
1843264fdd3SXinliang David Li   return MBFI ? &MBFI->getBPI() : nullptr;
1853264fdd3SXinliang David Li }
1863264fdd3SXinliang David Li 
187fd5c4b2cSMichael Gottesman raw_ostream &
188fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
189fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
1903dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
191fd5c4b2cSMichael Gottesman }
192fd5c4b2cSMichael Gottesman 
193fd5c4b2cSMichael Gottesman raw_ostream &
194fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
195fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
1963dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
197fd5c4b2cSMichael Gottesman }
198fd5c4b2cSMichael Gottesman 
1995e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
2003dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
201fd5c4b2cSMichael Gottesman }
202