[LLVMdev] Poor register allocations vs gcc
Quentin Colombet
qcolombet at apple.com
Mon Jul 13 11:10:32 PDT 2015
Hi Jog,
This look like a scheduling problem to me.
The main difference here is that in GCC the final “a + b” is scheduled before the call, whereas in LLVM case, this is scheduled after the call.
Because of that, %rdi cannot be used in the final add and it has to be saved somewhere else.
You can see that in effect by replacing:
puts("ok");
return a + b;
By
b += a;
puts("ok");
return b;
That being said, you shouldn’t have to do that to have the nice code.
Could you file a PR for the scheduling problem?
Thanks,
-Quentin
> On Jul 13, 2015, at 10:03 AM, deco33000 at yandex.com wrote:
>
> Hello,
>
> I have an issue with the llvm optimizations. I need to create object codes.
>
> the -ON PURPOSE poor && useless- code :
> ---------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
>
> int ci(int a){
>
> return 23;
>
> }
> int flop(int a, char ** c){
>
> a += 71;
>
> int b = 0;
>
> if (a == 56){
>
> b = 69;
> b += ci(a);
> }
>
> puts("ok");
> return a + b;
> }
> --------------------------------------
>
> Compiled that way (using the versions I downloaded and eventually compiled) :
> clang_custom -std=c11 -O3 -march=native -c app2.c -S
>
> against gcc:
> gcc_custom -std=c11 -O3 -march=native -c app2.c -S
>
> Versions (latest for each, downloaded just a few days ago):
> gcc : 5.1
> clang/llvm: clang+llvm-3.6.1-x86_64-apple-darwin
>
> Host:
> osx yosemite.
>
> The assembly (cut to the essential):
>
> LLVM:
> pushq %rbp
> movq %rsp, %rbp
> pushq %r14
> pushq %rbx
> movl %edi, %r14d
> leal 71(%r14), %eax
> xorl %ecx, %ecx
> cmpl $56, %eax
> movl $92, %ebx
> cmovnel %ecx, %ebx
> leaq L_.str(%rip), %rdi
> callq _puts
> leal 71(%rbx,%r14), %eax
> popq %rbx
> popq %r14
> popq %rbp
> retq
>
> and the gcc one:
>
> pushq %rbp
> movl $0, %eax
> movl $92, %ebp
> pushq %rbx
> leal 71(%rdi), %ebx
> leaq LC1(%rip), %rdi
> subq $8, %rsp
> cmpl $56, %ebx
> cmovne %eax, %ebp
> call _puts
> addq $8, %rsp
> leal 0(%rbp,%rbx), %eax
> popq %rbx
> popq %rbp
> ret
>
> As we can see, llvm makes poor register allocations (ecx and r14), leading to more instructions for the same result.
>
> Are there some optimizations I can bring on the table to avoid this ?
> --
> Jog
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
More information about the llvm-dev
mailing list