Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would gcc movl to the same register?

Tags:

assembly

For this piece of C code:

uint64_t roundUp(uint64_t value, uint32_t blockSize) 
{
    return (value + blockSize - 1) & ~(blockSize - 1);
}

gcc 4.6 -O3 generated the following assembly:

roundUp(unsigned long, unsigned int):
.LFB0:
    .cfi_startproc
    movl    %esi, %edx
    movl    %esi, %esi
    leaq    -1(%rdi,%rsi), %rax
    negl    %edx
    andl    %edx, %eax
    ret
    .cfi_endproc

Could anyone tell me why would it want to do this?

movl    %esi, %esi
like image 737
Inso Reiges Avatar asked May 25 '12 03:05

Inso Reiges


1 Answers

That clears the upper 32 bits. When you write to a 32 bit register in x86-64, the upper 32 bits are cleared automatically. Since esi contains a 32 bit parameter, the upper 32 bits could contain any value, so they need to be cleared before rsi can be used.

like image 119
ughoavgfhw Avatar answered Oct 16 '22 03:10

ughoavgfhw