[llvm] r296069 - Fix an iterator invalidation bug when simplifying LIC user.

Xin Tong via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 23 17:43:36 PST 2017

Author: trentxintong
Date: Thu Feb 23 19:43:36 2017
New Revision: 296069

URL: http://llvm.org/viewvc/llvm-project?rev=296069&view=rev
Fix an iterator invalidation bug when simplifying LIC user.

LoopUnswitch/simplify-with-nonvalness.ll is the test case for this.
The LIC has 2 users and deleting the 1st user when it can be simplified
invalidated the iterator for the 2nd user.


Modified: llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp?rev=296069&r1=296068&r2=296069&view=diff
--- llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopUnswitch.cpp Thu Feb 23 19:43:36 2017
@@ -1231,7 +1231,13 @@ void LoopUnswitch::RewriteLoopBodyWithCo
         // This in-loop instruction has been simplified w.r.t. its context,
         // i.e. LIC != Val, make sure we propagate its replacement value to
         // all its users.
-        ReplaceUsesOfWith(UI, Replacement, Worklist, L, LPM);
+        //  
+        // We can not yet delete UI, the LIC user, yet, because that would invalidate
+        // the LIC->users() iterator !. However, we can make this instruction
+        // dead by replacing all its users and push it onto the worklist so that
+        // it can be properly deleted and its operands simplified. 
+        UI->replaceAllUsesWith(Replacement);
+        Worklist.push_back(UI);

More information about the llvm-commits mailing list