gcc optimisation with LEA
This question already has an answer here:
lea (%ebx, %esi, 2), %edi
does nothing more than computing ebx + esi*2
and storing the result in edi
.
Even if lea
is designed to compute and store an effective address, it can and it is often used as an optimization trick to perform calculation on something that is not a memory address.
lea (%rdi,%rdi,2),%eax
shl $0x7,%eax
is equivalent to :
eax = rdi + rdi*2;
eax = eax * 128;
And since moo
is in rdi
, it stores moo*384
in eax
It is a standard optimization trick on x86 cores. The AGU, Address Generation Unit, the subsection of the processor that generates addresses, is capable of simple arithmetic. It is not a full blown ALU but has enough transistors to calculate indexed and scaled addresses. Adds and shifts. The LEA, Load Effective Address instruction is a way to invoke the logic in the AGU and get it to calculate simple expressions.
The optimization opportunity here is that the AGU operates independently from the ALU. So you can get superscalar execution, two instructions executing at the same time.
That doesn't actually happen visibly in your code snippet, but it could happen if there's a calculation being done before the shown instructions that required the ALU. It was a trick that only really payed off on simpler cpu cores, 486 and Pentium vintage. Modern processors have multiple ALUs so don't really require this trick anymore.
链接地址: http://www.djcxy.com/p/72364.html上一篇: LEA和几条指令如何工作?
下一篇: 使用LEA进行gcc优化