Skip to content

Commit 789cfcf

Browse files
committed
[ShrinkWrap] Ignore no-return blocks in shrink wrapping
1 parent 3dcc75d commit 789cfcf

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,13 @@ class MachineBasicBlock
988988
return !empty() && back().isEHScopeReturn();
989989
}
990990

991+
/// Convenience function that returns true if the block exits the function
992+
/// without returning.
993+
bool isNoReturnBlock() const {
994+
return !empty() && succ_empty() && !back().isReturn() &&
995+
!back().isIndirectBranch();
996+
}
997+
991998
/// Split a basic block into 2 pieces at \p SplitPoint. A new block will be
992999
/// inserted after this block, and all instructions after \p SplitInst moved
9931000
/// to it (\p SplitInst will be in the original block). If \p LIS is provided,

llvm/lib/CodeGen/ShrinkWrap.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -697,14 +697,12 @@ void ShrinkWrapImpl::updateSaveRestorePoints(MachineBasicBlock &MBB,
697697

698698
if (!Restore)
699699
Restore = &MBB;
700-
else if (MPDT->getNode(&MBB)) // If the block is not in the post dom tree, it
701-
// means the block never returns. If that's the
702-
// case, we don't want to call
703-
// `findNearestCommonDominator`, which will
704-
// return `Restore`.
700+
else if (MBB.isNoReturnBlock()) {
701+
// MBB exits the function without returning, so we don't need an epilogue
702+
// here. This is common for things like cleanup landing pads etc. In these
703+
// cases, we can skip updating `Restore`.
704+
} else
705705
Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
706-
else
707-
Restore = nullptr; // Abort, we can't find a restore point in this case.
708706

709707
// Make sure we would be able to insert the restore code before the
710708
// terminator.

llvm/test/CodeGen/AArch64/shrinkwrap-no-return.ll

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,33 @@ define noundef i32 @early_exit_or_throw(i32 %in) personality ptr @__gxx_personal
1010
; CHECK-NEXT: .cfi_personality 156, DW.ref.__gxx_personality_v0
1111
; CHECK-NEXT: .cfi_lsda 28, .Lexception0
1212
; CHECK-NEXT: // %bb.0: // %entry
13+
; CHECK-NEXT: cmp w0, #1
14+
; CHECK-NEXT: b.eq .LBB0_2
15+
; CHECK-NEXT: // %bb.1: // %exit
16+
; CHECK-NEXT: mov w0, wzr
17+
; CHECK-NEXT: ret
18+
; CHECK-NEXT: .LBB0_2: // %setup
1319
; CHECK-NEXT: str x30, [sp, #-32]! // 8-byte Folded Spill
1420
; CHECK-NEXT: stp x20, x19, [sp, #16] // 16-byte Folded Spill
1521
; CHECK-NEXT: .cfi_def_cfa_offset 32
1622
; CHECK-NEXT: .cfi_offset w19, -8
1723
; CHECK-NEXT: .cfi_offset w20, -16
1824
; CHECK-NEXT: .cfi_offset w30, -32
19-
; CHECK-NEXT: cmp w0, #1
20-
; CHECK-NEXT: b.eq .LBB1_2
21-
; CHECK-NEXT: // %bb.1: // %exit
22-
; CHECK-NEXT: ldp x20, x19, [sp, #16] // 16-byte Folded Reload
23-
; CHECK-NEXT: mov w0, wzr
24-
; CHECK-NEXT: ldr x30, [sp], #32 // 8-byte Folded Reload
25-
; CHECK-NEXT: ret
26-
; CHECK-NEXT: .LBB1_2: // %setup
2725
; CHECK-NEXT: mov w0, #32 // =0x20
2826
; CHECK-NEXT: bl __cxa_allocate_exception
29-
; CHECK-NEXT: mov x19, x0
3027
; CHECK-NEXT: .Ltmp0: // EH_LABEL
3128
; CHECK-NEXT: bl construct_exception
3229
; CHECK-NEXT: .Ltmp1: // EH_LABEL
30+
; CHECK-NEXT: ldp x20, x19, [sp, #16] // 16-byte Folded Reload
31+
; CHECK-NEXT: ldr x30, [sp], #32 // 8-byte Folded Reload
3332
; CHECK-NEXT: // %bb.3: // %throw
3433
; CHECK-NEXT: adrp x2, :got:destruct_exception
3534
; CHECK-NEXT: adrp x1, exception
3635
; CHECK-NEXT: add x1, x1, :lo12:exception
3736
; CHECK-NEXT: ldr x2, [x2, :got_lo12:destruct_exception]
3837
; CHECK-NEXT: mov x0, x19
3938
; CHECK-NEXT: bl __cxa_throw
40-
; CHECK-NEXT: .LBB1_4: // %teardown
39+
; CHECK-NEXT: .LBB0_4: // %teardown
4140
; CHECK-NEXT: .Ltmp2: // EH_LABEL
4241
; CHECK-NEXT: mov x20, x0
4342
; CHECK-NEXT: mov x0, x19

0 commit comments

Comments
 (0)