Dataflow Analysis and Optimization in Compilers at University of Michigan

undefined
EECS 583 – Class 5
Dataflow Analysis
University of Michigan
January 23, 2023
Reading Material + Announcements
v
Reminder – HW 1 due tonight at midnight
»
Submit uniquename_hw1.tgz file to:
eecs583a.eecs.umich.edu:/hw1_submissions
»
Before asking questions: 1) Read all threads on piazza, 2) Think a bit
Then, post question or talk to Aditya if you are stuck
v
Today’s class
»
Compilers: Principles, Techniques, and Tools
,
A. Aho, R. Sethi, and J. Ullman, Addison-Wesley, 1988.
(Chapters: 10.5, 10.6 Edition 1; Chapters 9.2 Edition 2)
v
Material for next Monday
»
Compilers: Principles, Techniques, and Tools
,
A. Aho, R. Sethi, and J. Ullman, Addison-Wesley, 1988.
(Chapters: 10.5, 10.6, 10.9, 10.10 Edition 1; Chapters 9.2, 9.3 Edition 2)
Looking Inside the Basic Blocks:
Dataflow Analysis + Optimization
v
Control flow analysis
»
Treat BB as black box
»
Just care about branches
v
Now
»
Start looking at ops in BBs
»
What’s computed and where
v
Classical optimizations
»
Want to make the
computation more efficient
v
Ex: Common Subexpression
Elimination (CSE)
»
Is r2 + r3 redundant?
»
Is r4 – r5 redundant?
»
What if there were 1000 BB’s
»
Dataflow analysis !!
r1 = r2 + r3
r6 = r4 – r5
r4 = 4
r6 = 8
r6 = r2 + r3
r7 = r4 – r5
Dataflow Analysis Introduction
Which VRs contain useful 
data values? (liveness or upward
exposed uses)
Which definitions may reach
this point? (reaching defns)
Which definitions are guaranteed
to reach this point? (available defns)
Which uses below are exposed?
(downward exposed uses)
Pick an arbitrary point in the program
r1 = r2 + r3
r6 = r4 – r5
r4 = 4
r6 = 8
r6 = r2 + r3
r7 = r4 – r5
Dataflow analysis
 – Collection of information
that summarizes the creation/destruction of
values in a program.  Used to identify legal 
optimization opportunities.
Live Variable (Liveness) Analysis
v
Defn: For each point p in a program and each variable y,
determine whether y can be used before being redefined
starting at p
v
Algorithm sketch
»
For each BB, y is live if it is used before defined in the BB or it is
live leaving the block
»
Backward dataflow analysis as propagation occurs from uses
upwards to defs
v
4 sets
»
GEN
 = set of external variables consumed in the BB
»
KILL
 = set of external variable uses killed by the BB
equivalent to set of variables defined by the BB
»
IN
 = set of variables that are live at the entry point of a BB
»
OUT
 = set of variables that are live at the exit point of a BB
Computing GEN/KILL Sets For Each BB
 
for 
each basic block in the procedure, X, 
do
    GEN(X) = 0
    KILL(X) = 0
    
for
 each operation in 
reverse
 sequential order in X, op, 
do
        
for
 each destination operand of op, dest, 
do
             
GEN(X) -= dest
             KILL(X)  += dest
        
endfor
        
for
 each source operand of op, src, 
do
             
GEN(X) += src
             KILL(X) -= src
        
endfor
    
endfor
endfor
Example – GEN/KILL Liveness Computation
1. r1 = MEM[r2+0]
2. r2 = MEM[r1 + 1]
3. r8 = r1 * r2
4. r1 = r1 + 5
5. r3 = r5 – r1
6. r7 = r3 * 2
7. r2 = 0
8. r7 = r1 + r2
9: r3 = 4
10: r3 = r3 + r7
11: r1 = r2 – r8
12: r3 = r1 * 2
BB1
BB2
BB3
BB4
Compute IN/OUT Sets for all BBs
initialize IN(X) to 0 for all basic blocks X
change = 1
while
 (change) 
do
    change = 0
    
for
 each basic block in procedure, X, 
do
        old_IN = IN(X)
        
OUT(X) = Union(IN(Y)) for all successors Y of X
        IN(X) = GEN(X) + (OUT(X) – KILL(X))
        
if 
(old_IN != IN(X)) 
then
            change = 1
        
endif
    
endfor
endfor
Example – Liveness Computation
OUT = Union(IN(succs))
IN = GEN + (OUT – KILL
)
1. r1 = MEM[r2+0]
2. r2 = MEM[r1 + 1]
3. r8 = r1 * r2
4. r1 = r1 + 5
5. r3 = r5 – r1
6. r7 = r3 * 2
7. r2 = 0
8. r7 = r1 + r2
9: r3 = 4
10: r3 = r3 + r7
11: r1 = r2 – r8
12: r3 = r1 * 2
BB1
BB2
BB3
BB4
GEN = r2
KILL = r1,r8 
GEN = r1,r5
KILL = r3,r7 
GEN = r2,r3,r7,r8
KILL = r1 
GEN = r1
KILL = r2,r3,r7 
Liveness Class Problem
1. r1 = 3
2. r2 = r3
3. r3 = r4
4. r1 = r1 + 1
5. r7 = r1 * r2
6. r4 = r4 + 1
7. r4 = r3 + r2
8. r8 = 8
9. r9 = r7 + r8
OUT = Union(IN(succs))
IN = GEN + (OUT – KILL
)
GEN = r3, r4
KILL = r1, r2
GEN = r1, r2
KILL = r7
GEN = r2, r3
KILL = r4
GEN = r4
KILL = NULL
GEN = NULL
KILL = r8
GEN = r7, r8
KILL = r9
Liveness Class Problem Answer
1. r1 = 3
2. r2 = r3
3. r3 = r4
4. r1 = r1 + 1
5. r7 = r1 * r2
6. r4 = r4 + 1
7. r4 = r3 + r2
8. r8 = 8
9. r9 = r7 + r8
OUT = Union(IN(succs))
IN = GEN + (OUT – KILL
)
GEN = r3, r4
KILL = r1, r2
GEN = r1, r2
KILL = r7
GEN = r2, r3
KILL = r4
GEN = r4
KILL = NULL
GEN = NULL
KILL = r8
GEN = r7, r8
KILL = r9
OUT = NULL
IN = r7, r8
OUT = r7, r8  
 
OUT = r1, r2, r3, r4, r7, r8
OUT = r7 
 
OUT = r1, r2, r3, r4, r7
OUT = r2, r3, r4, r7 
 
OUT = r1, r2, r3, r4, r7
OUT = r1, r2, r3, r4
OUT = r7 
 
OUT = r1, r2, r3, r4, r7
IN = r7 
 
IN = r1, r2, r3, r4, r7
IN = r2, r3, r7 
 
IN = r1, r2, r3, r7
IN = r1, r2, r3, r4 
 
IN = r1, r2, r3, r4 (same!)
IN = r3, r4
IN = r4, r7 
 
IN = r1, r2, r3, r4, r7
Blue sets are the first iteration IN/OUT,
Red sets are the second iteration IN/OUT
Reaching Definition Analysis (rdefs)
v
A 
definition
 of a variable x is an 
operation
 that assigns, or
may assign, a value to x
v
A definition d 
reaches
 a point p if there is a path from the
point immediately following d to p such that d is not
“killed” along that path
v
A definition of a variable is 
killed
 between 2 points when
there is another definition of that variable along the path
»
r1 = r2 + r3 kills previous definitions of r1
v
Liveness vs Reaching defs
»
Liveness 
 variables (e.g., virtual registers), don’t care about
specific users
»
Reaching defs 
 operations, each def is different
»
Forward dataflow analysis as propagation occurs from defs
downwards (liveness was backward analysis)
Compute Rdef GEN/KILL Sets for each BB
for 
each basic block in the procedure, X, 
do
    GEN(X) = 0
    KILL(X) = 0
    
for
 each operation in sequential order in X, op, 
do
        
for
 each destination operand of op, dest, 
do
             
G = op
             K = {all ops which define dest – op}
             GEN(X) = G + (GEN(X) – K)
             KILL(X) = K + (KILL(X) – G)
        
endfor
    
endfor
endwhile
GEN = set of definitions created by an operation
KILL = set of definitions destroyed by an operation
- Assume each operation only has 1 destination for simplicity
  so just keep track of “ops”..
Example GEN/KILL Rdef Calculation
1. r1 = MEM[r2+0]
2. r2 = MEM[r1 + 1]
3. r8 = r1 * r2
4. r1 = r1 + 5
5. r3 = r5 – r1
6. r7 = r3 * 2
7. r2 = 0
8. r7 = r1 + r2
9. r3 = 4
10. r3 = r3 + r7
11. r1 = r2 – r8
12. r3 = r1 * 2
BB1
BB2
BB3
BB4
G = op
K = {all ops which define dest – op}
GEN(X) = G + (GEN(X) – K)
KILL(X) = K + (KILL(X) – G)
Compute Rdef IN/OUT Sets for all BBs
initialize IN(X) = 0 for all basic blocks X
initialize OUT(X) = GEN(X) for all basic blocks X
change = 1
while
 (change) 
do
    change = 0
    
for
 each basic block in procedure, X, 
do
        old_OUT = OUT(X)
        
IN(X) = Union(OUT(Y)) for all predecessors Y of X
        OUT(X) = GEN(X) + (IN(X) – KILL(X))
        
if 
(old_OUT != OUT(X)) 
then
            change = 1
        
endif
    
endfor
endwhile
IN = set of definitions reaching the entry of BB
OUT = set of definitions leaving BB 
Example In/Out Rdef Calculation
1. r1 = MEM[r2+0]
2. r2 = MEM[r1 + 1]
3. r8 = r1 * r2
4. r1 = r1 + 5
5. r3 = r5 – r1
6. r7 = r3 * 2
7. r2 = 0
8. r7 = r1 + r2
9. r3 = 4
10. r3 = r3 + r7
11. r1 = r2 – r8
12. r3 = r1 * 2
BB1
BB2
BB3
BB4
IN = Union(OUT(preds))
OUT = GEN + (IN – KILL
)
GEN = 4,5,6
KILL = 1,8,9,10,11,12 
GEN = 1,2,3
KILL = 4,7,11 
GEN = 7,8,9
KILL = 2,5,6,10,12 
GEN = 11,12
KILL = 1,4,5,9,10 
Rdefs Homework Problem
1. r1 = 3
2. r2 = r3
3. r3 = r4
4. r1 = r1 + 1
5. r7 = r1 * r2
6. r4 = r4 + 1
7. r4 = r3 + r2
8. r8 = 8
9. r9 = r7 + r8
Compute reaching defs
    Calculate GEN/KILL for each BB
    Calculate IN/OUT for each BB
Rdefs Homework Problem –
Answer
1. r1 = 3
2. r2 = r3
3. r3 = r4
4. r1 = r1 + 1
5. r7 = r1 * r2
6. r4 = r4 + 1
7. r4 = r3 + r2
8. r8 = 8
9. r9 = r7 + r8
Compute reaching defs
    Calculate GEN/KILL for each BB
    Calculate IN/OUT for each BB
For solution IN/OUT sets specified as
A 
 B 
 C, A = initial state of sets,
B = after first iteration of analysis
C = after second iteration of analysis
- = empty set
GEN = 1, 2, 3
KILL = 4
GEN = 4,5
KILL = 1
GEN = 7
KILL = 6
GEN = 8
KILL = -
GEN = 9
KILL = -
GEN = 6
KILL = 7
IN = - 
 - 
 -
 
OUT = 1,2,3 
 1,2,3 
 1,2,3
 
IN = - 
 1,2,3,8 
 1,2,3,4,5,6,7,8
 
IN = - 
 2,3,4,5,8 
 2,3,4,5,6,7,8
IN = - 
 2,3,4,5,6,7,8
  
 2,3,4,5,6,7,8
IN = - 
 2,3,4,5,6,7,8 
 2,3,4,5,6,7,8
 
IN = - 
 2,3,4,5,8 
 2,3,4,5,6,7,8
OUT = 4,5 
 2,3,4,5,8 
 2,3,4,5,6,7,8
 
OUT = 7 
 2,3,4,5,7,8 
 2,3,4,5,7,8
OUT = 8 
 2,3,4,5,6,7,8 
 2,3,4,5,6,7,8
OUT = 9 
 2,3,4,5,6,7,8,9 
 2,3,4,5,6,7,8,9
 
OUT = 6 
 2,3,4,5,6,8 
 2,3,4,5,6,8
 
DU/UD Chains
v
Convenient way to access/use reaching defs info
v
Def-Use chains
»
Given a def, what are all the possible consumers of the
operand produced
»
Maybe consumer
v
Use-Def chains
»
Given a use, what are all the possible producers of the
operand consumed
»
Maybe producer
Example – DU/UD Chains
1. r1 = 3
2. r2 = r3
3. r3 = r4
4. r1 = r1 + 1
5. r7 = r1 * r2
6. r4 = r4 + 1
7. r4 = r3
8. r8 = 8
9. r9 = r7 + r8
Generalizing Dataflow Analysis
v
Transfer function
»
How information is changed by “something” (BB)
»
OUT = GEN + (IN – KILL)  /* forward analysis */
»
IN = GEN + (OUT – KILL)  /* backward analysis */
v
Meet function
»
How information from multiple paths is combined
»
IN = Union(OUT(predecessors))  /* forward analysis */
»
OUT = Union(IN(successors))  /* backward analysis */
v
Generalized dataflow algorithm
»
while (change)
change = false
for each BB
u
apply meet function
u
apply transfer functions
u
if any changes 
 change = true
What About All Path Problems?
v
Up to this point
»
Any path problems (maybe relations)
Definition reaches along some path
Some sequence of branches in which def reaches
Lots of defs of the same variable may reach a point
»
Use of 
Union operator
 in meet function
v
All-path: Definition guaranteed to reach
»
Regardless of sequence of branches taken, def reaches
»
Can always count on this
»
Only 1 def can be guaranteed to reach
»
Availability (as opposed to reaching)
Available definitions
Available expressions (could also have reaching expressions, but not
that useful)
Reaching vs Available Definitions
1:r1 = r2 + r3
2:r6 = r4 – r5
3:r4 = 4
4:r6 = 8
5:r6 = r2 + r3
6:r7 = r4 – r5
1,2,3,4 reach
1 available
1,2 reach
1,2 available
1,3,4 reach
1,3,4 available
1,2 reach
1,2 available
Available Definition Analysis (Adefs)
v
A definition d is 
available
 at a point p if along 
all
 paths
from d to p, d is not killed
v
Remember, a definition of a variable is 
killed
 between 2
points when there is another definition of that variable
along the path
»
r1 = r2 + r3 kills previous definitions of r1
v
Algorithm
»
Forward dataflow analysis as propagation occurs from defs
downwards
»
Use the Intersect function as the meet operator to guarantee the
all-path requirement
»
GEN/KILL/IN/OUT similar to reaching defs
Initialization of IN/OUT is the tricky part
Compute GEN/KILL Sets for each BB (Adefs)
for 
each basic block in the procedure, X, 
do
    GEN(X) = 0
    KILL(X) = 0
    
for
 each operation in sequential order in X, op, 
do
        
for
 each destination operand of op, dest, 
do
             
G = op
             K = {all ops which define dest – op}
             GEN(X) = G + (GEN(X) – K)
             KILL(X) = K + (KILL(X) – G)
        
endfor
    
endfor
endwhile
Exactly the same as reaching defs !!!
Compute IN/OUT Sets for all BBs (Adefs)
U = universal set of all operations in the Procedure
IN(0) = 0
OUT(0) = GEN(0)
for each basic block in procedure, W, (W != 0), do
    IN(W) = 0
    OUT(W) = U – KILL(W)
change = 1
while
 (change) 
do
    change = 0
    
for
 each basic block in procedure, X, 
do
        old_OUT = OUT(X)
        
IN(X) = 
Intersect
(OUT(Y)) for all predecessors Y of X
        OUT(X) = GEN(X) + (IN(X) – KILL(X))
        
if 
(old_OUT != OUT(X)) 
then
            change = 1
        
endif
    
endfor
endwhile
Example Adef Calculation
1. r1 = MEM[r2+0]
2. r2 = MEM[r1 + 1]
3. r8 = r1 * r2
4. r1 = r1 + 5
5. r3 = r5 – r1
6. r7 = r3 * 2
7. r2 = 0
8. r7 = r1 + r2
9. r3 = 4
10. r3 = r3 + r7
11. r1 = r2 – r8
12. r3 = r1 * 2
BB1
BB2
BB3
BB4
IN = Intersect(OUT(preds))
OUT = GEN + (IN – KILL
)
GEN = 7, 8, 9
KILL = 2, 5, 6, 10, 12
GEN = 4, 5, 6
KILL = 1, 8, 9, 10, 11, 12
GEN = 1, 2, 3
KILL = 4, 7, 11
GEN = 11, 12
KILL = 1,4,5,9,10
Example Adef Calculation - Answer
1. r1 = MEM[r2+0]
2. r2 = MEM[r1 + 1]
3. r8 = r1 * r2
4. r1 = r1 + 5
5. r3 = r5 – r1
6. r7 = r3 * 2
7. r2 = 0
8. r7 = r1 + r2
9. r3 = 4
10. r3 = r3 + r7
11. r1 = r2 – r8
12. r3 = r1 * 2
BB1
BB2
BB3
BB4
IN = Intersect(OUT(preds))
OUT = GEN + (IN – KILL
)
GEN = 7, 8, 9
KILL = 2, 5, 6, 10, 12
IN = 0 
 1,2,3
OUT = 1,3,4,7,8,9,11 
 1,3,7,8,9
GEN = 4, 5, 6
KILL = 1, 8, 9, 10, 11, 12
GEN = 1, 2, 3
KILL = 4, 7, 11
GEN = 11, 12
KILL = 1,4,5,9,10
OUT = 2,3,6,7,8,11,12 
 3,11,12
 
OUT = 2,3,4,5,6,7 
 2,3,4,5,6
OUT = 1,2,3 
 1,2,3
IN = 0 
 0
 
IN = 
 0 
 1,2,3
 
IN = 0 
 3
 
Slide Note
Embed
Share

Explore dataflow analysis techniques and optimization methods in the context of compilers through the course EECS 583 at the University of Michigan. Learn about identifying optimization opportunities, common subexpression elimination, liveness analysis, and more to enhance program efficiency and performance.

  • Dataflow Analysis
  • Optimization
  • Compilers
  • University of Michigan
  • EECS 583

Uploaded on Sep 19, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. EECS 583 Class 5 Dataflow Analysis University of Michigan January 23, 2023

  2. Reading Material + Announcements Reminder HW 1 due tonight at midnight Submit uniquename_hw1.tgz file to: eecs583a.eecs.umich.edu:/hw1_submissions Before asking questions: 1) Read all threads on piazza, 2) Think a bit Then, post question or talk to Aditya if you are stuck Today s class Compilers: Principles, Techniques, and Tools, A. Aho, R. Sethi, and J. Ullman, Addison-Wesley, 1988. (Chapters: 10.5, 10.6 Edition 1; Chapters 9.2 Edition 2) Material for next Monday Compilers: Principles, Techniques, and Tools, A. Aho, R. Sethi, and J. Ullman, Addison-Wesley, 1988. (Chapters: 10.5, 10.6, 10.9, 10.10 Edition 1; Chapters 9.2, 9.3 Edition 2) - 1 -

  3. Looking Inside the Basic Blocks: Dataflow Analysis + Optimization Control flow analysis Treat BB as black box Just care about branches Now Start looking at ops in BBs What s computed and where Classical optimizations Want to make the computation more efficient Ex: Common Subexpression Elimination (CSE) Is r2 + r3 redundant? Is r4 r5 redundant? What if there were 1000 BB s Dataflow analysis !! r1 = r2 + r3 r6 = r4 r5 r4 = 4 r6 = 8 r6 = r2 + r3 r7 = r4 r5 - 2 -

  4. Dataflow Analysis Introduction Dataflow analysis Collection of information that summarizes the creation/destruction of values in a program. Used to identify legal optimization opportunities. r1 = r2 + r3 r6 = r4 r5 Pick an arbitrary point in the program Which VRs contain useful data values? (liveness or upward exposed uses) r4 = 4 r6 = 8 Which definitions may reach this point? (reaching defns) Which definitions are guaranteed to reach this point? (available defns) r6 = r2 + r3 r7 = r4 r5 Which uses below are exposed? (downward exposed uses) - 3 -

  5. Live Variable (Liveness) Analysis Defn: For each point p in a program and each variable y, determine whether y can be used before being redefined starting at p Algorithm sketch For each BB, y is live if it is used before defined in the BB or it is live leaving the block Backward dataflow analysis as propagation occurs from uses upwards to defs 4 sets GEN = set of external variables consumed in the BB KILL = set of external variable uses killed by the BB equivalent to set of variables defined by the BB IN = set of variables that are live at the entry point of a BB OUT = set of variables that are live at the exit point of a BB - 4 -

  6. Computing GEN/KILL Sets For Each BB for each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in reverse sequential order in X, op, do for each destination operand of op, dest, do GEN(X) -= dest KILL(X) += dest endfor for each source operand of op, src, do GEN(X) += src KILL(X) -= src endfor endfor endfor - 5 -

  7. Example GEN/KILL Liveness Computation BB1 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 BB2 BB3 4. r1 = r1 + 5 5. r3 = r5 r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9: r3 = 4 BB4 10: r3 = r3 + r7 11: r1 = r2 r8 12: r3 = r1 * 2 - 6 -

  8. Compute IN/OUT Sets for all BBs initialize IN(X) to 0 for all basic blocks X change = 1 while (change) do change = 0 for each basic block in procedure, X, do old_IN = IN(X) OUT(X) = Union(IN(Y)) for all successors Y of X IN(X) = GEN(X) + (OUT(X) KILL(X)) if (old_IN != IN(X)) then change = 1 endif endfor endfor - 7 -

  9. Example Liveness Computation OUT = Union(IN(succs)) IN = GEN + (OUT KILL) BB1 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 GEN = r2 KILL = r1,r8 BB2 BB3 4. r1 = r1 + 5 5. r3 = r5 r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9: r3 = 4 GEN = r1 KILL = r2,r3,r7 GEN = r1,r5 KILL = r3,r7 BB4 10: r3 = r3 + r7 11: r1 = r2 r8 12: r3 = r1 * 2 GEN = r2,r3,r7,r8 KILL = r1 - 8 -

  10. Liveness Class Problem OUT = Union(IN(succs)) IN = GEN + (OUT KILL) 1. r1 = 3 2. r2 = r3 3. r3 = r4 GEN = r3, r4 KILL = r1, r2 4. r1 = r1 + 1 5. r7 = r1 * r2 GEN = r1, r2 KILL = r7 GEN = r4 KILL = NULL GEN = r2, r3 KILL = r4 6. r4 = r4 + 1 7. r4 = r3 + r2 GEN = NULL KILL = r8 8. r8 = 8 GEN = r7, r8 KILL = r9 9. r9 = r7 + r8 - 9 -

  11. Liveness Class Problem Answer OUT = Union(IN(succs)) IN = GEN + (OUT KILL) IN = r3, r4 1. r1 = 3 2. r2 = r3 3. r3 = r4 GEN = r3, r4 KILL = r1, r2 Blue sets are the first iteration IN/OUT, Red sets are the second iteration IN/OUT OUT = r1, r2, r3, r4 IN = r1, r2, r3, r4 IN = r1, r2, r3, r4 (same!) 4. r1 = r1 + 1 5. r7 = r1 * r2 GEN = r1, r2 KILL = r7 OUT = r2, r3, r4, r7 OUT = r1, r2, r3, r4, r7 IN = r4, r7 IN = r1, r2, r3, r4, r7 IN = r2, r3, r7 IN = r1, r2, r3, r7 GEN = r4 KILL = NULL GEN = r2, r3 KILL = r4 OUT = r7 OUT = r1, r2, r3, r4, r7 IN = r7 IN = r1, r2, r3, r4, r7 6. r4 = r4 + 1 7. r4 = r3 + r2 OUT = r7 OUT = r1, r2, r3, r4, r7 GEN = NULL KILL = r8 OUT = r7, r8 OUT = r1, r2, r3, r4, r7, r8 8. r8 = 8 IN = r7, r8 GEN = r7, r8 KILL = r9 OUT = NULL 9. r9 = r7 + r8 - 10 -

  12. Reaching Definition Analysis (rdefs) A definition of a variable x is an operation that assigns, or may assign, a value to x A definition d reaches a point p if there is a path from the point immediately following d to p such that d is not killed along that path A definition of a variable is killed between 2 points when there is another definition of that variable along the path r1 = r2 + r3 kills previous definitions of r1 Liveness vs Reaching defs Liveness variables (e.g., virtual registers), don t care about specific users Reaching defs operations, each def is different Forward dataflow analysis as propagation occurs from defs downwards (liveness was backward analysis) - 11 -

  13. Compute Rdef GEN/KILL Sets for each BB GEN = set of definitions created by an operation KILL = set of definitions destroyed by an operation - Assume each operation only has 1 destination for simplicity so just keep track of ops .. for each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in sequential order in X, op, do for each destination operand of op, dest, do G = op K = {all ops which define dest op} GEN(X) = G + (GEN(X) K) KILL(X) = K + (KILL(X) G) endfor endfor endwhile - 12 -

  14. Example GEN/KILL Rdef Calculation G = op K = {all ops which define dest op} GEN(X) = G + (GEN(X) K) KILL(X) = K + (KILL(X) G) BB1 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 BB2 BB3 4. r1 = r1 + 5 5. r3 = r5 r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9. r3 = 4 BB4 10. r3 = r3 + r7 11. r1 = r2 r8 12. r3 = r1 * 2 - 13 -

  15. Compute Rdef IN/OUT Sets for all BBs IN = set of definitions reaching the entry of BB OUT = set of definitions leaving BB initialize IN(X) = 0 for all basic blocks X initialize OUT(X) = GEN(X) for all basic blocks X change = 1 while (change) do change = 0 for each basic block in procedure, X, do old_OUT = OUT(X) IN(X) = Union(OUT(Y)) for all predecessors Y of X OUT(X) = GEN(X) + (IN(X) KILL(X)) if (old_OUT != OUT(X)) then change = 1 endif endfor endwhile - 14 -

  16. Example In/Out Rdef Calculation IN = Union(OUT(preds)) OUT = GEN + (IN KILL) BB1 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 GEN = 1,2,3 KILL = 4,7,11 BB2 BB3 4. r1 = r1 + 5 5. r3 = r5 r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9. r3 = 4 GEN = 4,5,6 KILL = 1,8,9,10,11,12 GEN = 7,8,9 KILL = 2,5,6,10,12 BB4 10. r3 = r3 + r7 11. r1 = r2 r8 12. r3 = r1 * 2 GEN = 11,12 KILL = 1,4,5,9,10 - 15 -

  17. Rdefs Homework Problem Compute reaching defs Calculate GEN/KILL for each BB Calculate IN/OUT for each BB 1. r1 = 3 2. r2 = r3 3. r3 = r4 4. r1 = r1 + 1 5. r7 = r1 * r2 6. r4 = r4 + 1 7. r4 = r3 + r2 8. r8 = 8 9. r9 = r7 + r8 - 16 -

  18. Rdefs Homework Problem Answer Compute reaching defs Calculate GEN/KILL for each BB Calculate IN/OUT for each BB IN = - - - 1. r1 = 3 2. r2 = r3 3. r3 = r4 GEN = 1, 2, 3 KILL = 4 For solution IN/OUT sets specified as A B C, A = initial state of sets, B = after first iteration of analysis C = after second iteration of analysis - = empty set OUT = 1,2,3 1,2,3 1,2,3 IN = - 1,2,3,8 1,2,3,4,5,6,7,8 4. r1 = r1 + 1 5. r7 = r1 * r2 GEN = 4,5 KILL = 1 OUT = 4,5 2,3,4,5,8 2,3,4,5,6,7,8 IN = - 2,3,4,5,8 2,3,4,5,6,7,8 IN = - 2,3,4,5,8 2,3,4,5,6,7,8 GEN = 6 KILL = 7 OUT = 6 2,3,4,5,6,8 2,3,4,5,6,8 GEN = 7 KILL = 6 6. r4 = r4 + 1 7. r4 = r3 + r2 OUT = 7 2,3,4,5,7,8 2,3,4,5,7,8 IN = - 2,3,4,5,6,7,8 2,3,4,5,6,7,8 GEN = 8 KILL = - OUT = 8 2,3,4,5,6,7,8 2,3,4,5,6,7,8 8. r8 = 8 IN = - 2,3,4,5,6,7,8 2,3,4,5,6,7,8 GEN = 9 KILL = - 9. r9 = r7 + r8 OUT = 9 2,3,4,5,6,7,8,9 2,3,4,5,6,7,8,9 - 17 -

  19. DU/UD Chains Convenient way to access/use reaching defs info Def-Use chains Given a def, what are all the possible consumers of the operand produced Maybe consumer Use-Def chains Given a use, what are all the possible producers of the operand consumed Maybe producer - 18 -

  20. Example DU/UD Chains 1. r1 = 3 2. r2 = r3 3. r3 = r4 4. r1 = r1 + 1 5. r7 = r1 * r2 6. r4 = r4 + 1 7. r4 = r3 8. r8 = 8 9. r9 = r7 + r8 - 19 -

  21. Generalizing Dataflow Analysis Transfer function How information is changed by something (BB) OUT = GEN + (IN KILL) /* forward analysis */ IN = GEN + (OUT KILL) /* backward analysis */ Meet function How information from multiple paths is combined IN = Union(OUT(predecessors)) /* forward analysis */ OUT = Union(IN(successors)) /* backward analysis */ Generalized dataflow algorithm while (change) change = false for each BB apply meet function apply transfer functions if any changes change = true - 20 -

  22. What About All Path Problems? Up to this point Any path problems (maybe relations) Definition reaches along some path Some sequence of branches in which def reaches Lots of defs of the same variable may reach a point Use of Union operator in meet function All-path: Definition guaranteed to reach Regardless of sequence of branches taken, def reaches Can always count on this Only 1 def can be guaranteed to reach Availability (as opposed to reaching) Available definitions Available expressions (could also have reaching expressions, but not that useful) - 21 -

  23. Reaching vs Available Definitions 1:r1 = r2 + r3 2:r6 = r4 r5 1,2 reach 1,2 available 3:r4 = 4 4:r6 = 8 1,2 reach 1,2 available 1,3,4 reach 1,3,4 available 5:r6 = r2 + r3 6:r7 = r4 r5 1,2,3,4 reach 1 available - 22 -

  24. Available Definition Analysis (Adefs) A definition d is available at a point p if along all paths from d to p, d is not killed Remember, a definition of a variable is killed between 2 points when there is another definition of that variable along the path r1 = r2 + r3 kills previous definitions of r1 Algorithm Forward dataflow analysis as propagation occurs from defs downwards Use the Intersect function as the meet operator to guarantee the all-path requirement GEN/KILL/IN/OUT similar to reaching defs Initialization of IN/OUT is the tricky part - 23 -

  25. Compute GEN/KILL Sets for each BB (Adefs) Exactly the same as reaching defs !!! for each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in sequential order in X, op, do for each destination operand of op, dest, do G = op K = {all ops which define dest op} GEN(X) = G + (GEN(X) K) KILL(X) = K + (KILL(X) G) endfor endfor endwhile - 24 -

  26. Compute IN/OUT Sets for all BBs (Adefs) U = universal set of all operations in the Procedure IN(0) = 0 OUT(0) = GEN(0) for each basic block in procedure, W, (W != 0), do IN(W) = 0 OUT(W) = U KILL(W) change = 1 while (change) do change = 0 for each basic block in procedure, X, do old_OUT = OUT(X) IN(X) = Intersect(OUT(Y)) for all predecessors Y of X OUT(X) = GEN(X) + (IN(X) KILL(X)) if (old_OUT != OUT(X)) then change = 1 endif endfor endwhile - 25 -

  27. Example Adef Calculation IN = Intersect(OUT(preds)) OUT = GEN + (IN KILL) BB1 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 GEN = 1, 2, 3 KILL = 4, 7, 11 BB2 BB3 4. r1 = r1 + 5 5. r3 = r5 r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9. r3 = 4 GEN = 4, 5, 6 KILL = 1, 8, 9, 10, 11, 12 GEN = 7, 8, 9 KILL = 2, 5, 6, 10, 12 BB4 10. r3 = r3 + r7 11. r1 = r2 r8 12. r3 = r1 * 2 GEN = 11, 12 KILL = 1,4,5,9,10 - 26 -

  28. Example Adef Calculation - Answer IN = Intersect(OUT(preds)) OUT = GEN + (IN KILL) BB1 IN = 0 0 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 GEN = 1, 2, 3 KILL = 4, 7, 11 OUT = 1,2,3 1,2,3 IN = 0 1,2,3 BB2 BB3 IN = 0 1,2,3 4. r1 = r1 + 5 5. r3 = r5 r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9. r3 = 4 GEN = 4, 5, 6 KILL = 1, 8, 9, 10, 11, 12 GEN = 7, 8, 9 KILL = 2, 5, 6, 10, 12 OUT = 1,3,4,7,8,9,11 1,3,7,8,9 OUT = 2,3,4,5,6,7 2,3,4,5,6 IN = 0 3 BB4 10. r3 = r3 + r7 11. r1 = r2 r8 12. r3 = r1 * 2 GEN = 11, 12 KILL = 1,4,5,9,10 OUT = 2,3,6,7,8,11,12 3,11,12 - 27 -

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#