CMPUT 680 Winter 2006 Topic Q Binary Decision
CMPUT 680 - Winter 2006 Topic Q: Binary Decision Diagrams José Nelson Amaral http: //www. cs. ualberta. ca/~amaral/courses/680 CMPUT 680 - Compiler Design and Optimization 1
Reading Material z. Michael Huth and Mark Ryan, Logic in Computer Science: Modelling and Reasoning about Systems, 2 nd Edition, Cambridge University Press, 2005 (Chapter 6). CMPUT 680 - Compiler Design and Optimization 2
Binary Decision Tree f(x, y) = x+y x Boolean Function x 0 0 1 y 0 1 0 f 1 0 0 1 1 0 Truth Table y y 0 1 0 0 Binary Decision Tree CMPUT 680 - Compiler Design and Optimization 3 Huth-Ryan, pp. 361
Binary Decision Tree Binary Decision Diagram x y 1 y y 0 x Removal of duplicate terminals. 0 Binary Decision Tree 0 y 0 1 Binary Decision Diagram CMPUT 680 - Compiler Design and Optimization 4 Huth-Ryan, pp. 362
Binary Decision Tree Binary Decision Diagram x y 1 x Removal of redundant tests. y 0 y 1 Binary Decision Diagram 0 Binary Decision Diagram CMPUT 680 - Compiler Design and Optimization 5 Huth-Ryan, pp. 362
Removal of duplicate nonterminals z z x x y y 0 y x y 1 y 0 CMPUT 680 - Compiler Design and Optimization y 1 6 Huth-Ryan, pp. 363
Removal of duplicate nonterminals z z x x y y 0 x 1 y 0 CMPUT 680 - Compiler Design and Optimization y 1 7 Huth-Ryan, pp. 363
Removal of redundant decision point z z x x y 0 x y 1 y 0 CMPUT 680 - Compiler Design and Optimization y 1 8 Huth-Ryan, pp. 363
Reduced BDDs z. We saw three optimizations that can be performed in BDDs: y. C 1: Removal of duplicate terminals y. C 2: Removal of redundant tests y. C 3: Removal of duplicate non-terminals z. If none of these optimizations can be applied to a BDD, the BDD is reduced. CMPUT 680 - Compiler Design and Optimization 9 Huth-Ryan, pp. 365
Boolean Operations on BDDs z. Given two BDDs Bf and Bg representing boolean functions f and g, how do we obtain a BDD for the following functions: yf yf + g yf · g CMPUT 680 - Compiler Design and Optimization 10 Huth-Ryan, pp. 365
Ordered BDDs z Let L = [x 1, … xn] be an ordered list of variables without duplications. y A BDD B is ordered for L if 1. all variable labels of B occur in L; and 2. for every occurrence of xi followed by xj along a path in B, i < j. z An ordered BDD (OBDD) is a BDD that is ordered according to some list of variables. CMPUT 680 - Compiler Design and Optimization 11 Huth-Ryan, pp. 367
Example of a BDD that is not ordered z y x x y 0 1 CMPUT 680 - Compiler Design and Optimization 12 Huth-Ryan, pp. 368
OBDDs z There cannot be multiple occurrences of any variable along a path in an OBDD. z When operations are performed on two OBDDs, they must have compatible variable ordering. z The reduced OBDD representing a given function f is unique OBDDs have a canonical form. y. We can apply C 1 -C 3 in any order. CMPUT 680 - Compiler Design and Optimization 13 Huth-Ryan, pp. 368
OBDDs can be compact x 1 The function feven(x 1, x 2, …, xn) which is 1 if there is an even number of inputs with value 1 has an OBDD representation with only 2 n+1 nodes. CMPUT 680 - Compiler Design and Optimization x 2 x 3 x 4 1 0 14 Huth-Ryan, pp. 370
Exercise Build a reduced OBDD for the following boolean function: f(x 1, x 2, …, xn) = (x 1+x 2). (x 3 + x 4)…. (x 2 n-1 + x 2 n); Try to select a variable ordering that minimizes the number of nodes in the OBDD. CMPUT 680 - Compiler Design and Optimization 15 Huth-Ryan, pp. 371
Impact of Variable Ordering x 1 x 2 x 3 x 4 x 5 x 3 x 5 x 2 x 2 x 4 x 6 0 x 5 x 6 1 1 CMPUT 680 - Compiler Design and Optimization 0 16 Huth-Ryan, pp. 371
Importance of Canonical Representation z Absence of redundant variables: y. If f(x 1, x 2, …, xn) does not depend on xi, then any reduced OBDD of f does not contain xi. z Test for semantic equivalence: y. If f(x 1, x 2, …, xn) and g(x 1, x 2, …, xn) are represented by OBDDs Bf and Bg, then to find if f and g are equivalent, reduce Bf and Bg and check if they have identical structures. z Text for validity: yf(x 1, x 2, …, xn) is valid (always computes 1) if its reduced OBDD is B 1. CMPUT 680 - Compiler Design and Optimization 17 Huth-Ryan, pp. 370
Importance of Canonical Representation z Test for implication: y f(x 1, x 2, …, xn) implies g(x 1, x 2, …, xn) if the OBDD for f. /g is B 0. z Test for satisfiability: y f(x 1, x 2, …, xn) is satisfiable (computes 1 for at list one input) if its reduced OBDD is not B 0. CMPUT 680 - Compiler Design and Optimization 18 Huth-Ryan, pp. 372
The reduce algorithm z. Step 1: Label each node in the OBDD ytwo nodes receive the same id if and only if they represent the same function. z. Step 2: Traverse the OBDD bottom up ycollapse all nodes with the same label onto a single node and redirect the edges. CMPUT 680 - Compiler Design and Optimization 19 Huth-Ryan, pp. 372
The reduce algorithm z. Step 1: Label y. Initialization: Assign (at bottom of BDD): x label #0 to all 0 -nodes; x label #1 to all 1 -nodes; y. Let: x lo(n): node pointed by the dashed line from node n x hi(n): node pointed by the solid line. CMPUT 680 - Compiler Design and Optimization 20 Huth-Ryan, pp. 372
The reduce algorithm z. Step 1: Label y. Rules to label an internal xi-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 xi 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. CMPUT 680 - Compiler Design and Optimization 21 Huth-Ryan, pp. 373
The reduce algorithm (example) Rules to label an internal xinode n : x 1 x 2 ? x 3 #0 x 2 ? 0 xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: #1 1 x 3 ? x 3 #0 ? 0 x 3 • m has variable xi 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. #1 1 CMPUT 680 - Compiler Design and Optimization 22 Huth-Ryan, pp. 373
The reduce algorithm (example) Rules to label an internal xinode n : x 1 ? #2 x 3 #0 x 2 ? #2 0 #1 1 x 3 #2 x 3 #0 x 2 #3 0 xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: x 3 • m has variable xi 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. #1 1 CMPUT 680 - Compiler Design and Optimization 23 Huth-Ryan, pp. 373
The reduce algorithm (example) ? #2 #2 x 3 #0 x 1 x 2 #4 #2 0 Rules to label an internal xinode n : #1 1 x 3 #2 x 3 #0 x 2 #3 0 xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: x 3 • m has variable xi 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. #1 1 CMPUT 680 - Compiler Design and Optimization 24 Huth-Ryan, pp. 373
The reduce algorithm (example) #5 #2 #2 x 3 #0 x 1 x 2 #4 #2 0 Rules to label an internal xinode n : #1 1 x 3 #2 x 3 #0 x 2 #3 0 xif id(lo(n)) = id(hi(n)) then id(n) = id(lo(n)); xelse if there is another node m such that: x 3 • m has variable xi 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. #1 1 CMPUT 680 - Compiler Design and Optimization 25 Huth-Ryan, pp. 373
The reduce algorithm (example) #5 #2 #2 x 3 #0 #5 x 2 #4 #2 0 x 1 #1 1 x 3 #2 x 3 #0 x 2 #3 0 x 1 #4 x 3 #1 1 CMPUT 680 - Compiler Design and Optimization #2 #0 x 3 0 x 2 #3 #1 x 3 1 26 Huth-Ryan, pp. 373
The apply algorithm z. Implement operations on boolean functions such as +, , , and complement. z. Let f and g be represented by OBDDs Bf and Bg. CMPUT 680 - Compiler Design and Optimization 27 Huth-Ryan, pp. 373
The apply algorithm z. Let v be the highest variable in Bf or Bg. z. Split the problem into two sub-problems, for v = 0 and for v = 1, and solve recursively. z. At the leaves, apply the Boolean operation directly. CMPUT 680 - Compiler Design and Optimization 28 Huth-Ryan, pp. 374
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. z. For all functions f and all variables x: f x f[0/x] + x f[1/x] CMPUT 680 - Compiler Design and Optimization 29 Huth-Ryan, pp. 374
The apply algorithm (Shannon expansion) f op g = xi (f[0/xi] op g[0/xi]) + xi (f[1/xi] op g[1/xi]) Let Bf and Bg be the OBDDs of f and g, we want to compute Bf op g Let rf and rg be the root node of Bf and Bg CMPUT 680 - Compiler Design and Optimization 30 Huth-Ryan, pp. 374
The apply algorithm z if rf and rg are terminals with labels lf and lg, then compute lf op lg (the result is B 0 or B 1). z if rf and rg are xi-nodes, then create an xi-node n with y a dashed line to apply(op, lo(rf), lo(rg)) y a solid line to apply(op, hi(rf), hi(rg)) z if rf is an xi-nodes and (rg is a terminal node or (rj is an xj-node and j>i)) then create an xi-node n with: y A dashed line to apply(op, lo(rf), rg) y A solid line to apply(op, hi(rf), rg) CMPUT 680 - Compiler Design and Optimization 31 Huth-Ryan, pp. 374
The apply algorithm (example) A(R 1, S 1) = ? R 1 R 2 x 1 S 1 R 5 A(R 1, S 1) x 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 x 4 0 S 5 1 CMPUT 680 - Compiler Design and Optimization 32 Huth-Ryan, pp. 375
The apply algorithm (example) A(R 1, S 1) = T 1 A(R 2, S 3) = ? R 1 R 2 x 1 S 1 R 5 T 1 x 2 A(R 2, S 3) R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 1 A(R 3, S 2) x 3 x 4 0 S 5 1 CMPUT 680 - Compiler Design and Optimization 33 Huth-Ryan, pp. 375
The apply algorithm (example) A(R 1, S 1) = T 1 A(R 2, S 3) = T 2 A(R 4, S 3) = ? R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 A(R 4, S 3) x 1 x 2 A(R 3, S 2) A(R 3, S 3) x 4 0 S 5 1 CMPUT 680 - Compiler Design and Optimization 34 Huth-Ryan, pp. 375
The apply algorithm (example) 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) = ? R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 x 4 A(R 5, S 4) S 5 x 1 x 2 A(R 3, S 2) A(R 3, S 3) A(R 6, S 5) 1 CMPUT 680 - Compiler Design and Optimization 35 Huth-Ryan, pp. 375
The apply algorithm (example) 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) = ? R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 x 4 0 x 1 x 2 A(R 3, S 2) A(R 3, S 3) A(R 6, S 5) 1 CMPUT 680 - Compiler Design and Optimization 36 Huth-Ryan, pp. 375
The apply algorithm (example) 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) = ? R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 T 5 x 2 A(R 3, S 2) A(R 3, S 3) x 4 0 x 1 1 1 CMPUT 680 - Compiler Design and Optimization 37 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 x 4 0 T 5 1 x 2 T 6 A(R 4, S 3) = ? A(R 3, S 2) x 3 A(R 4, S 3) A(R 6, S 3) 1 CMPUT 680 - Compiler Design and Optimization 38 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 x 4 0 T 5 1 x 2 T 6 A(R 6, S 3) = ? A(R 3, S 2) x 3 A(R 6, S 3) 1 CMPUT 680 - Compiler Design and Optimization 39 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 x 4 0 1 CMPUT 680 - Compiler Design and Optimization T 5 1 x 2 T 6 T 7 A(R 6, S 3) = T 7 A(R 6, S 4) = ? A(R 3, S 2) x 3 x 4 A(R 6, S 4) A(R 6, S 5) 40 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 1 CMPUT 680 - Compiler Design and Optimization T 5 x 1 x 2 T 6 x 4 0 A(R 6, S 3) = T 7 A(R 6, S 5) = ? 1 T 8 T 7 1 A(R 3, S 2) x 3 x 4 A(R 6, S 5) 41 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 1 CMPUT 680 - Compiler Design and Optimization T 5 x 1 x 2 T 6 x 4 0 A(R 6, S 3) = T 7 A(R 3, S 2) = ? 1 T 8 A(R 3, S 2) x 3 T 7 x 4 1 T 9 1 42 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 1 CMPUT 680 - Compiler Design and Optimization T 5 x 1 x 2 T 6 x 4 0 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 A(R 4, S 3) = ? 1 T 8 T 10 x 3 A(R 4, S 3) A(R 6, S 5) x 3 T 7 x 4 1 T 9 1 43 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 x 4 0 1 CMPUT 680 - Compiler Design and Optimization T 5 x 1 x 2 T 6 1 T 8 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 A(R 6, S 5) = ? T 10 x 3 A(R 6, S 5) x 3 T 7 x 4 1 T 9 1 44 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 T 2 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 T 4 S 5 x 4 0 1 CMPUT 680 - Compiler Design and Optimization T 5 x 1 x 2 T 6 1 T 8 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 3 #1 1 x 3 T 7 x 4 1 T 9 1 45 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 #0 S 5 T 6 x 4 0 1 CMPUT 680 - Compiler Design and Optimization #1 x 2 T 2 R 3 R 4 x 1 1 #1 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 3 #1 1 x 3 T 7 x 4 ? 1 #1 1 46 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 T 3 x 4 0 #0 S 5 x 4 ? 0 1 CMPUT 680 - Compiler Design and Optimization #1 x 2 T 2 R 3 R 4 x 1 1 #1 T 6 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 3 #1 1 x 3 #1 x 4 1 #1 1 47 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 #2 x 4 0 #0 S 5 T 6 x 4 0 1 CMPUT 680 - Compiler Design and Optimization #1 x 2 T 2 R 3 R 4 x 1 1 #1 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 3 #1 1 x 3 ? #1 x 4 1 #1 1 48 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 x 4 0 x 2 ? T 2 x 3 + S 3 R 6 1 S 4 S 2 x 4 #0 S 5 T 10 x 3 #1 x 3 #2 0 x 1 T 1 x 2 R 3 R 4 x 1 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 4 0 1 CMPUT 680 - Compiler Design and Optimization #1 1 #1 #3 x 3 #1 x 4 1 #1 1 1 49 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 #4 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 #2 0 #0 S 5 x 1 x 2 T 10 x 3 ? #1 x 3 x 4 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 4 0 1 CMPUT 680 - Compiler Design and Optimization #1 1 #1 #3 x 3 #1 x 4 1 #1 1 1 50 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 T 1 x 2 #4 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 #2 0 #0 S 5 x 1 ? x 2 #3 x 3 #1 x 3 x 4 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 4 0 1 CMPUT 680 - Compiler Design and Optimization #1 1 #1 #3 x 3 #1 x 4 1 #1 1 1 51 Huth-Ryan, pp. 375
The apply algorithm (example) 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 R 1 R 2 x 1 S 1 R 5 #5 x 2 #4 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 #2 0 #0 S 5 x 1 x 2 #3 x 3 #1 x 3 x 4 A(R 6, S 3) = T 7 A(R 3, S 2) = T 10 x 4 0 1 CMPUT 680 - Compiler Design and Optimization #1 1 #1 #3 x 3 #1 x 4 1 #1 1 1 52 Huth-Ryan, pp. 375
The apply algorithm (example) R 1 R 2 x 1 S 1 R 5 #5 x 2 #4 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 #2 x 4 0 #0 S 5 x 4 x 1 x 2 #3 #3 x 3 0 1 CMPUT 680 - Compiler Design and Optimization #1 1 53 Huth-Ryan, pp. 375
The apply algorithm (example) R 1 R 2 x 1 S 1 R 5 #5 x 2 #4 R 3 R 4 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 0 #0 S 5 x 2 #3 #2 x 4 x 1 x 4 x 3 0 1 CMPUT 680 - Compiler Design and Optimization #1 1 54 Huth-Ryan, pp. 375
The apply algorithm (example) R 1 R 2 x 1 S 1 x 2 R 3 R 4 R 5 x 1 x 3 x 4 0 + S 3 R 6 1 S 4 S 2 x 3 = x 4 0 x 3 S 5 1 CMPUT 680 - Compiler Design and Optimization 0 1 55 Huth-Ryan, pp. 375
The restrict algorithm (example) z. Restrict(0, x, Bf) computes the OBDD for f[0/x] with the variable ordering of Bf. y. For each node n labelled with x: x. Redirect incoming edges to lo(n); x. Remove n; x. Call reduce on the resulting OBDD; z. Restrict(1, x, Bf) is analogous, but incoming edges are redirected to hi(n). CMPUT 680 - Compiler Design and Optimization 56 Huth-Ryan, pp. 377
The exists quantifier z Given 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] z The 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] CMPUT 680 - Compiler Design and Optimization 57 Huth-Ryan, pp. 377
The exists algorithm z The exists algorithms can be defined in terms of apply and restrict: apply(+, restrict(0, x, Bf), restrict(1, x, Bf)) z But this is not efficient. Apply works on two BDDs that are identical to the level of the xnodes. z A more efficient algorithm returns Bf with each x -node n replaced with the result of apply(+, lo(n), hi(n)). CMPUT 680 - Compiler Design and Optimization 58 Huth-Ryan, pp. 377
The exists algorithm (example) R 1 x 1 R 2 R 3 y 1 x 2 R 4 R 5 x 3. f = ? apply(+, R 7, R 6) = ? A(R 7, R 6) y 3 y 2 x 3 A(R 7, R 7) A(R 7, R 8) 0 1 R 6 y 3 R 7 0 R 8 Bf 1 CMPUT 680 - Compiler Design and Optimization 59 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 y 1 x 2 R 4 R 5 R 1 R 2 R 3 x 3. f y 2 x 1 x 2 R 4 x 3 y 1 y 2 R 6 y 3 R 7 0 R 8 Bf 1 R 7 0 CMPUT 680 - Compiler Design and Optimization R 8 1 60 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 y 2 x 3. x 2. f = ? y 1 x 2 R 4 apply(+, R 6, R 4) = ? A(R 6, R 4) y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf 1 R 7 0 R 8 1 CMPUT 680 - Compiler Design and Optimization B x 3. f 61 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 y 2 x 3. x 2. f = ? y 1 x 2 R 4 y 2 x 3 A(R 6, R 6) R 6 y 3 R 7 0 A(R 6, R 8) R 6 y 3 R 8 Bf apply(+, R 6, R 4) = ? 1 R 7 0 R 8 1 CMPUT 680 - Compiler Design and Optimization B x 3. f 62 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 y 2 x 3. x 2. f = ? y 1 apply(+, R 6, R 4) = ? x 2 R 4 y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf y 3 1 R 7 0 R 8 1 CMPUT 680 - Compiler Design and Optimization B x 3. f 0 A(R 6, R 8) 1 63 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 y 2 x 3. x 2. f = ? y 1 apply(+, R 6, R 4) = ? x 2 R 4 y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf y 3 1 R 7 0 R 8 1 CMPUT 680 - Compiler Design and Optimization B x 3. f 0 1 y 3 A(R 7, R 8) A(R 8, R 8) 64 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 y 2 x 3. x 2. f = ? y 1 apply(+, R 6, R 4) = ? x 2 R 4 y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf y 3 1 R 7 0 R 8 1 CMPUT 680 - Compiler Design and Optimization B x 3. f 0 1 y 3 A(R 8, R 8) 65 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 y 2 x 3. x 2. f = ? y 1 apply(+, R 6, R 4) = ? x 2 R 4 y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf y 3 1 R 7 0 R 8 1 CMPUT 680 - Compiler Design and Optimization B x 3. f 0 y 3 1 66 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 y 2 x 3. x 2. f = ? y 1 apply(+, R 6, R 4) = ? x 2 R 4 y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf y 3 1 R 7 0 R 8 1 CMPUT 680 - Compiler Design and Optimization B x 3. f 0 1 67 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 y 1 x 3. x 2. f x 2 R 4 R 5 R 1 y 2 R 4 y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf 1 R 7 0 CMPUT 680 - Compiler Design and Optimization R 8 1 68 Huth-Ryan, pp. 379
The exists algorithm (example) R 1 x 1 R 2 R 3 x 1 y 1 R 2 x 2 R 4 R 5 R 1 R 3 R 1 x 1 y 1 R 2 y 1 x 2 y 2 R 4 y 2 x 3 R 6 y 3 R 7 0 R 6 y 3 R 8 Bf 1 R 7 0 R 6 y 3 R 8 1 CMPUT 680 - Compiler Design and x 3. f Optimization B R 7 0 R 8 B x 2. x 3. f 1 69 Huth-Ryan, pp. 379
Translation of Boolean formulas into OBDDs Formula f OBDD Bf 0 B 0 1 B 1 x Bx f Swap 0 - and 1 -nodes in Bf f+g apply(+, Bf, Bg) f g apply( , Bf, Bg) f[1/x] restrict(1, x, Bf) f[0/x] restrict(0, x, Bf) x. f apply(+, Bf[0/x], Bf[1/x]), x. f apply( , Bf[0/x], Bf[1/x]), CMPUT 680 - Compiler Design and Optimization 70 Huth-Ryan, pp. 380
Upper bounds for running times Algorithm Input OBDD(s) Output OBDD Timecomplexity reduce B reduced B O(|B|. log|B|) apply Bf , Bg (reduced) Bf op g (reduced) O(|Bf|. |Bg|) restrict Bf (reduced) Bf[0/x] or Bf[1/x] (reduced) Bf (reduced) B x 1. x 2… xn. f (reduced) CMPUT 680 - Compiler Design and Optimization O(|Bf|. log|Bf|) NP-complete 71 Huth-Ryan, pp. 380
Relational Product z Given two functions f(x, …) and g(x, . . . ), does a value of x exist that makes the conjunction of f and g true? x. (f • g) = (f • g)[0/x] + (f • g)[1/x] z This combination of the existence quantifier with conjunction is called the relational product of f and g. z The relational product can be defined in terms of apply and restrict: Bf • g = apply ( • , Bf, Bg) apply(+, restrict(0, x, Bf • g), restrict(1, x, Bf • g)) z But computing the relational product this way would be too expensive. CMPUT 680 - Compiler Design and Optimization 72
A relational product (example) R 1 R 2 x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 (R 4, S 3) S 5 1 (R 5, S 4) CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] 73 Huth-Ryan, pp. 375
A relational product (example) 0: T 1 R 2 x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 (R 4, S 3) S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] (R 6, S 5) 74 Huth-Ryan, pp. 375
A relational product (example) 0: T 1 1: T 2 A( • , R 4, S 3): ? R 1 R 2 x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 (R 4, S 3) S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] T 2 1 75 Huth-Ryan, pp. 375
0: T 1 1: T 2 A( • , R 4, S 3): T 3 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 (R 3, S 3) x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 76 Huth-Ryan, pp. 375
0: T 1 1: T 2 A( • , R 4, S 3): T 3 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 (R 3, S 3) x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 (R 4, S 3) 1 77 Huth-Ryan, pp. 375
0: T 1 1: T 2 A( • , R 4, S 3): T 3 A( • , R 6, S 3): ? R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 (R 3, S 3) x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] (R 6, S 3) x 4 T 2 1 78 Huth-Ryan, pp. 375
0: T 1 1: T 2 A( • , R 4, S 3): T 3 A( • , R 6, S 3): ? R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 (R 3, S 3) x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] (R 6, S 3) x 4 T 2 1 R 6 is a terminal node (B 1) thus (R 6, S 3) results in S 3, which already exists in the BDD. 79 Huth-Ryan, pp. 375
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): ? R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 (R 3, S 3) x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 80 Huth-Ryan, pp. 375
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): ? R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 2, S 3) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 81 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 82 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 3, S 2) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 83 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 3, S 2) x 2 R 3 R 4 x 1 x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 (R 4, S 3) x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 84 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 x 1 A( • , R 6, S 5): ? (R 1, S 1) (R 3, S 2) x 2 R 3 x 3 S 2 x 3 (R 6, S 5) R 4 R 5 x 4 0 S 3 R 6 1 S 4 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 85 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) (R 3, S 2) x 2 R 3 R 4 x 1 x 3 x 4 0 A( • , R 6, S 5): T 2 A( • , R 3, S 2): ? S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 86 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) x 3 x 2 R 3 R 4 x 1 x 3 x 4 0 A( • , R 6, S 5): T 2 A( • , R 3, S 2): ? S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 X 3 is a variable in the relational product, thus we must apply the existence quantifier to it. 87 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) A(+, T 3, T 2) x 2 R 3 R 4 x 1 x 3 x 4 0 A( • , R 6, S 5): T 2 A( • , R 3, S 2): ? A(+, T 3, T 2): ? S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 88 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 (R 1, S 1) x 2 R 3 R 4 x 1 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): ? x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 89 Huth-Ryan, pp. 375
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 R 1 R 2 A relational product (example) x 1 S 1 R 5 T 4 x 1 x 2 R 3 R 4 x 1 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): ? x 3 x 4 0 S 2 S 3 R 6 1 S 4 x 3 x 4 0 T 3 S 5 1 T 1 0 CMPUT 680 - Compiler Design and Optimization x 3. (f • g) = (f • g)[0/x 3] + (f • g)[1/x 3] x 4 T 2 1 90 Huth-Ryan, pp. 375
BDD packages z BDDs are becoming popular for several applications thanks to the existence of efficient BDD packages : y. Bu. DDy: A Binary Decision Diagram Package by JØrn Lind-Nielsen from Technical University of Denmark xhttp: //www. itu. dk/research/buddy y. CUDD: CU Decision diagram package, by Fabio Somenzi from University of Colorado at Boulder xhttp: //vlsi. colorado. edu/~fabio/CUDD y. Jedd: A BDD-based relational extension of Java by Ondrej Lhoták from Mc. Gill University xhttp: //www. sable. mcgill. ca/jedd CMPUT 680 - Compiler Design and Optimization 91
Representing Subsets with OBDDs z Let S be a finite set. z Assign a unique Boolean vector (v 1, v 2, …, vn) to each element s S: y 2 n-1 < |S| 2 n yvi {0, 1} z A subset T S is represented by a Boolean function f. T. z f. T maps onto 1 if s T, and maps it onto 0 otherwise. CMPUT 680 - Compiler Design and Optimization 92 Huth-Ryan, pp. 383
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 This program segment has: z three allocation statements (objects): L 1, L 2 and L 3; z three pointers: a, b, and c; z We can encode the pointers with two bits; z We can encode the objects with two bits; CMPUT 680 - Compiler Design and Optimization 93 Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; z We 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)} Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0 1110 x x 1111 x 0110 CMPUT 680 - Compiler Design and Optimization 0111 After Berndl-Lhotak et al. , PLDI 03 94
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x x 1111 x 0111 Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 2 v 3 v 3 v 4 v 4 CMPUT 680 - Compiler Design and Optimization 1 0 95 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x x 1111 x 0111 Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 2 v 3 v 3 v 4 v 4 CMPUT 680 - Compiler Design and Optimization 1 0 96 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x x 1111 x 0111 Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 2 v 3 v 3 v 4 v 4 v 4 CMPUT 680 - Compiler Design and Optimization 1 0 97 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x x 1111 x 0111 Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 2 v 3 v 4 CMPUT 680 - Compiler Design and Optimization 1 v 3 v 4 0 98 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x 0111 x 1111 x Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 2 v 3 CMPUT 680 - Compiler Design and Optimization v 3 1 v 3 0 99 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x 0111 x 1111 x Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 2 v 3 CMPUT 680 - Compiler Design and Optimization v 3 1 0 100 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x 0111 x 1111 x Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 2 v 3 CMPUT 680 - Compiler Design and Optimization 1 0 101 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; Code v 1 v 2 v 3 v 4 f. T 0000 1 1000 1 0001 1 1001 1 0010 0 1010 1 0011 x 1011 x 0100 1 1100 x 0101 1 1101 x 0110 0 1110 x 0111 x 1111 x Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 10 L 3 10 v 1 v 3 CMPUT 680 - Compiler Design and Optimization 1 0 102 After Berndl-Lhotak et al. , PLDI 03
Using OBDDs to represent points-to relations v 1 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. v 2 v 3 v 4 In their PLDI 2003 paper, Berndl/Lhotak et al assign 0 to the unused bit patterns and obtain the OBDD on the right. 1 v 3 0 1 CMPUT 680 - Compiler Design and Optimization 0 103 After Berndl-Lhotak et al. , PLDI 03
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 f. T 0000 1 0100 1 1000 1 1100 1 0001 0 0101 1 1001 x 1101 x 0010 1 0110 x 1010 1 1110 x 0011 0 0111 x x 1111 x 1011 Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 02 L 3 02 v 4 v 2 v 3 v 3 v 1 v 1 CMPUT 680 - Compiler Design and Optimization 1 0 104 After Berndl-Lhotak et al. , PLDI 03
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 f. T 0000 1 0100 1 1000 1 1100 1 0001 0 0101 1 1001 x 1101 x 0010 1 0110 x 1010 1 1110 x 0011 0 0111 x x 1111 x 1011 Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 02 L 3 02 v 4 v 2 v 3 v 3 v 1 v 1 CMPUT 680 - Compiler Design and Optimization 1 0 105 After Berndl-Lhotak et al. , PLDI 03
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 f. T 0000 1 0100 1 1000 1 1100 1 0001 0 0101 1 1001 x 1101 x 0010 1 0110 x 1010 1 1110 x 0011 0 0111 x x 1111 x 1011 Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 02 L 3 02 v 4 v 2 v 3 v 1 CMPUT 680 - Compiler Design and Optimization 1 0 106 After Berndl-Lhotak et al. , PLDI 03
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 f. T 0000 1 0100 1 1000 1 1100 1 0001 0 0101 1 1001 x 1101 x 0010 1 0110 x 1010 1 1110 x 0011 0 0111 x 1011 x 1111 x Pointer Code Object Code a 00 L 1 00 b 01 L 2 01 c 02 L 3 02 v 4 v 2 v 3 v 1 0 CMPUT 680 - Compiler Design and Optimization 1 107 After Berndl-Lhotak et al. , PLDI 03
Effect of bit ordering v 4 With the bit ordering we obtained the OBDD on the left when using don’t care values. v 2 v 3 In their PLDI 2003 paper, Berndl/Lhotak et al obtain the OBDD on the right (they do not use don’t care values). v 1 0 CMPUT 680 - Compiler Design and Optimization v 3 v 1 1 1 v 2 0 108 After Berndl-Lhotak et al. , PLDI 03
Effect of bit ordering and use of don’t cares v 4 v 1 v 3 v 3 0 v 3 v 4 0 1 1 CMPUT 680 - Compiler Design and Optimization 0 v 3 v 1 v 1 1 v 2 v 2 v 3 v 4 1 0 109 After Berndl-Lhotak et al. , PLDI 03
Relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; z. Reference analysis can be defined in terms of relations. z. A domain is a set of elements. y. To 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} CMPUT 680 - Compiler Design and Optimization 110 After Berndl-Lhotak et al. , PLDI 03
Relations z. An attribute is a domain along with an associated name. L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; y. To 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} CMPUT 680 - Compiler Design and Optimization 111 After Berndl-Lhotak et al. , PLDI 03
Relations L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; z. A tuple is a list of elements indexed by attributes. z. Examples of tuples include: pointer object b L 2 source a destination b CMPUT 680 - Compiler Design and Optimization 112 After Berndl-Lhotak et al. , PLDI 03
Relations z. A relation is a collection of tuples that share the same attributes. L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; y. Examples of relations for this code: pointer object a L 1 b L 2 c L 3 source destination b a a b b c Initial points-to pair. CMPUT 680 - Compiler Design and Optimization Assignments. 113 After Berndl-Lhotak et al. , PLDI 03
Point-to Algorithm z. The goal is to compute a point-to relation between variables of pointer time and allocation sites (or abstract objects). pointer object a L 1 ? ? b L 2 • • • c L 3 ? ? Initial points-to pair. CMPUT 680 - Compiler Design and Optimization 114 After Berndl-Lhotak et al. , PLDI 03
Point-to Algorithm z The context-insensitive, flow-insensitive algorithm repeatedly applies the program’s constraints to the initial point-to relation defined by the allocation sites. pointer object a L 1 ? ? b L 2 • • • c L 3 ? ? constraints Initial points-to pairs. Final points-to pairs. CMPUT 680 - Compiler Design and Optimization 115 After Berndl-Lhotak et al. , PLDI 03
Interference rules Simple assignment: if l 1 points to o, and l 1 is assigned to l 2, then l 2 also points to o. Field store: 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 load: if l is loaded from p. f, and p points to o 1, then for each o 2 pointed to by o 1. f, CMPUT 680 - Compiler Design to o. l points 2 and Optimization 116 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; z. The general algorithm uses five attributes, but this examples only needs three attributes: x. V = {a, b, c} x. W = {a, b, c} x. H = {L 1, L 2, L 3} CMPUT 680 - Compiler Design and Optimization Variables Heap Locations 117 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) z The 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; Code VH initial pt 0000 1 1000 0 0001 0 1001 0 V H a L 1 0010 0 1010 1 b L 2 0011 x 1011 x c L 3 0100 0 1100 x 0101 1 1101 x V Code H Code 0110 0 1110 x a 00 L 1 00 0111 x 1111 x b 01 L 2 01 c 01 L 3 01 CMPUT 680 - Compiler Design and Optimization 118 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) Code VH initial pt 0000 1 1000 0 0001 0 1001 0 0010 0 1010 0011 x 0100 Note: Don’t cares have been assigned so nodes at the same level merge. INITIAL POINTS-TO V 0 What have the don’t cares been assigned to? V 1 1 Code VH initial pt 1011 x 0 1100 x 0111 x 0101 1 1101 x 1011 x 0110 0 1110 x 1100 x 0111 x 1101 x 1110 x 1111 x H 0 H 1 0 1 CMPUT 680 - Compiler Design and Optimization 119 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) Code VH initial pt 0000 1 1000 0 0001 0 1001 0 0010 0 1010 0011 x 0100 Note: Don’t cares have been assigned so nodes at the same level merge. INITIAL POINTS-TO V 0 What have the don’t cares been assigned to? V 1 1 Code VH initial pt 1011 x 0011 0 0 1100 x 0111 x 0101 1 1101 x 1011 x 0110 0 1110 x 1100 x 0111 x 1101 x 1110 x 1111 x H 0 H 1 0 1 CMPUT 680 - Compiler Design and Optimization 120 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) Code VH initial pt 0000 1 1000 0 0001 0 1001 0 0010 0 1010 0011 x 0100 Note: Don’t cares have been assigned so nodes at the same level merge. INITIAL POINTS-TO V 0 What have the don’t cares been assigned to? V 1 1 Code VH initial pt 1011 x 0011 0 0 1100 x 0111 1 0101 1 1101 x 1011 x 0110 0 1110 x 1100 x 0111 x 1101 x 1110 x 1111 x H 0 H 1 0 1 CMPUT 680 - Compiler Design and Optimization 121 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) Code VH initial pt 0000 1 1000 0 0001 0 1001 0 0010 0 1010 0011 x 0100 Note: Don’t cares have been assigned so nodes at the same level merge. INITIAL POINTS-TO V 0 What have the don’t cares been assigned to? V 1 1 Code VH initial pt 1011 x 0011 0 0 1100 x 0111 1 0101 1 1101 x 1011 1 0110 0 1110 x 1100 0 0111 x 1101 0 1110 1 1111 1 H 0 H 1 0 1 CMPUT 680 - Compiler Design and Optimization 122 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) z The 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; Code VW edgeset 0000 0 1000 0 0001 1 1001 0 V W b a 0010 0 1010 0 a b 0011 x 1011 x b c 0100 1 1100 x 0101 0 1101 x V Code W Code 0110 1 1110 x a 00 0111 x 1111 x b 01 c 01 CMPUT 680 - Compiler Design and Optimization 123 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) EDGE-SET Code VW edgeset 0000 0 1000 0 0001 1 1001 0 0010 0 1010 0 0011 x 1011 x 0100 1 1100 x 0101 0 1101 x 0110 1 1110 x 0111 x 1111 x V 0 V 1 W 1 CMPUT 680 - Compiler Design and Optimization W 1 1 0 124 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) INITIAL POINTS-TO EDGE-SET W 1 V 1 V 1 H 1 AFTER REPLACE(W to V) V 0 H 0 AFTER RELPROD(Init, Edge, V) W 1 H 0 H 0 RELPROD(V) H 1 H 1 H 1 0 0 1 1 1 0 CMPUT 680 - Compiler Design and Optimization 126 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) AFTER REPLACE(W to V) V: Variables (a = 00, b = 01, c = 10) H : Heap Objects (L 1 = 00, L 2 = 01, L 3 = 10) V 1 L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; H 0 H 1 0 What is the domain of this relation? H 1 1 What elements are in this relation? {(a, L 2), (b, L 1), (c, L 2)} What does this relation represent? It represents propagation of the points-to relation caused by the assignments. CMPUT 680 - Compiler Design and Optimization 127 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) INITIAL POINTS-TO After 1 st Propagation POINTS-TO DUE TO EDGE-SET V 0 V 1 V 1 H 0 H 1 H 1 0 H 0 1 0 UNION H 1 H 1 1 CMPUT 680 - Compiler Design and Optimization H 0 1 0 128 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) POINTS-TO After 1 st Propagation Elements in the Points-to set: {(a, L 1), (a, L 2), , (b, L 1), (b, L 2), (c, L 3)} V 0 V 1 H 0 H 1 V 1 H 0 H 0 H 1 0000 (a, L 1) 0001 (a, L 2) 0100 (b, L 1) 0101 (b, L 2) 1001 (c, L 2) 1010 (c, L 3) L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; What’s Missing? 1 0 -Points-to relations caused by more than 1 propagation: {(a, L 1)} CMPUT 680 - Compiler Design and Optimization 129 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) POINTS-TO After 1 st Propagation POINTS-TO After 2 nd Propagation POINTS-TO DUE TO EDGE-SET V 0 V 1 H 0 H 1 H 1 1 UNION 0 0 H 1 1 CMPUT 680 - Compiler Design and Optimization H 0 0 1 130 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) POINTS-TO After 2 nd Propagation POINTS-TO After 3 nd Propagation (Unchanged) POINTS-TO DUE TO EDGE-SET V 0 V 1 V 1 H 0 H 0 UNION H 1 0 1 0 H 1 1 CMPUT 680 - Compiler Design and Optimization H 0 0 1 131 After Berndl-Lhotak et al. , PLDI 03
Domains, Relations, and BDDs (example) Final Points-to Set V 0 V 1 H 0 H 1 0 1 (a, L 1) 0000 1 (a, L 2) 0001 1 (b, L 1) 0100 1 (b, L 2) 0101 1 (c, L 2) 1001 1 (c, L 3) 1010 1 (c, L 1) 1000 1 (a, L 3) 0010 0 (b, L 3) 0110 0 CMPUT 680 - Compiler Design and Optimization L 1: a = new O(); L 2: b = new O(); L 3: c = new O(); a = b; b = a; c = b; 132 After Berndl-Lhotak et al. , PLDI 03
Why Use BDDs for Program Analysis? q Space q. Large analysis has only been accomplished when BDDs are used to represent the points-to set. q. Other efficient representations use 4 x to 6 x the space required by BDDs. q Time q. For small analysis BDDs are slower (~1. 5 to 2 x). q. For large analysis BDDs are significantly faster because of the reduced memory usage. CMPUT 680 - Compiler Design and Optimization 133 After Berndl-Lhotak et al. , Tech Report 2003
- Slides: 132