[llvm-commits] [llvm] r60332 - /llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Chris Lattner sabre at nondot.org
Sun Nov 30 22:27:42 PST 2008


Author: lattner
Date: Mon Dec  1 00:27:41 2008
New Revision: 60332

URL: http://llvm.org/viewvc/llvm-project?rev=60332&view=rev
Log:
Eliminate use of setvector for the DeadInsts set, just use a smallvector.
This is a lot cheaper and conceptually simpler.

Modified:
    llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp?rev=60332&r1=60331&r2=60332&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp Mon Dec  1 00:27:41 2008
@@ -31,7 +31,6 @@
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Target/TargetData.h"
-#include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Support/Debug.h"
@@ -138,7 +137,7 @@
 
     /// DeadInsts - Keep track of instructions we may have made dead, so that
     /// we can remove them after we are done working.
-    SetVector<Instruction*> DeadInsts;
+    SmallVector<Instruction*, 16> DeadInsts;
 
     /// TLI - Keep a pointer of a TargetLowering to consult for determining
     /// transformation profitability.
@@ -230,7 +229,7 @@
   if (New) return New;
   
   New = SCEVExpander::InsertCastOfTo(opcode, V, UIntPtrTy);
-  DeadInsts.insert(cast<Instruction>(New));
+  DeadInsts.push_back(cast<Instruction>(New));
   return New;
 }
 
@@ -239,20 +238,35 @@
 /// specified set are trivially dead, delete them and see if this makes any of
 /// their operands subsequently dead.
 void LoopStrengthReduce::DeleteTriviallyDeadInstructions() {
+  if (DeadInsts.empty()) return;
+  
+  // Sort the deadinsts list so that we can trivially eliminate duplicates as we
+  // go.  The code below never adds a non-dead instruction to the worklist, but
+  // callers may not be so careful.
+  std::sort(DeadInsts.begin(), DeadInsts.end());
+
+  // Drop duplicate instructions and those with uses.
+  for (unsigned i = 0, e = DeadInsts.size()-1; i < e; ++i) {
+    Instruction *I = DeadInsts[i];
+    if (!I->use_empty()) DeadInsts[i] = 0;
+    while (DeadInsts[i+1] == I && i != e)
+      DeadInsts[++i] = 0;
+  }
+  
   while (!DeadInsts.empty()) {
     Instruction *I = DeadInsts.back();
     DeadInsts.pop_back();
-
-    if (!isInstructionTriviallyDead(I))
+    
+    if (I == 0 || !isInstructionTriviallyDead(I))
       continue;
 
     SE->deleteValueFromRecords(I);
 
-    for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
-      if (Instruction *U = dyn_cast<Instruction>(*i)) {
-        *i = 0;
+    for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) {
+      if (Instruction *U = dyn_cast<Instruction>(*OI)) {
+        *OI = 0;
         if (U->use_empty())
-          DeadInsts.insert(U);
+          DeadInsts.push_back(U);
       }
     }
     
@@ -383,7 +397,7 @@
 /// should use the post-inc value).
 static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
                                        Loop *L, DominatorTree *DT, Pass *P,
-                                       SetVector<Instruction*> &DeadInsts){
+                                      SmallVectorImpl<Instruction*> &DeadInsts){
   // If the user is in the loop, use the preinc value.
   if (L->contains(User->getParent())) return false;
   
@@ -425,7 +439,7 @@
     }
 
   // PHI node might have become a constant value after SplitCriticalEdge.
-  DeadInsts.insert(User);
+  DeadInsts.push_back(User);
   
   return true;
 }
@@ -551,7 +565,7 @@
     void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
                                         Instruction *InsertPt,
                                        SCEVExpander &Rewriter, Loop *L, Pass *P,
-                                       SetVector<Instruction*> &DeadInsts);
+                                      SmallVectorImpl<Instruction*> &DeadInsts);
     
     Value *InsertCodeForBaseAtPosition(const SCEVHandle &NewBase, 
                                        SCEVExpander &Rewriter,
@@ -616,7 +630,7 @@
 void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
                                                Instruction *NewBasePt,
                                       SCEVExpander &Rewriter, Loop *L, Pass *P,
-                                      SetVector<Instruction*> &DeadInsts) {
+                                      SmallVectorImpl<Instruction*> &DeadInsts){
   if (!isa<PHINode>(Inst)) {
     // By default, insert code at the user instruction.
     BasicBlock::iterator InsertPt = Inst;
@@ -713,7 +727,7 @@
   }
 
   // PHI node might have become a constant value after SplitCriticalEdge.
-  DeadInsts.insert(Inst);
+  DeadInsts.push_back(Inst);
 
   DOUT << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst;
 }
@@ -1442,7 +1456,7 @@
 
       // Mark old value we replaced as possibly dead, so that it is eliminated
       // if we just replaced the last use of that value.
-      DeadInsts.insert(cast<Instruction>(User.OperandValToReplace));
+      DeadInsts.push_back(cast<Instruction>(User.OperandValToReplace));
 
       UsersToProcess.pop_back();
       ++NumReduced;
@@ -1672,7 +1686,7 @@
                         OldCond);
 
     // Remove the old compare instruction. The old indvar is probably dead too.
-    DeadInsts.insert(cast<Instruction>(CondUse->OperandValToReplace));
+    DeadInsts.push_back(cast<Instruction>(CondUse->OperandValToReplace));
     SE->deleteValueFromRecords(OldCond);
     OldCond->replaceAllUsesWith(Cond);
     OldCond->eraseFromParent();
@@ -2080,7 +2094,7 @@
           // Break the cycle and mark the PHI for deletion.
           SE->deleteValueFromRecords(PN);
           PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
-          DeadInsts.insert(PN);
+          DeadInsts.push_back(PN);
           Changed = true;
           break;
         }





More information about the llvm-commits mailing list