Skip to content

[AArch64] Failure to fold lsr or asr into cmp #122380

@Kmeakin

Description

@Kmeakin

https://godbolt.org/z/rMMbbfMdW

Instead of performing an lsr/asr and then comparing the result against zero, the shift can be performed as part of a cmp against xzr:

src:
        lsr     x8, x0, #32
        cmp     x8, #0
        cset    w0, ne
        ret

tgt:
        cmp     xzr, x0, lsr 32
        cset    w0, ne
        ret

LLVM does perform this fold for shifts <= 31, and for lsl it is able to find a different way of doing the comparison in one instruction using tst. It also seems to already perform the fold if comparing against a variable instead of 0. I guess the fold fails when comparing against 0 because a comparison against zero can be represented either as cmp x0, #0 or cmp xzr, x0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions