@@ -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 ;
0 commit comments