From 51b75b8c346dd0e062b9e81c22b75e36c4b15c1a Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Tue, 10 Mar 2015 05:13:47 +0000 Subject: [PATCH] Fix an infinite loop in InstCombine when an instruction with no users and side effects can be constant folded. ReplaceInstUsesWith needs to return nullptr when the input has no users, because in that case it does not mutate the program. Otherwise, we can get stuck in an infinite loop of repeatedly attempting to constant fold and instruction with no users. llvm-svn: 231755 --- .../Transforms/InstCombine/InstCombineInternal.h | 4 ++++ .../Transforms/InstCombine/constant-fold-hang.ll | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 llvm/test/Transforms/InstCombine/constant-fold-hang.ll diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h index 8c784ab1e682..fb2321df2e04 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -372,6 +372,10 @@ public: /// I to the worklist, replace all uses of I with the new value, then return /// I, so that the inst combiner will know that I was modified. Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { + // If there are no uses to replace, then we return nullptr to indicate that + // no changes were made to the program. + if (I.use_empty()) return nullptr; + Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist. // If we are replacing the instruction with itself, this must be in a diff --git a/llvm/test/Transforms/InstCombine/constant-fold-hang.ll b/llvm/test/Transforms/InstCombine/constant-fold-hang.ll new file mode 100644 index 000000000000..2ca6b86ccc2f --- /dev/null +++ b/llvm/test/Transforms/InstCombine/constant-fold-hang.ll @@ -0,0 +1,14 @@ +; RUN: opt -instcombine < %s + +; Function Attrs: nounwind readnone ssp +define void @mulByZero(<4 x i16> %x) #0 { +entry: + %a = tail call <4 x i32> @llvm.arm.neon.vmulls.v4i32(<4 x i16> %x, <4 x i16> zeroinitializer) #2 + ret void +} + +; Function Attrs: nounwind readnone +declare <4 x i32> @llvm.arm.neon.vmulls.v4i32(<4 x i16>, <4 x i16>) #1 + +attributes #0 = { nounwind readnone ssp } +attributes #1 = { nounwind readnone }