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

Dan Gohman gohman at apple.com
Tue May 20 17:54:13 PDT 2008


Author: djg
Date: Tue May 20 19:54:12 2008
New Revision: 51359

URL: http://llvm.org/viewvc/llvm-project?rev=51359&view=rev
Log:
When LSR is replacing an instruction, call
ScalarEvolution::deleteValueFromRecords on it before doing the
replaceAllUsesWith, because ScalarEvolution looks at the instruction's
users to find SCEV references to the instruction's SCEV object in its
internal maps.

Move all of LSR's loop-related state clearing after processing the loop
and before cleaning up dead PHI nodes. This eliminates all of LSR's SCEV
references just before the calls to ScalarEvolution::deleteValueFromRecords
so that when ScalarEvolution drops its own SCEV references, the reference
counts will reach zero and the SCEVs will be deleted immediately.

These changes fix some compiler aborts involving ScalarEvolution holding
onto and reusing SCEV objects for instructions that have been deleted.
No regression test unfortunately; because the symptoms were due to
dangling pointers, reduced testcases ended up being fairly arbitrary.

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=51359&r1=51358&r2=51359&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopStrengthReduce.cpp Tue May 20 19:54:12 2008
@@ -237,8 +237,8 @@
       if (Value *PNV = PN->hasConstantValue()) {
         if (Instruction *U = dyn_cast<Instruction>(PNV))
           Insts.insert(U);
-        PN->replaceAllUsesWith(PNV);
         SE->deleteValueFromRecords(PN);
+        PN->replaceAllUsesWith(PNV);
         PN->eraseFromParent();
         Changed = true;
         continue;
@@ -1663,8 +1663,8 @@
 
     // Remove the old compare instruction. The old indvar is probably dead too.
     DeadInsts.insert(cast<Instruction>(CondUse->OperandValToReplace));
-    OldCond->replaceAllUsesWith(Cond);
     SE->deleteValueFromRecords(OldCond);
+    OldCond->replaceAllUsesWith(Cond);
     OldCond->eraseFromParent();
 
     IVUsesByStride[*CondStride].Users.pop_back();
@@ -1782,7 +1782,7 @@
 #endif
 
   // IVsByStride keeps IVs for one particular loop.
-  IVsByStride.clear();
+  assert(IVsByStride.empty() && "Stale entries in IVsByStride?");
 
   // Sort the StrideOrder so we process larger strides first.
   std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());
@@ -1799,6 +1799,12 @@
     StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
   }
 
+  // We're done analyzing this loop; release all the state we built up for it.
+  CastedPointers.clear();
+  IVUsesByStride.clear();
+  IVsByStride.clear();
+  StrideOrder.clear();
+
   // Clean up after ourselves
   if (!DeadInsts.empty()) {
     DeleteTriviallyDeadInstructions(DeadInsts);
@@ -1826,8 +1832,8 @@
           if (BO->hasOneUse() && PN == *(BO->use_begin())) {
             DeadInsts.insert(BO);
             // Break the cycle, then delete the PHI.
-            PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
             SE->deleteValueFromRecords(PN);
+            PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
             PN->eraseFromParent();
           }
         }
@@ -1836,8 +1842,5 @@
     DeleteTriviallyDeadInstructions(DeadInsts);
   }
 
-  CastedPointers.clear();
-  IVUsesByStride.clear();
-  StrideOrder.clear();
   return false;
 }





More information about the llvm-commits mailing list