]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PR tree-optimization/123530] Fix ICE in recently added match.pd pattern
authorJeff Law <jeffrey.law@oss.qualcomm.com>
Tue, 13 Jan 2026 14:16:05 +0000 (07:16 -0700)
committerJeff Law <jeffrey.law@oss.qualcomm.com>
Tue, 13 Jan 2026 14:16:05 +0000 (07:16 -0700)
The gimple optimization passes can create negative shift counts and pass them
into the simplification routines as seen by the code in pr123530.  If we then
call tree_to_uhwi on those values we get a nice little ICE.

This guards the tree_to_uhwi calls on tree_fits_uhwi_p and resolves the ICE.  I
just protected them all in this recently added pattern.

Bootstrapped and regression tested on x86 and riscv.  Also tested on the rest
of the embedded targets without any regressions.

Pushing to the trunk.

PR tree-optimization/123530
gcc/
* match.pd (reassociating xor to enable rotations): Verify constants
fit into a uhwi before trying to extract them as a uhwi.

gcc/testsuite/
* gcc.dg/torture/pr123530.c: New test.

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

index f29f7638d6678660aac10c0cb1e30fed8dd95016..cc33a972b982eba7018e8c48fae9c741f675a221 100644 (file)
@@ -12190,7 +12190,10 @@ and,
 (simplify
   (bit_ior:c (lshift @0 INTEGER_CST@1)
             (bit_xor (rshift @2 INTEGER_CST@3) INTEGER_CST@4))
-   (if (((~((HOST_WIDE_INT_1U << tree_to_uhwi (@1)) - 1)) & tree_to_uhwi (@4)) == 0
+   (if (tree_fits_uhwi_p (@1)
+       && tree_fits_uhwi_p (@3)
+       && tree_fits_uhwi_p (@4)
+       && ((~((HOST_WIDE_INT_1U << tree_to_uhwi (@1)) - 1)) & tree_to_uhwi (@4)) == 0
         && (tree_to_uhwi (@1) + tree_to_uhwi (@3)) == TYPE_PRECISION (type)
         && TYPE_UNSIGNED (type)
         && @0 == @2)
@@ -12201,7 +12204,10 @@ and,
 (simplify
   (bit_ior:c (bit_xor (lshift @0 INTEGER_CST@1) INTEGER_CST@2)
             (rshift @3 INTEGER_CST@4))
-   (if ((((((HOST_WIDE_INT_1U << tree_to_uhwi (@1)) - 1)) & tree_to_uhwi (@2)) == 0)
+   (if (tree_fits_uhwi_p (@1)
+       && tree_fits_uhwi_p (@2)
+       && tree_fits_uhwi_p (@4)
+       && (((((HOST_WIDE_INT_1U << tree_to_uhwi (@1)) - 1)) & tree_to_uhwi (@2)) == 0)
         && (tree_to_uhwi (@1) + tree_to_uhwi (@4)) == TYPE_PRECISION (type)
         && TYPE_UNSIGNED (type)
         && @0 == @3)
diff --git a/gcc/testsuite/gcc.dg/torture/pr123530.c b/gcc/testsuite/gcc.dg/torture/pr123530.c
new file mode 100644 (file)
index 0000000..1012270
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+int a, b, c;
+void d() {
+  int e = -1;
+  (c >> e | ~(b << 1) ^ 1) & a;
+}