55b96961c35b7cde927a5c42a6bd7e4cc4c35ed3
[oota-llvm.git] / include / llvm / CodeGen / MachineBranchProbabilityInfo.h
1 //=- MachineBranchProbabilityInfo.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 on machine basic blocks.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
15 #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
16
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/BranchProbability.h"
20 #include <climits>
21
22 namespace llvm {
23
24 class MachineBranchProbabilityInfo : public ImmutablePass {
25   virtual void anchor();
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 public:
36   static char ID;
37
38   MachineBranchProbabilityInfo() : ImmutablePass(ID) {
39     PassRegistry &Registry = *PassRegistry::getPassRegistry();
40     initializeMachineBranchProbabilityInfoPass(Registry);
41   }
42
43   void getAnalysisUsage(AnalysisUsage &AU) const override {
44     AU.setPreservesAll();
45   }
46
47   // Return edge weight. If we don't have any informations about it - return
48   // DEFAULT_WEIGHT.
49   uint32_t getEdgeWeight(const MachineBasicBlock *Src,
50                          const MachineBasicBlock *Dst) const;
51
52   // Same thing, but using a const_succ_iterator from Src. This is faster when
53   // the iterator is already available.
54   uint32_t getEdgeWeight(const MachineBasicBlock *Src,
55                          MachineBasicBlock::const_succ_iterator Dst) const;
56
57   // Get sum of the block successors' weights, potentially scaling them to fit
58   // within 32-bits. If scaling is required, sets Scale based on the necessary
59   // adjustment. Any edge weights used with the sum should be divided by Scale.
60   uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;
61
62   // Get sum of the block successors' weights, and force normalizing the
63   // successors' weights of MBB so that their sum fit within 32-bits.
64   uint32_t getSumForBlock(MachineBasicBlock *MBB) const;
65
66   // A 'Hot' edge is an edge which probability is >= 80%.
67   bool isEdgeHot(const MachineBasicBlock *Src,
68                  const MachineBasicBlock *Dst) const;
69
70   // Return a hot successor for the block BB or null if there isn't one.
71   // NB: This routine's complexity is linear on the number of successors.
72   MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
73
74   // Return a probability as a fraction between 0 (0% probability) and
75   // 1 (100% probability), however the value is never equal to 0, and can be 1
76   // only iff SRC block has only one successor.
77   // NB: This routine's complexity is linear on the number of successors of
78   // Src. Querying sequentially for each successor's probability is a quadratic
79   // query pattern.
80   BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
81                                        const MachineBasicBlock *Dst) const;
82
83   // Print value between 0 (0% probability) and 1 (100% probability),
84   // however the value is never equal to 0, and can be 1 only iff SRC block
85   // has only one successor.
86   raw_ostream &printEdgeProbability(raw_ostream &OS,
87                                     const MachineBasicBlock *Src,
88                                     const MachineBasicBlock *Dst) const;
89
90   // Normalize a list of weights by scaling them down so that the sum of them
91   // doesn't exceed UINT32_MAX. Return the scale.
92   template <class WeightList>
93   static uint32_t normalizeEdgeWeights(WeightList &Weights);
94 };
95
96 template <class WeightList>
97 uint32_t
98 MachineBranchProbabilityInfo::normalizeEdgeWeights(WeightList &Weights) {
99   assert(Weights.size() < UINT32_MAX && "Too many weights in the list!");
100   // First we compute the sum with 64-bits of precision.
101   uint64_t Sum = std::accumulate(Weights.begin(), Weights.end(), uint64_t(0));
102
103   // If the computed sum fits in 32-bits, we're done.
104   if (Sum <= UINT32_MAX)
105     return 1;
106
107   // Otherwise, compute the scale necessary to cause the weights to fit, and
108   // re-sum with that scale applied.
109   assert((Sum / UINT32_MAX) < UINT32_MAX &&
110          "The sum of weights exceeds UINT32_MAX^2!");
111   uint32_t Scale = (Sum / UINT32_MAX) + 1;
112   for (auto &W : Weights)
113     W /= Scale;
114   return Scale;
115 }
116
117 }
118
119
120 #endif