Presentation is loading. Please wait.

Presentation is loading. Please wait.

Early Global Program Optimizations Chapter 12.4-6 Mooly Sagiv.

Similar presentations


Presentation on theme: "Early Global Program Optimizations Chapter 12.4-6 Mooly Sagiv."— Presentation transcript:

1 Early Global Program Optimizations Chapter 12.4-6 Mooly Sagiv

2 Outline Value Numbering –Basic blocks –Procedure based data flow analysis Sparse conditional constant propagation

3 Three Global Data Flow Problems Value-Numbers Assign integer values to expressions –e 1 and e 2 have the same value (at the entry a given block)  e 1 and e 2 evaluate to the same value every time the control reaches this block Constant-Propagation Assign integer values to program variables –v has a constant value n (at the entry of a given block)  every time the control reaches this block, has the value n (Formal) Common Available Sub-expression Evaluation Assign sets of available expressions –e is available (at the entry of a given block)  every time the control reaches this block, e is already computed

4 Example read(i) j  i + 1 k  i l  k + 1 i  2 j  i * 2 k  i + 2 read(i) if i > 0 goto L1 j  2 * i goto L2 L1: k  2 * i L2: l  2 * i

5 Solutions General –Develop the most precise algorithm that approximates the three problems together Specific –Develop the most efficient algorithm that approximates one problem –Apply several algorithms (multiple times)

6 The General Algorithm (Killdall 1973) An iterative algorithm The Lattice of data flow information = “pools” of sets of equivalent expressions with an optional constant value Optimistically start with everything being available and iterate until no more expressions/values are removed

7 {} read(i) {{i}} j  i + 1 {{i}, {i+1, j}} k  i {{i, k}, {i+1, k+1, j}} l  k + 1 {{i, k}, {i+1, k+1, j, l}} {} i  2 {{i, 2}} j  i * 2 {{i, 2}, {j, i *2, 4}} k  i + 2 {{i, 2}, {j, i *2, i+2, 4, k}} {} read(i) {{i}} if i > 0 goto L1 {{i}} j  2 * i {{i}, {2*i, j}} goto L2 L1: {{i}} k  2 * i {{i}, {2*i, k}} L2: {{i}, {2*i}} l  2 * i {{i}, {2*i, l}}

8 The General Algorithm (Killdall 1973) The Lattice of data flow information – Pool = Sets of sets of equivalent expressions – p  Pool, x, y  p  x = y  x  y = {} – p  Pool, x  p, z1, z2  x  z1=z2 p1  p2  p2 is a refinement of p1   x2  p2:  x1  p1: x2  x1 p1  p1 = {x1  x2 - {}: x1  p1, x2  p2}  = Init = {{}}

9 The effect of st==x  e on a pool P If e is in P  e is redundant Create a new class P with the partial computations in the program which have operands equivalent to e If e evaluates to a constant z, then add e to the (possibly new) class of z Remove all the expressions with argument x and add x to the class of e (and all its consequences)

10 {} read(i) {{i}} j  i + 1 {{i}, {i+1, j}} k  i {{i, k}, {i+1, k+1, j}} l  k + 1 {{i, k}, {i+1, k+1, j, l}} {} i  2 {{i, 2}} j  i * 2 {{i, 2}, {j, i *2, 4}} k  i + 2 {{i, 2}, {j, i *2, i+2, 4, k}} {} read(i) {{i}} if i > 0 goto L1 {{i}} j  2 * i {{i}, {2*i, j}} goto L2 L1: {{i}} k  2 * i {{i}, {2*i, k}} L2: {{i}, {2*i}} l  2 * i {{i}, {2*i, l}}

11 Efficient Algorithms for Value Numbering For basic blocks the problem is easy (12.4.1) SSA can be used for extended basic blocks Reif & Lewis 1982 O(E  (E, E)) algorithm Bowen, Wegman, Zadeck (1988) O(E log E) Extensions for more constants- Knoop, Steffen, Ruething 1999

12 Bowen, Wegman, Zadeck Algorithm Convert the program into SSA form Build a “Value Graph” --- a directed graph representing symbolic execution of the program Find the “congruent” nodes using the coerset partition of a set (automata minimization) Variables are detected as equivalent at a basic block if (i) the corresponding nodes are equivalent (ii) their defining assignment dominates p

13 A Simple Example I<29 J  1 K  1 J  2 K  2 I<29 Y N L  1 L  2 Y N

14 A Simple Example (Killdall’s Algorithm) I<29 J  1 K  1 J  2 K  2 I<29 Y N L  1 L  2 {{}} {{I<29}} {{I<29}{J, K, 1}} {{I<29},{J, K, 2}} {{I<29},{J, K}} {{I<29},{J, K}, {L, 1}} {{I<29},{J, K}, {L, 2}} {{I<29},{J, K}, {L}} Y N N

15 A Simple Example(Bowen, Wegman, Zadeck) I<29 J 1  1 K 1  1 J 2  2 K 2  2 J 3   4 (J 1, J 2 ) K 3   4 (K 1, K 2 ), I<29 Y N L 1  1 L 2  2 Y N 1 2 3 4 5 6 7 L 3   7 (L 1, L 2 ) 1 J1J1 2 J2J2 44 1 K1K1 2 K2K2 44 1 J1J1 2 J2J2 77 I 2929 < I 2929 < J3J3 K3K3 l l l l l r r r r r

16 Global Value Graph Directed labeled graph Nodes may be labeled by –constant numbers –normal function symbols (no side effect) –  functions Directed edges from functions to arguments (ordered according to argument position)

17 Congruence Two nodes in the value graph are congruent: –the nodes have identical function label –the corresponding destination of edges leaving the nodes are congruent Tricky for value graphs with cycles

18 J  J + 1 K  K + 1 J  1 K  1, I<29 N Y J  J + 2 K  K + 2 Loop Example

19 J 2  J 1 + 1 K 2  K 1 + 1 J 0  1 K 0  1 J 1   2 (J 0, J 4 ) K 1   2 (K 0, K 4 ), I<29 N Y 1 2 J 3  J 1 + 2 K 3  K 1 + 2 J 4   5 (J 2, J 3 ) K 4   5 (K 2, K 3 ) Loop Example(SSA) 34 5

20 Loop Example(Value Graph) 3 J 0  1 K 0  1 J 1   2 ( J 0, J 4 ) K 1   2 ( K 0, K 4 ) I < 29 J 2  J 1 + 1 K 2  K 1 + 1 J 4   5 ( J 2, J 3 ) K 4   5 ( K 3, K 3 ) YN 4 J 3  J 1 + 1 K 3  K 1 + 1 1 Z0Z0 1 J0J0 55 J4J4 2 Z1Z1 22 J1J1 + J3J3 + J2J2 1 Z2Z2 1 K0K0 55 K4K4 2 Z3Z3 22 K1K1 + K3K3 + K2K2 1 2 3 5 l l r l lr r r l l r l lr r r

21 A Simple Partitioning Algorithm Place all the nodes with the same label in the same partition Repeat splitting partitions with two nodes having corresponding edges leading to nodes in different partitions

22 A Simple Partitioning Algorithm Partition = {P | x, y  P  label(x) == label(y) } while (change) do change = false for each P  Partition do for each f i do if (  x,y  P.f i (x) != f i (y)) then split P change = true fi od

23 An (E log E) Partitioning Algorithm Aho, Hofcroft, Ulllman 1974 Given: – A set S –A function f: S  S –A partition of S into disjoints blocks –  ={B 1, B 2, …, B p } Find the coerset (having the fewest blocks) partition  ’={E 1, E 2, …, E q } such that –  ’ is a refinement of  (    ’) –a, b  E l  f(a), f(b)  E j

24 WAITING  { 1, 2,.. p } q  p while WAITING !=  do select and delete an integer I from WAITING for m from 1 to k do INVERSE   for x in B[i] INVERSE  INVERSE  f -1 m (x) end for each j such that B[j]  INVERSE !=  and not (B[j]  INVERSE) do q  q + 1 create a new block B[q] B[q]  B[j]  INVERSE B[j]  B[j] – B[q] if j is in WAITING then add q to WAITING else if  B[j]  <=  B[q]  then add j to WAITING else add j to WAITING fi od

25 Generalizations Identify control flow structures and generate special  functions Handle arrays with ACCESS, UPDATE But can we find all the Kildall’s expressions in O (E log E)

26 I<29 J 1  1 K 1  1 J 2  2 K 2  2 J 3   4 (J 1, J 2 ) K 3   4 (K 1, K 2 ), I<29 Y N L 1  1 L 2  2 Y N 1 2 3 4 5 6 7 L 3   7 (L 1, L 2 ) 1 J1J1 2 J2J2 44 1 K1K1 2 K2K2 44 1 J1J1 2 J2J2 77 I 2929 < I 2929 < J3J3 K3K3 l l l l l r r r r r

27 Conditional Constant Propagation Conditions with constant values can be interpreted to improve precision A more precise solution is obtained “optimistically”

28 char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3 );} main() { printf(“%s\n”, “red”);} “red”

29 char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);}

30 Iterative Data-Flow Algorithm Input: a flow graph G=(N,E,r) An init value Init A montonic function F B for every B in N Output:For every N in(N) Initializatio: in(Entry) := Init; for each node B in N-{Entry} do in(B) :=  WL := N - {Entry} Iteration: while WL != {} do Select and remove an B from WL out := F B (in(B)) For all B’ in succ(B) such that in(B’) != in(B’)  out do in(B’):= in(B’)  out WL := WL  {B’}

31 Iterative Data-Flow Algorithm Input: a flow graph G=(N,E,r) An init value Init A montonic function F B for every B in N Output:For every N in(N) Initializatio: in(Entry) := Init; for each node B in N-{Entry} do in(B) :=  WL := {Entry} Iteration: while WL != {} do Select and remove an B from WL out := F B (in(B)) For all B’ in succ(B) such that in(B’) != in(B’)  out do in(B’):= in(B’)  out WL := WL  {B’}

32 char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);}

33 Conditional Constant Propagation initialize the worklist to the entry node mark all edges as not executable repeat until the worklist is empty: –select and remove a node from the worklist –if it is an assignment then mark the successor edge as executable –if it is a test then symbolically evaluate the test and mark the enabled successor edges as executable if test evaluates to true or  mark true edge executable if test evaluates to false or  mark false edge executable –update the value of all the variables at the entry and exit of this node –if there are changes then add all successors reachable from the node with edges marked executable to the worklist

34 Sparse Conditional Constant bring the program in SSA form initialize the analysis information: –all variables are mapped to  –all flow edges are marked as not executable initialize the two worklists –Flow-Worklist contains all edges of the flow graph with the entry node as source –SSA-Worklist is empty

35 repeat until both worklists are empty: –select and remove an edge from one of the worklists –if it is a flow edge then if the edge is not marked executable then –mark it executable –if the target of the edge is a  -node then call visit-  –if it is the first time the node is visited (only one incoming flow edge is marked executable) and it is a normal node then call visit-instr –if it is an SSA edge then if the target of the edge is a  -node then call visit-  if it is a normal node and at least one of the flow edges entering the node are marked executable then call visit-instr

36 visit-  : (the node is a  -node) –the assigned variable is given a value that is the join the values of the arguments with incoming edges marked executable visit-instr: (the node is a normal node) –determine the value of the expression of the node and update the variable in case of an assignment –if there are changes then if the node is an assignment then add all SSA edges with source at the target of the current edge to the SSA-worklist if the node is a test then add all relevant flow edges to the Flow- worklist and mark them executable –if test evaluates to true or  add true edge –if test evaluates to false or  : add false edge


Download ppt "Early Global Program Optimizations Chapter 12.4-6 Mooly Sagiv."

Similar presentations


Ads by Google