0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Loop invariant computation

1,190

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,190
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
17
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. PRINCIPLES OF COMPILER DESIGN LOOP INVARIANT COMPUTATION By- Bharat P. Patil M.Sc. C.S.(Part 1) - 41
• 2. Loop Invariant Computationand Code Motion &#xF0E3; Loop-invariant computation &#xF0E3; Algorithms for code motion &#xF0E3; Summary: 13.1.2 (global CSE), 13.2
• 3. Loop-Invariant Computationand Code Motion &#xF097; Loop invariant computation &#x25E6; A computation whose value does not change as long as control stays within the loop &#xF097; Loop invariant code motion (LICM) &#x25E6; Move a loop invariant statement within a loop to the pre-header of the loop
• 4. Example&#x25E6; Which statement is a loop-invariant?&#x25E6; Which statement can we move to the preheader? I I I
• 5. LICM Algorithm&#xF097;Observations &#x25E6; Loop invariant: operands are defined outside loop or are defined by loop invariants &#x25E6; Code motion: not all invariant statements can be moved to the pre-header&#xF097;Algorithm &#x25E6; Detect loop invariant computations &#x25E6; Check conditions for code motion &#x25E6; Code transformation
• 6. Detecting loop invariantcomputation&#xF097;Compute reaching definitions&#xF097;Repeat: mark A=B+C as invariant if &#x25E6; 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 &#x25E6; Check similarly for C&#xF097;Until no changes to the set of loop- invariant statements occur
• 7. Example I(1) I(1) I(1) I(2)
• 8. Conditions for Code Motion &#xF097; Correctness: movement does not change the program semantics &#xF097; Performance: code should not be slowed down OK? I Need information on &#xF097; Control flow of the loop: dominate all the exits &#xF097; Other definitions: no other definitions of A &#xF097; Other uses: all uses of A are dominated by block b
• 9. Code Motion AlgorithmGiven a set of nodes in a loop&#xF097; Compute reaching definitions&#xF097; Compute loop invariant computation&#xF097; Compute dominators&#xF097; Find the exits of the loop, which are nodes whose successors are located outside loop
• 10. Code Motion Details&#xF097; Candidate statement for code motion &#x25E6; Loop invariant &#x25E6; Located in a block that dominates all the exits of the loop &#x25E6; Assign to a variable not assigned to elsewhere in the loop &#x25E6; Located in a block that dominate all blocks in the loop that contain the use of the variable&#xF097; Visit blocks in a reverse-postorder &#x25E6; Move the candidate statement to pre-header if all the invariant operations it depends on have been moved
• 11. Examples I I I I I I outside loop
• 12. More AggressiveOptimizations&#xF097;Gamble on: most loops get executed &#x25E6; Can we relax constraint of dominating all exits? &#xF096; If liveness check and exception check is satisfied
• 13. Landing PadsCode for most loops is generated in a test-repeat-until form to simplify dominance