0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===-------------- lib/Support/BranchProbability.cpp -----------*- C++ -*-===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // This file implements Branch Probability class.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 #include "llvm/Support/BranchProbability.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 #include "llvm/Support/Debug.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/Support/Format.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/Support/raw_ostream.h"
|
95
|
18 #include <cassert>
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21
|
95
|
22 const uint32_t BranchProbability::D;
|
|
23
|
77
|
24 raw_ostream &BranchProbability::print(raw_ostream &OS) const {
|
100
|
25 if (isUnknown())
|
|
26 return OS << "?%";
|
|
27
|
95
|
28 // 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;
|
100
|
31 return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
|
|
32 Percent);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34
|
77
|
35 void BranchProbability::dump() const { print(dbgs()) << '\n'; }
|
|
36
|
95
|
37 BranchProbability::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
|
100
|
49 BranchProbability
|
|
50 BranchProbability::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
|
95
|
62 // 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.
|
|
65 template <uint32_t ConstD>
|
77
|
66 static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
|
95
|
67 if (ConstD > 0)
|
|
68 D = ConstD;
|
|
69
|
77
|
70 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);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85
|
77
|
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;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107
|
77
|
108 uint64_t BranchProbability::scale(uint64_t Num) const {
|
95
|
109 return ::scale<D>(Num, N, D);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 }
|
77
|
111
|
|
112 uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
|
95
|
113 return ::scale<0>(Num, D, N);
|
77
|
114 }
|