On 9/4/26 6:35 AM, [email protected] wrote:
From: Kyrylo Tkachov <[email protected]>

Reassociation rebalances a chain of four or more comparisons into a tree.
The reassociation width of the boolean mode is the width of an integer
bitwise operation.  A conditional-compare sequence cannot use that
parallelism because every conditional comparison tests the flags that the
previous comparison set.  For

   int
   f (int a, int b, int c, int d, int e, int f, int g, int h)
   {
     return (a < b) & (c < d) & (e < f) & (g < h);
   }

reassociation produced:

   _3 = _1 & _2;
   _16 = _4 & _5;
   _17 = _3 & _16;

AArch64 emitted two sequences and a bitwise operation:

        cmp     w0, w1
        ccmp    w2, w3, 0, lt
        cset    w1, lt
        cmp     w4, w5
        ccmp    w6, w7, 0, lt
        cset    w0, lt
        and     w0, w1, w0

Keep the chain linear only when every operand is a scalar comparison in the
same block and at least one comparison can be folded into the flags.  Put a
forwarded trapping comparison in the innermost pair.  The expander tries
both orders of that pair and can make this comparison unconditional.
AArch64 then emits one sequence:

        cmp     w4, w5
        ccmp    w6, w7, 0, lt
        ccmp    w2, w3, 0, lt
        ccmp    w0, w1, 0, lt
        cset    w0, lt

Tests cover linear and balanced source forms, unsupported operands,
trapping comparisons in every source position, and comparison results with
other uses.

Changes since v1:
- Require the complete operand list to form one CCMP sequence.
- Put a trapping comparison first and reject conflicting heads.
- Add tests for trapping positions and comparison results with other uses.

gcc/

        * tree-ssa-reassoc.cc (ccmp_comparison_p): New function.
        (prepare_ccmp_chain): Likewise.
        (reassociate_bb): Keep a complete conditional-compare chain linear
        and put its required first operand in the innermost pair.

gcc/testsuite/

        * gcc.c-torture/execute/ccmp-tree-1.c: New test.
        * gcc.target/aarch64/ccmp_6.c: New test.
        * gcc.target/aarch64/ccmp_10.c: Likewise.
        * gcc.target/aarch64/ccmp_12.c: Likewise.
        * gcc.target/aarch64/ccmp_14.c: Likewise.
        * gcc.target/aarch64/fccmp_6.c: Likewise.
        * gcc.target/aarch64/fccmp_8.c: Likewise.

Signed-off-by: Kyrylo Tkachov <[email protected]>
OK
jeff

Reply via email to