[LLVMdev] Missing optimization - constant parameter

Maurice Marks maurice.marks at gmail.com
Fri Aug 2 13:12:17 PDT 2013


For the little  C test program where a constant is stored in memory and
also
used as a parameter:


#include <stdint.h>
uint64_t val, *p;
extern uint64_t xtr( uint64_t);

uint64_t caller() {
        uint64_t x;
        p = &val;
        x = 12345123400L;
        *p = x;
        return xtr(x);
}


 clang (3.2, 3.3 and svn) generates the following X86 code (at -O3):

caller:
        movq    $val, p(%rip)
        movabsq $12345123400, %rax      # imm = 0x2DFD3A248
        movq    %rax, val(%rip)
        movabsq $12345123400, %rdi      # imm = 0x2DFD3A248
        jmp     xtr                     # TAILCALL


Notice that the constant value is loaded twice, once into %rax
and again into %rdi as the first parameter for the (tail) call.

There is no need for the second constant load - the value is already in
a register. If the target register was assigned as %rdi
there would be no need for the second load instruction at all.

gcc (4.6+) generates better code:

caller:
        movabsq $12345123400, %rdi
        movq    $val, p(%rip)
        movq    %rdi, val(%rip)
        jmp     xtr

I expected that this optimization would be picked
up in a cse, gvn, machine-cse or even peepholing pass.

Comments?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130802/055f499e/attachment.html>


More information about the llvm-dev mailing list