Skip to content

[RISCV] Use a DAG combine to prune pointless vrgather.vi #135392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
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
13 changes: 13 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19709,6 +19709,19 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
if (SDValue V = combineToVCPOP(N, DAG, Subtarget))
return V;
break;
case RISCVISD::VRGATHER_VX_VL: {
// Drop a redundant vrgather_vx.
// Note this assumes that out of bounds indices produce poison
// and can thus be replaced without having to prove them inbounds..
SDValue Src = N->getOperand(0);
SDValue Passthru = N->getOperand(2);
SDValue VL = N->getOperand(4);
// TODO: Handle fmv.v.f?
if (Src.getOpcode() == RISCVISD::VMV_V_X_VL && Passthru.isUndef() &&
VL == Src.getOperand(2))
return Src;
break;
}
}

return SDValue();
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/RISCV/RISCVISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ enum NodeType : unsigned {
VMSET_VL,

// Matches the semantics of vrgather.vx and vrgather.vv with extra operands
// for passthru and VL. Operands are (src, index, mask, passthru, vl).
// for passthru and VL, except that out of bound indices result in a poison
// result not zero. Operands are (src, index, mask, passthru, vl).
VRGATHER_VX_VL,
VRGATHER_VV_VL,
VRGATHEREI16_VV_VL,
Expand Down
5 changes: 2 additions & 3 deletions llvm/test/CodeGen/RISCV/rvv/fixed-vectors-shuffle-int.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1346,10 +1346,9 @@ define <4 x i16> @vmerge_2(<4 x i16> %x) {
define <4 x i16> @vmerge_3(<4 x i16> %x) {
; CHECK-LABEL: vmerge_3:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, mu
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
; CHECK-NEXT: vmv.v.i v0, 6
; CHECK-NEXT: vmv.v.i v9, 5
; CHECK-NEXT: vrgather.vi v8, v9, 1, v0.t
; CHECK-NEXT: vmerge.vim v8, v8, 5, v0
; CHECK-NEXT: ret
%s = shufflevector <4 x i16> %x, <4 x i16> <i16 poison, i16 5, i16 poison, i16 poison>, <4 x i32> <i32 0, i32 5, i32 5, i32 3>
ret <4 x i16> %s
Expand Down