[llvm] [LoopDeletion] Check for uses in unreachable basic blocks even when there is no exit block. (PR #173428)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 24 09:33:14 PST 2025


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/173428

>From c77afefc5d828dc710f9069e0536f58bcc32bd8e Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 23 Dec 2025 16:02:05 -0800
Subject: [PATCH 1/2] [LoopDeletion] Check for uses in unreachable basic blocks
 even when there is no exit block.

Fixes #173357
---
 llvm/lib/Transforms/Utils/LoopUtils.cpp       | 72 +++++++++----------
 llvm/test/Transforms/LoopDeletion/pr173357.ll | 30 ++++++++
 2 files changed, 66 insertions(+), 36 deletions(-)
 create mode 100644 llvm/test/Transforms/LoopDeletion/pr173357.ll

diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 8e2a4f80fce16..84332da7fcc67 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -608,45 +608,45 @@ 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);
-        }
-
-        // 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.
-        for (DbgVariableRecord &DVR :
-             llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
-          DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
-                            DVR.getDebugLoc().get());
-          if (!DeadDebugSet.insert(Key).second)
+  // 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;
-          // Unlinks the DVR from it's container, for later insertion.
-          DVR.removeFromParent();
-          DeadDbgVariableRecords.push_back(&DVR);
-        }
+        // 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);
       }
 
+      // 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.
+      for (DbgVariableRecord &DVR :
+           llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
+        DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
+                          DVR.getDebugLoc().get());
+        if (!DeadDebugSet.insert(Key).second)
+          continue;
+        // Unlinks the DVR from it's container, for later insertion.
+        DVR.removeFromParent();
+        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 }

>From 963ccc4a2f4062aadb742213bffca5a7d72d1a55 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 24 Dec 2025 09:32:52 -0800
Subject: [PATCH 2/2] fixup! Address review comment

---
 llvm/lib/Transforms/Utils/LoopUtils.cpp | 26 +++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 84332da7fcc67..6a77fc47fb0e1 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -631,18 +631,20 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
         U.set(Poison);
       }
 
-      // 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.
-      for (DbgVariableRecord &DVR :
-           llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
-        DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
-                          DVR.getDebugLoc().get());
-        if (!DeadDebugSet.insert(Key).second)
-          continue;
-        // Unlinks the DVR from it's container, for later insertion.
-        DVR.removeFromParent();
-        DeadDbgVariableRecords.push_back(&DVR);
+      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.
+        for (DbgVariableRecord &DVR :
+             llvm::make_early_inc_range(filterDbgVars(I.getDbgRecordRange()))) {
+          DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
+                            DVR.getDebugLoc().get());
+          if (!DeadDebugSet.insert(Key).second)
+            continue;
+          // Unlinks the DVR from it's container, for later insertion.
+          DVR.removeFromParent();
+          DeadDbgVariableRecords.push_back(&DVR);
+        }
       }
     }
 



More information about the llvm-commits mailing list