1
play

1 Dead Code Elimination for SSA Constant Propagation Dead code - PowerPoint PPT Presentation

Using Static Single Assignment Form Variable Renaming (cont) Last Time Data Structures Constructing SSA form Stacks[v] v Holds the subscript of most recent definition of variable v, initially empty Counters[v] v Today


  1. Using Static Single Assignment Form Variable Renaming (cont) Last Time Data Structures – Constructing SSA form – Stacks[v] ∀ v Holds the subscript of most recent definition of variable v, initially empty – Counters[v] ∀ v Today Holds the current number of assignments to variable v; initially 0 – Finish naming algorithm – Transforming from SSA pop Auxiliary Routine push 1 – Using SSA for program optimization procedure GenName(variable v) – Dead-code elimination i := Counters[v] 2 4 5 13 9 12 – Constant propagation push i onto Stacks[v] – Register allocation Counters[v] := i + 1 3 6 8 7 10 11 – Copy propagation – Induction variables Use the Dominance Tree to remember the most recent definition of each variable CS380C Lecture 8 Using Static Single Assignment 1 CS380C Lecture 8 Using Static Single Assignment 2 Variable Renaming Algorithm Transformation from SSA Form procedure Rename(block b) Proposal if b previously visited return Call Rename(entry-node) – Restore original variable names ( i.e ., drop subscripts) for each φ -function p in b – Delete all φ -functions GenName(LHS(p)) and replace v with v i , where i=Top(Stack[v]) for each statement s in b (in order) for each variable v ∈ RHS(s) Complications replace v by v i , where i = Top(Stacks[v]) x 0 = − What if versions get out of order? for each variable v ∈ LHS(s) x 1 = (simultaneously live ranges) GenName(v) and replace v with v i , where i=Top(Stack[v]) = x 0 for each s ∈ succ(b) (in CFG) = x 1 j ← position in s’s φ -function corresponding to block b Alternative for each φ -function p in s Φ ( , , ) − Perform dead code elimination (to prune φ -functions) replace the j th operand of RHS(p) by v i , where i = Top(Stack[v]) for each s ∈ child(b) (in DT) Recurse using Depth First Search − Replace φ -functions with copies in predecessors Rename(s) − Rely on register allocation coalescing to remove unnecessary copies for each φ -function or statement t in b Unwind stack when done with this node for each v i ∈ LHS(t) Pop(Stack[v]) CS380C Lecture 8 Using Static Single Assignment 3 CS380C Lecture 8 Using Static Single Assignment 4 1

  2. Dead Code Elimination for SSA Constant Propagation Dead code elimination Goal while ∃ a variable v with no uses and whose def has no other side effects – Discover constant variables and expressions and propagate them forward through the program Delete the statement s that defines v for each of s’s ud-chains Uses Delete the corresponding du-chain that points to s – Evaluate expressions at compile time instead of run time x = a + b – Eliminate dead code ( e.g., debugging code) du ud If y becomes dead and there are no – Improve efficacy of other optimizations ( e.g., value numbering and other uses of x, then the assignment to software pipelining) s y = x + 3 x becomes dead, too – Contrast this approach with one that uses liveness analysis – This algorithm updates information incrementally – With liveness, we need to invoke liveness and dead code elimination iteratively until we reach a fixed point CS380C Lecture 8 Using Static Single Assignment 5 CS380C Lecture 8 Using Static Single Assignment 6 Data-Flow Analysis for Simple Constant Propagation Implementing Simple Constant Propagation Simple constant propagation Standard worklist algorithm Using tuples of lattices – V: {All constants} ∪ { ⊤ , ⊥ } – Identifies simple constants – * : c * ¨ ⊤ – For each program point, maintains one constant value for each variable = c c * ⊥ = ⊥ – O(EV) (E is the number of edges in the CFG; V is number of variables) c * d = ⊥ if c ≠ d . . . -3 -2 -1 0 1 2 3 . . . x = 1 c * d = c if c = d Problem − Inefficient, since constants may have – F: ⊥ to be propagated through irrelevant – F x ← c (In) = nodes c – F x ← y ⊕ z (In) = if c y =In y & c z =In z , then c y ⊕ c z , else ⊤ or ⊥ – . . . y = x Solution − Exploit a sparse dependence representation ( e.g., SSA) CS380C Lecture 8 Using Static Single Assignment 7 CS380C Lecture 8 Using Static Single Assignment 8 2

  3. Sparse Simple Constant Propagation Sparse Simple Constants Algorithm (Ch. 19 in Appel) Reif and Lewis algorithm worklist = all statements in SSA 3 x = 1 while worklist ≠ ∅ – Identifies simple constants Remove some statement S from worklist – Faster than Simple Constants algorithm 4 read(y) if S is x = phi(c,c,...,c) for some constant c replace S with v = c 5 a=y+z if S is x=c for some constant c SSA edges delete s from program x = 1 6 for each statement T that uses v − Explicitly connect defs with uses substitute c for x in T − How would you do this? 7 b = x worklist = worklist union {T} Main Idea − Iterate over SSA edges instead of over all CFG edges y = x CS380C Lecture 8 Using Static Single Assignment 9 CS380C Lecture 8 Using Static Single Assignment 10 Sparse Simple Constants Backward Analyses vs. Forward Analyses Complexity For forward data-flow analysis, at phi node apply meet function – O(E’) = O(EV), E’ is number of SSA edges – O(N) in practice For backward data-flow analysis? 1 v 0 :=... v 1 :=... 2 3 v 2 := φ (v 0 ,v 1 ) 4 ...v 2 ... CS380C Lecture 8 Using Static Single Assignment 11 CS380C Lecture 8 Using Static Single Assignment 12 3

  4. Static Single Information Form (SSI) Register Allocation Constructing the interference graph – Algorithm 19.17 in Appel – walk backward from each use until find def for that use – any defs found along the way cause interference – when use is in a phi function, only traverse the edge in the flow graph for the relevant predecessor Is it faster? – Liveness O(VE), where v is the # of variables and E is # of edges in CFG – Interference graph construction from Liveness is O(VE) + O(E+I), where I is the size of the interference graph. – Using SSA, interference graph construction is O(E+I) with certain restrictions. Ananian’s Masters Thesis, 1997 MIT CS380C Lecture 8 Using Static Single Assignment 13 CS380C Lecture 8 Using Static Single Assignment 14 Copy Propagation Induction Variable Identification Algorithm Types of Induction Variables worklist = all statements in SSA – Basic induction variables while worklist ≠ ∅ – Variables that are defined once in a loop by a statement of the form, Remove some statement S from worklist i=i+c (or i=i*c ), where c is a constant integer if S is x = phi(y) or x = y for each statement T that uses x – Derived induction variables replace all use of x with y – Variables that are defined once in a loop as a linear function of worklist = worklist union {T} another induction variable delete S – j = c 1 * i + c 2 – j = i /c 1 + c 2 , where c 1 and c 2 are loop invariant CS380C Lecture 8 Using Static Single Assignment 15 CS380C Lecture 8 Using Static Single Assignment 16 4

  5. Induction Variable Identification (cont) Induction Variable Identification (cont) Informal SSA-based Algorithm Informal SSA-based Algorithm (cont) – Build the SSA representation – Determining whether a variable is a function of loop invariants and its value on the current iteration – Iterate from innermost CFG loop to outermost loop – The φ -function in the cycle will have as one of its inputs a def – Find SSA cycles from inside the loop and a def from outside the loop – Each cycle may be a basic induction variable if a variable in a – The def inside the loop will be part of the cycle and will get one cycle is a function of loop invariants and its value on the current operand from the φ -function and all others will be loop invariant iteration – The operation will be plus, minus, or unary minus – Find derived induction variables as functions of loop invariants and basic induction variables CS380C Lecture 8 Using Static Single Assignment 17 CS380C Lecture 8 Using Static Single Assignment 18 Next Time Reading – Value Numbering chapter Lecture – Value Numbering CS380C Lecture 8 Using Static Single Assignment 19 5

Recommend


More recommend