Skip to content

Commit 41d0609

Browse files
committed
[SelectionDAG] Teach isConstOrConstSplat about ISD::SPLAT_VECTOR
This improves llvm::isConstOrConstSplat by allowing it to analyze ISD::SPLAT_VECTOR nodes, in order to allow more constant-folding of operations using scalable vector types. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D94168
1 parent 03d2493 commit 41d0609

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9129,6 +9129,18 @@ ConstantSDNode *llvm::isConstOrConstSplat(SDValue N, bool AllowUndefs,
91299129
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
91309130
return CN;
91319131

9132+
// SplatVectors can truncate their operands. Ignore that case here unless
9133+
// AllowTruncation is set.
9134+
if (N->getOpcode() == ISD::SPLAT_VECTOR) {
9135+
EVT VecEltVT = N->getValueType(0).getVectorElementType();
9136+
if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9137+
EVT CVT = CN->getValueType(0);
9138+
assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
9139+
if (AllowTruncation || CVT == VecEltVT)
9140+
return CN;
9141+
}
9142+
}
9143+
91329144
if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
91339145
BitVector UndefElements;
91349146
ConstantSDNode *CN = BV->getConstantSplatNode(&UndefElements);

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,8 +3463,11 @@ SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
34633463

34643464
// Ensure that the constant occurs on the RHS and fold constant comparisons.
34653465
// TODO: Handle non-splat vector constants. All undef causes trouble.
3466+
// FIXME: We can't yet fold constant scalable vector splats, so avoid an
3467+
// infinite loop here when we encounter one.
34663468
ISD::CondCode SwappedCC = ISD::getSetCCSwappedOperands(Cond);
34673469
if (isConstOrConstSplat(N0) &&
3470+
(!OpVT.isScalableVector() || !isConstOrConstSplat(N1)) &&
34683471
(DCI.isBeforeLegalizeOps() ||
34693472
isCondCodeLegal(SwappedCC, N0.getSimpleValueType())))
34703473
return DAG.getSetCC(dl, VT, N1, N0, SwappedCC);

llvm/test/CodeGen/AArch64/sve-intrinsics-int-compares-with-imm.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ define <vscale x 16 x i1> @wide_cmphi_b(<vscale x 16 x i1> %pg, <vscale x 16 x i
820820

821821
define <vscale x 8 x i1> @ir_cmphi_h(<vscale x 8 x i16> %a) {
822822
; CHECK-LABEL: ir_cmphi_h
823-
; CHECK: cmphi p0.h, p0/z, z0.h, #0
823+
; CHECK: cmpne p0.h, p0/z, z0.h, #0
824824
; CHECK-NEXT: ret
825825
%elt = insertelement <vscale x 8 x i16> undef, i16 0, i32 0
826826
%splat = shufflevector <vscale x 8 x i16> %elt, <vscale x 8 x i16> undef, <vscale x 8 x i32> zeroinitializer
@@ -948,7 +948,7 @@ define <vscale x 16 x i1> @wide_cmphs_b(<vscale x 16 x i1> %pg, <vscale x 16 x i
948948

949949
define <vscale x 8 x i1> @ir_cmphs_h(<vscale x 8 x i16> %a) {
950950
; CHECK-LABEL: ir_cmphs_h
951-
; CHECK: cmphs p0.h, p0/z, z0.h, #0
951+
; CHECK: ptrue p0.h
952952
; CHECK-NEXT: ret
953953
%elt = insertelement <vscale x 8 x i16> undef, i16 0, i32 0
954954
%splat = shufflevector <vscale x 8 x i16> %elt, <vscale x 8 x i16> undef, <vscale x 8 x i32> zeroinitializer
@@ -1076,7 +1076,7 @@ define <vscale x 16 x i1> @wide_cmplo_b(<vscale x 16 x i1> %pg, <vscale x 16 x i
10761076

10771077
define <vscale x 8 x i1> @ir_cmplo_h(<vscale x 8 x i16> %a) {
10781078
; CHECK-LABEL: ir_cmplo_h
1079-
; CHECK: cmplo p0.h, p0/z, z0.h, #0
1079+
; CHECK: whilelo p0.h, xzr, xzr
10801080
; CHECK-NEXT: ret
10811081
%elt = insertelement <vscale x 8 x i16> undef, i16 0, i32 0
10821082
%splat = shufflevector <vscale x 8 x i16> %elt, <vscale x 8 x i16> undef, <vscale x 8 x i32> zeroinitializer

llvm/test/CodeGen/RISCV/rvv/vdiv-sdnode-rv32.ll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,9 @@ define <vscale x 1 x i8> @vdiv_vi_nxv1i8_0(<vscale x 1 x i8> %va) {
3737
}
3838

3939
; Test V/1 to see if we can optimize it away for scalable vectors.
40-
; FIXME: We can't.
4140
define <vscale x 1 x i8> @vdiv_vi_nxv1i8_1(<vscale x 1 x i8> %va) {
4241
; CHECK-LABEL: vdiv_vi_nxv1i8_1:
4342
; CHECK: # %bb.0:
44-
; CHECK-NEXT: addi a0, zero, 1
45-
; CHECK-NEXT: vsetvli a1, zero, e8,mf8,ta,mu
46-
; CHECK-NEXT: vdivu.vx v16, v16, a0
4743
; CHECK-NEXT: ret
4844
%head = insertelement <vscale x 1 x i8> undef, i8 1, i32 0
4945
%splat = shufflevector <vscale x 1 x i8> %head, <vscale x 1 x i8> undef, <vscale x 1 x i32> zeroinitializer
@@ -52,13 +48,11 @@ define <vscale x 1 x i8> @vdiv_vi_nxv1i8_1(<vscale x 1 x i8> %va) {
5248
}
5349

5450
; Test 0/V to see if we can optimize it away for scalable vectors.
55-
; FIXME: We can't.
5651
define <vscale x 1 x i8> @vdiv_iv_nxv1i8_0(<vscale x 1 x i8> %va) {
5752
; CHECK-LABEL: vdiv_iv_nxv1i8_0:
5853
; CHECK: # %bb.0:
5954
; CHECK-NEXT: vsetvli a0, zero, e8,mf8,ta,mu
60-
; CHECK-NEXT: vmv.v.i v25, 0
61-
; CHECK-NEXT: vdivu.vv v16, v25, v16
55+
; CHECK-NEXT: vmv.v.i v16, 0
6256
; CHECK-NEXT: ret
6357
%head = insertelement <vscale x 1 x i8> undef, i8 0, i32 0
6458
%splat = shufflevector <vscale x 1 x i8> %head, <vscale x 1 x i8> undef, <vscale x 1 x i32> zeroinitializer

llvm/test/CodeGen/RISCV/rvv/vdivu-sdnode-rv32.ll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,9 @@ define <vscale x 1 x i8> @vdivu_vi_nxv1i8_0(<vscale x 1 x i8> %va) {
3737
}
3838

3939
; Test V/1 to see if we can optimize it away for scalable vectors.
40-
; FIXME: We can't.
4140
define <vscale x 1 x i8> @vdivu_vi_nxv1i8_1(<vscale x 1 x i8> %va) {
4241
; CHECK-LABEL: vdivu_vi_nxv1i8_1:
4342
; CHECK: # %bb.0:
44-
; CHECK-NEXT: addi a0, zero, 1
45-
; CHECK-NEXT: vsetvli a1, zero, e8,mf8,ta,mu
46-
; CHECK-NEXT: vdiv.vx v16, v16, a0
4743
; CHECK-NEXT: ret
4844
%head = insertelement <vscale x 1 x i8> undef, i8 1, i32 0
4945
%splat = shufflevector <vscale x 1 x i8> %head, <vscale x 1 x i8> undef, <vscale x 1 x i32> zeroinitializer
@@ -52,13 +48,11 @@ define <vscale x 1 x i8> @vdivu_vi_nxv1i8_1(<vscale x 1 x i8> %va) {
5248
}
5349

5450
; Test 0/V to see if we can optimize it away for scalable vectors.
55-
; FIXME: We can't.
5651
define <vscale x 1 x i8> @vdivu_iv_nxv1i8_0(<vscale x 1 x i8> %va) {
5752
; CHECK-LABEL: vdivu_iv_nxv1i8_0:
5853
; CHECK: # %bb.0:
5954
; CHECK-NEXT: vsetvli a0, zero, e8,mf8,ta,mu
60-
; CHECK-NEXT: vmv.v.i v25, 0
61-
; CHECK-NEXT: vdiv.vv v16, v25, v16
55+
; CHECK-NEXT: vmv.v.i v16, 0
6256
; CHECK-NEXT: ret
6357
%head = insertelement <vscale x 1 x i8> undef, i8 0, i32 0
6458
%splat = shufflevector <vscale x 1 x i8> %head, <vscale x 1 x i8> undef, <vscale x 1 x i32> zeroinitializer

0 commit comments

Comments
 (0)