[llvm] [SimpleLoopUnswitch] Preserve one PHI when removing a predecessor of a BB (PR #116813)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 19 06:30:37 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: DianQK (DianQK)
<details>
<summary>Changes</summary>
Fixes #<!-- -->116809.
---
Full diff: https://github.com/llvm/llvm-project/pull/116813.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp (+2-2)
- (added) llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-pred-removed.ll (+24)
``````````diff
diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index aa3cbc5e4bddc2..d807c66bd96195 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -1678,7 +1678,7 @@ deleteDeadClonedBlocks(Loop &L, ArrayRef<BasicBlock *> ExitBlocks,
if (BasicBlock *ClonedBB = cast_or_null<BasicBlock>(VMap->lookup(BB)))
if (!DT.isReachableFromEntry(ClonedBB)) {
for (BasicBlock *SuccBB : successors(ClonedBB))
- SuccBB->removePredecessor(ClonedBB);
+ SuccBB->removePredecessor(ClonedBB, /*KeepOneInputPHIs*/ true);
DeadBlocks.push_back(ClonedBB);
}
@@ -1716,7 +1716,7 @@ static void deleteDeadBlocksFromLoop(Loop &L,
auto *BB = DeathCandidates.pop_back_val();
if (!DeadBlockSet.count(BB) && !DT.isReachableFromEntry(BB)) {
for (BasicBlock *SuccBB : successors(BB)) {
- SuccBB->removePredecessor(BB);
+ SuccBB->removePredecessor(BB, /*KeepOneInputPHIs*/ true);
DeathCandidates.push_back(SuccBB);
}
DeadBlockSet.insert(BB);
diff --git a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-pred-removed.ll b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-pred-removed.ll
new file mode 100644
index 00000000000000..3545492f1e5e23
--- /dev/null
+++ b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-pred-removed.ll
@@ -0,0 +1,24 @@
+; RUN: opt -passes='loop-mssa(simple-loop-unswitch<nontrivial>),print<memoryssa>' -verify-memoryssa -disable-output -S < %s 2>&1 | FileCheck %s
+; RUN: opt -passes='loop-mssa(simple-loop-unswitch<nontrivial>,licm)' -verify-memoryssa -disable-output -S
+
+; Check that SimpleLoopUnswitch preserves the MemoryDef of `call i32 @bar()` by preserving the PHI node.
+; Also, check that executing LICM after SimpleLoopUnswitch does not result in a crash.
+
+; CHECK: %unswitched.select = phi ptr [ @bar, %bb2 ]
+; CHECK-NEXT: MemoryDef
+; CHECK-NEXT: call i32 %unswitched.select()
+
+define i32 @foo(i1 %arg, ptr %arg1) {
+bb:
+ br label %bb2
+
+bb2: ; preds = %bb2, %bb
+ %i = select i1 %arg, ptr %arg1, ptr @bar
+ %i3 = call i32 %i()
+ br i1 %arg, label %bb2, label %bb4
+
+bb4: ; preds = %bb2
+ ret i32 %i3
+}
+
+declare i32 @bar() nounwind willreturn memory(none)
``````````
</details>
https://github.com/llvm/llvm-project/pull/116813
More information about the llvm-commits
mailing list