AMD Athlon™ Processor x86 Code Optimization

22007E/0 — November 1999

Consider Expression Order in Compound Branch Conditions

Branch conditions in C programs are often compound conditions consisting of multiple boolean expressions joined by the boolean operators && and . C guarantees a short-circuit evaluation of these operators. This means that in the case of , the first operand to evaluate to TRUE terminates the evaluation, i.e., following operands are not evaluated at all. Similarly for &&, the first operand to evaluate to FALSE terminates the evaluation. Because of this short-circuit evaluation, it is not always possible to swap the operands of and &&. This is especially the case when the evaluation of one of the operands causes a side effect. However, in most cases the exchange of operands is possible.

When used to control conditional branches, expressions involving and && are translated into a series of conditional branches. The ordering of the conditional branches is a function of the ordering of the expressions in the compound condition, and can have a significant impact on performance. It is unfortunately not possible to give an easy, closed-form formula on how to order the conditions. Overall performance is a function of a variety of the following factors:

probability of a branch mispredict for each of the branches generated

additional latency incurred due to a branch mispredict

cost of evaluating the conditions controlling each of the branches generated

amount of parallelism that can be extracted in evaluating the branch conditions

data stream consumed by an application (mostly due to the dependence of mispredict probabilities on the nature of the incoming data in data dependent branches)

It is therefore recommended to experiment with the ordering of expressions in compound branch conditions in the most active areas of a program (so called hot spots) where most of the execution time is spent. Such hot spots can be found through the use of profiling. A "typical" data stream should be fed to the program while doing the experiments.

20

Consider Expression Order in Compound Branch Conditions

Page 36
Image 36
AMD x86 manual Consider Expression Order in Compound Branch Conditions