[llvm] r253421 - Let += and -= operators in BranchProbability have saturation behaviors.

Cong Hou via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 17 17:20:37 PST 2015


Author: conghou
Date: Tue Nov 17 19:20:37 2015
New Revision: 253421

URL: http://llvm.org/viewvc/llvm-project?rev=253421&view=rev
Log:
Let += and -= operators in BranchProbability have saturation behaviors.

This commit is for a later patch that is depend on it. The sum of two
branch probabilities can be greater than 1 due to rounding. It is safer
to saturate the results of sum and subtraction.



Modified:
    llvm/trunk/include/llvm/Support/BranchProbability.h

Modified: llvm/trunk/include/llvm/Support/BranchProbability.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BranchProbability.h?rev=253421&r1=253420&r2=253421&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/BranchProbability.h (original)
+++ llvm/trunk/include/llvm/Support/BranchProbability.h Tue Nov 17 19:20:37 2015
@@ -92,16 +92,14 @@ public:
   uint64_t scaleByInverse(uint64_t Num) const;
 
   BranchProbability &operator+=(BranchProbability RHS) {
-    assert(N <= D - RHS.N &&
-           "The sum of branch probabilities should not exceed one!");
-    N += RHS.N;
+    // Saturate the result in case of overflow.
+    N = (uint64_t(N) + RHS.N > D) ? D : N + RHS.N;
     return *this;
   }
 
   BranchProbability &operator-=(BranchProbability RHS) {
-    assert(N >= RHS.N &&
-           "Can only subtract a smaller probability from a larger one!");
-    N -= RHS.N;
+    // Saturate the result in case of underflow.
+    N = N < RHS.N ? 0 : N - RHS.N;
     return *this;
   }
 




More information about the llvm-commits mailing list