Skip to content

Commit 88690a9

Browse files
author
Yevgeny Rouban
committed
[CodeGenPrepare] Fix zapping dead operands of assume
This patch fixes a problem of the commit 52cc97a. A test case is created to demonstrate the crash caused by the instruction iterator invalidated by the recursive removal of dead operands of assume. The solution restarts from the blocks's first instruction in case CurInstIterator is invalidated by RecursivelyDeleteTriviallyDeadInstructions(). Reviewed By: bkramer Differential Revision: https://reviews.llvm.org/D87434
1 parent 6e42cad commit 88690a9

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,9 +2047,11 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, bool &ModifiedDT) {
20472047
Value *Operand = II->getOperand(0);
20482048
II->eraseFromParent();
20492049
// Prune the operand, it's most likely dead.
2050-
RecursivelyDeleteTriviallyDeadInstructions(
2051-
Operand, TLInfo, nullptr,
2052-
[&](Value *V) { removeAllAssertingVHReferences(V); });
2050+
resetIteratorIfInvalidatedWhileCalling(BB, [&]() {
2051+
RecursivelyDeleteTriviallyDeadInstructions(
2052+
Operand, TLInfo, nullptr,
2053+
[&](Value *V) { removeAllAssertingVHReferences(V); });
2054+
});
20532055
return true;
20542056
}
20552057

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: opt -codegenprepare -S -mtriple=x86_64-linux < %s | FileCheck %s
2+
3+
declare void @llvm.assume(i1 noundef) nounwind willreturn
4+
5+
; Recursively deleting dead operands of assume() may result in its next
6+
; instruction deleted and the iterator pointing to the next instruction
7+
; invalidated. This prevents the following simple loop in
8+
; CodeGenPrepare::optimizeBlock() unless CurInstIterator is fixed:
9+
;
10+
; CurInstIterator = BB.begin();
11+
; while (CurInstIterator != BB.end())
12+
; optimizeInst(&*CurInstIterator++, ModifiedDT);
13+
;
14+
define i32 @test_assume_in_loop(i1 %cond1, i1 %cond2) {
15+
; CHECK-LABEL: @test_assume_in_loop(
16+
; CHECK-NEXT: entry:
17+
entry:
18+
br label %loop
19+
20+
; CHECK: loop:
21+
; CHECK-NEXT: br label %loop
22+
loop:
23+
%cond3 = phi i1 [%cond1, %entry], [%cond4, %loop]
24+
call void @llvm.assume(i1 %cond3)
25+
%cond4 = icmp ult i1 %cond1, %cond2
26+
br label %loop
27+
}

0 commit comments

Comments
 (0)