Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:11

0001 //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Loops should be simplified before this analysis.
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 /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
0036 /// estimate IR basic block frequencies.
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   /// Handle invalidation explicitly.
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   /// getblockFreq - Return block frequency. Return 0 if we don't have the
0061   /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
0062   /// means that we should not rely on the value itself, but only on the
0063   /// comparison to the other block frequencies. We do this to avoid using of
0064   /// floating points.
0065   BlockFrequency getBlockFreq(const BasicBlock *BB) const;
0066 
0067   /// Returns the estimated profile count of \p BB.
0068   /// This computes the relative block frequency of \p BB and multiplies it by
0069   /// the enclosing function's count (if available) and returns the value.
0070   std::optional<uint64_t>
0071   getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic = false) const;
0072 
0073   /// Returns the estimated profile count of \p Freq.
0074   /// This uses the frequency \p Freq and multiplies it by
0075   /// the enclosing function's count (if available) and returns the value.
0076   std::optional<uint64_t> getProfileCountFromFreq(BlockFrequency Freq) const;
0077 
0078   /// Returns true if \p BB is an irreducible loop header
0079   /// block. Otherwise false.
0080   bool isIrrLoopHeader(const BasicBlock *BB);
0081 
0082   // Set the frequency of the given basic block.
0083   void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq);
0084 
0085   /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
0086   /// of the blocks in \p BlocksToScale such that their frequencies relative
0087   /// to \p ReferenceBB remain unchanged.
0088   void setBlockFreqAndScale(const BasicBlock *ReferenceBB, BlockFrequency Freq,
0089                             SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
0090 
0091   /// calculate - compute block frequency info for the given function.
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   // Compare to the other BFI and verify they match.
0100   void verifyMatch(BlockFrequencyInfo &Other) const;
0101 };
0102 
0103 /// Print the block frequency @p Freq relative to the current functions entry
0104 /// frequency. Returns a Printable object that can be piped via `<<` to a
0105 /// `raw_ostream`.
0106 Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq);
0107 
0108 /// Convenience function equivalent to calling
0109 /// `printBlockFreq(BFI, BFI.getBlocakFreq(&BB))`.
0110 Printable printBlockFreq(const BlockFrequencyInfo &BFI, const BasicBlock &BB);
0111 
0112 /// Analysis pass which computes \c BlockFrequencyInfo.
0113 class BlockFrequencyAnalysis
0114     : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
0115   friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
0116 
0117   static AnalysisKey Key;
0118 
0119 public:
0120   /// Provide the result type for this analysis pass.
0121   using Result = BlockFrequencyInfo;
0122 
0123   /// Run the analysis pass over a function and produce BFI.
0124   Result run(Function &F, FunctionAnalysisManager &AM);
0125 };
0126 
0127 /// Printer pass for the \c BlockFrequencyInfo results.
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 /// Legacy analysis pass which computes \c BlockFrequencyInfo.
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 } // end namespace llvm
0161 
0162 #endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H