@@ -1513,17 +1513,21 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
15131513// / this is geared to catch the common case of a fixed length vsetvl in a single
15141514// / block loop when it could execute once in the preheader instead.
15151515void RISCVInsertVSETVLI::doPRE (MachineBasicBlock &MBB) {
1516- // We need a prepredecessor to move the VSETVLI to
1517- if (MBB.pred_empty ())
1516+ // Only works for either one predecessor, or two predecessors if it's a loop
1517+ if (MBB.pred_empty () && MBB. pred_size () > 2 )
15181518 return ;
15191519
15201520 if (!BlockInfo[MBB.getNumber ()].Pred .isUnknown ())
15211521 return ;
15221522
1523+ bool isLoop = false ;
1524+
15231525 MachineBasicBlock *UnavailablePred = nullptr ;
15241526 VSETVLIInfo AvailableInfo;
15251527 MachineBasicBlock *PreviousPred = nullptr ;
15261528 for (MachineBasicBlock *P : MBB.predecessors ()) {
1529+ isLoop |= (P == &MBB);
1530+
15271531 const VSETVLIInfo &PredInfo = BlockInfo[P->getNumber ()].Exit ;
15281532 if (PredInfo.isUnknown ()) {
15291533 if (UnavailablePred)
@@ -1532,6 +1536,9 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
15321536 } else if (!AvailableInfo.isValid ()) {
15331537 AvailableInfo = PredInfo;
15341538 } else if (AvailableInfo != PredInfo) {
1539+ if (!isLoop)
1540+ return ;
1541+
15351542 DemandedFields PREDemands;
15361543 PREDemands.demandVTYPE ();
15371544
@@ -1546,17 +1553,7 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
15461553 }
15471554 }
15481555
1549- // Filter out loops
1550- SmallVector<MachineBasicBlock *, 4 > Preds = {MBB.predecessors ().begin (),
1551- MBB.predecessors ().end ()};
1552- Preds.erase (std::remove_if (Preds.begin (), Preds.end (),
1553- [&](MachineBasicBlock *P) { return P == &MBB; }),
1554- Preds.end ());
1555-
1556- // Only works for one Pred for now
1557- if (Preds.size () != 1 )
1558- return ;
1559- PreviousPred = *Preds.begin ();
1556+ PreviousPred = P;
15601557 }
15611558
15621559 // Unreachable, single pred, or full redundancy. Note that FRE is handled by
@@ -1573,7 +1570,7 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
15731570 return ;
15741571
15751572 // Critical edge - TODO: consider splitting?
1576- if (UnavailablePred->succ_size () != 1 )
1573+ if (UnavailablePred->succ_size () != 1 && !isLoop )
15771574 return ;
15781575
15791576 // If the AVL value is a register (other than our VLMAX sentinel),
0 commit comments