1*0b57cec5SDimitry Andric //===-------------- lib/Support/BranchProbability.cpp -----------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements Branch Probability class.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
14*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
16*0b57cec5SDimitry Andric #include "llvm/Support/Format.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
18*0b57cec5SDimitry Andric #include <cassert>
19*0b57cec5SDimitry Andric #include <cmath>
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric using namespace llvm;
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric constexpr uint32_t BranchProbability::D;
24*0b57cec5SDimitry Andric 
print(raw_ostream & OS) const25*0b57cec5SDimitry Andric raw_ostream &BranchProbability::print(raw_ostream &OS) const {
26*0b57cec5SDimitry Andric   if (isUnknown())
27*0b57cec5SDimitry Andric     return OS << "?%";
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric   // Get a percentage rounded to two decimal digits. This avoids
30*0b57cec5SDimitry Andric   // implementation-defined rounding inside printf.
31*0b57cec5SDimitry Andric   double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
32*0b57cec5SDimitry Andric   return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
33*0b57cec5SDimitry Andric                       Percent);
34*0b57cec5SDimitry Andric }
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const37*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; }
38*0b57cec5SDimitry Andric #endif
39*0b57cec5SDimitry Andric 
BranchProbability(uint32_t Numerator,uint32_t Denominator)40*0b57cec5SDimitry Andric BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
41*0b57cec5SDimitry Andric   assert(Denominator > 0 && "Denominator cannot be 0!");
42*0b57cec5SDimitry Andric   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
43*0b57cec5SDimitry Andric   if (Denominator == D)
44*0b57cec5SDimitry Andric     N = Numerator;
45*0b57cec5SDimitry Andric   else {
46*0b57cec5SDimitry Andric     uint64_t Prob64 =
47*0b57cec5SDimitry Andric         (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
48*0b57cec5SDimitry Andric     N = static_cast<uint32_t>(Prob64);
49*0b57cec5SDimitry Andric   }
50*0b57cec5SDimitry Andric }
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric BranchProbability
getBranchProbability(uint64_t Numerator,uint64_t Denominator)53*0b57cec5SDimitry Andric BranchProbability::getBranchProbability(uint64_t Numerator,
54*0b57cec5SDimitry Andric                                         uint64_t Denominator) {
55*0b57cec5SDimitry Andric   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
56*0b57cec5SDimitry Andric   // Scale down Denominator to fit in a 32-bit integer.
57*0b57cec5SDimitry Andric   int Scale = 0;
58*0b57cec5SDimitry Andric   while (Denominator > UINT32_MAX) {
59*0b57cec5SDimitry Andric     Denominator >>= 1;
60*0b57cec5SDimitry Andric     Scale++;
61*0b57cec5SDimitry Andric   }
62*0b57cec5SDimitry Andric   return BranchProbability(Numerator >> Scale, Denominator);
63*0b57cec5SDimitry Andric }
64*0b57cec5SDimitry Andric 
65*0b57cec5SDimitry Andric // If ConstD is not zero, then replace D by ConstD so that division and modulo
66*0b57cec5SDimitry Andric // operations by D can be optimized, in case this function is not inlined by the
67*0b57cec5SDimitry Andric // compiler.
68*0b57cec5SDimitry Andric template <uint32_t ConstD>
scale(uint64_t Num,uint32_t N,uint32_t D)69*0b57cec5SDimitry Andric static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
70*0b57cec5SDimitry Andric   if (ConstD > 0)
71*0b57cec5SDimitry Andric     D = ConstD;
72*0b57cec5SDimitry Andric 
73*0b57cec5SDimitry Andric   assert(D && "divide by 0");
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric   // Fast path for multiplying by 1.0.
76*0b57cec5SDimitry Andric   if (!Num || D == N)
77*0b57cec5SDimitry Andric     return Num;
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric   // Split Num into upper and lower parts to multiply, then recombine.
80*0b57cec5SDimitry Andric   uint64_t ProductHigh = (Num >> 32) * N;
81*0b57cec5SDimitry Andric   uint64_t ProductLow = (Num & UINT32_MAX) * N;
82*0b57cec5SDimitry Andric 
83*0b57cec5SDimitry Andric   // Split into 32-bit digits.
84*0b57cec5SDimitry Andric   uint32_t Upper32 = ProductHigh >> 32;
85*0b57cec5SDimitry Andric   uint32_t Lower32 = ProductLow & UINT32_MAX;
86*0b57cec5SDimitry Andric   uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
87*0b57cec5SDimitry Andric   uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric   // Carry.
90*0b57cec5SDimitry Andric   Upper32 += Mid32 < Mid32Partial;
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
93*0b57cec5SDimitry Andric   uint64_t UpperQ = Rem / D;
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   // Check for overflow.
96*0b57cec5SDimitry Andric   if (UpperQ > UINT32_MAX)
97*0b57cec5SDimitry Andric     return UINT64_MAX;
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric   Rem = ((Rem % D) << 32) | Lower32;
100*0b57cec5SDimitry Andric   uint64_t LowerQ = Rem / D;
101*0b57cec5SDimitry Andric   uint64_t Q = (UpperQ << 32) + LowerQ;
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric   // Check for overflow.
104*0b57cec5SDimitry Andric   return Q < LowerQ ? UINT64_MAX : Q;
105*0b57cec5SDimitry Andric }
106*0b57cec5SDimitry Andric 
scale(uint64_t Num) const107*0b57cec5SDimitry Andric uint64_t BranchProbability::scale(uint64_t Num) const {
108*0b57cec5SDimitry Andric   return ::scale<D>(Num, N, D);
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric 
scaleByInverse(uint64_t Num) const111*0b57cec5SDimitry Andric uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
112*0b57cec5SDimitry Andric   return ::scale<0>(Num, D, N);
113 }
114