Docsity

Lecture Slides for Reaching Definitions | CMSC 631, Study notes of Computer Science

Toggle sidebar
bg1
1
Reaching Definitions
Done many ways
How many ways can we do
Reaching Definitions?
•Data Flow analysis
•Constraint based approach
•Abstract Interpretation
•Type and Effect Systems
Running Example
y := x; // 1
z := 1; // 2
while y > 1 do { // 3
z := z*y; // 4
y := y-1 }; // 5
y := 0 // 6
Entry
1 y := x
2 z := 1
3 y > 1
4 z := z*y
5 y := y-1
6 y := 0
Exit
Data Flow Analysis
•RDexit(1) = RDentry(1) - { (y, l) | l in Lab } » {(y, 1)}
•RDexit(2) = RDentry(2) - { (z, l) | l in Lab } » {(z, 2)}
•…
•RDentry(1) = { (x, ?), (y, ?), (z, ?) }
•RDentry(2) = RDexit(1)
•RDentry(3) = RDexit(2) » RDexit(5)
•…
pf3
pf4
pf5

Partial preview of the text

Download Lecture Slides for Reaching Definitions | CMSC 631 and more Study notes Computer Science in PDF only on Docsity!

Reaching Definitions

Done many ways

How many ways can we do

Reaching Definitions?

  • Data Flow analysis
  • Constraint based approach
  • Abstract Interpretation
  • Type and Effect Systems

Running Example

y := x; // 1 z := 1; // 2 while y > 1 do { // 3 z := zy; // 4 y := y-1 }; // 5 y := 0 // 6 Entry 1 y := x 2 z := 1 3 y > 1 4 z := zy 5 y := y- 6 y := 0 Exit

Data Flow Analysis

  • RDexit(1) = RDentry(1) - { (y, l ) | l in Lab } » {(y, 1)}
  • RDexit(2) = RDentry(2) - { (z, l ) | l in Lab } » {(z, 2)}
  • …
  • RDentry(1) = { (x, ?), (y, ?), (z, ?) }
  • RDentry(2) = RDexit(1)
  • RDentry(3) = RDexit(2) » RDexit(5)
  • …

Finding a solution

  • Let F be this set of equations
  • Find RD s.t. RD = F(RD)
    • find least such solution
    • what is a non-minimal solution?

Constraint based approach

  • RDexit(1) ⊇ RDentry(1) - { (y, l ) | l in Lab }
  • RDexit(1) ⊇ {(y, 1)}
  • RDexit(2) ⊇ RDentry(2) - { (z, l ) | l in Lab }
  • RDexit(2) ⊇ {(z, 2)}
  • …
  • RDentry(1) ⊇ { (x, ?), (y, ?), (z, ?) }
  • RDentry(2) ⊇ RDexit(1)
  • RDentry(3) ⊇ RDexit(2)
  • RDentry(3) ⊇ RDexit(5)
  • …

Something different

  • [[ fn x => [x]^1 ]^2 [ fn y => [y]^3 ]^4 ]^5
  • For each function application, which function may be applied? - C( l ) = values that subexpression l can evaluate to - p( x ) = values that x can be bound to - [[ fn x => [x]^1 ]^2 [ fn y => [y]^3 ]^4 ]^5 - { fn x => [x]^1 } Õ C(2) - { fn y => [y]^3 } Õ C(4)

Static Single Assignment Form

SSA Form

  • Each variable is assigned to once
  • Each use of a variable has exactly one reaching definition
  • Makes a number of analysis techniques simpler and more effective

SSA of straight line code

Original x = 1 y = x+ x = x+ z = x+y In SSA form x 1 = 1 y 1 = x 1 + x 2 = x 1 + z 1 = x 2 +y 1

SSA with branches

Entry x = read() if x > 0 y = -x y = x write(y) Entry x 1 = read() if x 1 > 0 y 1 = -x 1 y 2 = x 1 y 3 = f(y 1 , y 2 ) write(y 3 ) Exit (^) Exit

Phi functions

  • Used when merging two values
  • Phi functions are easy to handle in analysis
    • not so easy to execute
  • To generate executable code, need to eliminate the phi functions

Why SSA?

  • More efficient
    • avoids some quadratic cases:
      • programs with M defs and N uses can require M * N def-use chains
      • for almost all realistic programs, SSA is linear in size of program
  • Makes algorithms more efficient and accurate
  • Unrelated uses of the same variable name are irrelevant

SSA with loops

Entry x = read() if x > 0 x = x+ write(x) Exit Entry x 1 = read() x 3 = f(x 1 , x 2 ) if x 3 > 0 x 2 = x 3 + write(x 3 ) Exit

Where do we place Phi

functions?

  • An assignment to x in node B generates a new assignment/phi function for x in the dominance frontier of B - which may, in turn, introduce additional assignments/phi functions for x