Skip to content

Commit aad6997

Browse files
authored
[RISCV] Fold PseudoVMV_V_V with undef passthru, handling policy (#106943)
If a vmv.v.v has an undef passthru then we can just replace it with its input operand, since the tail is completely undefined. This is a reattempt of #106840, but also checks to see if the input was a pseudo where we can relax its tail policy to undef. This also means we don't need to check for undef passthrus in foldVMV_V_V anymore because they will be handled by foldUndefPassthruVMV_V_V.
1 parent c2fc332 commit aad6997

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-to-vmv.mir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ body: |
1515
; CHECK-NEXT: %avl:gprnox0 = COPY $x1
1616
; CHECK-NEXT: %mask:vmv0 = PseudoVMSET_M_B8 %avl, 5 /* e32 */
1717
; CHECK-NEXT: $v0 = COPY %mask
18-
; CHECK-NEXT: %x:vr = PseudoVMV_V_V_M1 $noreg, %true, %avl, 5 /* e32 */, 0 /* tu, mu */
1918
%false:vr = COPY $v8
2019
%true:vr = COPY $v9
2120
%avl:gprnox0 = COPY $x1

llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,8 @@ define <vscale x 2 x i32> @unfoldable_mismatched_sew(<vscale x 2 x i32> %passthr
199199
define <vscale x 1 x i64> @undef_passthru(<vscale x 1 x i64> %passthru, <vscale x 1 x i64> %x, <vscale x 1 x i64> %y, iXLen %avl) {
200200
; CHECK-LABEL: undef_passthru:
201201
; CHECK: # %bb.0:
202-
; CHECK-NEXT: vsetvli zero, a0, e64, m1, tu, ma
202+
; CHECK-NEXT: vsetvli zero, a0, e64, m1, ta, ma
203203
; CHECK-NEXT: vadd.vv v8, v9, v10
204-
; CHECK-NEXT: vsetvli zero, zero, e64, m1, ta, ma
205-
; CHECK-NEXT: vmv.v.v v8, v8
206204
; CHECK-NEXT: ret
207205
%a = call <vscale x 1 x i64> @llvm.riscv.vadd.nxv1i64.nxv1i64(<vscale x 1 x i64> %passthru, <vscale x 1 x i64> %x, <vscale x 1 x i64> %y, iXLen %avl)
208206
%b = call <vscale x 1 x i64> @llvm.riscv.vmv.v.v.nxv1i64(<vscale x 1 x i64> undef, <vscale x 1 x i64> %a, iXLen %avl)

llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.mir

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ body: |
6969
; CHECK: liveins: $v8
7070
; CHECK-NEXT: {{ $}}
7171
; CHECK-NEXT: %passthru:vr = COPY $v8
72-
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5 /* e32 */, 0 /* tu, mu */
73-
; CHECK-NEXT: %y:vr = PseudoVMV_V_V_M1 $noreg, %x, 4, 5 /* e32 */, 1 /* ta, mu */
72+
; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5 /* e32 */, 1 /* ta, mu */
7473
%passthru:vr = COPY $v8
7574
%x:vr = PseudoVADD_VV_M1 %passthru, $noreg, $noreg, 4, 5 /* e32 */, 0 /* tu, mu */
7675
%y:vr = PseudoVMV_V_V_M1 $noreg, %x, 4, 5 /* e32 */, 1 /* ta, mu */

0 commit comments

Comments
 (0)