@@ -50,7 +50,9 @@ class RISCVVLOptimizer : public MachineFunctionPass {
5050 StringRef getPassName () const override { return PASS_NAME; }
5151
5252private:
53- bool checkUsers (const MachineOperand *&CommonVL, MachineInstr &MI);
53+ // / Returns the largest common VL MachineOperand that may be used to optimize
54+ // / MI. Returns std::nullopt if it failed to find a suitable VL.
55+ std::optional<const MachineOperand> checkUsers (MachineInstr &MI);
5456 bool tryReduceVL (MachineInstr &MI);
5557 bool isCandidate (const MachineInstr &MI) const ;
5658};
@@ -1045,15 +1047,15 @@ bool RISCVVLOptimizer::isCandidate(const MachineInstr &MI) const {
10451047 return true ;
10461048}
10471049
1048- static MachineOperand One = MachineOperand::CreateImm(1 );
1049-
1050- bool RISCVVLOptimizer::checkUsers (const MachineOperand *&CommonVL,
1051- MachineInstr &MI) {
1050+ std::optional<const MachineOperand>
1051+ RISCVVLOptimizer::checkUsers (MachineInstr &MI) {
10521052 // FIXME: Avoid visiting each user for each time we visit something on the
10531053 // worklist, combined with an extra visit from the outer loop. Restructure
10541054 // along lines of an instcombine style worklist which integrates the outer
10551055 // pass.
10561056 bool CanReduceVL = true ;
1057+ const MachineOperand *CommonVL = nullptr ;
1058+ const MachineOperand One = MachineOperand::CreateImm (1 );
10571059 for (auto &UserOp : MRI->use_operands (MI.getOperand (0 ).getReg ())) {
10581060 const MachineInstr &UserMI = *UserOp.getParent ();
10591061 LLVM_DEBUG (dbgs () << " Checking user: " << UserMI << " \n " );
@@ -1139,7 +1141,9 @@ bool RISCVVLOptimizer::checkUsers(const MachineOperand *&CommonVL,
11391141 break ;
11401142 }
11411143 }
1142- return CanReduceVL;
1144+ return CanReduceVL && CommonVL
1145+ ? std::make_optional<const MachineOperand>(*CommonVL)
1146+ : std::nullopt ;
11431147}
11441148
11451149bool RISCVVLOptimizer::tryReduceVL (MachineInstr &OrigMI) {
@@ -1151,12 +1155,11 @@ bool RISCVVLOptimizer::tryReduceVL(MachineInstr &OrigMI) {
11511155 MachineInstr &MI = *Worklist.pop_back_val ();
11521156 LLVM_DEBUG (dbgs () << " Trying to reduce VL for " << MI << " \n " );
11531157
1154- const MachineOperand *CommonVL = nullptr ;
1155- bool CanReduceVL = true ;
1156- if (isVectorRegClass (MI.getOperand (0 ).getReg (), MRI))
1157- CanReduceVL = checkUsers (CommonVL, MI);
1158+ if (!isVectorRegClass (MI.getOperand (0 ).getReg (), MRI))
1159+ continue ;
11581160
1159- if (!CanReduceVL || !CommonVL)
1161+ auto CommonVL = checkUsers (MI);
1162+ if (!CommonVL)
11601163 continue ;
11611164
11621165 assert ((CommonVL->isImm () || CommonVL->getReg ().isVirtual ()) &&
0 commit comments