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"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20
|
77
|
21 raw_ostream &BranchProbability::print(raw_ostream &OS) const {
|
|
22 return OS << N << " / " << D << " = "
|
|
23 << format("%g%%", ((double)N / D) * 100.0);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25
|
77
|
26 void BranchProbability::dump() const { print(dbgs()) << '\n'; }
|
|
27
|
|
28 static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
|
|
29 assert(D && "divide by 0");
|
|
30
|
|
31 // Fast path for multiplying by 1.0.
|
|
32 if (!Num || D == N)
|
|
33 return Num;
|
|
34
|
|
35 // Split Num into upper and lower parts to multiply, then recombine.
|
|
36 uint64_t ProductHigh = (Num >> 32) * N;
|
|
37 uint64_t ProductLow = (Num & UINT32_MAX) * N;
|
|
38
|
|
39 // Split into 32-bit digits.
|
|
40 uint32_t Upper32 = ProductHigh >> 32;
|
|
41 uint32_t Lower32 = ProductLow & UINT32_MAX;
|
|
42 uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
|
|
43 uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44
|
77
|
45 // Carry.
|
|
46 Upper32 += Mid32 < Mid32Partial;
|
|
47
|
|
48 // Check for overflow.
|
|
49 if (Upper32 >= D)
|
|
50 return UINT64_MAX;
|
|
51
|
|
52 uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
|
|
53 uint64_t UpperQ = Rem / D;
|
|
54
|
|
55 // Check for overflow.
|
|
56 if (UpperQ > UINT32_MAX)
|
|
57 return UINT64_MAX;
|
|
58
|
|
59 Rem = ((Rem % D) << 32) | Lower32;
|
|
60 uint64_t LowerQ = Rem / D;
|
|
61 uint64_t Q = (UpperQ << 32) + LowerQ;
|
|
62
|
|
63 // Check for overflow.
|
|
64 return Q < LowerQ ? UINT64_MAX : Q;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66
|
77
|
67 uint64_t BranchProbability::scale(uint64_t Num) const {
|
|
68 return ::scale(Num, N, D);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 }
|
77
|
70
|
|
71 uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
|
|
72 return ::scale(Num, D, N);
|
|
73 }
|