Skip to content

Commit 36239f9

Browse files
authored
[RISCV] Move AVL coalescing logic upwards into computeInfoForInstr. NFC (#73909)
There is an optimisation in transferBefore where if a VSETVLIInfo uses the AVL of a defining vsetvli, it uses that vsetvli's AVL provided VLMAX is the same. This patch moves it out of transferBefore and up into computeInfoForInstr to show how it isn't affected by the other optimisations in transferBefore, and to simplify the control flow by removing an early return. This should make #72352 easier to reason about.
1 parent 3bd5172 commit 36239f9

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,25 @@ char RISCVInsertVSETVLI::ID = 0;
781781
INITIALIZE_PASS(RISCVInsertVSETVLI, DEBUG_TYPE, RISCV_INSERT_VSETVLI_NAME,
782782
false, false)
783783

784+
// Return a VSETVLIInfo representing the changes made by this VSETVLI or
785+
// VSETIVLI instruction.
786+
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
787+
VSETVLIInfo NewInfo;
788+
if (MI.getOpcode() == RISCV::PseudoVSETIVLI) {
789+
NewInfo.setAVLImm(MI.getOperand(1).getImm());
790+
} else {
791+
assert(MI.getOpcode() == RISCV::PseudoVSETVLI ||
792+
MI.getOpcode() == RISCV::PseudoVSETVLIX0);
793+
Register AVLReg = MI.getOperand(1).getReg();
794+
assert((AVLReg != RISCV::X0 || MI.getOperand(0).getReg() != RISCV::X0) &&
795+
"Can't handle X0, X0 vsetvli yet");
796+
NewInfo.setAVLReg(AVLReg);
797+
}
798+
NewInfo.setVTYPE(MI.getOperand(2).getImm());
799+
800+
return NewInfo;
801+
}
802+
784803
static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
785804
const MachineRegisterInfo *MRI) {
786805
VSETVLIInfo InstrInfo;
@@ -841,6 +860,21 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
841860
#endif
842861
InstrInfo.setVTYPE(VLMul, SEW, TailAgnostic, MaskAgnostic);
843862

863+
// If AVL is defined by a vsetvli with the same VLMAX, we can replace the
864+
// AVL operand with the AVL of the defining vsetvli. We avoid general
865+
// register AVLs to avoid extending live ranges without being sure we can
866+
// kill the original source reg entirely.
867+
if (InstrInfo.hasAVLReg() && InstrInfo.getAVLReg().isVirtual()) {
868+
MachineInstr *DefMI = MRI->getVRegDef(InstrInfo.getAVLReg());
869+
if (DefMI && isVectorConfigInstr(*DefMI)) {
870+
VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI);
871+
if (DefInstrInfo.hasSameVLMAX(InstrInfo) &&
872+
(DefInstrInfo.hasAVLImm() || DefInstrInfo.getAVLReg() == RISCV::X0)) {
873+
InstrInfo.setAVL(DefInstrInfo);
874+
}
875+
}
876+
}
877+
844878
return InstrInfo;
845879
}
846880

@@ -851,25 +885,6 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB, MachineInstr &MI,
851885
insertVSETVLI(MBB, MachineBasicBlock::iterator(&MI), DL, Info, PrevInfo);
852886
}
853887

854-
// Return a VSETVLIInfo representing the changes made by this VSETVLI or
855-
// VSETIVLI instruction.
856-
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
857-
VSETVLIInfo NewInfo;
858-
if (MI.getOpcode() == RISCV::PseudoVSETIVLI) {
859-
NewInfo.setAVLImm(MI.getOperand(1).getImm());
860-
} else {
861-
assert(MI.getOpcode() == RISCV::PseudoVSETVLI ||
862-
MI.getOpcode() == RISCV::PseudoVSETVLIX0);
863-
Register AVLReg = MI.getOperand(1).getReg();
864-
assert((AVLReg != RISCV::X0 || MI.getOperand(0).getReg() != RISCV::X0) &&
865-
"Can't handle X0, X0 vsetvli yet");
866-
NewInfo.setAVLReg(AVLReg);
867-
}
868-
NewInfo.setVTYPE(MI.getOperand(2).getImm());
869-
870-
return NewInfo;
871-
}
872-
873888
void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
874889
MachineBasicBlock::iterator InsertPt, DebugLoc DL,
875890
const VSETVLIInfo &Info, const VSETVLIInfo &PrevInfo) {
@@ -1065,27 +1080,8 @@ void RISCVInsertVSETVLI::transferBefore(VSETVLIInfo &Info,
10651080
// to prevent extending live range of an avl register operand.
10661081
// TODO: We can probably relax this for immediates.
10671082
if (Demanded.VLZeroness && !Demanded.VLAny && PrevInfo.isValid() &&
1068-
PrevInfo.hasEquallyZeroAVL(Info, *MRI) && Info.hasSameVLMAX(PrevInfo)) {
1083+
PrevInfo.hasEquallyZeroAVL(Info, *MRI) && Info.hasSameVLMAX(PrevInfo))
10691084
Info.setAVL(PrevInfo);
1070-
return;
1071-
}
1072-
1073-
// If AVL is defined by a vsetvli with the same VLMAX, we can
1074-
// replace the AVL operand with the AVL of the defining vsetvli.
1075-
// We avoid general register AVLs to avoid extending live ranges
1076-
// without being sure we can kill the original source reg entirely.
1077-
if (!Info.hasAVLReg() || !Info.getAVLReg().isVirtual())
1078-
return;
1079-
MachineInstr *DefMI = MRI->getVRegDef(Info.getAVLReg());
1080-
if (!DefMI || !isVectorConfigInstr(*DefMI))
1081-
return;
1082-
1083-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1084-
if (DefInfo.hasSameVLMAX(Info) &&
1085-
(DefInfo.hasAVLImm() || DefInfo.getAVLReg() == RISCV::X0)) {
1086-
Info.setAVL(DefInfo);
1087-
return;
1088-
}
10891085
}
10901086

10911087
// Given a state with which we evaluated MI (see transferBefore above for why

0 commit comments

Comments
 (0)