cs293s redundancy removal
play

CS293S Redundancy Removal Yufei Ding Review of Last Class - PowerPoint PPT Presentation

CS293S Redundancy Removal Yufei Ding Review of Last Class Consideration of optimization Sources of inefficiency Components of optimization Paradigms of optimization Redundancy Elimination Types of intermediate


  1. CS293S Redundancy Removal Yufei Ding

  2. Review of Last Class � Consideration of optimization � Sources of inefficiency � Components of optimization � Paradigms of optimization � Redundancy Elimination � Types of intermediate representations of a program � Removing redundant expressions 2

  3. Topics of This Class � Removing redundant expressions � DAG: version tracking � Linear representation: value numbering � Scope of optimization � Basic block � Extended basic block � Region (dominator) 3

  4. 1. IR: Syntax Tree m <-- 2 x y x z n <-- 3 x y x z o <-- 2 x y - z 4

  5. DAG: Eliminating Identical Subtrees m <-- 2 x y x z n <-- 3 x y x z o <-- 2 x y - z Using hash-based constructor: • key is the textual notion of operators and operands; • value is the node number or address. 5

  6. Version Tracking m <-- 2 x y x z y <-- 3 x y x z o <-- 2 x y - z Associate a counter with each variable for version tracking m1 <-- 2 x y1 x z1 y2 <-- 3 x y1 x z1 o1 <-- 2 x y2 - z1 6

  7. Missing Any Opportunities? Original Code a ¬ x + y z ¬ y d ¬ 17 c ¬ x + z � Is there any redundant expression? � Can the DAG-based approach recognize it? The hash-based constructor uses a textual notion of “equality”, - so y equals y, independent of its value. - z does not equal y, independent of its value. 7

  8. Linear Representation: Linear IR Example: Three Address Code Statement form: � x ¬ y op z With 1 operator (op ) and, at most, 3 names (x, y, z) Example: t ¬ 2 * y becomes z ¬ x - 2 * y z ¬ x - t Statements are executed sequentially. 8

  9. Local Value Numbering Basic Algorithm For each expression e (assuming in the form result e <-- o 1 op o 2 ) � Get value numbers for operands from hash lookup table, represented by VN( o 1 ), VN( o 2 ). If no such key exists, insert these keys to the hash table with new value numbers; � Search with hash key < op ,VN( o 1 ),VN( o 2 ) >, If no such key exists, 1. insert the key to the hash table with a new value number; 2. use the same value number for result e and insert them to hash table. else, get the value of this key, use the same value number for result e, and insert them to hash table. 9

  10. Local Value Numbering to Find Redundancy An example Original Code With VNs Hash Table for VN a 3 ¬ x 1 + y 2 a ¬ x + y {<x,1>, <y,2>, <<+,1,2>,3>, <a,3>} z 2 ¬ y 2 {..., <z,2>} z ¬ y d 4 ¬ 17 {..., <z,2>, <17,4>, <d,4>} d ¬ 17 c 3 ¬ x 1 + z 2 c ¬ x + z {..., <z,2>, <17,4>, <d,4>, <c,3>} Compare Value Num with Version Num. (The way they get increase; why the latter is insufficient for the example. 10

  11. Rewritten for Redundancy Elimination An example With VNs Hash Table for VN Original Code a 3 ¬ x 1 + y 2 {<x,1>, <y,2>, <<+,1,2>,3>, <a,3>} a ¬ x + y z 2 ¬ y 2 {..., <z,2>} z ¬ y d 4 ¬ 17 {..., <z,2>, <17,4>, <d,4>} d ¬ 17 c 3 ¬ x 1 + z 2 {..., <z,2>, <17,4>, <d,4>, <c,3>} c ¬ x + z Hash Table for Rewritten Rewritten {<1,x>, <2,y>, <3,a>} a ¬ x + y {<1,x>, <2,y>, <3,a>} z ¬ y {<1,x>, <2,y>, <3,a>, <4,17>} d ¬ 17 {<1,x>, <2,y>, <3,a>, <4,17>} c ¬ a

  12. Resolving Overwritten Issue An example Original Code Rewritten With VNs a ¬ x + y a 3 ¬ x 1 + y 2 a ¬ x + y z ¬ y z ¬ y z 2 ¬ y 2 * b ¬ x + y * b 3 ¬ x 1 + y 2 * b ¬ a a ¬ 17 a 4 ¬ 17 a ¬ 17 * c ¬ x + y * c 3 ¬ x 1 + y 2 * c ¬ a Hash Table for Rewritten Two redundancies marked by * {<1,x>, <2,y>, <3,a>} {<1,x>, <2,y>, <3,a>} {<1,x>, <2,y>, <3,a>, <4,17>} Optional Solutions: {<1,x>, <2,y>, <3,a>, <4,17>} • Use c 3 ¬ b 3 • Save a 3 in t 3 • Rename around it (best) 12

  13. Renaming: Renaming in 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>} 13

  14. Reordering based on associativity � The order in which expressions are written matters � Example: either 2 x y or y x z will be missed in left- or right-associative treatment. m <-- 2 x y x z n <-- 3 x y x z o <-- 2 x y - z � Example: 3 x a x 5 14

  15. A Hard Problem: Pointer Assignments � *p = 17 could force every variable in a program to increase their version number. � A major motivation for pointer analysis Original Code a ¬ x + y b ¬ w + v *p ¬ 17 c ¬ x + y d ¬ w + v 15

  16. So far… � Removing redundant expressions � DAG: version tracking � Linear representation: value numbering 16

  17. Local Value Numbering <-> Linear IR Local Value Numbering A m ¬ a + b • 1 block at a time 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 w ¬ c + d x ¬ e + f G y ¬ a + b Missed opportunities z ¬ c + d (need stronger methods) * 17

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

  19. CFG Control-flow graph (CFG) A m ¬ a + b n ¬ a + b • Nodes for basic blocks • Edges for branches B C p ¬ c + d q ¬ a + b • Basis for many program r ¬ c + d r ¬ c + d analysis & transformation 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 • E = {(A,B),(A,C),(B,G),(C,D), y ¬ a + b z ¬ c + d (C,E),(D,F),(E,F),(F,E)} • |N| = 7, |E| = 8 19

  20. 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. � May have multiple exits � A tree structure

Recommend


More recommend