diff mbox series

fold-const: Fix BIT_INSERT_EXPR folding for BYTES_BIG_ENDIAN [PR116997]

Message ID 98276dae-6589-4af9-bc5f-76cfd80b1a0d@arm.com
State New
Headers show
Series fold-const: Fix BIT_INSERT_EXPR folding for BYTES_BIG_ENDIAN [PR116997] | expand

Commit Message

Andre Vieira (lists) Oct. 14, 2024, 11:13 a.m. UTC
Hi,

This patch fixes constant folding of BIT_INSER_EXPR for BYTES_BIG_ENDIAN 
targets.

Regression tested on aarch64be-none-elf.

Almost committed this as obvious, but I wanted to double check the 
testcase with a maintainer. I decided to not make the test be big-endian 
specific, nor to add any specific checks, since before this patch it 
would abort on a big-endian target and fail an execution test.  Just 
thought that anything else might end up being somewhat sensitive to 
testisms.

OK for trunk?


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-author: Andrew Pinski <quic_apinski@quicinc.com>

Comments

Richard Biener Oct. 14, 2024, 12:12 p.m. UTC | #1
On Mon, 14 Oct 2024, Andre Vieira (lists) wrote:

> Hi,
> 
> This patch fixes constant folding of BIT_INSER_EXPR for BYTES_BIG_ENDIAN
> targets.
> 
> Regression tested on aarch64be-none-elf.
> 
> Almost committed this as obvious, but I wanted to double check the testcase
> with a maintainer. I decided to not make the test be big-endian specific, nor
> to add any specific checks, since before this patch it would abort on a
> big-endian target and fail an execution test.  Just thought that anything else
> might end up being somewhat sensitive to testisms.
> 
> OK for trunk?

OK for trunk and backports.

Thanks,
Richard.

> 
> 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-author: Andrew Pinski <quic_apinski@quicinc.com>
>
diff mbox series

Patch

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 0578f42ac0c51566efe5eb8f75e645518642728c..6f73f648b70c4947fc9f26741ce099f8b9de8022 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -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
index 0000000000000000000000000000000000000000..4563fc2bfb6f86d4e8adc7160b71760003479c9e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr116997.c
@@ -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;
+}