summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/BranchProbability.cpp
diff options
context:
space:
mode:
authorCong Hou <congh@google.com>2015-09-25 23:09:59 +0000
committerCong Hou <congh@google.com>2015-09-25 23:09:59 +0000
commit15ea016346d90d52d7ae308a1259da39af1f0dbd (patch)
tree1466b4137249469f9c7de4ea0c464814ae4d4a1b /llvm/lib/Support/BranchProbability.cpp
parenta0bc8590fe011046edfeb3e97747c415b7a7ac49 (diff)
downloadbcm5719-llvm-15ea016346d90d52d7ae308a1259da39af1f0dbd.tar.gz
bcm5719-llvm-15ea016346d90d52d7ae308a1259da39af1f0dbd.zip
Use fixed-point representation for BranchProbability.
BranchProbability now is represented by its numerator and denominator in uint32_t type. This patch changes this representation into a fixed point that is represented by the numerator in uint32_t type and a constant denominator 1<<31. This is quite similar to the representation of BlockMass in BlockFrequencyInfoImpl.h. There are several pros and cons of this change: Pros: 1. It uses only a half space of the current one. 2. Some operations are much faster like plus, subtraction, comparison, and scaling by an integer. Cons: 1. Constructing a probability using arbitrary numerator and denominator needs additional calculations. 2. It is a little less precise than before as we use a fixed denominator. For example, 1 - 1/3 may not be exactly identical to 1 / 3 (this will lead to many BranchProbability unit test failures). This should not matter when we only use it for branch probability. If we use it like a rational value for some precise calculations we may need another construct like ValueRatio. One important reason for this change is that we propose to store branch probabilities instead of edge weights in MachineBasicBlock. We also want clients to use probability instead of weight when adding successors to a MBB. The current BranchProbability has more space which may be a concern. Differential revision: http://reviews.llvm.org/D12603 llvm-svn: 248633
Diffstat (limited to 'llvm/lib/Support/BranchProbability.cpp')
-rw-r--r--llvm/lib/Support/BranchProbability.cpp56
1 files changed, 51 insertions, 5 deletions
diff --git a/llvm/lib/Support/BranchProbability.cpp b/llvm/lib/Support/BranchProbability.cpp
index 65878d6e302..47b06c2e0ff 100644
--- a/llvm/lib/Support/BranchProbability.cpp
+++ b/llvm/lib/Support/BranchProbability.cpp
@@ -15,17 +15,63 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
+#include <cassert>
using namespace llvm;
raw_ostream &BranchProbability::print(raw_ostream &OS) const {
- return OS << N << " / " << D << " = "
- << format("%g%%", ((double)N / D) * 100.0);
+ auto GetHexDigit = [](int Val) -> char {
+ assert(Val < 16);
+ if (Val < 10)
+ return '0' + Val;
+ return 'a' + Val - 10;
+ };
+ OS << "0x";
+ for (int Digits = 0; Digits < 8; ++Digits)
+ OS << GetHexDigit(N >> (28 - Digits * 4) & 0xf);
+ OS << " / 0x";
+ for (int Digits = 0; Digits < 8; ++Digits)
+ OS << GetHexDigit(D >> (28 - Digits * 4) & 0xf);
+ OS << " = " << format("%.2f%%", ((double)N / D) * 100.0);
+ return OS;
}
void BranchProbability::dump() const { print(dbgs()) << '\n'; }
-static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
+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::operator+=(BranchProbability RHS) {
+ assert(N <= D - RHS.N &&
+ "The sum of branch probabilities should not exceed one!");
+ N += RHS.N;
+ return *this;
+}
+
+BranchProbability &BranchProbability::operator-=(BranchProbability RHS) {
+ assert(N >= RHS.N &&
+ "Can only subtract a smaller probability from a larger one!");
+ N -= RHS.N;
+ return *this;
+}
+
+// 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>
+inline 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 +111,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);
}
OpenPOWER on IntegriCloud