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

--- Comment #12 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jeff Law <l...@gcc.gnu.org>:

https://gcc.gnu.org/g:52fdf1e7eb89a148a9cdd1daade524f4540ab5fa

commit r15-4189-g52fdf1e7eb89a148a9cdd1daade524f4540ab5fa
Author: Artemiy Volkov <artemiy.vol...@synopsys.com>
Date:   Tue Oct 8 18:06:23 2024 -0600

    tree-optimization/116024 - simplify some cases of X +- C1 cmp C2

    Whenever C1 and C2 are integer constants, X is of a wrapping type, and
    cmp is a relational operator, the expression X +- C1 cmp C2 can be
    simplified in the following cases:

    (a) If cmp is <= and C2 -+ C1 == +INF(1), we can transform the initial
    comparison in the following way:
       X +- C1 <= C2
       -INF <= X +- C1 <= C2 (add left hand side which holds for any X, C1)
       -INF -+ C1 <= X <= C2 -+ C1 (add -+C1 to all 3 expressions)
       -INF -+ C1 <= X <= +INF (due to (1))
       -INF -+ C1 <= X (eliminate the right hand side since it holds for any X)

    (b) By analogy, if cmp if >= and C2 -+ C1 == -INF(1), use the following
    sequence of transformations:

       X +- C1 >= C2
       +INF >= X +- C1 >= C2 (add left hand side which holds for any X, C1)
       +INF -+ C1 >= X >= C2 -+ C1 (add -+C1 to all 3 expressions)
       +INF -+ C1 >= X >= -INF (due to (1))
       +INF -+ C1 >= X (eliminate the right hand side since it holds for any X)

    (c) The > and < cases are negations of (a) and (b), respectively.

    This transformation allows to occasionally save add / sub instructions,
    for instance the expression

    3 + (uint32_t)f() < 2

    compiles to

    cmn     w0, #4
    cset    w0, ls

    instead of

    add     w0, w0, 3
    cmp     w0, 2
    cset    w0, ls

    on aarch64.

    Testcases that go together with this patch have been split into two
    separate files, one containing testcases for unsigned variables and the
    other for wrapping signed ones (and thus compiled with -fwrapv).
    Additionally, one aarch64 test has been adjusted since the patch has
    caused the generated code to change from

    cmn     w0, #2
    csinc   w0, w1, wzr, cc   (x < -2)

    to

    cmn     w0, #3
    csinc   w0, w1, wzr, cs   (x <= -3)

    This patch has been bootstrapped and regtested on aarch64, x86_64, and
    i386, and additionally regtested on riscv32.

    gcc/ChangeLog:

            PR tree-optimization/116024
            * match.pd: New transformation around integer comparison.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/pr116024-2.c: New test.
            * gcc.dg/tree-ssa/pr116024-2-fwrapv.c: Ditto.
            * gcc.target/aarch64/gtu_to_ltu_cmp_1.c: Adjust.

Reply via email to