Dead Code Elimination on SSA Form Dead Code Elimination & Dead code elimination • Constant Propagation Conceptually similar to mark-sweep garbage collection C t ll i il t k b ll ti > Mark useful operations on SSA Form SSA F > Everything not marked is useless > Everything not marked is useless • Need an efficient way to find and to mark useful operations > Start with critical operations > Start with critical operations > Work back up SSA edges to find their antecedents Operations defined as critical p • I/O statements, linkage code ( entry & exit blocks ), return Slides mostly based on Keith Cooper’s set of slides values, calls to other procedures (COMP 512 class at Rice University, Fall 2002). ( l ll ) Used with kind permission. Algorithm will use post-dominators & reverse dominance frontiers Using SSA 2 Dead Code Elimination on SSA Form Useless Control Flow Elimination Handling Branches Mark Sweep for each op i for each op i for each op i for each op i • When is a branch useful? clear i’s mark if i is not marked then When a useful operation depends on its existence if i is critical then > if i is a branch then mark i rewrite with a jump to j p add i to WorkList dd i t W kLi t i’s nearest useful In the CFG, j is control dependent on i if post-dominator while (Worklist ≠ Ø) remove i from WorkList 1. ∃ a non-null path p from i to j ∋ j post-dominates if i is not a jump then p p j j p ( i has form “x ← y op z” ) ( i h f “ ” ) delete i every node on p after i if def(y) is not marked then mark def(y) 2. j does not strictly post-dominate i Notes: add def(y) to WorkList add def(y) to WorkList • Eliminates some branches if def(z) is not marked then • j control dependent on i ⇒ one path from i leads to j , one doesn’t mark def(z) • Reconnects dead branches to the add def(z) to WorkList • This is the reverse dominance frontier of j ( RDF (j)) This is the reverse dominance frontier of j ( RDF (j)) remaining live code remaining live code for each b ∈ RDF (block(i)) • Find useful post-dominator by mark the block-ending walking post-dominator tree Algorithm uses RDF ( n ) to mark branches as live branch in b > Entry & exit nodes are useful > Entry & exit nodes are useful add it to WorkList dd it t W kLi t Using SSA 3 Using SSA 4
Dead Code Elimination on SSA Form Constant Propagation Safety What’s left? • • Proves that name always has known value Proves that name always has known value • Algorithm eliminates useless definitions & some useless branches • Specializes code around that value • Algorithm leaves behind empty blocks & extraneous control-flow Moves some computations to compile time ( ⇒ code motion ) m mp mp m ( ) > Exposes some unreachable blocks ( ⇒ dead code ) > Algorithm from: Cytron, Ferrante, Rosen, Wegman, & Zadeck, Efficiently Computing O Opportunity t it St ti Si Static Single Assignment Form and the l A i t F d th Control Dependence Graph , ACM TOPLAS • Value ≠ ⊥ signifies an opportunity 13(4), October 1991 Two more issues with a correction due to Rob Shillner ith ti d t R b Shill Profitability • Simplifying control-flow • Compile-time evaluation is cheaper than run-time evaluation p p • Eliminating unreachable blocks • Branch removal may lead to block coalescing ( C LEAN ) Both are CFG transformations (no need for SSA) If not, it still avoids the test & makes branch predictable > * Using SSA 5 Using SSA 6 Sparse Constant Propagation on SSA Form Sparse Constant Propagation on SSA Form How long does this algorithm take to halt? TOP TOP if its value is unknown ∀ expression, e ( ) { { • • Initialization is two passes Initialization is two passes Value(e) ← c i if it if its value is known l i k V l |ops| + 2 x |ops| edges ... c m c n ... WorkList ← Ø BOT if its value is known to vary c i c j c k c l > ∀ SSA edge s = < u,v > • Value(x) can take on 3 values ( ) i e o is “a ← b op v” or “a ← v op b” i.e. , o is a ← b op v or a ← v op b if Value(u) ≠ TOP then if Value(u) ≠ TOP then BOT BOT TOP , c i , BOT add s to WorkList > Each use can be on WorkList twice > while (WorkList ≠ Ø) 2 x | r s| 4 x | ps| ev lu ti ns W rkList pushes & p ps 2 x |args| = 4 x |ops| evaluations, WorkList pushes & pops remove s = < u v > from WorkList remove s = < u,v > from WorkList > Evaluating a Φ -node: let o be the operation that uses v Φ (x 1 ,x 2 ,x 3 , … x n ) is if Value(o) ≠ BOT then t ← result of evaluating o t ← result of evaluating o Value(x 1 ) ∧ Value(x 2 ) Value(x 3 ) Value(x ) ∧ Value(x ) Value(x ) This is an optimistic algorithm: This is an optimistic algorithm: if t ≠ Value(o) then ∧ ... ∧ Value(x n ) • Initialize all values to TOP , unless they are known constants ∀ SSA edge < o,x > Where • add < o,x > to WorkList Every value becomes BOT or c i , unless its use is uninitialized Every value becomes BOT or c i unless its use is uninitialized TOP ∧ x = x TOP ∧ x = x ∀ x ∀ x c i ∧ c j = c i if c i = c j Same result, fewer ∧ operations c i ∧ c j = BOT if c i ≠ c j Performs ∧ only at Φ nodes Performs ∧ only at Φ nodes BOT ∧ x = BOT BOT ∧ x = BOT ∀ x ∀ x Using SSA 7 Using SSA 8
Sparse Conditional Constant Propagation Sparse Conditional Constant Propagation Optimism What happens when it propagates a value into a branch? Optimism p • TOP ⇒ we gain no knowledge i k l d P • This version of the algorithm is 12 12 i 0 ← 12 TRUE or FALSE ⇒ only one path can execute } does not use this ... } Optimistic Pessimistic • Clear BOT ⇒ either path can execute an optimistic formulation while ( … ) But, the algorithm initializations initializations that i is i 1 ← Φ (i 0 ,i 3 ) i 1 ← Φ (i 0 i 3 ) ⊥ ⊥ does not use this TOP TOP • Initializes values to TOP • always x ← i 1 * 17 Leads to: Leads to: TOP ⊥ 12 at j ← i 1 i 1 ≡ 12 ∧ TOP ≡ 12 i 1 ≡ 12 ∧ ⊥ ≡ ⊥ • Prior version used ⊥ TOP ⊥ def of x x ≡ 12 * 17 ≡ 204 x ≡ ⊥ * 17 ≡ ⊥ i 2 ← … TOP ⊥ ( implicit ) ( implicit ) j ≡ 12 j 12 j ≡ ⊥ j … i 3 ≡ 12 i 3 ≡ ⊥ i 3 ← j TOP ⊥ Working this into the algorithm i 1 ≡ 12 ∧ 12 ≡ 12 In general • Use two worklists: SSAWorkList & CFGWorkList kl k & FG k • Optimism helps inside loops > SSAWorkList determines values • Largely a matter of initial value > CFGWorkList governs reachability > CFGWorkList governs reachability • Don’t propagate into operation until its block is reachable M.N. Wegman & F.K. Zadeck, Constant propagation with conditional branches, ACM TOPLAS, 13(2), April 1991, pages 181–210. branches ACM TOPLAS 13(2) April 1991 pages 181 210 * Using SSA 9 Using SSA 10 Sparse Conditional Constant Propagation Sparse Conditional Constant Propagation There are some subtle points SSAWorkList ← Ø while ((CFGWorkList ∪ SSAWorkList) ≠ Ø) • Branch conditions should not be TOP when evaluated B h diti h ld t b TOP h l t d CFGWorkList ← n 0 C GW kLi while(CFGWorkList ≠ Ø) ∀ block b remove b from CFGWorkList > Indicates an upwards-exposed use ( no initial value ) clear b’s mark mark b > Hard to envision compiler producing such code > Hard to envision compiler producing such code evaluate each Φ -function in b evaluate each Φ function in b ∀ expression e in b ∀ expression e in b evaluate each op in b, in order Value(e) ← TOP • Initialize all operations to TOP while(SSAWorkList ≠ Ø) Initialization Step p remove s = < u,v > from WorkList remove s = < u v > from WorkList > Block processing will fill in the non-top initial values Bl k i ill fill i th t i iti l l let o be the operation that contains v > Unreachable paths contribute TOP to Φ -functions To evaluate a branch t ← result of evaluating o if arg is BOT then if arg is BOT then if t V l if t ≠ Value(o) then ( ) th • Code shows CFG edges first, then SSA edges put both targets on CFGWorklist Value(o) ← t else if arg is TRUE then > Can intermix them in arbitrary order ( correctness ) ∀ SSA edge < o,x > put TRUE target on CFGWorkList if x is marked, then if x is marked, then > Taking CFG edges first may help with speed ( minor effect ) T ki CFG d fi t h l ith d ( i ff ) else if arg is FALSE then l if i FALSE th add < o,x > to WorkList put FALSE target on CFGWorkList To evaluate a jump Propagation Step p place its target on CFGWorkList g Using SSA 11 Using SSA 12
Recommend
More recommend