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 
13 #include "llvm/Support/BranchProbability.h"
14 #include "llvm/Config/llvm-config.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cassert>
19 
20 using namespace llvm;
21 
22 // These default values are chosen to represent an extremely skewed outcome for
23 // a condition, but they leave some room for interpretation by later passes.
24 //
25 // If the documentation for __builtin_expect() was made explicit that it should
26 // only be used in extreme cases, we could make this ratio higher. As it stands,
27 // programmers may be using __builtin_expect() / llvm.expect to annotate that a
28 // branch is only mildly likely or unlikely to be taken.
29 cl::opt<uint32_t> llvm::LikelyBranchWeight(
30     "likely-branch-weight", cl::Hidden, cl::init(2000),
31     cl::desc("Weight of the branch likely to be taken (default = 2000)"));
32 cl::opt<uint32_t> llvm::UnlikelyBranchWeight(
33     "unlikely-branch-weight", cl::Hidden, cl::init(1),
34     cl::desc("Weight of the branch unlikely to be taken (default = 1)"));
35 
36 constexpr uint32_t BranchProbability::D;
37 
38 raw_ostream &BranchProbability::print(raw_ostream &OS) const {
39   if (isUnknown())
40     return OS << "?%";
41 
42   // Get a percentage rounded to two decimal digits. This avoids
43   // implementation-defined rounding inside printf.
44   double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
45   return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
46                       Percent);
47 }
48 
49 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
50 LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; }
51 #endif
52 
53 BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
54   assert(Denominator > 0 && "Denominator cannot be 0!");
55   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
56   if (Denominator == D)
57     N = Numerator;
58   else {
59     uint64_t Prob64 =
60         (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
61     N = static_cast<uint32_t>(Prob64);
62   }
63 }
64 
65 BranchProbability
66 BranchProbability::getBranchProbability(uint64_t Numerator,
67                                         uint64_t Denominator) {
68   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
69   // Scale down Denominator to fit in a 32-bit integer.
70   int Scale = 0;
71   while (Denominator > UINT32_MAX) {
72     Denominator >>= 1;
73     Scale++;
74   }
75   return BranchProbability(Numerator >> Scale, Denominator);
76 }
77 
78 // If ConstD is not zero, then replace D by ConstD so that division and modulo
79 // operations by D can be optimized, in case this function is not inlined by the
80 // compiler.
81 template <uint32_t ConstD>
82 static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
83   if (ConstD > 0)
84     D = ConstD;
85 
86   assert(D && "divide by 0");
87 
88   // Fast path for multiplying by 1.0.
89   if (!Num || D == N)
90     return Num;
91 
92   // Split Num into upper and lower parts to multiply, then recombine.
93   uint64_t ProductHigh = (Num >> 32) * N;
94   uint64_t ProductLow = (Num & UINT32_MAX) * N;
95 
96   // Split into 32-bit digits.
97   uint32_t Upper32 = ProductHigh >> 32;
98   uint32_t Lower32 = ProductLow & UINT32_MAX;
99   uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
100   uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
101 
102   // Carry.
103   Upper32 += Mid32 < Mid32Partial;
104 
105   uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
106   uint64_t UpperQ = Rem / D;
107 
108   // Check for overflow.
109   if (UpperQ > UINT32_MAX)
110     return UINT64_MAX;
111 
112   Rem = ((Rem % D) << 32) | Lower32;
113   uint64_t LowerQ = Rem / D;
114   uint64_t Q = (UpperQ << 32) + LowerQ;
115 
116   // Check for overflow.
117   return Q < LowerQ ? UINT64_MAX : Q;
118 }
119 
120 uint64_t BranchProbability::scale(uint64_t Num) const {
121   return ::scale<D>(Num, N, D);
122 }
123 
124 uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
125   return ::scale<0>(Num, D, N);
126 }
127