]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fold-const: Fix BIT_INSERT_EXPR folding for BYTES_BIG_ENDIAN [PR116997]
authorAndre Vieira <andre.simoesdiasvieira@arm.com>
Mon, 14 Oct 2024 15:24:07 +0000 (16:24 +0100)
committerAndre Vieira <andre.simoesdiasvieira@arm.com>
Mon, 14 Oct 2024 15:27:58 +0000 (16:27 +0100)
Fix constant folding of BIT_INSER_EXPR for BYTES_BIG_ENDIAN targets.

gcc/ChangeLog:

PR middle-end/116997
* fold-const.cc (fold_ternary_loc): Fix BIT_INSERT_EXPR constant folding
for BYTES_BIG_ENDIAN targets.

gcc/testsuite/ChangeLog:

* gcc.dg/vect/pr116997.c: New test.

Co-authored-by: Andrew Pinski <quic_apinski@quicinc.com>
gcc/fold-const.cc
gcc/testsuite/gcc.dg/vect/pr116997.c [new file with mode: 0644]

index 0578f42ac0c51566efe5eb8f75e645518642728c..6f73f648b70c4947fc9f26741ce099f8b9de8022 100644 (file)
@@ -13712,6 +13712,8 @@ fold_ternary_loc (location_t loc, enum tree_code code, tree type,
        {
          unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
          unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
+         if (BYTES_BIG_ENDIAN)
+           bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
          wide_int tem = (wi::to_wide (arg0)
                          & wi::shifted_mask (bitpos, bitsize, true,
                                              TYPE_PRECISION (type)));
diff --git a/gcc/testsuite/gcc.dg/vect/pr116997.c b/gcc/testsuite/gcc.dg/vect/pr116997.c
new file mode 100644 (file)
index 0000000..4563fc2
--- /dev/null
@@ -0,0 +1,18 @@
+/* PR 116997.  */
+struct S0
+{
+  unsigned f0;
+  signed f2 : 11;
+  signed : 6;
+} GlobS, *Ptr = &GlobS;
+
+const struct S0 Initializer = {7, 3};
+
+int main (void)
+{
+  for (unsigned i = 0; i <= 2; i++)
+    *Ptr = Initializer;
+  if (GlobS.f2 != 3)
+    __builtin_abort ();
+  return 0;
+}