180 likes | 318 Views
Loop Transformations Chapter 14. Mooly Sagiv. Outline. Induction-Variable Optimizations Strength Reduction Live Variable Analysis Optimizing array bound checking. Induction Variable Optimizations.
E N D
Loop Transformations Chapter 14 Mooly Sagiv
Outline • Induction-Variable Optimizations • Strength Reduction • Live Variable Analysis • Optimizing array bound checking
Induction Variable Optimizations • Variables that are updated in the “same way” every loop iteration are called “induction-variables” • May be used to decrease the cost of computations in loops
A Simple Fortran Example t1 = 202 do i=1,100 t1 = t1 - 2 a(i) = t1 enddo do i=1,100 a(i) = 202 - 2 * i enddo
A Simple ICAN Example x := init y := initz for ... a := x := x {a} if a z then y := y {a} od x is induction variable z is loop invariant y is loop invariant x := init for ... a := ... x := x {a} y := x z od
Identifying Induction Variables • Basic induction variables - modified by the same constant during each iteration • Dependent induction variables - depends on other induction variables
t1 202 i 1 t3 addr(a) t4 t3 - 4 L1: t2 i >100 if t2 goto L2 t1 t1 - 2 t5 4 * i t6 t4 + t5 *t6 t1 i i + 1 goto L1 L2: t1 202 i 1 L1: t2 i >100 if t2 goto L2 t1 t1 - 2 t3 addr(a) t4 t3 - 4 t5 4 * i t6 t4 + t5 *t6 t1 i i + 1 goto L1 L2: t1 = 202 do i=1,100 t1 = t1 - 2 a(i) = t1 enddo
t1 202; i 1 t3 addr(a) t4 t3 - 4 t5 4 t6 t4 L1: t2 i >100 if t2 goto L2 t1 t1 - 2 t6 t4 + t5 *t6 t1 i i + 1 goto L1 L2: t1 202; i 1 t3 addr(a) t4 t3 - 4 t5 4 t6 t4 t7 t3 - 396 L1: t2 t6 > t7 if t2 goto L2 t1 t1 - 2 t6 t4 + t5 *t6 t1 goto L1 L2: t1 202 i 1 t3 addr(a) t4 t3 - 4 L1: t2 i >100 if t2 goto L2 t1 t1 - 2 t5 4 * i t6 t4 + t5 *t6 t1 i i + 1 goto L1 L2:
Algorithm for Identifying Induction variables • Identify loops • Identify loop invariants and constants • Identify basic induction vatiablesbiv = biv + c • Inductively identify variables j with a unique assignmentj = b * biv + c • Split multiple assignments into the same induction variables into different induction variables
Strength Reduction • Replacej = b * biv + c in loop byj = j + c1 and appropriate initialization • The general idea “finite-differening” applied to computer programs 0 3 6 9 … 3 * i 3 3 3 3 … 3 s0=0, si+1 = si +3 0 1 4 9 16 … i2 1 3 5 7 … 2i+1
Strength Reduction Algorithm • For every induction variablej = b * biv + c • Allocate a new temporary variable tj and replace the (single) assignment to j by:j tj • After assignments biv biv + c0insert an assignment tj tj + b * c0 • Put an assignment tj b * biv + c in the preheader • Replace j by tj
Eliminating Induction Variables • Induction variables can be eliminated if: • there computation is useless--- induction variable is not live • Loop tests can be replaced by the linear function
Live Variable Analysis • A variable is live at a program point if it may be used before set in a path from this point • Local information • USE - variables that may be used before set in a block • DEF - variables that must be assigned in the block • Iterative solution LVout(EXIT) = LVout(B) = B’ Succ(B) LVin(B’) LVin(B) = USE(B) (LVout(B)-DEF(B))
Other Usage of Live Information • Global register allocation • Find software defects • Garbage collection
Unnecessary Bound Checking Elimination • Many bugs arise due to references beyond array bound • But checking array bounds at run-time is expensive • Compiler can optimize a way or most of the array checks in “reasonable” programs • Compiler can issue warning of inserted checks
Pascal Example L1: ... if 1 > i trap 6 if i > 100 trap 6 t3 addr(b) t4 t3 - 4 t5 4 * i t6 t4 + t5 t1 *t6 goto L1 L2: var b: array[1…100] of integer; … for i := 1 to n do …. b[i] ….
Eliminating Checks in Loops • Assume that we need to show that lo e hi • e is loop invariant move the check to preheader block • if e= i where i is a basic induction variable such that i= i + 1 is the increment code replace the check by checking that lo init and fin < hi in the preheader • Can be generalized (e.g.,, using partial redundecy elimination Kolte & Wolf 1995)
Conclusions • Two optimizations for loops • Reduction in strength • Elimination of array bound checks