https://gcc.gnu.org/g:0bf6e399dbc8e1f6d2b3f7e0f3a0cab9ac55b572

commit r17-3203-g0bf6e399dbc8e1f6d2b3f7e0f3a0cab9ac55b572
Author: Andrew MacLeod <[email protected]>
Date:   Wed Aug 5 14:05:33 2026 -0400

    Check for undefined in operator_lshift::op1_range.
    
    If we conclude that the op1_range calculation is undefined, simply
    abort the calculation.
    
            PR tree-optimization/126648
            gcc/
            * range-op.cc (operator_lshift::op1_range): Handle undefined.
    
            gcc/testsuite/
            * gcc.dg/pr126648.c: New.

Diff:
---
 gcc/range-op.cc                 |  4 ++++
 gcc/testsuite/gcc.dg/pr126648.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 07413ef70547..333ac748815c 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -2897,6 +2897,10 @@ operator_lshift::op1_range (irange &r,
       else
        op_rshift.fold_range (tmp_range, utype, lhs, op2);
 
+      // If no valid range is found, abort the calculation and return falae.
+      if (tmp_range.undefined_p ())
+       return false;
+
       // Start with ranges which can produce the LHS by right shifting the
       // result by the shift amount.
       // ie   [0x08, 0xF0] = op1 << 2 will start with
diff --git a/gcc/testsuite/gcc.dg/pr126648.c b/gcc/testsuite/gcc.dg/pr126648.c
new file mode 100644
index 000000000000..44272d20e44b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr126648.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-O3" } */
+long a;
+char b;
+short c;
+int f(int g) {
+  {
+    unsigned h = g;
+    {
+      long d = h;
+      int e = 0;
+      do {
+        if ((h & 15) == 4)
+          d = d + (h << 5);
+        if (d == 5)
+          break;
+        e = e + 1;
+      } while (e < 3);
+      c = d;
+    }
+  }
+  return c;
+}
+char k() {
+  long i;
+  unsigned j = b % 6u + 4;
+  while (a)
+    i = f(j + 826);
+  return i;
+}
+int main() {}

Reply via email to