News for hackers. Stuff that matters.
Re:Does a compiler use all x86 instructions? (2010) by rdtsc on Wednesday August 24, 2016 @12:38PM (#12353262)
> but I have no clue why there are so many lea everywhere.

Pointer arithmetic? Which is used for well, ... many things.

Re:Does a compiler use all x86 instructions? (2010) by efaref on Wednesday August 24, 2016 @12:51PM (#12353372)
LEA (load effective address) can perform computations of the form BASE + SCALE * INDEX + OFFSET, where scale can be 1, 2, 4 or 8. This allows optimisation of multiplications and additions into a single instruction, and the compiler takes advantage of that.

So if you write:

    a = 4 * b + c + 10;
It will be optimised to a single instruction like:

    lea    0xa(%rsi,%rdi,4),%rax
Rather than the more naive:

    imul   $0x4,%rdi,%rax
    add    %rsi,%rax
    add    $0xa,%rax
Re:Does a compiler use all x86 instructions? (2010) by rdtsc on Wednesday August 24, 2016 @06:23PM (#12355711)
Right, that's what I mean by pointer arithmetic -- specialized instruction for calculating memory addresses. It seems it can be co-opted to do math and other calculations as well. But at least that was its intended use?

Also Zen of Assembly mentions that LEA can store its result in any register and doesn't alter flags.

Re:Does a compiler use all x86 instructions? (2010) by efaref on Thursday August 25, 2016 @08:26AM (#12358456)
Yes, I think it was designed in particular for determining addresses of fields within structures and variables on the stack, for example if you had:

    struct foo
    {
       int field1;
       int field2[10];
    }
Then an access like:

    fooptr->field2[index]
Would compile to:

    fooptr + sizeof(int) * index + offsetof(struct foo, field2)
Which is:

    lea $4(fooptr,index,4)
Re:Does a compiler use all x86 instructions? (2010) by rainforest on Wednesday August 24, 2016 @12:43PM (#12353318)
Isn't LEA also a quick single instruction multiply-add for fixed scales in the SIB? e.g. `LEA EAX, EAX*4 + 0x3`
Re:Does a compiler use all x86 instructions? (2010) by rwmj on Wednesday August 24, 2016 @12:41PM (#12353304)
lea is used to implement addition in OCaml: http://caml.inria.fr/pub/ml-archives/caml-list/2004/07/e86a2...
Re:Does a compiler use all x86 instructions? (2010) by dibanez on Wednesday August 24, 2016 @12:48PM (#12353354)
Yea, I've seen compilers use lea for some integer math in C/C++, because it is handled by a different part of the CPU and so can happen in parallel with other integer math ops.
Re:Does a compiler use all x86 instructions? (2010) by rwmj on Wednesday August 24, 2016 @12:52PM (#12353377)
People might say this, but it's not really true. All these instructions are decomposed into micro-ops, and then the micro-ops are run in parallel - if data dependencies allow that - on a common pool of integer ALUs. The reason to use lea is for code compression - it allows you to express two or three operations in a single instruction.
Re:Does a compiler use all x86 instructions? (2010) by cperciva on Wednesday August 24, 2016 @12:59PM (#12353427)
On some Intel CPUs there are separate address-computation execution units.
Re:Does a compiler use all x86 instructions? (2010) by Someone on Wednesday August 24, 2016 @03:32PM (#12354610)
The lea instruction is designed for array indexing. See http://stackoverflow.com/a/1665570
Re:Does a compiler use all x86 instructions? (2010) by rdtsc on Wednesday August 24, 2016 @06:21PM (#12355703)
That's what I mean by pointer arithmetic. It seems it was intended to calculate memory addresses.

It happens to do adds and multiplications as well.

I understand things like a[i] would be equivalent in general to *(a+i)

Re:Does a compiler use all x86 instructions? (2010) by valleyer on Wednesday August 24, 2016 @04:15PM (#12354893)
Your username is an effective proof that the answer to the title question is "no". :)
Re:Does a compiler use all x86 instructions? (2010) by rdtsc on Wednesday August 24, 2016 @06:26PM (#12355737)
Sorry didn't get the joke, how does it interact with reading the timestamp counter?

Isn't LEA intended to calculate addresses? It seems it also doesn't alter flags and can put its result in any register.

Re:Does a compiler use all x86 instructions? (2010) by valleyer on Thursday August 25, 2016 @02:45AM (#12357293)
The title question, not anything to do with lea. I at least have never seen a compiler emit a rdtsc. (If there is some case where it does [presumably a benchmarking mode?] I'd be interested to hear of it.)
Re:Does a compiler use all x86 instructions? (2010) by rdtsc on Thursday August 25, 2016 @10:44AM (#12359372)
Heh ;-) I see.

Hmm, can't see it being emitted unless used in profiling code. But then as an inline assembly.

Now rdtscp (notice the "p" at the end) is a fencing instruction as well! So I can see someone using that for barriers or when doing serialization.

(Or I guess if a language somehow include getting a fast spinning counter as a feature that could end up as rdtsc* instruction).

Re:Does a compiler use all x86 instructions? (2010) by JoeAltmaier on Wednesday August 24, 2016 @12:39PM (#12353270)
Pointer creation I think.
Re:Does a compiler use all x86 instructions? (2010) by rdtsc on Wednesday August 24, 2016 @06:34PM (#12355773)
Originally it was for address calculations it seems. But can also be used to do other calculations not just for addresses.

Which I assumed to be equivalent to something like: res = ptr_base + offset