A widening cast from a signed _BitInt operand to an unsigned _BitInt
type involves filling the extra limb(s) with sign extension.
On a target that wants _BitInts extended in memory, if this unsigned
type has a partial limb, the unused part of it should be zeroed.
e.g. Assuming limb_mode == E_DImode, at the end of
void
test (unsigned _BitInt(519) *t, _BitInt(512) x)
{
*t = -x;
}
the most significant limb of *t should be masked with 0x7f.
This patch also fixes gcc.dg/torture/bitint-16.c, which aborts at -O2
when the extension on load is optimized away.
gcc/ChangeLog:
* gimple-lower-bitint.cc (bitint_large_huge::lower_mergeable_stmt):
zero-extend the partial limb of any unsigned _BitInt LHS assigned
with a widening sign-extension.
gcc/testsuite/ChangeLog:
* gcc.dg/torture/bitint-83.c: New test.
---
gcc/gimple-lower-bitint.cc | 11 +++++
gcc/testsuite/gcc.dg/torture/bitint-83.c | 52 ++++++++++++++++++++++++
2 files changed, 63 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/torture/bitint-83.c
diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc
index eb2829b261e..edfe2aa8a23 100644
--- a/gcc/gimple-lower-bitint.cc
+++ b/gcc/gimple-lower-bitint.cc
@@ -3169,6 +3169,17 @@ bitint_large_huge::lower_mergeable_stmt (gimple *stmt,
tree_code &cmp_code,
{
tree l = limb_access (nlhs ? NULL_TREE : lhs_type,
nlhs ? nlhs : lhs, idx, true);
+
+ if (bitint_extended
+ && sext
+ && TYPE_UNSIGNED (lhs_type)
+ && tree_fits_uhwi_p (idx)
+ && !nlhs)
+ {
+ rhs1 = add_cast (limb_access_type (lhs_type, idx), rhs1);
+ rhs1 = add_cast (TREE_TYPE (l), rhs1);
+ }
+
g = gimple_build_assign (l, rhs1);
}
insert_before (g);
diff --git a/gcc/testsuite/gcc.dg/torture/bitint-83.c
b/gcc/testsuite/gcc.dg/torture/bitint-83.c
new file mode 100644
index 00000000000..b106dd36ef1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/bitint-83.c
@@ -0,0 +1,52 @@
+/* Derived from a test in gcc.dg/torture/bitint-16.c */
+/* { dg-do run { target bitint } } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */
+/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */
+
+#include "../bitintext.h"
+
+#define BASIC_TESTS \
+ TEST(8) \
+ TEST(16) \
+ TEST(32) \
+ TEST(64) \
+ TEST(128)
+
+#if __BITINT_MAXWIDTH__ >= 512
+#define __ALL_TESTS__ \
+ BASIC_TESTS \
+ TEST(256) \
+ TEST(512)
+#elif __BITINT_MAXWIDTH__ >= 256
+#define __ALL_TESTS__ \
+ BASIC_TESTS \
+ TEST(256)
+#else
+#define __ALL_TESTS__ BASIC_TESTS
+#endif
+
+#define TEST(N) \
+void \
+test##N (unsigned _BitInt(N + 7) *t, _BitInt(N) x) \
+{ \
+ *t = -x; \
+}
+__ALL_TESTS__
+#undef TEST
+
+volatile int y = 0;
+
+int
+main (void)
+{
+#define TEST(N) \
+ { \
+ unsigned _BitInt(N + 7) t; \
+ _BitInt(N) x = y + N; \
+ test##N (&t, x); \
+ BEXTC (t); \
+ }
+ __ALL_TESTS__
+#undef TEST
+}
--
2.46.0