Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPUT 680 - Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic Q: Binary Decision Diagrams José Nelson Amaral

Similar presentations


Presentation on theme: "CMPUT 680 - Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic Q: Binary Decision Diagrams José Nelson Amaral"— Presentation transcript:

1 CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic Q: Binary Decision Diagrams José Nelson Amaral

2 CMPUT Compiler Design and Optimization2 Reading Material zMichael Huth and Mark Ryan, Logic in Computer Science: Modelling and Reasoning about Systems, 2nd Edition, Cambridge University Press, 2005 (Chapter 6).

3 CMPUT Compiler Design and Optimization3 Binary Decision Tree x y 1 0 y 0 0 f(x,y) = x+y xyf Boolean Function Truth Table Binary Decision Tree Huth-Ryan, pp. 361

4 CMPUT Compiler Design and Optimization4 Binary Decision Tree  Binary Decision Diagram x y 1 0 y 0 0 Binary Decision Tree x y 1 0 y Removal of duplicate terminals. Binary Decision Diagram Huth-Ryan, pp. 362

5 CMPUT Compiler Design and Optimization5 Binary Decision Tree  Binary Decision Diagram x y 1 0 y Binary Decision Diagram x y 1 0 Removal of redundant tests. Binary Decision Diagram Huth-Ryan, pp. 362

6 CMPUT Compiler Design and Optimization6 Removal of duplicate non- terminals z 0 1 x yy x yy z 0 1 x yy x y Huth-Ryan, pp. 363

7 CMPUT Compiler Design and Optimization7 Removal of duplicate non- terminals z 0 1 x yy x y z 0 1 x y x y Huth-Ryan, pp. 363

8 CMPUT Compiler Design and Optimization8 Removal of redundant decision point z 0 1 x y x y z 0 1 y x y Huth-Ryan, pp. 363

9 CMPUT Compiler Design and Optimization9 Reduced BDDs zWe saw three optimizations that can be performed in BDDs: yC1: Removal of duplicate terminals yC2: Removal of redundant tests yC3: Removal of duplicate non-terminals zIf none of these optimizations can be applied to a BDD, the BDD is reduced. Huth-Ryan, pp. 365

10 CMPUT Compiler Design and Optimization10 Boolean Operations on BDDs zGiven two BDDs B f and B g representing boolean functions f and g, how do we obtain a BDD for the following functions: yf yf + g yf · g Huth-Ryan, pp. 365

11 CMPUT Compiler Design and Optimization11 Ordered BDDs zLet L = [x 1, … x n ] be an ordered list of variables without duplications. yA BDD B is ordered for L if 1.all variable labels of B occur in L; and 2.for every occurrence of x i followed by x j along a path in B, i < j. zAn ordered BDD (OBDD) is a BDD that is ordered according to some list of variables. Huth-Ryan, pp. 367

12 CMPUT Compiler Design and Optimization12 Example of a BDD that is not ordered z 0 1 y x x y Huth-Ryan, pp. 368

13 CMPUT Compiler Design and Optimization13 OBDDs zThere cannot be multiple occurrences of any variable along a path in an OBDD. zWhen operations are performed on two OBDDs, they must have compatible variable ordering. zThe reduced OBDD representing a given function f is unique  OBDDs have a canonical form. yWe can apply C1-C3 in any order. Huth-Ryan, pp. 368

14 CMPUT Compiler Design and Optimization14 OBDDs can be compact x2x2 x2x2 x3x3 x3x3 x4x4 x4x4 1 0 x1x1 The function f even (x 1, x 2, …, x n ) which is 1 if there is an even number of inputs with value 1 has an OBDD representation with only 2n+1 nodes. Huth-Ryan, pp. 370

15 CMPUT Compiler Design and Optimization15 Exercise Build a reduced OBDD for the following boolean function: f(x 1, x 2, …, x n ) = (x 1 +x 2 ).(x 3 + x 4 )….(x 2n-1 + x 2n ); Try to select a variable ordering that minimizes the number of nodes in the OBDD. Huth-Ryan, pp. 371

16 CMPUT Compiler Design and Optimization16 Impact of Variable Ordering x2x2 x2x2 x2x2 x2x2 x5x5 x5x5 x3x3 x4x4 x4x4 x6x6 x1x1 x5x5 x5x5 x3x3 1 0 x1x1 x3x3 x5x5 x2x2 x4x4 x6x6 01 Huth-Ryan, pp. 371

17 CMPUT Compiler Design and Optimization17 Importance of Canonical Representation zAbsence of redundant variables: yIf f(x 1, x 2, …, x n ) does not depend on x i, then any reduced OBDD of f does not contain x i. zTest for semantic equivalence: yIf f(x 1, x 2, …, x n ) and g(x 1, x 2, …, x n ) are represented by OBDDs B f and B g, then to find if f and g are equivalent, reduce B f and B g and check if they have identical structures. zText for validity: yf(x 1, x 2, …, x n ) is valid (always computes 1) if its reduced OBDD is B 1. Huth-Ryan, pp. 370

18 CMPUT Compiler Design and Optimization18 Importance of Canonical Representation zTest for implication: y f(x 1, x 2, …, x n ) implies g(x 1, x 2, …, x n ) if the OBDD for f. /g is B 0. zTest for satisfiability: y f(x 1, x 2, …, x n ) is satisfiable (computes 1 for at list one input) if its reduced OBDD is not B 0. Huth-Ryan, pp. 372

19 CMPUT Compiler Design and Optimization19 The reduce algorithm zStep 1: Label each node in the OBDD ytwo nodes receive the same id if and only if they represent the same function. zStep 2: Traverse the OBDD bottom up ycollapse all nodes with the same label onto a single node and redirect the edges. Huth-Ryan, pp. 372

20 CMPUT Compiler Design and Optimization20 The reduce algorithm zStep 1: Label yInitialization: Assign (at bottom of BDD): x label #0 to all 0-nodes; x label #1 to all 1-nodes; yLet: x lo(n): node pointed by the dashed line from node n x hi(n): node pointed by the solid line. Huth-Ryan, pp. 372

21 CMPUT Compiler Design and Optimization21 The reduce algorithm zStep 1: Label yRules to label an internal x i -node n : xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: m has variable x i and id(lo(n)) = id(lo(m)) and id(hi(n)) = id(hi(m)) xthen id(n) = id(m) xelse id(n) = next unused integer. Huth-Ryan, pp. 373

22 CMPUT Compiler Design and Optimization22 The reduce algorithm (example) x1x1 01 x2x2 x3x3 x3x3 x2x2 x3x3 x3x3 01 #1 #0 ???? Rules to label an internal x i - node n : xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: m has variable x i and id(lo(n)) = id(lo(m)) and id(hi(n)) = id(hi(m)) xthen id(n) = id(m) xelse id(n) = next unused integer. Huth-Ryan, pp. 373

23 CMPUT Compiler Design and Optimization23 The reduce algorithm (example) x1x1 01 x2x2 x3x3 x3x3 x2x2 x3x3 x3x3 01 #1 #0 #2#3#2 ? ? Rules to label an internal x i - node n : xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: m has variable x i and id(lo(n)) = id(lo(m)) and id(hi(n)) = id(hi(m)) xthen id(n) = id(m) xelse id(n) = next unused integer. Huth-Ryan, pp. 373

24 CMPUT Compiler Design and Optimization24 The reduce algorithm (example) x1x1 01 x2x2 x3x3 x3x3 x2x2 x3x3 x3x3 01 #1 #0 #2#3#2 #4 ? Rules to label an internal x i - node n : xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: m has variable x i and id(lo(n)) = id(hi(m)) and id(hi(n)) = id(hi(m)) xthen id(n) = id(m) xelse id(n) = next unused integer. Huth-Ryan, pp. 373

25 CMPUT Compiler Design and Optimization25 The reduce algorithm (example) x1x1 01 x2x2 x3x3 x3x3 x2x2 x3x3 x3x3 01 #1 #0 #2#3#2 #4 #5 Rules to label an internal x i - node n : xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: m has variable x i and id(lo(n)) = id(lo(m)) and id(hi(n)) = id(hi(m)) xthen id(n) = id(m) xelse id(n) = next unused integer. Huth-Ryan, pp. 373

26 CMPUT Compiler Design and Optimization26 The reduce algorithm (example) x1x1 01 x2x2 x3x3 x3x3 x2x2 x3x3 x3x3 01 #1 #0 #2#3#2 #4 #5 x1x1 0 1 x3x3 x2x2 x3x3 #0 #1 #3#2 #4 #5 Huth-Ryan, pp. 373

27 CMPUT Compiler Design and Optimization27 The apply algorithm zImplement operations on boolean functions such as +, , , and complement. zLet f and g be represented by OBDDs B f and B g. Huth-Ryan, pp. 373

28 CMPUT Compiler Design and Optimization28 The apply algorithm zLet v be the highest variable in B f or B g. zSplit the problem into two sub-problems, for v = 0 and for v = 1, and solve recursively. zAt the leaves, apply the Boolean operation directly. Huth-Ryan, pp. 374

29 CMPUT Compiler Design and Optimization29 The apply algorithm zf[0/x] and f[1/x] are restrictions of f. zf[0/x] is obtained by replacing x in f by 0. zFor all functions f and all variables x: f  x  f[0/x] + x  f[1/x] Huth-Ryan, pp. 374

30 CMPUT Compiler Design and Optimization30 The apply algorithm (Shannon expansion) f op g = x i  (f[0/x i ] op g[0/x i ]) + x i  (f[1/x i ] op g[1/x i ]) Let B f and B g be the OBDDs of f and g, we want to compute B f op g Let r f and r g be the root node of B f and B g Huth-Ryan, pp. 374

31 CMPUT Compiler Design and Optimization31 The apply algorithm zif r f and r g are terminals with labels l f and l g, then compute l f op l g (the result is B 0 or B 1 ). zif r f and r g are x i -nodes, then create an x i -node n with  a dashed line to apply (op, lo(r f ), lo(r g ))  a solid line to apply (op, hi(r f ), hi(r g )) zif r f is an x i -nodes and (r g is a terminal node or (r j is an x j -node and j>i)) then create an x i -node n with:  A dashed line to apply (op, lo(r f ), r g )  A solid line to apply (op, hi(r f ), r g ) Huth-Ryan, pp. 374

32 CMPUT Compiler Design and Optimization32 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 A(R 1,S 1 ) A(R 1,S 1 ) = ?

33 CMPUT Compiler Design and Optimization33 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 A(R 2,S 3 ) x1x1 A(R 3,S 2 ) T1T1 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = ?

34 CMPUT Compiler Design and Optimization34 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 A(R 4,S 3 ) x1x1 A(R 3,S 2 ) x2x2 A(R 3,S 3 ) T1T1 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = ? T2T2

35 CMPUT Compiler Design and Optimization35 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 A(R 3,S 3 ) A(R 5,S 4 )A(R 6,S 5 ) x4x4 T1T1 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 5,S 4 ) = ? T2T2 T3T3

36 CMPUT Compiler Design and Optimization36 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 A(R 3,S 3 ) A(R 6,S 5 ) x4x4 0 T1T1 T2T2 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 6,S 5 ) = ? T3T3 T4T4

37 CMPUT Compiler Design and Optimization37 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 A(R 3,S 3 ) x4x4 01 T1T1 T2T2 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = ? T3T3 T4T4 T5T5

38 CMPUT Compiler Design and Optimization38 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 x4x4 01 A(R 4,S 3 )A(R 6,S 3 ) x3x3 T1T1 T2T2 T3T3 T4T4 A(R 4,S 3 ) = ? T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5

39 CMPUT Compiler Design and Optimization39 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 x4x4 01 A(R 6,S 3 ) x3x3 T1T1 T2T2 T3T3 T4T4 A(R 6,S 3 ) = ? T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5

40 CMPUT Compiler Design and Optimization40 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T4T4 A(R 6,S 3 ) = T 7 A(R 6,S 4 ) = ? T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5 A(R 6,S 4 ) A(R 6,S 5 ) x4x4 T7T7

41 CMPUT Compiler Design and Optimization41 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T4T4 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5 A(R 6,S 5 ) x4x4 T7T7 1 T8T8 A(R 6,S 3 ) = T 7 A(R 6,S 5 ) = ?

42 CMPUT Compiler Design and Optimization42 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 A(R 3,S 2 ) x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T4T4 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5 x4x4 T7T7 1 T8T8 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = ? 1 T9T9

43 CMPUT Compiler Design and Optimization43 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T4T4 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5 x4x4 T7T7 1 T8T8 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 A(R 4,S 3 ) = ? 1 T9T9 A(R 4,S 3 ) A(R 6,S 5 ) x3x3 T 10

44 CMPUT Compiler Design and Optimization44 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T4T4 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5 x4x4 T7T7 1 T8T8 1 T9T9 A(R 6,S 5 ) x3x3 T 10 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 A(R 6,S 5 ) = ?

45 CMPUT Compiler Design and Optimization45 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T4T4 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 T5T5 x4x4 T7T7 1 T8T8 1 T9T9 x3x3 T 10 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #1

46 CMPUT Compiler Design and Optimization46 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 x4x4 T7T7 11 x3x3 T 10 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #0 #1 ?

47 CMPUT Compiler Design and Optimization47 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 T2T2 T3T3 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 x4x4 11 x3x3 T 10 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #0 #1 ?

48 CMPUT Compiler Design and Optimization48 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 T2T2 T6T6 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 x4x4 11 x3x3 T 10 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #0 #1 #2 ? #1

49 CMPUT Compiler Design and Optimization49 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 T2T2 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 x4x4 11 x3x3 T 10 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #0 #1 #2 #3 ? #1

50 CMPUT Compiler Design and Optimization50 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 x4x4 11 x3x3 T 10 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #0 #1 #2 #3 ? #4 #1

51 CMPUT Compiler Design and Optimization51 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 T1T1 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 x4x4 11 x3x3 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #0 #1 #2 #3 #4 #1 #3 ?

52 CMPUT Compiler Design and Optimization52 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 01 x3x3 A(R 1,S 1 ) = T 1 A(R 2,S 3 ) = T 2 A(R 4,S 3 ) = T 3 A(R 3,S 3 ) = T 6 x4x4 11 x3x3 A(R 6,S 3 ) = T 7 A(R 3,S 2 ) = T 10 1 #0 #1 #2 #3 #4 #1 #3 #5

53 CMPUT Compiler Design and Optimization53 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 0 1 x3x3 x3x3 #0 #1 #2 #3 #4 #5

54 CMPUT Compiler Design and Optimization54 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 x2x2 x4x4 0 1 x3x3 #0 #1 #2 #3 #4 #5

55 CMPUT Compiler Design and Optimization55 The apply algorithm (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 + R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 x1x1 0 1 x3x3 x2x2 x4x4 =

56 CMPUT Compiler Design and Optimization56 Huth-Ryan, pp. 377 The restrict algorithm (example)  Restrict (0, x, B f ) computes the OBDD for f[0/x] with the variable ordering of B f. yFor each node n labelled with x: xRedirect incoming edges to lo(n); xRemove n;  Call reduce on the resulting OBDD;  Restrict (1, x, B f ) is analogous, but incoming edges are redirected to hi(n).

57 CMPUT Compiler Design and Optimization57 The exists quantifier zGiven a function f(x, …), does a value of x exist that makes f true? This existence quantifier, noted  x.f is defined as:  x.f = f[0/x] + f[1/x] zThe dual of  is a quantifier that is true if f can be made false by putting x to 0 or to 1. This quantifier, noted  x.f, is defined as:  x.f = f[0/x]  f[1/x] Huth-Ryan, pp. 377

58 CMPUT Compiler Design and Optimization58 The exists algorithm  The exists algorithms can be defined in terms of apply and restrict : apply (+, restrict (0,x,B f ), restrict (1,x,B f ))  But this is not efficient. Apply works on two BDDs that are identical to the level of the x- nodes.  A more efficient algorithm returns B f with each x-node n replaced with the result of apply (+,lo(n),hi(n)). Huth-Ryan, pp. 377

59 CMPUT Compiler Design and Optimization59 The exists algorithm (example) BfBf  x 3.f = ? apply (+,R 7,R 6 ) = ? x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 A(R 7,R 6 ) y3y3 A(R 7,R 7 ) A(R 7,R 8 ) y3y3 0 1 Huth-Ryan, pp. 379

60 CMPUT Compiler Design and Optimization60 The exists algorithm (example) BfBf  x 3.f x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 Huth-Ryan, pp. 379 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8

61 CMPUT Compiler Design and Optimization61 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f  x 3.  x 2.f = ? apply (+,R 6,R 4 ) = ? Huth-Ryan, pp. 379 A(R 6,R 4 )

62 CMPUT Compiler Design and Optimization62 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f  x 3.  x 2.f = ? apply (+,R 6,R 4 ) = ? Huth-Ryan, pp. 379 y2y2 A(R 6,R 6 ) A(R 6,R 8 )

63 CMPUT Compiler Design and Optimization63 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f  x 3.  x 2.f = ? apply (+,R 6,R 4 ) = ? Huth-Ryan, pp. 379 y2y2 A(R 6,R 8 ) y3y3 01

64 CMPUT Compiler Design and Optimization64 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f  x 3.  x 2.f = ? apply (+,R 6,R 4 ) = ? Huth-Ryan, pp. 379 y2y2 A(R 7,R 8 ) y3y3 01 y3y3 A(R 8,R 8 )

65 CMPUT Compiler Design and Optimization65 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f  x 3.  x 2.f = ? apply (+,R 6,R 4 ) = ? Huth-Ryan, pp. 379 y2y2 y3y3 01 y3y3 A(R 8,R 8 )

66 CMPUT Compiler Design and Optimization66 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f  x 3.  x 2.f = ? apply (+,R 6,R 4 ) = ? Huth-Ryan, pp. 379 y2y2 y3y3 01 y3y3

67 CMPUT Compiler Design and Optimization67 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f  x 3.  x 2.f = ? apply (+,R 6,R 4 ) = ? Huth-Ryan, pp. 379 y2y2 y3y3 01

68 CMPUT Compiler Design and Optimization68 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 0 1 y3y3 R1R1 R2R2 R4R4 R6R6 R7R7 R8R8  x 3.  x 2.f Huth-Ryan, pp. 379

69 CMPUT Compiler Design and Optimization69 The exists algorithm (example) BfBf x1x1 y1y1 y2y2 x2x2 x3x3 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 R7R7 R8R8 x1x1 y1y1 y2y2 x2x2 0 1 y3y3 R1R1 R2R2 R3R3 R4R4 R6R6 R7R7 R8R8 B  x3.f x1x1 y1y1 y2y2 0 1 y3y3 R1R1 R2R2 R4R4 R6R6 R7R7 R8R8 B  x2.x3.f Huth-Ryan, pp. 379

70 CMPUT Compiler Design and Optimization70 Translation of Boolean formulas into OBDDs Formula fOBDD B f 0 1 x B0B0 B1B1 BxBx fSwap 0- and 1-nodes in B f f + g f  g f  g apply (+, B f, B g ) apply ( , B f, B g ) apply ( , B f, B g ) f[1/x] f[0/x] restrict (1, x, B f ) restrict (0, x, B f )  x.f  x.f apply (+, B f[0/x], B f[1/x] ), apply ( , B f[0/x], B f[1/x] ), Huth-Ryan, pp. 380

71 CMPUT Compiler Design and Optimization71 Upper bounds for running times Algorithm Input OBDD(s) Output OBDD Time- complexity reduce Breduced BO(|B|.log|B|) apply B f, B g (reduced) B f op g (reduced) O(|B f |.|B g |) restrict B f (reduced) B f[0/x] or B f[1/x] (reduced) O(|B f |.log|B f |)  B f (reduced) B  x1.  x2…  xn.f (reduced) NP-complete Huth-Ryan, pp. 380

72 CMPUT Compiler Design and Optimization72 Relational Product zGiven two functions f(x, …) and g(x,...), does a value of x exist that makes the conjunction of f and g true?  x.(fg) = (fg)[0/x] + (fg)[1/x] zThis combination of the existence quantifier with conjunction is called the relational product of f and g.  The relational product can be defined in terms of apply and restrict : B fg = apply (, B f, B g ) apply (+, restrict (0,x, B fg ), restrict (1,x, B fg )) zBut computing the relational product this way would be too expensive.

73 CMPUT Compiler Design and Optimization73 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 ) (R 4,S 3 ) (R 5,S 4 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ]

74 CMPUT Compiler Design and Optimization74 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] 0 0: T 1 (R 6,S 5 ) T1T1 (R 4,S 3 )

75 CMPUT Compiler Design and Optimization75 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] 1 T2T2 0: T 1 1: T 2 A(,R 4,S 3 ): ? 0 T1T1 (R 4,S 3 )

76 CMPUT Compiler Design and Optimization76 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 3 ) 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 x4x4 T3T3 1 T2T2 0 T1T1

77 CMPUT Compiler Design and Optimization77 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 3 ) (R 4,S 3 ) 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 x4x4 T3T3 1 T2T2 0 T1T1

78 CMPUT Compiler Design and Optimization78 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 3 ) (R 6,S 3 ) 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): ? x4x4 T3T3 1 T2T2 0 T1T1

79 CMPUT Compiler Design and Optimization79 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 3 ) (R 6,S 3 ) R 6 is a terminal node (B 1 ) thus (R 6,S 3 ) results in S 3, which already exists in the BDD. 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): ? x4x4 T3T3 1 T2T2 0 T1T1

80 CMPUT Compiler Design and Optimization80 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 3 ) 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): ? x4x4 T3T3 1 T2T2 0 T1T1

81 CMPUT Compiler Design and Optimization81 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 ) (R 2,S 3 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] x4x4 T3T3 1 T2T2 0 T1T1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): ?

82 CMPUT Compiler Design and Optimization82 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] x4x4 T3T3 1 T2T2 0 T1T1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3

83 CMPUT Compiler Design and Optimization83 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 2 ) x4x4 T3T3 1 T2T2 0 T1T1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3

84 CMPUT Compiler Design and Optimization84 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 2 ) (R 4,S 3 ) x4x4 T3T3 1 T2T2 0 T1T1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3

85 CMPUT Compiler Design and Optimization85 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 2 ) (R 6,S 5 ) x4x4 T3T3 1 T2T2 0 T1T1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3 A(,R 6,S 5 ): ?

86 CMPUT Compiler Design and Optimization86 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] (R 3,S 2 ) x4x4 T3T3 1 T2T2 0 T1T1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3 A(,R 6,S 5 ): T 2 A(,R 3,S 2 ): ?

87 CMPUT Compiler Design and Optimization87 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] x4x4 T3T3 1 T2T2 0 T1T1 x3x3 X 3 is a variable in the relational product, thus we must apply the existence quantifier to it. 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3 A(,R 6,S 5 ): T 2 A(,R 3,S 2 ): ?

88 CMPUT Compiler Design and Optimization88 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] x4x4 T3T3 1 T2T2 0 T1T1 A(+,T 3,T 2 ) 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3 A(,R 6,S 5 ): T 2 A(,R 3,S 2 ): ? A( +,T 3,T 2 ): ?

89 CMPUT Compiler Design and Optimization89 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5 (R 1,S 1 )  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] x4x4 T3T3 1 T2T2 0 T1T1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3 A(,R 6,S 5 ): T 2 A(,R 3,S 2 ): T 2 A( +,T 3,T 2 ): T 2 A(,R 1,S 1 ): ?

90 CMPUT Compiler Design and Optimization90 A relational product (example) Huth-Ryan, pp. 375 x1x1 0 1 x3x3 x2x2 x4x4 x1x1 0 1 x3x3 x4x4 R1R1 R2R2 R3R3 R4R4 R5R5 R6R6 S1S1 S2S2 S3S3 S4S4 S5S5  x 3.(fg) = (fg)[0/x 3 ] + (fg)[1/x 3 ] x4x4 T3T3 1 T2T2 0 T1T1 x1x1 0: T 1 1: T 2 A(,R 4,S 3 ): T 3 A(,R 6,S 3 ): T 3 A(,R 3,S 3 ): T 3 A(,R 2,S 3 ): T 3 A(,R 6,S 5 ): T 2 A(,R 3,S 2 ): T 2 A( +,T 3,T 2 ): T 2 A(,R 1,S 1 ): ? T4T4

91 CMPUT Compiler Design and Optimization91 BDD packages zBDDs are becoming popular for several applications thanks to the existence of efficient BDD packages : yBuDDy: A Binary Decision Diagram Package by J Ø rn Lind-Nielsen from Technical University of Denmark xhttp://www.itu.dk/research/buddyhttp://www.itu.dk/research/buddy yCUDD: CU Decision diagram package, by Fabio Somenzi from University of Colorado at Boulder xhttp://vlsi.colorado.edu/~fabio/CUDDhttp://vlsi.colorado.edu/~fabio/CUDD yJedd: A BDD-based relational extension of Java by Ondrej Lhoták from McGill University xhttp://www.sable.mcgill.ca/jeddhttp://www.sable.mcgill.ca/jedd

92 CMPUT Compiler Design and Optimization92 Representing Subsets with OBDDs zLet S be a finite set. zAssign a unique Boolean vector (v 1, v 2, …, v n ) to each element s  S: y 2 n-1 < |S|  2 n yv i  {0, 1} zA subset T  S is represented by a Boolean function f T. zf T maps onto 1 if s  T, and maps it onto 0 otherwise. Huth-Ryan, pp. 383

93 CMPUT Compiler Design and Optimization93 Using OBDDs to represent points-to relations This program segment has: zthree allocation statements (objects): L 1, L 2 and L 3 ; zthree pointers: a, b, and c; zWe can encode the pointers with two bits; zWe can encode the objects with two bits; ObjectCode L1L1 00 L2L2 01 L3L3 10 PointerCode a00 b01 c10 Berndl-Lhotak et al., PLDI03 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b;

94 CMPUT Compiler Design and Optimization94 Using OBDDs to represent points-to relations zWe compute the may point-to relation: T = {(a,L 1 ); (a,L 2 ); (b,L 1 ); (b,L 2 ); (c,L 1 ); (c,L 2 ); (c,L 3 )} Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; ObjectCode L1L1 00 L2L2 01 L3L3 10 After Berndl-Lhotak et al., PLDI03

95 CMPUT Compiler Design and Optimization95 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v2v2 v2v2 v4v4 v4v4 v4v4 v4v4 v4v4 v4v4 v4v4 v4v4 v3v3 v3v3 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

96 CMPUT Compiler Design and Optimization96 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v2v2 v2v2 v4v4 v4v4 v4v4 v4v4 v4v4 v4v4 v4v4 v4v4 v3v3 v3v3 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

97 CMPUT Compiler Design and Optimization97 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v2v2 v2v2 v4v4 v4v4 v4v4 v4v4 v4v4 v4v4 v3v3 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

98 CMPUT Compiler Design and Optimization98 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v2v2 v2v2 v4v4 v4v4 v4v4 v3v3 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

99 CMPUT Compiler Design and Optimization99 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v2v2 v3v3 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

100 CMPUT Compiler Design and Optimization100 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v2v2 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

101 CMPUT Compiler Design and Optimization101 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v2v2 v3v3 10 After Berndl-Lhotak et al., PLDI03

102 CMPUT Compiler Design and Optimization102 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Using OBDDs to represent points-to relations Code v 1 v 2 v 3 v 4 fTfT x x PointerCode a00 b01 c10 Code v 1 v 2 v 3 v 4 fTfT x 1100x 1101x 1110x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 10 v1v1 v3v3 10 After Berndl-Lhotak et al., PLDI03

103 CMPUT Compiler Design and Optimization103 Using OBDDs to represent points-to relations v1v1 v3v3 10 v1v1 v3v3 10 v2v2 v3v3 v4v4 We obtained the OBDD on the left using don’t care values (x) for the bit patterns that were not used to encode any point-to relation. In their PLDI 2003 paper, Berndl/Lhotak et al assign 0 to the unused bit patterns and obtain the OBDD on the right. After Berndl-Lhotak et al., PLDI03

104 CMPUT Compiler Design and Optimization104 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Effect of bit ordering Code v 4 v 2 v 3 v 1 fTfT x x PointerCode a00 b01 c02 Code v 4 v 2 v 3 v 1 fTfT x 0110x 1110x 0111x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 02 v4v4 v2v2 v2v2 v1v1 v1v1 v1v1 v1v1 v1v1 v1v1 v1v1 v1v1 v3v3 v3v3 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

105 CMPUT Compiler Design and Optimization105 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Effect of bit ordering Code v 4 v 2 v 3 v 1 fTfT x x PointerCode a00 b01 c02 Code v 4 v 2 v 3 v 1 fTfT x 0110x 1110x 0111x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 02 v4v4 v2v2 v2v2 v1v1 v1v1 v1v1 v1v1 v1v1 v1v1 v1v1 v1v1 v3v3 v3v3 v3v3 v3v3 10 After Berndl-Lhotak et al., PLDI03

106 CMPUT Compiler Design and Optimization106 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Effect of bit ordering Code v 4 v 2 v 3 v 1 fTfT x x PointerCode a00 b01 c02 Code v 4 v 2 v 3 v 1 fTfT x 0110x 1110x 0111x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 02 v4v4 v2v2 v1v1 v3v3 10 After Berndl-Lhotak et al., PLDI03

107 CMPUT Compiler Design and Optimization107 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Effect of bit ordering Code v 4 v 2 v 3 v 1 fTfT x x PointerCode a00 b01 c02 Code v 4 v 2 v 3 v 1 fTfT x 0110x 1110x 0111x 1111x ObjectCode L1L1 00 L2L2 01 L3L3 02 After Berndl-Lhotak et al., PLDI03 v4v4 v2v2 v1v1 v3v3 1 0

108 CMPUT Compiler Design and Optimization108 Effect of bit ordering With the bit ordering we obtained the OBDD on the left when using don’t care values. In their PLDI 2003 paper, Berndl/Lhotak et al obtain the OBDD on the right (they do not use don’t care values). After Berndl-Lhotak et al., PLDI03 v4v4 v2v2 v1v1 v3v3 1 0 v4v4 v3v3 10 v2v2 v3v3 v1v1 v2v2 v3v3 v1v1

109 CMPUT Compiler Design and Optimization109 Effect of bit ordering and use of don’t cares After Berndl-Lhotak et al., PLDI03 v4v4 v2v2 v1v1 v3v3 1 0 v4v4 v3v3 10 v2v2 v3v3 v1v1 v2v2 v3v3 v1v1 v1v1 v3v3 10 v1v1 v3v3 10 v2v2 v3v3 v4v4

110 CMPUT Compiler Design and Optimization110 Relations zReference analysis can be defined in terms of relations. zA domain is a set of elements. yTo encode the relations in the code on the left we need two domains: xdomain of pointers = {a, b, c} xdomain of abstract objects = {L 1, L 2, L 3 } L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; After Berndl-Lhotak et al., PLDI03

111 CMPUT Compiler Design and Optimization111 Relations zAn attribute is a domain along with an associated name. yTo encode the relations in the code on the left we need four attributes: xpointer = {a, b, c} xobject = {L 1, L 2, L 3 } xsource = {a, b, c} xdestination = {a, b, c} L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; After Berndl-Lhotak et al., PLDI03

112 CMPUT Compiler Design and Optimization112 Relations zA tuple is a list of elements indexed by attributes. zExamples of tuples include: L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; pointerobject bL2L2 sourcedestination ab After Berndl-Lhotak et al., PLDI03

113 CMPUT Compiler Design and Optimization113 Relations zA relation is a collection of tuples that share the same attributes. yExamples of relations for this code: L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; pointerobject aL1L1 bL2L2 cL3L3 sourcedestination ba ab bc Initial points-to pair. Assignments. After Berndl-Lhotak et al., PLDI03

114 CMPUT Compiler Design and Optimization114 Point-to Algorithm zThe goal is to compute a point-to relation between variables of pointer time and allocation sites (or abstract objects). pointerobject ?? ?? pointerobject aL1L1 bL2L2 cL3L3 Initial points-to pair. After Berndl-Lhotak et al., PLDI03

115 CMPUT Compiler Design and Optimization115 Point-to Algorithm zThe context-insensitive, flow-insensitive algorithm repeatedly applies the program’s constraints to the initial point-to relation defined by the allocation sites. pointerobject ?? ?? pointerobject aL1L1 bL2L2 cL3L3 Initial points-to pairs. constraints Final points-to pairs. After Berndl-Lhotak et al., PLDI03

116 CMPUT Compiler Design and Optimization116 Interference rules if l 1 points to o, and l 1 is assigned to l 2, then l 2 also points to o. Simple assignment: if l points to o 2, and l is stored into q.f, then for each o 1 pointed to by q, o 1.f also points to o 2. Field store: if l is loaded from p.f, and p points to o 1, then for each o 2 pointed to by o 1.f, l points to o 2. Field load: After Berndl-Lhotak et al., PLDI03

117 CMPUT Compiler Design and Optimization117 Domains, Relations, and BDDs (example) zThe general algorithm uses five attributes, but this examples only needs three attributes: xV = {a, b, c} xW = {a, b, c} xH = {L 1, L 2, L 3 } L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; Variables Heap Locations After Berndl-Lhotak et al., PLDI03

118 CMPUT Compiler Design and Optimization118 Domains, Relations, and BDDs (example) zThe allocations generate the initial points-to relation: L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; VH aL1L1 bL2L2 cL3L3 VCode a00 b01 c HCode L1L1 00 L2L2 01 L3L3 Code VH initial pt x x Code VH initial pt x 1100x 1101x 1110x 1111x After Berndl-Lhotak et al., PLDI03

119 CMPUT Compiler Design and Optimization119 Domains, Relations, and BDDs (example) INITIAL POINTS-TO Note: Don’t cares have been assigned so nodes at the same level merge. What have the don’t cares been assigned to? V0V0 H1H1 1 H0H0 H0H0 0 V1V1 H1H1 Code VH initial pt x x Code VH initial pt x 1100x 1101x 1110x 1111x Code VH initial pt 0011x 0111x 1011x 1100x 1101x 1110x 1111x After Berndl-Lhotak et al., PLDI03

120 CMPUT Compiler Design and Optimization120 Domains, Relations, and BDDs (example) INITIAL POINTS-TO Note: Don’t cares have been assigned so nodes at the same level merge. What have the don’t cares been assigned to? V0V0 H1H1 1 H0H0 H0H0 0 V1V1 H1H1 Code VH initial pt x x Code VH initial pt x 1100x 1101x 1110x 1111x Code VH initial pt x 1011x 1100x 1101x 1110x 1111x After Berndl-Lhotak et al., PLDI03

121 CMPUT Compiler Design and Optimization121 Domains, Relations, and BDDs (example) INITIAL POINTS-TO Note: Don’t cares have been assigned so nodes at the same level merge. What have the don’t cares been assigned to? V0V0 H1H1 1 H0H0 H0H0 0 V1V1 H1H1 Code VH initial pt x x Code VH initial pt x 1100x 1101x 1110x 1111x Code VH initial pt x 1100x 1101x 1110x 1111x After Berndl-Lhotak et al., PLDI03

122 CMPUT Compiler Design and Optimization122 Domains, Relations, and BDDs (example) INITIAL POINTS-TO Note: Don’t cares have been assigned so nodes at the same level merge. What have the don’t cares been assigned to? V0V0 H1H1 1 H0H0 H0H0 0 V1V1 H1H1 Code VH initial pt x x Code VH initial pt x 1100x 1101x 1110x 1111x Code VH initial pt After Berndl-Lhotak et al., PLDI03

123 CMPUT Compiler Design and Optimization123 Domains, Relations, and BDDs (example) zThe assignments generate the edge-set relation: L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; VW ba ab bc VCode a00 b01 c WCode a00 b01 c Code VW edge- set x x Code VW edge- set x 1100x 1101x 1110x 1111x After Berndl-Lhotak et al., PLDI03

124 CMPUT Compiler Design and Optimization124 Domains, Relations, and BDDs (example) V0V0 V1V1 W1W1 0 1 W1W1 EDGE-SET Code VW edge- set x x Code VW edge- set x 1100x 1101x 1110x 1111x After Berndl-Lhotak et al., PLDI03

125 CMPUT Compiler Design and Optimization125 Domains, Relations, and BDDs (example) INITIAL POINTS-TO V0V0 V1V1 W1W1 0 1 W1W1 EDGE-SET Variable Order: V0V0 V1V1 W0W0 W1W1 H0H0 H1H1 V0V0 H1H1 1 H0H0 H0H0 0 V1V1 H1H1 After Berndl-Lhotak et al., PLDI03

126 CMPUT Compiler Design and Optimization126 Domains, Relations, and BDDs (example) W1W1 H1H1 H0H0 H1H1 1 0 RELPROD(V) AFTER RELPROD(Init, Edge, V) AFTER REPLACE(W to V) INITIAL POINTS-TO EDGE-SET V0V0 V1V1 W1W1 0 1 W1W1 V0V0 H1H1 1 H0H0 H0H0 0 V1V1 H1H1 V1V1 H1H1 H0H0 H1H1 1 0 After Berndl-Lhotak et al., PLDI03

127 CMPUT Compiler Design and Optimization127 Domains, Relations, and BDDs (example) AFTER REPLACE(W to V) It represents propagation of the points-to relation caused by the assignments. V1V1 H1H1 H0H0 H1H1 1 0 {(a,L 2 ),(b,L 1 ), (c,L 2 )} What does this relation represent? What is the domain of this relation? What elements are in this relation? V: Variables (a = 00, b = 01, c = 10) H : Heap Objects (L 1 = 00, L 2 = 01, L 3 = 10) L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; After Berndl-Lhotak et al., PLDI03

128 CMPUT Compiler Design and Optimization128 Domains, Relations, and BDDs (example) UNION INITIAL POINTS-TO DUE TO EDGE-SET V1V1 H1H1 H0H0 H1H1 1 0 V0V0 H1H1 1 H0H0 H0H0 0 V1V1 H1H1 V0V0 V1V1 H0H0 H1H1 1 0 H0H0 POINTS-TO After 1 st Propagation H1H1 After Berndl-Lhotak et al., PLDI03

129 CMPUT Compiler Design and Optimization129 Domains, Relations, and BDDs (example) Elements in the Points-to set: { (a,L 1 ), (a,L 2 ),, (b,L 1 ), (b,L 2 ), (c,L 2 ), (c,L 3 ) } V0V1H0H1V0V1H0H1 0000(a,L 1 ) 0001(a,L 2 ) 0100(b,L 1 ) 0101(b,L 2 ) 1001(c,L 2 ) 1010(c,L 3 ) POINTS-TO After 1 st Propagation After Berndl-Lhotak et al., PLDI03 V0V0 V1V1 H0H0 H1H1 1 0 H0H0 H1H1 H0H0 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; -Points-to relations caused by more than 1 propagation: { (a,L 1 ) } What’s Missing?

130 CMPUT Compiler Design and Optimization130 Domains, Relations, and BDDs (example) UNION V0V0 V1V1 H0H0 1 0 H0H0 POINTS-TO DUE TO EDGE-SET POINTS-TO After 2 nd Propagation V1V1 H1H1 H0H0 H1H1 1 0 H1H1 After Berndl-Lhotak et al., PLDI03 V0V0 V1V1 H0H0 H1H1 1 0 H0H0 H1H1 H0H0 POINTS-TO After 1 st Propagation

131 CMPUT Compiler Design and Optimization131 Domains, Relations, and BDDs (example) V0V0 V1V1 H0H0 1 0 H0H0 POINTS-TO After 3 nd Propagation (Unchanged) H1H1 POINTS-TO After 2 nd Propagation UNION V0V0 V1V1 H0H0 1 0 H0H0 POINTS-TO DUE TO EDGE-SET V1V1 H1H1 H0H0 H1H1 1 0 H1H1 After Berndl-Lhotak et al., PLDI03

132 CMPUT Compiler Design and Optimization132 Domains, Relations, and BDDs (example) V0V0 V1V1 H0H0 1 0 H0H0 H1H1 Final Points-to Set (a,L1)00001 (a,L2)00011 (b,L1)01001 (b,L2)01011 (c,L2)10011 (c,L3)10101 (c,L1)10001 (a,L3)00100 (b,L3)01100 L 1 : a = new O(); L 2 : b = new O(); L 3 : c = new O(); a = b; b = a; c = b; After Berndl-Lhotak et al., PLDI03

133 CMPUT Compiler Design and Optimization133 Why Use BDDs for Program Analysis?  Space  Large analysis has only been accomplished when BDDs are used to represent the points-to set.  Other efficient representations use 4x to 6x the space required by BDDs.  Time  For small analysis BDDs are slower (~1.5 to 2x).  For large analysis BDDs are significantly faster because of the reduced memory usage. After Berndl-Lhotak et al., Tech Report 2003


Download ppt "CMPUT 680 - Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic Q: Binary Decision Diagrams José Nelson Amaral"

Similar presentations


Ads by Google