Copy Propagation
Assignment s:x=y may be eliminated if at all the places where x is used we replace x by y
- Statement s must be the only definition of x reaching where substitution is to be made
- On every path from s to target there are no assignments to y (additional data flow analysis needs to be done)
Algorithm: for each copy s:x=y do the following:
- Determine those uses of x that are reached by this definition
- Determine whether it is the only definition of x reaching and there is no definition of y on the path
- If s meets the above conditions then remove s and replace all uses of x found in (1) by y
Loop Invariant Computations
If for an assignment x=y+z all the definitions of y and z are outside loop then x=y+z is invariant of loop.
Input: A loop L with basic blocks. Assume that ud chains are available for individual statements.
- Mark invariant statements whose operands are all either constants or or have their reaching definitions outside L
- Repeat step (3) until no new statements are marked invariant
- Mark invariant whose operands either are constant, have all their reaching definitions outside L, or have exactly one reaching definition and that definition is a statement in L marked invariant
Performing Code Motion
- Move an invariant statement s to pre-header if following conditions are met:
- The block containing s dominates all exit nodes of the nodes
- There is no other statement in the loop that assign to x
-
No use of x in the loop is reached by any definition of x other than s
- Maintaining dataflow information
- Ud chains: does not change by code motion
- Dominator information: changes by code motion; it needs to be recomputed.
- More general code motion:
If none of the three conditions are satisfied then for a loop invariant statement
A=B+C define T=B+C in the pre header and replace A=B+C by A=T
|