Skip to content

LoopDeletion: Move EH pad check before the isLoopNeverExecuted Check #78189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions llvm/lib/Transforms/Scalar/LoopDeletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
37 changes: 37 additions & 0 deletions llvm/test/Transforms/LoopDeletion/loop-with-ehpad-not-executed.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt %s -passes=loop-deletion -S | FileCheck %s

define void @wombat() personality ptr null {
; CHECK-LABEL: define void @wombat() 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()
; 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()
to label %bb2 unwind label %bb3

bb3: ; preds = %bb2
%landingpad = landingpad { ptr, i32 }
cleanup
ret void

bb4: ; preds = %bb
ret void
}