
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
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,
■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 |