LLVM 23.0.0git
BranchProbability.cpp
Go to the documentation of this file.
1//===-------------- lib/Support/BranchProbability.cpp -----------*- 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// This file implements Branch Probability class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/Config/llvm-config.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/Format.h"
18#include <cassert>
19#include <cmath>
20
21using namespace llvm;
22
24 if (isUnknown())
25 return OS << "?%";
26
27 // Get a percentage rounded to two decimal digits. This avoids
28 // implementation-defined rounding inside printf.
29 double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
30 return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
31 Percent);
32}
33
34#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
36#endif
37
40 uint64_t Denominator) {
41 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
42 // Scale down Denominator to fit in a 32-bit integer.
43 int Scale = 0;
44 while (Denominator > UINT32_MAX) {
45 Denominator >>= 1;
46 Scale++;
47 }
48 return BranchProbability(Numerator >> Scale, Denominator);
49}
50
51BranchProbability BranchProbability::getBranchProbability(double Prob) {
52 assert(0 <= Prob && Prob <= 1 && "Probability must be between 0 and 1!");
53 return BranchProbability(std::round(Prob * D), D);
54}
55
56// If ConstD is not zero, then replace D by ConstD so that division and modulo
57// operations by D can be optimized, in case this function is not inlined by the
58// compiler.
59template <uint32_t ConstD>
61 if (ConstD > 0)
62 D = ConstD;
63
64 assert(D && "divide by 0");
65
66 // Fast path for multiplying by 1.0.
67 if (!Num || D == N)
68 return Num;
69
70 // Split Num into upper and lower parts to multiply, then recombine.
71 uint64_t ProductHigh = (Num >> 32) * N;
72 uint64_t ProductLow = (Num & UINT32_MAX) * N;
73
74 // Split into 32-bit digits.
75 uint32_t Upper32 = ProductHigh >> 32;
76 uint32_t Lower32 = ProductLow & UINT32_MAX;
77 uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
78 uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
79
80 // Carry.
81 Upper32 += Mid32 < Mid32Partial;
82
83 uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
84 uint64_t UpperQ = Rem / D;
85
86 // Check for overflow.
87 if (UpperQ > UINT32_MAX)
88 return UINT64_MAX;
89
90 Rem = ((Rem % D) << 32) | Lower32;
91 uint64_t LowerQ = Rem / D;
92 uint64_t Q = (UpperQ << 32) + LowerQ;
93
94 // Check for overflow.
95 return Q < LowerQ ? UINT64_MAX : Q;
96}
97
99 return ::scale<D>(Num, N, D);
100}
101
103 return ::scale<0>(Num, D, N);
104}
105
106BranchProbability BranchProbability::pow(unsigned N) const {
107 BranchProbability Res = BranchProbability::getOne();
108 for (unsigned I = 0; I < N; ++I)
109 Res *= *this;
110 return Res;
111}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:672
#define I(x, y, z)
Definition MD5.cpp:57
LLVM_DUMP_METHOD void dump() const
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
static constexpr BranchProbability getOne()
LLVM_ABI BranchProbability pow(unsigned N) const
Compute pow(Probability, N).
LLVM_ABI raw_ostream & print(raw_ostream &OS) const
LLVM_ABI uint64_t scaleByInverse(uint64_t Num) const
Scale a large integer by the inverse.
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define UINT64_MAX
Definition DataTypes.h:77
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:94
#define N