Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,12 +745,24 @@ bool RISCVVectorPeephole::foldVMergeToMask(MachineInstr &MI) const {
if (PassthruReg && !isKnownSameDefs(PassthruReg, FalseReg))
return false;

std::optional<std::pair<unsigned, unsigned>> NeedsCommute;

// If True has a passthru operand then it needs to be the same as vmerge's
// False, since False will be used for the result's passthru operand.
Register TruePassthru = True.getOperand(True.getNumExplicitDefs()).getReg();
if (RISCVII::isFirstDefTiedToFirstUse(True.getDesc()) && TruePassthru &&
!isKnownSameDefs(TruePassthru, FalseReg))
return false;
!isKnownSameDefs(TruePassthru, FalseReg)) {
// If True's passthru != False, check if it uses False in another operand
// and try to commute it.
int OtherIdx = True.findRegisterUseOperandIdx(FalseReg, TRI);
if (OtherIdx == -1)
return false;
unsigned OpIdx1 = OtherIdx;
unsigned OpIdx2 = True.getNumExplicitDefs();
if (!TII->findCommutedOpIndices(True, OpIdx1, OpIdx2))
return false;
NeedsCommute = {OpIdx1, OpIdx2};
}

// Make sure it doesn't raise any observable fp exceptions, since changing the
// active elements will affect how fflags is set.
Expand Down Expand Up @@ -796,6 +808,14 @@ bool RISCVVectorPeephole::foldVMergeToMask(MachineInstr &MI) const {
if (!ensureDominates(MaskOp, True))
return false;

if (NeedsCommute) {
auto [OpIdx1, OpIdx2] = *NeedsCommute;
[[maybe_unused]] bool Commuted =
TII->commuteInstruction(True, /*NewMI=*/false, OpIdx1, OpIdx2);
assert(Commuted && "Failed to commute True?");
Info = RISCV::lookupMaskedIntrinsicByUnmasked(True.getOpcode());
}

True.setDesc(TII->get(Info->MaskedPseudo));

// Insert the mask operand.
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/CodeGen/RISCV/rvv/rvv-peephole-vmerge-vops.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1215,3 +1215,15 @@ define <vscale x 2 x i32> @unfoldable_mismatched_sew(<vscale x 2 x i32> %passthr
)
ret <vscale x 2 x i32> %b
}

define <vscale x 2 x float> @commute_vfmadd(<vscale x 2 x float> %passthru, <vscale x 2 x float> %x, <vscale x 2 x float> %y, <vscale x 2 x i1> %mask, i32 zeroext %evl) {
; CHECK-LABEL: commute_vfmadd:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli zero, a0, e32, m1, tu, mu
; CHECK-NEXT: vfmacc.vv v8, v9, v10, v0.t
; CHECK-NEXT: ret
%fmul = fmul contract <vscale x 2 x float> %x, %y
%fadd = fadd contract <vscale x 2 x float> %fmul, %passthru
%merge = call <vscale x 2 x float> @llvm.vp.merge(<vscale x 2 x i1> %mask, <vscale x 2 x float> %fadd, <vscale x 2 x float> %passthru, i32 %evl)
ret <vscale x 2 x float> %merge
}
Loading
Loading