Now that we have comparison on probabilities, add some static functions
authorChandler Carruth <chandlerc@gmail.com>
Sun, 23 Oct 2011 20:10:34 +0000 (20:10 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sun, 23 Oct 2011 20:10:34 +0000 (20:10 +0000)
to get important constant branch probabilities and use them for finding
the best branch out of a set of possibilities.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142762 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/BranchProbability.h
lib/CodeGen/MachineBlockPlacement.cpp

index a1e5ceb6255b8e976c52a100e845cf654cbe442d..6b83159de6b8f05a6426efa33763b7c4eabc518b 100644 (file)
@@ -39,6 +39,9 @@ public:
     assert(n <= d && "Probability cannot be bigger than 1!");
   }
 
+  static BranchProbability getZero() { return BranchProbability(0, 1); }
+  static BranchProbability getOne() { return BranchProbability(1, 1); }
+
   uint32_t getNumerator() const { return N; }
   uint32_t getDenominator() const { return D; }
 
index 043a884f6d4e545e69a7b2771cc4b1f61b308028..32eb70e21ff2738a0be50b118ac38dd64f317d1c 100644 (file)
@@ -287,10 +287,8 @@ void MachineBlockPlacement::mergeSuccessor(MachineBasicBlock *BB,
     return;
 
   // Walk through the successors looking for the highest probability edge.
-  // FIXME: This is an annoying way to do the comparison, but it's correct.
-  // Support should be added to BranchProbability to properly compare two.
   MachineBasicBlock *Successor = 0;
-  BlockFrequency BestFreq;
+  BranchProbability BestProb = BranchProbability::getZero();
   DEBUG(dbgs() << "Attempting merge from: " << getBlockName(BB) << "\n");
   for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
                                         SE = BB->succ_end();
@@ -298,13 +296,12 @@ void MachineBlockPlacement::mergeSuccessor(MachineBasicBlock *BB,
     if (BB == *SI || (Filter && !Filter->count(*SI)))
       continue;
 
-    BlockFrequency SuccFreq(BlockFrequency::getEntryFrequency());
-    SuccFreq *= MBPI->getEdgeProbability(BB, *SI);
-    DEBUG(dbgs() << "    " << getBlockName(*SI) << " -> " << SuccFreq << "\n");
-    if (!Successor || SuccFreq > BestFreq || (!(SuccFreq < BestFreq) &&
+    BranchProbability SuccProb = MBPI->getEdgeProbability(BB, *SI);
+    DEBUG(dbgs() << "    " << getBlockName(*SI) << " -> " << SuccProb << "\n");
+    if (!Successor || SuccProb > BestProb || (!(SuccProb < BestProb) &&
                                               BB->isLayoutSuccessor(*SI))) {
       Successor = *SI;
-      BestFreq = SuccFreq;
+      BestProb = SuccProb;
     }
   }
   if (!Successor)