aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/BranchProbability.cpp
diff options
context:
space:
mode:
authorCong Hou <congh@google.com>2015-12-01 00:02:51 +0000
committerCong Hou <congh@google.com>2015-12-01 00:02:51 +0000
commitfa1917c673aee79943eeca1319ebb6bcb0f1229f (patch)
tree9881e2f14716710ee86910e114b3d665921bfa6f /llvm/lib/Support/BranchProbability.cpp
parente9841a6bb5fd4fac1e63255da79306454e1997a0 (diff)
downloadllvm-fa1917c673aee79943eeca1319ebb6bcb0f1229f.zip
llvm-fa1917c673aee79943eeca1319ebb6bcb0f1229f.tar.gz
llvm-fa1917c673aee79943eeca1319ebb6bcb0f1229f.tar.bz2
Replace all weight-based interfaces in MBB with probability-based interfaces, and update all uses of old interfaces.
The patch in http://reviews.llvm.org/D13745 is broken into four parts: 1. New interfaces without functional changes (http://reviews.llvm.org/D13908). 2. Use new interfaces in SelectionDAG, while in other passes treat probabilities as weights (http://reviews.llvm.org/D14361). 3. Use new interfaces in all other passes. 4. Remove old interfaces. This patch is 3+4 above. In this patch, MBB won't provide weight-based interfaces any more, which are totally replaced by probability-based ones. The interface addSuccessor() is redesigned so that the default probability is unknown. We allow unknown probabilities but don't allow using it together with known probabilities in successor list. That is to say, we either have a list of successors with all known probabilities, or all unknown probabilities. In the latter case, we assume each successor has 1/N probability where N is the number of successors. An assertion checks if the user is attempting to add a successor with the disallowed mixed use as stated above. This can help us catch many misuses. All uses of weight-based interfaces are now updated to use probability-based ones. Differential revision: http://reviews.llvm.org/D14973 llvm-svn: 254348
Diffstat (limited to 'llvm/lib/Support/BranchProbability.cpp')
-rw-r--r--llvm/lib/Support/BranchProbability.cpp20
1 files changed, 18 insertions, 2 deletions
diff --git a/llvm/lib/Support/BranchProbability.cpp b/llvm/lib/Support/BranchProbability.cpp
index 3b0f6e6..771d02c 100644
--- a/llvm/lib/Support/BranchProbability.cpp
+++ b/llvm/lib/Support/BranchProbability.cpp
@@ -22,11 +22,14 @@ using namespace llvm;
const uint32_t BranchProbability::D;
raw_ostream &BranchProbability::print(raw_ostream &OS) const {
+ 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;
- OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D, Percent);
- return OS;
+ return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
+ Percent);
}
void BranchProbability::dump() const { print(dbgs()) << '\n'; }
@@ -43,6 +46,19 @@ BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
}
}
+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.