https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129039

            Bug ID: 129039
           Summary: [14/15/16/17 Regression] DOM exports a range from a
                    one-SSA unreachable condition and later unswitching
                    folds a live null check
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
                CC: amacleod at redhat dot com
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu

Fails with trunk (gcc 17.0.0 20261126, d1dbc89caa56) and with 14.1.0 through
16.2.0; 7.5.0 through 13.4.0 work.
Target: x86_64-pc-linux-gnu.

    #include <stdint.h>
    int64_t g2, g27, g10;
    static void *g13;
    _Bool f11___trans_tmp_1, f11_c17;
    void __attribute__((noinline)) f11(int64_t a0, void *a4, long a5, int64_t
v15, int32_t v12)
    {
        _Bool ob6;
    lbl_entry:
    lbl_sw_def4:
        ob6 = g13 == a4;
        if (ob6 || f11_c17) return;
        if (f11___trans_tmp_1) v12 = g10;
        a0 = (long)a4 + a5;
        ob6 = 0 == a0;
        g2 = 1;
        if (ob6) goto lbl_b22;
        __builtin_unreachable();
    lbl_b22:
        g27 = v12;
        switch (v15)
        {
            case 6009053803839173278: goto lbl_sw_def4;
            case 70689892217558167: goto lbl_entry;
        }
    }

    int main()
    {
        f11(0, 0, 0, 0, 0);
        if (g2) __builtin_abort();
    }

The program is expected to exit 0. On a native x86_64-pc-linux-gnu build of GCC
17.0.0 20261126 (experimental), git commit d1dbc89caa56, compiling with -O2
-fno-tree-ccp -funswitch-loops and running the binary aborts. The same command
also aborts on 14.1.0 through 16.2.0. It exits 0 on 7.5.0 through 13.4.0.

The expected exit status is independent of that optimization path. The same
source exits 0 at -O0, -O1, -O2, -O3 and -Os without those flags, including a
host GCC 14.2.0 build with -O0 -fsanitize=undefined,address. The executed path
is f11(0, 0, 0, 0, 0), which returns at the first test because the static
pointer g13 is null and equals a4; __builtin_unreachable is not reached.

Adding -fdisable-tree-dom2 to the failing command restores the correct exit
status. The abort also occurs at -O3 -fno-tree-ccp on 14.1.0 through 15.3.0 and
on trunk. 16.1.0 and 16.2.0 exit 0 at -O3 -fno-tree-ccp, but they still abort
at -O2 -fno-tree-ccp -funswitch-loops.

Godbolt: https://godbolt.org/z/oWacPfsTe

set_global_ranges_from_unreachable_edges in tree-ssa-dom.cc exports GORI ranges
from the edge that avoids __builtin_unreachable as global ranges. This is not a
duplicate of PR125501, which is RESOLVED FIXED. That fix returns early only
when both operands of the controlling condition are SSA names. Here the
condition is if (a0_18 == 0) with a0_18 = (long)a4 + a5, so one operand is not
an SSA name, a global nonzero range is recorded for the default definition of
a5, and later loop unswitching folds the live null check.

A possible direction is to apply the same early return in
set_global_ranges_from_unreachable_edges when only one operand of the condition
is an SSA name, or otherwise avoid exporting a global range from that edge. I
have not tested a patch.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to