HP C Programmer's Guide (92434-90009)

94 Chapter4
Optimizing HP C Programs
Level 2 Optimization Modules
The register is initialized outside the loop to the loop invariant portion of the virtual
memory address expression and the register is incremented or decremented within the
loop by the loop variant portion of the virtual memory address expression. On PA-RISC,
the update of such a dedicated register can often be performed for "free" using the
base-register modification capability of load and store instructions.
The net result is that array references in loops are converted into equivalent but more
efficient pointer dereferences.
For example:
int a[10][20][30];
void example (void)
{
int i, j, k;
for (k = 0; k < 10; k++)
for (j = 0; j < 10; j++)
for (i = 0; i < 10; i++)
{
a[i][j][k] = 1;
}
}
after register reassociation is applied to the innermost loop becomes:
int a[10][20][30];
void example (void)
{
int i, j, k;
register int (*p)[20][30];
for (k = 0; k < 10; k++)
for (j = 0; j < 10; j++)
for (p = (int (*)[20][30]) a[0][j][k],i=0;i<10;i++)
{
*(p++[0][0]) = 1;
}
}
In the above example, the compiler-generated temporary register variable, p, strides
through the array a in the innermost loop. This register pointer variable is initialized
outside the innermost loop and auto-incremented within the innermost loop as a
side-effect of the pointer dereference.
Register reassociation can often enable another loop optimization. After performing the
register reassociation optimization, the loop variable may be needed only to control the
iteration count of the loop. If this is case, the original loop variable can be eliminated
altogether by using the PA-RISC ADDIB and ADDB machine instructions to control the loop
iteration count.