xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Loops should be simplified before this analysis.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
140b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
150b57cec5SDimitry Andric #include "llvm/ADT/iterator.h"
160b57cec5SDimitry Andric #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
21480093f4SDimitry Andric #include "llvm/InitializePasses.h"
220b57cec5SDimitry Andric #include "llvm/Pass.h"
230b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
240b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h"
25*bdd1243dSDimitry Andric #include <optional>
260b57cec5SDimitry Andric #include <string>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric #define DEBUG_TYPE "machine-block-freq"
310b57cec5SDimitry Andric 
32fe6060f1SDimitry Andric namespace llvm {
330b57cec5SDimitry Andric static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(
340b57cec5SDimitry Andric     "view-machine-block-freq-propagation-dags", cl::Hidden,
350b57cec5SDimitry Andric     cl::desc("Pop up a window to show a dag displaying how machine block "
360b57cec5SDimitry Andric              "frequencies propagate through the CFG."),
370b57cec5SDimitry Andric     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
380b57cec5SDimitry Andric                clEnumValN(GVDT_Fraction, "fraction",
390b57cec5SDimitry Andric                           "display a graph using the "
400b57cec5SDimitry Andric                           "fractional block frequency representation."),
410b57cec5SDimitry Andric                clEnumValN(GVDT_Integer, "integer",
420b57cec5SDimitry Andric                           "display a graph using the raw "
430b57cec5SDimitry Andric                           "integer fractional block frequency representation."),
440b57cec5SDimitry Andric                clEnumValN(GVDT_Count, "count", "display a graph using the real "
450b57cec5SDimitry Andric                                                "profile count if available.")));
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric // Similar option above, but used to control BFI display only after MBP pass
480b57cec5SDimitry Andric cl::opt<GVDAGType> ViewBlockLayoutWithBFI(
490b57cec5SDimitry Andric     "view-block-layout-with-bfi", cl::Hidden,
500b57cec5SDimitry Andric     cl::desc(
510b57cec5SDimitry Andric         "Pop up a window to show a dag displaying MBP layout and associated "
520b57cec5SDimitry Andric         "block frequencies of the CFG."),
530b57cec5SDimitry Andric     cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),
540b57cec5SDimitry Andric                clEnumValN(GVDT_Fraction, "fraction",
550b57cec5SDimitry Andric                           "display a graph using the "
560b57cec5SDimitry Andric                           "fractional block frequency representation."),
570b57cec5SDimitry Andric                clEnumValN(GVDT_Integer, "integer",
580b57cec5SDimitry Andric                           "display a graph using the raw "
590b57cec5SDimitry Andric                           "integer fractional block frequency representation."),
600b57cec5SDimitry Andric                clEnumValN(GVDT_Count, "count",
610b57cec5SDimitry Andric                           "display a graph using the real "
620b57cec5SDimitry Andric                           "profile count if available.")));
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric // Command line option to specify the name of the function for CFG dump
650b57cec5SDimitry Andric // Defined in Analysis/BlockFrequencyInfo.cpp:  -view-bfi-func-name=
660b57cec5SDimitry Andric extern cl::opt<std::string> ViewBlockFreqFuncName;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric // Command line option to specify hot frequency threshold.
690b57cec5SDimitry Andric // Defined in Analysis/BlockFrequencyInfo.cpp:  -view-hot-freq-perc=
700b57cec5SDimitry Andric extern cl::opt<unsigned> ViewHotFreqPercent;
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric static cl::opt<bool> PrintMachineBlockFreq(
730b57cec5SDimitry Andric     "print-machine-bfi", cl::init(false), cl::Hidden,
740b57cec5SDimitry Andric     cl::desc("Print the machine block frequency info."));
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric // Command line option to specify the name of the function for block frequency
770b57cec5SDimitry Andric // dump. Defined in Analysis/BlockFrequencyInfo.cpp.
780b57cec5SDimitry Andric extern cl::opt<std::string> PrintBlockFreqFuncName;
79fe6060f1SDimitry Andric } // namespace llvm
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric static GVDAGType getGVDT() {
820b57cec5SDimitry Andric   if (ViewBlockLayoutWithBFI != GVDT_None)
830b57cec5SDimitry Andric     return ViewBlockLayoutWithBFI;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   return ViewMachineBlockFreqPropagationDAG;
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric namespace llvm {
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric template <> struct GraphTraits<MachineBlockFrequencyInfo *> {
910b57cec5SDimitry Andric   using NodeRef = const MachineBasicBlock *;
920b57cec5SDimitry Andric   using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
930b57cec5SDimitry Andric   using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
960b57cec5SDimitry Andric     return &G->getFunction()->front();
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   static ChildIteratorType child_begin(const NodeRef N) {
1000b57cec5SDimitry Andric     return N->succ_begin();
1010b57cec5SDimitry Andric   }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
1060b57cec5SDimitry Andric     return nodes_iterator(G->getFunction()->begin());
1070b57cec5SDimitry Andric   }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric   static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {
1100b57cec5SDimitry Andric     return nodes_iterator(G->getFunction()->end());
1110b57cec5SDimitry Andric   }
1120b57cec5SDimitry Andric };
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric using MBFIDOTGraphTraitsBase =
1150b57cec5SDimitry Andric     BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,
1160b57cec5SDimitry Andric                           MachineBranchProbabilityInfo>;
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric template <>
1190b57cec5SDimitry Andric struct DOTGraphTraits<MachineBlockFrequencyInfo *>
1200b57cec5SDimitry Andric     : public MBFIDOTGraphTraitsBase {
1210b57cec5SDimitry Andric   const MachineFunction *CurFunc = nullptr;
1220b57cec5SDimitry Andric   DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   explicit DOTGraphTraits(bool isSimple = false)
1250b57cec5SDimitry Andric       : MBFIDOTGraphTraitsBase(isSimple) {}
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   std::string getNodeLabel(const MachineBasicBlock *Node,
1280b57cec5SDimitry Andric                            const MachineBlockFrequencyInfo *Graph) {
1290b57cec5SDimitry Andric     int layout_order = -1;
1300b57cec5SDimitry Andric     // Attach additional ordering information if 'isSimple' is false.
1310b57cec5SDimitry Andric     if (!isSimple()) {
1320b57cec5SDimitry Andric       const MachineFunction *F = Node->getParent();
1330b57cec5SDimitry Andric       if (!CurFunc || F != CurFunc) {
1340b57cec5SDimitry Andric         if (CurFunc)
1350b57cec5SDimitry Andric           LayoutOrderMap.clear();
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric         CurFunc = F;
1380b57cec5SDimitry Andric         int O = 0;
1390b57cec5SDimitry Andric         for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {
1400b57cec5SDimitry Andric           LayoutOrderMap[&*MBI] = O;
1410b57cec5SDimitry Andric         }
1420b57cec5SDimitry Andric       }
1430b57cec5SDimitry Andric       layout_order = LayoutOrderMap[Node];
1440b57cec5SDimitry Andric     }
1450b57cec5SDimitry Andric     return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),
1460b57cec5SDimitry Andric                                                 layout_order);
1470b57cec5SDimitry Andric   }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   std::string getNodeAttributes(const MachineBasicBlock *Node,
1500b57cec5SDimitry Andric                                 const MachineBlockFrequencyInfo *Graph) {
1510b57cec5SDimitry Andric     return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
1520b57cec5SDimitry Andric                                                      ViewHotFreqPercent);
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
1560b57cec5SDimitry Andric                                 const MachineBlockFrequencyInfo *MBFI) {
1570b57cec5SDimitry Andric     return MBFIDOTGraphTraitsBase::getEdgeAttributes(
1580b57cec5SDimitry Andric         Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);
1590b57cec5SDimitry Andric   }
1600b57cec5SDimitry Andric };
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric } // end namespace llvm
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE,
1650b57cec5SDimitry Andric                       "Machine Block Frequency Analysis", true, true)
1660b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
1670b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
1680b57cec5SDimitry Andric INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE,
1690b57cec5SDimitry Andric                     "Machine Block Frequency Analysis", true, true)
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric char MachineBlockFrequencyInfo::ID = 0;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
1740b57cec5SDimitry Andric     : MachineFunctionPass(ID) {
1750b57cec5SDimitry Andric   initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric 
178480093f4SDimitry Andric MachineBlockFrequencyInfo::MachineBlockFrequencyInfo(
179480093f4SDimitry Andric       MachineFunction &F,
180480093f4SDimitry Andric       MachineBranchProbabilityInfo &MBPI,
181480093f4SDimitry Andric       MachineLoopInfo &MLI) : MachineFunctionPass(ID) {
182480093f4SDimitry Andric   calculate(F, MBPI, MLI);
183480093f4SDimitry Andric }
184480093f4SDimitry Andric 
1850b57cec5SDimitry Andric MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {
1880b57cec5SDimitry Andric   AU.addRequired<MachineBranchProbabilityInfo>();
1890b57cec5SDimitry Andric   AU.addRequired<MachineLoopInfo>();
1900b57cec5SDimitry Andric   AU.setPreservesAll();
1910b57cec5SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
1920b57cec5SDimitry Andric }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric void MachineBlockFrequencyInfo::calculate(
1950b57cec5SDimitry Andric     const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
1960b57cec5SDimitry Andric     const MachineLoopInfo &MLI) {
1970b57cec5SDimitry Andric   if (!MBFI)
1980b57cec5SDimitry Andric     MBFI.reset(new ImplType);
1990b57cec5SDimitry Andric   MBFI->calculate(F, MBPI, MLI);
2000b57cec5SDimitry Andric   if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
2010b57cec5SDimitry Andric       (ViewBlockFreqFuncName.empty() ||
2020b57cec5SDimitry Andric        F.getName().equals(ViewBlockFreqFuncName))) {
2030b57cec5SDimitry Andric     view("MachineBlockFrequencyDAGS." + F.getName());
2040b57cec5SDimitry Andric   }
2050b57cec5SDimitry Andric   if (PrintMachineBlockFreq &&
2060b57cec5SDimitry Andric       (PrintBlockFreqFuncName.empty() ||
2070b57cec5SDimitry Andric        F.getName().equals(PrintBlockFreqFuncName))) {
2080b57cec5SDimitry Andric     MBFI->print(dbgs());
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
2130b57cec5SDimitry Andric   MachineBranchProbabilityInfo &MBPI =
2140b57cec5SDimitry Andric       getAnalysis<MachineBranchProbabilityInfo>();
2150b57cec5SDimitry Andric   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
2160b57cec5SDimitry Andric   calculate(F, MBPI, MLI);
2170b57cec5SDimitry Andric   return false;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric /// Pop up a ghostview window with the current block frequency propagation
2230b57cec5SDimitry Andric /// rendered using dot.
2240b57cec5SDimitry Andric void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
2250b57cec5SDimitry Andric   // This code is only for debugging.
2260b57cec5SDimitry Andric   ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric BlockFrequency
2300b57cec5SDimitry Andric MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
2310b57cec5SDimitry Andric   return MBFI ? MBFI->getBlockFreq(MBB) : 0;
2320b57cec5SDimitry Andric }
2330b57cec5SDimitry Andric 
234*bdd1243dSDimitry Andric std::optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
2350b57cec5SDimitry Andric     const MachineBasicBlock *MBB) const {
236fe6060f1SDimitry Andric   if (!MBFI)
237*bdd1243dSDimitry Andric     return std::nullopt;
238fe6060f1SDimitry Andric 
2390b57cec5SDimitry Andric   const Function &F = MBFI->getFunction()->getFunction();
240fe6060f1SDimitry Andric   return MBFI->getBlockProfileCount(F, MBB);
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
243*bdd1243dSDimitry Andric std::optional<uint64_t>
2440b57cec5SDimitry Andric MachineBlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq) const {
245fe6060f1SDimitry Andric   if (!MBFI)
246*bdd1243dSDimitry Andric     return std::nullopt;
247fe6060f1SDimitry Andric 
2480b57cec5SDimitry Andric   const Function &F = MBFI->getFunction()->getFunction();
249fe6060f1SDimitry Andric   return MBFI->getProfileCountFromFreq(F, Freq);
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric 
252e8d8bef9SDimitry Andric bool MachineBlockFrequencyInfo::isIrrLoopHeader(
253e8d8bef9SDimitry Andric     const MachineBasicBlock *MBB) const {
2540b57cec5SDimitry Andric   assert(MBFI && "Expected analysis to be available");
2550b57cec5SDimitry Andric   return MBFI->isIrrLoopHeader(MBB);
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric 
258e8d8bef9SDimitry Andric void MachineBlockFrequencyInfo::onEdgeSplit(
259e8d8bef9SDimitry Andric     const MachineBasicBlock &NewPredecessor,
260e8d8bef9SDimitry Andric     const MachineBasicBlock &NewSuccessor,
261e8d8bef9SDimitry Andric     const MachineBranchProbabilityInfo &MBPI) {
2625ffd83dbSDimitry Andric   assert(MBFI && "Expected analysis to be available");
263e8d8bef9SDimitry Andric   auto NewSuccFreq = MBFI->getBlockFreq(&NewPredecessor) *
264e8d8bef9SDimitry Andric                      MBPI.getEdgeProbability(&NewPredecessor, &NewSuccessor);
265e8d8bef9SDimitry Andric 
266e8d8bef9SDimitry Andric   MBFI->setBlockFreq(&NewSuccessor, NewSuccFreq.getFrequency());
2675ffd83dbSDimitry Andric }
2685ffd83dbSDimitry Andric 
2690b57cec5SDimitry Andric const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
2700b57cec5SDimitry Andric   return MBFI ? MBFI->getFunction() : nullptr;
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
2740b57cec5SDimitry Andric   return MBFI ? &MBFI->getBPI() : nullptr;
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric raw_ostream &
2780b57cec5SDimitry Andric MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
2790b57cec5SDimitry Andric                                           const BlockFrequency Freq) const {
2800b57cec5SDimitry Andric   return MBFI ? MBFI->printBlockFreq(OS, Freq) : OS;
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric raw_ostream &
2840b57cec5SDimitry Andric MachineBlockFrequencyInfo::printBlockFreq(raw_ostream &OS,
2850b57cec5SDimitry Andric                                           const MachineBasicBlock *MBB) const {
2860b57cec5SDimitry Andric   return MBFI ? MBFI->printBlockFreq(OS, MBB) : OS;
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric uint64_t MachineBlockFrequencyInfo::getEntryFreq() const {
2900b57cec5SDimitry Andric   return MBFI ? MBFI->getEntryFreq() : 0;
2910b57cec5SDimitry Andric }
292