[PATCH] D32124: [BPI] Move tail computation out of the loop. NFC

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 19 23:35:09 PDT 2017


skatkov updated this revision to Diff 95893.
skatkov retitled this revision from "[BPI] Follow up rL300440. NFC" to "[BPI] Move tail computation out of the loop. NFC".
skatkov edited the summary of this revision.

https://reviews.llvm.org/D32124

Files:
  include/llvm/Support/BranchProbability.h
  lib/Analysis/BranchProbabilityInfo.cpp


Index: lib/Analysis/BranchProbabilityInfo.cpp
===================================================================
--- lib/Analysis/BranchProbabilityInfo.cpp
+++ lib/Analysis/BranchProbabilityInfo.cpp
@@ -301,6 +301,8 @@
       WeightSum += Weights[i];
     }
   }
+  assert(WeightSum <= UINT32_MAX &&
+         "Expected weights to scale down to 32 bits");
 
   if (WeightSum == 0 || ReachableIdxs.size() == 0) {
     for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
@@ -328,21 +330,18 @@
     // the difference between reachable blocks.
     if (ToDistribute > BranchProbability::getZero()) {
       BranchProbability PerEdge = ToDistribute / ReachableIdxs.size();
-      for (auto i : ReachableIdxs) {
+      for (auto i : ReachableIdxs)
         BP[i] += PerEdge;
-        ToDistribute -= PerEdge;
-      }
       // Tail goes to the first reachable edge.
-      BP[ReachableIdxs[0]] += ToDistribute;
+      BranchProbability Tail = ToDistribute - (PerEdge * ReachableIdxs.size());
+      assert(Tail.getNumerator() < ReachableIdxs.size() && "Tail is too big!");
+      BP[ReachableIdxs[0]] += Tail;
     }
   }
 
   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
     setEdgeProbability(BB, i, BP[i]);
 
-  assert(WeightSum <= UINT32_MAX &&
-         "Expected weights to scale down to 32 bits");
-
   return true;
 }
 
Index: include/llvm/Support/BranchProbability.h
===================================================================
--- include/llvm/Support/BranchProbability.h
+++ include/llvm/Support/BranchProbability.h
@@ -112,6 +112,13 @@
     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.");
@@ -135,6 +142,11 @@
     return Prob *= RHS;
   }
 
+  BranchProbability operator*(uint32_t RHS) const {
+    BranchProbability Prob(*this);
+    return Prob *= RHS;
+  }
+
   BranchProbability operator/(uint32_t RHS) const {
     BranchProbability Prob(*this);
     return Prob /= RHS;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32124.95893.patch
Type: text/x-patch
Size: 2265 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170420/0a1435bf/attachment.bin>


More information about the llvm-commits mailing list