326 lines · cpp
1//===- MachineBlockFrequencyInfo.cpp - MBB Frequency Analysis -------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Loops should be simplified before this analysis.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"14#include "llvm/ADT/DenseMap.h"15#include "llvm/ADT/iterator.h"16#include "llvm/Analysis/BlockFrequencyInfoImpl.h"17#include "llvm/CodeGen/MachineBasicBlock.h"18#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"19#include "llvm/CodeGen/MachineFunction.h"20#include "llvm/CodeGen/MachineLoopInfo.h"21#include "llvm/InitializePasses.h"22#include "llvm/Pass.h"23#include "llvm/Support/CommandLine.h"24#include "llvm/Support/GraphWriter.h"25#include <optional>26#include <string>27 28using namespace llvm;29 30#define DEBUG_TYPE "machine-block-freq"31 32static cl::opt<GVDAGType> ViewMachineBlockFreqPropagationDAG(33 "view-machine-block-freq-propagation-dags", cl::Hidden,34 cl::desc("Pop up a window to show a dag displaying how machine block "35 "frequencies propagate through the CFG."),36 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),37 clEnumValN(GVDT_Fraction, "fraction",38 "display a graph using the "39 "fractional block frequency representation."),40 clEnumValN(GVDT_Integer, "integer",41 "display a graph using the raw "42 "integer fractional block frequency representation."),43 clEnumValN(GVDT_Count, "count", "display a graph using the real "44 "profile count if available.")));45 46namespace llvm {47// Similar option above, but used to control BFI display only after MBP pass48cl::opt<GVDAGType> ViewBlockLayoutWithBFI(49 "view-block-layout-with-bfi", cl::Hidden,50 cl::desc(51 "Pop up a window to show a dag displaying MBP layout and associated "52 "block frequencies of the CFG."),53 cl::values(clEnumValN(GVDT_None, "none", "do not display graphs."),54 clEnumValN(GVDT_Fraction, "fraction",55 "display a graph using the "56 "fractional block frequency representation."),57 clEnumValN(GVDT_Integer, "integer",58 "display a graph using the raw "59 "integer fractional block frequency representation."),60 clEnumValN(GVDT_Count, "count",61 "display a graph using the real "62 "profile count if available.")));63 64// Command line option to specify the name of the function for CFG dump65// Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=66extern cl::opt<std::string> ViewBlockFreqFuncName;67 68// Command line option to specify hot frequency threshold.69// Defined in Analysis/BlockFrequencyInfo.cpp: -view-hot-freq-perc=70extern cl::opt<unsigned> ViewHotFreqPercent;71 72// Command line option to specify the name of the function for block frequency73// dump. Defined in Analysis/BlockFrequencyInfo.cpp.74extern cl::opt<std::string> PrintBFIFuncName;75} // namespace llvm76 77static cl::opt<bool>78 PrintMachineBlockFreq("print-machine-bfi", cl::init(false), cl::Hidden,79 cl::desc("Print the machine block frequency info."));80 81static GVDAGType getGVDT() {82 if (ViewBlockLayoutWithBFI != GVDT_None)83 return ViewBlockLayoutWithBFI;84 85 return ViewMachineBlockFreqPropagationDAG;86}87 88template <> struct llvm::GraphTraits<MachineBlockFrequencyInfo *> {89 using NodeRef = const MachineBasicBlock *;90 using ChildIteratorType = MachineBasicBlock::const_succ_iterator;91 using nodes_iterator = pointer_iterator<MachineFunction::const_iterator>;92 93 static NodeRef getEntryNode(const MachineBlockFrequencyInfo *G) {94 return &G->getFunction()->front();95 }96 97 static ChildIteratorType child_begin(const NodeRef N) {98 return N->succ_begin();99 }100 101 static ChildIteratorType child_end(const NodeRef N) { return N->succ_end(); }102 103 static nodes_iterator nodes_begin(const MachineBlockFrequencyInfo *G) {104 return nodes_iterator(G->getFunction()->begin());105 }106 107 static nodes_iterator nodes_end(const MachineBlockFrequencyInfo *G) {108 return nodes_iterator(G->getFunction()->end());109 }110};111 112using MBFIDOTGraphTraitsBase =113 BFIDOTGraphTraitsBase<MachineBlockFrequencyInfo,114 MachineBranchProbabilityInfo>;115 116template <>117struct llvm::DOTGraphTraits<MachineBlockFrequencyInfo *>118 : public MBFIDOTGraphTraitsBase {119 const MachineFunction *CurFunc = nullptr;120 DenseMap<const MachineBasicBlock *, int> LayoutOrderMap;121 122 explicit DOTGraphTraits(bool isSimple = false)123 : MBFIDOTGraphTraitsBase(isSimple) {}124 125 std::string getNodeLabel(const MachineBasicBlock *Node,126 const MachineBlockFrequencyInfo *Graph) {127 int layout_order = -1;128 // Attach additional ordering information if 'isSimple' is false.129 if (!isSimple()) {130 const MachineFunction *F = Node->getParent();131 if (!CurFunc || F != CurFunc) {132 if (CurFunc)133 LayoutOrderMap.clear();134 135 CurFunc = F;136 int O = 0;137 for (auto MBI = F->begin(); MBI != F->end(); ++MBI, ++O) {138 LayoutOrderMap[&*MBI] = O;139 }140 }141 layout_order = LayoutOrderMap[Node];142 }143 return MBFIDOTGraphTraitsBase::getNodeLabel(Node, Graph, getGVDT(),144 layout_order);145 }146 147 std::string getNodeAttributes(const MachineBasicBlock *Node,148 const MachineBlockFrequencyInfo *Graph) {149 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,150 ViewHotFreqPercent);151 }152 153 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,154 const MachineBlockFrequencyInfo *MBFI) {155 return MBFIDOTGraphTraitsBase::getEdgeAttributes(156 Node, EI, MBFI, MBFI->getMBPI(), ViewHotFreqPercent);157 }158};159 160AnalysisKey MachineBlockFrequencyAnalysis::Key;161 162MachineBlockFrequencyAnalysis::Result163MachineBlockFrequencyAnalysis::run(MachineFunction &MF,164 MachineFunctionAnalysisManager &MFAM) {165 auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF);166 auto &MLI = MFAM.getResult<MachineLoopAnalysis>(MF);167 return Result(MF, MBPI, MLI);168}169 170PreservedAnalyses171MachineBlockFrequencyPrinterPass::run(MachineFunction &MF,172 MachineFunctionAnalysisManager &MFAM) {173 auto &MBFI = MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);174 OS << "Machine block frequency for machine function: " << MF.getName()175 << '\n';176 MBFI.print(OS);177 return PreservedAnalyses::all();178}179 180INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfoWrapperPass, DEBUG_TYPE,181 "Machine Block Frequency Analysis", true, true)182INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)183INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)184INITIALIZE_PASS_END(MachineBlockFrequencyInfoWrapperPass, DEBUG_TYPE,185 "Machine Block Frequency Analysis", true, true)186 187char MachineBlockFrequencyInfoWrapperPass::ID = 0;188 189MachineBlockFrequencyInfoWrapperPass::MachineBlockFrequencyInfoWrapperPass()190 : MachineFunctionPass(ID) {191 initializeMachineBlockFrequencyInfoWrapperPassPass(192 *PassRegistry::getPassRegistry());193}194 195MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() = default;196 197MachineBlockFrequencyInfo::MachineBlockFrequencyInfo(198 MachineBlockFrequencyInfo &&) = default;199 200MachineBlockFrequencyInfo::MachineBlockFrequencyInfo(201 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,202 const MachineLoopInfo &MLI) {203 calculate(F, MBPI, MLI);204}205 206MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;207 208bool MachineBlockFrequencyInfo::invalidate(209 MachineFunction &MF, const PreservedAnalyses &PA,210 MachineFunctionAnalysisManager::Invalidator &) {211 // Check whether the analysis, all analyses on machine functions, or the212 // machine function's CFG have been preserved.213 auto PAC = PA.getChecker<MachineBlockFrequencyAnalysis>();214 return !PAC.preserved() &&215 !PAC.preservedSet<AllAnalysesOn<MachineFunction>>() &&216 !PAC.preservedSet<CFGAnalyses>();217}218 219void MachineBlockFrequencyInfoWrapperPass::getAnalysisUsage(220 AnalysisUsage &AU) const {221 AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();222 AU.addRequired<MachineLoopInfoWrapperPass>();223 AU.setPreservesAll();224 MachineFunctionPass::getAnalysisUsage(AU);225}226 227void MachineBlockFrequencyInfo::calculate(228 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,229 const MachineLoopInfo &MLI) {230 if (!MBFI)231 MBFI.reset(new ImplType);232 MBFI->calculate(F, MBPI, MLI);233 if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&234 (ViewBlockFreqFuncName.empty() || F.getName() == ViewBlockFreqFuncName)) {235 view("MachineBlockFrequencyDAGS." + F.getName());236 }237 if (PrintMachineBlockFreq &&238 (PrintBFIFuncName.empty() || F.getName() == PrintBFIFuncName)) {239 MBFI->print(dbgs());240 }241}242 243bool MachineBlockFrequencyInfoWrapperPass::runOnMachineFunction(244 MachineFunction &F) {245 MachineBranchProbabilityInfo &MBPI =246 getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();247 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();248 MBFI.calculate(F, MBPI, MLI);249 return false;250}251 252void MachineBlockFrequencyInfo::print(raw_ostream &OS) { MBFI->print(OS); }253 254void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }255 256/// Pop up a ghostview window with the current block frequency propagation257/// rendered using dot.258void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {259 // This code is only for debugging.260 ViewGraph(const_cast<MachineBlockFrequencyInfo *>(this), Name, isSimple);261}262 263BlockFrequency264MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {265 return MBFI ? MBFI->getBlockFreq(MBB) : BlockFrequency(0);266}267 268std::optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(269 const MachineBasicBlock *MBB) const {270 if (!MBFI)271 return std::nullopt;272 273 const Function &F = MBFI->getFunction()->getFunction();274 return MBFI->getBlockProfileCount(F, MBB);275}276 277std::optional<uint64_t>278MachineBlockFrequencyInfo::getProfileCountFromFreq(BlockFrequency Freq) const {279 if (!MBFI)280 return std::nullopt;281 282 const Function &F = MBFI->getFunction()->getFunction();283 return MBFI->getProfileCountFromFreq(F, Freq);284}285 286bool MachineBlockFrequencyInfo::isIrrLoopHeader(287 const MachineBasicBlock *MBB) const {288 assert(MBFI && "Expected analysis to be available");289 return MBFI->isIrrLoopHeader(MBB);290}291 292void MachineBlockFrequencyInfo::onEdgeSplit(293 const MachineBasicBlock &NewPredecessor,294 const MachineBasicBlock &NewSuccessor,295 const MachineBranchProbabilityInfo &MBPI) {296 assert(MBFI && "Expected analysis to be available");297 auto NewSuccFreq = MBFI->getBlockFreq(&NewPredecessor) *298 MBPI.getEdgeProbability(&NewPredecessor, &NewSuccessor);299 300 MBFI->setBlockFreq(&NewSuccessor, NewSuccFreq);301}302 303const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {304 return MBFI ? MBFI->getFunction() : nullptr;305}306 307const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {308 return MBFI ? &MBFI->getBPI() : nullptr;309}310 311BlockFrequency MachineBlockFrequencyInfo::getEntryFreq() const {312 return MBFI ? MBFI->getEntryFreq() : BlockFrequency(0);313}314 315Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,316 BlockFrequency Freq) {317 return Printable([&MBFI, Freq](raw_ostream &OS) {318 printRelativeBlockFreq(OS, MBFI.getEntryFreq(), Freq);319 });320}321 322Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,323 const MachineBasicBlock &MBB) {324 return printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB));325}326