comparison lib/Support/BranchProbability.cpp @ 100:7d135dc70f03

LLVM 3.9
author Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 22:53:40 +0900
parents afa8332a0e37
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
96:6418606d0ead 100:7d135dc70f03
20 using namespace llvm; 20 using namespace llvm;
21 21
22 const uint32_t BranchProbability::D; 22 const uint32_t BranchProbability::D;
23 23
24 raw_ostream &BranchProbability::print(raw_ostream &OS) const { 24 raw_ostream &BranchProbability::print(raw_ostream &OS) const {
25 if (isUnknown())
26 return OS << "?%";
27
25 // Get a percentage rounded to two decimal digits. This avoids 28 // Get a percentage rounded to two decimal digits. This avoids
26 // implementation-defined rounding inside printf. 29 // implementation-defined rounding inside printf.
27 double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0; 30 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); 31 return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
29 return OS; 32 Percent);
30 } 33 }
31 34
32 void BranchProbability::dump() const { print(dbgs()) << '\n'; } 35 void BranchProbability::dump() const { print(dbgs()) << '\n'; }
33 36
34 BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) { 37 BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
39 else { 42 else {
40 uint64_t Prob64 = 43 uint64_t Prob64 =
41 (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator; 44 (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
42 N = static_cast<uint32_t>(Prob64); 45 N = static_cast<uint32_t>(Prob64);
43 } 46 }
47 }
48
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);
44 } 60 }
45 61
46 // If ConstD is not zero, then replace D by ConstD so that division and modulo 62 // 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 63 // operations by D can be optimized, in case this function is not inlined by the
48 // compiler. 64 // compiler.