https://gcc.gnu.org/g:77d6090f8450928ea569e7bd62896c989cdede9a

commit r16-10006-g77d6090f8450928ea569e7bd62896c989cdede9a
Author: Hongyu Wang <[email protected]>
Date:   Fri Nov 13 07:43:57 2026 +0530

    i386: Fix the compare and the flags mode in the APX ccmp pattern [PR128911]
    
    The @ccmp<mode> leaf compared (minus a b) against zero, which only matches
    the flags of cmp a, b for EQ/NE, so simplify-rtx could fold ordered and
    unsigned conditions wrongly. Also the condition was hard-coded on
    (reg:CC FLAGS_REG), losing the CC mode of the previous compare and the
    scc output was incorrect. Compare operands 2 and 3 directly and make the
    flags register an operand to inhert previous CCmode.
    
    gcc/ChangeLog:
    
            PR target/128911
            * config/i386/i386.md (@ccmp<mode>): Compare operands 2 and 3
            directly.  Use operand 5 for the flags register in operand 1.
            * config/i386/i386-expand.cc (ix86_gen_ccmp_next): Keep the CC
            mode of the previous compare and pass it as operand 5.
    
    gcc/testsuite/ChangeLog:
    
            PR target/128911
            * gcc.target/i386/pr128911-1.c: New test.
            * gcc.target/i386/pr128911-2.c: Likewise.
            * gcc.target/i386/pr128911-3.c: Likewise.
    
    (cherry picked from commit bfd6e895d9ec31e43199c3581858becb80730fb3)

Diff:
---
 gcc/config/i386/i386-expand.cc             | 19 ++++++++++++-------
 gcc/config/i386/i386.md                    |  7 +++----
 gcc/testsuite/gcc.target/i386/pr128911-1.c | 25 +++++++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr128911-2.c | 27 +++++++++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr128911-3.c |  8 ++++++++
 5 files changed, 75 insertions(+), 11 deletions(-)

diff --git a/gcc/config/i386/i386-expand.cc b/gcc/config/i386/i386-expand.cc
index 6d47527fd075..dd07b340bc0a 100644
--- a/gcc/config/i386/i386-expand.cc
+++ b/gcc/config/i386/i386-expand.cc
@@ -27165,7 +27165,7 @@ ix86_gen_ccmp_next (rtx_insn **prep_seq, rtx_insn 
**gen_seq, rtx prev,
   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (treeop0));
   insn_code icode;
   rtx_code prev_code;
-  struct expand_operand ops[5];
+  struct expand_operand ops[6];
   int dfv;
 
   /* Exit early for non integer modes to avoid O(n^2) part of expand_operands. 
*/
@@ -27194,26 +27194,31 @@ ix86_gen_ccmp_next (rtx_insn **prep_seq, rtx_insn 
**gen_seq, rtx prev,
   dfv = ix86_get_flags_cc ((rtx_code) cmp_code);
 
   prev_code = GET_CODE (prev);
-  /* Fixup FP compare code here.  */
-  if (GET_MODE (XEXP (prev, 0)) == CCFPmode)
-    prev_code = ix86_fp_compare_code_to_integer (prev_code);
+  /* Keep the CC mode of the previous compare, except fixup FP compare
+     code to the integer one here.  */
+  rtx flags = XEXP (prev, 0);
+  if (GET_MODE (flags) == CCFPmode)
+    {
+      prev_code = ix86_fp_compare_code_to_integer (prev_code);
+      flags = gen_rtx_REG (CCmode, FLAGS_REG);
+    }
 
   if (bit_code != AND)
     prev_code = reverse_condition (prev_code);
   else
     dfv = (int)(dfv ^ 1);
 
-  prev = gen_rtx_fmt_ee (prev_code, VOIDmode, XEXP (prev, 0),
-                        const0_rtx);
+  prev = gen_rtx_fmt_ee (prev_code, VOIDmode, flags, const0_rtx);
 
   create_fixed_operand (&ops[0], target);
   create_fixed_operand (&ops[1], prev);
   create_fixed_operand (&ops[2], op0);
   create_fixed_operand (&ops[3], op1);
   create_fixed_operand (&ops[4], GEN_INT (dfv));
+  create_fixed_operand (&ops[5], flags);
 
   push_to_sequence (*gen_seq);
-  if (!maybe_expand_insn (icode, 5, ops))
+  if (!maybe_expand_insn (icode, 6, ops))
     {
       end_sequence ();
       return NULL_RTX;
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index 9603597bfb46..b1fcaab65e30 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -1535,11 +1535,10 @@
  [(set (match_operand:CC 0 "flags_reg_operand")
        (if_then_else:CC
         (match_operator 1 "comparison_operator"
-         [(reg:CC FLAGS_REG) (const_int 0)])
+         [(match_operand 5 "flags_reg_operand") (const_int 0)])
        (compare:CC
-         (minus:SWI (match_operand:SWI 2 "nonimmediate_operand" "<r>,<r>m,<r>")
-                    (match_operand:SWI 3 "<general_operand>" "C,<r><i>,<m>"))
-         (const_int 0))
+         (match_operand:SWI 2 "nonimmediate_operand" "<r>,<r>m,<r>")
+         (match_operand:SWI 3 "<general_operand>" "C,<r><i>,<m>"))
        (unspec:CC
          [(match_operand:SI 4 "const_0_to_15_operand")]
          UNSPEC_APX_DFV)))]
diff --git a/gcc/testsuite/gcc.target/i386/pr128911-1.c 
b/gcc/testsuite/gcc.target/i386/pr128911-1.c
new file mode 100644
index 000000000000..ad276f1916ed
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128911-1.c
@@ -0,0 +1,25 @@
+/* PR target/128911 */
+/* { dg-do run { target { { ! ia32 } && { ! *-*-darwin* } } } } */
+/* { dg-require-effective-target apxf } */
+/* { dg-options "-O1 -mapxf -fno-tree-fre -fno-tree-dominator-opts 
-fno-tree-sra" } */
+
+__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 (!__builtin_cpu_supports ("apxf"))
+    return 0;
+
+  if (f () != 0)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr128911-2.c 
b/gcc/testsuite/gcc.target/i386/pr128911-2.c
new file mode 100644
index 000000000000..6d102d3cca51
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128911-2.c
@@ -0,0 +1,27 @@
+/* PR target/128911 */
+/* { dg-do run { target { { ! ia32 } && { ! *-*-darwin* } } } } */
+/* { dg-require-effective-target apxf } */
+/* { dg-options "-O2 -mapxf" } */
+
+#define F(n, e) \
+  __attribute__((noipa)) int \
+  n (int x, unsigned a, unsigned b) { unsigned t = a - b; return e; }
+
+F (f1, x == 0 && (int) t < 0)
+F (f2, (int) t < 0 && x == 0)
+F (f3, x == 0 && (int) t >= 0)
+F (f4, (int) t >= 0 && x == 0)
+F (f5, x != 0 || (int) t < 0)
+
+int
+main ()
+{
+  if (!__builtin_cpu_supports ("apxf"))
+    return 0;
+
+  unsigned a = 0x80000000u, b = 1;
+  if (f1 (0, a, b) != 0 || f2 (0, a, b) != 0 || f3 (0, a, b) != 1
+      || f4 (0, a, b) != 1 || f5 (0, a, b) != 0)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr128911-3.c 
b/gcc/testsuite/gcc.target/i386/pr128911-3.c
new file mode 100644
index 000000000000..5af9367e562c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128911-3.c
@@ -0,0 +1,8 @@
+/* PR target/128911 */
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -mapxf" } */
+/* { dg-final { scan-assembler-times "ctestsl" 2 } } */
+/* { dg-final { scan-assembler-times "ctestnsl" 3 } } */
+/* { dg-final { scan-assembler-not "ctest(l|ge)l" } } */
+
+#include "pr128911-2.c"

Reply via email to