GCC doesn't make use of inc

隐身守侯 提交于 2019-12-01 04:46:11

Try it with -march=<your machine>. The result may be different.

However, note that add $1, %reg is not necessarily a poor choice. Although inc and dec have smaller encodings, which is attractive, they suffer from the fact that they only partially update flags, leading to false dependency problems. The Intel optimisation manual contains this comment (my emphasis):

The INC and DEC instructions modify only a subset of the bits in the flag register. This creates a dependence on all previous writes of the flag register. This is especially problematic when these instructions are on the critical path because they are used to change an address for a load on which many other instructions depend. Assembly/Compiler Coding Rule 33. (M impact, H generality) INC and DEC instructions should be replaced with ADD or SUB instructions, because ADD and SUB overwrite all flags, whereas INC and DEC do not, therefore creating false dependencies on earlier instructions that set the flags.

It may does depend on the exact optimization settings you are using (or not using). GCC can either be told to optimise for time or space (although optimising for space can sometimes be an effective way to optimise for execution time!)

Just because an instruction is available for a specialist task, doesn't mean it's necessarily the most efficient one to use.

Some of the old x86 instructions are actually implemented in microcode, not in hardware any more, because they are rarely used and aren't worth implementing in hardware. But this can make them slower. I don't know whether inc is such an instruction.

Also, if you don't tell GCC which x86 processor model you are going to be running the code on, it will have to guess at something generic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!