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/InitializePasses.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/GraphWriter.h"
27 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "block-freq"
36 static cl::opt
<GVDAGType
> ViewBlockFreqPropagationDAG(
37 "view-block-freq-propagation-dags", cl::Hidden
,
38 cl::desc("Pop up a window to show a dag displaying how block "
39 "frequencies propagation through the CFG."),
40 cl::values(clEnumValN(GVDT_None
, "none", "do not display graphs."),
41 clEnumValN(GVDT_Fraction
, "fraction",
42 "display a graph using the "
43 "fractional block frequency representation."),
44 clEnumValN(GVDT_Integer
, "integer",
45 "display a graph using the raw "
46 "integer fractional block frequency representation."),
47 clEnumValN(GVDT_Count
, "count", "display a graph using the real "
48 "profile count if available.")));
52 ViewBlockFreqFuncName("view-bfi-func-name", cl::Hidden
,
53 cl::desc("The option to specify "
54 "the name of the function "
55 "whose CFG will be displayed."));
58 ViewHotFreqPercent("view-hot-freq-percent", cl::init(10), cl::Hidden
,
59 cl::desc("An integer in percent used to specify "
60 "the hot blocks/edges to be displayed "
61 "in red: a block or edge whose frequency "
62 "is no less than the max frequency of the "
63 "function multiplied by this percent."));
65 // Command line option to turn on CFG dot or text dump after profile annotation.
66 cl::opt
<PGOViewCountsType
> PGOViewCounts(
67 "pgo-view-counts", cl::Hidden
,
68 cl::desc("A boolean option to show CFG dag or text with "
69 "block profile counts and branch probabilities "
70 "right after PGO profile annotation step. The "
71 "profile counts are computed using branch "
72 "probabilities from the runtime profile data and "
73 "block frequency propagation algorithm. To view "
74 "the raw counts from the profile, use option "
75 "-pgo-view-raw-counts instead. To limit graph "
76 "display to only one function, use filtering option "
77 "-view-bfi-func-name."),
78 cl::values(clEnumValN(PGOVCT_None
, "none", "do not show."),
79 clEnumValN(PGOVCT_Graph
, "graph", "show a graph."),
80 clEnumValN(PGOVCT_Text
, "text", "show in text.")));
82 static cl::opt
<bool> PrintBlockFreq(
83 "print-bfi", cl::init(false), cl::Hidden
,
84 cl::desc("Print the block frequency info."));
86 cl::opt
<std::string
> PrintBlockFreqFuncName(
87 "print-bfi-func-name", cl::Hidden
,
88 cl::desc("The option to specify the name of the function "
89 "whose block frequency info is printed."));
94 static GVDAGType
getGVDT() {
95 if (PGOViewCounts
== PGOVCT_Graph
)
97 return ViewBlockFreqPropagationDAG
;
101 struct GraphTraits
<BlockFrequencyInfo
*> {
102 using NodeRef
= const BasicBlock
*;
103 using ChildIteratorType
= const_succ_iterator
;
104 using nodes_iterator
= pointer_iterator
<Function::const_iterator
>;
106 static NodeRef
getEntryNode(const BlockFrequencyInfo
*G
) {
107 return &G
->getFunction()->front();
110 static ChildIteratorType
child_begin(const NodeRef N
) {
111 return succ_begin(N
);
114 static ChildIteratorType
child_end(const NodeRef N
) { return succ_end(N
); }
116 static nodes_iterator
nodes_begin(const BlockFrequencyInfo
*G
) {
117 return nodes_iterator(G
->getFunction()->begin());
120 static nodes_iterator
nodes_end(const BlockFrequencyInfo
*G
) {
121 return nodes_iterator(G
->getFunction()->end());
125 using BFIDOTGTraitsBase
=
126 BFIDOTGraphTraitsBase
<BlockFrequencyInfo
, BranchProbabilityInfo
>;
129 struct DOTGraphTraits
<BlockFrequencyInfo
*> : public BFIDOTGTraitsBase
{
130 explicit DOTGraphTraits(bool isSimple
= false)
131 : BFIDOTGTraitsBase(isSimple
) {}
133 std::string
getNodeLabel(const BasicBlock
*Node
,
134 const BlockFrequencyInfo
*Graph
) {
136 return BFIDOTGTraitsBase::getNodeLabel(Node
, Graph
, getGVDT());
139 std::string
getNodeAttributes(const BasicBlock
*Node
,
140 const BlockFrequencyInfo
*Graph
) {
141 return BFIDOTGTraitsBase::getNodeAttributes(Node
, Graph
,
145 std::string
getEdgeAttributes(const BasicBlock
*Node
, EdgeIter EI
,
146 const BlockFrequencyInfo
*BFI
) {
147 return BFIDOTGTraitsBase::getEdgeAttributes(Node
, EI
, BFI
, BFI
->getBPI(),
152 } // end namespace llvm
154 BlockFrequencyInfo::BlockFrequencyInfo() = default;
156 BlockFrequencyInfo::BlockFrequencyInfo(const Function
&F
,
157 const BranchProbabilityInfo
&BPI
,
158 const LoopInfo
&LI
) {
159 calculate(F
, BPI
, LI
);
162 BlockFrequencyInfo::BlockFrequencyInfo(BlockFrequencyInfo
&&Arg
)
163 : BFI(std::move(Arg
.BFI
)) {}
165 BlockFrequencyInfo
&BlockFrequencyInfo::operator=(BlockFrequencyInfo
&&RHS
) {
167 BFI
= std::move(RHS
.BFI
);
171 // Explicitly define the default constructor otherwise it would be implicitly
172 // defined at the first ODR-use which is the BFI member in the
173 // LazyBlockFrequencyInfo header. The dtor needs the BlockFrequencyInfoImpl
174 // template instantiated which is not available in the header.
175 BlockFrequencyInfo::~BlockFrequencyInfo() = default;
177 bool BlockFrequencyInfo::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
178 FunctionAnalysisManager::Invalidator
&) {
179 // Check whether the analysis, all analyses on functions, or the function's
180 // CFG have been preserved.
181 auto PAC
= PA
.getChecker
<BlockFrequencyAnalysis
>();
182 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Function
>>() ||
183 PAC
.preservedSet
<CFGAnalyses
>());
186 void BlockFrequencyInfo::calculate(const Function
&F
,
187 const BranchProbabilityInfo
&BPI
,
188 const LoopInfo
&LI
) {
190 BFI
.reset(new ImplType
);
191 BFI
->calculate(F
, BPI
, LI
);
192 if (ViewBlockFreqPropagationDAG
!= GVDT_None
&&
193 (ViewBlockFreqFuncName
.empty() ||
194 F
.getName().equals(ViewBlockFreqFuncName
))) {
197 if (PrintBlockFreq
&&
198 (PrintBlockFreqFuncName
.empty() ||
199 F
.getName().equals(PrintBlockFreqFuncName
))) {
204 BlockFrequency
BlockFrequencyInfo::getBlockFreq(const BasicBlock
*BB
) const {
205 return BFI
? BFI
->getBlockFreq(BB
) : 0;
209 BlockFrequencyInfo::getBlockProfileCount(const BasicBlock
*BB
,
210 bool AllowSynthetic
) const {
214 return BFI
->getBlockProfileCount(*getFunction(), BB
, AllowSynthetic
);
218 BlockFrequencyInfo::getProfileCountFromFreq(uint64_t Freq
) const {
221 return BFI
->getProfileCountFromFreq(*getFunction(), Freq
);
224 bool BlockFrequencyInfo::isIrrLoopHeader(const BasicBlock
*BB
) {
225 assert(BFI
&& "Expected analysis to be available");
226 return BFI
->isIrrLoopHeader(BB
);
229 void BlockFrequencyInfo::setBlockFreq(const BasicBlock
*BB
, uint64_t Freq
) {
230 assert(BFI
&& "Expected analysis to be available");
231 BFI
->setBlockFreq(BB
, Freq
);
234 void BlockFrequencyInfo::setBlockFreqAndScale(
235 const BasicBlock
*ReferenceBB
, uint64_t Freq
,
236 SmallPtrSetImpl
<BasicBlock
*> &BlocksToScale
) {
237 assert(BFI
&& "Expected analysis to be available");
238 // Use 128 bits APInt to avoid overflow.
239 APInt
NewFreq(128, Freq
);
240 APInt
OldFreq(128, BFI
->getBlockFreq(ReferenceBB
).getFrequency());
241 APInt
BBFreq(128, 0);
242 for (auto *BB
: BlocksToScale
) {
243 BBFreq
= BFI
->getBlockFreq(BB
).getFrequency();
244 // Multiply first by NewFreq and then divide by OldFreq
245 // to minimize loss of precision.
247 // udiv is an expensive operation in the general case. If this ends up being
248 // a hot spot, one of the options proposed in
249 // https://reviews.llvm.org/D28535#650071 could be used to avoid this.
250 BBFreq
= BBFreq
.udiv(OldFreq
);
251 BFI
->setBlockFreq(BB
, BBFreq
.getLimitedValue());
253 BFI
->setBlockFreq(ReferenceBB
, Freq
);
256 /// Pop up a ghostview window with the current block frequency propagation
257 /// rendered using dot.
258 void BlockFrequencyInfo::view(StringRef title
) const {
259 ViewGraph(const_cast<BlockFrequencyInfo
*>(this), title
);
262 const Function
*BlockFrequencyInfo::getFunction() const {
263 return BFI
? BFI
->getFunction() : nullptr;
266 const BranchProbabilityInfo
*BlockFrequencyInfo::getBPI() const {
267 return BFI
? &BFI
->getBPI() : nullptr;
270 raw_ostream
&BlockFrequencyInfo::
271 printBlockFreq(raw_ostream
&OS
, const BlockFrequency Freq
) const {
272 return BFI
? BFI
->printBlockFreq(OS
, Freq
) : OS
;
276 BlockFrequencyInfo::printBlockFreq(raw_ostream
&OS
,
277 const BasicBlock
*BB
) const {
278 return BFI
? BFI
->printBlockFreq(OS
, BB
) : OS
;
281 uint64_t BlockFrequencyInfo::getEntryFreq() const {
282 return BFI
? BFI
->getEntryFreq() : 0;
285 void BlockFrequencyInfo::releaseMemory() { BFI
.reset(); }
287 void BlockFrequencyInfo::print(raw_ostream
&OS
) const {
292 void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo
&Other
) const {
294 BFI
->verifyMatch(*Other
.BFI
);
297 INITIALIZE_PASS_BEGIN(BlockFrequencyInfoWrapperPass
, "block-freq",
298 "Block Frequency Analysis", true, true)
299 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass
)
300 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
)
301 INITIALIZE_PASS_END(BlockFrequencyInfoWrapperPass
, "block-freq",
302 "Block Frequency Analysis", true, true)
304 char BlockFrequencyInfoWrapperPass::ID
= 0;
306 BlockFrequencyInfoWrapperPass::BlockFrequencyInfoWrapperPass()
308 initializeBlockFrequencyInfoWrapperPassPass(*PassRegistry::getPassRegistry());
311 BlockFrequencyInfoWrapperPass::~BlockFrequencyInfoWrapperPass() = default;
313 void BlockFrequencyInfoWrapperPass::print(raw_ostream
&OS
,
314 const Module
*) const {
318 void BlockFrequencyInfoWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
319 AU
.addRequired
<BranchProbabilityInfoWrapperPass
>();
320 AU
.addRequired
<LoopInfoWrapperPass
>();
321 AU
.setPreservesAll();
324 void BlockFrequencyInfoWrapperPass::releaseMemory() { BFI
.releaseMemory(); }
326 bool BlockFrequencyInfoWrapperPass::runOnFunction(Function
&F
) {
327 BranchProbabilityInfo
&BPI
=
328 getAnalysis
<BranchProbabilityInfoWrapperPass
>().getBPI();
329 LoopInfo
&LI
= getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
330 BFI
.calculate(F
, BPI
, LI
);
334 AnalysisKey
BlockFrequencyAnalysis::Key
;
335 BlockFrequencyInfo
BlockFrequencyAnalysis::run(Function
&F
,
336 FunctionAnalysisManager
&AM
) {
337 BlockFrequencyInfo BFI
;
338 BFI
.calculate(F
, AM
.getResult
<BranchProbabilityAnalysis
>(F
),
339 AM
.getResult
<LoopAnalysis
>(F
));
344 BlockFrequencyPrinterPass::run(Function
&F
, FunctionAnalysisManager
&AM
) {
345 OS
<< "Printing analysis results of BFI for function "
346 << "'" << F
.getName() << "':"
348 AM
.getResult
<BlockFrequencyAnalysis
>(F
).print(OS
);
349 return PreservedAnalyses::all();