Skip to content

[ARM] [Clang] Missed optimization for bool return val arithmetic operations #162599

@xieyuanbin1-huawei

Description

@xieyuanbin1-huawei
bool xxxx();
int kkk()
{
    return xxxx() + 1;
}

https://godbolt.org/z/YoM1PYE1n

llvm:

kkk:
        push    {r11, lr}
        bl      xxxx
        mov     r1, #1
        cmp     r0, #0
        movwne  r1, #2
        mov     r0, r1
        pop     {r11, pc}

gcc:

kkk:
        push    {r4, lr}
        bl      xxxx
        add     r0, r0, #1
        pop     {r4, pc}

According to ARM's ABI (I'm not sure if there is documentation to support this, but both GCC and LLVM compilers behave this way), in the aforementioned use case, the return value of the bool type is stored in the r0 register, and the value of r0 is 0 (false) or 1 (true). This can be observed from the following use case: https://godbolt.org/z/Wj1Goq1Y4

However, the LLVM compiler seems to have missed some optimizations when performing arithmetic operations on return values.
No similar issues have been identified on other architectures at this time.

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