AMD 250 Computer Hardware User Manual


 
Chapter 2 C and C++ Source-Level Optimizations 41
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
2.19 Sorting Local Variables
Optimization
Sort local variables according to their type sizes, declaring those with larger type sizes ahead of those
with smaller type sizes.
Application
This optimization applies to:
32-bit software
64-bit software
Rationale
It can be helpful to presort local variables, if your compiler allocates local variables in the same order
in which they are declared in the source code. If the first variable is allocated for natural alignment, all
other variables are allocated contiguously in the order they are declared and are naturally aligned
without padding.
Some compilers do not allocate variables in the order they are declared. In these cases, the compiler
should automatically allocate variables that are naturally aligned with the minimum amount of
padding. In addition, some compilers do not guarantee that the stack is aligned suitably for the largest
type (that is, they do not guarantee quadword alignment), so that quadword operands might be
misaligned, even if this technique is used and the compiler does allocate variables in the order they
are declared.
Example
Avoid local variable declarations, when the variables are not declared in order of their type sizes:
short ga, gu, gi;
long foo, bar;
double x, y, z[3];
char a, b;
float baz;
Instead, sort the declarations according to their type sizes (largest to smallest):
double z[3];
double x, y;
long foo, bar;
float baz;
short ga, gu, gi;