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