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

            Bug ID: 128911
           Summary: [i386]APX ccmp pattern miscompiles (minus a b) leaf
                    and lost CC mode of the previous compare
           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: ---

The @ccmp<mode> pattern in i386.md has two independent problems, both present
since r15-1058-gc989e59fc99 ("[APX CCMP] Support APX CCMP"):

  (1) the compare leaf is (compare:CC (minus op2 op3) (const_int 0)) instead of
      (compare:CC op2 op3);
  (2) the condition on the previous flags is hard-coded as
      (match_operator 1 [(reg:CC FLAGS_REG) (const_int 0)]), so the CC mode
      the previous compare was generated in (e.g. CCGOCmode) is dropped.

==== (1) (minus a b) leaf ====

Testcase

__attribute__((noipa)) int
f (void)
{
  unsigned int m[2];
  m[0] = 8;
  m[1] = 48;
  unsigned int a = m[0], b = m[1];
  return (b > 175) | (a > 47);
}

int main () { if (f () != 0) __builtin_abort (); return 0; }

$ gcc -O1 -mapxf -fno-tree-fre -fno-tree-dominator-opts -fno-tree-sra t.c &&
./a.out
Aborted

f:
        movl    $1, %eax
        ret

The expected result is 0. Without -mapxf it returns 0.

Before combine the ccmp is

(insn 17 (set (reg:CC flags)
        (if_then_else:CC (leu (reg:CC flags) (const_int 0))
            (compare:CC (minus:SI (subreg:SI (reg:DI 111) 0) (const_int 47))
                        (const_int 0))
            (unspec:CC [(const_int 7)] UNSPEC_APX_DFV))) {ccmpsi}

After combine substitutes reg 111 = 8, both operands are constants and
simplify-rtx folds the leaf on the difference: (8 - 47) >u 0 is true, while GTU
on cmp 8, 47 is false. (minus a b) vs 0 only gives the flags of cmp a, b for
EQ/NE.


==== (2) CC mode of the previous compare is dropped ====

Testcase:

__attribute__((noipa)) int
f (int x, unsigned a, unsigned b)
{
  unsigned t = a - b;
  return x == 0 && (int) t < 0;
}

int
main ()
{
  if (f (0, 0x80000000u, 1) != 0)
    __builtin_abort ();
  return 0;
}

$ gcc -O2 -mapxf t.c && sde -future -- ./a.out
Aborted

f:
        cmpl    %edx, %esi
        ctestll {dfv=} %edi, %edi
        setzue  %al
        ret

t = 0x7fffffff, so (int) t < 0 is false and f must return 0. The ctest
condition is "l" (SF != OF) on the flags of cmp a, b; that subtraction
overflows (OF=1, SF=0), so "l" is true. The condition needs to be "s" (SF
only). Without -mapxf it returns 0.

At expand, ix86_gen_ccmp_first picks CCGOCmode for (lt t 0), but the ccmp insn
reads the flags as plain CCmode:

(insn 9 (set (reg:CCGOC flags)
        (compare:CCGOC (reg:SI 108 [ t_7 ]) (const_int 0))))
(insn 10 (set (reg:CC flags)
        (if_then_else:CC (lt (reg:CC flags) (const_int 0))
            (compare:CC (minus:SI (reg/v:SI 104 [ x ]) (const_int 0))
                        (const_int 0))
            (unspec:CC [(const_int 5)] UNSPEC_APX_DFV))) {ccmpsi}

The @ccmp<mode> pattern writes the condition operand as

  (match_operator 1 "comparison_operator"
    [(reg:CC FLAGS_REG) (const_int 0)])

so gen_ccmp<mode> rebuilds operand 1 on (reg:CC FLAGS_REG). ix86_gen_ccmp_next
passes (lt (reg:CCGOC flags) 0), and the CCGOC mode is lost. %C1 then prints
CCmode LT as "l" instead of the CCGOCmode "s".

Proposed fixes:
(1) Compare operands 2 and 3 directly in the leaf.
(2) Make the flags register in operand 1 a match_operand, and have
ix86_gen_ccmp_next pass the previous compare's CC mode through it.

Reply via email to