Skip to content

Commit e74254a

Browse files
committed
[RISCV] Remove redundant vsetvli by repeating the coalasce phase
1 parent 7cf8a9d commit e74254a

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "RISCV.h"
2828
#include "RISCVSubtarget.h"
29+
#include "llvm/ADT/SetVector.h"
2930
#include "llvm/ADT/Statistic.h"
3031
#include "llvm/CodeGen/LiveDebugVariables.h"
3132
#include "llvm/CodeGen/LiveIntervals.h"
@@ -895,7 +896,8 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
895896

896897
bool canMutatePriorConfig(const MachineInstr &PrevMI, const MachineInstr &MI,
897898
const DemandedFields &Used) const;
898-
void coalesceVSETVLIs(MachineBasicBlock &MBB) const;
899+
void coalesceVSETVLIs(SetVector<MachineBasicBlock *> &Worklist,
900+
MachineBasicBlock &MBB) const;
899901

900902
VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) const;
901903
VSETVLIInfo computeInfoForInstr(const MachineInstr &MI) const;
@@ -1642,7 +1644,8 @@ bool RISCVInsertVSETVLI::canMutatePriorConfig(
16421644
return areCompatibleVTYPEs(PriorVType, VType, Used);
16431645
}
16441646

1645-
void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
1647+
void RISCVInsertVSETVLI::coalesceVSETVLIs(
1648+
SetVector<MachineBasicBlock *> &Worklist, MachineBasicBlock &MBB) const {
16461649
MachineInstr *NextMI = nullptr;
16471650
// We can have arbitrary code in successors, so VL and VTYPE
16481651
// must be considered demanded.
@@ -1661,9 +1664,18 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
16611664
LIS->shrinkToUses(&LIS->getInterval(OldVLReg));
16621665

16631666
MachineInstr *VLOpDef = MRI->getUniqueVRegDef(OldVLReg);
1664-
if (VLOpDef && TII->isAddImmediate(*VLOpDef, OldVLReg) &&
1665-
MRI->use_nodbg_empty(OldVLReg))
1666-
ToDelete.push_back(VLOpDef);
1667+
if (VLOpDef && MRI->use_nodbg_empty(OldVLReg)) {
1668+
if (TII->isAddImmediate(*VLOpDef, OldVLReg))
1669+
ToDelete.push_back(VLOpDef);
1670+
// If the destination register of a vset* instruction becomes dead because
1671+
// of this, there might be a chance to eliminate it. Put into the worklist
1672+
// so that we can revisit it.
1673+
// Note that since this is a virtual register, the definition instruction
1674+
// is always placed earlier in the program order. Thus, we avoid
1675+
// enqueuing blocks in cycle and therefore guarantee to terminate.
1676+
if (RISCVInstrInfo::isVectorConfigInstr(*VLOpDef))
1677+
Worklist.insert(VLOpDef->getParent());
1678+
}
16671679
};
16681680

16691681
for (MachineInstr &MI : make_early_inc_range(reverse(MBB))) {
@@ -1840,8 +1852,14 @@ bool RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
18401852
// any cross block analysis within the dataflow. We can't have both
18411853
// demanded fields based mutation and non-local analysis in the
18421854
// dataflow at the same time without introducing inconsistencies.
1843-
for (MachineBasicBlock &MBB : MF)
1844-
coalesceVSETVLIs(MBB);
1855+
using BBPtrIterator = pointer_iterator<MachineFunction::iterator>;
1856+
SetVector<MachineBasicBlock *> Worklist(BBPtrIterator(MF.begin()),
1857+
BBPtrIterator(MF.end()));
1858+
while (!Worklist.empty()) {
1859+
MachineBasicBlock *MBB = Worklist.front();
1860+
Worklist.erase(Worklist.begin());
1861+
coalesceVSETVLIs(Worklist, *MBB);
1862+
}
18451863

18461864
// Insert PseudoReadVL after VLEFF/VLSEGFF and replace it with the vl output
18471865
// of VLEFF/VLSEGFF.

llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-coalesce.mir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ body: |
3030
; CHECK-NEXT: bb.1:
3131
; CHECK-NEXT: successors: %bb.2(0x80000000)
3232
; CHECK-NEXT: {{ $}}
33-
; CHECK-NEXT: dead [[PseudoVSETVLI:%[0-9]+]]:gprnox0 = PseudoVSETVLI [[DEF]], 199 /* e8, mf2, ta, ma */, implicit-def $vl, implicit-def $vtype
3433
; CHECK-NEXT: dead [[PseudoVSETVLIX0_:%[0-9]+]]:gpr = PseudoVSETVLIX0 killed $x0, 209 /* e32, m2, ta, ma */, implicit-def $vl, implicit-def $vtype
3534
; CHECK-NEXT: renamable $v10m2 = PseudoVMV_V_I_M2 undef renamable $v10m2, 0, -1, 5 /* e32 */, 0 /* tu, mu */, implicit $vl, implicit $vtype
3635
; CHECK-NEXT: {{ $}}

0 commit comments

Comments
 (0)