LLVM: lib/Support/BranchProbability.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
14#include "llvm/Config/llvm-config.h"
18#include
19#include
20
21using namespace llvm;
22
25 return OS << "?%";
26
27
28
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,
32}
33
34#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
36#endif
37
39 assert(Denominator > 0 && "Denominator cannot be 0!");
40 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
41 if (Denominator == D)
42 N = Numerator;
43 else {
45 (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
46 N = static_cast<uint32_t>(Prob64);
47 }
48}
49
53 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
54
55 int Scale = 0;
56 while (Denominator > UINT32_MAX) {
57 Denominator >>= 1;
58 Scale++;
59 }
61}
62
64 assert(0 <= Prob && Prob <= 1 && "Probability must be between 0 and 1!");
66}
67
68
69
70
71template <uint32_t ConstD>
73 if (ConstD > 0)
74 D = ConstD;
75
76 assert(D && "divide by 0");
77
78
80 return Num;
81
82
83 uint64_t ProductHigh = (Num >> 32) * N;
84 uint64_t ProductLow = (Num & UINT32_MAX) * N;
85
86
87 uint32_t Upper32 = ProductHigh >> 32;
88 uint32_t Lower32 = ProductLow & UINT32_MAX;
89 uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
90 uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
91
92
93 Upper32 += Mid32 < Mid32Partial;
94
97
98
99 if (UpperQ > UINT32_MAX)
101
102 Rem = ((Rem % D) << 32) | Lower32;
104 uint64_t Q = (UpperQ << 32) + LowerQ;
105
106
108}
109
111 return ::scale(Num, N, D);
112}
113
115 return ::scale<0>(Num, D, N);
116}
117
120 for (unsigned I = 0; I < N; ++I)
121 Res *= *this;
122 return Res;
123}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D)
Definition BranchProbability.cpp:72
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.
LLVM_DUMP_METHOD void dump() const
Definition BranchProbability.cpp:35
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
Definition BranchProbability.cpp:51
BranchProbability pow(unsigned N) const
Compute pow(Probability, N).
Definition BranchProbability.cpp:118
static BranchProbability getOne()
LLVM_ABI raw_ostream & print(raw_ostream &OS) const
Definition BranchProbability.cpp:23
LLVM_ABI uint64_t scaleByInverse(uint64_t Num) const
Scale a large integer by the inverse.
Definition BranchProbability.cpp:114
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
Definition BranchProbability.cpp:110
This class implements an extremely fast bulk output stream that can only output to a stream.
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.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.