[llvm] LoopDeletion: Move EH pad check before the isLoopNeverExecuted Check (PR #78189)

Manish Kausik H via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 15 08:47:59 PST 2024


https://github.com/Nirhar created https://github.com/llvm/llvm-project/pull/78189

This commit modifies `LoopDeletion::deleteLoopIfDead` to check if the exit block of a loop is an EH pad before checking if the loop gets executed. This handles the case where an unreacheable loop has a landingpad as an Exit block, and the loop gets deleted, leaving leaving the landingpad without an edge from an unwind clause.

Fixes #76852

>From 98a6318944582463cd0554b86b988586f7e7f3cd Mon Sep 17 00:00:00 2001
From: Nirhar <hmanishkausik at gmail.com>
Date: Mon, 15 Jan 2024 22:06:30 +0530
Subject: [PATCH] LoopDeletion: Move EH pad check before the
 isLoopNeverExecuted Check

This commit modifies `LoopDeletion::deleteLoopIfDead` to check if
the exit block of a loop is an EH pad before checking if the loop
gets executed. This handles the case where an unreacheable loop has
a landingpad as an Exit block, and the loop gets deleted, leaving
leaving the landingpad without an edge from an unwind clause.

Fixes #76852
---
 llvm/lib/Transforms/Scalar/LoopDeletion.cpp | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
index c041e3621a16bdd..bfe9374cf2f8c86 100644
--- a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
@@ -452,6 +452,13 @@ static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT,
 
   BasicBlock *ExitBlock = L->getUniqueExitBlock();
 
+  // We can't directly branch to an EH pad. Don't bother handling this edge
+  // case.
+  if (ExitBlock && ExitBlock->isEHPad()) {
+    LLVM_DEBUG(dbgs() << "Cannot delete loop exiting to EH pad.\n");
+    return LoopDeletionResult::Unmodified;
+  }
+
   if (ExitBlock && isLoopNeverExecuted(L)) {
     LLVM_DEBUG(dbgs() << "Loop is proven to never execute, delete it!\n");
     // We need to forget the loop before setting the incoming values of the exit
@@ -487,13 +494,6 @@ static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT,
     return LoopDeletionResult::Unmodified;
   }
 
-  // We can't directly branch to an EH pad. Don't bother handling this edge
-  // case.
-  if (ExitBlock && ExitBlock->isEHPad()) {
-    LLVM_DEBUG(dbgs() << "Cannot delete loop exiting to EH pad.\n");
-    return LoopDeletionResult::Unmodified;
-  }
-
   // Finally, we have to check that the loop really is dead.
   bool Changed = false;
   if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader, LI)) {



More information about the llvm-commits mailing list