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

Manish Kausik H via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 18 08:32:11 PST 2024


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

>From 4d8faa9cd857e1081ceb3806fb4d921e5c637de2 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 +++----
 .../loop-with-ehpad-not-executed.ll           | 40 +++++++++++++++++++
 2 files changed, 47 insertions(+), 7 deletions(-)
 create mode 100644 llvm/test/Transforms/LoopDeletion/loop-with-ehpad-not-executed.ll

diff --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
index c041e3621a16bd..bfe9374cf2f8c8 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)) {
diff --git a/llvm/test/Transforms/LoopDeletion/loop-with-ehpad-not-executed.ll b/llvm/test/Transforms/LoopDeletion/loop-with-ehpad-not-executed.ll
new file mode 100644
index 00000000000000..6a3f993b9c1495
--- /dev/null
+++ b/llvm/test/Transforms/LoopDeletion/loop-with-ehpad-not-executed.ll
@@ -0,0 +1,40 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt %s -passes=loop-deletion -S | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @wombat() gc "statepoint-example" personality ptr null {
+; CHECK-LABEL: define void @wombat() gc "statepoint-example" personality ptr null {
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    br i1 false, label [[BB1:%.*]], label [[BB4:%.*]]
+; CHECK:       bb1:
+; CHECK-NEXT:    br label [[BB2:%.*]]
+; CHECK:       bb2:
+; CHECK-NEXT:    [[INVOKE:%.*]] = invoke double null(i64 0)
+; CHECK-NEXT:            to label [[BB2]] unwind label [[BB3:%.*]]
+; CHECK:       bb3:
+; CHECK-NEXT:    [[LANDINGPAD:%.*]] = landingpad { ptr, i32 }
+; CHECK-NEXT:            cleanup
+; CHECK-NEXT:    ret void
+; CHECK:       bb4:
+; CHECK-NEXT:    ret void
+;
+bb:
+  br i1 false, label %bb1, label %bb4
+
+bb1:                                              ; preds = %bb
+  br label %bb2
+
+bb2:                                              ; preds = %bb1, %bb2
+  %invoke = invoke double null(i64 0)
+  to label %bb2 unwind label %bb3
+
+bb3:                                              ; preds = %bb2
+  %landingpad = landingpad { ptr, i32 }
+  cleanup
+  ret void
+
+bb4:                                              ; preds = %bb
+  ret void
+}



More information about the llvm-commits mailing list