Professional Documents
Culture Documents
Latticesuseful
Latticesuseful
• Lattice:
CS412/CS413
– Set augmented with a partial order relation ⊑
– Each subset has a LUB and a GLB
Introduction to Compilers – Can define: meet ⊓, join ⊔, top ⊤, bottom ⊥
Tim Teitelbaum • Use lattice in the compiler to express information
about the program
Lecture 27: More Dataflow Analysis • To compute information: build constraints that
02 Apr 07 describe how the lattice information changes
– Effect of instructions: transfer functions
– Effect of control flow: meet operation
CS 412/413 Spring 2007 Introduction to Compilers 1 CS 412/413 Spring 2007 Introduction to Compilers 2
CS 412/413 Spring 2007 Introduction to Compilers 3 CS 412/413 Spring 2007 Introduction to Compilers 4
1
Monotonicity of Meet Forward Dataflow Analysis
• Meet operation is monotonic over L x L, i.e., • Control flow graph G with entry (start) node Bs
x1 ⊑ y1 and x2 ⊑ y2 implies (x1 ⊓ x2) ⊑ (y1 ⊓ y2) • Lattice (L, ⊑) represents information about program
– Meet operator ⊓ , top element ⊤
• Monotonic transfer functions
• Proof:
– Transfer function FI:L → L for each instruction I
– any lower bound of {x1,x2} is also a lower bound of – Can derive transfer functions FB for basic blocks
{y1,y2}, because x1 ⊑ y1 and x2 ⊑ y2
– x1 ⊓ x2 is a lower bound of {x1,x2} • Goal: compute the information at each program
– So x1 ⊓ x2 is a lower bound of {y1,y2}
point, given the information at entry of Bs is X0
– But y1 ⊓ y2 is the greatest lower bound of {y1,y2} • Require the out[B] = FB(in[B]), for all B
– Hence (x1 ⊓ x2) ⊑ (y1 ⊓ y2) solution to in[B] = ⊓ {out[B’] | B’∈pred(B)}, for all B
satisfy: in[Bs] = X0
CS 412/413 Spring 2007 Introduction to Compilers 7 CS 412/413 Spring 2007 Introduction to Compilers 8
CS 412/413 Spring 2007 Introduction to Compilers 9 CS 412/413 Spring 2007 Introduction to Compilers 10
Algorithm Efficiency
• Algorithm is inefficient
in[BS] = X0
– Effects of basic blocks re-evaluated even if the input
out[B] = ⊤, for all B
information has not changed
CS 412/413 Spring 2007 Introduction to Compilers 11 CS 412/413 Spring 2007 Introduction to Compilers 12
2
Worklist Algorithm Correctness
in[BS] = X0 • Initial algorithm is correct
out[B] = ⊤, for all B – If dataflow information does not change in the last
worklist = set of all basic blocks B iteration, then it satisfies the equations
Repeat
• Worklist algorithm is correct
Remove a node B from the worklist
in[B] = ⊓ {out[B’] | B’∈pred(B)} – Maintains the invariant that
out[B] = FB(in[B]) in[B] = ⊓ {out[B’] | B’∈pred(B)}
if out[B] has changed, then out[B] = FB(in[B])
worklist = worklist ∪ succ(B) for all the blocks B not in the worklist
Until worklist = ∅ – At the end, worklist is empty
CS 412/413 Spring 2007 Introduction to Compilers 13 CS 412/413 Spring 2007 Introduction to Compilers 14
3
Safety Safety and Precision
• Solution for live variable analysis: • Safety: dataflow equations guarantee a safe solution to the
– Sets of live variables must include each variable whose analysis problem
values will further be used in some execution
– … may also include variables never used in any execution! • Precision: a solution to an analysis problem is more precise if
it is less conservative
• The analysis is safe if it takes into account all possible
executions of the program
• Live variables analysis problem:
– … may also characterize cases which never occur in any
– Solution is more precise if the sets of live variables are smaller
execution of the program
– Solution that reports that all variables are live at each point is
– Say that the analysis is a conservative approximation of safe, but is the least precise solution
all executions
CS 412/413 Spring 2007 Introduction to Compilers 21 CS 412/413 Spring 2007 Introduction to Compilers 22
CS 412/413 Spring 2007 Introduction to Compilers 23 CS 412/413 Spring 2007 Introduction to Compilers 24
4
Better Than MOP? Summary
• Is MOP the best solution to the analysis problem? • Dataflow analysis
– sets up system of equations
• MOP computes solution for all paths in the CFG – iteratively computes MFP
• There may be paths that will – Terminates because transfer functions are monotonic
never occur in any execution and lattice has finite height
• So MOP is conservative
if (c) • Other possible solutions: FP, MOP, IDEAL
• IDEAL = solution that takes • All are safe solutions, but some are more precise:
x=1 x=2
into account only paths that FP ⊑ MFP ⊑ MOP ⊑ IDEAL
occur in some execution if (c) • MFP = MOP if distributive transfer functions
• This is the best solution y = y+2 y = x+1 • MOP and IDEAL are intractable
• … but it is undecidable • Compilers use dataflow analysis and MFP
CS 412/413 Spring 2007 Introduction to Compilers 25 CS 412/413 Spring 2007 Introduction to Compilers 26