Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- MachineBlockFrequencyInfo.h - MBB 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_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 /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
0033 /// to estimate machine basic block frequencies.
0034 class MachineBlockFrequencyInfo {
0035   using ImplType = BlockFrequencyInfoImpl<MachineBasicBlock>;
0036   std::unique_ptr<ImplType> MBFI;
0037 
0038 public:
0039   MachineBlockFrequencyInfo(); // Legacy pass manager only.
0040   explicit MachineBlockFrequencyInfo(MachineFunction &F,
0041                                      MachineBranchProbabilityInfo &MBPI,
0042                                      MachineLoopInfo &MLI);
0043   MachineBlockFrequencyInfo(MachineBlockFrequencyInfo &&);
0044   ~MachineBlockFrequencyInfo();
0045 
0046   /// Handle invalidation explicitly.
0047   bool invalidate(MachineFunction &F, const PreservedAnalyses &PA,
0048                   MachineFunctionAnalysisManager::Invalidator &);
0049 
0050   /// calculate - compute block frequency info for the given function.
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   /// getblockFreq - Return block frequency. Return 0 if we don't have the
0060   /// information. Please note that initial frequency is equal to 1024. It means
0061   /// that we should not rely on the value itself, but only on the comparison to
0062   /// the other block frequencies. We do this to avoid using of floating points.
0063   /// For example, to get the frequency of a block relative to the entry block,
0064   /// divide the integral value returned by this function (the
0065   /// BlockFrequency::getFrequency() value) by getEntryFreq().
0066   BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
0067 
0068   /// Compute the frequency of the block, relative to the entry block.
0069   /// This API assumes getEntryFreq() is non-zero.
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   /// incrementally calculate block frequencies when we split edges, to avoid
0084   /// full CFG traversal.
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   /// Pop up a ghostview window with the current block frequency propagation
0093   /// rendered using dot.
0094   void view(const Twine &Name, bool isSimple = true) const;
0095 
0096   /// Divide a block's BlockFrequency::getFrequency() value by this value to
0097   /// obtain the entry block - relative frequency of said block.
0098   BlockFrequency getEntryFreq() const;
0099 };
0100 
0101 /// Print the block frequency @p Freq relative to the current functions entry
0102 /// frequency. Returns a Printable object that can be piped via `<<` to a
0103 /// `raw_ostream`.
0104 Printable printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
0105                          BlockFrequency Freq);
0106 
0107 /// Convenience function equivalent to calling
0108 /// `printBlockFreq(MBFI, MBFI.getBlockFreq(&MBB))`.
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 /// Printer pass for the \c MachineBlockFrequencyInfo results.
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 } // end namespace llvm
0156 
0157 #endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H