[llvm] r300945 - [BPI] Add multiplication by scalar operators to BranchProbability

Serguei Katkov via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 20 20:14:30 PDT 2017


Author: skatkov
Date: Thu Apr 20 22:14:30 2017
New Revision: 300945

URL: http://llvm.org/viewvc/llvm-project?rev=300945&view=rev
Log:
[BPI] Add multiplication by scalar operators to BranchProbability

This patch just adds two operators to BranchProbability class:
(BP * scalar) and (BP *= scalar).

Reviewers: junbuml, chandlerc, sanjoy, vsk

Reviewed By: chandlerc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32334

Modified:
    llvm/trunk/include/llvm/Support/BranchProbability.h
    llvm/trunk/unittests/Support/BranchProbabilityTest.cpp

Modified: llvm/trunk/include/llvm/Support/BranchProbability.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BranchProbability.h?rev=300945&r1=300944&r2=300945&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/BranchProbability.h (original)
+++ llvm/trunk/include/llvm/Support/BranchProbability.h Thu Apr 20 22:14:30 2017
@@ -112,6 +112,13 @@ public:
     return *this;
   }
 
+  BranchProbability &operator*=(uint32_t RHS) {
+    assert(N != UnknownN &&
+           "Unknown probability cannot participate in arithmetics.");
+    N = (uint64_t(N) * RHS > D) ? D : N * RHS;
+    return *this;
+  }
+
   BranchProbability &operator/=(uint32_t RHS) {
     assert(N != UnknownN &&
            "Unknown probability cannot participate in arithmetics.");
@@ -134,6 +141,11 @@ public:
     BranchProbability Prob(*this);
     return Prob *= RHS;
   }
+
+  BranchProbability operator*(uint32_t RHS) const {
+    BranchProbability Prob(*this);
+    return Prob *= RHS;
+  }
 
   BranchProbability operator/(uint32_t RHS) const {
     BranchProbability Prob(*this);

Modified: llvm/trunk/unittests/Support/BranchProbabilityTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/BranchProbabilityTest.cpp?rev=300945&r1=300944&r2=300945&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/BranchProbabilityTest.cpp (original)
+++ llvm/trunk/unittests/Support/BranchProbabilityTest.cpp Thu Apr 20 22:14:30 2017
@@ -115,6 +115,54 @@ TEST(BranchProbabilityTest, MoreOperator
   EXPECT_FALSE(BigZero >= BigOne);
 }
 
+TEST(BranchProbabilityTest, ArithmeticOperators) {
+  BP Z(0, 1);
+  BP O(1, 1);
+  BP H(1, 2);
+  BP Q(1, 4);
+  BP Q3(3, 4);
+
+  EXPECT_EQ(Z + O, O);
+  EXPECT_EQ(H + Z, H);
+  EXPECT_EQ(H + H, O);
+  EXPECT_EQ(Q + H, Q3);
+  EXPECT_EQ(Q + Q3, O);
+  EXPECT_EQ(H + Q3, O);
+  EXPECT_EQ(Q3 + Q3, O);
+
+  EXPECT_EQ(Z - O, Z);
+  EXPECT_EQ(O - Z, O);
+  EXPECT_EQ(O - H, H);
+  EXPECT_EQ(O - Q, Q3);
+  EXPECT_EQ(Q3 - H, Q);
+  EXPECT_EQ(Q - H, Z);
+  EXPECT_EQ(Q - Q3, Z);
+
+  EXPECT_EQ(Z * O, Z);
+  EXPECT_EQ(H * H, Q);
+  EXPECT_EQ(Q * O, Q);
+  EXPECT_EQ(O * O, O);
+  EXPECT_EQ(Z * Z, Z);
+
+  EXPECT_EQ(Z * 3, Z);
+  EXPECT_EQ(Q * 3, Q3);
+  EXPECT_EQ(H * 3, O);
+  EXPECT_EQ(Q3 * 2, O);
+  EXPECT_EQ(O * UINT32_MAX, O);
+
+  EXPECT_EQ(Z / 4, Z);
+  EXPECT_EQ(O / 4, Q);
+  EXPECT_EQ(Q3 / 3, Q);
+  EXPECT_EQ(H / 2, Q);
+  EXPECT_EQ(O / 2, H);
+  EXPECT_EQ(H / UINT32_MAX, Z);
+
+  BP Min(1, 1u << 31);
+
+  EXPECT_EQ(O / UINT32_MAX, Z);
+  EXPECT_EQ(Min * UINT32_MAX, O);
+}
+
 TEST(BranchProbabilityTest, getCompl) {
   EXPECT_EQ(BP(5, 7), BP(2, 7).getCompl());
   EXPECT_EQ(BP(2, 7), BP(5, 7).getCompl());




More information about the llvm-commits mailing list