<div dir="ltr">For the little C test program where a constant is stored in memory and also<br>used as a parameter:<br><br><br>#include <stdint.h><br>uint64_t val, *p;<br>extern uint64_t xtr( uint64_t);<br><br>uint64_t caller() {<br>
uint64_t x;<br> p = &val;<br> x = 12345123400L;<br> *p = x;<br> return xtr(x);<br>}<br><br><br> clang (3.2, 3.3 and svn) generates the following X86 code (at -O3):<br><br>caller: <br>
movq $val, p(%rip)<br> movabsq $12345123400, %rax # imm = 0x2DFD3A248<br> movq %rax, val(%rip)<br> movabsq $12345123400, %rdi # imm = 0x2DFD3A248<br> jmp xtr # TAILCALL<br>
<br><br>Notice that the constant value is loaded twice, once into %rax <br>and again into %rdi as the first parameter for the (tail) call.<br><br>There is no need for the second constant load - the value is already in<br>
a register. If the target register was assigned as %rdi <br>there would be no need for the second load instruction at all.<br><br>gcc (4.6+) generates better code:<br><br>caller:<br> movabsq $12345123400, %rdi<br> movq $val, p(%rip)<br>
movq %rdi, val(%rip)<br> jmp xtr<br><br>I expected that this optimization would be picked <br>up in a cse, gvn, machine-cse or even peepholing pass.<br><br>Comments?<br><br><div style id="__af745f8f43-e961-4b88-8424-80b67790c964__">
</div></div>