Skip to content

Commit cee023a

Browse files
committed
[RISCV] Keep AVLReg define instr inside VSETVLInfo
Currently, the vsetvli pass track the define instruction through MRI->getVRegDef due to the SSA form. This patch keeps the AVLReg DefMI within VSETVLInfo during construction. And replace MRI->getVRegDef(AVLReg) with getAVLRegDefMI(). This information is useful when vsetvli pass live in post-ra situation. The testcases don't change because the VReg always has a unique def in SSA.
1 parent 472b612 commit cee023a

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static std::optional<unsigned> getEEWForLoadStore(const MachineInstr &MI) {
153153
}
154154
}
155155

156-
static bool isNonZeroLoadImmediate(MachineInstr &MI) {
156+
static bool isNonZeroLoadImmediate(const MachineInstr &MI) {
157157
return MI.getOpcode() == RISCV::ADDI &&
158158
MI.getOperand(1).isReg() && MI.getOperand(2).isImm() &&
159159
MI.getOperand(1).getReg() == RISCV::X0 &&
@@ -438,6 +438,8 @@ class VSETVLIInfo {
438438
unsigned AVLImm;
439439
};
440440

441+
const MachineInstr *AVLDefMI;
442+
441443
enum : uint8_t {
442444
Uninitialized,
443445
AVLIsReg,
@@ -454,7 +456,7 @@ class VSETVLIInfo {
454456

455457
public:
456458
VSETVLIInfo()
457-
: AVLImm(0), TailAgnostic(false), MaskAgnostic(false),
459+
: AVLImm(0), AVLDefMI(nullptr), TailAgnostic(false), MaskAgnostic(false),
458460
SEWLMULRatioOnly(false) {}
459461

460462
static VSETVLIInfo getUnknown() {
@@ -478,6 +480,8 @@ class VSETVLIInfo {
478480
State = AVLIsImm;
479481
}
480482

483+
void setAVLDefMI(const MachineInstr *DefMI) { AVLDefMI = DefMI; }
484+
481485
bool hasAVLImm() const { return State == AVLIsImm; }
482486
bool hasAVLReg() const { return State == AVLIsReg; }
483487
Register getAVLReg() const {
@@ -489,13 +493,16 @@ class VSETVLIInfo {
489493
return AVLImm;
490494
}
491495

496+
const MachineInstr *getAVLDefMI() const { return AVLDefMI; }
497+
492498
void setAVL(VSETVLIInfo Info) {
493499
assert(Info.isValid());
494500
if (Info.isUnknown())
495501
setUnknown();
496-
else if (Info.hasAVLReg())
502+
else if (Info.hasAVLReg()) {
497503
setAVLReg(Info.getAVLReg());
498-
else {
504+
setAVLDefMI(Info.getAVLDefMI());
505+
} else {
499506
assert(Info.hasAVLImm());
500507
setAVLImm(Info.getAVLImm());
501508
}
@@ -512,8 +519,7 @@ class VSETVLIInfo {
512519
if (hasAVLReg()) {
513520
if (getAVLReg() == RISCV::X0)
514521
return true;
515-
if (MachineInstr *MI = MRI.getVRegDef(getAVLReg());
516-
MI && isNonZeroLoadImmediate(*MI))
522+
if (getAVLDefMI() && isNonZeroLoadImmediate(*getAVLDefMI()))
517523
return true;
518524
return false;
519525
}
@@ -792,7 +798,8 @@ INITIALIZE_PASS(RISCVInsertVSETVLI, DEBUG_TYPE, RISCV_INSERT_VSETVLI_NAME,
792798

793799
// Return a VSETVLIInfo representing the changes made by this VSETVLI or
794800
// VSETIVLI instruction.
795-
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
801+
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI,
802+
const MachineRegisterInfo &MRI) {
796803
VSETVLIInfo NewInfo;
797804
if (MI.getOpcode() == RISCV::PseudoVSETIVLI) {
798805
NewInfo.setAVLImm(MI.getOperand(1).getImm());
@@ -803,6 +810,8 @@ static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
803810
assert((AVLReg != RISCV::X0 || MI.getOperand(0).getReg() != RISCV::X0) &&
804811
"Can't handle X0, X0 vsetvli yet");
805812
NewInfo.setAVLReg(AVLReg);
813+
if (AVLReg.isVirtual())
814+
NewInfo.setAVLDefMI(MRI.getVRegDef(AVLReg));
806815
}
807816
NewInfo.setVTYPE(MI.getOperand(2).getImm());
808817

@@ -875,6 +884,8 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
875884
InstrInfo.setAVLImm(Imm);
876885
} else {
877886
InstrInfo.setAVLReg(VLOp.getReg());
887+
if (VLOp.getReg().isVirtual())
888+
InstrInfo.setAVLDefMI(MRI->getVRegDef(VLOp.getReg()));
878889
}
879890
} else {
880891
assert(isScalarExtractInstr(MI));
@@ -892,9 +903,10 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
892903
// register AVLs to avoid extending live ranges without being sure we can
893904
// kill the original source reg entirely.
894905
if (InstrInfo.hasAVLReg() && InstrInfo.getAVLReg().isVirtual()) {
895-
MachineInstr *DefMI = MRI->getVRegDef(InstrInfo.getAVLReg());
896-
if (DefMI && isVectorConfigInstr(*DefMI)) {
897-
VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI);
906+
if (InstrInfo.getAVLDefMI() &&
907+
isVectorConfigInstr(*InstrInfo.getAVLDefMI())) {
908+
VSETVLIInfo DefInstrInfo =
909+
getInfoForVSETVLI(*InstrInfo.getAVLDefMI(), *MRI);
898910
if (DefInstrInfo.hasSameVLMAX(InstrInfo) &&
899911
(DefInstrInfo.hasAVLImm() || DefInstrInfo.getAVLReg() == RISCV::X0)) {
900912
InstrInfo.setAVL(DefInstrInfo);
@@ -934,9 +946,9 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
934946
// same, we can use the X0, X0 form.
935947
if (Info.hasSameVLMAX(PrevInfo) && Info.hasAVLReg() &&
936948
Info.getAVLReg().isVirtual()) {
937-
if (MachineInstr *DefMI = MRI->getVRegDef(Info.getAVLReg())) {
938-
if (isVectorConfigInstr(*DefMI)) {
939-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
949+
if (Info.getAVLDefMI()) {
950+
if (isVectorConfigInstr(*Info.getAVLDefMI())) {
951+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*Info.getAVLDefMI(), *MRI);
940952
if (DefInfo.hasSameAVL(PrevInfo) && DefInfo.hasSameVLMAX(PrevInfo)) {
941953
BuildMI(MBB, InsertPt, DL, TII->get(RISCV::PseudoVSETVLIX0))
942954
.addReg(RISCV::X0, RegState::Define | RegState::Dead)
@@ -1056,9 +1068,9 @@ bool RISCVInsertVSETVLI::needVSETVLI(const MachineInstr &MI,
10561068
// VSETVLI here.
10571069
if (Require.hasAVLReg() && Require.getAVLReg().isVirtual() &&
10581070
CurInfo.hasCompatibleVTYPE(Used, Require)) {
1059-
if (MachineInstr *DefMI = MRI->getVRegDef(Require.getAVLReg())) {
1060-
if (isVectorConfigInstr(*DefMI)) {
1061-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1071+
if (Require.getAVLDefMI()) {
1072+
if (isVectorConfigInstr(*Require.getAVLDefMI())) {
1073+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*Require.getAVLDefMI(), *MRI);
10621074
if (DefInfo.hasSameAVL(CurInfo) && DefInfo.hasSameVLMAX(CurInfo))
10631075
return false;
10641076
}
@@ -1145,13 +1157,15 @@ void RISCVInsertVSETVLI::transferBefore(VSETVLIInfo &Info,
11451157
void RISCVInsertVSETVLI::transferAfter(VSETVLIInfo &Info,
11461158
const MachineInstr &MI) const {
11471159
if (isVectorConfigInstr(MI)) {
1148-
Info = getInfoForVSETVLI(MI);
1160+
Info = getInfoForVSETVLI(MI, *MRI);
11491161
return;
11501162
}
11511163

11521164
if (RISCV::isFaultFirstLoad(MI)) {
11531165
// Update AVL to vl-output of the fault first load.
11541166
Info.setAVLReg(MI.getOperand(1).getReg());
1167+
if (MI.getOperand(1).getReg().isVirtual())
1168+
Info.setAVLDefMI(MRI->getVRegDef(MI.getOperand(1).getReg()));
11551169
return;
11561170
}
11571171

@@ -1270,7 +1284,7 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo &Require,
12701284

12711285
// We found a VSET(I)VLI make sure it matches the output of the
12721286
// predecessor block.
1273-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1287+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI, *MRI);
12741288
if (!DefInfo.hasSameAVL(PBBInfo.Exit) ||
12751289
!DefInfo.hasSameVTYPE(PBBInfo.Exit))
12761290
return true;
@@ -1418,7 +1432,7 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
14181432
// we need to prove the value is available at the point we're going
14191433
// to insert the vsetvli at.
14201434
if (AvailableInfo.hasAVLReg() && RISCV::X0 != AvailableInfo.getAVLReg()) {
1421-
MachineInstr *AVLDefMI = MRI->getVRegDef(AvailableInfo.getAVLReg());
1435+
const MachineInstr *AVLDefMI = AvailableInfo.getAVLDefMI();
14221436
if (!AVLDefMI)
14231437
return;
14241438
// This is an inline dominance check which covers the case of
@@ -1504,8 +1518,8 @@ static bool canMutatePriorConfig(const MachineInstr &PrevMI,
15041518
if (Used.VLZeroness) {
15051519
if (isVLPreservingConfig(PrevMI))
15061520
return false;
1507-
if (!getInfoForVSETVLI(PrevMI).hasEquallyZeroAVL(getInfoForVSETVLI(MI),
1508-
MRI))
1521+
if (!getInfoForVSETVLI(PrevMI, MRI)
1522+
.hasEquallyZeroAVL(getInfoForVSETVLI(MI, MRI), MRI))
15091523
return false;
15101524
}
15111525

0 commit comments

Comments
 (0)