aboutsummaryrefslogtreecommitdiff
path: root/lib/Support/BranchProbability.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Support/BranchProbability.cpp')
-rw-r--r--lib/Support/BranchProbability.cpp49
1 files changed, 45 insertions, 4 deletions
diff --git a/lib/Support/BranchProbability.cpp b/lib/Support/BranchProbability.cpp
index 65878d6e3025..771d02c0aa3c 100644
--- a/lib/Support/BranchProbability.cpp
+++ b/lib/Support/BranchProbability.cpp
@@ -15,17 +15,58 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
+#include <cassert>
using namespace llvm;
+const uint32_t BranchProbability::D;
+
raw_ostream &BranchProbability::print(raw_ostream &OS) const {
- return OS << N << " / " << D << " = "
- << format("%g%%", ((double)N / D) * 100.0);
+ if (isUnknown())
+ return OS << "?%";
+
+ // Get a percentage rounded to two decimal digits. This avoids
+ // implementation-defined rounding inside printf.
+ double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
+ return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
+ Percent);
}
void BranchProbability::dump() const { print(dbgs()) << '\n'; }
+BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
+ assert(Denominator > 0 && "Denominator cannot be 0!");
+ assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
+ if (Denominator == D)
+ N = Numerator;
+ else {
+ uint64_t Prob64 =
+ (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
+ N = static_cast<uint32_t>(Prob64);
+ }
+}
+
+BranchProbability
+BranchProbability::getBranchProbability(uint64_t Numerator,
+ uint64_t Denominator) {
+ assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
+ // Scale down Denominator to fit in a 32-bit integer.
+ int Scale = 0;
+ while (Denominator > UINT32_MAX) {
+ Denominator >>= 1;
+ Scale++;
+ }
+ return BranchProbability(Numerator >> Scale, Denominator);
+}
+
+// If ConstD is not zero, then replace D by ConstD so that division and modulo
+// operations by D can be optimized, in case this function is not inlined by the
+// compiler.
+template <uint32_t ConstD>
static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
+ if (ConstD > 0)
+ D = ConstD;
+
assert(D && "divide by 0");
// Fast path for multiplying by 1.0.
@@ -65,9 +106,9 @@ static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
}
uint64_t BranchProbability::scale(uint64_t Num) const {
- return ::scale(Num, N, D);
+ return ::scale<D>(Num, N, D);
}
uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
- return ::scale(Num, D, N);
+ return ::scale<0>(Num, D, N);
}