by chrisseaton · 8y ago · 0 comments
> Micro-fusion fuses multiple micro-ops from the same instruction into a single complex micro-op.
And that:
> Macro-fusion merges two instructions to a single micro-op.
The point is that the three instruction format and the single instruction format you would think would decode both to three micro-ops (or something like that), but instead the single instruction format can use a single fused micro op. That's why it's better to write as a single instruction.
And your reference backs this up - it talks about single instructions in this section on micro-op fusion.
You may be confusing it with macro-fusion, which is about fusing multiple logical instructions, which is discussed in the next section in that Agner document, but isn't discussed in this blog.
> This article is instead about whether a particular operation is better written as a single-instruction
Yes - it's better to write as a single-instruction, because then micro-op fusion applies. Macro-op fusion isn't as strong, as doesn't apply to the three logical instructions.
This is not exactly what happens. A read-modify-write instruction in general results in 2 fused uops, one for the read+op, the other for the address generation+write.
In the specific case of INC, you still have 3 fused uops for unclear reasons---possibly because INC only partially changes the flags. Replacing the expression by ADD [memory], 1 results on the expected 2 fused ops.
Edit:
According the this pdf, macro/micro fusion happens very early on macro decoding, but I'm still not sure how it counts retired uops:
http://pc.watch.impress.co.jp/video/pcw/docs/601/161/p21.pdf
And does this mean micro fusion cannot occur when you use the separate load/op/store instructions?
If you use the three-instruction sequence the load and ALU op can't fuse, which potentially makes it slower (but not in this case since the bottleneck is elsewhere).
It's quite unusual that add gets the 2-uop treatment but inc doesn't. Yes, they treat flags differently, but that's mostly been resolved though flag renaming, and the reg forms of inc don't suffer any penalty.
I'll have to double check if this is true. If it is, compilers should generally be preferring add [mem], 1 then (except perhaps when optimizing for size) - the difference in the flag behavior is pretty much never relevant for compiled code.