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/gimple-lower-bitint.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc
index 631a7844790..d80191c6761 100644
--- a/gcc/gimple-lower-bitint.cc
+++ b/gcc/gimple-lower-bitint.cc
@@ -3161,6 +3161,14 @@ 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);
-- 
2.46.0

Reply via email to