Introduce BlockFrequency analysis for BasicBlocks.
[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/Support/BranchProbability.h"
19 #include "llvm/Analysis/LoopInfo.h"
20
21 namespace llvm {
22
23 class raw_ostream;
24
25 class BranchProbabilityInfo : public FunctionPass {
26
27   // Default weight value. Used when we don't have information about the edge.
28   // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
29   // the successors have a weight yet. But it doesn't make sense when providing
30   // weight to an edge that may have siblings with non-zero weights. This can
31   // be handled various ways, but it's probably fine for an edge with unknown
32   // weight to just "inherit" the non-zero weight of an adjacent successor.
33   static const uint32_t DEFAULT_WEIGHT = 16;
34
35   typedef std::pair<BasicBlock *, BasicBlock *> Edge;
36
37   DenseMap<Edge, uint32_t> Weights;
38
39   // Get sum of the block successors' weights.
40   uint32_t getSumForBlock(BasicBlock *BB) const;
41
42   // Get sum of the edge weights going to the BB block.
43   uint32_t getBackSumForBlock(BasicBlock *BB) const;
44
45 public:
46   static char ID;
47
48   BranchProbabilityInfo() : FunctionPass(ID) {
49     initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry());
50   }
51
52   void getAnalysisUsage(AnalysisUsage &AU) const {
53     AU.addRequired<LoopInfo>();
54     AU.setPreservesAll();
55   }
56
57   bool runOnFunction(Function &F);
58
59   // Returned value is between 1 and UINT32_MAX. Look at
60   // BranchProbabilityInfo.cpp for details.
61   uint32_t getEdgeWeight(BasicBlock *Src, BasicBlock *Dst) const;
62
63   // Look at BranchProbabilityInfo.cpp for details. Use it with caution!
64   void setEdgeWeight(BasicBlock *Src, BasicBlock *Dst, uint32_t Weight);
65
66   // A 'Hot' edge is an edge which probability is >= 80%.
67   bool isEdgeHot(BasicBlock *Src, BasicBlock *Dst) const;
68
69   // Return a hot successor for the block BB or null if there isn't one.
70   BasicBlock *getHotSucc(BasicBlock *BB) const;
71
72   // Return a probability as a fraction between 0 (0% probability) and
73   // 1 (100% probability), however the value is never equal to 0, and can be 1
74   // only iff SRC block has only one successor.
75   BranchProbability getEdgeProbability(BasicBlock *Src, BasicBlock *Dst) const;
76
77   // Return a probability of getting to the DST block through SRC->DST edge.
78   // Returned value is a fraction between 0 (0% probability) and
79   // 1 (100% probability), however the value is never equal to 0, and can be 1
80   // only iff DST block has only one predecesor.
81   BranchProbability getBackEdgeProbability(BasicBlock *Src,
82                                            BasicBlock *Dst) const;
83
84   // Print value between 0 (0% probability) and 1 (100% probability),
85   // however the value is never equal to 0, and can be 1 only iff SRC block
86   // has only one successor.
87   raw_ostream &printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
88                                     BasicBlock *Dst) const;
89 };
90
91 }
92
93 #endif