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/Support/BlockFrequency.h"
18 #include <cstdint>
19 #include <memory>
20 #include <optional>
21 
22 namespace llvm {
23 
24 template <class BlockT> class BlockFrequencyInfoImpl;
25 class MachineBasicBlock;
26 class MachineBranchProbabilityInfo;
27 class MachineFunction;
28 class MachineLoopInfo;
29 class raw_ostream;
30 
31 /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
32 /// to estimate machine basic block frequencies.
33 class MachineBlockFrequencyInfo : public MachineFunctionPass {
34   using ImplType = BlockFrequencyInfoImpl<MachineBasicBlock>;
35   std::unique_ptr<ImplType> MBFI;
36 
37 public:
38   static char ID;
39 
40   MachineBlockFrequencyInfo();
41   explicit MachineBlockFrequencyInfo(MachineFunction &F,
42                                      MachineBranchProbabilityInfo &MBPI,
43                                      MachineLoopInfo &MLI);
44   ~MachineBlockFrequencyInfo() override;
45 
46   void getAnalysisUsage(AnalysisUsage &AU) const override;
47 
48   bool runOnMachineFunction(MachineFunction &F) override;
49 
50   /// calculate - compute block frequency info for the given function.
51   void calculate(const MachineFunction &F,
52                  const MachineBranchProbabilityInfo &MBPI,
53                  const MachineLoopInfo &MLI);
54 
55   void releaseMemory() override;
56 
57   /// getblockFreq - Return block frequency. Return 0 if we don't have the
58   /// information. Please note that initial frequency is equal to 1024. It means
59   /// that we should not rely on the value itself, but only on the comparison to
60   /// the other block frequencies. We do this to avoid using of floating points.
61   /// For example, to get the frequency of a block relative to the entry block,
62   /// divide the integral value returned by this function (the
63   /// BlockFrequency::getFrequency() value) by getEntryFreq().
64   BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
65 
66   /// Compute the frequency of the block, relative to the entry block.
67   /// This API assumes getEntryFreq() is non-zero.
68   float getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const {
69     return getBlockFreq(MBB).getFrequency() * (1.0f / getEntryFreq());
70   }
71 
72   std::optional<uint64_t>
73   getBlockProfileCount(const MachineBasicBlock *MBB) const;
74   std::optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
75 
76   bool isIrrLoopHeader(const MachineBasicBlock *MBB) const;
77 
78   /// incrementally calculate block frequencies when we split edges, to avoid
79   /// full CFG traversal.
80   void onEdgeSplit(const MachineBasicBlock &NewPredecessor,
81                    const MachineBasicBlock &NewSuccessor,
82                    const MachineBranchProbabilityInfo &MBPI);
83 
84   const MachineFunction *getFunction() const;
85   const MachineBranchProbabilityInfo *getMBPI() const;
86 
87   /// Pop up a ghostview window with the current block frequency propagation
88   /// rendered using dot.
89   void view(const Twine &Name, bool isSimple = true) const;
90 
91   // Print the block frequency Freq to OS using the current functions entry
92   // frequency to convert freq into a relative decimal form.
93   raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
94 
95   // Convenience method that attempts to look up the frequency associated with
96   // BB and print it to OS.
97   raw_ostream &printBlockFreq(raw_ostream &OS,
98                               const MachineBasicBlock *MBB) const;
99 
100   /// Divide a block's BlockFrequency::getFrequency() value by this value to
101   /// obtain the entry block - relative frequency of said block.
102   uint64_t getEntryFreq() const;
103 };
104 
105 } // end namespace llvm
106 
107 #endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
108