1689a5073SDuncan P. N. Exon Smith //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
2875ebd5fSJakub Staszak //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6875ebd5fSJakub Staszak //
7875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
8875ebd5fSJakub Staszak //
9875ebd5fSJakub Staszak // Loops should be simplified before this analysis.
10875ebd5fSJakub Staszak //
11875ebd5fSJakub Staszak //===----------------------------------------------------------------------===//
12875ebd5fSJakub Staszak 
13875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
145df3d890SEugene Zelenko #include "llvm/ADT/DenseMap.h"
155df3d890SEugene Zelenko #include "llvm/ADT/None.h"
165df3d890SEugene Zelenko #include "llvm/ADT/iterator.h"
17689a5073SDuncan P. N. Exon Smith #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
185df3d890SEugene Zelenko #include "llvm/CodeGen/MachineBasicBlock.h"
19875ebd5fSJakub Staszak #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
2010be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineFunction.h"
2110be9a88SDuncan P. N. Exon Smith #include "llvm/CodeGen/MachineLoopInfo.h"
225df3d890SEugene Zelenko #include "llvm/Pass.h"
2365bbcdfaSMichael Gottesman #include "llvm/Support/CommandLine.h"
2465bbcdfaSMichael Gottesman #include "llvm/Support/GraphWriter.h"
255df3d890SEugene Zelenko #include <string>
26875ebd5fSJakub Staszak 
27875ebd5fSJakub Staszak using namespace llvm;
28875ebd5fSJakub Staszak 
291527baabSMatthias Braun #define DEBUG_TYPE "machine-block-freq"
301b9dde08SChandler Carruth 
31bc157084SXinliang David Li static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
32bc157084SXinliang David Li     "view-machine-block-freq-propagation-dags", cl::Hidden,
3365bbcdfaSMichael Gottesman     cl::desc("Pop up a window to show a dag displaying how machine block "
34748fe483SMichael Gottesman              "frequencies propagate through the CFG."),
35bc157084SXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
36bc157084SXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
37bc157084SXinliang David Li                           "display a graph using the "
3865bbcdfaSMichael Gottesman                           "fractional block frequency representation."),
39bc157084SXinliang David Li                clEnumValN(GVDT_Integer, "integer",
40bc157084SXinliang David Li                           "display a graph using the raw "
4165bbcdfaSMichael Gottesman                           "integer fractional block frequency representation."),
4230c50f3cSXinliang David Li                clEnumValN(GVDT_Count, "count", "display a graph using the real "
43732afdd0SMehdi Amini                                                "profile count if available.")));
445df3d890SEugene Zelenko 
45fd3f645fSXinliang David Li // Similar option above, but used to control BFI display only after MBP pass
46fd3f645fSXinliang David Li cl::opt<GVDAGType> ViewBlockLayoutWithBFI(
47fd3f645fSXinliang David Li     "view-block-layout-with-bfi", cl::Hidden,
48fd3f645fSXinliang David Li     cl::desc(
49fd3f645fSXinliang David Li         "Pop up a window to show a dag displaying MBP layout and associated "
50fd3f645fSXinliang David Li         "block frequencies of the CFG."),
51fd3f645fSXinliang David Li     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
52fd3f645fSXinliang David Li                clEnumValN(GVDT_Fraction, "fraction",
53fd3f645fSXinliang David Li                           "display a graph using the "
54fd3f645fSXinliang David Li                           "fractional block frequency representation."),
55fd3f645fSXinliang David Li                clEnumValN(GVDT_Integer, "integer",
56fd3f645fSXinliang David Li                           "display a graph using the raw "
57fd3f645fSXinliang David Li                           "integer fractional block frequency representation."),
58fd3f645fSXinliang David Li                clEnumValN(GVDT_Count, "count",
59fd3f645fSXinliang David Li                           "display a graph using the real "
60fd3f645fSXinliang David Li                           "profile count if available.")));
6165bbcdfaSMichael Gottesman 
6258fcc9bdSXinliang David Li // Command line option to specify the name of the function for CFG dump
6358fcc9bdSXinliang David Li // Defined in Analysis/BlockFrequencyInfo.cpp:  -view-bfi-func-name=
648dd5ce97SXinliang David Li extern cl::opt<std::string> ViewBlockFreqFuncName;
655df3d890SEugene Zelenko 
6658fcc9bdSXinliang David Li // Command line option to specify hot frequency threshold.
6758fcc9bdSXinliang David Li // Defined in Analysis/BlockFrequencyInfo.cpp:  -view-hot-freq-perc=
682d531580SSimon Pilgrim extern cl::opt<unsigned> ViewHotFreqPercent;
6980457ce5SXinliang David Li 
7063e17ebfSHiroshi Yamauchi static cl::opt<bool> PrintMachineBlockFreq(
7163e17ebfSHiroshi Yamauchi     "print-machine-bfi", cl::init(false), cl::Hidden,
7263e17ebfSHiroshi Yamauchi     cl::desc("Print the machine block frequency info."));
7363e17ebfSHiroshi Yamauchi 
7463e17ebfSHiroshi Yamauchi // Command line option to specify the name of the function for block frequency
7563e17ebfSHiroshi Yamauchi // dump. Defined in Analysis/BlockFrequencyInfo.cpp.
7663e17ebfSHiroshi Yamauchi extern cl::opt<std::string> PrintBlockFreqFuncName;
7763e17ebfSHiroshi Yamauchi 
78fd3f645fSXinliang David Li static GVDAGType getGVDT() {
79fd3f645fSXinliang David Li   if (ViewBlockLayoutWithBFI != GVDT_None)
80fd3f645fSXinliang David Li     return ViewBlockLayoutWithBFI;
81fd3f645fSXinliang David Li 
82fd3f645fSXinliang David Li   return ViewMachineBlockFreqPropagationDAG;
83fd3f645fSXinliang David Li }
84fd3f645fSXinliang David Li 
8565bbcdfaSMichael Gottesman namespace llvm {
8665bbcdfaSMichael Gottesman 
87bc157084SXinliang David Li template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
885df3d890SEugene Zelenko   using NodeRef = const MachineBasicBlock *;
895df3d890SEugene Zelenko   using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
905df3d890SEugene Zelenko   using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
9165bbcdfaSMichael Gottesman 
9248f814e8STim Shen   static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
930ac8eb91SDuncan P. N. Exon Smith     return &G->getFunction()->front();
9465bbcdfaSMichael Gottesman   }
95748fe483SMichael Gottesman 
96f2187ed3STim Shen   static ChildIteratorType child_begin(const NodeRef N) {
9765bbcdfaSMichael Gottesman     return N->succ_begin();
9865bbcdfaSMichael Gottesman   }
99748fe483SMichael Gottesman 
100f2187ed3STim Shen   static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
101748fe483SMichael Gottesman 
10265bbcdfaSMichael Gottesman   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
103b5e0f5acSTim Shen     return nodes_iterator(G->getFunction()->begin());
10465bbcdfaSMichael Gottesman   }
105748fe483SMichael Gottesman 
10665bbcdfaSMichael Gottesman   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
107b5e0f5acSTim Shen     return nodes_iterator(G->getFunction()->end());
10865bbcdfaSMichael Gottesman   }
10965bbcdfaSMichael Gottesman };
11065bbcdfaSMichael Gottesman 
1115df3d890SEugene Zelenko using MBFIDOTGraphTraitsBase =
1125df3d890SEugene Zelenko     BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
1135df3d890SEugene Zelenko                           MachineBranchProbabilityInfo>;
1145df3d890SEugene Zelenko 
11565bbcdfaSMichael Gottesman template <>
116bc157084SXinliang David Li struct DOTGraphTraits<MachineBlockFrequencyInfo *>
11755415f25SXinliang David Li     : public MBFIDOTGraphTraitsBase {
1185df3d890SEugene Zelenko   const MachineFunction *CurFunc = nullptr;
119fd3f645fSXinliang David Li   DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
12065bbcdfaSMichael Gottesman 
1215df3d890SEugene Zelenko   explicit DOTGraphTraits(bool isSimple = false)
1225df3d890SEugene Zelenko       : MBFIDOTGraphTraitsBase(isSimple) {}
1235df3d890SEugene Zelenko 
12465bbcdfaSMichael Gottesman   std::string getNodeLabel(const MachineBasicBlock *Node,
12565bbcdfaSMichael Gottesman                            const MachineBlockFrequencyInfo *Graph) {
126fd3f645fSXinliang David Li     int layout_order = -1;
127fd3f645fSXinliang David Li     // Attach additional ordering information if 'isSimple' is false.
128fd3f645fSXinliang David Li     if (!isSimple()) {
129fd3f645fSXinliang David Li       const MachineFunction *F = Node->getParent();
130fd3f645fSXinliang David Li       if (!CurFunc || F != CurFunc) {
131fd3f645fSXinliang David Li         if (CurFunc)
132fd3f645fSXinliang David Li           LayoutOrderMap.clear();
133fd3f645fSXinliang David Li 
134fd3f645fSXinliang David Li         CurFunc = F;
135fd3f645fSXinliang David Li         int O = 0;
136fd3f645fSXinliang David Li         for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
137fd3f645fSXinliang David Li           LayoutOrderMap[&*MBI] = O;
138fd3f645fSXinliang David Li         }
139fd3f645fSXinliang David Li       }
140fd3f645fSXinliang David Li       layout_order = LayoutOrderMap[Node];
141fd3f645fSXinliang David Li     }
142fd3f645fSXinliang David Li     return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
143fd3f645fSXinliang David Li                                                 layout_order);
14455415f25SXinliang David Li   }
14565bbcdfaSMichael Gottesman 
1463e176c77SXinliang David Li   std::string getNodeAttributes(const MachineBasicBlock *Node,
1473e176c77SXinliang David Li                                 const MachineBlockFrequencyInfo *Graph) {
1483e176c77SXinliang David Li     return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
1493e176c77SXinliang David Li                                                      ViewHotFreqPercent);
1503e176c77SXinliang David Li   }
1513e176c77SXinliang David Li 
15255415f25SXinliang David Li   std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
15369317f2eSXinliang David Li                                 const MachineBlockFrequencyInfo *MBFI) {
1543e176c77SXinliang David Li     return MBFIDOTGraphTraitsBase::getEdgeAttributes(
1553e176c77SXinliang David Li         Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
15669317f2eSXinliang David Li   }
15765bbcdfaSMichael Gottesman };
15865bbcdfaSMichael Gottesman 
15965bbcdfaSMichael Gottesman } // end namespace llvm
16065bbcdfaSMichael Gottesman 
1611527baabSMatthias Braun INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE,
162875ebd5fSJakub Staszak                       "Machine Block Frequency Analysis", true, true)
163875ebd5fSJakub Staszak INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
16410be9a88SDuncan P. N. Exon Smith INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
1651527baabSMatthias Braun INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE,
166875ebd5fSJakub Staszak                     "Machine Block Frequency Analysis", true, true)
167875ebd5fSJakub Staszak 
168875ebd5fSJakub Staszak char MachineBlockFrequencyInfo::ID = 0;
169875ebd5fSJakub Staszak 
170bc157084SXinliang David Li MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
171bc157084SXinliang David Li     : MachineFunctionPass(ID) {
172875ebd5fSJakub Staszak   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
173875ebd5fSJakub Staszak }
174875ebd5fSJakub Staszak 
1755df3d890SEugene Zelenko MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;
176875ebd5fSJakub Staszak 
177875ebd5fSJakub Staszak void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
178875ebd5fSJakub Staszak   AU.addRequired<MachineBranchProbabilityInfo>();
17910be9a88SDuncan P. N. Exon Smith   AU.addRequired<MachineLoopInfo>();
180875ebd5fSJakub Staszak   AU.setPreservesAll();
181875ebd5fSJakub Staszak   MachineFunctionPass::getAnalysisUsage(AU);
182875ebd5fSJakub Staszak }
183875ebd5fSJakub Staszak 
184bbb141c7SAdam Nemet void MachineBlockFrequencyInfo::calculate(
185bbb141c7SAdam Nemet     const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
186bbb141c7SAdam Nemet     const MachineLoopInfo &MLI) {
1873dbe1050SDuncan P. N. Exon Smith   if (!MBFI)
1883dbe1050SDuncan P. N. Exon Smith     MBFI.reset(new ImplType);
1895e67b666SCong Hou   MBFI->calculate(F, MBPI, MLI);
19080457ce5SXinliang David Li   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
1918dd5ce97SXinliang David Li       (ViewBlockFreqFuncName.empty() ||
1928dd5ce97SXinliang David Li        F.getName().equals(ViewBlockFreqFuncName))) {
193538d6668SXinliang David Li     view("MachineBlockFrequencyDAGS." + F.getName());
19465bbcdfaSMichael Gottesman   }
19563e17ebfSHiroshi Yamauchi   if (PrintMachineBlockFreq &&
19663e17ebfSHiroshi Yamauchi       (PrintBlockFreqFuncName.empty() ||
19763e17ebfSHiroshi Yamauchi        F.getName().equals(PrintBlockFreqFuncName))) {
19863e17ebfSHiroshi Yamauchi     MBFI->print(dbgs());
19963e17ebfSHiroshi Yamauchi   }
200bbb141c7SAdam Nemet }
201bbb141c7SAdam Nemet 
202bbb141c7SAdam Nemet bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
203bbb141c7SAdam Nemet   MachineBranchProbabilityInfo &MBPI =
204bbb141c7SAdam Nemet       getAnalysis<MachineBranchProbabilityInfo>();
205bbb141c7SAdam Nemet   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
206bbb141c7SAdam Nemet   calculate(F, MBPI, MLI);
207875ebd5fSJakub Staszak   return false;
208875ebd5fSJakub Staszak }
209875ebd5fSJakub Staszak 
2103dbe1050SDuncan P. N. Exon Smith void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
2113dbe1050SDuncan P. N. Exon Smith 
21265bbcdfaSMichael Gottesman /// Pop up a ghostview window with the current block frequency propagation
21365bbcdfaSMichael Gottesman /// rendered using dot.
214538d6668SXinliang David Li void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
21565bbcdfaSMichael Gottesman   // This code is only for debugging.
216538d6668SXinliang David Li   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
21765bbcdfaSMichael Gottesman }
21865bbcdfaSMichael Gottesman 
219bc157084SXinliang David Li BlockFrequency
220bc157084SXinliang David Li MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
2213dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
222875ebd5fSJakub Staszak }
22365bbcdfaSMichael Gottesman 
22430c50f3cSXinliang David Li Optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
22530c50f3cSXinliang David Li     const MachineBasicBlock *MBB) const {
226f1caa283SMatthias Braun   const Function &F = MBFI->getFunction()->getFunction();
227f1caa283SMatthias Braun   return MBFI ? MBFI->getBlockProfileCount(F, MBB) : None;
22830c50f3cSXinliang David Li }
22930c50f3cSXinliang David Li 
230f801575fSSean Silva Optional<uint64_t>
231f801575fSSean Silva MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
232f1caa283SMatthias Braun   const Function &F = MBFI->getFunction()->getFunction();
233f1caa283SMatthias Braun   return MBFI ? MBFI->getProfileCountFromFreq(F, Freq) : None;
234f801575fSSean Silva }
235f801575fSSean Silva 
236dce9def3SHiroshi Yamauchi bool
237dce9def3SHiroshi Yamauchi MachineBlockFrequencyInfo::isIrrLoopHeader(const MachineBasicBlock *MBB) {
238dce9def3SHiroshi Yamauchi   assert(MBFI && "Expected analysis to be available");
239dce9def3SHiroshi Yamauchi   return MBFI->isIrrLoopHeader(MBB);
240dce9def3SHiroshi Yamauchi }
241dce9def3SHiroshi Yamauchi 
242936aef92SDuncan P. N. Exon Smith const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
24310be9a88SDuncan P. N. Exon Smith   return MBFI ? MBFI->getFunction() : nullptr;
24465bbcdfaSMichael Gottesman }
245fd5c4b2cSMichael Gottesman 
2463264fdd3SXinliang David Li const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
2473264fdd3SXinliang David Li   return MBFI ? &MBFI->getBPI() : nullptr;
2483264fdd3SXinliang David Li }
2493264fdd3SXinliang David Li 
250fd5c4b2cSMichael Gottesman raw_ostream &
251fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
252fd5c4b2cSMichael Gottesman                                           const BlockFrequency Freq) const {
2533dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
254fd5c4b2cSMichael Gottesman }
255fd5c4b2cSMichael Gottesman 
256fd5c4b2cSMichael Gottesman raw_ostream &
257fd5c4b2cSMichael Gottesman MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
258fd5c4b2cSMichael Gottesman                                           const MachineBasicBlock *MBB) const {
2593dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
260fd5c4b2cSMichael Gottesman }
261fd5c4b2cSMichael Gottesman 
2625e985ee5SMichael Gottesman uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
2633dbe1050SDuncan P. N. Exon Smith   return MBFI ? MBFI->getEntryFreq() : 0;
264fd5c4b2cSMichael Gottesman }
265