BranchProbabilityInfo: floating point equality is unlikely.
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 21 Oct 2011 20:12:47 +0000 (20:12 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 21 Oct 2011 20:12:47 +0000 (20:12 +0000)
This is from the same paper from Ball and Larus as the rest of the currently implemented heuristics.

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

lib/Analysis/BranchProbabilityInfo.cpp

index 52090c9fc15dcc8bb85c915d7426786306a6009d..6e8d7e0115d402bb3eefe5bd4216f8dacb84d466 100644 (file)
@@ -76,6 +76,9 @@ class BranchProbabilityAnalysis {
   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 @@ public:
   // 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 @@ bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) {
   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 @@ bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
     if (calcPointerHeuristics(BB))
       continue;
 
-    calcZeroHeuristics(BB);
+    if (calcZeroHeuristics(BB))
+      continue;
+
+    calcFloatingPointHeuristics(BB);
   }
 
   return false;