1 / 12

Loop Invariant Computation and Code Motion

Loop Invariant Computation and Code Motion. Loop-invariant computation Algorithms for code motion Summary: 13.1.2 (global CSE), 13.2. Loop-Invariant Computation and Code Motion. Loop invariant computation A computation whose value does not change as long as control stays within the loop

sema
Download Presentation

Loop Invariant Computation and Code Motion

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 Invariant Computation and Code Motion Loop-invariant computation Algorithms for code motion Summary: 13.1.2 (global CSE), 13.2

  2. Loop-Invariant Computation and Code Motion • Loop invariant computation • A computationwhose value does not change as long as control stays within the loop • Loop invariant code motion (LICM) • Move a loop invariant statement within a loop to the pre-header of the loop ?

  3. Example • Which statement is a loop-invariant? • Which statement can we move to the preheader? I I I

  4. LICM Algorithm • Observations • Loop invariant: operands are defined outside loop or are defined by loop invariants • Code motion: not all invariant statements can be moved to the pre-header • Algorithm • Detect loop invariant computations • Check conditions for code motion • Code transformation

  5. Detecting loop invariant computation • Compute reaching definitions • Repeat:markA=B+C as invariant if • All reaching definitions of B are outside of the loop, or there is exactly one reaching definition for B and it is from a loop-invariant statement inside the loop • Check similarly for C • Until no changes to the set of loop-invariant statements occur

  6. Example I(1) I(1) I(1) I(2)

  7. Conditions for Code Motion • Correctness: movement does not change the program semantics • Performance: code should not be slowed down Need information on • Control flow of the loop: dominate all the exits • Other definitions: no other definitions of A • Other uses: all uses of A are dominated by block b OK? I

  8. Code Motion Algorithm Given a set of nodes in a loop • Compute reaching definitions • Compute loop invariant computation • Compute dominators • Find the exits of the loop, which are successor nodes located outside loop

  9. Code Motion Details • Candidate statement for code motion • Loop invariant • Located in a block that dominates all the exits of the loop • Assign to a variable not assigned to elsewhere in the loop • Located in a block that dominate all blocks in the loop that contain the use of the variable • Perform a depth-first search of the blocks • Move the candidate statement to pre-header if all the invariant operations it depends on have been moved

  10. Examples I I I I I I outside loop

  11. More Aggressive Optimizations • Gamble on: most loops get executed • Can we relax constraint of dominating all exits? • If liveness check and exception check is satisfied

  12. Landing Pads Code for most loops is generated in a test-repeat-until form to simplify dominance

More Related