Software Optimization Guide for AMD64 Processors

25112 Rev. 3.06 September 2005

2.21Frequently Dereferenced Pointer Arguments

Optimization

Avoid dereferenced pointer arguments inside a function.

Application

This optimization applies to:

32-bit software

64-bit software

Rationale

Because the compiler has no knowledge of whether aliasing exists between the pointers, such dereferencing cannot be “optimized away” by the compiler. Since data may not be maintained in registers, memory traffic can significantly increase.

Many compilers have an “assume no aliasing” optimization switch. This allows the compiler to assume that two different pointers always have disjoint contents and does not require copying of pointer arguments to local variables. If your compiler does not have this type of optimization, then copy the data pointed to by the pointer arguments to local variables at the start of the function and if necessary copy them back at the end of the function.

Examples

Listing 16. Avoid

// Assumes pointers are different and q != r.

void isqrt(unsigned long a, unsigned long *q, unsigned long *r) {

*q = a;

if (a > 0) {

while (*q > (*r = a / *q)) { *q = (*q + *r) >> 1;

}

}

*r = a - *q * *q;

}

44

C and C++ Source-Level Optimizations

Chapter 2

Page 60
Image 60
AMD 250 manual Frequently Dereferenced Pointer Arguments, Listing 16. Avoid