Skip to content

Commit 4fbde1e

Browse files
committed
Fix MemorySSAUpdater::insertDef for dead code
Fix for llvm/llvm-project#51257. Differential Revision: https://reviews.llvm.org/D122601
1 parent 71ec09b commit 4fbde1e

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

llvm/lib/Analysis/MemorySSAUpdater.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ static void setMemoryPhiValueForBlock(MemoryPhi *MP, const BasicBlock *BB,
305305
// point to the correct new defs, to ensure we only have one variable, and no
306306
// disconnected stores.
307307
void MemorySSAUpdater::insertDef(MemoryDef *MD, bool RenameUses) {
308+
// Don't bother updating dead code.
309+
if (!MSSA->DT->isReachableFromEntry(MD->getBlock())) {
310+
MD->setDefiningAccess(MSSA->getLiveOnEntryDef());
311+
return;
312+
}
313+
308314
VisitedBlocks.clear();
309315
InsertedPHIs.clear();
310316

@@ -422,10 +428,10 @@ void MemorySSAUpdater::insertDef(MemoryDef *MD, bool RenameUses) {
422428
if (NewPhiSize)
423429
tryRemoveTrivialPhis(ArrayRef<WeakVH>(&InsertedPHIs[NewPhiIndex], NewPhiSize));
424430

425-
// Now that all fixups are done, rename all uses if we are asked. Skip
426-
// renaming for defs in unreachable blocks.
431+
// Now that all fixups are done, rename all uses if we are asked. The defs are
432+
// guaranteed to be in reachable code due to the check at the method entry.
427433
BasicBlock *StartBlock = MD->getBlock();
428-
if (RenameUses && MSSA->getDomTree().getNode(StartBlock)) {
434+
if (RenameUses) {
429435
SmallPtrSet<BasicBlock *, 16> Visited;
430436
// We are guaranteed there is a def in the block, because we just got it
431437
// handed to us in this function.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: opt -passes='require<memoryssa>,gvn' -verify-memoryssa -S %s | FileCheck %s
2+
3+
; This is a regression test for a bug in MemorySSA updater.
4+
; Make sure that we don't crash and end up with a valid MemorySSA.
5+
6+
; CHECK: @test()
7+
define void @test() personality i32* ()* null {
8+
invoke void @bar()
9+
to label %bar.normal unwind label %exceptional
10+
11+
bar.normal:
12+
ret void
13+
14+
dead.block:
15+
br label %baz.invoke
16+
17+
baz.invoke:
18+
invoke void @baz()
19+
to label %baz.normal unwind label %exceptional
20+
21+
baz.normal:
22+
ret void
23+
24+
exceptional:
25+
%tmp9 = landingpad { i8*, i32 }
26+
cleanup
27+
call void @foo()
28+
ret void
29+
}
30+
31+
declare void @foo()
32+
declare void @bar()
33+
declare void @baz()

0 commit comments

Comments
 (0)