[llvm-branch-commits] [llvm] c01da65 - Revert "[SelectionDAG] Narrow (shr (sub A, B), 1) to AVGSUBFLOOR (#233635)"
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 3 00:06:13 PST 2026
Author: Craig Topper
Date: 2026-11-03T00:06:09-08:00
New Revision: c01da6570e7b7596cada28794759e14d84d112d2
URL: https://github.com/llvm/llvm-project/commit/c01da6570e7b7596cada28794759e14d84d112d2
DIFF: https://github.com/llvm/llvm-project/commit/c01da6570e7b7596cada28794759e14d84d112d2.diff
LOG: Revert "[SelectionDAG] Narrow (shr (sub A, B), 1) to AVGSUBFLOOR (#233635)"
This reverts commit 11827b9010c26ea3b401b58070d62ba0f1caee48.
Added:
Modified:
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/rvp-avgsub.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index b86498278b96b..cab0942a2718c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -1148,21 +1148,6 @@ SDValue TargetLowering::SimplifyMultipleUseDemandedVectorElts(
Depth);
}
-// Find the smallest power-2 type that is at least 8 bits and can hold a value
-// of type VT that has KnownBits known sign/zero bits. Returns std::nullopt if
-// that is wider than VT.
-static std::optional<EVT> getNarrowAVGType(EVT VT, unsigned KnownBits,
- LLVMContext &Ctx) {
- unsigned MinWidth =
- std::max<unsigned>(VT.getScalarSizeInBits() - KnownBits, 8);
- EVT NVT = EVT::getIntegerVT(Ctx, llvm::bit_ceil(MinWidth));
- if (NVT.getScalarSizeInBits() > VT.getScalarSizeInBits())
- return std::nullopt;
- if (VT.isVector())
- NVT = EVT::getVectorVT(Ctx, NVT, VT.getVectorElementCount());
- return NVT;
-}
-
// Attempt to form ext(avgfloor(A, B)) from shr(add(ext(A), ext(B)), 1).
// or to form ext(avgceil(A, B)) from shr(add(ext(A), ext(B), 1), 1).
static SDValue combineShiftToAVG(SDValue Op,
@@ -1271,11 +1256,13 @@ static SDValue combineShiftToAVG(SDValue Op,
// operation, given the original type size and the number of known sign/zero
// bits.
EVT VT = Op.getValueType();
- std::optional<EVT> NarrowVT =
- getNarrowAVGType(VT, KnownBits, *DAG.getContext());
- if (!NarrowVT)
+ unsigned MinWidth =
+ std::max<unsigned>(VT.getScalarSizeInBits() - KnownBits, 8);
+ EVT NVT = EVT::getIntegerVT(*DAG.getContext(), llvm::bit_ceil(MinWidth));
+ if (NVT.getScalarSizeInBits() > VT.getScalarSizeInBits())
return SDValue();
- EVT NVT = *NarrowVT;
+ if (VT.isVector())
+ NVT = EVT::getVectorVT(*DAG.getContext(), NVT, VT.getVectorElementCount());
if (TLO.LegalTypes() && !TLI.isOperationLegal(AVGOpc, NVT)) {
// If we could not transform, and (both) adds are nuw/nsw, we can use the
// larger type size to do the transform.
@@ -1303,94 +1290,6 @@ static SDValue combineShiftToAVG(SDValue Op,
return DAG.getExtOrTrunc(IsSigned, ResultAVG, DL, VT);
}
-// Attempt to form ext(avgsubfloor(A, B)) from shr(sub(ext(A), ext(B)), 1).
-static SDValue combineShiftToAVGSUB(SDValue Op,
- TargetLowering::TargetLoweringOpt &TLO,
- const TargetLowering &TLI,
- const APInt &DemandedBits,
- const APInt &DemandedElts, unsigned Depth) {
- assert((Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) &&
- "SRL or SRA node is required here!");
- // Is the right shift using an immediate value of 1?
- ConstantSDNode *N1C = isConstOrConstSplat(Op.getOperand(1), DemandedElts);
- if (!N1C || !N1C->isOne())
- return SDValue();
-
- SDValue Sub = Op.getOperand(0);
- if (Sub.getOpcode() != ISD::SUB)
- return SDValue();
-
- // The result of the avgsub is sign extended to match the sra. For srl, the
- // top bit of the result is always zero so it must not be demanded.
- if (Op.getOpcode() == ISD::SRL && !DemandedBits.isSignBitClear())
- return SDValue();
-
- SDValue A = Sub.getOperand(0);
- SDValue B = Sub.getOperand(1);
-
- // If both operands have at least 2 sign bits or at least 1 leading zero, the
- // subtract can't overflow and the operands are extended from a narrower type.
- SelectionDAG &DAG = TLO.DAG;
- bool IsSigned;
- unsigned KnownBits;
- unsigned NumSigned =
- std::min(DAG.ComputeNumSignBits(A, DemandedElts, Depth),
- DAG.ComputeNumSignBits(B, DemandedElts, Depth)) -
- 1;
- unsigned NumZero = std::min(
- DAG.computeKnownBits(A, DemandedElts, Depth).countMinLeadingZeros(),
- DAG.computeKnownBits(B, DemandedElts, Depth).countMinLeadingZeros());
- if (NumZero >= 1 && NumSigned < NumZero) {
- IsSigned = false;
- KnownBits = NumZero;
- } else if (NumSigned >= 1) {
- IsSigned = true;
- KnownBits = NumSigned;
- } else {
- return SDValue();
- }
-
- unsigned AVGOpc = IsSigned ? ISD::AVGSUBFLOORS : ISD::AVGSUBFLOORU;
-
- // Return true if the AVG node is legal or custom on the type VT will be
- // legalized to, provided that legalization doesn't change the scalar size.
- // That means vectors that are widened or split are accepted, but integers
- // that are promoted or expanded are not.
- LLVMContext &Ctx = *DAG.getContext();
- auto IsLegalAfterTypeLegalization = [&](EVT VT) {
- EVT LegalVT = TLI.getLegalTypeToTransformTo(Ctx, VT);
- return LegalVT.getScalarSizeInBits() == VT.getScalarSizeInBits() &&
- TLI.isOperationLegal(AVGOpc, LegalVT);
- };
-
- // Use the narrow type if the node is legal or custom there. Otherwise fall
- // back to the original type, which is still correct since the subtract can't
- // overflow. Don't form the node if the target can't do it on either type, as
- // expanding it may be worse than the original subtract and shift.
- // FIXME: Enable for illegal types/operations like combineShiftToAVG.
- EVT VT = Op.getValueType();
- std::optional<EVT> NarrowVT = getNarrowAVGType(VT, KnownBits, Ctx);
- EVT NVT = VT;
- if (NarrowVT && IsLegalAfterTypeLegalization(*NarrowVT))
- NVT = *NarrowVT;
- else if (!IsLegalAfterTypeLegalization(VT))
- return SDValue();
-
- // Don't create an AVGSUBFLOOR node with a scalar constant unless it's legal
- // as this is likely to stop other folds (reassociation, value tracking etc.)
- if (!TLI.isOperationLegal(AVGOpc, NVT) &&
- (isa<ConstantSDNode>(A) || isa<ConstantSDNode>(B)))
- return SDValue();
-
- // The
diff erence of two unsigned values can be negative, so the result is
- // always sign extended.
- SDLoc DL(Op);
- SDValue ResultAVG =
- DAG.getNode(AVGOpc, DL, NVT, DAG.getExtOrTrunc(IsSigned, A, DL, NVT),
- DAG.getExtOrTrunc(IsSigned, B, DL, NVT));
- return DAG.getExtOrTrunc(/*IsSigned=*/true, ResultAVG, DL, VT);
-}
-
/// Look at Op. At this point, we know that only the OriginalDemandedBits of the
/// result of Op are ever used downstream. If we can use this information to
/// simplify Op, create a new simplified DAG node and return true, returning the
@@ -2402,9 +2301,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SDValue AVG = combineShiftToAVG(Op, TLO, *this, DemandedBits,
DemandedElts, Depth + 1))
return TLO.CombineTo(Op, AVG);
- if (SDValue AVG = combineShiftToAVGSUB(Op, TLO, *this, DemandedBits,
- DemandedElts, Depth + 1))
- return TLO.CombineTo(Op, AVG);
break;
}
@@ -2514,9 +2410,6 @@ bool TargetLowering::SimplifyDemandedBits(
if (SDValue AVG = combineShiftToAVG(Op, TLO, *this, DemandedBits,
DemandedElts, Depth + 1))
return TLO.CombineTo(Op, AVG);
- if (SDValue AVG = combineShiftToAVGSUB(Op, TLO, *this, DemandedBits,
- DemandedElts, Depth + 1))
- return TLO.CombineTo(Op, AVG);
break;
}
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 26249990606fa..5b0ae94d805a3 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -20152,6 +20152,17 @@ static SDValue combinePExtTruncate(SDNode *N, SelectionDAG &DAG,
switch (Op.getOpcode()) {
default:
return SDValue();
+ case ISD::SUB:
+ // PASUB/PASUBU: shift amount must be 1
+ if (ShAmtVal != 1)
+ return SDValue();
+ if (LHSIsSExt && RHSIsSExt)
+ Opc = ISD::AVGSUBFLOORS;
+ else if (LHSIsZExt && RHSIsZExt)
+ Opc = ISD::AVGSUBFLOORU;
+ else
+ return SDValue();
+ break;
case ISD::MUL:
// MULH*/MULHR*: shift amount must be element size, only for i16/i32. The
// exception is the RV64 v8i8 MULHSU which is built from multiply halves.
diff --git a/llvm/test/CodeGen/RISCV/rvp-avgsub.ll b/llvm/test/CodeGen/RISCV/rvp-avgsub.ll
index 21f20e9315453..345fd7665f001 100644
--- a/llvm/test/CodeGen/RISCV/rvp-avgsub.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-avgsub.ll
@@ -467,201 +467,3 @@ declare i32 @llvm.riscv.asub.i32(i32, i32)
declare i32 @llvm.riscv.asubu.u32(i32, i32)
declare <8 x i8> @llvm.riscv.pasub.v8i8(<8 x i8>, <8 x i8>)
declare <8 x i8> @llvm.riscv.pasubu.v8i8(<8 x i8>, <8 x i8>)
-
-; The operands are known to be extended from a narrower type, so the subtract
-; can't overflow in the narrower type. The unsigned
diff erence can be negative,
-; so the result is sign extended.
-
-define <4 x i16> @narrow_zero_bits_ashr(<4 x i16> %x, <4 x i16> %y) {
-; RV32-LABEL: narrow_zero_bits_ashr:
-; RV32: # %bb.0:
-; RV32-NEXT: pncvt.b a2, a2
-; RV32-NEXT: pncvt.b a0, a0
-; RV32-NEXT: pasubu.b a0, a0, a2
-; RV32-NEXT: pwcvt.b a0, a0
-; RV32-NEXT: ret
-;
-; RV64-LABEL: narrow_zero_bits_ashr:
-; RV64: # %bb.0:
-; RV64-NEXT: pasubu.b a0, a0, a1
-; RV64-NEXT: pzext.h.b a0, a0
-; RV64-NEXT: psext.h.b a0, a0
-; RV64-NEXT: ret
- %a = and <4 x i16> %x, splat (i16 255)
- %b = and <4 x i16> %y, splat (i16 255)
- %d = sub <4 x i16> %a, %b
- %r = ashr <4 x i16> %d, splat (i16 1)
- ret <4 x i16> %r
-}
-
-define <4 x i16> @narrow_sign_bits_ashr(<4 x i16> %x, <4 x i16> %y) {
-; RV32-LABEL: narrow_sign_bits_ashr:
-; RV32: # %bb.0:
-; RV32-NEXT: pncvt.b a2, a2
-; RV32-NEXT: pncvt.b a0, a0
-; RV32-NEXT: pasub.b a0, a0, a2
-; RV32-NEXT: pwcvt.b a0, a0
-; RV32-NEXT: ret
-;
-; RV64-LABEL: narrow_sign_bits_ashr:
-; RV64: # %bb.0:
-; RV64-NEXT: pasub.b a0, a0, a1
-; RV64-NEXT: pzext.h.b a0, a0
-; RV64-NEXT: psext.h.b a0, a0
-; RV64-NEXT: ret
- %a = shl <4 x i16> %x, splat (i16 8)
- %b = shl <4 x i16> %y, splat (i16 8)
- %a.sext = ashr <4 x i16> %a, splat (i16 8)
- %b.sext = ashr <4 x i16> %b, splat (i16 8)
- %d = sub <4 x i16> %a.sext, %b.sext
- %r = ashr <4 x i16> %d, splat (i16 1)
- ret <4 x i16> %r
-}
-
-; The sign bit of the lshr result isn't demanded by the truncate.
-
-define <4 x i8> @narrow_zero_bits_lshr_trunc(<4 x i16> %x, <4 x i16> %y) {
-; RV32-LABEL: narrow_zero_bits_lshr_trunc:
-; RV32: # %bb.0:
-; RV32-NEXT: pncvt.b a2, a2
-; RV32-NEXT: pncvt.b a0, a0
-; RV32-NEXT: pasubu.b a0, a0, a2
-; RV32-NEXT: ret
-;
-; RV64-LABEL: narrow_zero_bits_lshr_trunc:
-; RV64: # %bb.0:
-; RV64-NEXT: pasubu.b a0, a0, a1
-; RV64-NEXT: pncvt.wb a0, a0
-; RV64-NEXT: ret
- %a = and <4 x i16> %x, splat (i16 255)
- %b = and <4 x i16> %y, splat (i16 255)
- %d = sub <4 x i16> %a, %b
- %s = lshr <4 x i16> %d, splat (i16 1)
- %r = trunc <4 x i16> %s to <4 x i8>
- ret <4 x i8> %r
-}
-
-; The sign bit of the lshr result is demanded, so we can't use the narrow node.
-
-define <4 x i16> @narrow_zero_bits_lshr_no_trunc(<4 x i16> %x, <4 x i16> %y) {
-; RV32-LABEL: narrow_zero_bits_lshr_no_trunc:
-; RV32: # %bb.0:
-; RV32-NEXT: pzext.dh.b a0, a0
-; RV32-NEXT: pzext.dh.b a2, a2
-; RV32-NEXT: psub.dh a0, a0, a2
-; RV32-NEXT: psrli.dh a0, a0, 1
-; RV32-NEXT: ret
-;
-; RV64-LABEL: narrow_zero_bits_lshr_no_trunc:
-; RV64: # %bb.0:
-; RV64-NEXT: pzext.h.b a0, a0
-; RV64-NEXT: pzext.h.b a1, a1
-; RV64-NEXT: psub.h a0, a0, a1
-; RV64-NEXT: psrli.h a0, a0, 1
-; RV64-NEXT: ret
- %a = and <4 x i16> %x, splat (i16 255)
- %b = and <4 x i16> %y, splat (i16 255)
- %d = sub <4 x i16> %a, %b
- %r = lshr <4 x i16> %d, splat (i16 1)
- ret <4 x i16> %r
-}
-
-; Only 1 leading zero in the operands is enough for the unsigned node.
-
-define <4 x i16> @narrow_one_zero_bit_ashr(<4 x i16> %x, <4 x i16> %y) {
-; RV32-LABEL: narrow_one_zero_bit_ashr:
-; RV32: # %bb.0:
-; RV32-NEXT: lui a4, 8
-; RV32-NEXT: addi a4, a4, -1
-; RV32-NEXT: pmv.hs a4, a4
-; RV32-NEXT: and a1, a1, a4
-; RV32-NEXT: and a3, a3, a4
-; RV32-NEXT: and a0, a0, a4
-; RV32-NEXT: and a2, a2, a4
-; RV32-NEXT: pasubu.dh a0, a0, a2
-; RV32-NEXT: ret
-;
-; RV64-LABEL: narrow_one_zero_bit_ashr:
-; RV64: # %bb.0:
-; RV64-NEXT: lui a2, 8
-; RV64-NEXT: addi a2, a2, -1
-; RV64-NEXT: pmv.hs a2, a2
-; RV64-NEXT: and a0, a0, a2
-; RV64-NEXT: and a1, a1, a2
-; RV64-NEXT: pasubu.h a0, a0, a1
-; RV64-NEXT: ret
- %a = and <4 x i16> %x, splat (i16 32767)
- %b = and <4 x i16> %y, splat (i16 32767)
- %d = sub <4 x i16> %a, %b
- %r = ashr <4 x i16> %d, splat (i16 1)
- ret <4 x i16> %r
-}
-
-; Operands that aren't known to be extended can overflow.
-
-define <4 x i16> @no_narrow_ashr(<4 x i16> %x, <4 x i16> %y) {
-; RV32-LABEL: no_narrow_ashr:
-; RV32: # %bb.0:
-; RV32-NEXT: psub.dh a0, a0, a2
-; RV32-NEXT: psrai.dh a0, a0, 1
-; RV32-NEXT: ret
-;
-; RV64-LABEL: no_narrow_ashr:
-; RV64: # %bb.0:
-; RV64-NEXT: psub.h a0, a0, a1
-; RV64-NEXT: psrai.h a0, a0, 1
-; RV64-NEXT: ret
- %d = sub <4 x i16> %x, %y
- %r = ashr <4 x i16> %d, splat (i16 1)
- ret <4 x i16> %r
-}
-
-; Don't form an AVGSUBFLOOR node with a scalar constant operand unless it's
-; legal. i16 isn't legal for the scalar nodes. The constant is on the left
-; because (sub x, C) is canonicalized to (add x, -C).
-
-define i16 @narrow_scalar_constant(i16 %x) {
-; RV32-LABEL: narrow_scalar_constant:
-; RV32: # %bb.0:
-; RV32-NEXT: li a1, 100
-; RV32-NEXT: zext.b a0, a0
-; RV32-NEXT: asubu a0, a1, a0
-; RV32-NEXT: ret
-;
-; RV64-LABEL: narrow_scalar_constant:
-; RV64: # %bb.0:
-; RV64-NEXT: li a1, 100
-; RV64-NEXT: zext.b a0, a0
-; RV64-NEXT: sub a0, a1, a0
-; RV64-NEXT: srai a0, a0, 1
-; RV64-NEXT: ret
- %a = and i16 %x, 255
- %d = sub i16 100, %a
- %r = ashr i16 %d, 1
- ret i16 %r
-}
-
-; i8 and i16 aren't legal types so we don't narrow to them. On RV32 the
-; AVGSUBFLOORU node is created for i32, which is legal. It isn't legal on RV64.
-
-define i16 @narrow_scalar_illegal_type(i16 %x, i16 %y) {
-; RV32-LABEL: narrow_scalar_illegal_type:
-; RV32: # %bb.0:
-; RV32-NEXT: zext.b a0, a0
-; RV32-NEXT: zext.b a1, a1
-; RV32-NEXT: asubu a0, a0, a1
-; RV32-NEXT: ret
-;
-; RV64-LABEL: narrow_scalar_illegal_type:
-; RV64: # %bb.0:
-; RV64-NEXT: zext.b a0, a0
-; RV64-NEXT: zext.b a1, a1
-; RV64-NEXT: sub a0, a0, a1
-; RV64-NEXT: srai a0, a0, 1
-; RV64-NEXT: ret
- %a = and i16 %x, 255
- %b = and i16 %y, 255
- %d = sub i16 %a, %b
- %r = ashr i16 %d, 1
- ret i16 %r
-}
More information about the llvm-branch-commits
mailing list