Skip to content

[AArch64] Need to adjust comparison with -1 to comparison with 0 when it is beneficial #141137

Closed
@AZero13

Description

@AZero13

When I corrected an issue with getAArch64Cmp: the following changed:

; CHECK-SD-NEXT: ands w8, w8, w8, asr #31
; CHECK-SD-NEXT: csinv w8, w8, wzr, ge

to

; CHECK-SD-NEXT: and w8, w8, w8, asr #31
; CHECK-SD-NEXT: cmn w8, #1
; CHECK-SD-NEXT: csinv w8, w8, wzr, gt

Now, this is only one regression; the rest of the changes are improvements. However, it made me realize an issue: the compiler is peephole optimizing a compare with -1 but not because it is better, but because it thought -1 was not a legal immediate for a compare, which my patch corrects the compiler into realizing -1 is a valid cmp, so it changes it to a comparison with 0, which is beneficial in a tiny number of cases, like this one, but does more harm than good.

So the issue is not with this patch, but rather, we ought to peephole optimize a comparison with -1 to be a comparison with 0, if we know cse will do the job.

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