Introduce MachineBranchProbabilityInfo class, which has similar API to
[oota-llvm.git] / include / llvm / CodeGen / MachineBranchProbabilityInfo.h
1
2 //==- MachineBranchProbabilityInfo.h - Machine Branch Probability Analysis -==//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This pass is used to evaluate branch probabilties on machine basic blocks.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
16 #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
17
18 #include "llvm/Pass.h"
19 #include "llvm/Support/BranchProbability.h"
20 #include <climits>
21
22 namespace llvm {
23
24 class raw_ostream;
25
26 class MachineBranchProbabilityInfo : public ImmutablePass {
27
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;
35
36   // Get sum of the block successors' weights.
37   uint32_t getSumForBlock(MachineBasicBlock *MBB) const;
38
39 public:
40   static char ID;
41
42   MachineBranchProbabilityInfo() : ImmutablePass(ID) {
43     PassRegistry &Registry = *PassRegistry::getPassRegistry();
44     initializeMachineBranchProbabilityInfoPass(Registry);
45   }
46
47   void getAnalysisUsage(AnalysisUsage &AU) const {
48     AU.setPreservesAll();
49   }
50
51   // Return edge weight. If we don't have any informations about it - return
52   // DEFAULT_WEIGHT.
53   uint32_t getEdgeWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst) const;
54
55   // A 'Hot' edge is an edge which probability is >= 80%.
56   bool isEdgeHot(MachineBasicBlock *Src, MachineBasicBlock *Dst) const;
57
58   // Return a hot successor for the block BB or null if there isn't one.
59   MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
60
61   // Return a probability as a fraction between 0 (0% probability) and
62   // 1 (100% probability), however the value is never equal to 0, and can be 1
63   // only iff SRC block has only one successor.
64   BranchProbability getEdgeProbability(MachineBasicBlock *Src,
65                                        MachineBasicBlock *Dst) const;
66
67   // Print value between 0 (0% probability) and 1 (100% probability),
68   // however the value is never equal to 0, and can be 1 only iff SRC block
69   // has only one successor.
70   raw_ostream &printEdgeProbability(raw_ostream &OS, MachineBasicBlock *Src,
71                                     MachineBasicBlock *Dst) const;
72 };
73
74 }
75
76
77 #endif