Skip to content

Commit cd60ee9

Browse files
authored
[RISCV] Prevent using dummy_reg_pair_with_x0 in more places. (#141273)
Similar to #141261. These aren't easy to test without write MIR tests in areas we don't currently have tests. I'm not sure we use X0_Pair anywhere today. I'm going to try to migrate RISCVMakeCompressible to use copyToReg so we can share that code instead of basically duplicating it. I'm still concerned that target independent code may fold an extract_subreg operation and get an incorrect register if we do start using X0_Pair. We may have to special case dummy_reg_pair_with_x0 in the encoder and printer to be safe.
1 parent df03f7e commit cd60ee9

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ bool RISCVExpandPseudo::expandRV32ZdinxStore(MachineBasicBlock &MBB,
323323
TRI->getSubReg(MBBI->getOperand(0).getReg(), RISCV::sub_gpr_even);
324324
Register Hi =
325325
TRI->getSubReg(MBBI->getOperand(0).getReg(), RISCV::sub_gpr_odd);
326+
if (Hi == RISCV::DUMMY_REG_PAIR_WITH_X0)
327+
Hi = RISCV::X0;
326328

327329
auto MIBLo = BuildMI(MBB, MBBI, DL, TII->get(RISCV::SW))
328330
.addReg(Lo, getKillRegState(MBBI->getOperand(0).isKill()))
@@ -370,6 +372,7 @@ bool RISCVExpandPseudo::expandRV32ZdinxLoad(MachineBasicBlock &MBB,
370372
TRI->getSubReg(MBBI->getOperand(0).getReg(), RISCV::sub_gpr_even);
371373
Register Hi =
372374
TRI->getSubReg(MBBI->getOperand(0).getReg(), RISCV::sub_gpr_odd);
375+
assert(Hi != RISCV::DUMMY_REG_PAIR_WITH_X0 && "Cannot write to X0_Pair");
373376

374377
MachineInstrBuilder MIBLo, MIBHi;
375378

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,16 +543,21 @@ void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
543543
}
544544

545545
if (RISCV::GPRPairRegClass.contains(DstReg, SrcReg)) {
546+
MCRegister EvenReg = TRI->getSubReg(SrcReg, RISCV::sub_gpr_even);
547+
MCRegister OddReg = TRI->getSubReg(SrcReg, RISCV::sub_gpr_odd);
548+
// We need to correct the odd register of X0_Pair.
549+
if (OddReg == RISCV::DUMMY_REG_PAIR_WITH_X0)
550+
OddReg == RISCV::X0;
551+
assert(DstReg != RISCV::X0_Pair && "Cannot write to X0_Pair");
552+
546553
// Emit an ADDI for both parts of GPRPair.
547554
BuildMI(MBB, MBBI, DL, get(RISCV::ADDI),
548555
TRI->getSubReg(DstReg, RISCV::sub_gpr_even))
549-
.addReg(TRI->getSubReg(SrcReg, RISCV::sub_gpr_even),
550-
getKillRegState(KillSrc))
556+
.addReg(EvenReg, getKillRegState(KillSrc))
551557
.addImm(0);
552558
BuildMI(MBB, MBBI, DL, get(RISCV::ADDI),
553559
TRI->getSubReg(DstReg, RISCV::sub_gpr_odd))
554-
.addReg(TRI->getSubReg(SrcReg, RISCV::sub_gpr_odd),
555-
getKillRegState(KillSrc))
560+
.addReg(OddReg, getKillRegState(KillSrc))
556561
.addImm(0);
557562
return;
558563
}

0 commit comments

Comments
 (0)