Title: ECE540S Optimizing Compilers
1ECE540SOptimizing Compilers
- http//www.eecg.toronto.edu/voss/ece540/
- Loop Optimizations, February 12, 2003
- Muchnick, Chapter 14
2Well-behaved loops
- Fortran and Pascal have all well-behaved loops
- For C, only a subset are well-behaved, defined as
-
- for (exp1 exp2 exp3)
- stmt
- where exp1 assigns a value to an integer
variable i - exp2 compares i to a loop constant
- exp3 increments or decrements i by a loop
constant - Similar if-goto loops can also be considered
well-behaved
3Induction-Variable Optimizations
- induction-variables are variables whose
successive values form an arithmetic progression
over some part of a program, usually a loop. - A loops iterations are usually counted by an
integer variable that increases/decreases by a
constant amount each iteration - Other variables, e.g. subscripts, often follow
patterns similar to the loop-control variables.
4Induction Variables Example
- INTEGER A(100)
- INTEGER A(100) T1 202
- DO I 1,100 DO I 1,100
- A(I) 202 2I T1 T1 2
- ENDDO A(I) T1
- ENDD
- I has an initial value 1, increments by 1, and
ends as 100 - A(I) is initially assigned 200, decreases by 2,
and ends as 2 - The address of A(I) is initially addr a,
increases by 4 each iteration, and ends as (addr
a) 396 - addr a(i) (addr a) 4 i - 4
5Induction Variables Example
- t1 202 t1 202
- i 1 t3 addr a
- L1 t2 i gt 100 t4 t3 4
- if t2 goto L2 t5 4
- t1 t1 2 t6 t4
- t3 addr a t7 t3 396
- t4 t3 4 L1 t2 t6 gt t7
- t5 4 i if t2 goto L2
- t6 t4 t5 t1 t1 - 2
- t6 t1 t6 t4 t5
- i i 1 t6 t1
- GOTO L1 t5 t5 4
- L2 GOTO L1
- L2
-
- i is used to count iterations and calculate A(I)
- Induction variable optimizations improve if
preceded by constant propagation
6Strength Reduction
- Strength reduction in simplest form
- 2K KK K ltlt 1
- If induction variables are recognized
- do i 1, 100 t1 202
- a(i) 202 - 2i do i 1, 100
- end do a(i) t1 - 2
- t1 t1 2
- end do
7Induction-variable Optimizations
- Induction-variable identification
- identify variables i whose values change as i i
c. - identify variables j whose values change
according to a formula j a i b. - Strength reduction
- knowing Di c, then Dj a (i c) b - a
i - b a c. - initialize j outside the loop, and
- replace computations of j by j j Dj.
t1 202 do i 1, 100 a(i) t1 2 t1
t1 - 2 end do
do i 1, 100 a(i) 202 - 2i end do
i i1 j -2i202 Di 1 Dj -2
- Induction-variable elimination.
8Basic Induction Variables
- A variable v is said to be a basic (or
fundamental) induction variable (BIV) if all its
assignments within L are in the form
v v cwhere c is a constant or a loop
invariant.
i 1 L1 if (i gt 100) go to L2 t0 202 t1
i2 t2 t0-t1 t3 addr(a) t4 t3-4 t5
4i t6 t4t5 t6 t2 i i 1 goto L1 L2
do i 1, 100 a(i) 202 - 2i end do
9Derived Induction Variables
- We search for variables j with a single
assignment within the loop having one of the
following forms
j a i or j i a j b i or
j i b j b - i or j i - b j i
/ a
i is a BIV
- We say that j is a derived (or dependent)
induction variable (DIV) that belongs to the
family of i. - We associate with j a triplet that describes j as
a function of i
j a i (i, a, 0) j b i
(i, 1, b) j b - i (i, -1, b) j i / a
(i, 1/a, 0)
10Derived Induction Variables - Example
i 1 L1 if (i gt 100) go to L2 t0 202 t1
i2 t2 t0-t1 t3 addr(a) t4 t3-4 t5
4i t6 t4t5 t6 t2 i i 1 goto L1 L2
- Now we know that every time i increments by 1, t1
increments by 2, and t5 increments by 4.
11Derived Induction Variables
- We search for variables k with a single
assignment within the loop having one of the
following forms
k d j or k j d k e j or
k j e k e - j or k j - e k j
/ d
j is a DIV (i,a,b)
- The variable k is also a DIV that belongs to the
family of i. - We associate with k a triplet that describes k as
a function of i
k d j (i, da, db) k e j
(i, a, eb) k e - j (i, -a, eb) k
j / d (i, a/d, b/d)
12Derived Induction Variables - Example
i 1 L1 if (i gt 100) go to L2 t0 202 t1
i2 t2 t0-t1 t3 addr(a) t4 t3-4 t5
4i t6 t4t5 t6 t2 i i 1 goto L1 L2
- Now we know that every time i increments by 1, t2
decrements by 2, and t6 increments by 4.
13Derived Induction Variables
- However, for our formulae to hold we must
require that - There is no definition of i between the sole
definition of j and the sole definition of k. - The only definition of j that reaches the
definition of k is the one in the loop (i.e., not
from outside the loop).
j 4 i i i 1 k
j 1
j 1
if (i gt 100) go to L2 j j 1 L2 k j
1
14Induction-variable Identification
- We now have a simple iterative algorithm for
identifying induction variables in a loop
- We have upon termination
- a set of basic induction variables i (i, 1, 0),
and - a set of derived induction variables j, and for
each one a formula (i, a, b). - We are now set to perform strength reduction.
15Strength Reduction
- Given a loop L and a set of induction variable
families
for each BIV i for each variable j in
family i (i, a, b) create a new
variable sj initialize sj in Ls
pre-header as sj aib replace the
assignment to j by j sj after each
statement i i c in L append
sj sj ca
add sj to the family of i
16Strength Reduction
- Given a loop L and a set of induction variable
families
for each BIV i for each variable j in
family i (i, a, b) create a new
variable sj initialize sj in Ls
pre-header as sj aib replace the
assignment to j by j sj after each
statement i i c in L append
sj sj ca
add sj to the family of i
- created a new variable sj that holds values of j.
- initialize sj in pre-header.
- increment sj every time i is incremented.
- replace computation of j by j sj since sj holds
value of j.
17Strength Reduction - Example
i 1 L1 if (i gt 100) go to L2 t0 202 t1
i2 t2 t0-t1 t3 addr(a) t4 t3-4 t5
4i t6 t4t5 t6 t2 i i 1 goto L1 L2
18Induction-variable Elimination
- If the only use of some induction variables
inside a loop L is in tests, we can replace a
test of such an induction variable by that of
another, elimination the induction variable.
i 1 t0 202 t3 addr(a) t4 t3-4 s2
-2i t0 s6 4i t4 L1 if (i gt 100) go to L2
s6 s2 i i 1 s2 s2 - 2 s6 s6
4 goto L1 L2
19Induction-variable Elimination
- If the only use of some induction variables
inside a loop L is in tests, we can replace a
test of such an induction variable by that of
another, elimination the induction variable.
i 1 t0 202 t3 addr(a) t4 t3-4 s2
-2i t0 s6 4i t4 L1 if (i gt 100) go to L2
s6 s2 i i 1 s2 s2 - 2 s6 s6
4 goto L1 L2
For condition (i relop x), pick an induction
variable j such that j (i, a,
b) Compute r a x
r r b Replace (i relop x) by
(j relop r)
20Combined Optimizations - Example
i 1 t0 202 t3 addr(a) t4 t3-4 s2
-2i t0 s6 4i t4 r 4100 r r
t4 L1 if (s6 gt r) go to L2 s6 s2 s2 s2 -
2 s6 s6 4 goto L1 L2
i 1 L1 if (i gt 100) go to L2 t0 202 t1
i2 t2 t0-t1 t3 addr(a) t4 t3-4 t5
4i t6 t4t5 t6 t2 i i 1 goto L1 L2
702 additions 200 multiplications
308 additions 2 multiplications
21The Details of Induction Variable Opts
- Multiple updates of a basic induction variable
- is this ok? If so, how do we deal with it?
- Multiple updates of a derived induction variable
- is this ok? If so, how do we deal with it?
- What happens if the eliminated induction variable
is live after the loop? - Is replacing i relop x with j relop r always
straightforward?
22Multiple Updates of a BIV
i n t something L1 use of i t
something use of t i something use
of i goto L1
i n L1 use of i i i a use of
i i i b use of i goto L1
n b
t (b a)
i (a b)
23Multiple Updates of a DIV (easier)
i n L1 j ie use of j j
if use of j i i 1 goto L1
i n L1 t ie use of t j
if use of j i i 1 goto L1
? (i, e, 0)
? (i, 0, f)
24Induction Variable Live After Loop
i 1 t0 202 t3 addr(a) t4 t3-4 s2
-2i t0 s6 4i t4 r 4100 r r
t4 L1 if (s6 gt r) go to L2 s6 s2 s2 s2 -
2 s6 s6 4 goto L1 L2 i
i 1 L1 if (i gt 100) go to L2 i i
1 L2
i 101
Ooops, we eliminated i so the value is now wrong.
25i ? x ? j ? r Why can this be tricky?
What happens if a is negative? j -i
1 (i, -1 , 1) if ( i gt 100 )
? ???
j (i, a, b) Compute r a
x r r b Replace (i relop x)
by (j relop r)
(j lt -99)
What if a is loop invariant but not a known
constant?
26Unnecessary Bound-Checking Elimination
- Bound checking or range checking determines
whether the value of a variable is within
specified bounds in all of its uses in a program. - Typically used to ensure that an access to an
array element is within the bounds of the array. - var b array1..100,1..10 of integer
- i, j , s integer
- s 0
- for i 1 to 50 do
- for j 1 to 10 do
- s s bi,j
- Some languages require this, e.g. Pascal, Ada and
Java. - Some languages dont, but its good to check
anyway.
27Bound checking of bi,j
if 1 gt i trap 6 if i gt 100 trap 6 if 1 gt j trap
6 if j gt 10 trap 6 t2 addr b t3 j 1 t3 t3
100 t3 t3 i t3 te 1 t3 t3 4 t3 t2
t3 t4 t3
28The Basics of Bounds Checking
- First we need to be able to express constraints
- Use lo ? var ? hi, where lo and hi are constants
representing the minimal and maximal values of
the range and ? is a relational operator. - For the Pascal example, 1 i 100 and 1 j
10 - Can we deduce from the loop statements that these
constraints are met - If so, remove the check completely
- If not, move the check outside of the loop if
possible.
29Assume a well-behaved loop
- there is an iteration variable i with an initial
value of init and a final value of fin - i increases by 1 on each iteration (this can be
relaxed) - that only the loop-control code modifies i
- for ( i init i lt fin i)
- av
-
30Three bound checking cases
- If v is loop-invariant, we can move the check lo
v hi outside of the loop. - if it can be evaluated at compile-time, do it
- If range expression to be satisfied is lo i
hi, where i is the loop control variable - satisfied if lo init and fin hi
- if lo gt init trap
- t min(fin, hi)
- for ( i 0 i lt fin i )
for (i 0 i lt t i) - ai ai
-
- if (i lt fin) trap
-
- of course evaluate at compile-time if possible.
31Three bound checking cases (continued)
- If range expression to be satisfied is lo j
hi, where j is a DIV of the form j bi c -
- lo bi c hi
- (lo c) / b i (hi c) / b
- The transformation of the code is similar to the
previous case. - These cases can be generalized such that i need
not increment by 1 and that other than the
loop-control code modifies i.
32The GCC Pass Order
Parser (CS488) Tree Optimization (inlining,
constant folding, arithmetic simplification) RTL
Generation (generates IR) (CS488) Sibling Call
Optimization (tail recursion elimination and call
overhead reduction, SM Ch 15)) Jump Optimization
(jumps to jumps, unreachable code, unused labels
, SM Sec 18.2) CSE Constant Propagation Jump
Optimization (if CSE changed any conditional
jumps) SSA (dead code elimination) Global CSE,
Loop Invariant Code Motion, and Global Constant
and Copy Propagation Loop Optimizations (Loop
Invariant Code Motion, Strength Reduction, Loop
Unrolling) CSE Constant Propagation Jump
Optimization Data Flow Analysis (Find BBs, remove
unreachable code, dead code elimination) Instructi
on Combination IF Conversion Instruction
Scheduling Local Global Register
Allocation Instruction Scheduling Jump
Optimization Delayed Branch Scheduling
(Instruction Scheduling) Final (CS488, SM Ch6)
Dependence Analysis Parallelization Locality
Optimizations