cs293s redundancy removal svn dvn gcse
play

CS293S Redundancy Removal: SVN & DVN & GCSE Yufei Ding - PowerPoint PPT Presentation

CS293S Redundancy Removal: SVN & DVN & GCSE Yufei Ding Review of Last Class Removing redundant expressions DAG: version tracking Linear representation: value numbering 2 Local Value Numbering <-> Linear IR Local Value


  1. CS293S Redundancy Removal: SVN & DVN & GCSE Yufei Ding

  2. Review of Last Class � Removing redundant expressions � DAG: version tracking � Linear representation: value numbering 2

  3. Local Value Numbering <-> Linear IR Local Value Numbering A m ¬ a + b • 1 block at a time n ¬ a + b • Strong local results B C p ¬ c + d q ¬ a + b • No cross-block effects r ¬ c + d r ¬ c + d D E e ¬ b + 18 e ¬ a + 17 s ¬ a + b t ¬ c + d u ¬ e + f u ¬ e + f F v ¬ a + b w ¬ c + d x ¬ e + f G y ¬ a + b Missed opportunities z ¬ c + d (need stronger methods) * 3

  4. Local Value Numbering <-> Linear IR Local Value Numbering A m ¬ a + b • 1 block at a time n ¬ a + b • Strong local results B C p ¬ c + d q ¬ a + b • No cross-block effects r ¬ c + d r ¬ c + d D E e ¬ b + 18 e ¬ a + 17 s ¬ a + b t ¬ c + d u ¬ e + f u ¬ e + f F v ¬ a + b Missed opportunities w ¬ c + d x ¬ e + f (need stronger methods) G y ¬ a + b z ¬ c + d Can we find set of blocks that also ensures the sequential execution order in the basic block? 4

  5. Topics of This Class � Scope of optimization � Basic block -> Local value numbering � Extended basic block -> Superlocal value numbering (SVN) � Dominator -> Dominator-based value numbering (DVN) � Global Common Subexpression Elimination (GCSE) � More close to DAG-based methods � Work on lexical notation instead of expression values. 5

  6. Basic blocks � A basic block is a maximal-length segment of straight-line, unpredicated code. In another word, it has one entry point (i.e., no code within it is the destination of a jump instruction), one exit point and no jump instructions contained within it. � Example m = 2; c = m + n; L2: if(c>0) goto L1; d = 4; goto L2; c = 5; L1: 6

  7. CFG Control-flow graph (CFG) • Nodes for basic blocks A m ¬ a + b • Edges for branches n ¬ a + b • Basis for many program B C analysis & transformation p ¬ c + d q ¬ a + b r ¬ c + d r ¬ c + d D E e ¬ b + 18 e ¬ a + 17 s ¬ a + b t ¬ c + d u ¬ e + f u ¬ e + f F v ¬ a + b w ¬ c + d This CFG, G = (N,E) x ¬ e + f • N = {A,B,C,D,E,F,G} G y ¬ a + b • E = {(A,B),(A,C),(B,G),(C,D), z ¬ c + d (C,E),(D,F),(E,F),(F,E)} • |N| = 7, |E| = 8 7

  8. Extended basic block (EBB) A m ¬ a + b n ¬ a + b B C p ¬ c + d q ¬ a + b r ¬ c + d r ¬ c + d � An EBB is a set of blocks B1, D E e ¬ b + 18 e ¬ a + 17 s ¬ a + b t ¬ c + d B2, ..., Bn, where Bi, 2 <= i <= n u ¬ e + f u ¬ e + f has a unique predecessor, which F is in the EBB. v ¬ a + b w ¬ c + d � May have multiple exits x ¬ e + f � A tree structure G y ¬ a + b � If a block is added to the EBB, z ¬ c + d all of its predecessors must be included. Bi is the one with on predecessor, i.e., the root of the Can you find the maximum EBB � EBB.

  9. Superlocal Value Numbering 1. First find the maximum EBB: A m ¬ a + b ABCDE, F, G n ¬ a + b 2. Apply local method to EBBs’ paths • Do { A,B }, { A,C,D }, { A,C,E }, {F}, {G} B C p ¬ c + d q ¬ a + b r ¬ c + d r ¬ c + d D E e ¬ b + 18 e ¬ a + 17 s ¬ a + b t ¬ c + d u ¬ e + f u ¬ e + f F v ¬ a + b w ¬ c + d x ¬ e + f G y ¬ a + b z ¬ c + d 9

  10. Implementation � Reuse the value numbering results of some common blocks for efficiency � Which necessitates the undoing of a block’s effect � After {A,C,D}, it must recreate the state of {A,C} before processing E. � Options: 1. Record the state of the tables at each block boundary, and restore the state when needed 2. Walking backward and undo the effect. Need record the “lost” information. 3. Scoped hash tables (Lowest cost) keep the table produced at the current block 10

  11. Scoped Value Table a->1 b->2 1+2->3 A m->3 m ¬ a + b c->4 n ¬ a + b n->3 d->5 4+5->6 B C p ¬ c + d r ¬ c + d r->6 r ¬ c + d q ¬ a + b q->3 D E e ¬ b + 18 c->4 t->6 t ¬ c + d s ¬ a + b d->5 u->3 u ¬ a + b u ¬ e + f 4+5->6 p->6 F v ¬ a + b r->6 w ¬ c + d x ¬ e + f G y ¬ a + b z ¬ c + d 11

  12. Rewritten b -> 1 1->b a ¬ b + c c -> 2 2->c 1 + 2 ->3 3->a a -> 3 e ¬ b - c d ¬ b - c 1-2 -> 4 4 -> e 1-2 -> 4 4 -> d f ¬ b - c d-> 4 e -> 4 f-> 4 d ¬ b - c f ¬ d 12

  13. Rewritten a ¬ b + c a 1 ¬ b 1 + c 1 a ¬ 17 d ¬ b + c a 2 ¬ 17 d 1 ¬ b 1 + c 1 e ¬ b + c e 1 ¬ b 1 + c 1 Renaming is still needed. But does it work in all scenarios? 13

  14. Extra Complexity a 1 ¬ b + c a 2 ¬ a 1 + c a 3 ¬ 17 d ¬ a + c ? 14

  15. SSA (Single Static Assignment) Name Space Two principles � Each name is defined by exactly one operation � Each operand refers to exactly one definition To reconcile these principles with real code � Insert f -functions at merge points to reconcile name space x 0 ¬ ... x 1 ¬ ... x ¬ ... x ¬ ... becomes x 2 ¬f (x 0 ,x 1 ) ... ¬ x + ... ¬ x 2 + ... 15

  16. Another SSA Example x 1 ¬f (x 0 ,x 5 ) x 2 ¬ x 1 + ... x ¬ x + ... becomes x ¬ ... x ¬ ... x 3 ¬ ... x 4 ¬ ... ... ¬ x + ... x 5 ¬f (x 3 ,x 4 ) ¬ x 5 + ... Detail: CT-2ndEd: Section 5.4.2; CT-1stEd: Section 5.5. 16

  17. Superlocal Value Numbering 1.Build SSA form A m 0 ¬ a + b 2.Find EBBs n 0 ¬ a + b 3.Apply value numbering to B C p 0 ¬ c + d q 0 ¬ a + b each path in each EBB r 0 ¬ c + d r 1 ¬ c + d using scoped hash tables D E e 0 ¬ b + 18 e 1 ¬ a + 17 s 0 ¬ a + b t 0 ¬ c + d u 0 ¬ e + f u 1 ¬ e + f F e 3 ¬ f (e 0 ,e 1 ) u 2 ¬ f (u 0 ,u 1 ) v 0 ¬ a + b This is in w 0 ¬ c + d SSA Form x 0 ¬ e + f G r 2 ¬ f (r 0 ,r 1 ) y 0 ¬ a + b z 0 ¬ c + d 17

  18. Superlocal Value Numbering With all the bells & whistles A m 0 ¬ a + b • Find more redundancy n 0 ¬ a + b • Pay little additional cost B C p 0 ¬ c + d q 0 ¬ a + b • Still does nothing for F & G r 0 ¬ c + d r 1 ¬ c + d D E e 0 ¬ b + 18 e 1 ¬ a + 17 s 0 ¬ a + b t 0 ¬ c + d u 0 ¬ e + f u 1 ¬ e + f F e 3 ¬ f (e 0 ,e 1 ) u 2 ¬ f (u 0 ,u 1 ) v 0 ¬ a + b This is in w 0 ¬ c + d SSA Form x 0 ¬ e + f G r 2 ¬ f (r 0 ,r 1 ) y 0 ¬ a + b z 0 ¬ c + d 18

  19. Dominator-Based Value Numbering 19

  20. Regional (Dominator-based) Methods � Dominators of b: all blocks that dominate b � if every path from the entry of the graph to b goes through a, then a is one of b’s dominator. � The full set of dominators for b is denoted by DOM(b). � Strict Dominators: � If a dominators b and a ≠ b, then we say a strictly dominates b. � Immediate Dominator: � The immediate dominator of b is the strict dominator of b that is closest to b. It is denoted IDOM(b). 20

  21. Example A m ¬ a + b n ¬ a + b B C p ¬ c + d q ¬ a + b r ¬ c + d r ¬ c + d D E e ¬ b + 18 e ¬ a + 17 s ¬ a + b t ¬ c + d u ¬ e + f u ¬ e + f F v ¬ a + b w ¬ c + d x ¬ e + f G y ¬ a + b z ¬ c + d BLOCK A B C D E F G DOM IDOM

  22. Dominator-Based Value Numbering � Basic strategy: use table from IDom(x ) to A m 0 ¬ a + b n 0 ¬ a + b start value numbering x B C � Use C for F and A for G p 0 ¬ c + d q 0 ¬ a + b r 0 ¬ c + d r 1 ¬ c + d � Imposes a Dom-based application D E e 0 ¬ b + 18 e 1 ¬ a + 17 s 0 ¬ a + b t 0 ¬ c + d order u 0 ¬ e + f u 1 ¬ e + f F e 3 ¬ f (e 0 ,e 1 ) u 2 ¬ f (u 0 ,u 1 ) v 0 ¬ a + b w 0 ¬ c + d x 0 ¬ e + f G r 2 ¬ f (r 0 ,r 1 ) y 0 ¬ a + b z 0 ¬ c + d 22

  23. SSA Resolves Name Conflicts a ¬ b + c a ¬ b 0 + c b ¬ 17 d ¬ b - c b 1 ¬ 17 d ¬ b 0 - c e ¬ b + c b 2 ¬f (b 0 ,b 1 ) e ¬ b 2 + c 23

  24. Summary � Two methods in a scope beyond a basic block � Superlocal value numbering (SVN) � Value numbering across basic blocks � Dominator-based value numbering (DVN) � Uses dominance information to handle join points in CFG � They can be used together � First Build SSA � Do SVN � Do DVN with the value tables built in SVN reused Build SSA form is the prerequisite for both! 24

  25. Examples x = a + b; e = c + d; f = c + d; c = a - b; g = c + d; 25

  26. Global Common Subexpression Elimination (GCSE) � The first data-flow problem � A global method 26

  27. Some Expression Sets For each block b Let A VAIL (b) be the set of expressions available on entry to b. Let E XPR K ILL (b) be the set of expressions killed in b. i.e. one or more operands of the expression are redefined in b. !!!! Must consider all expressions in the whole graph. Let DEE XPR (b) include the downward exposed expressions in b. i.e. expressions defined in b and not subsequently killed in b 27

  28. Formula to Compute AVAIL � Now, A VAIL (b) can be defined as: A VAIL (b) = Ç x Î pred(b) (DEE XPR (x) È (A VAIL (x) Ç E XPR K ILL (x) )) • preds(b) is the set of b’s predecessors in the control-flow graph. (Again, a predecessor is an immediate parent, not including other ancestors.) 28

  29. Computing Available Expressions The Big Picture 1. Build a control-flow graph 2. Gather the initial data: DEE XPR (b) & E XPR K ILL (b) 3. Propagate information around the graph, evaluating the equation Works for loops through an iterative algorithm: finding the fixed- point. All data-flow problems are solved, essentially, this way. 29

Recommend


More recommend