Skip to content

Commit dffbc03

Browse files
authored
[MachineCopyPropagation] Recognise and delete no-op moves produced after forwarded uses (#129889)
This change removes 189 static instances of no-op reg-reg moves (i.e. where src == dest) across llvm-test-suite when compiled for RISC-V rv64gc and with SPEC included.
1 parent 4e453d5 commit dffbc03

File tree

5 files changed

+13
-5
lines changed

5 files changed

+13
-5
lines changed

llvm/lib/CodeGen/MachineCopyPropagation.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,19 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
971971

972972
forwardUses(MI);
973973

974+
// It's possible that the previous transformation has resulted in a no-op
975+
// register move (i.e. one where source and destination registers are the
976+
// same and are not referring to a reserved register). If so, delete it.
977+
CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr);
978+
if (CopyOperands &&
979+
CopyOperands->Source->getReg() == CopyOperands->Destination->getReg() &&
980+
!MRI->isReserved(CopyOperands->Source->getReg())) {
981+
MI.eraseFromParent();
982+
NumDeletes++;
983+
Changed = true;
984+
continue;
985+
}
986+
974987
// Not a copy.
975988
SmallVector<Register, 4> Defs;
976989
const MachineOperand *RegMask = nullptr;

llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ define void @constant_fold_barrier_i128(ptr %p) {
3838
; RV32-NEXT: seqz a7, a6
3939
; RV32-NEXT: and a1, a7, a1
4040
; RV32-NEXT: add a7, a4, zero
41-
; RV32-NEXT: add a5, a5, zero
4241
; RV32-NEXT: sltu a4, a4, a4
4342
; RV32-NEXT: or a1, a3, a1
4443
; RV32-NEXT: add a7, a7, a1

llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ define i128 @constant_fold_barrier_i128(i128 %x) {
2626
; RV64-NEXT: and a0, a0, a2
2727
; RV64-NEXT: add a0, a0, a2
2828
; RV64-NEXT: sltu a2, a0, a2
29-
; RV64-NEXT: add a1, a1, zero
3029
; RV64-NEXT: add a1, a1, a2
3130
; RV64-NEXT: ret
3231
entry:

llvm/test/CodeGen/RISCV/machine-copyprop-noop-removal.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
## This test was added to capture a case where MachineCopyPropagation risks
55
## leaving a no-op register move (add, x0, reg).
6-
## FIXME: No-op register move is left behind after machine-cp.
76

87
---
98
name: ham
@@ -22,7 +21,6 @@ body: |
2221
; CHECK-NEXT: liveins: $x10
2322
; CHECK-NEXT: {{ $}}
2423
; CHECK-NEXT: $x11 = ADDI $x0, 0
25-
; CHECK-NEXT: renamable $x10 = ADD $x0, killed renamable $x10
2624
; CHECK-NEXT: BEQ renamable $x10, $x0, %bb.4
2725
; CHECK-NEXT: {{ $}}
2826
; CHECK-NEXT: bb.2:

llvm/test/CodeGen/RISCV/sextw-removal.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,6 @@ define signext i32 @sextw_sh2add(i1 zeroext %0, ptr %1, i32 signext %2, i32 sign
13521352
; NOREMOVAL-LABEL: sextw_sh2add:
13531353
; NOREMOVAL: # %bb.0:
13541354
; NOREMOVAL-NEXT: sh2add a2, a2, a3
1355-
; NOREMOVAL-NEXT: mv a2, a2
13561355
; NOREMOVAL-NEXT: beqz a0, .LBB22_2
13571356
; NOREMOVAL-NEXT: # %bb.1:
13581357
; NOREMOVAL-NEXT: sw a2, 0(a1)

0 commit comments

Comments
 (0)