1 //===- MachineBlockFrequencyInfo.h - MBB Frequency Analysis -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Loops should be simplified before this analysis.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
15 #define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Support/BlockFrequency.h"
25 template <class BlockT> class BlockFrequencyInfoImpl;
26 class MachineBasicBlock;
27 class MachineBranchProbabilityInfo;
28 class MachineFunction;
29 class MachineLoopInfo;
32 /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
33 /// to estimate machine basic block frequencies.
34 class MachineBlockFrequencyInfo : public MachineFunctionPass {
35 using ImplType = BlockFrequencyInfoImpl<MachineBasicBlock>;
36 std::unique_ptr<ImplType> MBFI;
41 MachineBlockFrequencyInfo();
42 ~MachineBlockFrequencyInfo() override;
44 void getAnalysisUsage(AnalysisUsage &AU) const override;
46 bool runOnMachineFunction(MachineFunction &F) override;
48 /// calculate - compute block frequency info for the given function.
49 void calculate(const MachineFunction &F,
50 const MachineBranchProbabilityInfo &MBPI,
51 const MachineLoopInfo &MLI);
53 void releaseMemory() override;
55 /// getblockFreq - Return block frequency. Return 0 if we don't have the
56 /// information. Please note that initial frequency is equal to 1024. It means
57 /// that we should not rely on the value itself, but only on the comparison to
58 /// the other block frequencies. We do this to avoid using of floating points.
60 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
62 Optional<uint64_t> getBlockProfileCount(const MachineBasicBlock *MBB) const;
63 Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
65 const MachineFunction *getFunction() const;
66 const MachineBranchProbabilityInfo *getMBPI() const;
67 void view(const Twine &Name, bool isSimple = true) const;
69 // Print the block frequency Freq to OS using the current functions entry
70 // frequency to convert freq into a relative decimal form.
71 raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
73 // Convenience method that attempts to look up the frequency associated with
74 // BB and print it to OS.
75 raw_ostream &printBlockFreq(raw_ostream &OS,
76 const MachineBasicBlock *MBB) const;
78 uint64_t getEntryFreq() const;
81 } // end namespace llvm
83 #endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H