Home > front end >  LEA vs MOV INC for incrementing a variable and storing it in another one?
LEA vs MOV INC for incrementing a variable and storing it in another one?

Time:08-28

What's the difference between

lea r12, [rsi   1]

and

mov r12, rsi
inc r12

Is one advantageous over the other (performance-wise)?

CodePudding user response:

LEA is shorter machine code and only 1 uop instead of 2, and 1 cycle latency without relying on mov-elimination (disabled on Ice Lake). It's pretty clearly better on all existing microarchitectures (https://uops.info/ / https://agner.org/optimize/), which is why compilers use it when you compile int foo(int x){ return x 1; }

See also Using LEA on values that aren't addresses / pointers?


And re: counting uops and so on in general, What considerations go into predicting latency for operations on modern superscalar processors and how can I calculate them by hand?

inc specifically has a minor performance downside on Alder Lake E-cores or other Silvermont-family CPUs because of its partial-flag handling. INC instruction vs ADD 1: Does it matter?

lea doesn't affect FLAGS, which can occasionally be helpful with the surrounding code. Modern CPUs can rename FLAGS as many times per cycle as the pipeline width, so it's not generally a problem to write FLAGS other than adc loops or other special cases.

  • Related