Skip to content

Commit defbbf0

Browse files
authored
[RISCV][MoveMerge] Don't copy kill flag when moving past an instruction that reads the register. (llvm#153644)
If we're moving the second copy before another instruction that reads the copied register, we need to clear the kill flag on the combined move. Fixes llvm#153598.
1 parent b989c7c commit defbbf0

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

llvm/lib/Target/RISCV/RISCVMoveMerger.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ RISCVMoveMerge::mergePairedInsns(MachineBasicBlock::iterator I,
137137
NextI = next_nodbg(NextI, E);
138138
DebugLoc DL = I->getDebugLoc();
139139

140+
// Make a copy so we can update the kill flag in the MoveFromAToS case. The
141+
// copied operand needs to be scoped outside the if since we make a pointer
142+
// to it.
143+
MachineOperand PairedSource = *PairedRegs.Source;
144+
140145
// The order of S-reg depends on which instruction holds A0, instead of
141146
// the order of register pair.
142147
// e,g.
@@ -147,8 +152,15 @@ RISCVMoveMerge::mergePairedInsns(MachineBasicBlock::iterator I,
147152
// mv a1, s1 => cm.mva01s s2,s1
148153
bool StartWithX10 = ARegInFirstPair == RISCV::X10;
149154
if (isMoveFromAToS(Opcode)) {
150-
Sreg1 = StartWithX10 ? FirstPair.Source : PairedRegs.Source;
151-
Sreg2 = StartWithX10 ? PairedRegs.Source : FirstPair.Source;
155+
// We are moving one of the copies earlier so its kill flag may become
156+
// invalid. Clear the copied kill flag if there are any reads of the
157+
// register between the new location and the old location.
158+
for (auto It = std::next(I); It != Paired && PairedSource.isKill(); ++It)
159+
if (It->readsRegister(PairedSource.getReg(), TRI))
160+
PairedSource.setIsKill(false);
161+
162+
Sreg1 = StartWithX10 ? FirstPair.Source : &PairedSource;
163+
Sreg2 = StartWithX10 ? &PairedSource : FirstPair.Source;
152164
} else {
153165
Sreg1 = StartWithX10 ? FirstPair.Destination : PairedRegs.Destination;
154166
Sreg2 = StartWithX10 ? PairedRegs.Destination : FirstPair.Destination;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=riscv32 -mattr=+zcmp -run-pass=riscv-move-merge -verify-machineinstrs %s -o - | FileCheck %s
3+
---
4+
name: mov-merge
5+
tracksRegLiveness: true
6+
body: |
7+
bb.0.entry:
8+
liveins: $x8, $x9
9+
; CHECK-LABEL: name: mov-merge
10+
; CHECK: liveins: $x8, $x9
11+
; CHECK-NEXT: {{ $}}
12+
; CHECK-NEXT: $x12 = ADDI $x0, -3
13+
; CHECK-NEXT: SW renamable $x9, $x2, 56
14+
; CHECK-NEXT: CM_MVA01S killed renamable $x9, renamable $x8, implicit-def $x10, implicit-def $x11
15+
; CHECK-NEXT: SW renamable $x8, $x2, 60
16+
; CHECK-NEXT: PseudoRET
17+
$x12 = ADDI $x0, -3
18+
SW renamable $x9, $x2, 56
19+
$x10 = ADDI killed renamable $x9, 0
20+
SW renamable $x8, $x2, 60
21+
$x11 = ADDI killed renamable $x8, 0
22+
PseudoRET
23+
...

0 commit comments

Comments
 (0)