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

            Bug ID: 128843
           Summary: [14/15/16/17 Regression] sparc64 conditional-move
                    expansion reverses an FP CC comparison with
                    reverse_condition and builds UNKNOWN RTL (heap
                    overflow, ICE)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: ice-on-valid-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
  Target Milestone: ---

Fails with a sparc64-linux-gnu cross trunk build (gcc 17.0.0 20261118,
1190f702416e), with gcc-15 and gcc-16 branch builds (15.3.1 20261105, 16.2.1
20261030) and with 12.4.0, 12.5.0, 13.3.0 and 14.2.0. 9.5.0 and 11.5.0 do not
build the invalid RTL.
Target: sparc64-linux-gnu.

    #define F(n) double g##n (double a, double b) \
      { double u = __builtin_islessgreater (a, b) ? a : b; \
        double v = __builtin_islessgreater (a, b) ? b : a; return u * v; }
    #define F10(n) F(n##0) F(n##1) F(n##2) F(n##3) F(n##4) F(n##5) F(n##6)
F(n##7) F(n##8) F(n##9)
    #define F100(n) F10(n##0) F10(n##1) F10(n##2) F10(n##3) F10(n##4) F10(n##5)
F10(n##6) F10(n##7) F10(n##8) F10(n##9)
    F100(1) F100(2) F100(3) F100(4) F100(5) F100(6) F100(7) F100(8) F100(9)
F100(0)

    sparc64-linux-gnu-gcc -O2 -fno-trapping-math -c t.c

    t.c: In function 'g196':
    t.c:2:21: internal compiler error: Segmentation fault

Expected: successful compilation. The trunk build (--enable-checking=yes)
crashed in 3 of 3 runs. The crash is a heap overrun, so the failing function
and the visible symptom depend on the build and on ASLR (the 15.3.1 checking
build also showed a Bus error and an ICE in extract_insn on the same file).
Before r17-5585 (PR122307), this also happens without -fno-trapping-math; the
15.3.1 and 16.2.1 builds crash at plain -O2. A release that compiles the file
without crashing may still have built the invalid RTL.

Deterministic check: in gdb, break on rtx_alloc_stat_v with code == UNKNOWN.
With trunk cc1 -O2 -fno-trapping-math, the small function below reaches it
twice from sparc_expand_conditional_move (called from gen_movdfcc); the 15.3.1
and 16.2.1 cc1 reach it twice at plain -O2.

    void
    f (double a, double b, double *p)
    {
      double u = a, v = b;
      if (!__builtin_islessgreater (a, b))
        {
          u = b;
          v = a;
        }
      p[0] = u;
      p[1] = v;
    }

Cause: ifcvt passes the existing CC condition, such as (uneq (reg:CCFP %fcc0)
(const_int 0)), to emit_conditional_move. When operands[2] is the destination,
sparc_expand_conditional_move (config/sparc/sparc.cc) swaps the arms and
reverses the condition:

          if (GET_MODE_CLASS (cmp_mode) == MODE_FLOAT)
            rc = reverse_condition_maybe_unordered (rc);
          else
            rc = reverse_condition (rc);

cmp_mode is CCFPmode or CCFPEmode here, not MODE_FLOAT, so reverse_condition is
used. It returns UNKNOWN for UNEQ and LTGT. gen_rtx_fmt_ee (UNKNOWN, ...) then
writes two operands into a 16-byte rtx, which overwrites the next object in the
GC heap.

Possible fix: use reverse_condition_maybe_unordered for CCFPmode and CCFPEmode
as well (add cmp_mode == CCFPmode || cmp_mode == CCFPEmode to the MODE_FLOAT
test). With that change on trunk, the 1000-function file compiles in 3 of 3
runs at -O2 -fno-trapping-math, rtx_alloc is no longer called with UNKNOWN on
the small function, and a run-time check of f under qemu-sparc64 (finite,
equal, NaN and signed-zero inputs) still gives the expected values.

Related: PR104198 (ifcvt reuse of CC for conditional moves) and PR122307.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.
  • [Bug target/128843] New: [14/1... lzy20000419 at outlook dot com via Gcc-bugs

Reply via email to