为什么 GCC 会发出“lea"?而不是“子"减法? [英] Why does GCC emit "lea" instead of "sub" for subtraction?
问题描述
我正在查看通过反汇编一些 C 程序生成的一些程序集,但我对我经常看到重复的单个优化感到困惑.
I am looking at some assembly that was generated by disassembling some C programs and I am confused by a single optimization that I see repeated frequently.
当我在 GCC 编译器上没有优化时使用 subl
指令进行减法,但是当我打开优化时(-O3
准确地说)编译器使用 leal
指令代替减法,示例如下:
When I have no optimizations on the GCC compiler uses the subl
instruction for subtraction, but when I do have optimizations turned on (-O3
to be precise) the compiler uses a leal
instruction instead of subtraction, example below:
没有优化:
83 e8 01 subl $0x1, %eax
有优化
8d 6f ff leal -0x1(%edi), %ebp
这两条指令的长度都是 3 个字节,所以我在这里没有看到优化.有人可以帮我解释一下编译器的选择吗?
Both of these instructions are 3 bytes long, so I am not seeing an optimization here. Could someone help me out and try to explain the compiler's choice ?
任何帮助将不胜感激.
推荐答案
如果没有看到产生这个的原始 C 代码,很难判断.
It's hard to tell without seeing the original C code that produces this.
但如果我不得不猜测的话,那是因为 leal
允许在不破坏源寄存器的情况下不正确地进行减法.
But if I had to guess, it's because the leal
allows the subtraction to be done out-of-place without destroying the source register.
这可以节省额外的寄存器移动.
This can save an extra register move.
第一个例子:
83 e8 01 subl $0x1, %eax
覆盖 %eax
从而破坏原始值.
overwrites %eax
thereby destroying the original value.
第二个例子:
8d 6f ff leal -0x1(%edi), %ebp
将 %edi - 1
存储到 %ebp
中.%edi
保留以备将来使用.
stores %edi - 1
into %ebp
. %edi
is preserved for future use.
这篇关于为什么 GCC 会发出“lea"?而不是“子"减法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!