[llvm-commits] [poolalloc] r108177 - /poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp

John Criswell criswell at uiuc.edu
Mon Jul 12 12:11:19 PDT 2010


Author: criswell
Date: Mon Jul 12 14:11:19 2010
New Revision: 108177

URL: http://llvm.org/viewvc/llvm-project?rev=108177&view=rev
Log:
Refactored code to avoid iterator invalidation error.
This fixes PR#7629.

Modified:
    poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp

Modified: poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp?rev=108177&r1=108176&r2=108177&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp Mon Jul 12 14:11:19 2010
@@ -213,6 +213,7 @@
     //     have been transformed already), and
     //  o) the called function is the function that we're replacing
     //
+    std::vector<User *> toReplace;
     for (Function::use_iterator User = F->use_begin();
                                 User != F->use_end();
                                 ++User) {
@@ -228,17 +229,30 @@
             continue;
       }
 
+      //
+      // We want to replace this use.  Add it to the worklist.
+      //
+      toReplace.push_back (*User);
+    }
+
+    //
+    // Now do replacement on all items within the worklist.
+    //
+    while (toReplace.size()) {
+      llvm::User * user = toReplace.back();
+      toReplace.pop_back();
+
       Constant* CEnew = ConstantExpr::getPointerCast(I->second, F->getType());
 
       // Must handle Constants specially, we cannot call replaceUsesOfWith on a
       // constant because they are uniqued.
-      if (Constant *C = dyn_cast<Constant>(User)) {
+      if (Constant *C = dyn_cast<Constant>(user)) {
         if (!isa<GlobalValue>(C)) {
-          C->replaceUsesOfWithOnConstant(F, CEnew, User->op_begin());
+          C->replaceUsesOfWithOnConstant(F, CEnew, user->op_begin());
           continue;
         }
       }
-      User->replaceUsesOfWith (F, CEnew);
+      user->replaceUsesOfWith (F, CEnew);
     }
   }
 





More information about the llvm-commits mailing list