Skip to content

[MachineCopyPropagation] Recognise and delete no-op moves produced after forwarded uses #129889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions llvm/lib/CodeGen/MachineCopyPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,19 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {

forwardUses(MI);

// It's possible that the previous transformation has resulted in a no-op
// register move (i.e. one where source and destination registers are the
// same and are not referring to a reserved register). If so, delete it.
CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr);
if (CopyOperands &&
CopyOperands->Source->getReg() == CopyOperands->Destination->getReg() &&
!MRI->isReserved(CopyOperands->Source->getReg())) {
MI.eraseFromParent();
NumDeletes++;
Changed = true;
continue;
}

// Not a copy.
SmallVector<Register, 4> Defs;
const MachineOperand *RegMask = nullptr;
Expand Down
1 change: 0 additions & 1 deletion llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ define void @constant_fold_barrier_i128(ptr %p) {
; RV32-NEXT: seqz a7, a6
; RV32-NEXT: and a1, a7, a1
; RV32-NEXT: add a7, a4, zero
; RV32-NEXT: add a5, a5, zero
; RV32-NEXT: sltu a4, a4, a4
; RV32-NEXT: or a1, a3, a1
; RV32-NEXT: add a7, a7, a1
Expand Down
1 change: 0 additions & 1 deletion llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ define i128 @constant_fold_barrier_i128(i128 %x) {
; RV64-NEXT: and a0, a0, a2
; RV64-NEXT: add a0, a0, a2
; RV64-NEXT: sltu a2, a0, a2
; RV64-NEXT: add a1, a1, zero
; RV64-NEXT: add a1, a1, a2
; RV64-NEXT: ret
entry:
Expand Down
2 changes: 0 additions & 2 deletions llvm/test/CodeGen/RISCV/machine-copyprop-noop-removal.mir
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

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

---
name: ham
Expand All @@ -22,7 +21,6 @@ body: |
; CHECK-NEXT: liveins: $x10
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: $x11 = ADDI $x0, 0
; CHECK-NEXT: renamable $x10 = ADD $x0, killed renamable $x10
; CHECK-NEXT: BEQ renamable $x10, $x0, %bb.4
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.2:
Expand Down
1 change: 0 additions & 1 deletion llvm/test/CodeGen/RISCV/sextw-removal.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,6 @@ define signext i32 @sextw_sh2add(i1 zeroext %0, ptr %1, i32 signext %2, i32 sign
; NOREMOVAL-LABEL: sextw_sh2add:
; NOREMOVAL: # %bb.0:
; NOREMOVAL-NEXT: sh2add a2, a2, a3
; NOREMOVAL-NEXT: mv a2, a2
; NOREMOVAL-NEXT: beqz a0, .LBB22_2
; NOREMOVAL-NEXT: # %bb.1:
; NOREMOVAL-NEXT: sw a2, 0(a1)
Expand Down
Loading