[llvm] d2c5892 - [SelectionDAG] Add TRUNCATE_SSAT_S/U and TRUNCATE_USAT_U to canCreateUndefOrPoison and computeKnownBits (#152143) (#168809)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 19 02:25:13 PST 2026
Author: Jerry Dang
Date: 2026-01-19T10:25:08Z
New Revision: d2c5892c226342bd11341e5bd75c8b5152bde9a9
URL: https://github.com/llvm/llvm-project/commit/d2c5892c226342bd11341e5bd75c8b5152bde9a9
DIFF: https://github.com/llvm/llvm-project/commit/d2c5892c226342bd11341e5bd75c8b5152bde9a9.diff
LOG: [SelectionDAG] Add TRUNCATE_SSAT_S/U and TRUNCATE_USAT_U to canCreateUndefOrPoison and computeKnownBits (#152143) (#168809)
1. Implement `SelectionDAG::computeKnownBits` for TRUNCATE_SSAT_S/U and
TRUNCATE_USAT_U
2. Saturating truncation operations are well-defined for all inputs and
cannot create poison or undef values. This allows the optimizer to
eliminate unnecessary freeze instructions after these operations.
Fixes #152143
Added:
llvm/test/CodeGen/AArch64/sat-trunc-knownbits.ll
Modified:
llvm/include/llvm/Support/KnownBits.h
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
llvm/lib/Support/KnownBits.cpp
llvm/unittests/Support/KnownBitsTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index b9bde435d2ee5..929f6bcc2f919 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -214,6 +214,16 @@ struct KnownBits {
return *this;
}
+ /// Truncate with signed saturation (signed input -> signed output)
+ LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const;
+
+ /// Truncate with signed saturation to unsigned (signed input -> unsigned
+ /// output)
+ LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const;
+
+ /// Truncate with unsigned saturation (unsigned input -> unsigned output)
+ LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const;
+
/// Return known bits for a in-register sign extension of the value we're
/// tracking.
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const;
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 0692e8e7e83e3..a6e1889f49ac4 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4047,6 +4047,21 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
Known = Known.trunc(BitWidth);
break;
}
+ case ISD::TRUNCATE_SSAT_S: {
+ Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+ Known = Known.truncSSat(BitWidth);
+ break;
+ }
+ case ISD::TRUNCATE_SSAT_U: {
+ Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+ Known = Known.truncSSatU(BitWidth);
+ break;
+ }
+ case ISD::TRUNCATE_USAT_U: {
+ Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+ Known = Known.truncUSat(BitWidth);
+ break;
+ }
case ISD::AssertZext: {
EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
@@ -5755,6 +5770,9 @@ bool SelectionDAG::canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts,
case ISD::FP_EXTEND:
case ISD::FP_TO_SINT_SAT:
case ISD::FP_TO_UINT_SAT:
+ case ISD::TRUNCATE_SSAT_S:
+ case ISD::TRUNCATE_SSAT_U:
+ case ISD::TRUNCATE_USAT_U:
// No poison except from flags (which is handled above)
return false;
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index c7eb37808d166..d8f0f7c1686f9 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -155,6 +155,127 @@ KnownBits KnownBits::computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
/*CarryOne=*/Borrow.Zero.getBoolValue());
}
+KnownBits KnownBits::truncSSat(unsigned BitWidth) const {
+ unsigned InputBits = getBitWidth();
+ APInt MinInRange = APInt::getSignedMinValue(BitWidth).sext(InputBits);
+ APInt MaxInRange = APInt::getSignedMaxValue(BitWidth).sext(InputBits);
+ APInt InputMin = getSignedMinValue();
+ APInt InputMax = getSignedMaxValue();
+ KnownBits Known(BitWidth);
+
+ // Case 1: All values fit - just truncate
+ if (InputMin.sge(MinInRange) && InputMax.sle(MaxInRange)) {
+ Known = trunc(BitWidth);
+ }
+ // Case 2: All saturate to min
+ else if (InputMax.slt(MinInRange)) {
+ Known = KnownBits::makeConstant(APInt::getSignedMinValue(BitWidth));
+ }
+ // Case 3: All saturate to max
+ else if (InputMin.sgt(MaxInRange)) {
+ Known = KnownBits::makeConstant(APInt::getSignedMaxValue(BitWidth));
+ }
+ // Case 4: All non-negative, some fit, some saturate to max
+ else if (InputMin.isNonNegative()) {
+ // Output: truncated OR max saturation
+ // Max saturation has only sign bit as 0
+ Known.Zero = APInt(BitWidth, 0);
+ Known.Zero.setBit(BitWidth - 1); // Sign bit always 0
+ // Max saturation has all lower bits as 1, so preserve InputOneLower
+ Known.One = One.trunc(BitWidth);
+ Known.One.clearBit(BitWidth - 1); // Sign bit is 0, not 1
+ }
+ // Case 5: All negative, some fit, some saturate to min
+ else if (InputMax.isNegative()) {
+ // Output: truncated OR min saturation
+ // Min saturation has all lower bits as 0, so preserve InputZeroLower
+ Known.Zero = Zero.trunc(BitWidth);
+ Known.Zero.clearBit(BitWidth - 1); // Sign bit is 1, not 0
+ // Min saturation has only sign bit as 1
+ Known.One = APInt(BitWidth, 0);
+ Known.One.setBit(BitWidth - 1); // Sign bit always 1
+ }
+ // Case 6: Mixed positive and negative
+ else {
+ // Output: min saturation, truncated, or max saturation
+ APInt InputZeroLower = Zero.trunc(BitWidth);
+ APInt InputOneLower = One.trunc(BitWidth);
+ APInt MinSat = APInt::getSignedMinValue(BitWidth);
+ APInt MaxSat = APInt::getSignedMaxValue(BitWidth);
+ KnownBits MinSatKB = KnownBits::makeConstant(MinSat);
+ KnownBits MaxSatKB = KnownBits::makeConstant(MaxSat);
+ if (InputMax.sle(MaxInRange)) {
+ // Positive values fit, only negatives saturate to min
+ Known.Zero = InputZeroLower & MinSatKB.Zero;
+ Known.One = InputOneLower & MinSatKB.One;
+ } else if (InputMin.sge(MinInRange)) {
+ // Negative values fit, only positives saturate to max
+ Known.Zero = InputZeroLower & MaxSatKB.Zero;
+ Known.One = InputOneLower & MaxSatKB.One;
+ } else {
+ // Both positive and negative values might saturate
+ Known.Zero = InputZeroLower & MinSatKB.Zero & MaxSatKB.Zero;
+ Known.One = InputOneLower & MinSatKB.One & MaxSatKB.One;
+ }
+ }
+ return Known;
+}
+
+KnownBits KnownBits::truncSSatU(unsigned BitWidth) const {
+ unsigned InputBits = getBitWidth();
+ APInt MaxInRange = APInt::getAllOnes(BitWidth).zext(InputBits);
+ APInt InputMin = getSignedMinValue();
+ APInt InputMax = getSignedMaxValue();
+ KnownBits Known(BitWidth);
+
+ if (isNegative()) {
+ Known.setAllZero();
+ } else if (InputMin.isNonNegative() && InputMax.ule(MaxInRange)) {
+ Known = trunc(BitWidth);
+ } else if (InputMin.isNonNegative() && InputMin.ugt(MaxInRange)) {
+ Known.setAllOnes();
+ } else if (InputMin.isNonNegative()) {
+ // All non-negative but mixed: some fit, some saturate to all-ones
+ // No common zero bits (saturation is all-ones)
+ Known.Zero = APInt(BitWidth, 0);
+ // Common one bits: bits that are 1 in input stay 1
+ Known.One = One.trunc(BitWidth);
+ } else {
+ // Mixed: sign bit unknown
+ if (InputMax.ule(MaxInRange)) {
+ // Positive values all fit (no saturation to all-ones)
+ // Output: all-zeros (negatives) OR truncated (positives)
+ Known.Zero = Zero.trunc(BitWidth);
+ Known.One = APInt(BitWidth, 0);
+ } else {
+ // Positive values might saturate to all-ones
+ // Output: all-zeros OR truncated OR all-ones
+ // No common bits
+ Known.Zero = APInt(BitWidth, 0);
+ Known.One = APInt(BitWidth, 0);
+ }
+ }
+ return Known;
+}
+
+KnownBits KnownBits::truncUSat(unsigned BitWidth) const {
+ unsigned InputBits = getBitWidth();
+ APInt MaxInRange = APInt::getLowBitsSet(InputBits, BitWidth);
+ APInt InputMax = getMaxValue();
+ APInt InputMin = getMinValue();
+ KnownBits Known(BitWidth);
+
+ if (InputMax.ule(MaxInRange)) {
+ Known = trunc(BitWidth);
+ } else if (InputMin.ugt(MaxInRange)) {
+ Known.setAllOnes();
+ } else {
+ Known.resetAll();
+ Known.One = One.trunc(BitWidth);
+ }
+ return Known;
+}
+
KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const {
unsigned BitWidth = getBitWidth();
assert(0 < SrcBitWidth && SrcBitWidth <= BitWidth &&
diff --git a/llvm/test/CodeGen/AArch64/sat-trunc-knownbits.ll b/llvm/test/CodeGen/AArch64/sat-trunc-knownbits.ll
new file mode 100644
index 0000000000000..bd4db8f2c8d36
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/sat-trunc-knownbits.ll
@@ -0,0 +1,128 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=aarch64 | FileCheck %s
+
+;; ============================================================================
+;; Tests for canCreateUndefOrPoison = false
+;; These verify that freeze operations are correctly eliminated
+;; ============================================================================
+
+; TRUNCATE_SSAT_S: No saturation path
+define i1 @sqxtn_no_sat_with_freeze(<4 x i32> %x) {
+; CHECK-LABEL: sqxtn_no_sat_with_freeze:
+; CHECK: // %bb.0:
+; CHECK-NEXT: movi v1.4s, #100
+; CHECK-NEXT: and v0.16b, v0.16b, v1.16b
+; CHECK-NEXT: sqxtn v0.4h, v0.4s
+; CHECK-NEXT: umov w8, v0.h[0]
+; CHECK-NEXT: and w8, w8, #0xfffc
+; CHECK-NEXT: cmp w8, #200
+; CHECK-NEXT: cset w0, gt
+; CHECK-NEXT: ret
+ %masked = and <4 x i32> %x, <i32 100, i32 100, i32 100, i32 100>
+ %trunc = call <4 x i16> @llvm.aarch64.neon.sqxtn.v4i16(<4 x i32> %masked)
+ %freeze = freeze <4 x i16> %trunc
+ %extract = extractelement <4 x i16> %freeze, i32 0
+ ; Input is [0,100], so result > 200 is always false
+ %cmp = icmp sgt i16 %extract, 200
+ ret i1 %cmp
+}
+
+; TRUNCATE_SSAT_S: Test specific known bits
+define i16 @sqxtn_known_bits(<4 x i32> %x) {
+; CHECK-LABEL: sqxtn_known_bits:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w0, wzr
+; CHECK-NEXT: ret
+ ; Input: [0, 32512] fits in i16 without saturation
+ %masked = and <4 x i32> %x, <i32 32512, i32 32512, i32 32512, i32 32512>
+ %trunc = call <4 x i16> @llvm.aarch64.neon.sqxtn.v4i16(<4 x i32> %masked)
+ %freeze = freeze <4 x i16> %trunc
+ %extract = extractelement <4 x i16> %freeze, i32 0
+ ; Mask to lower 7 bits - with KnownBits, knows upper bits are already 0
+ ; so this AND can be simplified
+ %and = and i16 %extract, 127
+ ret i16 %and
+}
+
+;; ============================================================================
+;; Tests for computeKnownBits
+;; These verify that known bits analysis enables optimizations
+;; TRUNCATE_SSAT_S: Signed to Signed Saturating Truncate (sqxtn)
+;; ============================================================================
+
+; Constant 32512 = 0b0111111110000000 has known zero lower bits
+; After truncation, ANDing with 127 (lower 7 bits) gives 0
+define i16 @sqxtn_known_zero_bits(<4 x i32> %x) {
+; CHECK-LABEL: sqxtn_known_zero_bits:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w0, wzr
+; CHECK-NEXT: ret
+ %masked = and <4 x i32> %x, <i32 32512, i32 32512, i32 32512, i32 32512>
+ %trunc = call <4 x i16> @llvm.aarch64.neon.sqxtn.v4i16(<4 x i32> %masked)
+ %extract = extractelement <4 x i16> %trunc, i32 0
+ %and = and i16 %extract, 127
+ ret i16 %and
+}
+
+; Input range [0, 256] means result is always non-negative
+; Comparison with negative value should fold to false (returns 0)
+define i1 @sqxtn_known_nonnegative(<4 x i32> %x) {
+; CHECK-LABEL: sqxtn_known_nonnegative:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w0, wzr
+; CHECK-NEXT: ret
+ %masked = and <4 x i32> %x, <i32 256, i32 256, i32 256, i32 256>
+ %trunc = call <4 x i16> @llvm.aarch64.neon.sqxtn.v4i16(<4 x i32> %masked)
+ %extract = extractelement <4 x i16> %trunc, i32 0
+ %cmp = icmp slt i16 %extract, 0
+ ret i1 %cmp
+}
+
+;; ============================================================================
+;; TRUNCATE_SSAT_U: Signed to Unsigned Saturating Truncate (sqxtun)
+;; ============================================================================
+
+define i16 @sqxtun_known_zero_lower_bits(<4 x i32> %x) {
+; CHECK-LABEL: sqxtun_known_zero_lower_bits:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w0, wzr
+; CHECK-NEXT: ret
+ %masked = and <4 x i32> %x, <i32 32512, i32 32512, i32 32512, i32 32512>
+ %trunc = call <4 x i16> @llvm.aarch64.neon.sqxtun.v4i16(<4 x i32> %masked)
+ %extract = extractelement <4 x i16> %trunc, i32 0
+ %and = and i16 %extract, 255
+ ret i16 %and
+}
+
+define i16 @sqxtun_known_zero_upper_bits(<4 x i32> %x) {
+; CHECK-LABEL: sqxtun_known_zero_upper_bits:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w0, wzr
+; CHECK-NEXT: ret
+ %masked = and <4 x i32> %x, <i32 127, i32 127, i32 127, i32 127>
+ %trunc = call <4 x i16> @llvm.aarch64.neon.sqxtun.v4i16(<4 x i32> %masked)
+ %extract = extractelement <4 x i16> %trunc, i32 0
+ %shift = lshr i16 %extract, 7
+ ret i16 %shift
+}
+
+;; ============================================================================
+;; TRUNCATE_USAT_U: Unsigned to Unsigned Saturating Truncate (uqxtn)
+;; ============================================================================
+
+define i8 @uqxtn_known_zero_upper_bits(<8 x i16> %x) {
+; CHECK-LABEL: uqxtn_known_zero_upper_bits:
+; CHECK: // %bb.0:
+; CHECK-NEXT: mov w0, wzr
+; CHECK-NEXT: ret
+ %masked = and <8 x i16> %x, <i16 63, i16 63, i16 63, i16 63, i16 63, i16 63, i16 63, i16 63>
+ %trunc = call <8 x i8> @llvm.aarch64.neon.uqxtn.v8i8(<8 x i16> %masked)
+ %extract = extractelement <8 x i8> %trunc, i32 0
+ %shift = lshr i8 %extract, 6
+ ret i8 %shift
+}
+
+
+declare <4 x i16> @llvm.aarch64.neon.sqxtn.v4i16(<4 x i32>)
+declare <4 x i16> @llvm.aarch64.neon.sqxtun.v4i16(<4 x i32>)
+declare <8 x i8> @llvm.aarch64.neon.uqxtn.v8i8(<8 x i16>)
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 8da300a947b1d..af6854c2e3895 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -913,11 +913,76 @@ TEST(KnownBitsTest, ReduceAddExhaustive) {
EnumerateCombinations(0, APInt(Bits, 0));
if (!Exact.hasConflict()) {
- EXPECT_TRUE(checkResult("reduceAdd", Exact, Computed, {EltKnown},
- /*CheckOptimality=*/false));
+ EXPECT_TRUE(
+ checkResult("reduceAdd", Exact, Computed, {EltKnown}, false));
}
});
}
}
+TEST(KnownBitsTest, TruncateSatExhaustive) {
+ for (unsigned FromBits : {4, 8}) {
+ for (unsigned ToBits = 1; ToBits < FromBits; ++ToBits) {
+ ForeachKnownBits(FromBits, [&](const KnownBits &Known) {
+ // Test truncSSat
+ {
+ KnownBits Computed = Known.truncSSat(ToBits);
+ KnownBits Exact(ToBits);
+ Exact.Zero.setAllBits();
+ Exact.One.setAllBits();
+
+ ForeachNumInKnownBits(Known, [&](const APInt &N) {
+ APInt Res = N.truncSSat(ToBits);
+ Exact.One &= Res;
+ Exact.Zero &= ~Res;
+ });
+
+ if (!Exact.hasConflict()) {
+ EXPECT_TRUE(
+ checkResult("truncSSat", Exact, Computed, {Known}, true));
+ }
+ }
+
+ // Test truncSSatU
+ {
+ KnownBits Computed = Known.truncSSatU(ToBits);
+ KnownBits Exact(ToBits);
+ Exact.Zero.setAllBits();
+ Exact.One.setAllBits();
+
+ ForeachNumInKnownBits(Known, [&](const APInt &N) {
+ APInt Res = N.truncSSatU(ToBits);
+ Exact.One &= Res;
+ Exact.Zero &= ~Res;
+ });
+
+ if (!Exact.hasConflict()) {
+ EXPECT_TRUE(
+ checkResult("truncSSatU", Exact, Computed, {Known}, true));
+ }
+ }
+
+ // Test truncUSat
+ {
+ KnownBits Computed = Known.truncUSat(ToBits);
+ KnownBits Exact(ToBits);
+ Exact.Zero.setAllBits();
+ Exact.One.setAllBits();
+
+ ForeachNumInKnownBits(Known, [&](const APInt &N) {
+ APInt Res = N.truncUSat(ToBits);
+ Exact.One &= Res;
+ Exact.Zero &= ~Res;
+ });
+
+ if (!Exact.hasConflict()) {
+ EXPECT_TRUE(
+ checkResult("truncUSat", Exact, Computed, {Known}, true));
+ }
+ }
+ });
+ }
+ }
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list