Lazy Code Motion and Partial Redundancy Elimination in Optimizing Compiler

Slide Note
Embed
Share

Lazy code motion, partial redundancy elimination, common subexpression elimination, and loop invariant code motion are optimization techniques used in compilers to improve code efficiency by eliminating redundant computations and moving code blocks to optimize performance. These techniques aim to delay computations, reduce redundancy, and improve register allocation for efficient execution of programs.


Uploaded on Oct 06, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Lazy Code Motion Bojian Zheng CSCD70 Spring 2018 bojian@cs.toronto.edu 1

  2. Partial Redundancy Elimination Can we replace calculations of ? + ? such that no path re-executes the same expression? subsumes Global Common Subexpression Full Redundancy Loop Invariant Code Motion Partial Redundancy for-loops 2

  3. Common Subexpression Elimination On every path reaching ? Expression ? + ? has been computed. Neither ? nor ? is overwritten after the expression. 3

  4. Loop Invariant Code Motion Given an expression ? + ? inside a loop, Does the value of ? + ? change inside the loop? Is the code executed at least once? 4

  5. Lazy Code Motion 5

  6. Lazy Code Motion The optimization of eliminating partial redundancy with the goal of delaying the computations as much as possible. How are we going to achieve this? Anticipated Expressions & Will-be-Available Expressions Postponable Expressions Used Expressions 6

  7. Our Goal Safety Maximum Redundancy Elimination Shortest Register Lifetime 7

  8. Anticipated Expressions 8

  9. Safety We cannot introduce operations that are not executed originally. Given the diagram on the right, can we insert the expression ? + ? on the right parent? 9

  10. Anticipated Expressions An expression ? is said to be anticipated at program point ? if all paths leading from ? eventually computes ? (from the values of ? s operands that are available at ?). 10

  11. Safety We cannot introduce operations that are not executed originally. Given the diagram on the right, can we insert the expression ? + ? on the right parent? NO! The reason is because ? + ? is not anticipated at the right parent. 11

  12. Critical Edge If the source has multiple successors, and the destination has multiple predecessors, then the path that is connecting them is defined as Critical Edge. 12

  13. Solution: Synthetic Block Add a basic block for every edge that leads to a basic block with multiple predecessors (not just the back edge). This simplifies the algorithm since we can always place at the beginning of the basic block. 13

  14. Example 1 What is the result after insertion at the anticipation frontier? 14

  15. Example 2: Loop Invariance Will insertion at the anticipation frontier help in this case? 15

  16. Example 3: More Complex Loop Where are we expecting to place expression ? + ?? Where will it actually be placed? 16

  17. Example 4: Complex Loop Variation Can we place expression ? + ? at the left synthetic block like what we did previously? 17

  18. Questions? Keywords: Safety Anticipated Expressions Synthetic Block 18

  19. Will-be-Available Expressions 19

  20. Complications Does the anticipation frontier approach always work? The reason is because we have not yet considered expression availability. Want to make the expression ? available wherever it is anticipated but unavailable. 20

  21. Will-be-Available Expressions An expression ? is said to be will-be-available at program point ? if it is anticipated and not subsequently killed along all paths reaching ?. Note how it is different from Available Expressions. 21

  22. Early Placement earliest ? is the set of expressions added to block ? under early placement, and is computed from the results of anticipated and will-be-available. earliest ? = anticipated.in ? in will be available ? in 22

  23. Example Where is the earliest placement? Is it different from the anticipation frontier? 23

  24. Questions? Keywords: Will-be-Available Expressions Early Placement 24

  25. Postponable Expressions 25

  26. Shortest Register Lifetime? Early Placement goes against our goal of shortest register lifetime. We want to delay creating redundancy to reduce register pressure. 26

  27. Postponable Expressions An expression ? is said to be postponable at program point ? if all paths leading to ? have seen earliest placement of ? but not a subsequent use. 27

  28. Example 28

  29. Latest Placement We define the term Latest as follows: It is ok to place the expression ?: either Earliest or Postponable . Need to place at ? if either: ? is used in ? . It is NOT ok to place in one of its successors . Latest ? = earliest ? postponable ? EUse ? postponable ? ? succ ? 29

  30. Example 30

  31. Questions? Keywords: Postponable Expressions Latest Placement 31

  32. Used Expressions 32

  33. Used Expressions An expression ? is said to be used at program point ? if there exists a path leading from ? that uses the expression before the operands are reevaluated. 33

  34. Final Placement Our code transformation goes as follows: ?, if expression ? latest ? used ? at the beginning of ?, insert ? = ?, and replace every original ? with ? 34

  35. Summary 35

Related


More Related Content