@@ -156,7 +156,7 @@ static std::optional<unsigned> getEEWForLoadStore(const MachineInstr &MI) {
156
156
}
157
157
}
158
158
159
- static bool isNonZeroLoadImmediate (MachineInstr &MI) {
159
+ static bool isNonZeroLoadImmediate (const MachineInstr &MI) {
160
160
return MI.getOpcode () == RISCV::ADDI &&
161
161
MI.getOperand (1 ).isReg () && MI.getOperand (2 ).isImm () &&
162
162
MI.getOperand (1 ).getReg () == RISCV::X0 &&
@@ -448,6 +448,8 @@ class VSETVLIInfo {
448
448
unsigned AVLImm;
449
449
};
450
450
451
+ const MachineInstr *AVLDefMI;
452
+
451
453
enum : uint8_t {
452
454
Uninitialized,
453
455
AVLIsReg,
@@ -464,7 +466,7 @@ class VSETVLIInfo {
464
466
465
467
public:
466
468
VSETVLIInfo ()
467
- : AVLImm(0 ), TailAgnostic(false ), MaskAgnostic(false ),
469
+ : AVLImm(0 ), AVLDefMI( nullptr ), TailAgnostic(false ), MaskAgnostic(false ),
468
470
SEWLMULRatioOnly (false ) {}
469
471
470
472
static VSETVLIInfo getUnknown () {
@@ -488,6 +490,8 @@ class VSETVLIInfo {
488
490
State = AVLIsImm;
489
491
}
490
492
493
+ void setAVLDefMI (const MachineInstr *DefMI) { AVLDefMI = DefMI; }
494
+
491
495
bool hasAVLImm () const { return State == AVLIsImm; }
492
496
bool hasAVLReg () const { return State == AVLIsReg; }
493
497
Register getAVLReg () const {
@@ -499,13 +503,16 @@ class VSETVLIInfo {
499
503
return AVLImm;
500
504
}
501
505
506
+ const MachineInstr *getAVLDefMI () const { return AVLDefMI; }
507
+
502
508
void setAVL (VSETVLIInfo Info) {
503
509
assert (Info.isValid ());
504
510
if (Info.isUnknown ())
505
511
setUnknown ();
506
- else if (Info.hasAVLReg ())
512
+ else if (Info.hasAVLReg ()) {
507
513
setAVLReg (Info.getAVLReg ());
508
- else {
514
+ setAVLDefMI (Info.getAVLDefMI ());
515
+ } else {
509
516
assert (Info.hasAVLImm ());
510
517
setAVLImm (Info.getAVLImm ());
511
518
}
@@ -522,8 +529,7 @@ class VSETVLIInfo {
522
529
if (hasAVLReg ()) {
523
530
if (getAVLReg () == RISCV::X0)
524
531
return true ;
525
- if (MachineInstr *MI = MRI.getVRegDef (getAVLReg ());
526
- MI && isNonZeroLoadImmediate (*MI))
532
+ if (getAVLDefMI () && isNonZeroLoadImmediate (*getAVLDefMI ()))
527
533
return true ;
528
534
return false ;
529
535
}
@@ -836,7 +842,8 @@ INITIALIZE_PASS(RISCVCoalesceVSETVLI, "riscv-coalesce-vsetvli",
836
842
837
843
// Return a VSETVLIInfo representing the changes made by this VSETVLI or
838
844
// VSETIVLI instruction.
839
- static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
845
+ static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI,
846
+ const MachineRegisterInfo &MRI) {
840
847
VSETVLIInfo NewInfo;
841
848
if (MI.getOpcode () == RISCV::PseudoVSETIVLI) {
842
849
NewInfo.setAVLImm (MI.getOperand (1 ).getImm ());
@@ -847,6 +854,8 @@ static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
847
854
assert ((AVLReg != RISCV::X0 || MI.getOperand (0 ).getReg () != RISCV::X0) &&
848
855
" Can't handle X0, X0 vsetvli yet" );
849
856
NewInfo.setAVLReg (AVLReg);
857
+ if (AVLReg.isVirtual ())
858
+ NewInfo.setAVLDefMI (MRI.getVRegDef (AVLReg));
850
859
}
851
860
NewInfo.setVTYPE (MI.getOperand (2 ).getImm ());
852
861
@@ -919,6 +928,8 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
919
928
InstrInfo.setAVLImm (Imm);
920
929
} else {
921
930
InstrInfo.setAVLReg (VLOp.getReg ());
931
+ if (VLOp.getReg ().isVirtual ())
932
+ InstrInfo.setAVLDefMI (MRI->getVRegDef (VLOp.getReg ()));
922
933
}
923
934
} else {
924
935
assert (isScalarExtractInstr (MI));
@@ -936,9 +947,10 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
936
947
// register AVLs to avoid extending live ranges without being sure we can
937
948
// kill the original source reg entirely.
938
949
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);
942
954
if (DefInstrInfo.hasSameVLMAX (InstrInfo) &&
943
955
(DefInstrInfo.hasAVLImm () || DefInstrInfo.getAVLReg () == RISCV::X0)) {
944
956
InstrInfo.setAVL (DefInstrInfo);
@@ -978,9 +990,9 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
978
990
// same, we can use the X0, X0 form.
979
991
if (Info.hasSameVLMAX (PrevInfo) && Info.hasAVLReg () &&
980
992
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 );
984
996
if (DefInfo.hasSameAVL (PrevInfo) && DefInfo.hasSameVLMAX (PrevInfo)) {
985
997
BuildMI (MBB, InsertPt, DL, TII->get (RISCV::PseudoVSETVLIX0))
986
998
.addReg (RISCV::X0, RegState::Define | RegState::Dead)
@@ -1100,9 +1112,9 @@ bool RISCVInsertVSETVLI::needVSETVLI(const MachineInstr &MI,
1100
1112
// VSETVLI here.
1101
1113
if (Require.hasAVLReg () && Require.getAVLReg ().isVirtual () &&
1102
1114
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 );
1106
1118
if (DefInfo.hasSameAVL (CurInfo) && DefInfo.hasSameVLMAX (CurInfo))
1107
1119
return false ;
1108
1120
}
@@ -1189,13 +1201,15 @@ void RISCVInsertVSETVLI::transferBefore(VSETVLIInfo &Info,
1189
1201
void RISCVInsertVSETVLI::transferAfter (VSETVLIInfo &Info,
1190
1202
const MachineInstr &MI) const {
1191
1203
if (isVectorConfigInstr (MI)) {
1192
- Info = getInfoForVSETVLI (MI);
1204
+ Info = getInfoForVSETVLI (MI, *MRI );
1193
1205
return ;
1194
1206
}
1195
1207
1196
1208
if (RISCV::isFaultFirstLoad (MI)) {
1197
1209
// Update AVL to vl-output of the fault first load.
1198
1210
Info.setAVLReg (MI.getOperand (1 ).getReg ());
1211
+ if (MI.getOperand (1 ).getReg ().isVirtual ())
1212
+ Info.setAVLDefMI (MRI->getVRegDef (MI.getOperand (1 ).getReg ()));
1199
1213
return ;
1200
1214
}
1201
1215
@@ -1315,7 +1329,7 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo &Require,
1315
1329
1316
1330
// We found a VSET(I)VLI make sure it matches the output of the
1317
1331
// predecessor block.
1318
- VSETVLIInfo DefInfo = getInfoForVSETVLI (*DefMI);
1332
+ VSETVLIInfo DefInfo = getInfoForVSETVLI (*DefMI, *MRI );
1319
1333
if (!DefInfo.hasSameAVL (PBBInfo.Exit ) ||
1320
1334
!DefInfo.hasSameVTYPE (PBBInfo.Exit ))
1321
1335
return true ;
@@ -1464,7 +1478,7 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
1464
1478
// we need to prove the value is available at the point we're going
1465
1479
// to insert the vsetvli at.
1466
1480
if (AvailableInfo.hasAVLReg () && RISCV::X0 != AvailableInfo.getAVLReg ()) {
1467
- MachineInstr *AVLDefMI = MRI-> getVRegDef ( AvailableInfo.getAVLReg () );
1481
+ const MachineInstr *AVLDefMI = AvailableInfo.getAVLDefMI ( );
1468
1482
if (!AVLDefMI)
1469
1483
return ;
1470
1484
// This is an inline dominance check which covers the case of
@@ -1550,8 +1564,8 @@ static bool canMutatePriorConfig(const MachineInstr &PrevMI,
1550
1564
if (Used.VLZeroness ) {
1551
1565
if (isVLPreservingConfig (PrevMI))
1552
1566
return false ;
1553
- if (!getInfoForVSETVLI (PrevMI). hasEquallyZeroAVL ( getInfoForVSETVLI (MI),
1554
- MRI))
1567
+ if (!getInfoForVSETVLI (PrevMI, MRI)
1568
+ . hasEquallyZeroAVL ( getInfoForVSETVLI (MI, MRI), MRI))
1555
1569
return false ;
1556
1570
}
1557
1571
0 commit comments