]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/123537 - fix too permissive .REDUC_* folding
authorRichard Biener <rguenther@suse.de>
Wed, 28 Jan 2026 09:04:45 +0000 (10:04 +0100)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 28 Jan 2026 11:52:30 +0000 (12:52 +0100)
The following restricts the allowed conversions to those maintaining
the element type size.

PR tree-optimization/123537
* match.pd (REDUC (@0 & @1) -> @0[I] & @1[I]): Restrict
allowed conversions.

* gcc.dg/pr123537.c: New testcase.

gcc/match.pd
gcc/testsuite/gcc.dg/pr123537.c [new file with mode: 0644]

index e94e474bdc5763166189e209023e9cf5ad87dc83..01b95ec075538fcd36d1a2dcc8abfa728e2c582d 100644 (file)
@@ -11415,10 +11415,11 @@ and,
            unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
            tree size = bitsize_int (elt_bits);
            tree pos = bitsize_int (elt_bits * i); }
-     (view_convert
-      (bit_and:elt_type
-       (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
-       { elt; })))))))
+     (if (compare_tree_int (TYPE_SIZE (type), elt_bits) == 0)
+      (view_convert
+       (bit_and:elt_type
+        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
+        { elt; }))))))))
 
 /* Fold reduction of a single nonzero element constructor.  */
 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
diff --git a/gcc/testsuite/gcc.dg/pr123537.c b/gcc/testsuite/gcc.dg/pr123537.c
new file mode 100644 (file)
index 0000000..ff17835
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target int128 } */
+/* { dg-options "-O2" } */
+
+typedef __attribute__((__vector_size__(16))) __int128 V;
+
+union {
+  _Complex long c;
+  V v;
+} u;
+
+__int128 j;
+int i;
+
+void
+foo()
+{
+  u.v &= 4;
+  i %= u.c ? 3 : j;
+}