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

The conditional-compare expander accepts a plain boolean as a comparison
against zero.  This is useful when a chain also contains an explicit
comparison.  It is harmful when every leaf is a boolean.  For example:

   _Bool
   f (_Bool x, _Bool y)
   {
     return x || y;
   }

AArch64 emitted:

        and     w1, w1, 255
        tst     w0, 255
        ccmp    w1, 0, 0, eq
        cset    w0, ne

Make candidate recognition report whether it found an explicit comparison.
It already validates every statement before it follows the next definition.
Reject a candidate without an explicit comparison.

TER fallback starts a new conditional-compare recognition call for each
forwarded suffix.  Cache accepted all-boolean subchains in the existing
per-function ssaexpand state so that these calls inspect a linear chain in
linear time.  The state is shared by cfgexpand and expression expansion and
is released when RTL expansion finishes.

The example then emits:

        orr     w0, w0, w1

Tests cover all-boolean and mixed chains, a non-binary boolean definition,
and a long rejected chain.

Changes since v1:
- Report explicit comparisons during candidate recognition.
- Cache all-boolean subchains so TER fallback stays linear.
- Add non-binary definition and long-chain tests.

gcc/

        PR middle-end/109832
        * ccmp.cc (ccmp_tree_comparison_p): Report whether an accepted leaf
        is a comparison.
        (ccmp_candidate_p): Propagate the result and cache all-boolean
        candidates.
        (expand_ccmp_expr): Reject an all-boolean candidate.
        * tree-outof-ssa.h (ssaexpand::ccmp_no_comparison): New member.
        * tree-outof-ssa.cc (finish_out_of_ssa): Free it.

gcc/testsuite/

        PR middle-end/109832
        * gcc.target/aarch64/ccmp_7.c: New test.
        * gcc.target/aarch64/ccmp_9.c: Likewise.
        * gcc.target/aarch64/ccmp_11.c: Likewise.
        * gcc.target/aarch64/ccmp_13.c: Likewise.

Signed-off-by: Kyrylo Tkachov <[email protected]>
Thanks for setting me straight on the initial RTL generation issue. I had no idea how ccmp has wired in and never would have guessed we were intercepting cases like you showed above and sending those into the ccmp path.

This looks OK to me.

jeff

Reply via email to