1

What's the difference between

lea r12, [rsi + 1]

and

mov r12, rsi
inc r12

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

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
avighnac
  • 376
  • 4
  • 12

1 Answers1

4

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • I'd normally just post a quick comment while I look for a duplicate, but apparently [moderators would rather we post everything useful as answers](https://meta.stackoverflow.com/a/419996/224132), no matter how short. – Peter Cordes Aug 24 '22 at 05:54
  • @prl: True, and `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?](https://stackoverflow.com/q/36510095) – Peter Cordes Aug 24 '22 at 10:19