loop invariant computation
DESCRIPTION
TRANSCRIPT
![Page 1: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/1.jpg)
PRINCIPLES OF COMPILER DESIGNPRINCIPLES OF COMPILER DESIGN
LOOP INVARIANT COMPUTATION
By- Bharat P. Patil
M.Sc. C.S.(Part 1) - 41
![Page 2: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/2.jpg)
Loop Invariant Computation Loop Invariant Computation and Code Motionand Code Motion
Loop-invariant computation Algorithms for code motion Summary: 13.1.2 (global CSE), 13.2
![Page 3: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/3.jpg)
Loop-Invariant Computation Loop-Invariant Computation and Code Motionand Code Motion
Loop invariant computation◦A computation whose value does not change as
long as control stays within the loopLoop invariant code motion (LICM)◦Move a loop invariant statement within a loop
to the pre-header of the loop
![Page 4: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/4.jpg)
ExampleExample
◦Which statement is a loop-invariant?◦Which statement can we move to the preheader?
I
I
I
![Page 5: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/5.jpg)
LICM AlgorithmLICM AlgorithmObservations◦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
![Page 6: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/6.jpg)
Detecting loop invariant Detecting loop invariant computationcomputation
Compute reaching definitionsRepeat: mark A=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 CUntil no changes to the set of loop-
invariant statements occur
![Page 7: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/7.jpg)
ExampleExample
I(1)
I(1) I(1)
I(2)
![Page 8: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/8.jpg)
Conditions for Code MotionConditions 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
![Page 9: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/9.jpg)
Code Motion AlgorithmCode Motion Algorithm
Given a set of nodes in a loopCompute reaching definitionsCompute loop invariant computationCompute dominatorsFind the exits of the loop, which are nodes whose
successors are located outside loop
![Page 10: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/10.jpg)
Code Motion Details 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
Visit blocks in a reverse-postorder◦ Move the candidate statement to pre-header
if all the invariant operations it depends on have been moved
![Page 11: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/11.jpg)
ExamplesExamples
outside loop
I I
I
I I
I
![Page 12: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/12.jpg)
More Aggressive More Aggressive OptimizationsOptimizationsGamble on: most loops get executed◦Can we relax constraint of dominating all
exits? If liveness check and exception check is satisfied
![Page 13: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/13.jpg)
Landing PadsLanding Pads
Code for most loops is generated in a test-repeat-until form to simplify dominance
![Page 14: Loop invariant computation](https://reader033.vdocuments.mx/reader033/viewer/2022061211/54939278b479597c7a8b45c5/html5/thumbnails/14.jpg)