User`s guide
Improving the Performance of Ported Code
4.2 Code Flow and Branch Prediction
MOVL (R0),R1 ; Get structure
.BRANCH_LIKELY
BNEQ 10$ ; Structure exists
.
(Code to deal with missing structure, which is too large for
the compiler to automatically change the branch prediction)
.
10$:
The compiler will follow the branch and will modify the code flow as described in
the previous example, moving all the code that deals with the missing structure
out of line to the end of the module.
4.2.4 How to Use .BRANCH_UNLIKELY
If your code has backward conditional branches that you know will most likely not
be taken, you can instruct the compiler to generate code using that assumption
by inserting the directive .BRANCH_UNLIKELY immediately before the branch
instruction. For example:
MOVL #QUEUE,R0 ;Get queue header
10$: MOVL (R0),R0 ;Get entry from queue
BEQL 20$ ;Forward branch assumed unlikely
. ;by default
. ;Process queue entry
.
TSTL (R0) ;More than one entry (known to be
.BRANCH_UNLIKELY ;unlikely)
BNEQ 10$ ;This branch made into forward
20$: ;conditional branch
The .BRANCH_UNLIKELY directive is used here because the compiler would
predict a backward branch to 10$ as likely to be taken. The programmer knows it
is a rare case, so the directive is used to change the branch to a forward branch,
which is predicted not taken.
There is an unconditional branch instruction at the forward branch destination
which branches back to the original destination. Again, this code fragment is
moved to a point beyond the normal routine exit point. The code that would be
generated by the previous VAX MACRO code follows:
LDQ R0, 48(R27) ;Get address of QUEUE from linkage sect.
10$: LDL R0, (R0) ;Get entry from QUEUE
BEQ R0, 20$
.
. ;Process queue entry
.
LDL R22, (R0) ;Load temporary register with (R0)
BNE R22,$L1 ;Conditional forward branch predicted
20$: ;not taken by Alpha hardware
.
.
.
(routine exit)
$L1: BR 10$ ;Branch to original destination
4–6 Improving the Performance of Ported Code