Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,

Similar presentations


Presentation on theme: "Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,"— Presentation transcript:

1 Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use. This primary algorithm described in this lecture is due to Rob Shillner, when he worked in the MSCP group at Rice University. To our knowledge, the only published version of the algorithm is in Chapter 10 of EaC.

2 COMP 512, Fall 20032 Dead Code Elimination Three distinct problems Useless operations  Any operation whose value is not used in some visible way  Use the SSA-based mark/sweep algorithm (D EAD ) Useless control flow  Branches to branches, empty blocks  Simple CFG-based algorithm ( Shillner’s C LEAN ) Unreachable blocks  No path from n 0 to b  b cannot execute  Simple graph reachability problem

3 COMP 512, Fall 20033 Eliminating Useless Control Flow The Problem After optimization, the CFG can contain empty blocks “Empty” blocks still end with either a branch or a jump Produces jump to jump, which wastes time & space Need to simplify the CFG & eliminate these The Algorithm (C LEAN ) Use four distinct transformations Apply them in a carefully selected order Iterate until done Devised by Rob Shillner ( 1992 ), documented by John Lu ( 1994 ) We must carefully distinguish between these Branch is conditional Jump is absolute

4 COMP 512, Fall 20034 Eliminating Useless Control Flow Transformations B1B1 B2B2 B1B1 B2B2 Eliminating redundant branches Both sides of branch target B i Neither block must be empty Replace it with a jump to B i Simple rewrite of last op in B 1 How does this happen? Rewriting other branches How do we find it? Check each branch Branch, not a jump *

5 COMP 512, Fall 20035 Eliminating Useless Control Flow Transformations Merging an empty block Empty B 1 ends in a jump Coalesce B 1 with B 2 Move B 1 ’s incoming edges Eliminates extraneous jump Faster, smaller code How does this happen? Eliminate operations in B 1 How do we find it? Test for empty block Eliminating empty blocks B2B2 B1B1 B2B2 empty

6 COMP 512, Fall 20036 Eliminating Useless Control Flow TransformationsCoalescing blocks Neither block must be empty B 1 ends with a jump B 2 has 1 predecessor Combine the two blocks Eliminates a jump How does this happen? Simplifying edges out of B 1 How do we find it? Check target of jump |preds | Combining non-empty blocks B 1 B 2 B1B1 B2B2 B 1 and B 2 should be a single basic block If one executes, both execute, in linear order. *

7 COMP 512, Fall 20037 Eliminating Useless Control Flow TransformationsJump to a branch B 1 ends with jump, B 2 is empty Eliminates pointless jump Copy branch into end of B 1 Might make B 2 unreachable How does this happen? Eliminating operations in B 2 How do we find this? Jump to empty block Hoisting branches from empty blocks B1B1 B2B2 empty B1B1 B2B2

8 COMP 512, Fall 20038 Eliminating Useless Control Flow Putting the transformations together Process the blocks in postorder  Clean up B i ’s successors before B i  Simplifies implementation & understanding At each node, apply transformations in a fixed order  Eliminate redundant branch  Eliminate empty block  Merge block with successor  Hoist branch from empty successor May need to iterate  Postorder  unprocessed successors along back edges  Can bound iterations, but a deriving tight bound is hard  Must recompute postorder between iterations Creates a jump that should be checked in the same pass { Handle jump *  Eliminates an edge  Eliminates a node  Eliminates node & edge  Adds an edge

9 COMP 512, Fall 20039 Eliminating Useless Control Flow What about an empty loop? By itself, C LEAN cannot eliminate the loop Loop body branches to itself  Branch is not redundant  Doesn’t end with a jump  Hoisting does not help Key is to eliminate self-loop  Add a new transformation?  Then, B 1 merges with B 2 B0B0 B2B2 B1B1 * Targets two distinct blocks! B0B0 B2B2 B1B1 * B0B0 B2B2 B1B1 * New transformation must recognize that B 1 is empty. Presumably, it has code to test exit condition & (probably) increment an induction variable. This requires looking at code inside B 1 and doing some sophisticated pattern matching. This seems awfully complicated. *

10 COMP 512, Fall 200310 Eliminating Useless Control Flow What about an empty loop? How to eliminate ?  Pattern matching ?  Useless code elimination ? B0B0 B2B2 B1B1 *

11 COMP 512, Fall 200311 Eliminating Useless Control Flow What about an empty loop? How to eliminate ?  Pattern matching ?  Useless code elimination ? What does D EAD do to B 1 ?  Remember, it is empty  B 1 has only one exit  So, B 1  RDF(B 2 )  B 1 ’s branch is useless  D EAD rewrites it as a jump B0B0 B2B2 B1B1 *

12 COMP 512, Fall 200312 Eliminating Useless Control Flow What about an empty loop? How to eliminate ?  Pattern matching ?  Useless code elimination ? What does D EAD do to B 1 ?  Remember, it is empty  B 1 has only one exit  So, B 1  RDF(B 2 )  B 1 ’s branch is useless  D EAD rewrites it as a jump D EAD converts it to a form where C LEAN handles it ! B0B0 B2B2 B1B1 B0B0 B2B2 B1B1 D EAD

13 COMP 512, Fall 200313 Eliminating Useless Control Flow The Algorithm CleanPass() for each block i, in postorder if i ends in a branch then if both targets are identical then rewrite with a jump if i ends in a jump to j then if i is empty then merge i with j else if j has only one predecessor merge i with j else if j is empty & j has a branch then rewrite i’s jump with j’s branch Clean() until CFG stops changing compute postorder CleanPass() Summary Simple, structural algorithm Limited transformation set Cooperates with D EAD In practice, its quite fast

14 COMP 512, Fall 200314 The Problem Block with no entering edge Situation created by other optimizations The Cure Compute reachability & delete unreachable code Simple mark/sweep algorithm on CFG Mark during computation of postorder, reverse postorder... In M SCP, importing I LOC does this (every time) Eliminating Unreachable Code

15 COMP 512, Fall 200315 Dead Code Elimination Summary Useless Computations  D EAD Useless Control-flow  C LEAN Unreachable Blocks  Simple housekeeping Other Techniques Constant propagation can eliminate branches Algebraic identities eliminate some operations Redundancy elimination  Creates useless operations, or  Eliminates them

16 COMP 512, Fall 200316 Next lecture: Algebraic Reassociation Where are we? Machine Independent Redundancy Redundancy elim. Partial red. elim. Consolidation Code motion Loop-invariant c.m. Consolidation Global Scheduling [Click] Constant propagation Useless code Dead code elim. Partial d.c.e. Constant propagation Algebraic identities Create opportunities Reassociation Replication Specialization Replication Strength Reduction Method caching Heap  stack allocation Tail recursion elimination *

17 COMP 512, Fall 200317 The Lab Due end of semester You may work in teams of two people Either  Build SSA and implement 1 SSA-based optimization, or  Use our SSA implementation and build 2 optimizations (1 must be based on SSA) D EAD is too easy, so D EAD and C LEAN count as 1 optimization  MSCP infrastructure already removes unreachable code Possible optimizations  Constant propagation, strength reduction, lazy code motion, D EAD /C LEAN, D VNT, A WZ partitioning, others to be negotiated...


Download ppt "Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,"

Similar presentations


Ads by Google