Update MachineBranchProbabilityInfo::normalizeEdgeWeights to make sure there is no...
[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 <algorithm>
21 #include <climits>
22 #include <numeric>
23
24 namespace llvm {
25
26 class MachineBranchProbabilityInfo : public ImmutablePass {
27   virtual void anchor();
28
29   // Default weight value. Used when we don't have information about the edge.
30   // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
31   // the successors have a weight yet. But it doesn't make sense when providing
32   // weight to an edge that may have siblings with non-zero weights. This can
33   // be handled various ways, but it's probably fine for an edge with unknown
34   // weight to just "inherit" the non-zero weight of an adjacent successor.
35   static const uint32_t DEFAULT_WEIGHT = 16;
36
37 public:
38   static char ID;
39
40   MachineBranchProbabilityInfo() : ImmutablePass(ID) {
41     PassRegistry &Registry = *PassRegistry::getPassRegistry();
42     initializeMachineBranchProbabilityInfoPass(Registry);
43   }
44
45   void getAnalysisUsage(AnalysisUsage &AU) const override {
46     AU.setPreservesAll();
47   }
48
49   // Return edge weight. If we don't have any informations about it - return
50   // DEFAULT_WEIGHT.
51   uint32_t getEdgeWeight(const MachineBasicBlock *Src,
52                          const MachineBasicBlock *Dst) const;
53
54   // Same thing, but using a const_succ_iterator from Src. This is faster when
55   // the iterator is already available.
56   uint32_t getEdgeWeight(const MachineBasicBlock *Src,
57                          MachineBasicBlock::const_succ_iterator Dst) const;
58
59   // Get sum of the block successors' weights, potentially scaling them to fit
60   // within 32-bits. If scaling is required, sets Scale based on the necessary
61   // adjustment. Any edge weights used with the sum should be divided by Scale.
62   uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;
63
64   // A 'Hot' edge is an edge which probability is >= 80%.
65   bool isEdgeHot(const MachineBasicBlock *Src,
66                  const MachineBasicBlock *Dst) const;
67
68   // Return a hot successor for the block BB or null if there isn't one.
69   // NB: This routine's complexity is linear on the number of successors.
70   MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) 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   // NB: This routine's complexity is linear on the number of successors of
76   // Src. Querying sequentially for each successor's probability is a quadratic
77   // query pattern.
78   BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
79                                        const MachineBasicBlock *Dst) const;
80
81   // Print value between 0 (0% probability) and 1 (100% probability),
82   // however the value is never equal to 0, and can be 1 only iff SRC block
83   // has only one successor.
84   raw_ostream &printEdgeProbability(raw_ostream &OS,
85                                     const MachineBasicBlock *Src,
86                                     const MachineBasicBlock *Dst) const;
87
88   // Normalize a list of weights by scaling them down so that the sum of them
89   // doesn't exceed UINT32_MAX.
90   template <class WeightListIter>
91   static void normalizeEdgeWeights(WeightListIter Begin, WeightListIter End);
92 };
93
94 template <class WeightListIter>
95 void MachineBranchProbabilityInfo::normalizeEdgeWeights(WeightListIter Begin,
96                                                         WeightListIter End) {
97   // First we compute the sum with 64-bits of precision.
98   uint64_t Sum = std::accumulate(Begin, End, uint64_t(0));
99
100   if (Sum > UINT32_MAX) {
101     // Compute the scale necessary to cause the weights to fit, and re-sum with
102     // that scale applied.
103     assert(Sum / UINT32_MAX < UINT32_MAX &&
104            "The sum of weights exceeds UINT32_MAX^2!");
105     uint32_t Scale = Sum / UINT32_MAX + 1;
106     for (auto I = Begin; I != End; ++I)
107       *I /= Scale;
108     Sum = std::accumulate(Begin, End, uint64_t(0));
109   }
110
111   // Eliminate zero weights.
112   auto ZeroWeightNum = std::count(Begin, End, 0u);
113   if (ZeroWeightNum > 0) {
114     // If all weights are zeros, replace them by 1.
115     if (Sum == 0)
116       std::fill(Begin, End, 1u);
117     else {
118       // Scale up non-zero weights and turn zero weights into ones.
119       uint64_t ScalingFactor = (UINT32_MAX - ZeroWeightNum) / Sum;
120       if (ScalingFactor > 1)
121         for (auto I = Begin; I != End; ++I)
122           *I *= ScalingFactor;
123       std::replace(Begin, End, 0u, 1u);
124     }
125   }
126 }
127
128 }
129
130
131 #endif