From 30d4f6571e5cd46c3179aab4c1759742caf9d531 Mon Sep 17 00:00:00 2001 From: Xin Tong Date: Fri, 24 Feb 2017 01:43:36 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@296069 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopUnswitch.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/Scalar/LoopUnswitch.cpp b/lib/Transforms/Scalar/LoopUnswitch.cpp index 2c31074f0e9..b1083a52ba1 100644 --- a/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -1231,7 +1231,13 @@ void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, // 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); continue; } } -- 2.40.0