[llvm-bugs] [Bug 46316] New: Clang unnecessarily uses rbx and pushes it and rax

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jun 13 16:16:21 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=46316

            Bug ID: 46316
           Summary: Clang unnecessarily uses rbx and pushes it and rax
           Product: new-bugs
           Version: 10.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: josephcsible at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Consider this code:

int g(int);
void h(void);
int f(int x) {
    if(g(0) == -1) {
        return 1;
    }
    h();
    return 0;
}

When compiled on Clang 10.0.0 on x86-64 Linux with "-O3
-fno-omit-frame-pointer", it produces this:

f:
        pushq   %rbp
        movq    %rsp, %rbp
        pushq   %rbx
        pushq   %rax
        xorl    %ebx, %ebx
        xorl    %edi, %edi
        callq   g
        cmpl    $-1, %eax
        je      .LBB0_1
        callq   h
        jmp     .LBB0_3
.LBB0_1:
        movl    $1, %ebx
.LBB0_3:
        movl    %ebx, %eax
        addq    $8, %rsp
        popq    %rbx
        popq    %rbp
        retq

https://godbolt.org/z/J3It3A

There's no need to mess with ebx at all, or to push anything other than rbp. It
should have produced something like this instead:

f:
        pushq   %rbp
        movq    %rsp, %rbp
        xorl    %edi, %edi
        callq   g
        cmpl    $-1, %eax
        je      .LBB0_1
        callq   h
        xorl    %eax, %eax
        popq    %rbp
        retq
.LBB0_1:
        movl    $1, %eax
        popq    %rbp
        retq

And Clang basically knows how to emit that code. In particular, if I change
"g(0)" to "g(x)", then that's exactly what I get, aside from the "xorl    %edi,
%edi".

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200613/7f7c037d/attachment.html>


More information about the llvm-bugs mailing list