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-exception
6//
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 pass
48cl::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 dump
65// 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 frequency
73// dump. Defined in Analysis/BlockFrequencyInfo.cpp.
74extern cl::opt<std::string> PrintBFIFuncName;
75} // namespace llvm
76
77static cl::opt<bool>
78 PrintMachineBlockFreq("print-machine-bfi", cl::init(Val: 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, GType: getGVDT(),
144 layout_order);
145 }
146
147 std::string getNodeAttributes(const MachineBasicBlock *Node,
148 const MachineBlockFrequencyInfo *Graph) {
149 return MBFIDOTGraphTraitsBase::getNodeAttributes(Node, Graph,
150 HotPercentThreshold: ViewHotFreqPercent);
151 }
152
153 std::string getEdgeAttributes(const MachineBasicBlock *Node, EdgeIter EI,
154 const MachineBlockFrequencyInfo *MBFI) {
155 return MBFIDOTGraphTraitsBase::getEdgeAttributes(
156 Node, EI, BFI: MBFI, BPI: MBFI->getMBPI(), HotPercentThreshold: ViewHotFreqPercent);
157 }
158};
159
160AnalysisKey MachineBlockFrequencyAnalysis::Key;
161
162MachineBlockFrequencyAnalysis::Result
163MachineBlockFrequencyAnalysis::run(MachineFunction &MF,
164 MachineFunctionAnalysisManager &MFAM) {
165 auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(IR&: MF);
166 auto &MLI = MFAM.getResult<MachineLoopAnalysis>(IR&: MF);
167 return Result(MF, MBPI, MLI);
168}
169
170PreservedAnalyses
171MachineBlockFrequencyPrinterPass::run(MachineFunction &MF,
172 MachineFunctionAnalysisManager &MFAM) {
173 auto &MBFI = MFAM.getResult<MachineBlockFrequencyAnalysis>(IR&: 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
192MachineBlockFrequencyInfo::MachineBlockFrequencyInfo() = default;
193
194MachineBlockFrequencyInfo::MachineBlockFrequencyInfo(
195 MachineBlockFrequencyInfo &&) = default;
196
197MachineBlockFrequencyInfo::MachineBlockFrequencyInfo(
198 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
199 const MachineLoopInfo &MLI) {
200 calculate(F, MBPI, MLI);
201}
202
203MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;
204
205bool MachineBlockFrequencyInfo::invalidate(
206 MachineFunction &MF, const PreservedAnalyses &PA,
207 MachineFunctionAnalysisManager::Invalidator &) {
208 // Check whether the analysis, all analyses on machine functions, or the
209 // machine function's CFG have been preserved.
210 auto PAC = PA.getChecker<MachineBlockFrequencyAnalysis>();
211 return !PAC.preserved() &&
212 !PAC.preservedSet<AllAnalysesOn<MachineFunction>>() &&
213 !PAC.preservedSet<CFGAnalyses>();
214}
215
216void MachineBlockFrequencyInfoWrapperPass::getAnalysisUsage(
217 AnalysisUsage &AU) const {
218 AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
219 AU.addRequired<MachineLoopInfoWrapperPass>();
220 AU.setPreservesAll();
221 MachineFunctionPass::getAnalysisUsage(AU);
222}
223
224void MachineBlockFrequencyInfo::calculate(
225 const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
226 const MachineLoopInfo &MLI) {
227 if (!MBFI)
228 MBFI.reset(p: new ImplType);
229 MBFI->calculate(F, BPI: MBPI, LI: MLI);
230 if (ViewMachineBlockFreqPropagationDAG != GVDT_None &&
231 (ViewBlockFreqFuncName.empty() || F.getName() == ViewBlockFreqFuncName)) {
232 view(Name: "MachineBlockFrequencyDAGS." + F.getName());
233 }
234 if (PrintMachineBlockFreq &&
235 (PrintBFIFuncName.empty() || F.getName() == PrintBFIFuncName)) {
236 MBFI->print(OS&: dbgs());
237 }
238}
239
240bool MachineBlockFrequencyInfoWrapperPass::runOnMachineFunction(
241 MachineFunction &F) {
242 MachineBranchProbabilityInfo &MBPI =
243 getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
244 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
245 MBFI.calculate(F, MBPI, MLI);
246 return false;
247}
248
249void MachineBlockFrequencyInfo::print(raw_ostream &OS) { MBFI->print(OS); }
250
251void MachineBlockFrequencyInfo::releaseMemory() { MBFI.reset(); }
252
253/// Pop up a ghostview window with the current block frequency propagation
254/// rendered using dot.
255void MachineBlockFrequencyInfo::view(const Twine &Name, bool isSimple) const {
256 // This code is only for debugging.
257 ViewGraph(G: const_cast<MachineBlockFrequencyInfo *>(this), Name, ShortNames: isSimple);
258}
259
260BlockFrequency
261MachineBlockFrequencyInfo::getBlockFreq(const MachineBasicBlock *MBB) const {
262 return MBFI ? MBFI->getBlockFreq(BB: MBB) : BlockFrequency(0);
263}
264
265std::optional<uint64_t> MachineBlockFrequencyInfo::getBlockProfileCount(
266 const MachineBasicBlock *MBB) const {
267 if (!MBFI)
268 return std::nullopt;
269
270 const Function &F = MBFI->getFunction()->getFunction();
271 return MBFI->getBlockProfileCount(F, BB: MBB);
272}
273
274std::optional<uint64_t>
275MachineBlockFrequencyInfo::getProfileCountFromFreq(BlockFrequency Freq) const {
276 if (!MBFI)
277 return std::nullopt;
278
279 const Function &F = MBFI->getFunction()->getFunction();
280 return MBFI->getProfileCountFromFreq(F, Freq);
281}
282
283bool MachineBlockFrequencyInfo::isIrrLoopHeader(
284 const MachineBasicBlock *MBB) const {
285 assert(MBFI && "Expected analysis to be available");
286 return MBFI->isIrrLoopHeader(BB: MBB);
287}
288
289void MachineBlockFrequencyInfo::onEdgeSplit(
290 const MachineBasicBlock &NewPredecessor,
291 const MachineBasicBlock &NewSuccessor,
292 const MachineBranchProbabilityInfo &MBPI) {
293 assert(MBFI && "Expected analysis to be available");
294 auto NewSuccFreq = MBFI->getBlockFreq(BB: &NewPredecessor) *
295 MBPI.getEdgeProbability(Src: &NewPredecessor, Dst: &NewSuccessor);
296
297 MBFI->setBlockFreq(BB: &NewSuccessor, Freq: NewSuccFreq);
298}
299
300const MachineFunction *MachineBlockFrequencyInfo::getFunction() const {
301 return MBFI ? MBFI->getFunction() : nullptr;
302}
303
304const MachineBranchProbabilityInfo *MachineBlockFrequencyInfo::getMBPI() const {
305 return MBFI ? &MBFI->getBPI() : nullptr;
306}
307
308BlockFrequency MachineBlockFrequencyInfo::getEntryFreq() const {
309 return MBFI ? MBFI->getEntryFreq() : BlockFrequency(0);
310}
311
312Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
313 BlockFrequency Freq) {
314 return Printable([&MBFI, Freq](raw_ostream &OS) {
315 printRelativeBlockFreq(OS, EntryFreq: MBFI.getEntryFreq(), Freq);
316 });
317}
318
319Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
320 const MachineBasicBlock &MBB) {
321 return printBlockFreq(MBFI, Freq: MBFI.getBlockFreq(MBB: &MBB));
322}
323