https://gcc.gnu.org/g:77ff7c2bb2240d64b8b03c87adc3d42957d59ae8

commit r16-6744-g77ff7c2bb2240d64b8b03c87adc3d42957d59ae8
Author: Jeff Law <[email protected]>
Date:   Tue Jan 13 07:16:05 2026 -0700

    [PR tree-optimization/123530] Fix ICE in recently added match.pd pattern
    
    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.

Diff:
---
 gcc/match.pd                            | 10 ++++++++--
 gcc/testsuite/gcc.dg/torture/pr123530.c |  6 ++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index f29f7638d667..cc33a972b982 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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
index 000000000000..1012270098bf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr123530.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+int a, b, c;
+void d() {
+  int e = -1;
+  (c >> e | ~(b << 1) ^ 1) & a;
+}

Reply via email to