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