[llvm-commits] [llvm] r152706 - /llvm/trunk/lib/Analysis/InlineCost.cpp

Chandler Carruth chandlerc at gmail.com
Wed Mar 14 00:32:53 PDT 2012


Author: chandlerc
Date: Wed Mar 14 02:32:53 2012
New Revision: 152706

URL: http://llvm.org/viewvc/llvm-project?rev=152706&view=rev
Log:
Refactor the inline cost bonus calculation for constants to use
a worklist rather than a recursive call.

No functionality changed.

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

Modified: llvm/trunk/lib/Analysis/InlineCost.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InlineCost.cpp?rev=152706&r1=152705&r2=152706&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InlineCost.cpp (original)
+++ llvm/trunk/lib/Analysis/InlineCost.cpp Wed Mar 14 02:32:53 2012
@@ -165,18 +165,24 @@
 unsigned InlineCostAnalyzer::FunctionInfo::countCodeReductionForConstant(
     const CodeMetrics &Metrics, Value *V) {
   unsigned Reduction = 0;
-  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
-    User *U = *UI;
-    if (isa<BranchInst>(U) || isa<SwitchInst>(U)) {
-      // We will be able to eliminate all but one of the successors.
-      const TerminatorInst &TI = cast<TerminatorInst>(*U);
-      const unsigned NumSucc = TI.getNumSuccessors();
-      unsigned Instrs = 0;
-      for (unsigned I = 0; I != NumSucc; ++I)
-        Instrs += Metrics.NumBBInsts.lookup(TI.getSuccessor(I));
-      // We don't know which blocks will be eliminated, so use the average size.
-      Reduction += InlineConstants::InstrCost*Instrs*(NumSucc-1)/NumSucc;
-    } else {
+  SmallVector<Value *, 4> Worklist;
+  Worklist.push_back(V);
+  do {
+    Value *V = Worklist.pop_back_val();
+    for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
+      User *U = *UI;
+      if (isa<BranchInst>(U) || isa<SwitchInst>(U)) {
+        // We will be able to eliminate all but one of the successors.
+        const TerminatorInst &TI = cast<TerminatorInst>(*U);
+        const unsigned NumSucc = TI.getNumSuccessors();
+        unsigned Instrs = 0;
+        for (unsigned I = 0; I != NumSucc; ++I)
+          Instrs += Metrics.NumBBInsts.lookup(TI.getSuccessor(I));
+        // We don't know which blocks will be eliminated, so use the average size.
+        Reduction += InlineConstants::InstrCost*Instrs*(NumSucc-1)/NumSucc;
+        continue;
+      }
+
       // Figure out if this instruction will be removed due to simple constant
       // propagation.
       Instruction &Inst = cast<Instruction>(*U);
@@ -198,17 +204,17 @@
           AllOperandsConstant = false;
           break;
         }
+      if (!AllOperandsConstant)
+        continue;
 
-      if (AllOperandsConstant) {
-        // We will get to remove this instruction...
-        Reduction += InlineConstants::InstrCost;
-
-        // And any other instructions that use it which become constants
-        // themselves.
-        Reduction += countCodeReductionForConstant(Metrics, &Inst);
-      }
+      // We will get to remove this instruction...
+      Reduction += InlineConstants::InstrCost;
+
+      // And any other instructions that use it which become constants
+      // themselves.
+      Worklist.push_back(&Inst);
     }
-  }
+  } while (!Worklist.empty());
   return Reduction;
 }
 





More information about the llvm-commits mailing list