loop invariant computation

14
PRINCIPLES OF COMPILER PRINCIPLES OF COMPILER DESIGN DESIGN LOOP INVARIANT COMPUTATION By- Bharat P. Patil M.Sc. C.S. (Part 1) - 41

Upload: bharat-patil

Post on 19-Dec-2014

994 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Loop invariant computation

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

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

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

ExampleExample

◦Which statement is a loop-invariant?◦Which statement can we move to the preheader?

I

I

I

Page 5: Loop invariant computation

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

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

ExampleExample

I(1)

I(1) I(1)

I(2)

Page 8: Loop invariant computation

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

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

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

ExamplesExamples

outside loop

I I

I

I I

I

Page 12: Loop invariant computation

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

Landing PadsLanding Pads

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

Page 14: Loop invariant computation