[llvm-commits] CVS: llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

Chris Lattner lattner at cs.uiuc.edu
Sat Oct 4 20:54:01 PDT 2003


Changes in directory llvm/lib/Transforms/Utils:

PromoteMemoryToRegister.cpp updated: 1.42 -> 1.43

---
Log message:

* Minor cleanups
* Eliminate the KillList instance variable, instead, just delete loads and
  stores as they are "renamed", and delete allocas when they are done
* Make the 'visited' set an instance variable to avoid passing it on the stack.


---
Diffs of the changes:

Index: llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
diff -u llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp:1.42 llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp:1.43
--- llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp:1.42	Mon Aug 18 09:34:00 2003
+++ llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp	Sat Oct  4 20:52:53 2003
@@ -51,7 +51,7 @@
 
 namespace {
   struct PromoteMem2Reg {
-    const std::vector<AllocaInst*>   &Allocas;      // the alloca instructions..
+    const std::vector<AllocaInst*> &Allocas;        // the alloca instructions..
     std::vector<unsigned> VersionNumbers;           // Current version counters
     DominanceFrontier &DF;
     const TargetData &TD;
@@ -60,33 +60,29 @@
     
     std::vector<std::vector<BasicBlock*> > PhiNodes;// Idx corresponds 2 Allocas
     
-    // List of instructions to remove at end of pass
-    std::vector<Instruction *>        KillList;
-    
-    std::map<BasicBlock*,
-             std::vector<PHINode*> >  NewPhiNodes; // the PhiNodes we're adding
+    // NewPhiNodes - The PhiNodes we're adding.
+    std::map<BasicBlock*, std::vector<PHINode*> > NewPhiNodes;
+
+    // Visited - The set of basic blocks the renamer has already visited.
+    std::set<BasicBlock*> Visited;
 
   public:
     PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominanceFrontier &df,
-                   const TargetData &td)
-      : Allocas(A), DF(df), TD(td) {}
+                   const TargetData &td) : Allocas(A), DF(df), TD(td) {}
 
     void run();
 
   private:
     void RenamePass(BasicBlock *BB, BasicBlock *Pred,
-                    std::vector<Value*> &IncVals,
-                    std::set<BasicBlock*> &Visited);
+                    std::vector<Value*> &IncVals);
     bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx);
   };
 }  // end of anonymous namespace
 
 
 void PromoteMem2Reg::run() {
-  // If there is nothing to do, bail out...
-  if (Allocas.empty()) return;
-
   Function &F = *DF.getRoot()->getParent();
+
   VersionNumbers.resize(Allocas.size());
 
   for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
@@ -97,11 +93,6 @@
     AllocaLookup[Allocas[i]] = i;
   }
 
-
-  // Add each alloca to the KillList.  Note: KillList is destroyed MOST recently
-  // added to least recently.
-  KillList.assign(Allocas.begin(), Allocas.end());
-
   // Calculate the set of write-locations for each alloca.  This is analogous to
   // counting the number of 'redefinitions' of each variable.
   std::vector<std::vector<BasicBlock*> > WriteSets;// Idx corresponds to Allocas
@@ -153,27 +144,20 @@
   // Walks all basic blocks in the function performing the SSA rename algorithm
   // and inserting the phi nodes we marked as necessary
   //
-  std::set<BasicBlock*> Visited;      // The basic blocks we've already visited
-  RenamePass(F.begin(), 0, Values, Visited);
+  RenamePass(F.begin(), 0, Values);
+  Visited.clear();
 
-  // Remove all instructions marked by being placed in the KillList...
-  //
-  while (!KillList.empty()) {
-    Instruction *I = KillList.back();
-    KillList.pop_back();
+  // Remove the allocas themselves from the function...
+  for (unsigned i = 0, e = Allocas.size(); i != e; ++i) {
+    Instruction *A = Allocas[i];
 
-    // If there are any uses of these instructions left, they must be in
+    // If there are any uses of the alloca instructions left, they must be in
     // sections of dead code that were not processed on the dominance frontier.
     // Just delete the users now.
     //
-    while (!I->use_empty()) {
-      Instruction *U = cast<Instruction>(I->use_back());
-      if (!U->use_empty())  // If uses remain in dead code segment...
-        U->replaceAllUsesWith(Constant::getNullValue(U->getType()));
-      U->getParent()->getInstList().erase(U);
-    }
-
-    I->getParent()->getInstList().erase(I);
+    if (!A->use_empty())
+      A->replaceAllUsesWith(Constant::getNullValue(A->getType()));
+    A->getParent()->getInstList().erase(A);
   }
 }
 
@@ -215,8 +199,7 @@
 }
 
 void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred,
-                             std::vector<Value*> &IncomingVals,
-                             std::set<BasicBlock*> &Visited) {
+                                std::vector<Value*> &IncomingVals) {
   // If this is a BB needing a phi node, lookup/create the phinode for each
   // variable we need phinodes for.
   std::vector<PHINode *> &BBPNs = NewPhiNodes[BB];
@@ -240,9 +223,9 @@
   // mark as visited
   Visited.insert(BB);
 
-  // keep track of the value of each variable we're watching.. how?
-  for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) {
-    Instruction *I = II; // get the instruction
+  BasicBlock::iterator II = BB->begin();
+  while (1) {
+    Instruction *I = II++; // get the instruction, increment iterator
 
     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
       if (AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand())) {
@@ -252,7 +235,7 @@
 
           // walk the use list of this load and replace all uses with r
           LI->replaceAllUsesWith(V);
-          KillList.push_back(LI); // Mark the load to be deleted
+          BB->getInstList().erase(LI);
         }
       }
     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
@@ -263,7 +246,7 @@
         if (ai != AllocaLookup.end()) {
           // what value were we writing?
           IncomingVals[ai->second] = SI->getOperand(0);
-          KillList.push_back(SI);  // Mark the store to be deleted
+          BB->getInstList().erase(SI);
         }
       }
       
@@ -271,8 +254,9 @@
       // Recurse across our successors
       for (unsigned i = 0; i != TI->getNumSuccessors(); i++) {
         std::vector<Value*> OutgoingVals(IncomingVals);
-        RenamePass(TI->getSuccessor(i), BB, OutgoingVals, Visited);
+        RenamePass(TI->getSuccessor(i), BB, OutgoingVals);
       }
+      break;
     }
   }
 }
@@ -284,5 +268,7 @@
 ///
 void PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
                      DominanceFrontier &DF, const TargetData &TD) {
+  // If there is nothing to do, bail out...
+  if (Allocas.empty()) return;
   PromoteMem2Reg(Allocas, DF, TD).run();
 }





More information about the llvm-commits mailing list