HP C Programmer's Guide (92434-90009)
Chapter 4 89
Optimizing HP C Programs
Level 2 Optimization Modules
becomes:
LDI 2,r25
LDO 5(r25),r26
LDO 10(r26),r31
Induction Variables and Strength Reduction
The induction variables and strength reduction module removes expressions that are
linear functions of a loop counter and replaces each of them with a variable that contains
the value of the function. Variables of the same linear function are computed only once.
This module also simplifies the function by replacing multiplication instructions with
addition instructions wherever possible.
For example, the code:
for (i=0; i<25; i) {
r[i]=i*k;
}
becomes:
t1=0;
for (i=0; i<25; i) {
r[i] = t1;
t1 += k;
}
Local and Global Common Subexpression Elimination
The common subexpression elimination module identifies expressions that appear more
than once and have the same result, computes the result, and substitutes the result for
each occurrence of the expression. The types of subexpression include instructions that
load values from memory, as well as arithmetic evaluation.
For example, the code:
a=x+y+z;
b=x+y+w;
becomes:
t1=x+y;
a=t1+z;
b=t1+w;
Constant Folding and Propagation
Constant folding computes the value of a constant expression at compile time. For
example:
A = 10;
B=A+5;
C=4*B;
can be replaced by: