I&#x27;ve recently been diving into low-level loop optimization on current Intel x64 processors.  It&#x27;s a rabbit hole, but makes me think there are considerably better optimizations that compilers could be making. Here are some thoughts on this &quot;naive&quot; loop example as they would execute on Skylake, with reference to sections in the (excellent) Intel Architectures Reference Manual:  <a href="http:&#x2F;&#x2F;www.intel.com&#x2F;content&#x2F;dam&#x2F;www&#x2F;public&#x2F;us&#x2F;en&#x2F;documents&#x2F;manuals&#x2F;64-ia-32-architectures-optimization-manual.pdf" rel="nofollow">http:&#x2F;&#x2F;www.intel.com&#x2F;content&#x2F;dam&#x2F;www&#x2F;public&#x2F;us&#x2F;en&#x2F;documents&#x2F;...</a><p>First, since the µops have already been decoded, instruction length doesn&#x27;t really matter.  Also, the limiting factor in this loop is not the execution ports, but the &quot;renamer&quot;. While we can supply 6 µops per cycle [2.1.1] and dispatch and execute up to 8 µops per cycle [2.2.2] (limited by instruction mix) only 4 (possibly fused) µops can be moved from the Decoded Instruction Queue to the Scheduler per cycle. [2.3.3.1]<p>Using &quot;xor %reg -&gt; %reg&quot; for zeroing a register is considered a special &quot;zero idiom&quot; (along with several other forms), and does not need an execution port [2.3.3.1].  It is handled in the renamer with zero latency. It does however take up one of the 4 available renamer slots, but since it&#x27;s outside the loop here this doesn&#x27;t matter.<p>The &quot;test %ecx -&gt; %ecx; jng done&quot; sequence is &quot;macro-fused&quot; to a single µop.  It&#x27;s a single both for the renamer and for execution.  These instructions need to be consecutive for this to happen. Again, outside the loop so no real difference.<p>At the top of the loop, &quot;movsxd %ebx -&gt; %rdx&quot; is a single µop, 1 cycle latency, and can execute on any of the 4 arithmetic ports.  While it can&#x27;t be substituted here, it&#x27;s interesting to note that if sign extension is not needed, &quot;mov %ebx -&gt; %edx&quot; and &quot;movzx %bl -&gt; %edx&quot; are specially treated as a &quot;zero latency move&quot;: 0 latency, no execution port, but do use a renamer slot.<p>The add from memory &quot;add [%rsi+%rdx*4] -&gt; %eax&quot; is internally treated as a single &quot;micro-fused load op&quot; when stored in the Decoded Instruction Queue.  But because it uses an addressing mode with an index register, it is &quot;un-laminated&quot; as it is transferred to the scheduler [2.3.2.4], and thus uses two renamer slots.  Since we only have 4 available slots per cycle, this can be a major reason to prefer a non-indexed address like &quot;add [reg+offset] -&gt; eax&quot; as in his unrolled example.<p>&quot;inc %ebx&quot; can perform poorly as an instruction, since it updates only part of the flag register (leaves &quot;carry&quot; unmodified).   While it doesn&#x27;t stall post-Sandy Bridge, in some cases it may require an extra µop to merge the flag register.  Intel suggests avoiding it, and using &quot;sub $1 -&gt; %ebx&quot; instead [3.5.2.6].<p>The  &quot;cmp %ecx, %ebx; jl lp&quot; pair are fused and treated as a single µop.  This is good, and loop termination should almost always keep these instructions consecutive for this reason.  Even better would be to arrange the loop so that one can update the loop counter and then check the flags resulting from the arithmetic.   The usual way to do this is to count down to zero instead of up to a maximum: &quot;sub $1 -&gt; %ebx; jnz lp&quot;.   Possibly worth noting is that fusion with add&#x2F;sub does not occur with jno (jump not overflow) or jns (jump not sign), so terminating at zero is usually the best choice.<p>(Note that I intend this as supplementary information to the article, and not a criticism.  While I agree with CJefferson and cokernel_hacker that loop termination is a more important reason for undefined overflow, I think the technical details in the article are correct.)