Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Dataflow AnalysisLecture 17
Happy Pi day
March 14, 2018
Control Flow Graphs
É Directed graph structure that represents howexecution flows during the program’s life.É Typically consider CFGs on per-method basis.
É Terminology:É PRED(A): The set of Basic Blocks that are parents
(predecessor) of basic block AÉ B ∈ PRED(A) if B→A in CFG
É SUCC(A): The set of Basic Blocks that are children(successor) of basic block AÉ B ∈ SUCC(A) if A→ B in CFG
Compiler Construction 2/37
Predecessors and SuccessorsA
B C
D
E
F
A
B C
D E
F
Compiler Construction 3/37
Example
Compiler Construction 4/37
Example (2) main_0:a <- 2b <- 3if a = bgoto main_1
else goto main_2
main_1:jmp main_4 ; why?
main_2:c <- 5jmp main_3
main_3:return
main_4:if i < 10goto main_6
else goto main_5
main_5:r0 <- new Object ; why?jmp main_3
main_6:c <- c + b;i <- i += 1;jmp main_4
Compiler Construction 5/37
Dataflow Analysis
É For any dataflow analysis, we come up with aset of rules that help us determine the desiredproperty after executing particular instructionsÉ Examples for liveness forthcoming
É We maintain sets that track the propertythroughout the analysisÉ e.g., the set of variables that are alive before and
after an instruction!
É These sets get propagated around the CFGÉ The resulting sets allow us to conclude certain
program properties!
Compiler Construction 6/37
Dataflow Analysis Intuition
1. Think about an interesting program property
2. Think about a set of rules that describe howthat property changes between adjacentinstructions
3. Think real hard
4. Do something useful with the resulting analysis
Compiler Construction 7/37
Dead code elimination
É We use dataflow analysis to assess the livenessproperty of every variable in the program.Assignments to dead variables do not affectprogram output and can be removed!
Compiler Construction 8/37
Dead code eliminationÉ A statement x := rhs is dead code if x is dead
after the assignmentÉ You can delete dead code from the program
É You have to determine liveness for a variableto know whether assignments are dead!É This is not too bad—liveness is a boolean property
É In fact, we can express liveness in terms ofinformation transferred between adjacentinstructionsÉ Build up a set of rules to help us determine liveness
after each instruction!É Eventually apply these rules to reason about
liveness of variables for blocks throughout theCFG!
Compiler Construction 9/37
Liveness Analysis
É liveness is a binary property of a variable thatindicates whether the variable will be used atsome point in the futureÉ You can perform a liveness analysis over the
program’s instructions for each variableÉ liveness analysis is a type of dataflow analysis
that tells us which variables are alive at whichpoints of executionÉ Intuition: A variable must have been alive if an
instruction refers to the variableA variable is not alive if an instruction assigns to itwithout referring to that variable
Compiler Construction 10/37
Liveness rule 1
lhs := x + ...
x = Live!
x = ???
For a single instruction, we can say that x is livewhen the instruction refers to x on the rhs
LIVEin(lhs := x + ...) = {x}
Compiler Construction 11/37
Liveness rule 2
x := e
x = Dead
x = ???
For a single instruction, we can say that x is dead ifthe instruction refers to x on the lhs
LIVEin(x:=e) = {}
Compiler Construction 12/37
Liveness rule 3
s
x = unchanged
x = unchanged
For a single instruction that does not refer to x, theliveness of x is unchanged
LIVEin(s) = LIVEout(s)
Compiler Construction 13/37
Liveness rule 4p
x = Live
x=? x=? x=live x=?
In the case of a Basic Block with multiple children,we must conclude that x is live if any of thechildren have concluded that x is live!
LIVEout(p) = ∪SUCC(p){LIVEin(s)}
Compiler Construction 14/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {}
LIVE = {}
LIVE = {}y <- z + w LIVE = {}
LIVE = {}
LIVE = {}y <- 0 LIVE = {}
LIVE = {}
LIVE = {}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {}
Compiler Construction 15/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {}
LIVE = {}
LIVE = {}y <- z + w LIVE = {}
LIVE = {}
LIVE = {}y <- 0 LIVE = {}
LIVE = {}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {}
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {}
LIVE = {}
LIVE = {}y <- z + w LIVE = {}
LIVE = {x}
LIVE = {}y <- 0 LIVE = {}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {}
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {}
LIVE = {}
LIVE = {}y <- z + w LIVE = {x}
LIVE = {x}
LIVE = {}y <- 0 LIVE = {x}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {}
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {}
LIVE = {}
LIVE = {x}y <- z + w LIVE = {x}
LIVE = {x}
LIVE = {x}y <- 0 LIVE = {x}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {}
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {}
LIVE = {x}
LIVE = {x}y <- z + w LIVE = {x}
LIVE = {x}
LIVE = {x}y <- 0 LIVE = {x}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {}
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {x}
LIVE = {x}
LIVE = {x}y <- z + w LIVE = {x}
LIVE = {x}
LIVE = {x}y <- 0 LIVE = {x}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {}
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {x}
LIVE = {x}
LIVE = {x}y <- z + w LIVE = {x}
LIVE = {x}
LIVE = {x}y <- 0 LIVE = {x}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {}
LIVE = {x}
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {x}
LIVE = {x}
LIVE = {x}y <- z + w LIVE = {x}
LIVE = {x}
LIVE = {x}y <- 0 LIVE = {x}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {x}
LIVE = {x}
dead code
Compiler Construction 16/37
Liveness analysis example
LIVE = {}x <- 3 LIVE = {}B > 0 ? LIVE = {x}
LIVE = {x}
LIVE = {x}y <- z + w LIVE = {x}
LIVE = {x}
LIVE = {x}y <- 0 LIVE = {x}
LIVE = {x}
LIVE = {x}x <- x * x LIVE = {}x <- 4 LIVE = {}A < B LIVE = {x}
LIVE = {x}
dead code
dead code?
Compiler Construction 16/37
Remarks on LivenessÉ You can always reach a fixed point because the
rules only add variables to liveness sets
É You only need to change the liveness sets onceper sweep per block, so this analysis willterminate
É Once the analysis is complete, search forassignment instructions where the assignedvariable is not live!
É Liveness analysis is backwards analysis: youpush liveness sets backwards through the CFGÉ Other analysis can be forward (constant
propagation)Compiler Construction 17/37
Summary for Liveness
É All basic blocks, instructions in CFGinitialized with LIVEin = {} and LIVEout = {}É Repeat until LIVE′in = LIVEin for all blocksÉ For each block b in CFG
É
LIVEout =⋃
b′s children
LIVEin (b′s children)
É Starting with myset= LIVEout, move backwardsthrough instructions
É Remove assignee of instruction if presentÉ Add operandsÉ At top of block, LIVE′in =myset
Compiler Construction 18/37
Correctness in Livenessx <- li 3y <- li 3
z <- + y 1 y <- li 2z <- li 3
return w
What could go wrong?
Compiler Construction 19/37
CorrectnessIf we want to remove an instruction x <- rhs, thenwe must consider a correctness condition:
On every path following an instruction x <- rhs,the variable x is not alive
Remember: preserving semantics is key!
Compiler Construction 20/37
Constant Propagation
É We can also use dataflow analysis to propagateconstant values throughout the CFG
É Similar to liveness analysis, we track sets beforeand after each instruction in each CFG
É What would the transfer rules look like forconstant propagation?
Compiler Construction 21/37
Constant Propagation rule 1
s
x = ⊥
x = ⊥
For a single instruction that does not involve x, wecan say that the value of x is unknown if it wasunknown before that instruction
Cout(x, s) =⊥ if Cin(x, s) =⊥
Compiler Construction 22/37
Constant Propagation rule 2
x := c
x = ???
x = c
For a single instruction, we can say that x is c if theinstruction refers to x on the lhs and assigns thevalue c
Cout(x,x:=c) = c if c is a constant
Compiler Construction 23/37
Constant Propagation rule 3
x := f(...)
x = ???
x = >
For function calls, we conservatively assume thatwe can’t determine what the value is (i.e., x could bemany values depending on f (...)’s return value
Cout(x,x := f(...)) =>
Compiler Construction 24/37
Constant Propagation rule 4
y := ...
x = a
x = a
For a single instruction that does not refer to x, wesay the value of x is unchanged.
Cout(x,y := ...) = a if Cin(x,y := ...) = a, x 6= y
Compiler Construction 25/37
Constant Propagation rule 5
s x = >
x=? x=? x=> x=?
In the case of a Basic Block with multiple parents,we must conclude that x is > if any of the parentshave concluded that x is >!
if ∃Cout(x,pi) =>, then Cin(x, s) =>
Compiler Construction 26/37
Constant Propagation rule 6
s x = >
x=c x=? x=d x=?
In the case of a Basic Block with multiple parents,we must conclude that x is > even if more than oneparent has concluded that x is a constant value
if Cout(x,pi) = c and Cout(x,pj) = d and d 6= c, thenCin(x, s) =>
Compiler Construction 27/37
Constant Propagation rule 7
s x = c
x=c x=⊥ x=c x=⊥
In the case of a Basic Block with multiple parents,we must conclude that x is c if the parents haveconcluded x= c or x=⊥if Cout(x,pi) = c or ⊥ , then Cin(x, s) = c
Compiler Construction 28/37
Constant Propagation rule 8
s x = ⊥
x=⊥ x=⊥ x=⊥ x=⊥
In the case of a Basic Block with multiple parents,we must conclude that x=⊥ if all the parentsconclude x=⊥if ∀i . Cout(x,pi) =⊥ , then Cin(x, s) =⊥
Compiler Construction 29/37
Remarks on Constant Propagation
É We use ⊥ (bottom) to represent a value forwhich a variable is not currently known (wehaven’t seen it yet)É We use {1,2,3, ...} to represent the concrete
constant values a variable could take if we canconclude a variable takes a constant valueÉ We use > (top) to denote a variable that we
cannot conclude takes a single constant valueÉ e.g., if ... then x <- 4 else x <- 5 fiÉ We can’t conclude which value x takes!
É x=> in this case
Compiler Construction 30/37
Remarks on Constant Propagation
É Start out by saying Cin(x, s) => at the entry tothe program
É Cout(x, s) =Cout(x, s) =⊥ everywhere else
É Move from top to bottom of each block,update C values accordingly
Compiler Construction 31/37
Constant Propagation example
x = >x <- 3 x = ⊥B > 0 ? x = ⊥
x = ⊥
x = ⊥y <- z + w x = ⊥
x = ⊥
x = ⊥y <- 0 x = ⊥
x = ⊥
x = ⊥x <- x * x x = ⊥x <- 4 x = ⊥A < B x = ⊥
x = ⊥
Compiler Construction 32/37
Constant Propagation example
x = >x <- 3 x = 3B > 0 ? x = 3
x = 3
x = 3y <- z + w x = 3
x = 3
x = ?????y <- 0 x = ⊥
x = ⊥
x = ⊥x <- x * x x = ⊥x <- 4 x = ⊥A < B x = ⊥
x = ⊥
Compiler Construction 33/37
Constant Propagation example
x = >x <- 3 x = 3B > 0 ? x = 3
x = 3
x = 3y <- z + w x = 3
x = 3
x = 3y <- 0 x = 3
x = 3
x = 3x <- x * x x = 3x <- 4 x = 3A < B x = 3
x = 3
Compiler Construction 34/37
Orderings and Lattices
É Constant propagation terminates becausevalues only move from ⊥ to {1,2,3, ...} to >.Values cannot move the other way!É At most 2 iterations per block to reach >!
É We say that ⊥< c<>
>
... -1 0 1 ...
⊥Compiler Construction 35/37
General Dataflow Analysis
É Dataflow analysis helps us reason aboutprogram propertiesÉ e.g., “variable x is alive”, “variable y is always 4”
É General dataflow analysis often involves severaltraits:É Depends on knowing a property P at a particular
point in program executionÉ Proving P at any point requires knowledge of the
entire method bodyÉ P is typically undecidable
É Dataflow Analysis is conservative
Compiler Construction 36/37
ConservatismÉ We must maintain correctness
É Correctness vs. aggressiveness tradeoffÉ Either we definitely know for sure that the
property is trueÉ “x is definitely 3”
É Or we don’t know for sureÉ “can’t tell was x is”
É Err on the side of correctness—if you can’tmake a definite determination, it’s always OKto know you “don’t know”
Compiler Construction 37/37