On 9/30/24 2:04 AM, Artemiy Volkov wrote:
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
And I've pushed this as well.

jeff

Reply via email to