https://gcc.gnu.org/g:29413f3786a0c83e29f649568ae94017961b9f30

commit r17-6350-g29413f3786a0c83e29f649568ae94017961b9f30
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Nov 12 01:50:56 2026 -0800

    if-to-switch: Do not merge IORs of anti-ranges [PR128588]
    
    find_conditions handles "_3 = a | b; if (_3 != 0)" by recording one range
    per IOR operand.  It checks that both operands test the same SSA name with
    the same IN_P flag.  When IN_P is false, both operands are anti-ranges and
    (x not in R1) | (x not in R2) is the complement of the intersection of R1
    and R2, not of their union.  The pass takes the false edge as the switch
    target and emits one case label per range, so the labels cover the union
    and some values reach the wrong edge.
    
    For
      _Bool a = (unsigned) x - 113u > 2u;
      _Bool b = x != 120;
      if (a | b) return 8;
      return 9;
    the condition is true for every x, but the generated switch has
    case 113 ... 115 and case 120 going to the "return 9" block.
    
    Only accept an IOR of several ranges when the ranges are IN_P.
    
    Bootstrapped and tested on aarch64-none-linux-gnu.
    
    gcc/ChangeLog:
    
            PR tree-optimization/128588
            * gimple-if-to-switch.cc (find_conditions): Reject an IOR of
            anti-ranges.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/128588
            * gcc.dg/torture/pr128588.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/gimple-if-to-switch.cc              |  6 ++++++
 gcc/testsuite/gcc.dg/torture/pr128588.c | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/gcc/gimple-if-to-switch.cc b/gcc/gimple-if-to-switch.cc
index f536d2ebab1a..e632a10456a0 100644
--- a/gcc/gimple-if-to-switch.cc
+++ b/gcc/gimple-if-to-switch.cc
@@ -444,6 +444,12 @@ find_conditions (basic_block bb,
            || info->m_ranges[i].in_p != in_p)
          goto exit;
 
+      /* The IOR of several ranges is the union of the ranges only when
+        each range is an IN_P range.  The IOR of anti-ranges is the
+        complement of their intersection.  */
+      if (info->m_ranges.length () > 1 && !in_p)
+       goto exit;
+
       info->record_phi_mapping (info->m_true_edge,
                                &info->m_true_edge_phi_mapping);
       info->record_phi_mapping (info->m_false_edge,
diff --git a/gcc/testsuite/gcc.dg/torture/pr128588.c 
b/gcc/testsuite/gcc.dg/torture/pr128588.c
new file mode 100644
index 000000000000..676aa08d6ad7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128588.c
@@ -0,0 +1,33 @@
+/* { dg-do run } */
+
+/* (x not in [113, 115]) | (x != 120) holds for every x.  */
+
+__attribute__((noipa)) int
+f (int x)
+{
+  if (x == 1) return 1;
+  if (x == 2) return 1;
+  if (x == 3) return 1;
+  if (x == 4) return 1;
+  if (x == 5) return 1;
+  if (x == 6) return 1;
+  if (x == 7) return 1;
+  if (x == 8) return 1;
+  if (x == 9) return 1;
+  if (x == 10) return 1;
+  if (x == 11) return 1;
+  if (x == 12) return 1;
+  _Bool a = (unsigned) x - 113u > 2u;
+  _Bool b = x != 120;
+  if (a | b)
+    return 8;
+  return 9;
+}
+
+int
+main (void)
+{
+  if (f (120) != 8 || f (114) != 8)
+    __builtin_abort ();
+  return 0;
+}

Reply via email to