Software Optimization Guide for AMD64 Processors | 25112 Rev. 3.06 September 2005 |
2.4Expression Order in Compound Branch Conditions
Optimization
In the most active areas of a program, order the expressions in compound branch conditions to take advantage of short circuiting of compound conditional expressions.
Application
This optimization applies to:
•
•
Rationale
Branch conditions in C programs often consist of compound conditions consisting of multiple boolean expressions joined by the logical AND (&&) and logical OR () operators. C compilers guarantee
When used to control conditional branches, expressions involving logical OR and logical 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 impossible to give an easy,
•Probability of a branch misprediction for each of the branches generated
•Additional latency incurred due to a branch misprediction
•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 misprediction probabilities on the nature of the incoming data in
It is recommended to experiment with the ordering of expressions in compound branch conditions in the most active areas of a program
14 | C and C++ | Chapter 2 |