1 //=- MachineBranchProbabilityInfo.h - Branch Probability Analysis -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is used to evaluate branch probabilties on machine basic blocks.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
15 #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/BranchProbability.h"
25 class MachineBranchProbabilityInfo : public ImmutablePass {
26 virtual void anchor();
28 // Default weight value. Used when we don't have information about the edge.
29 // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
30 // the successors have a weight yet. But it doesn't make sense when providing
31 // weight to an edge that may have siblings with non-zero weights. This can
32 // be handled various ways, but it's probably fine for an edge with unknown
33 // weight to just "inherit" the non-zero weight of an adjacent successor.
34 static const uint32_t DEFAULT_WEIGHT = 16;
39 MachineBranchProbabilityInfo() : ImmutablePass(ID) {
40 PassRegistry &Registry = *PassRegistry::getPassRegistry();
41 initializeMachineBranchProbabilityInfoPass(Registry);
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
48 // Return edge weight. If we don't have any informations about it - return
50 uint32_t getEdgeWeight(const MachineBasicBlock *Src,
51 const MachineBasicBlock *Dst) const;
53 // Same thing, but using a const_succ_iterator from Src. This is faster when
54 // the iterator is already available.
55 uint32_t getEdgeWeight(const MachineBasicBlock *Src,
56 MachineBasicBlock::const_succ_iterator Dst) const;
58 // Get sum of the block successors' weights, potentially scaling them to fit
59 // within 32-bits. If scaling is required, sets Scale based on the necessary
60 // adjustment. Any edge weights used with the sum should be divided by Scale.
61 uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;
63 // A 'Hot' edge is an edge which probability is >= 80%.
64 bool isEdgeHot(const MachineBasicBlock *Src,
65 const MachineBasicBlock *Dst) const;
67 // Return a hot successor for the block BB or null if there isn't one.
68 // NB: This routine's complexity is linear on the number of successors.
69 MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
71 // Return a probability as a fraction between 0 (0% probability) and
72 // 1 (100% probability), however the value is never equal to 0, and can be 1
73 // only iff SRC block has only one successor.
74 // NB: This routine's complexity is linear on the number of successors of
75 // Src. Querying sequentially for each successor's probability is a quadratic
77 BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
78 const MachineBasicBlock *Dst) const;
80 // Print value between 0 (0% probability) and 1 (100% probability),
81 // however the value is never equal to 0, and can be 1 only iff SRC block
82 // has only one successor.
83 raw_ostream &printEdgeProbability(raw_ostream &OS,
84 const MachineBasicBlock *Src,
85 const MachineBasicBlock *Dst) const;
87 // Normalize a list of weights by scaling them down so that the sum of them
88 // doesn't exceed UINT32_MAX. Return the scale.
89 template <class WeightListIter>
90 static uint32_t normalizeEdgeWeights(WeightListIter Begin,
94 template <class WeightListIter>
96 MachineBranchProbabilityInfo::normalizeEdgeWeights(WeightListIter Begin,
98 // First we compute the sum with 64-bits of precision.
99 uint64_t Sum = std::accumulate(Begin, End, uint64_t(0));
101 // If the computed sum fits in 32-bits, we're done.
102 if (Sum <= UINT32_MAX)
105 // Otherwise, compute the scale necessary to cause the weights to fit, and
106 // re-sum with that scale applied.
107 assert((Sum / UINT32_MAX) < UINT32_MAX &&
108 "The sum of weights exceeds UINT32_MAX^2!");
109 uint32_t Scale = (Sum / UINT32_MAX) + 1;
110 for (auto I = Begin; I != End; ++I)