Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //=- MachineBranchProbabilityInfo.h - Branch Probability 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 // This pass is used to evaluate branch probabilties on machine basic blocks.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
0014 #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
0015 
0016 #include "llvm/CodeGen/MachineBasicBlock.h"
0017 #include "llvm/CodeGen/MachinePassManager.h"
0018 #include "llvm/Pass.h"
0019 #include "llvm/Support/BranchProbability.h"
0020 
0021 namespace llvm {
0022 
0023 class MachineBranchProbabilityInfo {
0024   // Default weight value. Used when we don't have information about the edge.
0025   // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
0026   // the successors have a weight yet. But it doesn't make sense when providing
0027   // weight to an edge that may have siblings with non-zero weights. This can
0028   // be handled various ways, but it's probably fine for an edge with unknown
0029   // weight to just "inherit" the non-zero weight of an adjacent successor.
0030   static const uint32_t DEFAULT_WEIGHT = 16;
0031 
0032 public:
0033   bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
0034                   MachineFunctionAnalysisManager::Invalidator &);
0035 
0036   // Return edge probability.
0037   BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
0038                                        const MachineBasicBlock *Dst) const;
0039 
0040   // Same as above, but using a const_succ_iterator from Src. This is faster
0041   // when the iterator is already available.
0042   BranchProbability
0043   getEdgeProbability(const MachineBasicBlock *Src,
0044                      MachineBasicBlock::const_succ_iterator Dst) const;
0045 
0046   // A 'Hot' edge is an edge which probability is >= 80%.
0047   bool isEdgeHot(const MachineBasicBlock *Src,
0048                  const MachineBasicBlock *Dst) const;
0049 
0050   // Print value between 0 (0% probability) and 1 (100% probability),
0051   // however the value is never equal to 0, and can be 1 only iff SRC block
0052   // has only one successor.
0053   raw_ostream &printEdgeProbability(raw_ostream &OS,
0054                                     const MachineBasicBlock *Src,
0055                                     const MachineBasicBlock *Dst) const;
0056 };
0057 
0058 class MachineBranchProbabilityAnalysis
0059     : public AnalysisInfoMixin<MachineBranchProbabilityAnalysis> {
0060   friend AnalysisInfoMixin<MachineBranchProbabilityAnalysis>;
0061 
0062   static AnalysisKey Key;
0063 
0064 public:
0065   using Result = MachineBranchProbabilityInfo;
0066 
0067   Result run(MachineFunction &, MachineFunctionAnalysisManager &);
0068 };
0069 
0070 class MachineBranchProbabilityPrinterPass
0071     : public PassInfoMixin<MachineBranchProbabilityPrinterPass> {
0072   raw_ostream &OS;
0073 
0074 public:
0075   MachineBranchProbabilityPrinterPass(raw_ostream &OS) : OS(OS) {}
0076   PreservedAnalyses run(MachineFunction &MF,
0077                         MachineFunctionAnalysisManager &MFAM);
0078 };
0079 
0080 class MachineBranchProbabilityInfoWrapperPass : public ImmutablePass {
0081   virtual void anchor();
0082 
0083   MachineBranchProbabilityInfo MBPI;
0084 
0085 public:
0086   static char ID;
0087 
0088   MachineBranchProbabilityInfoWrapperPass();
0089 
0090   void getAnalysisUsage(AnalysisUsage &AU) const override {
0091     AU.setPreservesAll();
0092   }
0093 
0094   MachineBranchProbabilityInfo &getMBPI() { return MBPI; }
0095   const MachineBranchProbabilityInfo &getMBPI() const { return MBPI; }
0096 };
0097 }
0098 
0099 
0100 #endif