[llvm-commits] [llvm] r142677 - /llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp

Benjamin Kramer benny.kra at googlemail.com
Fri Oct 21 13:12:47 PDT 2011


Author: d0k
Date: Fri Oct 21 15:12:47 2011
New Revision: 142677

URL: http://llvm.org/viewvc/llvm-project?rev=142677&view=rev
Log:
BranchProbabilityInfo: floating point equality is unlikely.

This is from the same paper from Ball and Larus as the rest of the currently implemented heuristics.

Modified:
    llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp

Modified: llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp?rev=142677&r1=142676&r2=142677&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp Fri Oct 21 15:12:47 2011
@@ -76,6 +76,9 @@
   static const uint32_t ZH_TAKEN_WEIGHT = 20;
   static const uint32_t ZH_NONTAKEN_WEIGHT = 12;
 
+  static const uint32_t FPH_TAKEN_WEIGHT = 20;
+  static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
+
   // Standard weight value. Used when none of the heuristics set weight for
   // the edge.
   static const uint32_t NORMAL_WEIGHT = 16;
@@ -131,9 +134,12 @@
   // Loop Branch Heuristics
   bool calcLoopBranchHeuristics(BasicBlock *BB);
 
-  // Zero Heurestics
+  // Zero Heuristics
   bool calcZeroHeuristics(BasicBlock *BB);
 
+  // Floating Point Heuristics
+  bool calcFloatingPointHeuristics(BasicBlock *BB);
+
   bool runOnFunction(Function &F);
 };
 } // end anonymous namespace
@@ -378,6 +384,29 @@
   return true;
 }
 
+bool BranchProbabilityAnalysis::calcFloatingPointHeuristics(BasicBlock *BB) {
+  BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
+  if (!BI || !BI->isConditional())
+    return false;
+
+  Value *Cond = BI->getCondition();
+  FCmpInst *FCmp = dyn_cast<FCmpInst>(Cond);
+  if (!FCmp || !FCmp->isEquality())
+    return false;
+
+  BasicBlock *Taken = BI->getSuccessor(0);
+  BasicBlock *NonTaken = BI->getSuccessor(1);
+
+  // f1 == f2 -> Unlikely
+  // f1 != f2 -> Likely
+  if (FCmp->isTrueWhenEqual())
+    std::swap(Taken, NonTaken);
+
+  BP->setEdgeWeight(BB, Taken, FPH_TAKEN_WEIGHT);
+  BP->setEdgeWeight(BB, NonTaken, FPH_NONTAKEN_WEIGHT);
+
+  return true;
+}
 
 bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
 
@@ -396,7 +425,10 @@
     if (calcPointerHeuristics(BB))
       continue;
 
-    calcZeroHeuristics(BB);
+    if (calcZeroHeuristics(BB))
+      continue;
+
+    calcFloatingPointHeuristics(BB);
   }
 
   return false;





More information about the llvm-commits mailing list