@@ -52,7 +52,9 @@ class RISCVVLOptimizer : public MachineFunctionPass {
52
52
53
53
private:
54
54
MachineOperand getMinimumVLForUser (MachineOperand &UserOp);
55
- // / Computes the VL of \p MI that is actually used by its users.
55
+ // / Computes the minimum demanded VL of \p MI, i.e. the minimum VL that's used
56
+ // / by its users downstream.
57
+ // / Returns 0 if MI has no users.
56
58
MachineOperand computeDemandedVL (const MachineInstr &MI);
57
59
bool tryReduceVL (MachineInstr &MI);
58
60
bool isCandidate (const MachineInstr &MI) const ;
@@ -1208,13 +1210,10 @@ MachineOperand RISCVVLOptimizer::getMinimumVLForUser(MachineOperand &UserOp) {
1208
1210
// If we know the demanded VL of UserMI, then we can reduce the VL it
1209
1211
// requires.
1210
1212
if (DemandedVLs.contains (&UserMI)) {
1211
- // We can only shrink the demanded VL if the elementwise result doesn't
1212
- // depend on VL (i.e. not vredsum/viota etc.)
1213
- // Also conservatively restrict to supported instructions for now.
1214
- // TODO: Can we remove the isSupportedInstr check?
1213
+ // We can only shrink the VL used if the elementwise result doesn't depend
1214
+ // on VL (i.e. not vredsum/viota etc.)
1215
1215
if (!RISCVII::elementsDependOnVL (
1216
- TII->get (RISCV::getRVVMCOpcode (UserMI.getOpcode ())).TSFlags ) &&
1217
- isSupportedInstr (UserMI)) {
1216
+ TII->get (RISCV::getRVVMCOpcode (UserMI.getOpcode ())).TSFlags )) {
1218
1217
const MachineOperand &DemandedVL = DemandedVLs.at (&UserMI);
1219
1218
if (RISCV::isVLKnownLE (DemandedVL, VLOp))
1220
1219
return DemandedVL;
@@ -1244,13 +1243,14 @@ MachineOperand RISCVVLOptimizer::computeDemandedVL(const MachineInstr &MI) {
1244
1243
1245
1244
const MachineOperand &VLOp = getMinimumVLForUser (UserOp);
1246
1245
1247
- // Use the largest VL among all the users. If we cannot determine this
1248
- // statically, then we cannot optimize the VL.
1246
+ // The minimum demanded VL is the largest VL read amongst all the users. If
1247
+ // we cannot determine this statically, then we cannot optimize the VL.
1249
1248
if (RISCV::isVLKnownLE (DemandedVL, VLOp)) {
1250
1249
DemandedVL = VLOp;
1251
1250
LLVM_DEBUG (dbgs () << " Demanded VL is: " << VLOp << " \n " );
1252
1251
} else if (!RISCV::isVLKnownLE (VLOp, DemandedVL)) {
1253
- LLVM_DEBUG (dbgs () << " Abort because cannot determine a common VL\n " );
1252
+ LLVM_DEBUG (
1253
+ dbgs () << " Abort because cannot determine the demanded VL\n " );
1254
1254
return VLMAX;
1255
1255
}
1256
1256
@@ -1291,41 +1291,42 @@ MachineOperand RISCVVLOptimizer::computeDemandedVL(const MachineInstr &MI) {
1291
1291
bool RISCVVLOptimizer::tryReduceVL (MachineInstr &MI) {
1292
1292
LLVM_DEBUG (dbgs () << " Trying to reduce VL for " << MI << " \n " );
1293
1293
1294
- const MachineOperand &CommonVL = DemandedVLs.at (&MI);
1294
+ const MachineOperand &DemandedVL = DemandedVLs.at (&MI);
1295
1295
1296
- assert ((CommonVL .isImm () || CommonVL .getReg ().isVirtual ()) &&
1296
+ assert ((DemandedVL .isImm () || DemandedVL .getReg ().isVirtual ()) &&
1297
1297
" Expected VL to be an Imm or virtual Reg" );
1298
1298
1299
1299
unsigned VLOpNum = RISCVII::getVLOpNum (MI.getDesc ());
1300
1300
MachineOperand &VLOp = MI.getOperand (VLOpNum);
1301
1301
1302
- if (!RISCV::isVLKnownLE (CommonVL , VLOp)) {
1302
+ if (!RISCV::isVLKnownLE (DemandedVL , VLOp)) {
1303
1303
LLVM_DEBUG (dbgs () << " Abort due to DemandedVL not <= VLOp.\n " );
1304
1304
return false ;
1305
1305
}
1306
1306
1307
- if (CommonVL .isIdenticalTo (VLOp)) {
1307
+ if (DemandedVL .isIdenticalTo (VLOp)) {
1308
1308
LLVM_DEBUG (
1309
1309
dbgs ()
1310
1310
<< " Abort due to DemandedVL == VLOp, no point in reducing.\n " );
1311
1311
return false ;
1312
1312
}
1313
1313
1314
- if (CommonVL .isImm ()) {
1314
+ if (DemandedVL .isImm ()) {
1315
1315
LLVM_DEBUG (dbgs () << " Reduce VL from " << VLOp << " to "
1316
- << CommonVL .getImm () << " for " << MI << " \n " );
1317
- VLOp.ChangeToImmediate (CommonVL .getImm ());
1316
+ << DemandedVL .getImm () << " for " << MI << " \n " );
1317
+ VLOp.ChangeToImmediate (DemandedVL .getImm ());
1318
1318
return true ;
1319
1319
}
1320
- const MachineInstr *VLMI = MRI->getVRegDef (CommonVL .getReg ());
1320
+ const MachineInstr *VLMI = MRI->getVRegDef (DemandedVL .getReg ());
1321
1321
if (!MDT->dominates (VLMI, &MI))
1322
1322
return false ;
1323
- LLVM_DEBUG (dbgs () << " Reduce VL from " << VLOp << " to "
1324
- << printReg (CommonVL.getReg (), MRI->getTargetRegisterInfo ())
1325
- << " for " << MI << " \n " );
1323
+ LLVM_DEBUG (
1324
+ dbgs () << " Reduce VL from " << VLOp << " to "
1325
+ << printReg (DemandedVL.getReg (), MRI->getTargetRegisterInfo ())
1326
+ << " for " << MI << " \n " );
1326
1327
1327
1328
// All our checks passed. We can reduce VL.
1328
- VLOp.ChangeToRegister (CommonVL .getReg (), false );
1329
+ VLOp.ChangeToRegister (DemandedVL .getReg (), false );
1329
1330
return true ;
1330
1331
}
1331
1332
0 commit comments