1 //===- BlockFrequencyInfo.cpp - Block Frequency Analysis ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Loops should be simplified before this analysis.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/BlockFrequencyInfo.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/iterator.h"
17 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
18 #include "llvm/Analysis/BranchProbabilityInfo.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/IR/CFG.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/GraphWriter.h"
26 #include "llvm/Support/raw_ostream.h"
33 #define DEBUG_TYPE "block-freq"
35 static cl::opt
<GVDAGType
> ViewBlockFreqPropagationDAG(
36 "view-block-freq-propagation-dags", cl::Hidden
,
37 cl::desc("Pop up a window to show a dag displaying how block "
38 "frequencies propagation through the CFG."),
39 cl::values(clEnumValN(GVDT_None
, "none", "do not display graphs."),
40 clEnumValN(GVDT_Fraction
, "fraction",
41 "display a graph using the "
42 "fractional block frequency representation."),
43 clEnumValN(GVDT_Integer
, "integer",
44 "display a graph using the raw "
45 "integer fractional block frequency representation."),
46 clEnumValN(GVDT_Count
, "count", "display a graph using the real "
47 "profile count if available.")));
50 ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden
,
51 cl::desc("The option to specify "
52 "the name of the function "
53 "whose CFG will be displayed."));
56 ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden
,
57 cl::desc("An integer in percent used to specify "
58 "the hot blocks/edges to be displayed "
59 "in red: a block or edge whose frequency "
60 "is no less than the max frequency of the "
61 "function multiplied by this percent."));
63 // Command line option to turn on CFG dot or text dump after profile annotation.
64 cl::opt
<PGOViewCountsType
> PGOViewCounts(
65 "pgo-view-counts", cl::Hidden
,
66 cl::desc("A boolean option to show CFG dag or text with "
67 "block profile counts and branch probabilities "
68 "right after PGO profile annotation step. The "
69 "profile counts are computed using branch "
70 "probabilities from the runtime profile data and "
71 "block frequency propagation algorithm. To view "
72 "the raw counts from the profile, use option "
73 "-pgo-view-raw-counts instead. To limit graph "
74 "display to only one function, use filtering option "
75 "-view-bfi-func-name."),
76 cl::values(clEnumValN(PGOVCT_None
, "none", "do not show."),
77 clEnumValN(PGOVCT_Graph
, "graph", "show a graph."),
78 clEnumValN(PGOVCT_Text
, "text", "show in text.")));
80 static cl::opt
<bool> PrintBlockFreq(
81 "print-bfi", cl::init(false), cl::Hidden
,
82 cl::desc("Print the block frequency info."));
84 cl::opt
<std::string
> PrintBlockFreqFuncName(
85 "print-bfi-func-name", cl::Hidden
,
86 cl::desc("The option to specify the name of the function "
87 "whose block frequency info is printed."));
91 static GVDAGType
getGVDT() {
92 if (PGOViewCounts
== PGOVCT_Graph
)
94 return ViewBlockFreqPropagationDAG
;
98 struct GraphTraits
<BlockFrequencyInfo
*> {
99 using NodeRef
= const BasicBlock
*;
100 using ChildIteratorType
= succ_const_iterator
;
101 using nodes_iterator
= pointer_iterator
<Function::const_iterator
>;
103 static NodeRef
getEntryNode(const BlockFrequencyInfo
*G
) {
104 return &G
->getFunction()->front();
107 static ChildIteratorType
child_begin(const NodeRef N
) {
108 return succ_begin(N
);
111 static ChildIteratorType
child_end(const NodeRef N
) { return succ_end(N
); }
113 static nodes_iterator
nodes_begin(const BlockFrequencyInfo
*G
) {
114 return nodes_iterator(G
->getFunction()->begin());
117 static nodes_iterator
nodes_end(const BlockFrequencyInfo
*G
) {
118 return nodes_iterator(G
->getFunction()->end());
122 using BFIDOTGTraitsBase
=
123 BFIDOTGraphTraitsBase
<BlockFrequencyInfo
, BranchProbabilityInfo
>;
126 struct DOTGraphTraits
<BlockFrequencyInfo
*> : public BFIDOTGTraitsBase
{
127 explicit DOTGraphTraits(bool isSimple
= false)
128 : BFIDOTGTraitsBase(isSimple
) {}
130 std::string
getNodeLabel(const BasicBlock
*Node
,
131 const BlockFrequencyInfo
*Graph
) {
133 return BFIDOTGTraitsBase::getNodeLabel(Node
, Graph
, getGVDT());
136 std::string
getNodeAttributes(const BasicBlock
*Node
,
137 const BlockFrequencyInfo
*Graph
) {
138 return BFIDOTGTraitsBase::getNodeAttributes(Node
, Graph
,
142 std::string
getEdgeAttributes(const BasicBlock
*Node
, EdgeIter EI
,
143 const BlockFrequencyInfo
*BFI
) {
144 return BFIDOTGTraitsBase::getEdgeAttributes(Node
, EI
, BFI
, BFI
->getBPI(),
149 } // end namespace llvm
151 BlockFrequencyInfo::BlockFrequencyInfo() = default;
153 BlockFrequencyInfo::BlockFrequencyInfo(const Function
&F
,
154 const BranchProbabilityInfo
&BPI
,
155 const LoopInfo
&LI
) {
156 calculate(F
, BPI
, LI
);
159 BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo
&&Arg
)
160 : BFI(std::move(Arg
.BFI
)) {}
162 BlockFrequencyInfo
&BlockFrequencyInfo::operator=(BlockFrequencyInfo
&&RHS
) {
164 BFI
= std::move(RHS
.BFI
);
168 // Explicitly define the default constructor otherwise it would be implicitly
169 // defined at the first ODR-use which is the BFI member in the
170 // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
171 // template instantiated which is not available in the header.
172 BlockFrequencyInfo::~BlockFrequencyInfo() = default;
174 bool BlockFrequencyInfo::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
175 FunctionAnalysisManager::Invalidator
&) {
176 // Check whether the analysis, all analyses on functions, or the function's
177 // CFG have been preserved.
178 auto PAC
= PA
.getChecker
<BlockFrequencyAnalysis
>();
179 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Function
>>() ||
180 PAC
.preservedSet
<CFGAnalyses
>());
183 void BlockFrequencyInfo::calculate(const Function
&F
,
184 const BranchProbabilityInfo
&BPI
,
185 const LoopInfo
&LI
) {
187 BFI
.reset(new ImplType
);
188 BFI
->calculate(F
, BPI
, LI
);
189 if (ViewBlockFreqPropagationDAG
!= GVDT_None
&&
190 (ViewBlockFreqFuncName
.empty() ||
191 F
.getName().equals(ViewBlockFreqFuncName
))) {
194 if (PrintBlockFreq
&&
195 (PrintBlockFreqFuncName
.empty() ||
196 F
.getName().equals(PrintBlockFreqFuncName
))) {
201 BlockFrequency
BlockFrequencyInfo::getBlockFreq(const BasicBlock
*BB
) const {
202 return BFI
? BFI
->getBlockFreq(BB
) : 0;
206 BlockFrequencyInfo::getBlockProfileCount(const BasicBlock
*BB
) const {
210 return BFI
->getBlockProfileCount(*getFunction(), BB
);
214 BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq
) const {
217 return BFI
->getProfileCountFromFreq(*getFunction(), Freq
);
220 bool BlockFrequencyInfo::isIrrLoopHeader(const BasicBlock
*BB
) {
221 assert(BFI
&& "Expected analysis to be available");
222 return BFI
->isIrrLoopHeader(BB
);
225 void BlockFrequencyInfo::setBlockFreq(const BasicBlock
*BB
, uint64_t Freq
) {
226 assert(BFI
&& "Expected analysis to be available");
227 BFI
->setBlockFreq(BB
, Freq
);
230 void BlockFrequencyInfo::setBlockFreqAndScale(
231 const BasicBlock
*ReferenceBB
, uint64_t Freq
,
232 SmallPtrSetImpl
<BasicBlock
*> &BlocksToScale
) {
233 assert(BFI
&& "Expected analysis to be available");
234 // Use 128 bits APInt to avoid overflow.
235 APInt
NewFreq(128, Freq
);
236 APInt
OldFreq(128, BFI
->getBlockFreq(ReferenceBB
).getFrequency());
237 APInt
BBFreq(128, 0);
238 for (auto *BB
: BlocksToScale
) {
239 BBFreq
= BFI
->getBlockFreq(BB
).getFrequency();
240 // Multiply first by NewFreq and then divide by OldFreq
241 // to minimize loss of precision.
243 // udiv is an expensive operation in the general case. If this ends up being
244 // a hot spot, one of the options proposed in
245 // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
246 BBFreq
= BBFreq
.udiv(OldFreq
);
247 BFI
->setBlockFreq(BB
, BBFreq
.getLimitedValue());
249 BFI
->setBlockFreq(ReferenceBB
, Freq
);
252 /// Pop up a ghostview window with the current block frequency propagation
253 /// rendered using dot.
254 void BlockFrequencyInfo::view(StringRef title
) const {
255 ViewGraph(const_cast<BlockFrequencyInfo
*>(this), title
);
258 const Function
*BlockFrequencyInfo::getFunction() const {
259 return BFI
? BFI
->getFunction() : nullptr;
262 const BranchProbabilityInfo
*BlockFrequencyInfo::getBPI() const {
263 return BFI
? &BFI
->getBPI() : nullptr;
266 raw_ostream
&BlockFrequencyInfo::
267 printBlockFreq(raw_ostream
&OS
, const BlockFrequency Freq
) const {
268 return BFI
? BFI
->printBlockFreq(OS
, Freq
) : OS
;
272 BlockFrequencyInfo::printBlockFreq(raw_ostream
&OS
,
273 const BasicBlock
*BB
) const {
274 return BFI
? BFI
->printBlockFreq(OS
, BB
) : OS
;
277 uint64_t BlockFrequencyInfo::getEntryFreq() const {
278 return BFI
? BFI
->getEntryFreq() : 0;
281 void BlockFrequencyInfo::releaseMemory() { BFI
.reset(); }
283 void BlockFrequencyInfo::print(raw_ostream
&OS
) const {
288 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass
, "block-freq",
289 "Block Frequency Analysis", true, true)
290 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass
)
291 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
292 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass
, "block-freq",
293 "Block Frequency Analysis", true, true)
295 char BlockFrequencyInfoWrapperPass::ID
= 0;
297 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
299 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
302 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default;
304 void BlockFrequencyInfoWrapperPass::print(raw_ostream
&OS
,
305 const Module
*) const {
309 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
310 AU
.addRequired
<BranchProbabilityInfoWrapperPass
>();
311 AU
.addRequired
<LoopInfoWrapperPass
>();
312 AU
.setPreservesAll();
315 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI
.releaseMemory(); }
317 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function
&F
) {
318 BranchProbabilityInfo
&BPI
=
319 getAnalysis
<BranchProbabilityInfoWrapperPass
>().getBPI();
320 LoopInfo
&LI
= getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
321 BFI
.calculate(F
, BPI
, LI
);
325 AnalysisKey
BlockFrequencyAnalysis::Key
;
326 BlockFrequencyInfo
BlockFrequencyAnalysis::run(Function
&F
,
327 FunctionAnalysisManager
&AM
) {
328 BlockFrequencyInfo BFI
;
329 BFI
.calculate(F
, AM
.getResult
<BranchProbabilityAnalysis
>(F
),
330 AM
.getResult
<LoopAnalysis
>(F
));
335 BlockFrequencyPrinterPass::run(Function
&F
, FunctionAnalysisManager
&AM
) {
336 OS
<< "Printing analysis results of BFI for function "
337 << "'" << F
.getName() << "':"
339 AM
.getResult
<BlockFrequencyAnalysis
>(F
).print(OS
);
340 return PreservedAnalyses::all();