Skip to content

Commit b712c80

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 50082d6 commit b712c80

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
@@ -156,7 +156,7 @@ static std::optional<unsigned> getEEWForLoadStore(const MachineInstr &MI) {
156156
}
157157
}
158158

159-
static bool isNonZeroLoadImmediate(MachineInstr &MI) {
159+
static bool isNonZeroLoadImmediate(const MachineInstr &MI) {
160160
return MI.getOpcode() == RISCV::ADDI &&
161161
MI.getOperand(1).isReg() && MI.getOperand(2).isImm() &&
162162
MI.getOperand(1).getReg() == RISCV::X0 &&
@@ -448,6 +448,8 @@ class VSETVLIInfo {
448448
unsigned AVLImm;
449449
};
450450

451+
const MachineInstr *AVLDefMI;
452+
451453
enum : uint8_t {
452454
Uninitialized,
453455
AVLIsReg,
@@ -464,7 +466,7 @@ class VSETVLIInfo {
464466

465467
public:
466468
VSETVLIInfo()
467-
: AVLImm(0), TailAgnostic(false), MaskAgnostic(false),
469+
: AVLImm(0), AVLDefMI(nullptr), TailAgnostic(false), MaskAgnostic(false),
468470
SEWLMULRatioOnly(false) {}
469471

470472
static VSETVLIInfo getUnknown() {
@@ -488,6 +490,8 @@ class VSETVLIInfo {
488490
State = AVLIsImm;
489491
}
490492

493+
void setAVLDefMI(const MachineInstr *DefMI) { AVLDefMI = DefMI; }
494+
491495
bool hasAVLImm() const { return State == AVLIsImm; }
492496
bool hasAVLReg() const { return State == AVLIsReg; }
493497
Register getAVLReg() const {
@@ -499,13 +503,16 @@ class VSETVLIInfo {
499503
return AVLImm;
500504
}
501505

506+
const MachineInstr *getAVLDefMI() const { return AVLDefMI; }
507+
502508
void setAVL(VSETVLIInfo Info) {
503509
assert(Info.isValid());
504510
if (Info.isUnknown())
505511
setUnknown();
506-
else if (Info.hasAVLReg())
512+
else if (Info.hasAVLReg()) {
507513
setAVLReg(Info.getAVLReg());
508-
else {
514+
setAVLDefMI(Info.getAVLDefMI());
515+
} else {
509516
assert(Info.hasAVLImm());
510517
setAVLImm(Info.getAVLImm());
511518
}
@@ -522,8 +529,7 @@ class VSETVLIInfo {
522529
if (hasAVLReg()) {
523530
if (getAVLReg() == RISCV::X0)
524531
return true;
525-
if (MachineInstr *MI = MRI.getVRegDef(getAVLReg());
526-
MI && isNonZeroLoadImmediate(*MI))
532+
if (getAVLDefMI() && isNonZeroLoadImmediate(*getAVLDefMI()))
527533
return true;
528534
return false;
529535
}
@@ -836,7 +842,8 @@ INITIALIZE_PASS(RISCVCoalesceVSETVLI, "riscv-coalesce-vsetvli",
836842

837843
// Return a VSETVLIInfo representing the changes made by this VSETVLI or
838844
// VSETIVLI instruction.
839-
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
845+
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI,
846+
const MachineRegisterInfo &MRI) {
840847
VSETVLIInfo NewInfo;
841848
if (MI.getOpcode() == RISCV::PseudoVSETIVLI) {
842849
NewInfo.setAVLImm(MI.getOperand(1).getImm());
@@ -847,6 +854,8 @@ static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
847854
assert((AVLReg != RISCV::X0 || MI.getOperand(0).getReg() != RISCV::X0) &&
848855
"Can't handle X0, X0 vsetvli yet");
849856
NewInfo.setAVLReg(AVLReg);
857+
if (AVLReg.isVirtual())
858+
NewInfo.setAVLDefMI(MRI.getVRegDef(AVLReg));
850859
}
851860
NewInfo.setVTYPE(MI.getOperand(2).getImm());
852861

@@ -919,6 +928,8 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
919928
InstrInfo.setAVLImm(Imm);
920929
} else {
921930
InstrInfo.setAVLReg(VLOp.getReg());
931+
if (VLOp.getReg().isVirtual())
932+
InstrInfo.setAVLDefMI(MRI->getVRegDef(VLOp.getReg()));
922933
}
923934
} else {
924935
assert(isScalarExtractInstr(MI));
@@ -936,9 +947,10 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
936947
// register AVLs to avoid extending live ranges without being sure we can
937948
// kill the original source reg entirely.
938949
if (InstrInfo.hasAVLReg() && InstrInfo.getAVLReg().isVirtual()) {
939-
MachineInstr *DefMI = MRI->getVRegDef(InstrInfo.getAVLReg());
940-
if (DefMI && isVectorConfigInstr(*DefMI)) {
941-
VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI);
950+
if (InstrInfo.getAVLDefMI() &&
951+
isVectorConfigInstr(*InstrInfo.getAVLDefMI())) {
952+
VSETVLIInfo DefInstrInfo =
953+
getInfoForVSETVLI(*InstrInfo.getAVLDefMI(), *MRI);
942954
if (DefInstrInfo.hasSameVLMAX(InstrInfo) &&
943955
(DefInstrInfo.hasAVLImm() || DefInstrInfo.getAVLReg() == RISCV::X0)) {
944956
InstrInfo.setAVL(DefInstrInfo);
@@ -978,9 +990,9 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
978990
// same, we can use the X0, X0 form.
979991
if (Info.hasSameVLMAX(PrevInfo) && Info.hasAVLReg() &&
980992
Info.getAVLReg().isVirtual()) {
981-
if (MachineInstr *DefMI = MRI->getVRegDef(Info.getAVLReg())) {
982-
if (isVectorConfigInstr(*DefMI)) {
983-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
993+
if (Info.getAVLDefMI()) {
994+
if (isVectorConfigInstr(*Info.getAVLDefMI())) {
995+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*Info.getAVLDefMI(), *MRI);
984996
if (DefInfo.hasSameAVL(PrevInfo) && DefInfo.hasSameVLMAX(PrevInfo)) {
985997
BuildMI(MBB, InsertPt, DL, TII->get(RISCV::PseudoVSETVLIX0))
986998
.addReg(RISCV::X0, RegState::Define | RegState::Dead)
@@ -1100,9 +1112,9 @@ bool RISCVInsertVSETVLI::needVSETVLI(const MachineInstr &MI,
11001112
// VSETVLI here.
11011113
if (Require.hasAVLReg() && Require.getAVLReg().isVirtual() &&
11021114
CurInfo.hasCompatibleVTYPE(Used, Require)) {
1103-
if (MachineInstr *DefMI = MRI->getVRegDef(Require.getAVLReg())) {
1104-
if (isVectorConfigInstr(*DefMI)) {
1105-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1115+
if (Require.getAVLDefMI()) {
1116+
if (isVectorConfigInstr(*Require.getAVLDefMI())) {
1117+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*Require.getAVLDefMI(), *MRI);
11061118
if (DefInfo.hasSameAVL(CurInfo) && DefInfo.hasSameVLMAX(CurInfo))
11071119
return false;
11081120
}
@@ -1189,13 +1201,15 @@ void RISCVInsertVSETVLI::transferBefore(VSETVLIInfo &Info,
11891201
void RISCVInsertVSETVLI::transferAfter(VSETVLIInfo &Info,
11901202
const MachineInstr &MI) const {
11911203
if (isVectorConfigInstr(MI)) {
1192-
Info = getInfoForVSETVLI(MI);
1204+
Info = getInfoForVSETVLI(MI, *MRI);
11931205
return;
11941206
}
11951207

11961208
if (RISCV::isFaultFirstLoad(MI)) {
11971209
// Update AVL to vl-output of the fault first load.
11981210
Info.setAVLReg(MI.getOperand(1).getReg());
1211+
if (MI.getOperand(1).getReg().isVirtual())
1212+
Info.setAVLDefMI(MRI->getVRegDef(MI.getOperand(1).getReg()));
11991213
return;
12001214
}
12011215

@@ -1315,7 +1329,7 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo &Require,
13151329

13161330
// We found a VSET(I)VLI make sure it matches the output of the
13171331
// predecessor block.
1318-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1332+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI, *MRI);
13191333
if (!DefInfo.hasSameAVL(PBBInfo.Exit) ||
13201334
!DefInfo.hasSameVTYPE(PBBInfo.Exit))
13211335
return true;
@@ -1464,7 +1478,7 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
14641478
// we need to prove the value is available at the point we're going
14651479
// to insert the vsetvli at.
14661480
if (AvailableInfo.hasAVLReg() && RISCV::X0 != AvailableInfo.getAVLReg()) {
1467-
MachineInstr *AVLDefMI = MRI->getVRegDef(AvailableInfo.getAVLReg());
1481+
const MachineInstr *AVLDefMI = AvailableInfo.getAVLDefMI();
14681482
if (!AVLDefMI)
14691483
return;
14701484
// This is an inline dominance check which covers the case of
@@ -1550,8 +1564,8 @@ static bool canMutatePriorConfig(const MachineInstr &PrevMI,
15501564
if (Used.VLZeroness) {
15511565
if (isVLPreservingConfig(PrevMI))
15521566
return false;
1553-
if (!getInfoForVSETVLI(PrevMI).hasEquallyZeroAVL(getInfoForVSETVLI(MI),
1554-
MRI))
1567+
if (!getInfoForVSETVLI(PrevMI, MRI)
1568+
.hasEquallyZeroAVL(getInfoForVSETVLI(MI, MRI), MRI))
15551569
return false;
15561570
}
15571571

0 commit comments

Comments
 (0)