blob: 31dee9561f4941e2630bc04f9a76877bad8a00a2 [file] [log] [blame]
Andrew Trick3d4e64b2011-06-11 01:05:22 +00001//===-------------- lib/Support/BranchProbability.cpp -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Branch Probability class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/BranchProbability.h"
Nico Weber432a3882018-04-30 14:59:11 +000015#include "llvm/Config/llvm-config.h"
Andrew Trick3d4e64b2011-06-11 01:05:22 +000016#include "llvm/Support/Debug.h"
Benjamin Kramercc0ed6b2011-10-23 11:32:54 +000017#include "llvm/Support/Format.h"
Andrew Trick3d4e64b2011-06-11 01:05:22 +000018#include "llvm/Support/raw_ostream.h"
Cong Hou15ea0162015-09-25 23:09:59 +000019#include <cassert>
Andrew Trick3d4e64b2011-06-11 01:05:22 +000020
21using namespace llvm;
22
Benjamin Kramer2a636312015-09-26 10:09:36 +000023const uint32_t BranchProbability::D;
24
Duncan P. N. Exon Smithbdc1e2a2014-04-29 17:07:42 +000025raw_ostream &BranchProbability::print(raw_ostream &OS) const {
Cong Houd97c1002015-12-01 05:29:22 +000026 if (isUnknown())
27 return OS << "?%";
28
Benjamin Kramer2a636312015-09-26 10:09:36 +000029 // Get a percentage rounded to two decimal digits. This avoids
30 // implementation-defined rounding inside printf.
31 double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
Cong Houd97c1002015-12-01 05:29:22 +000032 return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
33 Percent);
Andrew Trick3d4e64b2011-06-11 01:05:22 +000034}
35
Aaron Ballman615eb472017-10-15 14:32:27 +000036#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000037LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; }
Matthias Braun8c209aa2017-01-28 02:02:38 +000038#endif
Andrew Trick3d4e64b2011-06-11 01:05:22 +000039
Cong Hou15ea0162015-09-25 23:09:59 +000040BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
41 assert(Denominator > 0 && "Denominator cannot be 0!");
42 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
43 if (Denominator == D)
44 N = Numerator;
45 else {
46 uint64_t Prob64 =
47 (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
48 N = static_cast<uint32_t>(Prob64);
49 }
50}
51
Cong Houd97c1002015-12-01 05:29:22 +000052BranchProbability
53BranchProbability::getBranchProbability(uint64_t Numerator,
54 uint64_t Denominator) {
55 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
56 // Scale down Denominator to fit in a 32-bit integer.
57 int Scale = 0;
58 while (Denominator > UINT32_MAX) {
59 Denominator >>= 1;
60 Scale++;
61 }
62 return BranchProbability(Numerator >> Scale, Denominator);
63}
64
Cong Hou15ea0162015-09-25 23:09:59 +000065// If ConstD is not zero, then replace D by ConstD so that division and modulo
66// operations by D can be optimized, in case this function is not inlined by the
67// compiler.
68template <uint32_t ConstD>
Benjamin Kramer2a636312015-09-26 10:09:36 +000069static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
Cong Hou15ea0162015-09-25 23:09:59 +000070 if (ConstD > 0)
71 D = ConstD;
72
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +000073 assert(D && "divide by 0");
74
75 // Fast path for multiplying by 1.0.
76 if (!Num || D == N)
77 return Num;
78
79 // Split Num into upper and lower parts to multiply, then recombine.
80 uint64_t ProductHigh = (Num >> 32) * N;
81 uint64_t ProductLow = (Num & UINT32_MAX) * N;
82
83 // Split into 32-bit digits.
84 uint32_t Upper32 = ProductHigh >> 32;
85 uint32_t Lower32 = ProductLow & UINT32_MAX;
86 uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
87 uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
88
89 // Carry.
90 Upper32 += Mid32 < Mid32Partial;
91
92 // Check for overflow.
93 if (Upper32 >= D)
94 return UINT64_MAX;
95
96 uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
97 uint64_t UpperQ = Rem / D;
98
99 // Check for overflow.
100 if (UpperQ > UINT32_MAX)
101 return UINT64_MAX;
102
103 Rem = ((Rem % D) << 32) | Lower32;
104 uint64_t LowerQ = Rem / D;
105 uint64_t Q = (UpperQ << 32) + LowerQ;
106
107 // Check for overflow.
108 return Q < LowerQ ? UINT64_MAX : Q;
109}
110
111uint64_t BranchProbability::scale(uint64_t Num) const {
Cong Hou15ea0162015-09-25 23:09:59 +0000112 return ::scale<D>(Num, N, D);
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +0000113}
114
115uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
Cong Hou15ea0162015-09-25 23:09:59 +0000116 return ::scale<0>(Num, D, N);
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +0000117}