File indexing completed on 2026-05-10 08:43:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
0014 #define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
0015
0016 #include "llvm/CodeGen/MachineFunctionPass.h"
0017 #include "llvm/CodeGen/MachinePassManager.h"
0018 #include "llvm/Support/BlockFrequency.h"
0019 #include <cstdint>
0020 #include <memory>
0021 #include <optional>
0022
0023 namespace llvm {
0024
0025 template <class BlockT> class BlockFrequencyInfoImpl;
0026 class MachineBasicBlock;
0027 class MachineBranchProbabilityInfo;
0028 class MachineFunction;
0029 class MachineLoopInfo;
0030 class raw_ostream;
0031
0032
0033
0034 class MachineBlockFrequencyInfo {
0035 using ImplType = BlockFrequencyInfoImpl<MachineBasicBlock>;
0036 std::unique_ptr<ImplType> MBFI;
0037
0038 public:
0039 MachineBlockFrequencyInfo();
0040 explicit MachineBlockFrequencyInfo(MachineFunction &F,
0041 MachineBranchProbabilityInfo &MBPI,
0042 MachineLoopInfo &MLI);
0043 MachineBlockFrequencyInfo(MachineBlockFrequencyInfo &&);
0044 ~MachineBlockFrequencyInfo();
0045
0046
0047 bool invalidate(MachineFunction &F, const PreservedAnalyses &PA,
0048 MachineFunctionAnalysisManager::Invalidator &);
0049
0050
0051 void calculate(const MachineFunction &F,
0052 const MachineBranchProbabilityInfo &MBPI,
0053 const MachineLoopInfo &MLI);
0054
0055 void print(raw_ostream &OS);
0056
0057 void releaseMemory();
0058
0059
0060
0061
0062
0063
0064
0065
0066 BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
0067
0068
0069
0070 double getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const {
0071 assert(getEntryFreq() != BlockFrequency(0) &&
0072 "getEntryFreq() should not return 0 here!");
0073 return static_cast<double>(getBlockFreq(MBB).getFrequency()) /
0074 static_cast<double>(getEntryFreq().getFrequency());
0075 }
0076
0077 std::optional<uint64_t>
0078 getBlockProfileCount(const MachineBasicBlock *MBB) const;
0079 std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency Freq) const;
0080
0081 bool isIrrLoopHeader(const MachineBasicBlock *MBB) const;
0082
0083
0084
0085 void onEdgeSplit(const MachineBasicBlock &NewPredecessor,
0086 const MachineBasicBlock &NewSuccessor,
0087 const MachineBranchProbabilityInfo &MBPI);
0088
0089 const MachineFunction *getFunction() const;
0090 const MachineBranchProbabilityInfo *getMBPI() const;
0091
0092
0093
0094 void view(const Twine &Name, bool isSimple = true) const;
0095
0096
0097
0098 BlockFrequency getEntryFreq() const;
0099 };
0100
0101
0102
0103
0104 Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
0105 BlockFrequency Freq);
0106
0107
0108
0109 Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
0110 const MachineBasicBlock &MBB);
0111
0112 class MachineBlockFrequencyAnalysis
0113 : public AnalysisInfoMixin<MachineBlockFrequencyAnalysis> {
0114 friend AnalysisInfoMixin<MachineBlockFrequencyAnalysis>;
0115 static AnalysisKey Key;
0116
0117 public:
0118 using Result = MachineBlockFrequencyInfo;
0119
0120 Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
0121 };
0122
0123
0124 class MachineBlockFrequencyPrinterPass
0125 : public PassInfoMixin<MachineBlockFrequencyPrinterPass> {
0126 raw_ostream &OS;
0127
0128 public:
0129 explicit MachineBlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
0130
0131 PreservedAnalyses run(MachineFunction &MF,
0132 MachineFunctionAnalysisManager &MFAM);
0133
0134 static bool isRequired() { return true; }
0135 };
0136
0137 class MachineBlockFrequencyInfoWrapperPass : public MachineFunctionPass {
0138 MachineBlockFrequencyInfo MBFI;
0139
0140 public:
0141 static char ID;
0142
0143 MachineBlockFrequencyInfoWrapperPass();
0144
0145 void getAnalysisUsage(AnalysisUsage &AU) const override;
0146
0147 bool runOnMachineFunction(MachineFunction &F) override;
0148
0149 void releaseMemory() override { MBFI.releaseMemory(); }
0150
0151 MachineBlockFrequencyInfo &getMBFI() { return MBFI; }
0152
0153 const MachineBlockFrequencyInfo &getMBFI() const { return MBFI; }
0154 };
0155 }
0156
0157 #endif