Use Const Type Qualifier; Generic Loop Hoisting - AMD Athlon Processor x86 Optimization Manual

X86 code optimization
Table of Contents

Advertisement

AMD Athlon™ Processor x86 Code Optimization

Use Const Type Qualifier

Generic Loop Hoisting

22
Use the "const" type qualifier as much as possible. This
optimization makes code more robust and may enable higher
performance code to be generated due to the additional
information available to the compiler. For example, the C
standard allows compilers to not allocate storage for objects
that are declared "const", if their address is never taken.
To improve the performance of inner loops, it is beneficial to
reduce redundant constant calculations (i.e., loop invariant
calculations). However, this idea can be extended to invariant
control structures.
The first case is that of a constant "if()" statement in a "for()"
loop.
Example 1:
for( i ... ) {
if( CONSTANT0 ) {
DoWork0( i );
} else {
DoWork1( i );
}
}
The above loop should be transformed into:
if( CONSTANT0 ) {
for( i ... ) {
DoWork0( i );
}
} else {
for( i ... ) {
DoWork1( i );
}
}
This will make your inner loops tighter by avoiding repetitious
evaluation of a known "if()" control structure. Although the
branch would be easily predicted, the extra instructions and
decode limitations imposed by branching are saved, which are
usually well worth it.
22007E/0—November 1999
// does not affect CONSTANT0
// does not affect CONSTANT0
Use Const Type Qualifier

Advertisement

Table of Contents
loading

Table of Contents