Skip to content

Commit 0c1b33d

Browse files
vladimirradosavljevicDanielCChen
authored andcommitted
[MCP] Skip invalidating def constant regs during forward propagation (llvm#111129)
Before this patch, redundant COPY couldn't be removed for the following case: ``` %reg1 = COPY %const-reg ... // There is a def of %const-reg %reg2 = COPY killed %reg1 ``` where this can be optimized to: ``` ... // There is a def of %const-reg %reg2 = COPY %const-reg ``` This patch allows for such optimization by not invalidating defined constant registers. This is safe, as architectures like AArch64 and RISCV replace a dead definition of a GPR with a zero constant register for certain instructions.
1 parent 85662ee commit 0c1b33d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

llvm/lib/CodeGen/MachineCopyPropagation.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,11 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
886886
"MachineCopyPropagation should be run after register allocation!");
887887

888888
if (MO.isDef() && !MO.isEarlyClobber()) {
889-
Defs.push_back(Reg.asMCReg());
890-
continue;
889+
// Skip invalidating constant registers.
890+
if (!MRI->isConstantPhysReg(Reg)) {
891+
Defs.push_back(Reg.asMCReg());
892+
continue;
893+
}
891894
} else if (MO.readsReg())
892895
ReadRegister(Reg.asMCReg(), MI, MO.isDebug() ? DebugUse : RegularUse);
893896
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass machine-cp -verify-machineinstrs -o - %s | FileCheck %s
3+
4+
---
5+
name: test
6+
body: |
7+
bb.0:
8+
liveins: $w2
9+
; CHECK-LABEL: name: test
10+
; CHECK: liveins: $w2
11+
; CHECK-NEXT: {{ $}}
12+
; CHECK-NEXT: $wzr = SUBSWri killed renamable $w2, 0, 0, implicit-def $nzcv
13+
; CHECK-NEXT: renamable $w0 = COPY $wzr
14+
; CHECK-NEXT: RET_ReallyLR implicit killed $w0
15+
renamable $w1 = COPY $wzr
16+
$wzr = SUBSWri killed renamable $w2, 0, 0, implicit-def $nzcv
17+
renamable $w0 = COPY killed renamable $w1
18+
RET_ReallyLR implicit killed $w0
19+
...

0 commit comments

Comments
 (0)