190 likes | 362 Views
CS 201 Compiler Construction. Code Optimizations: Partial Redundancy Elimination. Redundancy Elimination. The goal of redundancy elimination is to eliminate unnecessary reevaluations of the same computation. Global Common Sub-expression Elimination Loop Invariant Code Motion
E N D
CS 201Compiler Construction Code Optimizations: Partial Redundancy Elimination
Redundancy Elimination The goal of redundancy elimination is to eliminate unnecessary reevaluations of the same computation. • Global Common Sub-expression Elimination • Loop Invariant Code Motion • Partial Redundancy Elimination – this is the most general and thus it subsumes the above two optimizations.
Global Common SubexpressionElimination T=X+Y A=T T=X+Y B=T A=X+Y B=X+Y C=X+Y C=T • Analysis Required • Available Expressions • Reaching Definitions
Transformation Algorithm For each statement S: A = B op C stB op C is available at entry of S’s basic block and neither B or C are redefined prior to S do the following: • Find definitions that reach S’s block that have B op C on the right hand side. • Create a new name T. • Replace each statement D = B op C found in step 1 by: T = B op C; D = T; • Replace statement S by A = T.
Loop Invariant Code Motion Loop Invariant: A statement that performs the same computation over and over again during multiple iterations of a loop’s execution. To detect invariant statements in a loop L: • Mark invariant statements whose operands are either constants or variables with ALL reaching definitions outside the loop. • Mark invariant statements whose operands satisfy conditions in step 1 or have exactly one reaching definition that is from inside the loop and has already been marked invariant. • Repeat step 2 till no more invariants are identified.
Transformation For each statement S (A=B, A=op B, A=B op C) that is marked invariant wrt loop L, check the following: • S is in a block that dominates all exits of L; • A is not defined elsewhere in L; and. • All uses of A in L can only be reached by the definition of A in S. Move in the order identified as invariant, each invariant statement S found to satisfy the above conditions to a newly created pre-header.
Limitations of… Global Common Sub-expression Elim. Loop Invariant Code Motion .. = X =A+B X+Y is not Available here
Partial Redundancy Elimination … = X … = T Use code motion Move expressions
Algorithm for PRE Critical edge Before performing PRE, split critical edges to create suitable placement points.
Algorithm for PRE Key Idea: Move expression evaluations back through the control flow graph and place them at the earliest possible points in the control flow graph. This process eliminates full and partial redundancy.
Algorithm Steps Down-Safety Analysis backward analysis that determines to where all expressions can be “safely” moved. Earliestness Analysis forward analysis that determines the earliest points to where an expression can be safely moved. Perform transformation by placing expression evaluations at earliest points and eliminating them from other points.
Down-Safety Analysis if n = e D-SAFE(n) = (entry point of node n) false Used(n) V otherwise V Transp(n) D-SAFE(m) V m εsucc(n) Used(n) is true if n evaluates the expression. Transp(n) is true if n does not modify any operands of the expression (i.e., n is transparent, it does not kill the expression)
Earliestness Analysis true if n = s Earliest(n) (entry point of node n) = ( V otherwise Transp(m) V Earliest(m) ) D-SAFE(m) m εpred(n) V Forward analysis that finds the earliest nodes that are down-safe. A node n is an earliest node if there is a path from start node to n such that along this path n is the first node that is down-safe for the current value of the expression.
Transformation Step Introduce a new auxiliary variable h for expression exp. Insert at the entry of every node n for which D-Safe(n) ∧ Earliest(n) is true, the assignment: h = exp. Replace every original computation of exp by h.
Example E E E V D E E E E V D E E E D E V E D D D E V E D D D D D D D D D D D D D D D D E D D E
Optional Step Earliest(n) D-SAFE(n) V Delay(n) = if n = s false V Delay(m) ) owise ( Used(m) V V m εpred(n) ( Delay(m) ) V Used(n) Latest(n) = Delay(n) V V m εsucc(n) Suppressing unnecessary code motion using Delayability Analaysis - reduces register usage.
Sample Problem X= Y= = X+Y = X+Y = X+Y = X+Y X = = X+Y Apply the PRE algorithm to expression X+Y in the given code example.