Skip to content

Commit da331a1

Browse files
committed
[PHIElimination] Reuse existing COPY in predecessor basic block
Simplifies the CFG later on, added a regression test.
1 parent 6ae4030 commit da331a1

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

llvm/lib/CodeGen/PHIElimination.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,15 @@ void PHIEliminationImpl::LowerPHINode(MachineBasicBlock &MBB,
582582
continue;
583583
}
584584

585+
// Reuse an existing copy in the block if possible.
586+
if (MachineInstr *DefMI = MRI->getUniqueVRegDef(SrcReg)) {
587+
if (DefMI->isCopy() && DefMI->getParent() == &opBlock &&
588+
MRI->use_empty(SrcReg)) {
589+
DefMI->getOperand(0).setReg(IncomingReg);
590+
continue;
591+
}
592+
}
593+
585594
// Find a safe location to insert the copy, this may be the first terminator
586595
// in the block (or end()).
587596
MachineBasicBlock::iterator InsertPos =
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -run-pass=phi-node-elimination -mtriple=aarch64-linux-gnu -o - %s | FileCheck %s
3+
4+
# Verify that the original COPY in bb.1 is reappropriated as the PHI source in bb.2,
5+
# instead of creating a new COPY with the same source register.
6+
7+
---
8+
name: copy_virtual_reg
9+
tracksRegLiveness: true
10+
body: |
11+
; CHECK-LABEL: name: copy_virtual_reg
12+
; CHECK: bb.0:
13+
; CHECK-NEXT: successors: %bb.2(0x40000000), %bb.1(0x40000000)
14+
; CHECK-NEXT: liveins: $nzcv, $w0
15+
; CHECK-NEXT: {{ $}}
16+
; CHECK-NEXT: %a:gpr32 = COPY $w0
17+
; CHECK-NEXT: [[DEF:%[0-9]+]]:gpr32 = IMPLICIT_DEF
18+
; CHECK-NEXT: Bcc 8, %bb.2, implicit $nzcv
19+
; CHECK-NEXT: {{ $}}
20+
; CHECK-NEXT: bb.1:
21+
; CHECK-NEXT: successors: %bb.2(0x80000000)
22+
; CHECK-NEXT: {{ $}}
23+
; CHECK-NEXT: [[DEF:%[0-9]+]]:gpr32 = COPY %a
24+
; CHECK-NEXT: {{ $}}
25+
; CHECK-NEXT: bb.2:
26+
; CHECK-NEXT: %c:gpr32 = COPY [[DEF]]
27+
; CHECK-NEXT: %d:gpr32 = COPY %c
28+
bb.0:
29+
liveins: $nzcv, $w0
30+
%a:gpr32 = COPY $w0
31+
Bcc 8, %bb.2, implicit $nzcv
32+
bb.1:
33+
%b:gpr32 = COPY %a:gpr32
34+
bb.2:
35+
%c:gpr32 = PHI %b:gpr32, %bb.1, undef %undef:gpr32, %bb.0
36+
%d:gpr32 = COPY %c:gpr32
37+
...
38+
39+
---
40+
name: copy_physical_reg
41+
tracksRegLiveness: true
42+
body: |
43+
; CHECK-LABEL: name: copy_physical_reg
44+
; CHECK: bb.0:
45+
; CHECK-NEXT: successors: %bb.2(0x40000000), %bb.1(0x40000000)
46+
; CHECK-NEXT: liveins: $nzcv, $w0
47+
; CHECK-NEXT: {{ $}}
48+
; CHECK-NEXT: [[DEF:%[0-9]+]]:gpr32 = IMPLICIT_DEF
49+
; CHECK-NEXT: Bcc 8, %bb.2, implicit $nzcv
50+
; CHECK-NEXT: {{ $}}
51+
; CHECK-NEXT: bb.1:
52+
; CHECK-NEXT: successors: %bb.2(0x80000000)
53+
; CHECK-NEXT: {{ $}}
54+
; CHECK-NEXT: $x0 = IMPLICIT_DEF
55+
; CHECK-NEXT: [[DEF:%[0-9]+]]:gpr32 = COPY $w0
56+
; CHECK-NEXT: {{ $}}
57+
; CHECK-NEXT: bb.2:
58+
; CHECK-NEXT: %b:gpr32 = COPY [[DEF]]
59+
bb.0:
60+
liveins: $nzcv, $w0
61+
Bcc 8, %bb.2, implicit $nzcv
62+
bb.1:
63+
$x0 = IMPLICIT_DEF
64+
%a:gpr32 = COPY $w0
65+
bb.2:
66+
%b:gpr32 = PHI %a:gpr32, %bb.1, undef %undef:gpr32, %bb.0
67+
...
68+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# RUN: llc -mtriple=aarch64-linux-gnu -verify-machineinstrs -o - %s \
2+
# RUN: -start-before=phi-node-elimination -stop-after=branch-relaxation \
3+
# RUN: | FileCheck %s
4+
5+
# Verify an optimal block layout is produced for the following nested loop, when
6+
# there's a PHI node in bb.5 that relies on an operand in bb.2.
7+
# The previous layout used a CBNZX for a null check followed by a unconditional
8+
# branch to bb.6, instead of a fallthrough.
9+
10+
# [ bb.0 ENTRY ]
11+
# |
12+
# v
13+
# [ bb.1 ] <-------+
14+
# / \ |
15+
# v v |
16+
# [bb.2] [bb.3] <-+ |
17+
# | | | |
18+
# | v | |
19+
# | [bb.4] --+ |
20+
# | | |
21+
# v v |
22+
# [ bb.5 ] |
23+
# / \ |
24+
# | v |
25+
# | [bb.6] -----+
26+
# | |
27+
# v v
28+
# [ bb.7 RET ]
29+
30+
# CHECK-LABEL: test
31+
# CHECK-NOT: CBNZX
32+
# CHECK-NOT: B %bb.
33+
# CHECK-COUNT-2: CBZX
34+
35+
---
36+
name: test
37+
tracksRegLiveness: true
38+
body: |
39+
bb.0:
40+
successors: %bb.1(0x80000000); %bb.1(100.00%)
41+
liveins: $x0, $w1, $x2, $x3
42+
%0:gpr64all = IMPLICIT_DEF
43+
%1:gpr64common = IMPLICIT_DEF
44+
%2:gpr32common = IMPLICIT_DEF
45+
%3:gpr32 = IMPLICIT_DEF
46+
B %bb.1
47+
48+
bb.1:
49+
successors: %bb.2(0x30000000), %bb.3(0x50000000); %bb.2(37.50%), %bb.3(62.50%)
50+
%4:gpr64common = PHI undef %0:gpr64all, %bb.0, %5:gpr64common, %bb.6
51+
%6:gpr64 = LDRXui undef %1:gpr64common, 0 :: (load (s64))
52+
STRXui killed %4:gpr64common, undef %1:gpr64common, 0 :: (store (s64))
53+
CBNZX undef %6:gpr64, %bb.3
54+
55+
bb.2:
56+
successors: %bb.5(0x80000000); %bb.5(100.00%)
57+
%7:gpr64all = COPY killed %6:gpr64
58+
B %bb.5
59+
60+
bb.3:
61+
successors: %bb.5(0x04000000), %bb.4(0x7c000000); %bb.5(3.12%), %bb.4(96.88%)
62+
dead $wzr = SUBSWrr killed undef %3:gpr32, killed undef %2:gpr32common, implicit-def $nzcv
63+
Bcc 12, %bb.5, implicit killed undef $nzcv
64+
B %bb.4
65+
66+
bb.4:
67+
successors: %bb.5(0x04000000), %bb.3(0x7c000000); %bb.5(3.12%), %bb.3(96.88%)
68+
dead $xzr = SUBSXrr killed undef %6:gpr64, killed undef %6:gpr64, implicit-def $nzcv
69+
Bcc 1, %bb.3, implicit killed undef $nzcv
70+
71+
bb.5:
72+
successors: %bb.7(0x04000000), %bb.6(0x7c000000); %bb.7(3.12%), %bb.6(96.88%)
73+
%5:gpr64common = PHI %7:gpr64all, %bb.2, undef %0:gpr64all, %bb.3, undef %0:gpr64all, %bb.4
74+
CBZX undef %5:gpr64common, %bb.7
75+
B %bb.6
76+
77+
bb.6:
78+
successors: %bb.7(0x04000000), %bb.1(0x7c000000); %bb.7(3.12%), %bb.1(96.88%)
79+
dead $wzr = SUBSWrr killed undef %3:gpr32, killed undef %2:gpr32common, implicit-def $nzcv
80+
Bcc 12, %bb.7, implicit killed undef $nzcv
81+
B %bb.1
82+
83+
bb.7:
84+
RET_ReallyLR
85+
...

0 commit comments

Comments
 (0)