@@ -70,6 +70,7 @@ class RISCVVectorPeephole : public MachineFunctionPass {
70
70
71
71
bool isAllOnesMask (const MachineInstr *MaskDef) const ;
72
72
std::optional<unsigned > getConstant (const MachineOperand &VL) const ;
73
+ bool ensureDominates (const MachineOperand &Use, MachineInstr &Src) const ;
73
74
74
75
// / Maps uses of V0 to the corresponding def of V0.
75
76
DenseMap<const MachineInstr *, const MachineInstr *> V0Defs;
@@ -165,6 +166,9 @@ bool RISCVVectorPeephole::tryToReduceVL(MachineInstr &MI) const {
165
166
if (VL.isIdenticalTo (SrcVL) || !isVLKnownLE (VL, SrcVL))
166
167
return false ;
167
168
169
+ if (!ensureDominates (VL, *Src))
170
+ return false ;
171
+
168
172
if (VL.isImm ())
169
173
SrcVL.ChangeToImmediate (VL.getImm ());
170
174
else if (VL.isReg ())
@@ -456,6 +460,26 @@ static bool dominates(MachineBasicBlock::const_iterator A,
456
460
return &*I == A;
457
461
}
458
462
463
+ // / If the register in \p MO doesn't dominate \p Src, try to move \p Src so it
464
+ // / does. Returns false if doesn't dominate and we can't move. \p MO must be in
465
+ // / the same basic block as \Src.
466
+ bool RISCVVectorPeephole::ensureDominates (const MachineOperand &MO,
467
+ MachineInstr &Src) const {
468
+ assert (MO.getParent ()->getParent () == Src.getParent ());
469
+ if (!MO.isReg () || MO.getReg () == RISCV::NoRegister)
470
+ return true ;
471
+
472
+ MachineInstr *Def = MRI->getVRegDef (MO.getReg ());
473
+ if (Def->getParent () == Src.getParent () && !dominates (Def, Src)) {
474
+ if (!isSafeToMove (Src, *Def->getNextNode ()))
475
+ return false ;
476
+ // FIXME: Update V0Defs
477
+ Src.moveBefore (Def->getNextNode ());
478
+ }
479
+
480
+ return true ;
481
+ }
482
+
459
483
// / If a PseudoVMV_V_V is the only user of its input, fold its passthru and VL
460
484
// / into it.
461
485
// /
@@ -501,15 +525,8 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
501
525
return false ;
502
526
503
527
// If the new passthru doesn't dominate Src, try to move Src so it does.
504
- if (Passthru.getReg () != RISCV::NoRegister) {
505
- MachineInstr *PassthruDef = MRI->getVRegDef (Passthru.getReg ());
506
- if (PassthruDef->getParent () == Src->getParent () &&
507
- !dominates (PassthruDef, Src)) {
508
- if (!isSafeToMove (*Src, *PassthruDef->getNextNode ()))
509
- return false ;
510
- Src->moveBefore (PassthruDef->getNextNode ());
511
- }
512
- }
528
+ if (!ensureDominates (Passthru, *Src))
529
+ return false ;
513
530
514
531
if (SrcPassthru.getReg () != Passthru.getReg ()) {
515
532
SrcPassthru.setReg (Passthru.getReg ());
0 commit comments