2 //==- MachineBranchProbabilityInfo.h - Machine Branch Probability Analysis -==//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This pass is used to evaluate branch probabilties on machine basic blocks.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
16 #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
18 #include "llvm/Pass.h"
19 #include "llvm/Support/BranchProbability.h"
25 class MachineBasicBlock;
27 class MachineBranchProbabilityInfo : public ImmutablePass {
29 // Default weight value. Used when we don't have information about the edge.
30 // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
31 // the successors have a weight yet. But it doesn't make sense when providing
32 // weight to an edge that may have siblings with non-zero weights. This can
33 // be handled various ways, but it's probably fine for an edge with unknown
34 // weight to just "inherit" the non-zero weight of an adjacent successor.
35 static const uint32_t DEFAULT_WEIGHT = 16;
40 MachineBranchProbabilityInfo() : ImmutablePass(ID) {
41 PassRegistry &Registry = *PassRegistry::getPassRegistry();
42 initializeMachineBranchProbabilityInfoPass(Registry);
45 void getAnalysisUsage(AnalysisUsage &AU) const {
49 // Return edge weight. If we don't have any informations about it - return
51 uint32_t getEdgeWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst) const;
53 // Get sum of the block successors' weights, potentially scaling them to fit
54 // within 32-bits. If scaling is required, sets Scale based on the necessary
55 // adjustment. Any edge weights used with the sum should be divided by Scale.
56 uint32_t getSumForBlock(MachineBasicBlock *MBB, uint32_t &Scale) const;
58 // A 'Hot' edge is an edge which probability is >= 80%.
59 bool isEdgeHot(MachineBasicBlock *Src, MachineBasicBlock *Dst) const;
61 // Return a hot successor for the block BB or null if there isn't one.
62 // NB: This routine's complexity is linear on the number of successors.
63 MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
65 // Return a probability as a fraction between 0 (0% probability) and
66 // 1 (100% probability), however the value is never equal to 0, and can be 1
67 // only iff SRC block has only one successor.
68 // NB: This routine's complexity is linear on the number of successors of
69 // Src. Querying sequentially for each successor's probability is a quadratic
71 BranchProbability getEdgeProbability(MachineBasicBlock *Src,
72 MachineBasicBlock *Dst) const;
74 // Print value between 0 (0% probability) and 1 (100% probability),
75 // however the value is never equal to 0, and can be 1 only iff SRC block
76 // has only one successor.
77 raw_ostream &printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src,
78 MachineBasicBlock *Dst) const;