New BranchProbabilityInfo analysis. Patch by Jakub Staszak!
[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/Analysis/LoopInfo.h"
19 #include "llvm/Support/Debug.h"
20
21 namespace llvm {
22
23 class BranchProbabilityInfo : public FunctionPass {
24
25   // Default weight value. Used when we don't have information about the edge.
26   static const unsigned int DEFAULT_WEIGHT = 16;
27
28   typedef std::pair<BasicBlock *, BasicBlock *> Edge;
29
30   DenseMap<Edge, unsigned> Weights;
31
32 public:
33   static char ID;
34
35   BranchProbabilityInfo() : FunctionPass(ID) {
36     initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry());
37   }
38
39   void getAnalysisUsage(AnalysisUsage &AU) const {
40     AU.addRequired<LoopInfo>();
41     AU.setPreservesAll();
42   }
43
44   bool runOnFunction(Function &F);
45
46   // Returned value is between 1 and UINT_MAX. Look at BranchProbabilityInfo.cpp
47   // for details.
48   unsigned getEdgeWeight(BasicBlock *Src, BasicBlock *Dst) const;
49
50   // Look at BranchProbabilityInfo.cpp for details. Use it with caution!
51   void setEdgeWeight(BasicBlock *Src, BasicBlock *Dst, unsigned Weight);
52
53   // A 'Hot' edge is an edge which probability is >= 80%.
54   bool isEdgeHot(BasicBlock *Src, BasicBlock *Dst) const;
55
56   // Return a hot successor for the block BB or null if there isn't one.
57   BasicBlock *getHotSucc(BasicBlock *BB) const;
58
59   // Print value between 0 (0% probability) and 1 (100% probability),
60   // however the value is never equal to 0, and can be 1 only iff SRC block
61   // has only one successor.
62   raw_ostream &printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
63                                    BasicBlock *Dst) const;
64 };
65
66 }
67
68 #endif