[llvm] f98cc40 - [LoopDeletion] Check for uses in unreachable basic blocks even when there is no exit block. (#173428)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 30 09:00:13 PST 2025
Author: Craig Topper
Date: 2025-12-30T09:00:09-08:00
New Revision: f98cc40b5211a7f008556609fea0bcf285d52bce
URL: https://github.com/llvm/llvm-project/commit/f98cc40b5211a7f008556609fea0bcf285d52bce
DIFF: https://github.com/llvm/llvm-project/commit/f98cc40b5211a7f008556609fea0bcf285d52bce.diff
LOG: [LoopDeletion] Check for uses in unreachable basic blocks even when there is no exit block. (#173428)
Fixes #173357
Added:
llvm/test/Transforms/LoopDeletion/pr173357.ll
Modified:
llvm/lib/Transforms/Utils/LoopUtils.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 8e2a4f80fce16..6a77fc47fb0e1 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -608,30 +608,30 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
llvm::SmallDenseSet<DebugVariable, 4> DeadDebugSet;
llvm::SmallVector<DbgVariableRecord *, 4> DeadDbgVariableRecords;
- if (ExitBlock) {
- // Given LCSSA form is satisfied, we should not have users of instructions
- // within the dead loop outside of the loop. However, LCSSA doesn't take
- // unreachable uses into account. We handle them here.
- // We could do it after drop all references (in this case all users in the
- // loop will be already eliminated and we have less work to do but according
- // to API doc of User::dropAllReferences only valid operation after dropping
- // references, is deletion. So let's substitute all usages of
- // instruction from the loop with poison value of corresponding type first.
- for (auto *Block : L->blocks())
- for (Instruction &I : *Block) {
- auto *Poison = PoisonValue::get(I.getType());
- for (Use &U : llvm::make_early_inc_range(I.uses())) {
- if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
- if (L->contains(Usr->getParent()))
- continue;
- // If we have a DT then we can check that uses outside a loop only in
- // unreachable block.
- if (DT)
- assert(!DT->isReachableFromEntry(U) &&
- "Unexpected user in reachable block");
- U.set(Poison);
- }
+ // Given LCSSA form is satisfied, we should not have users of instructions
+ // within the dead loop outside of the loop. However, LCSSA doesn't take
+ // unreachable uses into account. We handle them here.
+ // We could do it after drop all references (in this case all users in the
+ // loop will be already eliminated and we have less work to do but according
+ // to API doc of User::dropAllReferences only valid operation after dropping
+ // references, is deletion. So let's substitute all usages of
+ // instruction from the loop with poison value of corresponding type first.
+ for (auto *Block : L->blocks())
+ for (Instruction &I : *Block) {
+ auto *Poison = PoisonValue::get(I.getType());
+ for (Use &U : llvm::make_early_inc_range(I.uses())) {
+ if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
+ if (L->contains(Usr->getParent()))
+ continue;
+ // If we have a DT then we can check that uses outside a loop only in
+ // unreachable block.
+ if (DT)
+ assert(!DT->isReachableFromEntry(U) &&
+ "Unexpected user in reachable block");
+ U.set(Poison);
+ }
+ if (ExitBlock) {
// For one of each variable encountered, preserve a debug record (set
// to Poison) and transfer it to the loop exit. This terminates any
// variable locations that were set during the loop.
@@ -646,7 +646,9 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
DeadDbgVariableRecords.push_back(&DVR);
}
}
+ }
+ if (ExitBlock) {
// After the loop has been deleted all the values defined and modified
// inside the loop are going to be unavailable. Values computed in the
// loop will have been deleted, automatically causing their debug uses
diff --git a/llvm/test/Transforms/LoopDeletion/pr173357.ll b/llvm/test/Transforms/LoopDeletion/pr173357.ll
new file mode 100644
index 0000000000000..25caf178ecb2a
--- /dev/null
+++ b/llvm/test/Transforms/LoopDeletion/pr173357.ll
@@ -0,0 +1,30 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes=loop-deletion < %s | FileCheck %s
+
+define void @foo(ptr %P) #0 {
+; CHECK-LABEL: define void @foo(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[BB:.*:]]
+; CHECK-NEXT: [[P_PROMOTED2:%.*]] = load ptr, ptr [[P]], align 8
+; CHECK-NEXT: unreachable
+; CHECK: [[MERGE:.*:]]
+; CHECK-NEXT: store ptr poison, ptr [[P]], align 8
+; CHECK-NEXT: unreachable
+;
+bb:
+ %p.promoted2 = load ptr, ptr %P, align 8
+ br label %bb1
+
+bb1: ; preds = %bb1.backedge, %bb
+ %p.ph = phi ptr [ %P, %bb ], [ %p.promoted2, %bb1.backedge ]
+ br label %bb1.backedge
+
+bb1.backedge: ; preds = %bb1, %merge
+ br label %bb1
+
+merge: ; No predecessors!
+ store ptr %p.ph, ptr %P, align 8
+ br label %bb1.backedge
+}
+
+attributes #0 = { willreturn }
More information about the llvm-commits
mailing list