Skip to content

Commit 0e2958a

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 47682e4 commit 0e2958a

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp

Lines changed: 29 additions & 18 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 &&
@@ -459,6 +459,8 @@ class VSETVLIInfo {
459459
unsigned AVLImm;
460460
};
461461

462+
const MachineInstr *AVLDefMI;
463+
462464
enum : uint8_t {
463465
Uninitialized,
464466
AVLIsReg,
@@ -477,7 +479,7 @@ class VSETVLIInfo {
477479

478480
public:
479481
VSETVLIInfo()
480-
: AVLImm(0), TailAgnostic(false), MaskAgnostic(false),
482+
: AVLImm(0), AVLDefMI(nullptr), TailAgnostic(false), MaskAgnostic(false),
481483
SEWLMULRatioOnly(false) {}
482484

483485
static VSETVLIInfo getUnknown() {
@@ -504,6 +506,7 @@ class VSETVLIInfo {
504506
void setAVLVLMAX() { State = AVLIsVLMAX; }
505507

506508
void setAVLIgnored() { State = AVLIsIgnored; }
509+
void setAVLDefMI(const MachineInstr *DefMI) { AVLDefMI = DefMI; }
507510

508511
bool hasAVLImm() const { return State == AVLIsImm; }
509512
bool hasAVLReg() const { return State == AVLIsReg; }
@@ -518,12 +521,16 @@ class VSETVLIInfo {
518521
return AVLImm;
519522
}
520523

524+
const MachineInstr *getAVLDefMI() const { return AVLDefMI; }
525+
521526
void setAVL(VSETVLIInfo Info) {
522527
assert(Info.isValid());
523528
if (Info.isUnknown())
524529
setUnknown();
525-
else if (Info.hasAVLReg())
530+
else if (Info.hasAVLReg()) {
526531
setAVLReg(Info.getAVLReg());
532+
setAVLDefMI(Info.getAVLDefMI());
533+
}
527534
else if (Info.hasAVLVLMAX())
528535
setAVLVLMAX();
529536
else if (Info.hasAVLIgnored())
@@ -543,7 +550,7 @@ class VSETVLIInfo {
543550
if (hasAVLImm())
544551
return getAVLImm() > 0;
545552
if (hasAVLReg()) {
546-
MachineInstr *MI = MRI.getUniqueVRegDef(getAVLReg());
553+
const MachineInstr *MI = getAVLDefMI();
547554
assert(MI);
548555
return isNonZeroLoadImmediate(*MI);
549556
}
@@ -870,7 +877,8 @@ INITIALIZE_PASS(RISCVCoalesceVSETVLI, "riscv-coalesce-vsetvli",
870877

871878
// Return a VSETVLIInfo representing the changes made by this VSETVLI or
872879
// VSETIVLI instruction.
873-
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
880+
static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI,
881+
const MachineRegisterInfo &MRI) {
874882
VSETVLIInfo NewInfo;
875883
if (MI.getOpcode() == RISCV::PseudoVSETIVLI) {
876884
NewInfo.setAVLImm(MI.getOperand(1).getImm());
@@ -883,7 +891,7 @@ static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
883891
if (AVLReg == RISCV::X0)
884892
NewInfo.setAVLVLMAX();
885893
else
886-
NewInfo.setAVLReg(AVLReg);
894+
NewInfo.setAVLDefMI(MRI.getVRegDef(AVLReg));
887895
}
888896
NewInfo.setVTYPE(MI.getOperand(2).getImm());
889897

@@ -956,6 +964,8 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
956964
InstrInfo.setAVLImm(Imm);
957965
} else {
958966
InstrInfo.setAVLReg(VLOp.getReg());
967+
if (VLOp.getReg().isVirtual())
968+
InstrInfo.setAVLDefMI(MRI->getVRegDef(VLOp.getReg()));
959969
}
960970
} else {
961971
assert(isScalarExtractInstr(MI));
@@ -976,10 +986,9 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
976986
// register AVLs to avoid extending live ranges without being sure we can
977987
// kill the original source reg entirely.
978988
if (InstrInfo.hasAVLReg()) {
979-
MachineInstr *DefMI = MRI->getUniqueVRegDef(InstrInfo.getAVLReg());
980-
assert(DefMI);
989+
const MachineInstr *DefMI = InstrInfo.getAVLDefMI();
981990
if (isVectorConfigInstr(*DefMI)) {
982-
VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI);
991+
VSETVLIInfo DefInstrInfo = getInfoForVSETVLI(*DefMI, *MRI);
983992
if (DefInstrInfo.hasSameVLMAX(InstrInfo) &&
984993
(DefInstrInfo.hasAVLImm() || DefInstrInfo.hasAVLVLMAX()))
985994
InstrInfo.setAVL(DefInstrInfo);
@@ -1017,10 +1026,10 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
10171026
// it has the same VLMAX we want and the last VL/VTYPE we observed is the
10181027
// same, we can use the X0, X0 form.
10191028
if (Info.hasSameVLMAX(PrevInfo) && Info.hasAVLReg()) {
1020-
MachineInstr *DefMI = MRI->getUniqueVRegDef(Info.getAVLReg());
1029+
const MachineInstr *DefMI = Info.getAVLDefMI();
10211030
assert(DefMI);
10221031
if (isVectorConfigInstr(*DefMI)) {
1023-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1032+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI, *MRI);
10241033
if (DefInfo.hasSameAVL(PrevInfo) && DefInfo.hasSameVLMAX(PrevInfo)) {
10251034
BuildMI(MBB, InsertPt, DL, TII->get(RISCV::PseudoVSETVLIX0))
10261035
.addReg(RISCV::X0, RegState::Define | RegState::Dead)
@@ -1138,8 +1147,8 @@ bool RISCVInsertVSETVLI::needVSETVLI(const MachineInstr &MI,
11381147
if (Require.hasAVLReg() && CurInfo.hasCompatibleVTYPE(Used, Require)) {
11391148
MachineInstr *DefMI = MRI->getUniqueVRegDef(Require.getAVLReg());
11401149
assert(DefMI);
1141-
if (isVectorConfigInstr(*DefMI)) {
1142-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1150+
if (Require.getAVLDefMI() && isVectorConfigInstr(*Require.getAVLDefMI())) {
1151+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*Require.getAVLDefMI(), *MRI);
11431152
if (DefInfo.hasSameAVL(CurInfo) && DefInfo.hasSameVLMAX(CurInfo))
11441153
return false;
11451154
}
@@ -1225,13 +1234,15 @@ void RISCVInsertVSETVLI::transferBefore(VSETVLIInfo &Info,
12251234
void RISCVInsertVSETVLI::transferAfter(VSETVLIInfo &Info,
12261235
const MachineInstr &MI) const {
12271236
if (isVectorConfigInstr(MI)) {
1228-
Info = getInfoForVSETVLI(MI);
1237+
Info = getInfoForVSETVLI(MI, *MRI);
12291238
return;
12301239
}
12311240

12321241
if (RISCV::isFaultFirstLoad(MI)) {
12331242
// Update AVL to vl-output of the fault first load.
12341243
Info.setAVLReg(MI.getOperand(1).getReg());
1244+
if (MI.getOperand(1).getReg().isVirtual())
1245+
Info.setAVLDefMI(MRI->getVRegDef(MI.getOperand(1).getReg()));
12351246
return;
12361247
}
12371248

@@ -1346,7 +1357,7 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo &Require,
13461357

13471358
// We found a VSET(I)VLI make sure it matches the output of the
13481359
// predecessor block.
1349-
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI);
1360+
VSETVLIInfo DefInfo = getInfoForVSETVLI(*DefMI, *MRI);
13501361
if (DefInfo != PBBExit)
13511362
return true;
13521363

@@ -1500,7 +1511,7 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
15001511
// we need to prove the value is available at the point we're going
15011512
// to insert the vsetvli at.
15021513
if (AvailableInfo.hasAVLReg()) {
1503-
MachineInstr *AVLDefMI = MRI->getUniqueVRegDef(AvailableInfo.getAVLReg());
1514+
const MachineInstr *AVLDefMI = AvailableInfo.getAVLDefMI();
15041515
assert(AVLDefMI);
15051516
// This is an inline dominance check which covers the case of
15061517
// UnavailablePred being the preheader of a loop.
@@ -1580,8 +1591,8 @@ static bool canMutatePriorConfig(const MachineInstr &PrevMI,
15801591
if (Used.VLZeroness) {
15811592
if (isVLPreservingConfig(PrevMI))
15821593
return false;
1583-
if (!getInfoForVSETVLI(PrevMI).hasEquallyZeroAVL(getInfoForVSETVLI(MI),
1584-
MRI))
1594+
if (!getInfoForVSETVLI(PrevMI, MRI)
1595+
.hasEquallyZeroAVL(getInfoForVSETVLI(MI, MRI), MRI))
15851596
return false;
15861597
}
15871598

0 commit comments

Comments
 (0)