@@ -51,8 +51,7 @@ class RISCVVLOptimizer : public MachineFunctionPass {
51
51
StringRef getPassName () const override { return PASS_NAME; }
52
52
53
53
private:
54
- bool checkUsers (std::optional<const MachineOperand *> &CommonVL,
55
- MachineInstr &MI);
54
+ bool checkUsers (const MachineOperand *&CommonVL, MachineInstr &MI);
56
55
bool tryReduceVL (MachineInstr &MI);
57
56
bool isCandidate (const MachineInstr &MI) const ;
58
57
};
@@ -669,7 +668,7 @@ bool RISCVVLOptimizer::isCandidate(const MachineInstr &MI) const {
669
668
unsigned PolicyOpNum = RISCVII::getVecPolicyOpNum (Desc);
670
669
const MachineOperand &PolicyOp = MI.getOperand (PolicyOpNum);
671
670
uint64_t Policy = PolicyOp.getImm ();
672
- UseTAPolicy = ( Policy & RISCVII::TAIL_AGNOSTIC) == RISCVII::TAIL_AGNOSTIC;
671
+ UseTAPolicy = Policy & RISCVII::TAIL_AGNOSTIC;
673
672
if (HasPassthru) {
674
673
unsigned PassthruOpIdx = MI.getNumExplicitDefs ();
675
674
UseTAPolicy = UseTAPolicy || (MI.getOperand (PassthruOpIdx).getReg () ==
@@ -711,8 +710,8 @@ bool RISCVVLOptimizer::isCandidate(const MachineInstr &MI) const {
711
710
return true ;
712
711
}
713
712
714
- bool RISCVVLOptimizer::checkUsers (
715
- std::optional< const MachineOperand *> &CommonVL, MachineInstr &MI) {
713
+ bool RISCVVLOptimizer::checkUsers (const MachineOperand *&CommonVL,
714
+ MachineInstr &MI) {
716
715
// FIXME: Avoid visiting each user for each time we visit something on the
717
716
// worklist, combined with an extra visit from the outer loop. Restructure
718
717
// along lines of an instcombine style worklist which integrates the outer
@@ -757,17 +756,15 @@ bool RISCVVLOptimizer::checkUsers(
757
756
758
757
unsigned VLOpNum = RISCVII::getVLOpNum (Desc);
759
758
const MachineOperand &VLOp = UserMI.getOperand (VLOpNum);
759
+
760
760
// Looking for an immediate or a register VL that isn't X0.
761
- if (VLOp.isReg () && VLOp.getReg () == RISCV::X0) {
762
- LLVM_DEBUG (dbgs () << " Abort due to user uses X0 as VL.\n " );
763
- CanReduceVL = false ;
764
- break ;
765
- }
761
+ assert (!VLOp.isReg () ||
762
+ VLOp.getReg () != RISCV::X0 && " Did not expect X0 VL" );
766
763
767
764
if (!CommonVL) {
768
765
CommonVL = &VLOp;
769
766
LLVM_DEBUG (dbgs () << " User VL is: " << VLOp << " \n " );
770
- } else if (!(* CommonVL) ->isIdenticalTo (VLOp)) {
767
+ } else if (!CommonVL->isIdenticalTo (VLOp)) {
771
768
LLVM_DEBUG (dbgs () << " Abort because users have different VL\n " );
772
769
CanReduceVL = false ;
773
770
break ;
@@ -804,43 +801,40 @@ bool RISCVVLOptimizer::tryReduceVL(MachineInstr &OrigMI) {
804
801
MachineInstr &MI = *Worklist.pop_back_val ();
805
802
LLVM_DEBUG (dbgs () << " Trying to reduce VL for " << MI << " \n " );
806
803
807
- std::optional< const MachineOperand *> CommonVL;
804
+ const MachineOperand *CommonVL = nullptr ;
808
805
bool CanReduceVL = true ;
809
806
if (isVectorRegClass (MI.getOperand (0 ).getReg (), MRI))
810
807
CanReduceVL = checkUsers (CommonVL, MI);
811
808
812
809
if (!CanReduceVL || !CommonVL)
813
810
continue ;
814
811
815
- const MachineOperand *CommonVLMO = *CommonVL;
816
- if (!CommonVLMO->isImm () && !CommonVLMO->getReg ().isVirtual ()) {
817
- LLVM_DEBUG (dbgs () << " Abort because common VL is not valid.\n " );
818
- continue ;
819
- }
812
+ assert ((CommonVL->isImm () || CommonVL->getReg ().isVirtual ()) &&
813
+ " Expected VL to be an Imm or virtual Reg" );
820
814
821
815
unsigned VLOpNum = RISCVII::getVLOpNum (MI.getDesc ());
822
816
MachineOperand &VLOp = MI.getOperand (VLOpNum);
823
817
824
- if (!RISCV::isVLKnownLE (*CommonVLMO , VLOp)) {
818
+ if (!RISCV::isVLKnownLE (*CommonVL , VLOp)) {
825
819
LLVM_DEBUG (dbgs () << " Abort due to no benefit.\n " );
826
820
continue ;
827
821
}
828
822
829
- if (CommonVLMO ->isImm ()) {
823
+ if (CommonVL ->isImm ()) {
830
824
LLVM_DEBUG (dbgs () << " Reduce VL from " << VLOp << " to "
831
- << CommonVLMO ->getImm () << " for " << MI << " \n " );
832
- VLOp.ChangeToImmediate (CommonVLMO ->getImm ());
825
+ << CommonVL ->getImm () << " for " << MI << " \n " );
826
+ VLOp.ChangeToImmediate (CommonVL ->getImm ());
833
827
} else {
834
- const MachineInstr *VLMI = MRI->getVRegDef (CommonVLMO ->getReg ());
828
+ const MachineInstr *VLMI = MRI->getVRegDef (CommonVL ->getReg ());
835
829
if (!MDT->dominates (VLMI, &MI))
836
830
continue ;
837
831
LLVM_DEBUG (
838
832
dbgs () << " Reduce VL from " << VLOp << " to "
839
- << printReg (CommonVLMO ->getReg (), MRI->getTargetRegisterInfo ())
833
+ << printReg (CommonVL ->getReg (), MRI->getTargetRegisterInfo ())
840
834
<< " for " << MI << " \n " );
841
835
842
836
// All our checks passed. We can reduce VL.
843
- VLOp.ChangeToRegister (CommonVLMO ->getReg (), false );
837
+ VLOp.ChangeToRegister (CommonVL ->getReg (), false );
844
838
}
845
839
846
840
MadeChange = true ;
0 commit comments