Skip to content

Commit

Permalink
[RISCV] Reverse iteration/deletion structure in vsetvli coalescing [N…
Browse files Browse the repository at this point in the history
…FC] (#98936)

The code previously deferred deleting the vsetvli to avoid invalidating
iterators, but eagerly deleted any ADDIs feeding the AVL register
operand. This was safe because the iterator was known to point to a
non-ADDI instruction (the vsetvli which was the previous user.) This
change switches to using an early_inc_range so that we can eagerly
delete the vsetvlis, but have to track ADDIs for later deletion.

This is purely stylistic, but IMO makes the code easier to follow. It
will also simplify a future change to support recursive deletion of
trivially dead instructions (i.e. LUI/ADDI pairs.)
  • Loading branch information
preames authored Jul 23, 2024
1 parent d1e28e2 commit fd58e50
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,24 +1645,23 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
Used.demandVTYPE();
SmallVector<MachineInstr*> ToDelete;

// Update LIS and cleanup dead AVLs given a value which has
// has had one use (as an AVL) removed.
auto afterDroppedAVLUse = [&](Register OldVLReg) {
auto dropAVLUse = [&](MachineOperand &MO) {
if (!MO.isReg() || !MO.getReg().isVirtual())
return;
Register OldVLReg = MO.getReg();
MO.setReg(RISCV::NoRegister);

if (LIS)
LIS->shrinkToUses(&LIS->getInterval(OldVLReg));

MachineInstr *VLOpDef = MRI->getUniqueVRegDef(OldVLReg);
if (VLOpDef && TII->isAddImmediate(*VLOpDef, OldVLReg) &&
MRI->use_nodbg_empty(OldVLReg)) {
if (LIS) {
LIS->removeInterval(OldVLReg);
LIS->RemoveMachineInstrFromMaps(*VLOpDef);
}
VLOpDef->eraseFromParent();
}
MRI->use_nodbg_empty(OldVLReg))
ToDelete.push_back(VLOpDef);
};

for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) {
for (MachineInstr &MI :
make_early_inc_range(make_range(MBB.rbegin(), MBB.rend()))) {

if (!isVectorConfigInstr(MI)) {
Used.doUnion(getDemanded(MI, ST));
Expand All @@ -1678,7 +1677,11 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {

if (NextMI) {
if (!Used.usedVL() && !Used.usedVTYPE()) {
ToDelete.push_back(&MI);
dropAVLUse(MI.getOperand(1));
if (LIS)
LIS->RemoveMachineInstrFromMaps(MI);
MI.eraseFromParent();
NumCoalescedVSETVL++;
// Leave NextMI unchanged
continue;
}
Expand Down Expand Up @@ -1707,37 +1710,37 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
LIS->shrinkToUses(&DefLI);
}

Register OldVLReg;
if (MI.getOperand(1).isReg())
OldVLReg = MI.getOperand(1).getReg();
dropAVLUse(MI.getOperand(1));
if (NextMI->getOperand(1).isImm())
MI.getOperand(1).ChangeToImmediate(NextMI->getOperand(1).getImm());
else
MI.getOperand(1).ChangeToRegister(NextMI->getOperand(1).getReg(), false);
if (OldVLReg && OldVLReg.isVirtual())
afterDroppedAVLUse(OldVLReg);
MI.getOperand(1).ChangeToRegister(NextMI->getOperand(1).getReg(),
false);

MI.setDesc(NextMI->getDesc());
}
MI.getOperand(2).setImm(NextMI->getOperand(2).getImm());
ToDelete.push_back(NextMI);

dropAVLUse(NextMI->getOperand(1));
if (LIS)
LIS->RemoveMachineInstrFromMaps(*NextMI);
NextMI->eraseFromParent();
NumCoalescedVSETVL++;
// fallthrough
}
}
NextMI = &MI;
Used = getDemanded(MI, ST);
}

NumCoalescedVSETVL += ToDelete.size();
// Loop over the dead AVL values, and delete them now. This has
// to be outside the above loop to avoid invalidating iterators.
for (auto *MI : ToDelete) {
if (LIS)
if (LIS) {
LIS->removeInterval(MI->getOperand(0).getReg());
LIS->RemoveMachineInstrFromMaps(*MI);
Register OldAVLReg;
if (MI->getOperand(1).isReg())
OldAVLReg = MI->getOperand(1).getReg();
}
MI->eraseFromParent();
if (OldAVLReg && OldAVLReg.isVirtual())
afterDroppedAVLUse(OldAVLReg);
}
}

Expand Down

0 comments on commit fd58e50

Please sign in to comment.