Presentation is loading. Please wait.

Presentation is loading. Please wait.

Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm

Similar presentations


Presentation on theme: "Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm"— Presentation transcript:

1 Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm
Based on “Detecting Equality of Variables in Programs,” B. Alpern, M. Wegman, & F. Zadeck, Proceedings of the 15th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Langauges, San Diego, CA, USA, January 1988, pages This lecture ties back to Chapter 2 … Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

2 Review We have already seen Local & Superlocal Value Numbering
Finds redundancy, constants, & identities in a block Dominator Value Numbering Extends scope to “almost” global (no back edges) Uses dominance information to handle join points in CFG Global Common Subexpression Elimination (GCSE) Applying data-flow analysis to the problem Today’s lecture The Partitioning Method Using a well-known algorithm for a new purpose

3 Partitioning Method Key idea
Congruence is related to redundancy, but not the same Using the idea Start with SSA form (we need the name space) Partition the set of all expressions into congruence classes Find largest possible sets Each class needs one (or more) representative computation Other uses can be replaced with representer Operations x and y are congruent iff they have the same operator & their operands are congruent Alpern, Wegman, & Zadeck POPL 1988

4 Partitioning Method The algorithm
Based on Hopcroft’s algorithm for DFA minimization A widely-used, often re-invented algorithm s z  x+y n  q+y 1. Partition operations into initial classes by opcode 2. Worklist  all classes 3. While Worklist is not empty A. Remove a class c from Worklist B. For each class s that uses some x  c i. Split s into s1 and s2 around uses of c ii. Put smaller of s1 and s2 onto Worklist 4. Pick a representer for each class & rewrite the code to replace other uses of the class with representer c may split s c x  …

5 } Partitioning Method All Operations Some Initial Values * m0  a + b
n0  a + b p0  c + d r0  c + d q0  a + b r1  c + d e0  b + 18 s0  a + b u0  e0 + f e1  a + 17 t0  c + d u1  e1 + f e2  (e0,e1) u2  (u0,u1) v0  a + b w0  c + d x0  e2 + f r2  (r0,r1) y0  a + b z0  c + d a  1 b  2 c  3 d  4 f  5 All Operations a  1 b  2 c  3 d  4 f  5 m0  a + b n0  a + b A p0  c + d r0  c + d B r2  (r0,r1) y0  a + b z0  c + d G q0  a + b r1  c + d C e0  b + 18 s0  a + b u0  e0 + f D e1  a + 17 t0  c + d u1  e1 + f E e3  (e0,e1) u2  (u0,u1) v0  a + b w0  c + d x0  e2 + f F *

6 Partitioning Method Compute Initial Partition * Worklist
m0  a + b n0  a + b p0  c + d r0  c + d q0  a + b r1  c + d e0  b + 18 s0  a + b u0  e0 + f e1  a + 17 t0  c + d u1  e1 + f e2  (e0,e1) u2  (u0,u1) v0  a + b w0  c + d x0  e2 + f r2  (r0,r1) y0  a + b z0  c + d a  1 b  2 c  3 d  4 f  5 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b p0  c + d r0  c + d q0  a + b r1  c + d e0  b + 18 s0  a + b u0  e0 + f e1  a + 17 t0  c + d u1  e1 + f v0  a + b w0  c + d x0  e2 + f y0  a + b z0  c + d 7 Worklist 1, 2, 3, 4, 5, 6, 7 Compute Initial Partition *

7 Partitioning Method Split on a * Worklist 2, 3, 4, 5, 6, 7 a  1 1
b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b p0  c + d r0  c + d q0  a + b r1  c + d e0  b + 18 s0  a + b u0  e0 + f e1  a + 17 t0  c + d u1  e1 + f v0  a + b w0  c + d x0  e2 + f y0  a + b z0  c + d 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b e1  a + 17 v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d e0  b + 18 u0  e0 + f t0  c + d u1  e1 + f w0  c + d x0  e2 + f z0  c + d 8 Split on a Worklist 2, 3, 4, 5, 6, 7 *

8 Partitioning Method Split on b * Worklist 3, 4, 5, 6, 7, 9, 10 a  1 1
2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b e1  a + 17 v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d e0  b + 18 u0  e0 + f t0  c + d u1  e1 + f w0  c + d x0  e2 + f z0  c + d 8 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d u0  e0 + f t0  c + d u1  e1 + f w0  c + d x0  e2 + f z0  c + d 8 e1  a + 17 9 e0  b + 18 10 Split on b Worklist 3, 4, 5, 6, 7, 9, 10 *

9 Partitioning Method Split on c * Worklist 4, 5, 6, 7, 9, 10, 11
e1  a + 17 9 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d u0  e0 + f t0  c + d u1  e1 + f w0  c + d x0  e2 + f z0  c + d 8 e0  b + 18 10 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e0  b + 18 10 u0  e0 + f u1  e1 + f x0  e2 + f 11 e1  a + 17 9 Split on c Worklist 4, 5, 6, 7, 9, 10, 11 *

10 Partitioning Method Split on d, f
1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e0  b + 18 10 u0  e0 + f u1  e1 + f x0  e2 + f 11 e1  a + 17 9 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 u0  e0 + f u1  e1 + f x0  e2 + f 11 Neither d nor f refine the partitions Split on d, f Worklist 6, 7, 9, 10, 11 *

11 11 is already on the Worklist
Partitioning Method a  1 1 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u0  e0 + f u1  e1 + f 12 11 is already on the Worklist b  2 2 c  3 3 Split on e2,u2,r2 d  4 4 f  5 5 e1  a + 17 9 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 u0  e0 + f u1  e1 + f x0  e2 + f 11 e0  b + 18 10 Worklist 7, 9, 10, 11 *

12 Partitioning Method Split on m0, ...
1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u0  e0 + f u1  e1 + f 12 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u0  e0 + f u1  e1 + f 12 Does not refine the partitions ... Split on m0, ... Worklist 9, 10, 11 *

13 Partitioning Method Split on e1 * Worklist 10, 11, 12, 6 a  1 1
m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 u2  (u0,u1) r2  (r0,r1) 14 b  2 2 c  3 3 Split on e1 d  4 4 f  5 5 e1  a + 17 9 e2  (e0,e1) u2  (u0,u1) r2  (r0,r1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 x0  e2 + f 11 x0  e2 + f 11 u0  e0 + f u1  e1 + f 12 Worklist 10, 11, 12, 6 e0  b + 18 10 *

14 Partitioning Method Split on e0 Does not refine the partitions ... *
1 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 14 u2  (u0,u1) r2  (r0,r1) Does not refine the partitions ... b  2 2 c  3 3 Split on e0 d  4 4 f  5 5 e1  a + 17 9 e2  (e0,e1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 u2  (u0,u1) r2  (r0,r1) 14 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 Worklist 11, 12, 6 e0  b + 18 10 *

15 Partitioning Method Split on x0 Does not refine the partitions ... *
1 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 u2  (u0,u1) r2  (r0,r1) 14 Does not refine the partitions ... b  2 2 c  3 3 Split on x0 d  4 4 f  5 5 e1  a + 17 9 e2  (e0,e1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 u2  (u0,u1) r2  (r0,r1) 14 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 Worklist 12, 6 e0  b + 18 10 *

16 Partitioning Method Split on u1 * Worklist 6, 14 a  1 1 m0  a + b
n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 u2  (u0,u1) 14 r2  (r0,r1) 16 b  2 2 c  3 3 Split on u1 d  4 4 f  5 5 e1  a + 17 9 e2  (e0,e1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 u2  (u0,u1) r2  (r0,r1) 14 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 Worklist 6, 14 e0  b + 18 10 *

17 Partitioning Method Split on e2 Does not refine the partitions ... *
1 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 u2  (u0,u1) 14 r2  (r0,r1) 16 Does not refine the partitions ... b  2 2 c  3 3 Split on e2 d  4 4 f  5 5 e1  a + 17 9 e2  (e0,e1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 u2  (u0,u1) 14 r2  (r0,r1) 16 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 Worklist 14 e0  b + 18 10 *

18 Partitioning Method Split on u2 Does not refine the partitions ... *
1 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 a  1 1 b  2 2 c  3 3 d  4 4 f  5 5 e2  (e0,e1) 6 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 e1  a + 17 9 e0  b + 18 10 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 u2  (u0,u1) 14 r2  (r0,r1) 16 Does not refine the partitions ... b  2 2 c  3 3 Split on u2 d  4 4 f  5 5 e1  a + 17 9 e2  (e0,e1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 u2  (u0,u1) 14 r2  (r0,r1) 16 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 Worklist is empty e0  b + 18 10 *

19 Partitioning Method Now what? We have proved a set of theorems
Must rewrite code to use them Knowledge alone won’t do it! a  1 1 m0  a + b n0  a + b q0  a + b s0  a + b v0  a + b y0  a + b 7 b  2 2 c  3 3 d  4 4 f  5 5 e1  a + 17 9 Rebuild the code from the partition Sets with > 1 member  reuse Sets with 1 member  no reuse Let’s look at the code Opportunities are a+b & c+d Can we make this work? e2  (e0,e1) 6 p0  c + d r0  c + d r1  c + d t0  c + d w0  c + d z0  c + d 8 u2  (u0,u1) 14 r2  (r0,r1) 16 x0  e2 + f 11 u1  e1 + f 12 u0  e0 + f 15 e0  b + 18 10

20 Partitioning Method A B G C D E F Fixing a+b is easy
m0  a + b n0  a + b A p0  c + d r0  c + d B r2  (r0,r1) y0  a + b z0  c + d G q0  a + b r1  c + d C e0  b + 18 s0  a + b u0  e + f D e1  a + 17 t0  c + d u1  e + f E e3  (e0,e1) u2  (u0,u1) v0  a + b w0  c + d x0  e + f F Fixing a+b is easy Need to choose the right instance to keep Rest are redundant

21 Partitioning Method A B G C D E F
m0  a + b n0  m0 A p0  c + d r0  c + d B r2  (r0,r1) y0  m0 z0  c + d G q0  m0 r1  c + d C e0  b + 18 s0  m0 u0  e + f D e1  a + 17 t0  c + d u1  e + f E e3  (e0,e1) u2  (u0,u1) v0  m0 w0  c + d x0  e + f F Notice that when we move to a global scope for optimization, we suddenly need to separate analysis & transformation. Fixing c+d is harder Need some rationale for replacement scheme *

22 Partitioning Method A B G C D E F
m0  a + b n0  a + b A p0  c + d r0  c + d B r2  (r0,r1) y0  a + b z0  c + d G q0  a + b r1  c + d C e0  b + 18 s0  a + b u0  e + f D e1  a + 17 t0  c + d u1  e + f E e3  (e0,e1) u2  (u0,u1) v0  a + b w0  c + d x0  e + f F AWZ suggest removing any computation that is dominated by another computation in the same partition For a+b, the def of m0 dominates all other instances For c+d no single instance will do Some cases cannot be caught this way Simpson suggested an alternative approach. *

23 Partitioning Method Simpson’s suggestion
Encode congruences into the name space Same idea Briggs used in reassociation Use LCM to remove redundancies Simplification In SSA name space, there are no KILLs Knocks terms out of some equations Makes initial information cheaper to compute Larger name space, but O(n) rather than O(n2) initialization Consistently outdoes dominator-based replacement Lazy Code Motion is a major extension of AVAIL-based GCSE

24 } Partitioning Method Is this optimistic or pessimistic?
Earlier view said Initialize to top  optimistic Initialize to bottom  pessimistic This algorithm does neither AWZ builds a set of congruence relations Assumes maximal size sets Knocks out infeasible elements Over-estimates set of congruences Finds largest set of self-consistent congruences  Must let it run to completion, or sets might contain lies } inherently optimistic *

25 Partitioning Method Strengths
Global algorithm for finding redundancies Handles arbitrary control flow (loops) Finds largest congruence classes consistent with definition Relies on well understood technology Weaknesses Cannot put 2xa and a+a in same class Not obvious how to handle identities or constants (Click) Slower than DVNT

26 Redundancy Elimination Wrap-up
The partitioning method based on DFA minimization Redundancy Elimination Wrap-up Generalizations Hash-based methods are fastest AWZ find the most cases Expect better results with larger scope Experimental data Ran LVN, SVN, DVNT, AWZ Used global name space for DVNT Requires offline replacement Exposes more opportunities Code was compiled with lots of optimization How did they do? DVNT beat AWZ Improvements grew with scope SCCVN 2.5x faster than AWZ *

27 Redundancy Elimination Wrap-up
Conclusions Redundancy elimination has some depth & subtlety Variations on names, algorithms & analysis matter Compile-time speed does not have to sacrifice code quality DVNT is probably the method of choice Results quite close to the global methods (± 1%) Much lower costs than AWZ


Download ppt "Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm"

Similar presentations


Ads by Google