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"
25bdd1243dSDimitry 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.
78*c9157d92SDimitry Andric extern cl::opt<std::string> PrintBFIFuncName;
79fe6060f1SDimitry Andric } // namespace llvm
800b57cec5SDimitry Andric
getGVDT()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
getEntryNodellvm::GraphTraits950b57cec5SDimitry Andric static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {
960b57cec5SDimitry Andric return &G->getFunction()->front();
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
child_beginllvm::GraphTraits990b57cec5SDimitry Andric static ChildIteratorType child_begin(const NodeRef N) {
1000b57cec5SDimitry Andric return N->succ_begin();
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
child_endllvm::GraphTraits1030b57cec5SDimitry Andric static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }
1040b57cec5SDimitry Andric
nodes_beginllvm::GraphTraits1050b57cec5SDimitry Andric static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {
1060b57cec5SDimitry Andric return nodes_iterator(G->getFunction()->begin());
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
nodes_endllvm::GraphTraits1090b57cec5SDimitry 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
DOTGraphTraitsllvm::DOTGraphTraits1240b57cec5SDimitry Andric explicit DOTGraphTraits(bool isSimple = false)
1250b57cec5SDimitry Andric : MBFIDOTGraphTraitsBase(isSimple) {}
1260b57cec5SDimitry Andric
getNodeLabelllvm::DOTGraphTraits1270b57cec5SDimitry 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
getNodeAttributesllvm::DOTGraphTraits1490b57cec5SDimitry 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
getEdgeAttributesllvm::DOTGraphTraits1550b57cec5SDimitry 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
MachineBlockFrequencyInfo()1730b57cec5SDimitry Andric MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
1740b57cec5SDimitry Andric : MachineFunctionPass(ID) {
1750b57cec5SDimitry Andric initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric
MachineBlockFrequencyInfo(MachineFunction & F,MachineBranchProbabilityInfo & MBPI,MachineLoopInfo & MLI)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
getAnalysisUsage(AnalysisUsage & AU) const1870b57cec5SDimitry 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
calculate(const MachineFunction & F,const MachineBranchProbabilityInfo & MBPI,const MachineLoopInfo & MLI)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 &&
206*c9157d92SDimitry Andric (PrintBFIFuncName.empty() || F.getName().equals(PrintBFIFuncName))) {
2070b57cec5SDimitry Andric MBFI->print(dbgs());
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & F)2110b57cec5SDimitry Andric bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
2120b57cec5SDimitry Andric MachineBranchProbabilityInfo &MBPI =
2130b57cec5SDimitry Andric getAnalysis<MachineBranchProbabilityInfo>();
2140b57cec5SDimitry Andric MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
2150b57cec5SDimitry Andric calculate(F, MBPI, MLI);
2160b57cec5SDimitry Andric return false;
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
releaseMemory()2190b57cec5SDimitry Andric void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric /// Pop up a ghostview window with the current block frequency propagation
2220b57cec5SDimitry Andric /// rendered using dot.
view(const Twine & Name,bool isSimple) const2230b57cec5SDimitry Andric void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
2240b57cec5SDimitry Andric // This code is only for debugging.
2250b57cec5SDimitry Andric ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric BlockFrequency
getBlockFreq(const MachineBasicBlock * MBB) const2290b57cec5SDimitry Andric MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
230*c9157d92SDimitry Andric return MBFI ? MBFI->getBlockFreq(MBB) : BlockFrequency(0);
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric
getBlockProfileCount(const MachineBasicBlock * MBB) const233bdd1243dSDimitry Andric std::optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
2340b57cec5SDimitry Andric const MachineBasicBlock *MBB) const {
235fe6060f1SDimitry Andric if (!MBFI)
236bdd1243dSDimitry Andric return std::nullopt;
237fe6060f1SDimitry Andric
2380b57cec5SDimitry Andric const Function &F = MBFI->getFunction()->getFunction();
239fe6060f1SDimitry Andric return MBFI->getBlockProfileCount(F, MBB);
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric
242bdd1243dSDimitry Andric std::optional<uint64_t>
getProfileCountFromFreq(BlockFrequency Freq) const243*c9157d92SDimitry Andric MachineBlockFrequencyInfo::getProfileCountFromFreq(BlockFrequency Freq) const {
244fe6060f1SDimitry Andric if (!MBFI)
245bdd1243dSDimitry Andric return std::nullopt;
246fe6060f1SDimitry Andric
2470b57cec5SDimitry Andric const Function &F = MBFI->getFunction()->getFunction();
248fe6060f1SDimitry Andric return MBFI->getProfileCountFromFreq(F, Freq);
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric
isIrrLoopHeader(const MachineBasicBlock * MBB) const251e8d8bef9SDimitry Andric bool MachineBlockFrequencyInfo::isIrrLoopHeader(
252e8d8bef9SDimitry Andric const MachineBasicBlock *MBB) const {
2530b57cec5SDimitry Andric assert(MBFI && "Expected analysis to be available");
2540b57cec5SDimitry Andric return MBFI->isIrrLoopHeader(MBB);
2550b57cec5SDimitry Andric }
2560b57cec5SDimitry Andric
onEdgeSplit(const MachineBasicBlock & NewPredecessor,const MachineBasicBlock & NewSuccessor,const MachineBranchProbabilityInfo & MBPI)257e8d8bef9SDimitry Andric void MachineBlockFrequencyInfo::onEdgeSplit(
258e8d8bef9SDimitry Andric const MachineBasicBlock &NewPredecessor,
259e8d8bef9SDimitry Andric const MachineBasicBlock &NewSuccessor,
260e8d8bef9SDimitry Andric const MachineBranchProbabilityInfo &MBPI) {
2615ffd83dbSDimitry Andric assert(MBFI && "Expected analysis to be available");
262e8d8bef9SDimitry Andric auto NewSuccFreq = MBFI->getBlockFreq(&NewPredecessor) *
263e8d8bef9SDimitry Andric MBPI.getEdgeProbability(&NewPredecessor, &NewSuccessor);
264e8d8bef9SDimitry Andric
265*c9157d92SDimitry Andric MBFI->setBlockFreq(&NewSuccessor, NewSuccFreq);
2665ffd83dbSDimitry Andric }
2675ffd83dbSDimitry Andric
getFunction() const2680b57cec5SDimitry Andric const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
2690b57cec5SDimitry Andric return MBFI ? MBFI->getFunction() : nullptr;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric
getMBPI() const2720b57cec5SDimitry Andric const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
2730b57cec5SDimitry Andric return MBFI ? &MBFI->getBPI() : nullptr;
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric
getEntryFreq() const276*c9157d92SDimitry Andric BlockFrequency MachineBlockFrequencyInfo::getEntryFreq() const {
277*c9157d92SDimitry Andric return MBFI ? MBFI->getEntryFreq() : BlockFrequency(0);
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric
printBlockFreq(const MachineBlockFrequencyInfo & MBFI,BlockFrequency Freq)280*c9157d92SDimitry Andric Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
281*c9157d92SDimitry Andric BlockFrequency Freq) {
282*c9157d92SDimitry Andric return Printable([&MBFI, Freq](raw_ostream &OS) {
283*c9157d92SDimitry Andric printBlockFreqImpl(OS, MBFI.getEntryFreq(), Freq);
284*c9157d92SDimitry Andric });
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric
printBlockFreq(const MachineBlockFrequencyInfo & MBFI,const MachineBasicBlock & MBB)287*c9157d92SDimitry Andric Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
288*c9157d92SDimitry Andric const MachineBasicBlock &MBB) {
289*c9157d92SDimitry Andric return printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB));
2900b57cec5SDimitry Andric }
291