diff --git a/llvm/lib/CodeGen/MachineCopyPropagation.cpp b/llvm/lib/CodeGen/MachineCopyPropagation.cpp index 1105b8c15515f..5102639a3a30a 100644 --- a/llvm/lib/CodeGen/MachineCopyPropagation.cpp +++ b/llvm/lib/CodeGen/MachineCopyPropagation.cpp @@ -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 Defs; const MachineOperand *RegMask = nullptr; diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll index 2fcb911c2654a..309ebf71127c4 100644 --- a/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll +++ b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv32.ll @@ -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 diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll index 2c3e3faddc391..8e4c0376ad0a5 100644 --- a/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll +++ b/llvm/test/CodeGen/RISCV/GlobalISel/constbarrier-rv64.ll @@ -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: diff --git a/llvm/test/CodeGen/RISCV/machine-copyprop-noop-removal.mir b/llvm/test/CodeGen/RISCV/machine-copyprop-noop-removal.mir index 0ddd7d8bfc12a..d739537b50d05 100644 --- a/llvm/test/CodeGen/RISCV/machine-copyprop-noop-removal.mir +++ b/llvm/test/CodeGen/RISCV/machine-copyprop-noop-removal.mir @@ -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 @@ -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: diff --git a/llvm/test/CodeGen/RISCV/sextw-removal.ll b/llvm/test/CodeGen/RISCV/sextw-removal.ll index e0a16aa05cd00..49494608eee4d 100644 --- a/llvm/test/CodeGen/RISCV/sextw-removal.ll +++ b/llvm/test/CodeGen/RISCV/sextw-removal.ll @@ -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)