1 //===- BranchProbability.h - Branch Probability Wrapper ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Definition of BranchProbability shared by IR and Machine Instructions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
15 #define LLVM_SUPPORT_BRANCHPROBABILITY_H
17 #include "llvm/Support/DataTypes.h"
27 // This class represents Branch Probability as a non-negative fraction that is
28 // no greater than 1. It uses a fixed-point-like implementation, in which the
29 // denominator is always a constant value (here we use 1<<31 for maximum
31 class BranchProbability {
35 // Denominator, which is a constant value.
36 static const uint32_t D = 1u << 31;
38 // Construct a BranchProbability with only numerator assuming the denominator
39 // is 1<<31. For internal use only.
40 explicit BranchProbability(uint32_t n) : N(n) {}
43 BranchProbability() : N(0) {}
44 BranchProbability(uint32_t Numerator, uint32_t Denominator);
46 bool isZero() const { return N == 0; }
48 static BranchProbability getZero() { return BranchProbability(0); }
49 static BranchProbability getOne() { return BranchProbability(D); }
50 // Create a BranchProbability object with the given numerator and 1<<31
52 static BranchProbability getRaw(uint32_t N) { return BranchProbability(N); }
54 // Normalize given probabilties so that the sum of them becomes approximate
56 template <class ProbabilityList>
57 static void normalizeProbabilities(ProbabilityList &Probs);
59 // Normalize a list of weights by scaling them down so that the sum of them
60 // doesn't exceed UINT32_MAX.
61 template <class WeightListIter>
62 static void normalizeEdgeWeights(WeightListIter Begin, WeightListIter End);
64 uint32_t getNumerator() const { return N; }
65 static uint32_t getDenominator() { return D; }
67 // Return (1 - Probability).
68 BranchProbability getCompl() const { return BranchProbability(D - N); }
70 raw_ostream &print(raw_ostream &OS) const;
74 /// \brief Scale a large integer.
76 /// Scales \c Num. Guarantees full precision. Returns the floor of the
79 /// \return \c Num times \c this.
80 uint64_t scale(uint64_t Num) const;
82 /// \brief Scale a large integer by the inverse.
84 /// Scales \c Num by the inverse of \c this. Guarantees full precision.
85 /// Returns the floor of the result.
87 /// \return \c Num divided by \c this.
88 uint64_t scaleByInverse(uint64_t Num) const;
90 BranchProbability &operator+=(BranchProbability RHS) {
91 assert(N <= D - RHS.N &&
92 "The sum of branch probabilities should not exceed one!");
97 BranchProbability &operator-=(BranchProbability RHS) {
99 "Can only subtract a smaller probability from a larger one!");
104 BranchProbability &operator*=(BranchProbability RHS) {
105 N = (static_cast<uint64_t>(N) * RHS.N + D / 2) / D;
109 BranchProbability operator+(BranchProbability RHS) const {
110 BranchProbability Prob(*this);
114 BranchProbability operator-(BranchProbability RHS) const {
115 BranchProbability Prob(*this);
119 BranchProbability operator*(BranchProbability RHS) const {
120 BranchProbability Prob(*this);
124 bool operator==(BranchProbability RHS) const { return N == RHS.N; }
125 bool operator!=(BranchProbability RHS) const { return !(*this == RHS); }
126 bool operator<(BranchProbability RHS) const { return N < RHS.N; }
127 bool operator>(BranchProbability RHS) const { return RHS < *this; }
128 bool operator<=(BranchProbability RHS) const { return !(RHS < *this); }
129 bool operator>=(BranchProbability RHS) const { return !(*this < RHS); }
132 inline raw_ostream &operator<<(raw_ostream &OS, BranchProbability Prob) {
133 return Prob.print(OS);
136 template <class ProbabilityList>
137 void BranchProbability::normalizeProbabilities(ProbabilityList &Probs) {
139 for (auto Prob : Probs)
142 for (auto &Prob : Probs)
143 Prob.N = (Prob.N * uint64_t(D) + Sum / 2) / Sum;
146 template <class WeightListIter>
147 void BranchProbability::normalizeEdgeWeights(WeightListIter Begin,
148 WeightListIter End) {
149 // First we compute the sum with 64-bits of precision.
150 uint64_t Sum = std::accumulate(Begin, End, uint64_t(0));
152 if (Sum > UINT32_MAX) {
153 // Compute the scale necessary to cause the weights to fit, and re-sum with
154 // that scale applied.
155 assert(Sum / UINT32_MAX < UINT32_MAX &&
156 "The sum of weights exceeds UINT32_MAX^2!");
157 uint32_t Scale = Sum / UINT32_MAX + 1;
158 for (auto I = Begin; I != End; ++I)
160 Sum = std::accumulate(Begin, End, uint64_t(0));
163 // Eliminate zero weights.
164 auto ZeroWeightNum = std::count(Begin, End, 0u);
165 if (ZeroWeightNum > 0) {
166 // If all weights are zeros, replace them by 1.
168 std::fill(Begin, End, 1u);
170 // We are converting zeros into ones, and here we need to make sure that
171 // after this the sum won't exceed UINT32_MAX.
172 if (Sum + ZeroWeightNum > UINT32_MAX) {
173 for (auto I = Begin; I != End; ++I)
175 ZeroWeightNum = std::count(Begin, End, 0u);
176 Sum = std::accumulate(Begin, End, uint64_t(0));
178 // Scale up non-zero weights and turn zero weights into ones.
179 uint64_t ScalingFactor = (UINT32_MAX - ZeroWeightNum) / Sum;
180 assert(ScalingFactor >= 1);
181 if (ScalingFactor > 1)
182 for (auto I = Begin; I != End; ++I)
184 std::replace(Begin, End, 0u, 1u);