| 1 | //=- MachineBranchProbabilityInfo.h - Branch Probability Analysis -*- C++ -*-=// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass is used to evaluate branch probabilties on machine basic blocks. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H |
| 14 | #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H |
| 15 | |
| 16 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 17 | #include "llvm/CodeGen/MachinePassManager.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Support/BranchProbability.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | class MachineBranchProbabilityInfo { |
| 24 | // Default weight value. Used when we don't have information about the edge. |
| 25 | // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of |
| 26 | // the successors have a weight yet. But it doesn't make sense when providing |
| 27 | // weight to an edge that may have siblings with non-zero weights. This can |
| 28 | // be handled various ways, but it's probably fine for an edge with unknown |
| 29 | // weight to just "inherit" the non-zero weight of an adjacent successor. |
| 30 | static const uint32_t DEFAULT_WEIGHT = 16; |
| 31 | |
| 32 | public: |
| 33 | bool invalidate(MachineFunction &, const PreservedAnalyses &PA, |
| 34 | MachineFunctionAnalysisManager::Invalidator &); |
| 35 | |
| 36 | // Return edge probability. |
| 37 | BranchProbability getEdgeProbability(const MachineBasicBlock *Src, |
| 38 | const MachineBasicBlock *Dst) const; |
| 39 | |
| 40 | // Same as above, but using a const_succ_iterator from Src. This is faster |
| 41 | // when the iterator is already available. |
| 42 | BranchProbability |
| 43 | getEdgeProbability(const MachineBasicBlock *Src, |
| 44 | MachineBasicBlock::const_succ_iterator Dst) const; |
| 45 | |
| 46 | // A 'Hot' edge is an edge which probability is >= 80%. |
| 47 | bool isEdgeHot(const MachineBasicBlock *Src, |
| 48 | const MachineBasicBlock *Dst) const; |
| 49 | |
| 50 | // Print value between 0 (0% probability) and 1 (100% probability), |
| 51 | // however the value is never equal to 0, and can be 1 only iff SRC block |
| 52 | // has only one successor. |
| 53 | raw_ostream &printEdgeProbability(raw_ostream &OS, |
| 54 | const MachineBasicBlock *Src, |
| 55 | const MachineBasicBlock *Dst) const; |
| 56 | }; |
| 57 | |
| 58 | class MachineBranchProbabilityAnalysis |
| 59 | : public AnalysisInfoMixin<MachineBranchProbabilityAnalysis> { |
| 60 | friend AnalysisInfoMixin<MachineBranchProbabilityAnalysis>; |
| 61 | |
| 62 | static AnalysisKey Key; |
| 63 | |
| 64 | public: |
| 65 | using Result = MachineBranchProbabilityInfo; |
| 66 | |
| 67 | Result run(MachineFunction &, MachineFunctionAnalysisManager &); |
| 68 | }; |
| 69 | |
| 70 | class MachineBranchProbabilityPrinterPass |
| 71 | : public PassInfoMixin<MachineBranchProbabilityPrinterPass> { |
| 72 | raw_ostream &OS; |
| 73 | |
| 74 | public: |
| 75 | MachineBranchProbabilityPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 76 | PreservedAnalyses run(MachineFunction &MF, |
| 77 | MachineFunctionAnalysisManager &MFAM); |
| 78 | }; |
| 79 | |
| 80 | class MachineBranchProbabilityInfoWrapperPass : public ImmutablePass { |
| 81 | virtual void anchor(); |
| 82 | |
| 83 | MachineBranchProbabilityInfo MBPI; |
| 84 | |
| 85 | public: |
| 86 | static char ID; |
| 87 | |
| 88 | MachineBranchProbabilityInfoWrapperPass(); |
| 89 | |
| 90 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 91 | AU.setPreservesAll(); |
| 92 | } |
| 93 | |
| 94 | MachineBranchProbabilityInfo &getMBPI() { return MBPI; } |
| 95 | const MachineBranchProbabilityInfo &getMBPI() const { return MBPI; } |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | #endif |
| 101 | |