Loop invariant computation

8,032 views 14 slides Mar 20, 2013
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

No description available for this slideshow.


Slide Content

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

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

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 loop
Loop invariant code motion (LICM)
◦Move a loop invariant statement within a loop
to the pre-header of the loop

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

LICM AlgorithmLICM 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

Detecting loop invariant Detecting loop invariant
computationcomputation
Compute reaching definitions
Repeat: 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 C
Until no changes to the set of loop-
invariant statements occur

ExampleExample
I(1)
I(1)
I(1)
I(2)

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

Code Motion AlgorithmCode 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 nodes whose
successors are located outside loop

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

ExamplesExamples
outside
loop
I
I
I
I I
I

More Aggressive More Aggressive
OptimizationsOptimizations
Gamble on: most loops get executed
◦Can we relax constraint of dominating all
exits?
If liveness check and exception check is satisfied

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