120 likes | 210 Vues
Explore the advantages and disadvantages of optimizing in SSA form, including global constant propagation, dead code elimination, and global value numbering. Learn how to convert back to CFG form and incorporate branch folding for improved code efficiency.
E N D
Objectives • Identify advantages (and disadvantages ?) of optimizing in SSA form • Given a CFG in SSA form, perform • Global Constant Propagation • Dead code elimination • Global Value Numbering • Conversion of SSA back to CFG form
Constant Propagation • Operate on sparse graph (SSA) • Incorporate branch folding • Meet operations at -nodes
EvalStmt Algorithm EvalStmt( Stmt ) if Stmt is arithmetic or -node evaluate Stmt if result is “lowered” foreach j Uses (Stmt.Lval())) propagate result if j.block Visited Work = { j } else if Stmt is a branch foreach possible destination DB of Stmt if Edge(Stmt, DB) not executable mark Edge(Stmt, DB) executable Blocks U= DB
Constant Prop Algorithm Mark all CFG Edges not executable Init node of SSA graph to T Work = Visited = Ø Blocks = {Entry} While Work Ø Blocks Ø While Work Ø Choose Stmt from Work EvalStmt (Stmt) While Blocks Ø Choose BB from Blocks foreach Stmt in (BB) EvalStmt (Stmt) if BB Visited Visited = {BB} foreach Stmt in (BB) EvalStmt (Stmt)
Dead Code Elimination • Use SSA to detect dead code • Method • Remove statement(s) that do not directly or indirectly use data observable outside the procedure • Eliminate branches never taken • Uses control dependence
EliminateDeadCode () Worklist = Necessary = Ø foreach BB CFG foreach StmtBB if (Stmt defines external data ) (Stmt is and I/O instruction) (Stmt is function call) Necessary U= { Stmt } WorkList U= { Stmt } endif endfor endfor
Eliminate Dead Code Algorithm while Worklist Ø choose Stmt from Worklist BB = Stmt’s basic block foreach block, CB, upon which BB is control dependent J = CB’s branch statement Necessary U= J Worklist U= J foreach T Stmt’s operands Def = Stmt’s definition Necessary U= {Def } WorkList U= {Def } foreach BB CFG foreach StmtinBB if Stmt Necessary removeStmt else if Stmt isa branch Stmt Necessary Stmt.target=ipdom(BB)
Global Value Numbering • Apply value numbering at function scope • Associate a field (for value number) with each temporary. • Temps with same value number are = • No loops ==> reverse postorder traversal suffices (operands defined before used.)
Strongly Connected Components • What about loops ? • Value numbers for -nodes ambiguous • Assume best case • Iterate on the SCC in reverse postorder until no further changes
Processing -nodes • If -node has a value number, VN, assign VN to temp defined by -node • Consider operands with non-Null value numbers: If two have different values, assign a new value for -node. • If all operands have same value number assign that to temp defined by -node
Efficiency Refinements • If -node has non-Null value number different from its operands, don’t need a new value number • For SCC, use a temp value number table called scratch table. Once that table “stabilizes”, copy to value table.
SSA to CFG • -nodes “not executable” directly • Must convert SSA back to CFG • “Semantics” of -nodes is that they are “executed” simultaneously upon entry to block • Insert code for each operand of -node in appropriate predecessor block.