[clang] 8784816 - [ADT] Add APInt::truncSSatU truncation from signed to unsigned variant (#175603)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 13 09:51:12 PST 2026
Author: Simon Pilgrim
Date: 2026-01-13T17:51:07Z
New Revision: 8784816a419069b68aa0f798dc882d70e0176fdd
URL: https://github.com/llvm/llvm-project/commit/8784816a419069b68aa0f798dc882d70e0176fdd
DIFF: https://github.com/llvm/llvm-project/commit/8784816a419069b68aa0f798dc882d70e0176fdd.diff
LOG: [ADT] Add APInt::truncSSatU truncation from signed to unsigned variant (#175603)
This matches the behaviour of ISD::TRUNCATE_SSAT_U and X86ISD::PACKUS
truncations: saturate signed input to unsigned result -
`truncate(smin(smax(x, 0), C))`
Add unit test coverage and update existing PACKUS constant folding to
use the APInt helper
Added:
Modified:
clang/lib/AST/ByteCode/InterpBuiltin.cpp
clang/lib/AST/ExprConstant.cpp
llvm/include/llvm/ADT/APInt.h
llvm/lib/Support/APInt.cpp
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/unittests/ADT/APIntTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 1975349051781..76368b7c8c62f 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -4817,12 +4817,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
case clang::X86::BI__builtin_ia32_packuswb256:
case clang::X86::BI__builtin_ia32_packuswb512:
return interp__builtin_x86_pack(S, OpPC, Call, [](const APSInt &Src) {
- unsigned DstBits = Src.getBitWidth() / 2;
- if (Src.isNegative())
- return APInt::getZero(DstBits);
- if (Src.isIntN(DstBits))
- return APInt(Src).trunc(DstBits);
- return APInt::getAllOnes(DstBits);
+ return APInt(Src).truncSSatU(Src.getBitWidth() / 2);
});
case clang::X86::BI__builtin_ia32_selectss_128:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index ec7ca893fb50e..c91261988434e 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12665,12 +12665,7 @@ bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
case X86::BI__builtin_ia32_packuswb256:
case X86::BI__builtin_ia32_packuswb512:
return evalPackBuiltin(E, Info, Result, [](const APSInt &Src) {
- unsigned DstBits = Src.getBitWidth() / 2;
- if (Src.isNegative())
- return APInt::getZero(DstBits);
- if (Src.isIntN(DstBits))
- return APInt((Src).trunc(DstBits));
- return APInt::getAllOnes(DstBits);
+ return APSInt(Src).truncSSatU(Src.getBitWidth() / 2);
});
case clang::X86::BI__builtin_ia32_selectss_128:
return EvalSelectScalar(4);
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 7e73cc1957c05..9193b5f8994e0 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -1278,13 +1278,21 @@ class [[nodiscard]] APInt {
/// the new bitwidth, then return truncated APInt. Else, return max value.
LLVM_ABI APInt truncUSat(unsigned width) const;
- /// Truncate to new width with signed saturation.
+ /// Truncate to new width with signed saturation to signed result.
///
/// If this APInt, treated as signed integer, can be losslessly truncated to
/// the new bitwidth, then return truncated APInt. Else, return either
/// signed min value if the APInt was negative, or signed max value.
LLVM_ABI APInt truncSSat(unsigned width) const;
+ /// Truncate to new width with signed saturation to unsigned result.
+ ///
+ /// If this APInt, treated as signed integer, can be losslessly truncated to
+ /// the new bitwidth, then return truncated APInt. Else, return either
+ /// zero if the APInt was negative, or unsigned max value.
+ /// If \p width matches the current bit width then no changes are made.
+ LLVM_ABI APInt truncSSatU(unsigned width) const;
+
/// Sign extend to a new width.
///
/// This operation sign extends the APInt to a new width. If the high order
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 673cd867f0e45..c35abf725dbe0 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -969,7 +969,7 @@ APInt APInt::truncUSat(unsigned width) const {
return APInt::getMaxValue(width);
}
-// Truncate to new width with signed saturation.
+// Truncate to new width with signed saturation to signed result.
APInt APInt::truncSSat(unsigned width) const {
assert(width <= BitWidth && "Invalid APInt Truncate request");
@@ -981,6 +981,17 @@ APInt APInt::truncSSat(unsigned width) const {
: APInt::getSignedMaxValue(width);
}
+// Truncate to new width with signed saturation to unsigned result.
+APInt APInt::truncSSatU(unsigned width) const {
+ assert(width <= BitWidth && "Invalid APInt Truncate request");
+
+ // Can we just losslessly truncate it?
+ if (isIntN(width))
+ return trunc(width);
+ // If not, then just return the new limits.
+ return isNegative() ? APInt::getZero(width) : APInt::getMaxValue(width);
+}
+
// Sign extend to a new width.
APInt APInt::sext(unsigned Width) const {
assert(Width >= BitWidth && "Invalid APInt SignExtend request");
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 03cb7908b4881..7f64255e23a59 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -50713,12 +50713,7 @@ static SDValue combineVectorPack(SDNode *N, SelectionDAG &DAG,
// Source values less than zero are saturated to zero.
// Source values greater than dst maxuint are saturated to maxuint.
// NOTE: This is
diff erent from APInt::truncUSat.
- if (Val.isIntN(DstBitsPerElt))
- Val = Val.trunc(DstBitsPerElt);
- else if (Val.isNegative())
- Val = APInt::getZero(DstBitsPerElt);
- else
- Val = APInt::getAllOnes(DstBitsPerElt);
+ Val = Val.truncSSatU(DstBitsPerElt);
}
Bits[Lane * NumDstEltsPerLane + Elt] = Val;
}
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 4cb537da72e87..271d17cb29905 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -1269,11 +1269,21 @@ TEST(APIntTest, SaturatingMath) {
EXPECT_EQ(APInt(6, 31), AP_42.truncSSat(6));
EXPECT_EQ(APInt(5, 15), AP_42.truncSSat(5));
+ EXPECT_EQ(APInt(8, 42, false), AP_42.truncSSatU(8));
+ EXPECT_EQ(APInt(7, 42, false), AP_42.truncSSatU(7));
+ EXPECT_EQ(APInt(6, 42, false), AP_42.truncSSatU(6));
+ EXPECT_EQ(APInt(5, 31, false), AP_42.truncSSatU(5));
+
EXPECT_EQ(APInt(8, -56, true), AP_200.truncSSat(8));
EXPECT_EQ(APInt(7, -56, true), AP_200.truncSSat(7));
EXPECT_EQ(APInt(6, -32, true), AP_200.truncSSat(6));
EXPECT_EQ(APInt(5, -16, true), AP_200.truncSSat(5));
+ EXPECT_EQ(APInt(8, 200, false), AP_200.truncSSatU(8));
+ EXPECT_EQ(APInt(7, 0, false), AP_200.truncSSatU(7));
+ EXPECT_EQ(APInt(6, 0, false), AP_200.truncSSatU(6));
+ EXPECT_EQ(APInt(5, 0, false), AP_200.truncSSatU(5));
+
EXPECT_EQ(APInt(8, 200), AP_100.uadd_sat(AP_100));
EXPECT_EQ(APInt(8, 255), AP_100.uadd_sat(AP_200));
EXPECT_EQ(APInt(8, 255), APInt(8, 255).uadd_sat(APInt(8, 255)));
More information about the cfe-commits
mailing list