Simplify the design of BranchProbabilityInfo by collapsing it into
[oota-llvm.git] / include / llvm / Analysis / BranchProbabilityInfo.h
1 //===--- BranchProbabilityInfo.h - Branch Probability Analysis --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is used to evaluate branch probabilties.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
15 #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
16
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/BranchProbability.h"
21
22 namespace llvm {
23 class LoopInfo;
24 class raw_ostream;
25
26 class BranchProbabilityInfo : public FunctionPass {
27 public:
28   static char ID;
29
30   BranchProbabilityInfo() : FunctionPass(ID) {
31     initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry());
32   }
33
34   void getAnalysisUsage(AnalysisUsage &AU) const;
35   bool runOnFunction(Function &F);
36   void print(raw_ostream &OS, const Module *M = 0) const;
37
38   // Returned value is between 1 and UINT32_MAX. Look at
39   // BranchProbabilityInfo.cpp for details.
40   uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const;
41
42   // Look at BranchProbabilityInfo.cpp for details. Use it with caution!
43   void setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst,
44                      uint32_t Weight);
45
46   // A 'Hot' edge is an edge which probability is >= 80%.
47   bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const;
48
49   // Return a hot successor for the block BB or null if there isn't one.
50   BasicBlock *getHotSucc(BasicBlock *BB) const;
51
52   // Return a probability as a fraction between 0 (0% probability) and
53   // 1 (100% probability), however the value is never equal to 0, and can be 1
54   // only iff SRC block has only one successor.
55   BranchProbability getEdgeProbability(const BasicBlock *Src,
56                                        const BasicBlock *Dst) const;
57
58   // Print value between 0 (0% probability) and 1 (100% probability),
59   // however the value is never equal to 0, and can be 1 only iff SRC block
60   // has only one successor.
61   raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
62                                     const BasicBlock *Dst) const;
63
64 private:
65   typedef std::pair<const BasicBlock *, const BasicBlock *> Edge;
66
67   // Default weight value. Used when we don't have information about the edge.
68   // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
69   // the successors have a weight yet. But it doesn't make sense when providing
70   // weight to an edge that may have siblings with non-zero weights. This can
71   // be handled various ways, but it's probably fine for an edge with unknown
72   // weight to just "inherit" the non-zero weight of an adjacent successor.
73   static const uint32_t DEFAULT_WEIGHT = 16;
74
75   DenseMap<Edge, uint32_t> Weights;
76
77   /// \brief Handle to the LoopInfo analysis.
78   LoopInfo *LI;
79
80   /// \brief Track the last function we run over for printing.
81   Function *LastF;
82
83   /// \brief Get sum of the block successors' weights.
84   uint32_t getSumForBlock(const BasicBlock *BB) const;
85
86   bool calcMetadataWeights(BasicBlock *BB);
87   bool calcReturnHeuristics(BasicBlock *BB);
88   bool calcPointerHeuristics(BasicBlock *BB);
89   bool calcLoopBranchHeuristics(BasicBlock *BB);
90   bool calcZeroHeuristics(BasicBlock *BB);
91   bool calcFloatingPointHeuristics(BasicBlock *BB);
92 };
93
94 }
95
96 #endif