1 //===- MachineBlockFrequencyInfo.h - MBB Frequency Analysis -----*- C++ -*-===//
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 #ifndef LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
14 #define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/Support/BlockFrequency.h"
24 template <class BlockT> class BlockFrequencyInfoImpl;
25 class MachineBasicBlock;
26 class MachineBranchProbabilityInfo;
27 class MachineFunction;
28 class MachineLoopInfo;
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;
40 MachineBlockFrequencyInfo();
41 explicit MachineBlockFrequencyInfo(MachineFunction &F,
42 MachineBranchProbabilityInfo &MBPI,
43 MachineLoopInfo &MLI);
44 ~MachineBlockFrequencyInfo() override;
46 void getAnalysisUsage(AnalysisUsage &AU) const override;
48 bool runOnMachineFunction(MachineFunction &F) override;
50 /// calculate - compute block frequency info for the given function.
51 void calculate(const MachineFunction &F,
52 const MachineBranchProbabilityInfo &MBPI,
53 const MachineLoopInfo &MLI);
55 void releaseMemory() override;
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.
62 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
64 Optional<uint64_t> getBlockProfileCount(const MachineBasicBlock *MBB) const;
65 Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
67 bool isIrrLoopHeader(const MachineBasicBlock *MBB);
69 void setBlockFreq(const MachineBasicBlock *MBB, uint64_t Freq);
71 const MachineFunction *getFunction() const;
72 const MachineBranchProbabilityInfo *getMBPI() const;
73 void view(const Twine &Name, bool isSimple = true) const;
75 // Print the block frequency Freq to OS using the current functions entry
76 // frequency to convert freq into a relative decimal form.
77 raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
79 // Convenience method that attempts to look up the frequency associated with
80 // BB and print it to OS.
81 raw_ostream &printBlockFreq(raw_ostream &OS,
82 const MachineBasicBlock *MBB) const;
84 uint64_t getEntryFreq() const;
87 } // end namespace llvm
89 #endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H