[llvm-branch-commits] [llvm-branch] r142547 - in /llvm/branches/release_30: ./ lib/Analysis/BranchProbabilityInfo.cpp test/Analysis/BlockFrequencyInfo/basic.ll

Bill Wendling isanbard at gmail.com
Wed Oct 19 15:12:52 PDT 2011


Author: void
Date: Wed Oct 19 17:12:52 2011
New Revision: 142547

URL: http://llvm.org/viewvc/llvm-project?rev=142547&view=rev
Log:
Merging r142492:
------------------------------------------------------------------------
r142492 | chandlerc | 2011-10-19 03:30:30 -0700 (Wed, 19 Oct 2011) | 7 lines

Teach the BranchProbabilityInfo analysis pass to read any metadata
encoding of probabilities. In the absense of metadata, it continues to
fall back on static heuristics.

This allows __builtin_expect, after lowering through llvm.expect
a branch instruction's metadata, to actually enter the branch
probability model. This is one component of resolving PR2577.
------------------------------------------------------------------------

Modified:
    llvm/branches/release_30/   (props changed)
    llvm/branches/release_30/lib/Analysis/BranchProbabilityInfo.cpp
    llvm/branches/release_30/test/Analysis/BlockFrequencyInfo/basic.ll

Propchange: llvm/branches/release_30/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Oct 19 17:12:52 2011
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:142039,142055,142058,142112,142123,142125,142165,142168,142243,142482,142486,142489,142491,142537
+/llvm/trunk:142039,142055,142058,142112,142123,142125,142165,142168,142243,142482,142486,142489,142491-142492,142537

Modified: llvm/branches/release_30/lib/Analysis/BranchProbabilityInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_30/lib/Analysis/BranchProbabilityInfo.cpp?rev=142547&r1=142546&r2=142547&view=diff
==============================================================================
--- llvm/branches/release_30/lib/Analysis/BranchProbabilityInfo.cpp (original)
+++ llvm/branches/release_30/lib/Analysis/BranchProbabilityInfo.cpp Wed Oct 19 17:12:52 2011
@@ -13,6 +13,8 @@
 
 #include "llvm/Constants.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/Debug.h"
@@ -120,6 +122,9 @@
     : Weights(W), BP(BP), LI(LI) {
   }
 
+  // Metadata Weights
+  bool calcMetadataWeights(BasicBlock *BB);
+
   // Return Heuristics
   bool calcReturnHeuristics(BasicBlock *BB);
 
@@ -136,6 +141,36 @@
 };
 } // end anonymous namespace
 
+// Propagate existing explicit probabilities from either profile data or
+// 'expect' intrinsic processing.
+// FIXME: This doesn't correctly extract probabilities for switches.
+bool BranchProbabilityAnalysis::calcMetadataWeights(BasicBlock *BB) {
+  BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
+  if (!BI || !BI->isConditional())
+    return false;
+
+  MDNode *WeightsNode = BI->getMetadata(LLVMContext::MD_prof);
+  if (!WeightsNode || WeightsNode->getNumOperands() < 3)
+    return false;
+
+  // Pull the weights out of the metadata. Note that the zero operand is the
+  // name.
+  ConstantInt *Weights[] = {
+    dyn_cast<ConstantInt>(WeightsNode->getOperand(1)),
+    dyn_cast<ConstantInt>(WeightsNode->getOperand(2))
+  };
+  if (!Weights[0] || !Weights[1])
+    return false;
+
+  uint32_t WeightLimit = getMaxWeightFor(BB);
+  BP->setEdgeWeight(BB, BI->getSuccessor(0),
+                    Weights[0]->getLimitedValue(WeightLimit));
+  BP->setEdgeWeight(BB, BI->getSuccessor(1),
+                    Weights[1]->getLimitedValue(WeightLimit));
+
+  return true;
+}
+
 // Calculate Edge Weights using "Return Heuristics". Predict a successor which
 // leads directly to Return Instruction will not be taken.
 bool BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
@@ -344,6 +379,9 @@
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
     BasicBlock *BB = I++;
 
+    if (calcMetadataWeights(BB))
+      continue;
+
     if (calcLoopBranchHeuristics(BB))
       continue;
 

Modified: llvm/branches/release_30/test/Analysis/BlockFrequencyInfo/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_30/test/Analysis/BlockFrequencyInfo/basic.ll?rev=142547&r1=142546&r2=142547&view=diff
==============================================================================
--- llvm/branches/release_30/test/Analysis/BlockFrequencyInfo/basic.ll (original)
+++ llvm/branches/release_30/test/Analysis/BlockFrequencyInfo/basic.ll Wed Oct 19 17:12:52 2011
@@ -22,3 +22,28 @@
 exit:
   ret i32 %sum
 }
+
+define i32 @test2(i32 %i, i32 %a, i32 %b) {
+; CHECK: Printing analysis {{.*}} for function 'test2'
+; CHECK: entry = 1024
+entry:
+  %cond = icmp ult i32 %i, 42
+  br i1 %cond, label %then, label %else, !prof !0
+
+; The 'then' branch is predicted more likely via branch weight metadata.
+; CHECK: then = 963
+then:
+  br label %exit
+
+; CHECK: else = 60
+else:
+  br label %exit
+
+; FIXME: It may be a bug that we don't sum back to 1024.
+; CHECK: exit = 1023
+exit:
+  %result = phi i32 [ %a, %then ], [ %b, %else ]
+  ret i32 %result
+}
+
+!0 = metadata !{metadata !"branch_weights", i32 64, i32 4}





More information about the llvm-branch-commits mailing list