1 / 25

Code Improving Transformations Chapter 13,14

Code Improving Transformations Chapter 13,14. Mooly Sagiv. Outline. Chapter 12- Redundency Elimination Global common sub-expression elimination/Forward Substitution Loop-Invariant Code Motion Partial Redundency Elimination Code Hoisting Chapter 13 - Loop Optimization (Friday)

ajaxe
Download Presentation

Code Improving Transformations Chapter 13,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. Code Improving Transformations Chapter 13,14 Mooly Sagiv

  2. Outline • Chapter 12- Redundency Elimination • Global common sub-expression elimination/Forward Substitution • Loop-Invariant Code Motion • Partial Redundency Elimination • Code Hoisting • Chapter 13 - Loop Optimization (Friday) • Induction-Variable Optimizations • Strength Reduction • Live Variable Analysis • Optimizing array bound checking

  3. entry t1  a+2 b  t1 c  4 * b b < c N Y b1 b t1 exit Global Common Subexpression Elimination entry b  a+2 c  4 * b b < c N Y b1 b a+2 exit

  4. Who generates common subexpressions? • Functional programming style • Hidden computations in high level constructs • local variables allocated at stack frames • structure elements • array references • array bound checking • strings • ...

  5. A Simple Iterative Computation • Local information in basic blocks • EVALP - the expression that must be computed executing the block • KILLP - the expressions that may be killed by executing the block • System of equations AEinP(ENTRY) =  AEinP(B) = B’  Pred(B) AEoutP(B’) AEoutP(B) = EVALP(B)  (AEinP(B)-KILLP(B))

  6. KILLP EVALP AEinP

  7. How does this algorithm relate to Kildall’s?

  8. Using Global Information • All the expressions e  AEinP(B) are available at the beginning of B • But how can we determine • the usage of e in B • the places where e is generated?

  9. Represent available expressions as triplets <e, B, k> - e is generated in block B at position k • Local information is now tripletslocks • EVALP - the expression that must be computed executing the block • KILLP - the expressions that may be killed by executing the block • System of equations AEinP(ENTRY) =  AEinP(B) = B’  Pred(B) AEoutP(B’) AEoutP(B) = EVALP(B)  (AEinP(B)-KILLP(B))

  10. EVALP

  11. KILLP

  12. Global Subexpression Elimination • For every e s.t. <e, *, *>  AEinP(B) • Locate the first occurrence of e in B • Search backward to find if e was modified in B • If e was not modified then: • generate a new temporary tj • replace the occurrence of e by tj • for all <e, B’, j> AEinP(B) • assign e to tj • replace e by tj

  13. Copy Propagation • Common subexpression elimination may generate a lot of copy assignments x y • Can lead to slower programs (why?) • Solutions: • Try to avoid copies (e.g., using CFA information) • Apply copy propagation algorithm • Use register allocation with coalescing

  14. Forward Substitution • But still in many cases it my be beneficial to re-evaluate expressions • Can be implemented easily • Conclusions • LIR can be more appropriate for common subexpression-elimination • Algorithms that combines elimination with register allocation are being invented

  15. t1b+2 a t1 ab+2 cb+2 d a*b ct1 d a*b

  16. Loop Invariant Code Motion • Can be simply implemented using: • natural loops or strongly connected components • ud-chains • In many cases cannot be eliminated by the programmer

  17. A simple example t1 = 100*n t2 = 10* (n+2) do i=1,100 t3 = t1 + i * t2 do j =1,100 a(i, j) = t3+j enddo enddo do i=1,100 do j =1,100 a(i, j) = 100*n+10*(i*(n+2))+j enddo enddo

  18. Identifying Loop Invariants • An instruction is loop invariant if for every operand: • the operand is constant • all the definitions that reach this use are outside the loop • there is exactly one definition that reaches this use from inside the loop and this instruction is loop invariant

  19. Partial Redundency Elimination • An expression ispartially redundentat aprogram point if it is computed more than once along some path to that point • Generalizes loop invariants and commonsubexpression elimination • Does not require loop identification • The original formulation is bi-directed • Formulated by Knoop, Ruting, and Steffen as a series of unidirectional bit-vector problems

  20. Code Hoisting • Find expressions that are “very busy” evaluated at all paths • An expression is very busy if it is evaluated regardless of the path taken from that point • Can be used to reduce code size • Iterative solution VBout(EXIT) =  VBout(B) = B’  Succ(B) VBin(B’) VBin(B) = EVALP(B)  (VBout(B)-KILLP(B))

  21. Reassociation • Can lead to many more common subexpression/loop invariants/partial redundencies

  22. A Trivial Example do i=m,n a = b + i c = a - i d = a enddo do i=m,n a = b + i c = b d = a enddo c = b do i=m,n a = b + i d = a enddo

  23. Conclusions • Two viable options • common subexpression elimination+loop invariant code motion • partial redundency elimination • Optimizations that may help: • before? - constant propagation, reassociation • after? - copy propagation, code hoisting, dead-code elimination

More Related