@@ -66,6 +66,7 @@ class RISCVVectorPeephole : public MachineFunctionPass {
6666 bool convertToWholeRegister (MachineInstr &MI) const ;
6767 bool convertToUnmasked (MachineInstr &MI) const ;
6868 bool convertVMergeToVMv (MachineInstr &MI) const ;
69+ bool foldUndefPassthruVMV_V_V (MachineInstr &MI);
6970 bool foldVMV_V_V (MachineInstr &MI);
7071
7172 bool isAllOnesMask (const MachineInstr *MaskDef) const ;
@@ -472,6 +473,38 @@ bool RISCVVectorPeephole::ensureDominates(const MachineOperand &MO,
472473 return true ;
473474}
474475
476+ // / If a PseudoVMV_V_V's passthru is undef then we can replace it with its input
477+ bool RISCVVectorPeephole::foldUndefPassthruVMV_V_V (MachineInstr &MI) {
478+ if (RISCV::getRVVMCOpcode (MI.getOpcode ()) != RISCV::VMV_V_V)
479+ return false ;
480+ if (MI.getOperand (1 ).getReg () != RISCV::NoRegister)
481+ return false ;
482+
483+ // If the input was a pseudo with a policy operand, we can give it a tail
484+ // agnostic policy if MI's undef tail subsumes the input's.
485+ MachineInstr *Src = MRI->getVRegDef (MI.getOperand (2 ).getReg ());
486+ if (Src && !Src->hasUnmodeledSideEffects () &&
487+ MRI->hasOneUse (MI.getOperand (2 ).getReg ()) &&
488+ RISCVII::hasVLOp (Src->getDesc ().TSFlags ) &&
489+ RISCVII::hasVecPolicyOp (Src->getDesc ().TSFlags ) &&
490+ getSEWLMULRatio (MI) == getSEWLMULRatio (*Src)) {
491+ const MachineOperand &MIVL = MI.getOperand (3 );
492+ const MachineOperand &SrcVL =
493+ Src->getOperand (RISCVII::getVLOpNum (Src->getDesc ()));
494+
495+ MachineOperand &SrcPolicy =
496+ Src->getOperand (RISCVII::getVecPolicyOpNum (Src->getDesc ()));
497+
498+ if (isVLKnownLE (MIVL, SrcVL))
499+ SrcPolicy.setImm (SrcPolicy.getImm () | RISCVII::TAIL_AGNOSTIC);
500+ }
501+
502+ MRI->replaceRegWith (MI.getOperand (0 ).getReg (), MI.getOperand (2 ).getReg ());
503+ MI.eraseFromParent ();
504+ V0Defs.erase (&MI);
505+ return true ;
506+ }
507+
475508// / If a PseudoVMV_V_V is the only user of its input, fold its passthru and VL
476509// / into it.
477510// /
@@ -531,9 +564,8 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
531564
532565 // If MI was tail agnostic and the VL didn't increase, preserve it.
533566 int64_t Policy = RISCVII::TAIL_UNDISTURBED_MASK_UNDISTURBED;
534- bool TailAgnostic = (MI.getOperand (5 ).getImm () & RISCVII::TAIL_AGNOSTIC) ||
535- Passthru.getReg () == RISCV::NoRegister;
536- if (TailAgnostic && isVLKnownLE (MI.getOperand (3 ), SrcVL))
567+ if ((MI.getOperand (5 ).getImm () & RISCVII::TAIL_AGNOSTIC) &&
568+ isVLKnownLE (MI.getOperand (3 ), SrcVL))
537569 Policy |= RISCVII::TAIL_AGNOSTIC;
538570 Src->getOperand (RISCVII::getVecPolicyOpNum (Src->getDesc ())).setImm (Policy);
539571
@@ -584,6 +616,10 @@ bool RISCVVectorPeephole::runOnMachineFunction(MachineFunction &MF) {
584616 Changed |= convertToUnmasked (MI);
585617 Changed |= convertToWholeRegister (MI);
586618 Changed |= convertVMergeToVMv (MI);
619+ if (foldUndefPassthruVMV_V_V (MI)) {
620+ Changed |= true ;
621+ continue ; // MI is erased
622+ }
587623 Changed |= foldVMV_V_V (MI);
588624 }
589625 }
0 commit comments