File indexing completed on 2026-05-10 08:43:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
0014 #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
0015
0016 #include "llvm/IR/PassManager.h"
0017 #include "llvm/Pass.h"
0018 #include "llvm/Support/BlockFrequency.h"
0019 #include "llvm/Support/Printable.h"
0020 #include <cstdint>
0021 #include <memory>
0022 #include <optional>
0023
0024 namespace llvm {
0025
0026 class BasicBlock;
0027 class BranchProbabilityInfo;
0028 class LoopInfo;
0029 class Module;
0030 class raw_ostream;
0031 template <class BlockT> class BlockFrequencyInfoImpl;
0032
0033 enum PGOViewCountsType { PGOVCT_None, PGOVCT_Graph, PGOVCT_Text };
0034
0035
0036
0037 class BlockFrequencyInfo {
0038 using ImplType = BlockFrequencyInfoImpl<BasicBlock>;
0039
0040 std::unique_ptr<ImplType> BFI;
0041
0042 public:
0043 BlockFrequencyInfo();
0044 BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
0045 const LoopInfo &LI);
0046 BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
0047 BlockFrequencyInfo &operator=(const BlockFrequencyInfo &) = delete;
0048 BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
0049 BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
0050 ~BlockFrequencyInfo();
0051
0052
0053 bool invalidate(Function &F, const PreservedAnalyses &PA,
0054 FunctionAnalysisManager::Invalidator &);
0055
0056 const Function *getFunction() const;
0057 const BranchProbabilityInfo *getBPI() const;
0058 void view(StringRef = "BlockFrequencyDAGs") const;
0059
0060
0061
0062
0063
0064
0065 BlockFrequency getBlockFreq(const BasicBlock *BB) const;
0066
0067
0068
0069
0070 std::optional<uint64_t>
0071 getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic = false) const;
0072
0073
0074
0075
0076 std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency Freq) const;
0077
0078
0079
0080 bool isIrrLoopHeader(const BasicBlock *BB);
0081
0082
0083 void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq);
0084
0085
0086
0087
0088 void setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq,
0089 SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
0090
0091
0092 void calculate(const Function &F, const BranchProbabilityInfo &BPI,
0093 const LoopInfo &LI);
0094
0095 BlockFrequency getEntryFreq() const;
0096 void releaseMemory();
0097 void print(raw_ostream &OS) const;
0098
0099
0100 void verifyMatch(BlockFrequencyInfo &Other) const;
0101 };
0102
0103
0104
0105
0106 Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq);
0107
0108
0109
0110 Printable printBlockFreq(const BlockFrequencyInfo &BFI, const BasicBlock &BB);
0111
0112
0113 class BlockFrequencyAnalysis
0114 : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
0115 friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
0116
0117 static AnalysisKey Key;
0118
0119 public:
0120
0121 using Result = BlockFrequencyInfo;
0122
0123
0124 Result run(Function &F, FunctionAnalysisManager &AM);
0125 };
0126
0127
0128 class BlockFrequencyPrinterPass
0129 : public PassInfoMixin<BlockFrequencyPrinterPass> {
0130 raw_ostream &OS;
0131
0132 public:
0133 explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
0134
0135 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0136
0137 static bool isRequired() { return true; }
0138 };
0139
0140
0141 class BlockFrequencyInfoWrapperPass : public FunctionPass {
0142 BlockFrequencyInfo BFI;
0143
0144 public:
0145 static char ID;
0146
0147 BlockFrequencyInfoWrapperPass();
0148 ~BlockFrequencyInfoWrapperPass() override;
0149
0150 BlockFrequencyInfo &getBFI() { return BFI; }
0151 const BlockFrequencyInfo &getBFI() const { return BFI; }
0152
0153 void getAnalysisUsage(AnalysisUsage &AU) const override;
0154
0155 bool runOnFunction(Function &F) override;
0156 void releaseMemory() override;
0157 void print(raw_ostream &OS, const Module *M) const override;
0158 };
0159
0160 }
0161
0162 #endif