[llvm-commits] [llvm] r133776 - in /llvm/trunk: include/llvm/Analysis/BlockFrequencyImpl.h include/llvm/Analysis/BranchProbabilityInfo.h lib/Analysis/BranchProbabilityInfo.cpp

Jakub Staszak jstaszak at apple.com
Thu Jun 23 16:52:11 PDT 2011


Author: kuba
Date: Thu Jun 23 18:52:11 2011
New Revision: 133776

URL: http://llvm.org/viewvc/llvm-project?rev=133776&view=rev
Log:
Calculate backedge probability correctly.

Modified:
    llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h
    llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h
    llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp

Modified: llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h?rev=133776&r1=133775&r2=133776&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h (original)
+++ llvm/trunk/include/llvm/Analysis/BlockFrequencyImpl.h Thu Jun 23 18:52:11 2011
@@ -133,6 +133,15 @@
   }
 
 
+  /// Return a probability of getting to the DST block through SRC->DST edge.
+  ///
+  BranchProbability getBackEdgeProbability(BlockT *Src, BlockT *Dst) const {
+    uint32_t N = getEdgeFreq(Src, Dst);
+    uint32_t D = getBlockFreq(Dst);
+
+    return BranchProbability(N, D);
+  }
+
   /// isReachable - Returns if BB block is reachable from the entry.
   ///
   bool isReachable(BlockT *BB) {
@@ -213,7 +222,9 @@
       return;
 
     assert(START_FREQ >= CycleProb[BB]);
-    divBlockFreq(BB, BranchProbability(START_FREQ - CycleProb[BB], START_FREQ));
+    uint32_t CProb = CycleProb[BB];
+    uint32_t Numerator = START_FREQ - CProb ? START_FREQ - CProb : 1;
+    divBlockFreq(BB, BranchProbability(Numerator, START_FREQ));
   }
 
   /// doLoop - Propagate block frequency down throught the loop.
@@ -238,19 +249,17 @@
       BlockT *Pred = *PI;
       assert(Pred);
       if (isReachable(Pred) && isBackedge(Pred, Head)) {
-        BranchProbability Prob = BPI->getBackEdgeProbability(Pred, Head);
+        BranchProbability Prob = getBackEdgeProbability(Pred, Head);
         uint64_t N = Prob.getNumerator();
         uint64_t D = Prob.getDenominator();
         uint64_t Res = (N * START_FREQ) / D;
 
-        // CycleProb[Head] += getEdgeFreq(Pred, Head);
         assert(Res <= UINT32_MAX);
         CycleProb[Head] += (uint32_t) Res;
       }
     }
   }
 
-
   friend class BlockFrequency;
 
   void doFunction(FunctionT *fn, BlockProbInfoT *bpi) {

Modified: llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h?rev=133776&r1=133775&r2=133776&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h Thu Jun 23 18:52:11 2011
@@ -39,9 +39,6 @@
   // Get sum of the block successors' weights.
   uint32_t getSumForBlock(BasicBlock *BB) const;
 
-  // Get sum of the edge weights going to the BB block.
-  uint32_t getBackSumForBlock(BasicBlock *BB) const;
-
 public:
   static char ID;
 
@@ -74,13 +71,6 @@
   // only iff SRC block has only one successor.
   BranchProbability getEdgeProbability(BasicBlock *Src, BasicBlock *Dst) const;
 
-  // Return a probability of getting to the DST block through SRC->DST edge.
-  // Returned value is a fraction between 0 (0% probability) and
-  // 1 (100% probability), however the value is never equal to 0, and can be 1
-  // only iff DST block has only one predecesor.
-  BranchProbability getBackEdgeProbability(BasicBlock *Src,
-                                           BasicBlock *Dst) const;
-
   // Print value between 0 (0% probability) and 1 (100% probability),
   // however the value is never equal to 0, and can be 1 only iff SRC block
   // has only one successor.

Modified: llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp?rev=133776&r1=133775&r2=133776&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp Thu Jun 23 18:52:11 2011
@@ -279,21 +279,6 @@
   return Sum;
 }
 
-uint32_t BranchProbabilityInfo::getBackSumForBlock(BasicBlock *BB) const {
-  uint32_t Sum = 0;
-
-  for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
-    BasicBlock *Pred = *I;
-    uint32_t Weight = getEdgeWeight(Pred, BB);
-    uint32_t PrevSum = Sum;
-
-    Sum += Weight;
-    assert(Sum > PrevSum); (void) PrevSum;
-  }
-
-  return Sum;
-}
-
 bool BranchProbabilityInfo::isEdgeHot(BasicBlock *Src, BasicBlock *Dst) const {
   // Hot probability is at least 4/5 = 80%
   uint32_t Weight = getEdgeWeight(Src, Dst);
@@ -360,15 +345,6 @@
   return BranchProbability(N, D);
 }
 
-BranchProbability BranchProbabilityInfo::
-getBackEdgeProbability(BasicBlock *Src, BasicBlock *Dst) const {
-
-  uint32_t N = getEdgeWeight(Src, Dst);
-  uint32_t D = getBackSumForBlock(Dst);
-
-  return BranchProbability(N, D);
-}
-
 raw_ostream &
 BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
                                             BasicBlock *Dst) const {





More information about the llvm-commits mailing list