@@ -66,6 +66,7 @@ class RISCVVectorPeephole : public MachineFunctionPass {
66
66
bool convertToWholeRegister (MachineInstr &MI) const ;
67
67
bool convertToUnmasked (MachineInstr &MI) const ;
68
68
bool convertVMergeToVMv (MachineInstr &MI) const ;
69
+ bool foldUndefPassthruVMV_V_V (MachineInstr &MI);
69
70
bool foldVMV_V_V (MachineInstr &MI);
70
71
71
72
bool isAllOnesMask (const MachineInstr *MaskDef) const ;
@@ -472,6 +473,38 @@ bool RISCVVectorPeephole::ensureDominates(const MachineOperand &MO,
472
473
return true ;
473
474
}
474
475
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 (), Src->getOperand (0 ).getReg ());
503
+ MI.eraseFromParent ();
504
+ V0Defs.erase (&MI);
505
+ return true ;
506
+ }
507
+
475
508
// / If a PseudoVMV_V_V is the only user of its input, fold its passthru and VL
476
509
// / into it.
477
510
// /
@@ -531,9 +564,8 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
531
564
532
565
// If MI was tail agnostic and the VL didn't increase, preserve it.
533
566
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))
537
569
Policy |= RISCVII::TAIL_AGNOSTIC;
538
570
Src->getOperand (RISCVII::getVecPolicyOpNum (Src->getDesc ())).setImm (Policy);
539
571
@@ -584,6 +616,7 @@ bool RISCVVectorPeephole::runOnMachineFunction(MachineFunction &MF) {
584
616
Changed |= convertToUnmasked (MI);
585
617
Changed |= convertToWholeRegister (MI);
586
618
Changed |= convertVMergeToVMv (MI);
619
+ Changed |= foldUndefPassthruVMV_V_V (MI);
587
620
Changed |= foldVMV_V_V (MI);
588
621
}
589
622
}
0 commit comments