I'm reviewing disassembly of some program in Visual Studio and see the following:
001B1015 cmp ebx, edx
001B1017 jae wmain+19h (001B1019h)
001B1019 pop esi
This code is just dumb. If jae
causes a conditional jump control passes to exactly the same instruction where it would otherwise fall through.
The question is how branch prediction in the CPU will deal with it. The CPU will make a prediction whether it should "jump" or "fall through". Until they gather some statistics over previous execution of the code Intel x86 processors predict conditional jumps forward as "won't be done". So this jae
will be predicted as "won't happen, fall through" and the CPU will select the "fall through" path.
If a misprediction happens (it turns out the "fall through" path was erroneously selected) the CPU formally should reset the pipeline and run the "branch" path instead.
The trick here is "fall through" and "branch" effectively do the same and are equivalent to nop
.
I've never seen this scenario analyzed in any documentation on branching.
Can a popular CPU typically treat this jae
as nop
or will it use usual prediction logic?