Skip to content

Commit 336b290

Browse files
authored
[RISCV] Use a DAG combine to prune pointless vrgather.vi (#135392)
If the vrgather.vi is preceeded by a vmv.v.x which writes a superset of the lanes writen by the vrgather, and the vrgather has no passthru, then the vrgather has no semantic effect. This is the start of a mini-series of patches around rewriting vrgather.vi/vx preceeded by vmv.v.x, vfmf.v.f, vmv.s.x, etc... Starting with the simplest, but also lowest impact. One point I'd like a second oppinion on is the out of bounds semenatic change. As far as I can tell, all the indices are in bounds by construction. The doc change is as much as I couldn't figure out how to test the alternative as anything else.
1 parent 357e380 commit 336b290

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19709,6 +19709,19 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
1970919709
if (SDValue V = combineToVCPOP(N, DAG, Subtarget))
1971019710
return V;
1971119711
break;
19712+
case RISCVISD::VRGATHER_VX_VL: {
19713+
// Drop a redundant vrgather_vx.
19714+
// Note this assumes that out of bounds indices produce poison
19715+
// and can thus be replaced without having to prove them inbounds..
19716+
SDValue Src = N->getOperand(0);
19717+
SDValue Passthru = N->getOperand(2);
19718+
SDValue VL = N->getOperand(4);
19719+
// TODO: Handle fmv.v.f?
19720+
if (Src.getOpcode() == RISCVISD::VMV_V_X_VL && Passthru.isUndef() &&
19721+
VL == Src.getOperand(2))
19722+
return Src;
19723+
break;
19724+
}
1971219725
}
1971319726

1971419727
return SDValue();

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ enum NodeType : unsigned {
388388
VMSET_VL,
389389

390390
// Matches the semantics of vrgather.vx and vrgather.vv with extra operands
391-
// for passthru and VL. Operands are (src, index, mask, passthru, vl).
391+
// for passthru and VL, except that out of bound indices result in a poison
392+
// result not zero. Operands are (src, index, mask, passthru, vl).
392393
VRGATHER_VX_VL,
393394
VRGATHER_VV_VL,
394395
VRGATHEREI16_VV_VL,

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-shuffle-int.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,10 +1346,9 @@ define <4 x i16> @vmerge_2(<4 x i16> %x) {
13461346
define <4 x i16> @vmerge_3(<4 x i16> %x) {
13471347
; CHECK-LABEL: vmerge_3:
13481348
; CHECK: # %bb.0:
1349-
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, mu
1349+
; CHECK-NEXT: vsetivli zero, 4, e16, mf2, ta, ma
13501350
; CHECK-NEXT: vmv.v.i v0, 6
1351-
; CHECK-NEXT: vmv.v.i v9, 5
1352-
; CHECK-NEXT: vrgather.vi v8, v9, 1, v0.t
1351+
; CHECK-NEXT: vmerge.vim v8, v8, 5, v0
13531352
; CHECK-NEXT: ret
13541353
%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>
13551354
ret <4 x i16> %s

0 commit comments

Comments
 (0)