TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Mike Bilow
from: Jonathan de Boyne Pollard
date: 1997-02-17 00:58:48
subject: Optimisation

MB>
  > MB>
  >   > In all cases, a branch taken flushes the pipeline.
  > MB>
  >
  > JdeBP> No.
  >
  > JdeBP> It obviously doesn't flush the pipeline if it contains the
  > JdeBP> instructions for the target of the branch.  That is, after
  > JdeBP> all, what branch prediction is all about.
  >
  > JdeBP> The Cyrix branch prediction logic uses a heuristic to guess
  > JdeBP> whether to fill the pre-fetch queue with instructions from
  > JdeBP> the straight-line execution path or from the branch target.
  > JdeBP> It can get it wrong either way. It can pre-fetch from the
  > JdeBP> branch target, and the branch ends up not being taken, or it
  > JdeBP> can pre-fetch from the straight-line path, and the branch
  > JdeBP> ends up being taken.
  >
  > The only case in which a branch taken will not flush the pipeline when
  > branch prediction is in effect is if the branch target itself
  > happens to be in the pipeline.
  >
  > [...]
  >          and   eax, 55AAh
  >          cmp   eax, 0
  >          je    mainline
  >          inc   eax
  >    mainline:
MB>

You are still thinking of the world of the 486 and Pentium, which don't
have branch prediction.  Your example makes a point that is really only
interesting for the 486 and Pentium, because it shows a case where a
simple optimisation in the case of overlap within the pre-fetch queue can
improve performance on those processors.

It _isn't_ relevant to the 6x86 and Pentium Pro because branch prediction
is all about guessing how best to fill the pre-fetch queue in the first
place.  Unlike the 486, pre-fetch with branch prediction isn't sequential.
Thinking that it _is_ sequential like the 486 appears to be the mistake
that you are basing your argument upon.

In the example above, if the heuristic guesses that the branch will be
taken branch prediction causes the instruction at `mainline:' to be the
next instruction fetched after the JE.  The INC EAX is _not_ fetched.

In which case branch _not_ taken is what will cause a flush and a stall,
since the INC EAX instruction will then have to be fetched.  Branch taken
will simply use the next instruction from the queue, because it will be
the correct one.

  > JdeBP <
___
 X MegaMail 2.10 #0:

--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-725167) (2:440/4)
SEEN-BY: 50/99 54/99 270/101 620/243 625/160 711/401 413 430 934 712/311 407
SEEN-BY: 712/505 506 517 623 624 704 713/317 800/1
@PATH: 440/4 141/209 270/101 712/624 711/934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.