LLVM 22.0.0git
BranchProbability.h
Go to the documentation of this file.
1//===- BranchProbability.h - Branch Probability Wrapper ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Definition of BranchProbability shared by IR and Machine Instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
14#define LLVM_SUPPORT_BRANCHPROBABILITY_H
15
16#include "llvm/ADT/ADL.h"
19#include <algorithm>
20#include <cassert>
21#include <iterator>
22#include <numeric>
23
24namespace llvm {
25
26class raw_ostream;
27
28// This class represents Branch Probability as a non-negative fraction that is
29// no greater than 1. It uses a fixed-point-like implementation, in which the
30// denominator is always a constant value (here we use 1<<31 for maximum
31// precision).
33 // Numerator
34 uint32_t N;
35
36 // Denominator, which is a constant value.
37 static constexpr uint32_t D = 1u << 31;
38 static constexpr uint32_t UnknownN = UINT32_MAX;
39
40 // Construct a BranchProbability with only numerator assuming the denominator
41 // is 1<<31. For internal use only.
42 explicit BranchProbability(uint32_t n) : N(n) {}
43
44public:
45 BranchProbability() : N(UnknownN) {}
46 LLVM_ABI BranchProbability(uint32_t Numerator, uint32_t Denominator);
47
48 bool isZero() const { return N == 0; }
49 bool isUnknown() const { return N == UnknownN; }
50
53 static BranchProbability getUnknown() { return BranchProbability(UnknownN); }
54 // Create a BranchProbability object with the given numerator and 1<<31
55 // as denominator.
57 // Create a BranchProbability object from 64-bit integers.
59 uint64_t Denominator);
60
61 // Normalize given probabilties so that the sum of them becomes approximate
62 // one.
63 template <class ProbabilityIter>
64 static void normalizeProbabilities(ProbabilityIter Begin,
65 ProbabilityIter End);
66
67 template <class ProbabilityContainer>
68 static void normalizeProbabilities(ProbabilityContainer &&R) {
70 }
71
72 uint32_t getNumerator() const { return N; }
73 static uint32_t getDenominator() { return D; }
74
75 // Return (1 - Probability).
77
79
80#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
81 LLVM_DUMP_METHOD void dump() const;
82#endif
83
84 /// Scale a large integer.
85 ///
86 /// Scales \c Num. Guarantees full precision. Returns the floor of the
87 /// result.
88 ///
89 /// \return \c Num times \c this.
91
92 /// Scale a large integer by the inverse.
93 ///
94 /// Scales \c Num by the inverse of \c this. Guarantees full precision.
95 /// Returns the floor of the result.
96 ///
97 /// \return \c Num divided by \c this.
99
101 assert(N != UnknownN && RHS.N != UnknownN &&
102 "Unknown probability cannot participate in arithmetics.");
103 // Saturate the result in case of overflow.
104 N = (uint64_t(N) + RHS.N > D) ? D : N + RHS.N;
105 return *this;
106 }
107
109 assert(N != UnknownN && RHS.N != UnknownN &&
110 "Unknown probability cannot participate in arithmetics.");
111 // Saturate the result in case of underflow.
112 N = N < RHS.N ? 0 : N - RHS.N;
113 return *this;
114 }
115
117 assert(N != UnknownN && RHS.N != UnknownN &&
118 "Unknown probability cannot participate in arithmetics.");
119 N = (static_cast<uint64_t>(N) * RHS.N + D / 2) / D;
120 return *this;
121 }
122
124 assert(N != UnknownN &&
125 "Unknown probability cannot participate in arithmetics.");
126 N = (uint64_t(N) * RHS > D) ? D : N * RHS;
127 return *this;
128 }
129
131 assert(N != UnknownN && RHS.N != UnknownN &&
132 "Unknown probability cannot participate in arithmetics.");
133 N = (static_cast<uint64_t>(N) * D + RHS.N / 2) / RHS.N;
134 return *this;
135 }
136
138 assert(N != UnknownN &&
139 "Unknown probability cannot participate in arithmetics.");
140 assert(RHS > 0 && "The divider cannot be zero.");
141 N /= RHS;
142 return *this;
143 }
144
146 BranchProbability Prob(*this);
147 Prob += RHS;
148 return Prob;
149 }
150
152 BranchProbability Prob(*this);
153 Prob -= RHS;
154 return Prob;
155 }
156
158 BranchProbability Prob(*this);
159 Prob *= RHS;
160 return Prob;
161 }
162
164 BranchProbability Prob(*this);
165 Prob *= RHS;
166 return Prob;
167 }
168
170 BranchProbability Prob(*this);
171 Prob /= RHS;
172 return Prob;
173 }
174
176 BranchProbability Prob(*this);
177 Prob /= RHS;
178 return Prob;
179 }
180
181 bool operator==(BranchProbability RHS) const { return N == RHS.N; }
182 bool operator!=(BranchProbability RHS) const { return !(*this == RHS); }
183
185 assert(N != UnknownN && RHS.N != UnknownN &&
186 "Unknown probability cannot participate in comparisons.");
187 return N < RHS.N;
188 }
189
191 assert(N != UnknownN && RHS.N != UnknownN &&
192 "Unknown probability cannot participate in comparisons.");
193 return RHS < *this;
194 }
195
197 assert(N != UnknownN && RHS.N != UnknownN &&
198 "Unknown probability cannot participate in comparisons.");
199 return !(RHS < *this);
200 }
201
203 assert(N != UnknownN && RHS.N != UnknownN &&
204 "Unknown probability cannot participate in comparisons.");
205 return !(*this < RHS);
206 }
207};
208
210 return Prob.print(OS);
211}
212
213template <class ProbabilityIter>
215 ProbabilityIter End) {
216 if (Begin == End)
217 return;
218
219 unsigned UnknownProbCount = 0;
220 uint64_t Sum = std::accumulate(Begin, End, uint64_t(0),
221 [&](uint64_t S, const BranchProbability &BP) {
222 if (!BP.isUnknown())
223 return S + BP.N;
224 UnknownProbCount++;
225 return S;
226 });
227
228 if (UnknownProbCount > 0) {
230 // If the sum of all known probabilities is less than one, evenly distribute
231 // the complement of sum to unknown probabilities. Otherwise, set unknown
232 // probabilities to zeros and continue to normalize known probabilities.
234 ProbForUnknown = BranchProbability::getRaw(
235 (BranchProbability::getDenominator() - Sum) / UnknownProbCount);
236
237 std::replace_if(Begin, End,
238 [](const BranchProbability &BP) { return BP.isUnknown(); },
239 ProbForUnknown);
240
242 return;
243 }
244
245 if (Sum == 0) {
246 BranchProbability BP(1, std::distance(Begin, End));
247 std::fill(Begin, End, BP);
248 return;
249 }
250
251 for (auto I = Begin; I != End; ++I)
252 I->N = (I->N * uint64_t(D) + Sum / 2) / Sum;
253}
254
255}
256
257#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
bool End
Definition: ELF_riscv.cpp:480
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
Value * RHS
LLVM_DUMP_METHOD void dump() const
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
BranchProbability operator-(BranchProbability RHS) const
BranchProbability & operator-=(BranchProbability RHS)
static uint32_t getDenominator()
bool operator<(BranchProbability RHS) const
bool operator!=(BranchProbability RHS) const
static BranchProbability getRaw(uint32_t N)
bool operator==(BranchProbability RHS) const
BranchProbability operator/(uint32_t RHS) const
BranchProbability & operator/=(BranchProbability RHS)
bool operator<=(BranchProbability RHS) const
static BranchProbability getOne()
LLVM_ABI raw_ostream & print(raw_ostream &OS) const
BranchProbability & operator*=(BranchProbability RHS)
LLVM_ABI uint64_t scaleByInverse(uint64_t Num) const
Scale a large integer by the inverse.
BranchProbability operator*(BranchProbability RHS) const
static BranchProbability getUnknown()
BranchProbability operator/(BranchProbability RHS) const
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
static void normalizeProbabilities(ProbabilityContainer &&R)
BranchProbability operator+(BranchProbability RHS) const
bool operator>=(BranchProbability RHS) const
BranchProbability operator*(uint32_t RHS) const
BranchProbability & operator*=(uint32_t RHS)
BranchProbability getCompl() const
BranchProbability & operator+=(BranchProbability RHS)
BranchProbability & operator/=(uint32_t RHS)
static BranchProbability getZero()
bool operator>(BranchProbability RHS) const
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
Definition: ADL.h:78
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
Definition: ADL.h:86
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
#define N