Tidy up a loop to be more idiomatic for LLVM's codebase, and remove some
[oota-llvm.git] / lib / Analysis / BranchProbabilityInfo.cpp
index 4f15858aa34ff1ef810b2356e1e4bd8cf241d572..46fe3310c7e6e23d4635237506102eb8b7818fed 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Constants.h"
+#include "llvm/Function.h"
 #include "llvm/Instructions.h"
 #include "llvm/LLVMContext.h"
 #include "llvm/Metadata.h"
 #include "llvm/Analysis/BranchProbabilityInfo.h"
 #include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Support/CFG.h"
 #include "llvm/Support/Debug.h"
 
 using namespace llvm;
@@ -422,42 +424,48 @@ bool BranchProbabilityAnalysis::calcFloatingPointHeuristics(BasicBlock *BB) {
 }
 
 bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
-
-  for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
-    BasicBlock *BB = I++;
-
-    if (calcMetadataWeights(BB))
+  for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
+    if (calcMetadataWeights(I))
       continue;
-
-    if (calcLoopBranchHeuristics(BB))
+    if (calcLoopBranchHeuristics(I))
       continue;
-
-    if (calcReturnHeuristics(BB))
+    if (calcReturnHeuristics(I))
       continue;
-
-    if (calcPointerHeuristics(BB))
+    if (calcPointerHeuristics(I))
       continue;
-
-    if (calcZeroHeuristics(BB))
+    if (calcZeroHeuristics(I))
       continue;
-
-    calcFloatingPointHeuristics(BB);
+    calcFloatingPointHeuristics(I);
   }
-
   return false;
 }
 
 void BranchProbabilityInfo::getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.addRequired<LoopInfo>();
-    AU.setPreservesAll();
+  AU.addRequired<LoopInfo>();
+  AU.setPreservesAll();
 }
 
 bool BranchProbabilityInfo::runOnFunction(Function &F) {
+  LastF = &F; // Store the last function we ran on for printing.
   LoopInfo &LI = getAnalysis<LoopInfo>();
   BranchProbabilityAnalysis BPA(this, &LI);
   return BPA.runOnFunction(F);
 }
 
+void BranchProbabilityInfo::print(raw_ostream &OS, const Module *) const {
+  OS << "---- Branch Probabilities ----\n";
+  // We print the probabilities from the last function the analysis ran over,
+  // or the function it is currently running over.
+  assert(LastF && "Cannot print prior to running over a function");
+  for (Function::const_iterator BI = LastF->begin(), BE = LastF->end();
+       BI != BE; ++BI) {
+    for (succ_const_iterator SI = succ_begin(BI), SE = succ_end(BI);
+         SI != SE; ++SI) {
+      printEdgeProbability(OS << "  ", BI, *SI);
+    }
+  }
+}
+
 uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
   uint32_t Sum = 0;
 
@@ -537,8 +545,9 @@ getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst) const {
 }
 
 raw_ostream &
-BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
-                                            BasicBlock *Dst) const {
+BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
+                                            const BasicBlock *Src,
+                                            const BasicBlock *Dst) const {
 
   const BranchProbability Prob = getEdgeProbability(Src, Dst);
   OS << "edge " << Src->getNameStr() << " -> " << Dst->getNameStr()