<div dir="ltr">Thanks for letting me know this. Could you please send me some links showing such errors?<div><br></div><div><br></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature">thanks,<br>Cong</div></div>
<br><div class="gmail_quote">On Tue, Dec 22, 2015 at 3:05 PM, Eric Christopher <span dir="ltr"><<a href="mailto:echristo@gmail.com" target="_blank">echristo@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Cong,<div><br></div><div>Just to let you know I'm seeing some (likely) divide by zeros here. I'm looking into it and will either fix, or revert and get a testcase for you.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-eric</div></font></span></div><div class="HOEnZb"><div class="h5"><br><div class="gmail_quote"><div dir="ltr">On Tue, Dec 22, 2015 at 10:59 AM Cong Hou via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: conghou<br>
Date: Tue Dec 22 12:56:14 2015<br>
New Revision: 256263<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=256263&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=256263&view=rev</a><br>
Log:<br>
[BPI] Replace weights by probabilities in BPI.<br>
<br>
This patch removes all weight-related interfaces from BPI and replace<br>
them by probability versions. With this patch, we won't use edge weight<br>
anymore in either IR or MC passes. Edge probabilitiy is a better<br>
representation in terms of CFG update and validation.<br>
<br>
<br>
Differential revision: <a href="http://reviews.llvm.org/D15519" rel="noreferrer" target="_blank">http://reviews.llvm.org/D15519</a><br>
<br>
<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h<br>
    llvm/trunk/include/llvm/Support/BranchProbability.h<br>
    llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp<br>
    llvm/trunk/lib/CodeGen/StackProtector.cpp<br>
    llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp<br>
    llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp<br>
    llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll<br>
    llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll<br>
<br>
Modified: llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h (original)<br>
+++ llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h Tue Dec 22 12:56:14 2015<br>
@@ -84,36 +84,14 @@ public:<br>
   raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,<br>
                                     const BasicBlock *Dst) const;<br>
<br>
-  /// \brief Get the raw edge weight calculated for the edge.<br>
+  /// \brief Set the raw edge probability for the given edge.<br>
   ///<br>
-  /// This returns the raw edge weight. It is guaranteed to fall between 1 and<br>
-  /// UINT32_MAX. Note that the raw edge weight is not meaningful in isolation.<br>
-  /// This interface should be very carefully, and primarily by routines that<br>
-  /// are updating the analysis by later calling setEdgeWeight.<br>
-  uint32_t getEdgeWeight(const BasicBlock *Src,<br>
-                         unsigned IndexInSuccessors) const;<br>
-<br>
-  /// \brief Get the raw edge weight calculated for the block pair.<br>
-  ///<br>
-  /// This returns the sum of all raw edge weights from Src to Dst.<br>
-  /// It is guaranteed to fall between 1 and UINT32_MAX.<br>
-  uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const;<br>
-<br>
-  uint32_t getEdgeWeight(const BasicBlock *Src,<br>
-                         succ_const_iterator Dst) const;<br>
-<br>
-  /// \brief Set the raw edge weight for a given edge.<br>
-  ///<br>
-  /// This allows a pass to explicitly set the edge weight for an edge. It can<br>
-  /// be used when updating the CFG to update and preserve the branch<br>
+  /// This allows a pass to explicitly set the edge probability for an edge. It<br>
+  /// can be used when updating the CFG to update and preserve the branch<br>
   /// probability information. Read the implementation of how these edge<br>
-  /// weights are calculated carefully before using!<br>
-  void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,<br>
-                     uint32_t Weight);<br>
-<br>
-  static uint32_t getBranchWeightStackProtector(bool IsLikely) {<br>
-    return IsLikely ? (1u << 20) - 1 : 1;<br>
-  }<br>
+  /// probabilities are calculated carefully before using!<br>
+  void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors,<br>
+                          BranchProbability Prob);<br>
<br>
   static BranchProbability getBranchProbStackProtector(bool IsLikely) {<br>
     static const BranchProbability LikelyProb((1u << 20) - 1, 1u << 20);<br>
@@ -135,7 +113,7 @@ private:<br>
   // weight to just "inherit" the non-zero weight of an adjacent successor.<br>
   static const uint32_t DEFAULT_WEIGHT = 16;<br>
<br>
-  DenseMap<Edge, uint32_t> Weights;<br>
+  DenseMap<Edge, BranchProbability> Probs;<br>
<br>
   /// \brief Track the last function we run over for printing.<br>
   Function *LastF;<br>
@@ -146,9 +124,6 @@ private:<br>
   /// \brief Track the set of blocks that always lead to a cold call.<br>
   SmallPtrSet<BasicBlock *, 16> PostDominatedByColdCall;<br>
<br>
-  /// \brief Get sum of the block successors' weights.<br>
-  uint32_t getSumForBlock(const BasicBlock *BB) const;<br>
-<br>
   bool calcUnreachableHeuristics(BasicBlock *BB);<br>
   bool calcMetadataWeights(BasicBlock *BB);<br>
   bool calcColdCallHeuristics(BasicBlock *BB);<br>
<br>
Modified: llvm/trunk/include/llvm/Support/BranchProbability.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BranchProbability.h?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BranchProbability.h?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/BranchProbability.h (original)<br>
+++ llvm/trunk/include/llvm/Support/BranchProbability.h Tue Dec 22 12:56:14 2015<br>
@@ -63,11 +63,6 @@ public:<br>
   static void normalizeProbabilities(ProbabilityIter Begin,<br>
                                      ProbabilityIter End);<br>
<br>
-  // Normalize a list of weights by scaling them down so that the sum of them<br>
-  // doesn't exceed UINT32_MAX.<br>
-  template <class WeightListIter><br>
-  static void normalizeEdgeWeights(WeightListIter Begin, WeightListIter End);<br>
-<br>
   uint32_t getNumerator() const { return N; }<br>
   static uint32_t getDenominator() { return D; }<br>
<br>
@@ -219,49 +214,6 @@ void BranchProbability::normalizeProbabi<br>
     I->N = (I->N * uint64_t(D) + Sum / 2) / Sum;<br>
 }<br>
<br>
-template <class WeightListIter><br>
-void BranchProbability::normalizeEdgeWeights(WeightListIter Begin,<br>
-                                             WeightListIter End) {<br>
-  // First we compute the sum with 64-bits of precision.<br>
-  uint64_t Sum = std::accumulate(Begin, End, uint64_t(0));<br>
-<br>
-  if (Sum > UINT32_MAX) {<br>
-    // Compute the scale necessary to cause the weights to fit, and re-sum with<br>
-    // that scale applied.<br>
-    assert(Sum / UINT32_MAX < UINT32_MAX &&<br>
-           "The sum of weights exceeds UINT32_MAX^2!");<br>
-    uint32_t Scale = Sum / UINT32_MAX + 1;<br>
-    for (auto I = Begin; I != End; ++I)<br>
-      *I /= Scale;<br>
-    Sum = std::accumulate(Begin, End, uint64_t(0));<br>
-  }<br>
-<br>
-  // Eliminate zero weights.<br>
-  auto ZeroWeightNum = std::count(Begin, End, 0u);<br>
-  if (ZeroWeightNum > 0) {<br>
-    // If all weights are zeros, replace them by 1.<br>
-    if (Sum == 0)<br>
-      std::fill(Begin, End, 1u);<br>
-    else {<br>
-      // We are converting zeros into ones, and here we need to make sure that<br>
-      // after this the sum won't exceed UINT32_MAX.<br>
-      if (Sum + ZeroWeightNum > UINT32_MAX) {<br>
-        for (auto I = Begin; I != End; ++I)<br>
-          *I /= 2;<br>
-        ZeroWeightNum = std::count(Begin, End, 0u);<br>
-        Sum = std::accumulate(Begin, End, uint64_t(0));<br>
-      }<br>
-      // Scale up non-zero weights and turn zero weights into ones.<br>
-      uint64_t ScalingFactor = (UINT32_MAX - ZeroWeightNum) / Sum;<br>
-      assert(ScalingFactor >= 1);<br>
-      if (ScalingFactor > 1)<br>
-        for (auto I = Begin; I != End; ++I)<br>
-          *I *= ScalingFactor;<br>
-      std::replace(Begin, End, 0u, 1u);<br>
-    }<br>
-  }<br>
-}<br>
-<br>
 }<br>
<br>
 #endif<br>
<br>
Modified: llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp (original)<br>
+++ llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp Tue Dec 22 12:56:14 2015<br>
@@ -108,13 +108,6 @@ static const uint32_t IH_TAKEN_WEIGHT =<br>
 /// instruction. This is essentially never taken.<br>
 static const uint32_t IH_NONTAKEN_WEIGHT = 1;<br>
<br>
-// Standard weight value. Used when none of the heuristics set weight for<br>
-// the edge.<br>
-static const uint32_t NORMAL_WEIGHT = 16;<br>
-<br>
-// Minimum weight of an edge. Please note, that weight is NEVER 0.<br>
-static const uint32_t MIN_WEIGHT = 1;<br>
-<br>
 /// \brief Calculate edge weights for successors lead to unreachable.<br>
 ///<br>
 /// Predict that a successor which leads necessarily to an<br>
@@ -157,22 +150,24 @@ bool BranchProbabilityInfo::calcUnreacha<br>
       return false;<br>
     }<br>
<br>
-  uint32_t UnreachableWeight =<br>
-    std::max(UR_TAKEN_WEIGHT / (unsigned)UnreachableEdges.size(), MIN_WEIGHT);<br>
-  for (SmallVectorImpl<unsigned>::iterator I = UnreachableEdges.begin(),<br>
-                                           E = UnreachableEdges.end();<br>
-       I != E; ++I)<br>
-    setEdgeWeight(BB, *I, UnreachableWeight);<br>
-<br>
-  if (ReachableEdges.empty())<br>
+  if (ReachableEdges.empty()) {<br>
+    BranchProbability Prob(1, UnreachableEdges.size());<br>
+    for (unsigned SuccIdx : UnreachableEdges)<br>
+      setEdgeProbability(BB, SuccIdx, Prob);<br>
     return true;<br>
-  uint32_t ReachableWeight =<br>
-    std::max(UR_NONTAKEN_WEIGHT / (unsigned)ReachableEdges.size(),<br>
-             NORMAL_WEIGHT);<br>
-  for (SmallVectorImpl<unsigned>::iterator I = ReachableEdges.begin(),<br>
-                                           E = ReachableEdges.end();<br>
-       I != E; ++I)<br>
-    setEdgeWeight(BB, *I, ReachableWeight);<br>
+  }<br>
+<br>
+  BranchProbability UnreachableProb(UR_TAKEN_WEIGHT,<br>
+                                    (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) *<br>
+                                        UnreachableEdges.size());<br>
+  BranchProbability ReachableProb(UR_NONTAKEN_WEIGHT,<br>
+                                  (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) *<br>
+                                      ReachableEdges.size());<br>
+<br>
+  for (unsigned SuccIdx : UnreachableEdges)<br>
+    setEdgeProbability(BB, SuccIdx, UnreachableProb);<br>
+  for (unsigned SuccIdx : ReachableEdges)<br>
+    setEdgeProbability(BB, SuccIdx, ReachableProb);<br>
<br>
   return true;<br>
 }<br>
@@ -223,10 +218,12 @@ bool BranchProbabilityInfo::calcMetadata<br>
<br>
   WeightSum = 0;<br>
   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {<br>
-    uint32_t W = Weights[i] / ScalingFactor;<br>
-    WeightSum += W;<br>
-    setEdgeWeight(BB, i, W);<br>
+    Weights[i] /= ScalingFactor;<br>
+    WeightSum += Weights[i];<br>
   }<br>
+  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)<br>
+    setEdgeProbability(BB, i, {Weights[i], static_cast<uint32_t>(WeightSum)});<br>
+<br>
   assert(WeightSum <= UINT32_MAX &&<br>
          "Expected weights to scale down to 32 bits");<br>
<br>
@@ -275,21 +272,24 @@ bool BranchProbabilityInfo::calcColdCall<br>
   if (TI->getNumSuccessors() == 1 || ColdEdges.empty())<br>
     return false;<br>
<br>
-  uint32_t ColdWeight =<br>
-      std::max(CC_TAKEN_WEIGHT / (unsigned) ColdEdges.size(), MIN_WEIGHT);<br>
-  for (SmallVectorImpl<unsigned>::iterator I = ColdEdges.begin(),<br>
-                                           E = ColdEdges.end();<br>
-       I != E; ++I)<br>
-    setEdgeWeight(BB, *I, ColdWeight);<br>
-<br>
-  if (NormalEdges.empty())<br>
+  if (NormalEdges.empty()) {<br>
+    BranchProbability Prob(1, ColdEdges.size());<br>
+    for (unsigned SuccIdx : ColdEdges)<br>
+      setEdgeProbability(BB, SuccIdx, Prob);<br>
     return true;<br>
-  uint32_t NormalWeight = std::max(<br>
-      CC_NONTAKEN_WEIGHT / (unsigned) NormalEdges.size(), NORMAL_WEIGHT);<br>
-  for (SmallVectorImpl<unsigned>::iterator I = NormalEdges.begin(),<br>
-                                           E = NormalEdges.end();<br>
-       I != E; ++I)<br>
-    setEdgeWeight(BB, *I, NormalWeight);<br>
+  }<br>
+<br>
+  BranchProbability ColdProb(CC_TAKEN_WEIGHT,<br>
+                             (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) *<br>
+                                 ColdEdges.size());<br>
+  BranchProbability NormalProb(CC_NONTAKEN_WEIGHT,<br>
+                               (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) *<br>
+                                   NormalEdges.size());<br>
+<br>
+  for (unsigned SuccIdx : ColdEdges)<br>
+    setEdgeProbability(BB, SuccIdx, ColdProb);<br>
+  for (unsigned SuccIdx : NormalEdges)<br>
+    setEdgeProbability(BB, SuccIdx, NormalProb);<br>
<br>
   return true;<br>
 }<br>
@@ -322,8 +322,10 @@ bool BranchProbabilityInfo::calcPointerH<br>
   if (!isProb)<br>
     std::swap(TakenIdx, NonTakenIdx);<br>
<br>
-  setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT);<br>
-  setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT);<br>
+  BranchProbability TakenProb(PH_TAKEN_WEIGHT,<br>
+                              PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);<br>
+  setEdgeProbability(BB, TakenIdx, TakenProb);<br>
+  setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());<br>
   return true;<br>
 }<br>
<br>
@@ -351,37 +353,35 @@ bool BranchProbabilityInfo::calcLoopBran<br>
   if (BackEdges.empty() && ExitingEdges.empty())<br>
     return false;<br>
<br>
+  // Collect the sum of probabilities of back-edges/in-edges/exiting-edges, and<br>
+  // normalize them so that they sum up to one.<br>
+  SmallVector<BranchProbability, 4> Probs(3, BranchProbability::getZero());<br>
+  unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +<br>
+                   (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +<br>
+                   (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT);<br>
+  if (!BackEdges.empty())<br>
+    Probs[0] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);<br>
+  if (!InEdges.empty())<br>
+    Probs[1] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);<br>
+  if (!ExitingEdges.empty())<br>
+    Probs[2] = BranchProbability(LBH_NONTAKEN_WEIGHT, Denom);<br>
+<br>
   if (uint32_t numBackEdges = BackEdges.size()) {<br>
-    uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;<br>
-    if (backWeight < NORMAL_WEIGHT)<br>
-      backWeight = NORMAL_WEIGHT;<br>
-<br>
-    for (SmallVectorImpl<unsigned>::iterator EI = BackEdges.begin(),<br>
-         EE = BackEdges.end(); EI != EE; ++EI) {<br>
-      setEdgeWeight(BB, *EI, backWeight);<br>
-    }<br>
+    auto Prob = Probs[0] / numBackEdges;<br>
+    for (unsigned SuccIdx : BackEdges)<br>
+      setEdgeProbability(BB, SuccIdx, Prob);<br>
   }<br>
<br>
   if (uint32_t numInEdges = InEdges.size()) {<br>
-    uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;<br>
-    if (inWeight < NORMAL_WEIGHT)<br>
-      inWeight = NORMAL_WEIGHT;<br>
-<br>
-    for (SmallVectorImpl<unsigned>::iterator EI = InEdges.begin(),<br>
-         EE = InEdges.end(); EI != EE; ++EI) {<br>
-      setEdgeWeight(BB, *EI, inWeight);<br>
-    }<br>
+    auto Prob = Probs[1] / numInEdges;<br>
+    for (unsigned SuccIdx : InEdges)<br>
+      setEdgeProbability(BB, SuccIdx, Prob);<br>
   }<br>
<br>
   if (uint32_t numExitingEdges = ExitingEdges.size()) {<br>
-    uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;<br>
-    if (exitWeight < MIN_WEIGHT)<br>
-      exitWeight = MIN_WEIGHT;<br>
-<br>
-    for (SmallVectorImpl<unsigned>::iterator EI = ExitingEdges.begin(),<br>
-         EE = ExitingEdges.end(); EI != EE; ++EI) {<br>
-      setEdgeWeight(BB, *EI, exitWeight);<br>
-    }<br>
+    auto Prob = Probs[2] / numExitingEdges;<br>
+    for (unsigned SuccIdx : ExitingEdges)<br>
+      setEdgeProbability(BB, SuccIdx, Prob);<br>
   }<br>
<br>
   return true;<br>
@@ -463,9 +463,10 @@ bool BranchProbabilityInfo::calcZeroHeur<br>
   if (!isProb)<br>
     std::swap(TakenIdx, NonTakenIdx);<br>
<br>
-  setEdgeWeight(BB, TakenIdx, ZH_TAKEN_WEIGHT);<br>
-  setEdgeWeight(BB, NonTakenIdx, ZH_NONTAKEN_WEIGHT);<br>
-<br>
+  BranchProbability TakenProb(ZH_TAKEN_WEIGHT,<br>
+                              ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);<br>
+  setEdgeProbability(BB, TakenIdx, TakenProb);<br>
+  setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());<br>
   return true;<br>
 }<br>
<br>
@@ -499,9 +500,10 @@ bool BranchProbabilityInfo::calcFloating<br>
   if (!isProb)<br>
     std::swap(TakenIdx, NonTakenIdx);<br>
<br>
-  setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT);<br>
-  setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT);<br>
-<br>
+  BranchProbability TakenProb(FPH_TAKEN_WEIGHT,<br>
+                              FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);<br>
+  setEdgeProbability(BB, TakenIdx, TakenProb);<br>
+  setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());<br>
   return true;<br>
 }<br>
<br>
@@ -510,13 +512,15 @@ bool BranchProbabilityInfo::calcInvokeHe<br>
   if (!II)<br>
     return false;<br>
<br>
-  setEdgeWeight(BB, 0/*Index for Normal*/, IH_TAKEN_WEIGHT);<br>
-  setEdgeWeight(BB, 1/*Index for Unwind*/, IH_NONTAKEN_WEIGHT);<br>
+  BranchProbability TakenProb(IH_TAKEN_WEIGHT,<br>
+                              IH_TAKEN_WEIGHT + IH_NONTAKEN_WEIGHT);<br>
+  setEdgeProbability(BB, 0 /*Index for Normal*/, TakenProb);<br>
+  setEdgeProbability(BB, 1 /*Index for Unwind*/, TakenProb.getCompl());<br>
   return true;<br>
 }<br>
<br>
 void BranchProbabilityInfo::releaseMemory() {<br>
-  Weights.clear();<br>
+  Probs.clear();<br>
 }<br>
<br>
 void BranchProbabilityInfo::print(raw_ostream &OS) const {<br>
@@ -532,20 +536,6 @@ void BranchProbabilityInfo::print(raw_os<br>
   }<br>
 }<br>
<br>
-uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {<br>
-  uint32_t Sum = 0;<br>
-<br>
-  for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {<br>
-    uint32_t Weight = getEdgeWeight(BB, I.getSuccessorIndex());<br>
-    uint32_t PrevSum = Sum;<br>
-<br>
-    Sum += Weight;<br>
-    assert(Sum >= PrevSum); (void) PrevSum;<br>
-  }<br>
-<br>
-  return Sum;<br>
-}<br>
-<br>
 bool BranchProbabilityInfo::<br>
 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {<br>
   // Hot probability is at least 4/5 = 80%<br>
@@ -554,113 +544,74 @@ isEdgeHot(const BasicBlock *Src, const B<br>
 }<br>
<br>
 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {<br>
-  uint32_t Sum = 0;<br>
-  uint32_t MaxWeight = 0;<br>
+  auto MaxProb = BranchProbability::getZero();<br>
   BasicBlock *MaxSucc = nullptr;<br>
<br>
   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {<br>
     BasicBlock *Succ = *I;<br>
-    uint32_t Weight = getEdgeWeight(BB, Succ);<br>
-    uint32_t PrevSum = Sum;<br>
-<br>
-    Sum += Weight;<br>
-    assert(Sum > PrevSum); (void) PrevSum;<br>
-<br>
-    if (Weight > MaxWeight) {<br>
-      MaxWeight = Weight;<br>
+    auto Prob = getEdgeProbability(BB, Succ);<br>
+    if (Prob > MaxProb) {<br>
+      MaxProb = Prob;<br>
       MaxSucc = Succ;<br>
     }<br>
   }<br>
<br>
   // Hot probability is at least 4/5 = 80%<br>
-  if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))<br>
+  if (MaxProb > BranchProbability(4, 5))<br>
     return MaxSucc;<br>
<br>
   return nullptr;<br>
 }<br>
<br>
-/// Get the raw edge weight for the edge. If can't find it, return<br>
-/// DEFAULT_WEIGHT value. Here an edge is specified using PredBlock and an index<br>
-/// to the successors.<br>
-uint32_t BranchProbabilityInfo::<br>
-getEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors) const {<br>
-  DenseMap<Edge, uint32_t>::const_iterator I =<br>
-      Weights.find(std::make_pair(Src, IndexInSuccessors));<br>
+/// Get the raw edge probability for the edge. If can't find it, return a<br>
+/// default probability 1/N where N is the number of successors. Here an edge is<br>
+/// specified using PredBlock and an<br>
+/// index to the successors.<br>
+BranchProbability<br>
+BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,<br>
+                                          unsigned IndexInSuccessors) const {<br>
+  auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));<br>
<br>
-  if (I != Weights.end())<br>
+  if (I != Probs.end())<br>
     return I->second;<br>
<br>
-  return DEFAULT_WEIGHT;<br>
+  return {1,<br>
+          static_cast<uint32_t>(std::distance(succ_begin(Src), succ_end(Src)))};<br>
 }<br>
<br>
-uint32_t BranchProbabilityInfo::getEdgeWeight(const BasicBlock *Src,<br>
-                                              succ_const_iterator Dst) const {<br>
-  return getEdgeWeight(Src, Dst.getSuccessorIndex());<br>
+BranchProbability<br>
+BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,<br>
+                                          succ_const_iterator Dst) const {<br>
+  return getEdgeProbability(Src, Dst.getSuccessorIndex());<br>
 }<br>
<br>
-/// Get the raw edge weight calculated for the block pair. This returns the sum<br>
-/// of all raw edge weights from Src to Dst.<br>
-uint32_t BranchProbabilityInfo::<br>
-getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {<br>
-  uint32_t Weight = 0;<br>
-  bool FoundWeight = false;<br>
-  DenseMap<Edge, uint32_t>::const_iterator MapI;<br>
+/// Get the raw edge probability calculated for the block pair. This returns the<br>
+/// sum of all raw edge probabilities from Src to Dst.<br>
+BranchProbability<br>
+BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,<br>
+                                          const BasicBlock *Dst) const {<br>
+  auto Prob = BranchProbability::getZero();<br>
+  bool FoundProb = false;<br>
   for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I != E; ++I)<br>
     if (*I == Dst) {<br>
-      MapI = Weights.find(std::make_pair(Src, I.getSuccessorIndex()));<br>
-      if (MapI != Weights.end()) {<br>
-        FoundWeight = true;<br>
-        Weight += MapI->second;<br>
+      auto MapI = Probs.find(std::make_pair(Src, I.getSuccessorIndex()));<br>
+      if (MapI != Probs.end()) {<br>
+        FoundProb = true;<br>
+        Prob += MapI->second;<br>
       }<br>
     }<br>
-  return (!FoundWeight) ? DEFAULT_WEIGHT : Weight;<br>
-}<br>
-<br>
-/// Set the edge weight for a given edge specified by PredBlock and an index<br>
-/// to the successors.<br>
-void BranchProbabilityInfo::<br>
-setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,<br>
-              uint32_t Weight) {<br>
-  Weights[std::make_pair(Src, IndexInSuccessors)] = Weight;<br>
-  DEBUG(dbgs() << "set edge " << Src->getName() << " -> "<br>
-               << IndexInSuccessors << " successor weight to "<br>
-               << Weight << "\n");<br>
+  uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src));<br>
+  return FoundProb ? Prob : BranchProbability(1, succ_num);<br>
 }<br>
<br>
-/// Get an edge's probability, relative to other out-edges from Src.<br>
-BranchProbability BranchProbabilityInfo::<br>
-getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const {<br>
-  uint32_t N = getEdgeWeight(Src, IndexInSuccessors);<br>
-  uint32_t D = getSumForBlock(Src);<br>
-<br>
-  // It is possible that the edge weight on the only successor edge of Src is<br>
-  // zero, in which case we return 100%.<br>
-  if (N == 0 && D == 0)<br>
-    return BranchProbability::getOne();<br>
-<br>
-  return BranchProbability(N, D);<br>
-}<br>
-<br>
-/// Get the probability of going from Src to Dst. It returns the sum of all<br>
-/// probabilities for edges from Src to Dst.<br>
-BranchProbability BranchProbabilityInfo::<br>
-getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {<br>
-<br>
-  uint32_t N = getEdgeWeight(Src, Dst);<br>
-  uint32_t D = getSumForBlock(Src);<br>
-<br>
-  // It is possible that the edge weight on the only successor edge of Src is<br>
-  // zero, in which case we return 100%.<br>
-  if (N == 0 && D == 0)<br>
-    return BranchProbability::getOne();<br>
-<br>
-  return BranchProbability(N, D);<br>
-}<br>
-<br>
-BranchProbability<br>
-BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,<br>
-                                          succ_const_iterator Dst) const {<br>
-  return getEdgeProbability(Src, Dst.getSuccessorIndex());<br>
+/// Set the edge probability for a given edge specified by PredBlock and an<br>
+/// index to the successors.<br>
+void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src,<br>
+                                               unsigned IndexInSuccessors,<br>
+                                               BranchProbability Prob) {<br>
+  Probs[std::make_pair(Src, IndexInSuccessors)] = Prob;<br>
+  DEBUG(dbgs() << "set edge " << Src->getName() << " -> " << IndexInSuccessors<br>
+               << " successor probability to " << Prob << "\n");<br>
 }<br>
<br>
 raw_ostream &<br>
<br>
Modified: llvm/trunk/lib/CodeGen/StackProtector.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackProtector.cpp?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackProtector.cpp?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/StackProtector.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/StackProtector.cpp Tue Dec 22 12:56:14 2015<br>
@@ -453,12 +453,13 @@ bool StackProtector::InsertStackProtecto<br>
       LoadInst *LI1 = B.CreateLoad(StackGuardVar);<br>
       LoadInst *LI2 = B.CreateLoad(AI);<br>
       Value *Cmp = B.CreateICmpEQ(LI1, LI2);<br>
-      unsigned SuccessWeight =<br>
-          BranchProbabilityInfo::getBranchWeightStackProtector(true);<br>
-      unsigned FailureWeight =<br>
-          BranchProbabilityInfo::getBranchWeightStackProtector(false);<br>
+      auto SuccessProb =<br>
+          BranchProbabilityInfo::getBranchProbStackProtector(true);<br>
+      auto FailureProb =<br>
+          BranchProbabilityInfo::getBranchProbStackProtector(false);<br>
       MDNode *Weights = MDBuilder(F->getContext())<br>
-                            .createBranchWeights(SuccessWeight, FailureWeight);<br>
+                            .createBranchWeights(SuccessProb.getNumerator(),<br>
+                                                 FailureProb.getNumerator());<br>
       B.CreateCondBr(Cmp, NewBB, FailBB, Weights);<br>
     }<br>
   }<br>
<br>
Modified: llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp (original)<br>
+++ llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp Tue Dec 22 12:56:14 2015<br>
@@ -414,8 +414,8 @@ static unsigned getBranchHint(unsigned P<br>
   const BasicBlock *TBB = BBTerm->getSuccessor(0);<br>
   const BasicBlock *FBB = BBTerm->getSuccessor(1);<br>
<br>
-  uint32_t TWeight = FuncInfo->BPI->getEdgeWeight(BB, TBB);<br>
-  uint32_t FWeight = FuncInfo->BPI->getEdgeWeight(BB, FBB);<br>
+  auto TProb = FuncInfo->BPI->getEdgeProbability(BB, TBB);<br>
+  auto FProb = FuncInfo->BPI->getEdgeProbability(BB, FBB);<br>
<br>
   // We only want to handle cases which are easy to predict at static time, e.g.<br>
   // C++ throw statement, that is very likely not taken, or calling never<br>
@@ -432,24 +432,22 @@ static unsigned getBranchHint(unsigned P<br>
   // 5. PH/ZH/FPH          20:12<br>
   const uint32_t Threshold = 10000;<br>
<br>
-  // Minimal weight should be at least 1<br>
-  if (std::max(TWeight, FWeight) /<br>
-      std::max(1u, std::min(TWeight, FWeight)) < Threshold)<br>
+  if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb))<br>
     return PPC::BR_NO_HINT;<br>
<br>
   DEBUG(dbgs() << "Use branch hint for '" << FuncInfo->Fn->getName() << "::"<br>
                << BB->getName() << "'\n"<br>
-               << " -> " << TBB->getName() << ": " << TWeight << "\n"<br>
-               << " -> " << FBB->getName() << ": " << FWeight << "\n");<br>
+               << " -> " << TBB->getName() << ": " << TProb << "\n"<br>
+               << " -> " << FBB->getName() << ": " << FProb << "\n");<br>
<br>
   const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB);<br>
<br>
-  // If Dest BasicBlock is False-BasicBlock (FBB), swap branch weight,<br>
-  // because we want 'TWeight' stands for 'branch weight' to Dest BasicBlock<br>
+  // If Dest BasicBlock is False-BasicBlock (FBB), swap branch probabilities,<br>
+  // because we want 'TProb' stands for 'branch probability' to Dest BasicBlock<br>
   if (BBDN->getBasicBlock()->getBasicBlock() != TBB)<br>
-    std::swap(TWeight, FWeight);<br>
+    std::swap(TProb, FProb);<br>
<br>
-  return (TWeight > FWeight) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT;<br>
+  return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT;<br>
 }<br>
<br>
 // isOpcWithIntImmediate - This method tests to see if the node is a specific<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp Tue Dec 22 12:56:14 2015<br>
@@ -1636,7 +1636,7 @@ void JumpThreading::UpdateBlockFreqAndEd<br>
   BFI->setBlockFreq(BB, BBNewFreq.getFrequency());<br>
<br>
   // Collect updated outgoing edges' frequencies from BB and use them to update<br>
-  // edge weights.<br>
+  // edge probabilities.<br>
   SmallVector<uint64_t, 4> BBSuccFreq;<br>
   for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {<br>
     auto SuccFreq = (*I == SuccBB)<br>
@@ -1645,18 +1645,26 @@ void JumpThreading::UpdateBlockFreqAndEd<br>
     BBSuccFreq.push_back(SuccFreq.getFrequency());<br>
   }<br>
<br>
-  // Normalize edge weights in Weights64 so that the sum of them can fit in<br>
-  BranchProbability::normalizeEdgeWeights(BBSuccFreq.begin(), BBSuccFreq.end());<br>
+  uint64_t MaxBBSuccFreq =<br>
+      *std::max_element(BBSuccFreq.begin(), BBSuccFreq.end());<br>
+  SmallVector<BranchProbability, 4> BBSuccProbs;<br>
+  for (uint64_t Freq : BBSuccFreq)<br>
+    BBSuccProbs.push_back(<br>
+        BranchProbability::getBranchProbability(Freq, MaxBBSuccFreq));<br>
<br>
-  SmallVector<uint32_t, 4> Weights;<br>
-  for (auto Freq : BBSuccFreq)<br>
-    Weights.push_back(static_cast<uint32_t>(Freq));<br>
-<br>
-  // Update edge weights in BPI.<br>
-  for (int I = 0, E = Weights.size(); I < E; I++)<br>
-    BPI->setEdgeWeight(BB, I, Weights[I]);<br>
+  // Normalize edge probabilities so that they sum up to one.<br>
+  BranchProbability::normalizeProbabilities(BBSuccProbs.begin(),<br>
+                                            BBSuccProbs.end());<br>
+<br>
+  // Update edge probabilities in BPI.<br>
+  for (int I = 0, E = BBSuccProbs.size(); I < E; I++)<br>
+    BPI->setEdgeProbability(BB, I, BBSuccProbs[I]);<br>
+<br>
+  if (BBSuccProbs.size() >= 2) {<br>
+    SmallVector<uint32_t, 4> Weights;<br>
+    for (auto Prob : BBSuccProbs)<br>
+      Weights.push_back(Prob.getNumerator());<br>
<br>
-  if (Weights.size() >= 2) {<br>
     auto TI = BB->getTerminator();<br>
     TI->setMetadata(<br>
         LLVMContext::MD_prof,<br>
<br>
Modified: llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll (original)<br>
+++ llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll Tue Dec 22 12:56:14 2015<br>
@@ -26,11 +26,11 @@ entry:<br>
                               i32 2, label %case_b<br>
                               i32 3, label %case_c<br>
                               i32 4, label %case_d]<br>
-; CHECK: edge entry -> exit probability is 0x7fffe000 / 0x80000000 = 100.00% [HOT edge]<br>
-; CHECK: edge entry -> case_a probability is 0x00000800 / 0x80000000 = 0.00%<br>
-; CHECK: edge entry -> case_b probability is 0x00000800 / 0x80000000 = 0.00%<br>
-; CHECK: edge entry -> case_c probability is 0x00000800 / 0x80000000 = 0.00%<br>
-; CHECK: edge entry -> case_d probability is 0x00000800 / 0x80000000 = 0.00%<br>
+; CHECK: edge entry -> exit probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]<br>
+; CHECK: edge entry -> case_a probability is 0x00000200 / 0x80000000 = 0.00%<br>
+; CHECK: edge entry -> case_b probability is 0x00000200 / 0x80000000 = 0.00%<br>
+; CHECK: edge entry -> case_c probability is 0x00000200 / 0x80000000 = 0.00%<br>
+; CHECK: edge entry -> case_d probability is 0x00000200 / 0x80000000 = 0.00%<br>
<br>
 case_a:<br>
   br label %case_b<br>
<br>
Modified: llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll?rev=256263&r1=256262&r2=256263&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll?rev=256263&r1=256262&r2=256263&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll (original)<br>
+++ llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll Tue Dec 22 12:56:14 2015<br>
@@ -2,7 +2,7 @@<br>
<br>
 ; Test if edge weights are properly updated after jump threading.<br>
<br>
-; CHECK: !2 = !{!"branch_weights", i32 22, i32 7}<br>
+; CHECK: !2 = !{!"branch_weights", i32 1629125526, i32 518358122}<br>
<br>
 define void @foo(i32 %n) !prof !0 {<br>
 entry:<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>
</div></div></blockquote></div><br></div>