Dataflow analysis

Theory and Applications

cs6463 1 Control-flow graph

 Graphical representation of runtime control-flow paths  Nodes of graph: basic blocks (straight-line computations)  Edges of graph: flows of control  Useful for collecting information about computation  Detect loops, remove redundant computations, , …  Alternative CFG: Each node contains a single statement

…… i =0; i = 0 while (i < 50) { if I < 50 t1 = b * 2; a = a + t1; t1 := b * 2; i = i + 1; a := a + t1; …… } i = i + 1; ….

cs6463 2 Building control-flow graphs Identifying basic blocks

 Input: a sequence of three-address statements  Output: a list of basic blocks  Method:  Determine each statement that starts a new basic block, including  The first statement of the input sequence  Any statement that is the target of a goto statement  Any statement that immediately follows a goto statement  Each basic block consists of  A starting statement S0 (leader of the basic block)  All statements following S0 up to but not including the next starting statement (or the end of input) …… Starting statements: i := 0 s0: if i < 50 goto s1 i := 0 goto s2 S0, s1: t1 := b * 2 goto S2 a := a + t1 S1, goto s0 S2 S2: …

cs6463 3 Building control-flow graphs

 Identify all the basic blocks  Create a flow graph node for each basic block  For each basic block B1  If B1 ends with a jump to a statement that starts basic block B2, create an edge from B1 to B2  If B1 does not end with an unconditional jump, create an edge from B1 to the basic block that immediately follows B1 in the original evaluation order

i :=0 …… i := 0 s0: if i < 50 goto s1 S0: if i < 50 goto s1 goto s2 s1: t1 := b * 2 s1: t1 := b * 2 a := a + t1 goto s2 goto s0 a := a + t1 S2: … goto s0 S2: …… cs6463 4 Example Dataflow

 A data-flow analysis problem  A variable v is live at CFG point p iff there is a path from p to a use of v along which v is not redefined  At any CFG point p, what variables are alive?  Live variable analysis can be used in  Global register allocation  Dead variables no longer need to be in registers  Useless-store elimination  Dead variable don’t need to be stored back to memory  Uninitialized variable detection  No variable should be alive at program entry point

cs6463 5 Computing live variables

 For each basic block n, let  UEVar(n)=variables used before any definition in n  VarKill(n)=variables defined (modified) in n (killed by n)

for each basic block n:S1;S2;S3;…;Sk

M VarKill := ∅ UEVar(n) := ∅ S1: m := y * z for i = 1 to k S2: y := y -z suppose Si is “x := y op z” S3: o := y * z if y ∉ VarKill UEVar(n) = UEVar(n) ∪ {y} if z ∉ VarKill UEVar(n) = UEVar(n) ∪ {z} VarKill = VarKill ∪ {x}

cs6463 6 Computing live variables

 Domain A m:=a+b n:=a+b  All variables inside a function  For each basic block n, let B  UEVar(n) p:=c+d C q:=a+b vars used before defined r:=c+d r:=c+d  VarKill(n) vars defined (killed by n) e:=b+18 e:=a+17 Goal: evaluate vars alive on D s:=a+b E t:=c+d entry to and exit from n u:=e+f u:=e+f LiveOut(n)=∪m∈succ(n)LiveIn(m) LiveIn(m)=UEVar(m) ∪ v:=a+b (LiveOut(m)-VarKill(m)) F w:=c+d ==> LiveOut(n)= ∪ m∈succ(n) (UEVar(m) ∪ m:=a+b G (LiveOut(m)-VarKill(m)) n:=c+d

cs6463 7 Algorithm: computing live variables

 For each basic block n, let  UEVar(n)=variables used before any definition in n  VarKill(n)=variables defined (modified) in n (killed by n) Goal: evaluate names of variables alive on exit from n  LiveOut(n)= ∪ (UEVar(m) ∪ (LiveOut(m) - VarKill(m)) m∈succ(n) for each basic block bi compute UEVar(bi) and VarKill(bi) LiveOut(bi) := ∅ for (chang