blob: 3b0f6e6f06e42c48157190dac6b3887047e951f1 [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 {
Benjamin Kramer2a636312015-09-26 10:09:36 +000025 // Get a percentage rounded to two decimal digits. This avoids
26 // implementation-defined rounding inside printf.
27 double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
28 OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D, Percent);
Cong Hou15ea0162015-09-25 23:09:59 +000029 return OS;
Andrew Trick3d4e64b2011-06-11 01:05:22 +000030}
31
Duncan P. N. Exon Smithbdc1e2a2014-04-29 17:07:42 +000032void BranchProbability::dump() const { print(dbgs()) << '\n'; }
Andrew Trick3d4e64b2011-06-11 01:05:22 +000033
Cong Hou15ea0162015-09-25 23:09:59 +000034BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
35 assert(Denominator > 0 && "Denominator cannot be 0!");
36 assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
37 if (Denominator == D)
38 N = Numerator;
39 else {
40 uint64_t Prob64 =
41 (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
42 N = static_cast<uint32_t>(Prob64);
43 }
44}
45
Cong Hou15ea0162015-09-25 23:09:59 +000046// If ConstD is not zero, then replace D by ConstD so that division and modulo
47// operations by D can be optimized, in case this function is not inlined by the
48// compiler.
49template <uint32_t ConstD>
Benjamin Kramer2a636312015-09-26 10:09:36 +000050static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
Cong Hou15ea0162015-09-25 23:09:59 +000051 if (ConstD > 0)
52 D = ConstD;
53
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +000054 assert(D && "divide by 0");
55
56 // Fast path for multiplying by 1.0.
57 if (!Num || D == N)
58 return Num;
59
60 // Split Num into upper and lower parts to multiply, then recombine.
61 uint64_t ProductHigh = (Num >> 32) * N;
62 uint64_t ProductLow = (Num & UINT32_MAX) * N;
63
64 // Split into 32-bit digits.
65 uint32_t Upper32 = ProductHigh >> 32;
66 uint32_t Lower32 = ProductLow & UINT32_MAX;
67 uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
68 uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
69
70 // Carry.
71 Upper32 += Mid32 < Mid32Partial;
72
73 // Check for overflow.
74 if (Upper32 >= D)
75 return UINT64_MAX;
76
77 uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
78 uint64_t UpperQ = Rem / D;
79
80 // Check for overflow.
81 if (UpperQ > UINT32_MAX)
82 return UINT64_MAX;
83
84 Rem = ((Rem % D) << 32) | Lower32;
85 uint64_t LowerQ = Rem / D;
86 uint64_t Q = (UpperQ << 32) + LowerQ;
87
88 // Check for overflow.
89 return Q < LowerQ ? UINT64_MAX : Q;
90}
91
92uint64_t BranchProbability::scale(uint64_t Num) const {
Cong Hou15ea0162015-09-25 23:09:59 +000093 return ::scale<D>(Num, N, D);
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +000094}
95
96uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
Cong Hou15ea0162015-09-25 23:09:59 +000097 return ::scale<0>(Num, D, N);
Duncan P. N. Exon Smith415e7652014-04-29 16:15:35 +000098}