On 11/19/26 2:36 AM, Tamar Christina wrote:
Ping.

-----Original Message-----
From: Tamar Christina <[email protected]>
Sent: 22 October 2026 08:25
To: [email protected]
Cc: nd <[email protected]>; [email protected]; [email protected];
[email protected]
Subject: [patch v2 12/14][compare-elim]: recognize SVE predicate flag setters

This patch extends compare-elim so it can recognise target-specific flag-setting
operations, not only explicit COMPARE rtxes.

AArch64 SVE expansion can produce two equivalent vector predicate
comparisons
for one flag consuming instruction: one comparison produces the predicate
value
that is needed later, while another comparison exists only to produce the
condition flags for the branch.  The flags-producing operation is represented
using target-specific RTL rather than a plain COMPARE, so the old compare-
elim
logic did not see the duplication.

This happens because SVE compares are used 3 ways for the same instruction:

1. Only the flags are interesting (used for control flow)
2. Only the result are interesting (normal vectorized data compares)
3. Both are interesting (for instance early break).

With the changes to early break we now produce 1 and 2 more readily, for
instance

The example:

   int any_gt (signed char *a, signed char *b, int n)
   {
     for (int i = 0; i < n; ++i)
       if (a[i] > b[i])
         return i;
     return 0;
   }

compiled with -O3 -march=armv8-a+sve

With cmpelim disabled, the SVE loop body contains two identical compares:
one
for the live predicate value and one for the branch flags:

   ld1b    z30.b, p7/z, [x0, x3]
   ld1b    z31.b, p7/z, [x1, x3]
   cmpgt   p15.b, p7/z, z30.b, z31.b
   cmpgt   p7.b, p7/z, z30.b, z31.b
   b.none  .L5

With this patch, the duplicate compare is removed and the branch uses the
flags
from the remaining SVE compare:

   ld1b    z30.b, p7/z, [x0, x3]
   ld1b    z31.b, p7/z, [x1, x3]
   cmpgt   p15.b, p7/z, z30.b, z31.b
   b.none  .L5

The patch teaches compare-elim to record non-COMPARE flag setters by
looking at
the target flags_regnum.  It then tries to combine the flags result with the
preceding value-producing instruction.  All flag uses must be known and the
inputs must be unchanged between the two instructions.  The target
recognizer
decides whether the combined instruction is valid.

Bootstrapped Regtested on aarch64-none-linux-gnu,
arm-none-linux-gnueabihf, x86_64-pc-linux-gnu
-m32, -m64 and no issues.

-O3, profiled, profiled -O3 and -O3 -LTO bootstrap tested and no issues.

  Also SPECCPU 2017 and SPECCPU 2026 tested.

OK for master?

Thanks,
Tamar

gcc/ChangeLog:

        * compare-elim.cc (comparison::flags_src): New member.
        (conforming_flags_set): New function.  Recognize non-COMPARE flag
        setters that do not read the flags register.
        (find_comparison_dom_walker::before_dom_children): Record them
and
        their preceding flags clobber without treating them as ordinary
        comparisons for dominance-based reuse.
        (try_validate_parallel): Return the validated PARALLEL.
        (try_eliminate_compare): Merge non-COMPARE flag setters with a
        preceding value-producing instruction when all flag uses are known,
        the operands are unchanged and the merged instruction is recognized.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/sve/cmpelim-sve-ptest-2.c: New test.
        * gcc.target/aarch64/sve/cmpelim-sve-ptest.c: Likewise.
OK

jeff

Reply via email to