1 / 18

Loop Transformations Chapter 14

Loop Transformations Chapter 14. Mooly Sagiv. Outline. Induction-Variable Optimizations Strength Reduction Live Variable Analysis Optimizing array bound checking. Induction Variable Optimizations.

constance
Télécharger la présentation

Loop Transformations Chapter 14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loop Transformations Chapter 14 Mooly Sagiv

  2. Outline • Induction-Variable Optimizations • Strength Reduction • Live Variable Analysis • Optimizing array bound checking

  3. 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

  4. 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

  5. A Simple ICAN Example x := init y := initz 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

  6. Identifying Induction Variables • Basic induction variables - modified by the same constant during each iteration • Dependent induction variables - depends on other induction variables

  7. 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

  8. 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:

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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))

  14. Other Usage of Live Information • Global register allocation • Find software defects • Garbage collection

  15. 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

  16. 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] ….

  17. 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)

  18. Conclusions • Two optimizations for loops • Reduction in strength • Elimination of array bound checks

More Related