22.3. Avoiding jumps (all processors)
There can be many reasons why you may want reduce the number of jumps, calls and returns:
Calls and returns can be avoided by replacing small procedures with inline macros. And in many cases it is possible to reduce the number of jumps by restructuring your code. For example, a jump to a jump should be replaced by a jump to the final target. In some cases this is even possible with conditional jumps if the condition is the same or is known. A jump to a return can be replaced by a return. If you want to eliminate a return to a return, then you should not manipulate the stack pointer because that would interfere with the prediction mechanism of the return stack buffer. Instead, you can replace the preceding call with a jump. For example CALL PRO1 / RET can be replaced by JMP PRO1 if PRO1 ends with the same kind of RET.
You may also eliminate a jump by dublicating the code jumped to. This can be useful if you have a two-way branch inside a loop or before a return. Example:
A: CMP [EAX+4*EDX],ECX JE B CALL X JMP C B: CALL Y C: INC EDX JNZ A MOV ESP, EBP POP EBP RETThe jump to C may be eliminated by dublicating the loop epilog:
A: CMP [EAX+4*EDX],ECX JE B CALL X INC EDX JNZ A JMP D B: CALL Y C: INC EDX JNZ A D: MOV ESP, EBP POP EBP RETThe most often executed branch should come first here. The jump to D is outside the loop and therefore less critical. If this jump is executed so often that it needs optimizing too, then replace it with the three instructions following D.