xref: /llvm-project/llvm/include/llvm/Analysis/BlockFrequencyInfo.h (revision 0da2ba811ac8a01509bc533428941fb9519c0715)
1 //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
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 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
14 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
15 
16 #include "llvm/IR/PassManager.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/BlockFrequency.h"
19 #include "llvm/Support/Printable.h"
20 #include <cstdint>
21 #include <memory>
22 #include <optional>
23 
24 namespace llvm {
25 
26 class BasicBlock;
27 class BranchProbabilityInfo;
28 class LoopInfo;
29 class Module;
30 class raw_ostream;
31 template <class BlockT> class BlockFrequencyInfoImpl;
32 
33 enum PGOViewCountsType { PGOVCT_None, PGOVCT_Graph, PGOVCT_Text };
34 
35 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
36 /// estimate IR basic block frequencies.
37 class BlockFrequencyInfo {
38   using ImplType = BlockFrequencyInfoImpl<BasicBlock>;
39 
40   std::unique_ptr<ImplType> BFI;
41 
42 public:
43   BlockFrequencyInfo();
44   BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
45                      const LoopInfo &LI);
46   BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
47   BlockFrequencyInfo &operator=(const BlockFrequencyInfo &) = delete;
48   BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
49   BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
50   ~BlockFrequencyInfo();
51 
52   /// Handle invalidation explicitly.
53   bool invalidate(Function &F, const PreservedAnalyses &PA,
54                   FunctionAnalysisManager::Invalidator &);
55 
56   const Function *getFunction() const;
57   const BranchProbabilityInfo *getBPI() const;
58   void view(StringRef = "BlockFrequencyDAGs") const;
59 
60   /// getblockFreq - Return block frequency. Return 0 if we don't have the
61   /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
62   /// means that we should not rely on the value itself, but only on the
63   /// comparison to the other block frequencies. We do this to avoid using of
64   /// floating points.
65   BlockFrequency getBlockFreq(const BasicBlock *BB) const;
66 
67   /// Returns the estimated profile count of \p BB.
68   /// This computes the relative block frequency of \p BB and multiplies it by
69   /// the enclosing function's count (if available) and returns the value.
70   std::optional<uint64_t>
71   getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic = false) const;
72 
73   /// Returns the estimated profile count of \p Freq.
74   /// This uses the frequency \p Freq and multiplies it by
75   /// the enclosing function's count (if available) and returns the value.
76   std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency Freq) const;
77 
78   /// Returns true if \p BB is an irreducible loop header
79   /// block. Otherwise false.
80   bool isIrrLoopHeader(const BasicBlock *BB);
81 
82   // Set the frequency of the given basic block.
83   void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq);
84 
85   /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
86   /// of the blocks in \p BlocksToScale such that their frequencies relative
87   /// to \p ReferenceBB remain unchanged.
88   void setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq,
89                             SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
90 
91   /// calculate - compute block frequency info for the given function.
92   void calculate(const Function &F, const BranchProbabilityInfo &BPI,
93                  const LoopInfo &LI);
94 
95   BlockFrequency getEntryFreq() const;
96   void releaseMemory();
97   void print(raw_ostream &OS) const;
98 
99   // Compare to the other BFI and verify they match.
100   void verifyMatch(BlockFrequencyInfo &Other) const;
101 };
102 
103 /// Print the block frequency @p Freq relative to the current functions entry
104 /// frequency. Returns a Printable object that can be piped via `<<` to a
105 /// `raw_ostream`.
106 Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq);
107 
108 /// Convenience function equivalent to calling
109 /// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
110 Printable printBlockFreq(const BlockFrequencyInfo &BFI, const BasicBlock &BB);
111 
112 /// Analysis pass which computes \c BlockFrequencyInfo.
113 class BlockFrequencyAnalysis
114     : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
115   friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
116 
117   static AnalysisKey Key;
118 
119 public:
120   /// Provide the result type for this analysis pass.
121   using Result = BlockFrequencyInfo;
122 
123   /// Run the analysis pass over a function and produce BFI.
124   Result run(Function &F, FunctionAnalysisManager &AM);
125 };
126 
127 /// Printer pass for the \c BlockFrequencyInfo results.
128 class BlockFrequencyPrinterPass
129     : public PassInfoMixin<BlockFrequencyPrinterPass> {
130   raw_ostream &OS;
131 
132 public:
133   explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
134 
135   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
136 
137   static bool isRequired() { return true; }
138 };
139 
140 /// Legacy analysis pass which computes \c BlockFrequencyInfo.
141 class BlockFrequencyInfoWrapperPass : public FunctionPass {
142   BlockFrequencyInfo BFI;
143 
144 public:
145   static char ID;
146 
147   BlockFrequencyInfoWrapperPass();
148   ~BlockFrequencyInfoWrapperPass() override;
149 
150   BlockFrequencyInfo &getBFI() { return BFI; }
151   const BlockFrequencyInfo &getBFI() const { return BFI; }
152 
153   void getAnalysisUsage(AnalysisUsage &AU) const override;
154 
155   bool runOnFunction(Function &F) override;
156   void releaseMemory() override;
157   void print(raw_ostream &OS, const Module *M) const override;
158 };
159 
160 } // end namespace llvm
161 
162 #endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
163