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

            Bug ID: 128770
           Summary: [i386] AVX10.2 UNSPEC_OPTCOMX breaks if-conversion for
                    Float EQ/NE
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hongyuw at gcc dot gnu.org
  Target Milestone: ---

$ cat t.c
float
sel (float a, float b, float x, float y)
{
  return a == b ? x : y;
}

float
loop (const float *s, const float *d, float m, int n, float *err)
{
  float lo = 0, e = 0;
  for (int i = 0; i < n; i++)
    {
      e += d[i] * d[i];
      float t = lo + 1.0f - 2.0f * d[i];
      lo = s[i] == m ? t : lo;
    }
  *err = e;
  return lo;
}

$ gcc -O2 -ffast-math -ffp-contract=off -fno-tree-vectorize
-march=diamondrapids -S t.c

sel:
        vcomxss   %xmm1, %xmm0
        vmovd     %xmm2, %eax
        vmovd     %xmm3, %edx
        cmovne    %edx, %eax
        vmovd     %eax, %xmm0
        ret
loop (inner):
        vcomxss   (%rdi,%rax), %xmm4
        ...
        jne       .L3
        vaddss    %xmm5, %xmm0, %xmm0
        vaddss    %xmm1, %xmm1, %xmm1
        vsubss    %xmm1, %xmm0, %xmm0
.L3:

With -mno-avx10.2 added:

sel:
        vcmpneq_osss  %xmm1, %xmm0, %xmm0
        vblendvps     %xmm0, %xmm3, %xmm2, %xmm0
        ret
loop (inner):
        vcmpeq_uqss   (%rdi,%rax), %xmm4, %xmm1
        vblendvps     %xmm1, %xmm0, %xmm2, %xmm2


godbolt:https://gcc.godbolt.org/z/WP7KEnEMn

For -mavx10.2, ix86_expand_fp_compare (since r15-3354-g3b1decef830) wraps every
FP EQ/NE compare as

  (set (reg:CCFP flags) (unspec:CCFP [(compare:CCFP a b)] UNSPEC_OPTCOMX))

canonicalize_condition only looks through a COMPARE setter, so ifcvt gets
cond = (ne (reg:CCFP flags) 0) instead of (eq a b).  ix86_expand_fp_movcc
then refuses (comparison mode CCFP != move mode SF), and the fallback
noce_try_cmove_arith matches *movsfcc_1_387, i.e. GPR cmov with xmm<->gpr
moves.  In the loop that sequence costs more than the branch, so the branch
stays.

Under -ffinite-math-only there is no reason for VCOMX: plain COMISS sets
ZF exactly for EQ/NE when there are no NaNs, which is why the
-mno-avx10.2 path is a single je/jne and converted to cmp+blend.

Gating UNSPEC_OPTCOMX expansion on HONOR_NANS (op_mode) fixes both functions
above.

Reply via email to