blob: 771d02c0aa3c210112721671e606c8a070070d5b [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"
15#include "llvm/Support/Debug.h"
Benjamin Kramercc0ed6b2011-10-23 11:32:54 +000016#include "llvm/Support/Format.h"
Andrew Trick3d4e64b2011-06-11 01:05:22 +000017#include "llvm/Support/raw_ostream.h"
Cong Hou15ea0162015-09-25 23:09:59 +000018#include <cassert>
Andrew Trick3d4e64b2011-06-11 01:05:22 +000019
20using namespace llvm;
21
Benjamin Kramer2a636312015-09-26 10:09:36 +000022const uint32_t BranchProbability::D;
23
Duncan P. N. Exon Smithbdc1e2a2014-04-29 17:07:42 +000024raw_ostream &BranchProbability::print(raw_ostream &OS) const {
Cong Houd97c1002015-12-01 05:29:22 +000025 if (isUnknown())
26 return OS << "?%";
27
Benjamin Kramer2a636312015-09-26 10:09:36 +000028 // Get a percentage rounded to two decimal digits. This avoids
29 // implementation-defined rounding inside printf.
30 double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
Cong Houd97c1002015-12-01 05:29:22 +000031 return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
32 Percent);
Andrew Trick3d4e64b2011-06-11 01:05:22 +000033}
34
Duncan P. N. Exon Smithbdc1e2a2014-04-29 17:07:42 +000035void BranchProbability::dump() const { print(dbgs()) << '\n'; }
Andrew Trick3d4e64b2011-06-11 01:05:22 +000036
Cong Hou15ea0162015-09-25 23:09:59 +000037BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
38 assert(Denominator > 0 && "Denominator cannot be 0!");
39 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
40 if (Denominator == D)
41 N = Numerator;
42 else {
43 uint64_t Prob64 =
44 (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
45 N = static_cast<uint32_t>(Prob64);
46 }
47}
48
Cong Houd97c1002015-12-01 05:29:22 +000049BranchProbability
50BranchProbability::getBranchProbability(uint64_t Numerator,
51 uint64_t Denominator) {
52 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
53 // Scale down Denominator to fit in a 32-bit integer.
54 int Scale = 0;
55 while (Denominator > UINT32_MAX) {
56 Denominator >>= 1;
57 Scale++;
58 }
59 return BranchProbability(Numerator >> Scale, Denominator);
60}
61
Cong Hou15ea0162015-09-25 23:09:59 +000062// If ConstD is not zero, then replace D by ConstD so that division and modulo
63// operations by D can be optimized, in case this function is not inlined by the
64// compiler.
65template <uint32_t ConstD>
Benjamin Kramer2a636312015-09-26 10:09:36 +000066static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
Cong Hou15ea0162015-09-25 23:09:59 +000067 if (ConstD > 0)
68 D = ConstD;
69
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +000070 assert(D && "divide by 0");
71
72 // Fast path for multiplying by 1.0.
73 if (!Num || D == N)
74 return Num;
75
76 // Split Num into upper and lower parts to multiply, then recombine.
77 uint64_t ProductHigh = (Num >> 32) * N;
78 uint64_t ProductLow = (Num & UINT32_MAX) * N;
79
80 // Split into 32-bit digits.
81 uint32_t Upper32 = ProductHigh >> 32;
82 uint32_t Lower32 = ProductLow & UINT32_MAX;
83 uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
84 uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
85
86 // Carry.
87 Upper32 += Mid32 < Mid32Partial;
88
89 // Check for overflow.
90 if (Upper32 >= D)
91 return UINT64_MAX;
92
93 uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
94 uint64_t UpperQ = Rem / D;
95
96 // Check for overflow.
97 if (UpperQ > UINT32_MAX)
98 return UINT64_MAX;
99
100 Rem = ((Rem % D) << 32) | Lower32;
101 uint64_t LowerQ = Rem / D;
102 uint64_t Q = (UpperQ << 32) + LowerQ;
103
104 // Check for overflow.
105 return Q < LowerQ ? UINT64_MAX : Q;
106}
107
108uint64_t BranchProbability::scale(uint64_t Num) const {
Cong Hou15ea0162015-09-25 23:09:59 +0000109 return ::scale<D>(Num, N, D);
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +0000110}
111
112uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
Cong Hou15ea0162015-09-25 23:09:59 +0000113 return ::scale<0>(Num, D, N);
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +0000114}