Skip to content

Commit bf7342d

Browse files
aemersonmemfrob
authored andcommitted
[GlobalISel] Clear unreachable blocks' contents after selection.
If these blocks are unreachable, then we can discard all of the instructions. However, keep the block around because it may have an address taken or the block may have a stale reference from a PHI somewhere. Instead of finding those PHIs and fixing them up, just leave the block empty. Differential Revision: https://reviews.llvm.org/D111201
1 parent 0ab2216 commit bf7342d

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
130130
// Until then, keep track of the number of blocks to assert that we don't.
131131
const size_t NumBlocks = MF.size();
132132
#endif
133+
// Keep track of selected blocks, so we can delete unreachable ones later.
134+
DenseSet<MachineBasicBlock *> SelectedBlocks;
133135

134136
for (MachineBasicBlock *MBB : post_order(&MF)) {
135137
ISel->CurMBB = MBB;
138+
SelectedBlocks.insert(MBB);
136139
if (MBB->empty())
137140
continue;
138141

@@ -205,6 +208,15 @@ bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
205208
if (MBB.empty())
206209
continue;
207210

211+
if (!SelectedBlocks.contains(&MBB)) {
212+
// This is an unreachable block and therefore hasn't been selected, since
213+
// the main selection loop above uses a postorder block traversal.
214+
// We delete all the instructions in this block since it's unreachable.
215+
MBB.clear();
216+
// Don't delete the block in case the block has it's address taken or is
217+
// still being referenced by a phi somewhere.
218+
continue;
219+
}
208220
// Try to find redundant copies b/w vregs of the same register class.
209221
bool ReachedBegin = false;
210222
for (auto MII = std::prev(MBB.end()), Begin = MBB.begin(); !ReachedBegin;) {

llvm/test/CodeGen/AArch64/GlobalISel/select-blockaddress.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ body: |
3636
; CHECK: STRXui [[COPY]], [[MOVaddr]], 0 :: (store (p0) into @addr)
3737
; CHECK: BR [[MOVaddrBA]]
3838
; CHECK: bb.1.block (address-taken):
39-
; CHECK: RET_ReallyLR
4039
; LARGE-LABEL: name: test_blockaddress
4140
; LARGE: bb.0 (%ir-block.0):
4241
; LARGE: [[MOVZXi:%[0-9]+]]:gpr64 = MOVZXi target-flags(aarch64-g0, aarch64-nc) blockaddress(@test_blockaddress, %ir-block.block), 0
@@ -50,7 +49,6 @@ body: |
5049
; LARGE: STRXui [[MOVKXi2]], [[MOVKXi5]], 0 :: (store (p0) into @addr)
5150
; LARGE: BR [[MOVKXi2]]
5251
; LARGE: bb.1.block (address-taken):
53-
; LARGE: RET_ReallyLR
5452
bb.1 (%ir-block.0):
5553
%0:gpr(p0) = G_BLOCK_ADDR blockaddress(@test_blockaddress, %ir-block.block)
5654
%1:gpr(p0) = G_GLOBAL_VALUE @addr
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple=aarch64-unknown-unknown -o - -verify-machineinstrs -run-pass=instruction-select %s | FileCheck %s
3+
---
4+
name: test_unreachable_delete
5+
alignment: 4
6+
exposesReturnsTwice: false
7+
legalized: true
8+
regBankSelected: true
9+
selected: false
10+
tracksRegLiveness: true
11+
liveins:
12+
body: |
13+
; CHECK-LABEL: name: test_unreachable_delete
14+
; CHECK: bb.0:
15+
; CHECK-NEXT: successors: %bb.2(0x80000000)
16+
; CHECK-NEXT: liveins: $w0
17+
; CHECK-NEXT: {{ $}}
18+
; CHECK-NEXT: [[COPY:%[0-9]+]]:gpr32sp = COPY $w0
19+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:gpr32 = COPY $wzr
20+
; CHECK-NEXT: [[MOVi32imm:%[0-9]+]]:gpr32 = MOVi32imm 1
21+
; CHECK-NEXT: B %bb.2
22+
; CHECK-NEXT: {{ $}}
23+
; CHECK-NEXT: bb.1:
24+
; CHECK-NEXT: successors: %bb.2(0x80000000)
25+
; CHECK-NEXT: {{ $}}
26+
; CHECK-NEXT: {{ $}}
27+
; CHECK-NEXT: bb.2:
28+
; CHECK-NEXT: successors: %bb.3(0x80000000)
29+
; CHECK-NEXT: {{ $}}
30+
; CHECK-NEXT: [[ADDWri:%[0-9]+]]:gpr32sp = ADDWri [[COPY]], 2, 0
31+
; CHECK-NEXT: {{ $}}
32+
; CHECK-NEXT: bb.3:
33+
; CHECK-NEXT: [[PHI:%[0-9]+]]:gpr32 = PHI [[ADDWri]], %bb.2
34+
; CHECK-NEXT: $w0 = COPY [[PHI]]
35+
; CHECK-NEXT: RET_ReallyLR implicit $w0
36+
bb.1:
37+
successors: %bb.3
38+
liveins: $w0
39+
40+
%0:gpr(s32) = COPY $w0
41+
%1:gpr(s32) = G_CONSTANT i32 0
42+
%3:gpr(s32) = G_CONSTANT i32 1
43+
%5:gpr(s32) = G_CONSTANT i32 2
44+
G_BR %bb.3
45+
46+
bb.5:
47+
; This block is unreachable.
48+
%unreachable_inst:gpr(s32) = G_XOR %1, %3
49+
$w0 = COPY %unreachable_inst(s32)
50+
51+
bb.3:
52+
successors: %bb.4(0x80000000)
53+
54+
%6:gpr(s32) = G_ADD %0, %5
55+
56+
bb.4:
57+
%7:gpr(s32) = G_PHI %6(s32), %bb.3
58+
$w0 = COPY %7(s32)
59+
RET_ReallyLR implicit $w0
60+
61+
...

0 commit comments

Comments
 (0)