xref: /freebsd/contrib/llvm-project/llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- MachineBlockFrequencyInfo.h - MBB 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_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
14 #define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
15 
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachinePassManager.h"
18 #include "llvm/Support/BlockFrequency.h"
19 #include "llvm/Support/Compiler.h"
20 #include <cstdint>
21 #include <memory>
22 #include <optional>
23 
24 namespace llvm {
25 
26 template <class BlockT> class BlockFrequencyInfoImpl;
27 class MachineBasicBlock;
28 class MachineBranchProbabilityInfo;
29 class MachineFunction;
30 class MachineLoopInfo;
31 class raw_ostream;
32 
33 /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
34 /// to estimate machine basic block frequencies.
35 class MachineBlockFrequencyInfo {
36   using ImplType = BlockFrequencyInfoImpl<MachineBasicBlock>;
37   std::unique_ptr<ImplType> MBFI;
38 
39 public:
40   LLVM_ABI MachineBlockFrequencyInfo(); // Legacy pass manager only.
41   LLVM_ABI explicit MachineBlockFrequencyInfo(
42       MachineFunction &F, MachineBranchProbabilityInfo &MBPI,
43       MachineLoopInfo &MLI);
44   LLVM_ABI MachineBlockFrequencyInfo(MachineBlockFrequencyInfo &&);
45   LLVM_ABI ~MachineBlockFrequencyInfo();
46 
47   /// Handle invalidation explicitly.
48   LLVM_ABI bool invalidate(MachineFunction &F, const PreservedAnalyses &PA,
49                            MachineFunctionAnalysisManager::Invalidator &);
50 
51   /// calculate - compute block frequency info for the given function.
52   LLVM_ABI void calculate(const MachineFunction &F,
53                           const MachineBranchProbabilityInfo &MBPI,
54                           const MachineLoopInfo &MLI);
55 
56   LLVM_ABI void print(raw_ostream &OS);
57 
58   LLVM_ABI void releaseMemory();
59 
60   /// getblockFreq - Return block frequency. Return 0 if we don't have the
61   /// information. Please note that initial frequency is equal to 1024. It means
62   /// that we should not rely on the value itself, but only on the comparison to
63   /// the other block frequencies. We do this to avoid using of floating points.
64   /// For example, to get the frequency of a block relative to the entry block,
65   /// divide the integral value returned by this function (the
66   /// BlockFrequency::getFrequency() value) by getEntryFreq().
67   LLVM_ABI BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
68 
69   /// Compute the frequency of the block, relative to the entry block.
70   /// This API assumes getEntryFreq() is non-zero.
getBlockFreqRelativeToEntryBlock(const MachineBasicBlock * MBB)71   double getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const {
72     assert(getEntryFreq() != BlockFrequency(0) &&
73            "getEntryFreq() should not return 0 here!");
74     return static_cast<double>(getBlockFreq(MBB).getFrequency()) /
75            static_cast<double>(getEntryFreq().getFrequency());
76   }
77 
78   LLVM_ABI std::optional<uint64_t>
79   getBlockProfileCount(const MachineBasicBlock *MBB) const;
80   LLVM_ABI std::optional<uint64_t>
81   getProfileCountFromFreq(BlockFrequency Freq) const;
82 
83   LLVM_ABI bool isIrrLoopHeader(const MachineBasicBlock *MBB) const;
84 
85   /// incrementally calculate block frequencies when we split edges, to avoid
86   /// full CFG traversal.
87   LLVM_ABI void onEdgeSplit(const MachineBasicBlock &NewPredecessor,
88                             const MachineBasicBlock &NewSuccessor,
89                             const MachineBranchProbabilityInfo &MBPI);
90 
91   LLVM_ABI const MachineFunction *getFunction() const;
92   LLVM_ABI const MachineBranchProbabilityInfo *getMBPI() const;
93 
94   /// Pop up a ghostview window with the current block frequency propagation
95   /// rendered using dot.
96   LLVM_ABI void view(const Twine &Name, bool isSimple = true) const;
97 
98   /// Divide a block's BlockFrequency::getFrequency() value by this value to
99   /// obtain the entry block - relative frequency of said block.
100   LLVM_ABI BlockFrequency getEntryFreq() 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 LLVM_ABI Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
107                                   BlockFrequency Freq);
108 
109 /// Convenience function equivalent to calling
110 /// `printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB))`.
111 LLVM_ABI Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
112                                   const MachineBasicBlock &MBB);
113 
114 class MachineBlockFrequencyAnalysis
115     : public AnalysisInfoMixin<MachineBlockFrequencyAnalysis> {
116   friend AnalysisInfoMixin<MachineBlockFrequencyAnalysis>;
117   LLVM_ABI static AnalysisKey Key;
118 
119 public:
120   using Result = MachineBlockFrequencyInfo;
121 
122   LLVM_ABI Result run(MachineFunction &MF,
123                       MachineFunctionAnalysisManager &MFAM);
124 };
125 
126 /// Printer pass for the \c MachineBlockFrequencyInfo results.
127 class MachineBlockFrequencyPrinterPass
128     : public PassInfoMixin<MachineBlockFrequencyPrinterPass> {
129   raw_ostream &OS;
130 
131 public:
MachineBlockFrequencyPrinterPass(raw_ostream & OS)132   explicit MachineBlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
133 
134   LLVM_ABI PreservedAnalyses run(MachineFunction &MF,
135                                  MachineFunctionAnalysisManager &MFAM);
136 
isRequired()137   static bool isRequired() { return true; }
138 };
139 
140 class LLVM_ABI MachineBlockFrequencyInfoWrapperPass
141     : public MachineFunctionPass {
142   MachineBlockFrequencyInfo MBFI;
143 
144 public:
145   static char ID;
146 
147   MachineBlockFrequencyInfoWrapperPass();
148 
149   void getAnalysisUsage(AnalysisUsage &AU) const override;
150 
151   bool runOnMachineFunction(MachineFunction &F) override;
152 
releaseMemory()153   void releaseMemory() override { MBFI.releaseMemory(); }
154 
getMBFI()155   MachineBlockFrequencyInfo &getMBFI() { return MBFI; }
156 
getMBFI()157   const MachineBlockFrequencyInfo &getMBFI() const { return MBFI; }
158 };
159 } // end namespace llvm
160 
161 #endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
162