cs293s svn dvn gcse
play

CS293S SVN & DVN & GCSE Yufei Ding Review of Last Class - PowerPoint PPT Presentation

CS293S SVN & DVN & GCSE Yufei Ding Review of Last Class Removing redundant expressions DAG: version tracking Linear representation: (local) value numbering Scope of optimization Basic block, Extended basic block,


  1. CS293S SVN & DVN & GCSE Yufei Ding

  2. Review of Last Class � Removing redundant expressions � DAG: version tracking � Linear representation: (local) value numbering � Scope of optimization � Basic block, Extended basic block, … 2

  3. Renaming: Renaming + Value Numbering • Give each value a unique name Example ( continued ) Original Code With VNs Rewritten a 03 ¬ x 01 + y 02 a 0 ¬ x 0 + y 0 a 0 ¬ x 0 + y 0 z 0 ¬ y 0 z 20 ¬ y 02 z 0 ¬ y 0 * b 03 ¬ x 01 + y 02 * b 0 ¬ x 0 + y 0 * b 0 ¬ a 0 a 14 ¬ 17 a 1 ¬ 17 a 1 ¬ 17 * c 03 ¬ x 01 + y 02 * c 0 ¬ x 0 + y 0 * c 0 ¬ a 0 Result: Hash Table for Rewritten • a 0 is available {<1,x 0 >, <2,y 0 >, <3,a 0 >} • Rewriting just {<1,x 0 >, <2,y 0 >, <3,a 0 >} works {<1,x 0 >, <2,y 0 >, <3,a 0 >, <4,17>} {<1,x 0 >, <2,y 0 >, <3,a 0 >, <4,17>} 3

  4. Local Value Numbering 1 basic block at a time (1 entry A point + 1 exit point) m ¬ a + b n ¬ a + b • Strong local results No cross-block effects 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 Missed opportunities w ¬ c + d (need stronger methods) x ¬ e + f 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 (EBB) -> Superlocal value numbering � Dominator -> Dominator-based value numbering � Global Common Subexpression Elimination (GCSE) � More close to DAG-based methods � Work on lexical notation instead of expression values. 5

  6. 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 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 � An EBB is a set of blocks B1, B2, ..., Bn, where Bi, 2 <= i <= n has a unique predecessor, which is in the EBB. (If a block is added to the EBB, all of its predecessors must be included. Bi is the one with on predecessor, i.e., the root of the EBB).

  7. 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 7

  8. 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 8

  9. 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 c->4 e ¬ b + 18 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 9

  10. Rewritten Scoped rewritten table b -> 1 1->b c -> 2 a ¬ b + c 2->c 1 + 2 ->3 3->a a -> 3 1-2 -> 4 4 -> e 1-2 -> 4 e ¬ b - c d ¬ b - c 4 -> d d-> 4 f ¬ b - c e -> 4 f-> 4 d ¬ b - c f ¬ d 10

  11. 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? 11

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

  13. 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 + ... 13

  14. 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. 14

  15. Superlocal Value Numbering 1.Build SSA form A m 0 ¬ a + b 2.Find EBBs n 0 ¬ a + b 3.Apply value numbering to each path in each EBB using B C p 0 ¬ c + d q 0 ¬ a + b scoped hash tables 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 15

  16. Superlocal Value Numbering A With all the bells & whistles m 0 ¬ a + b n 0 ¬ a + b • Find more redundancy • 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 16

  17. Dominator-Based Value Numbering 17

  18. 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). 18

  19. 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

  20. 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 20

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

  22. Summary Build SSA form is the prerequisite for both! � 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 22

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

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

  25. 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 25

  26. 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.) 26

  27. Making Theory Concrete Computing A VAIL for the example A VAIL (A) = Ø A m ¬ a + b A VAIL (B) = { a+b } È ( Ø Ç all ) n ¬ a + b = { a+b } A VAIL (C) = { a+b } B C p ¬ c + d q ¬ a + b r ¬ c + d r ¬ c + d A VAIL (D) = { a+b,c+d } È ({ a+b } Ç all ) = { a+b,c+d } D E e ¬ b + 18 e ¬ a + 17 A VAIL (E) = { a+b,c+d } s ¬ a + b t ¬ c + d u ¬ e + f u ¬ e + f A VAIL (F) = [{ b+18,a+b,e+f } È ({ a+b,c+d } Ç { all - e+f })] F v ¬ a + b w ¬ c + d Ç [{ a+17,c+d,e+f } È x ¬ e + f ({ a+b,c+d } Ç { all - e+f })] G = { a+b,c+d,e+f } y ¬ a + b z ¬ c + d A VAIL (G) = [ { c+d } È ({ a+b } Ç all )] Ç [{ a+b,c+d,e+f } È ({ a+b,c+d,e+f } Ç all )] = { a+b,c+d } 27

  28. 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. 28

Recommend


More recommend