1.32k likes | 1.41k Views
CMPUT680 - Winter 2006. Topic Q: Binary Decision Diagrams José Nelson Amaral http://www.cs.ualberta.ca/~amaral/courses/680. Reading Material.
E N D
CMPUT680 - Winter 2006 Topic Q: Binary Decision Diagrams José Nelson Amaral http://www.cs.ualberta.ca/~amaral/courses/680 CMPUT 680 - Compiler Design and Optimization
Reading Material • Michael Huth and Mark Ryan, Logic in Computer Science: Modelling and Reasoning about Systems, 2nd Edition, Cambridge University Press, 2005 (Chapter 6). CMPUT 680 - Compiler Design and Optimization
f(x,y) = x+y x y y 0 0 1 0 Binary Decision Tree Boolean Function Binary Decision Tree Truth Table CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 361
x y y y 0 0 0 1 0 1 Binary Decision Tree Binary Decision Diagram x Removal of duplicate terminals. y Binary Decision Tree Binary Decision Diagram CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 362
x y y y 0 0 1 1 Binary Decision Tree Binary Decision Diagram x Removal of redundant tests. Binary Decision Diagram Binary Decision Diagram CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 362
z z x x x x y y y y y y y 1 1 0 0 Removal of duplicate non-terminals CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 363
z x x y y y y 1 1 0 0 Removal of duplicate non-terminals z x x y CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 363
z x y y 1 1 0 0 Removal of redundant decision point z x x y y CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 363
Reduced BDDs • We saw three optimizations that can be performed in BDDs: • C1: Removal of duplicate terminals • C2: Removal of redundant tests • C3: Removal of duplicate non-terminals • If none of these optimizations can be applied to a BDD, the BDD is reduced. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 365
Boolean Operations on BDDs • Given two BDDs Bf and Bg representing boolean functions f and g, how do we obtain a BDD for the following functions: • f • f + g • f · g CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 365
Ordered BDDs • Let L = [x1, … xn] be an ordered list of variables without duplications. • A BDD B is ordered for L if • all variable labels of B occur in L; and • for every occurrence of xi followed by xj along a path in B, i < j. • An ordered BDD (OBDD) is a BDD that is ordered according to some list of variables. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 367
1 0 Example of a BDD that is not ordered z y x x y CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 368
OBDDs • There cannot be multiple occurrences of any variable along a path in an OBDD. • When operations are performed on two OBDDs, they must have compatible variable ordering. • The reduced OBDD representing a given function fis unique OBDDs have a canonical form. • We can apply C1-C3 in any order. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 368
x3 x4 x3 x4 0 1 OBDDs can be compact x1 x2 x2 The function feven(x1, x2, …, xn) which is 1 if there is an even number of inputs with value 1 has an OBDD representation with only 2n+1 nodes. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 370
Exercise Build a reduced OBDD for the following boolean function: f(x1, x2, …, xn) = (x1+x2).(x3 + x4)….(x2n-1 + x2n); Try to select a variable ordering that minimizes the number of nodes in the OBDD. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 371
x1 x3 x3 x5 x5 x5 x5 x2 x4 x2 x4 x2 x2 x6 1 0 Impact of Variable Ordering x1 x2 x3 x4 x5 x6 0 1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 371
Importance of Canonical Representation • Absence of redundant variables: • If f(x1, x2, …, xn) does not depend on xi, then any reduced OBDD of f does not contain xi. • Test for semantic equivalence: • If f(x1, x2, …, xn) and g(x1, x2, …, xn) are represented by OBDDs Bf and Bg, then to find if f and g are equivalent, reduce Bf and Bgand check if they have identical structures. • Text for validity: • f(x1, x2, …, xn) is valid (always computes 1) if its reduced OBDD is B1. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 370
Importance of Canonical Representation • Test for implication: • f(x1, x2, …, xn) implies g(x1, x2, …, xn) if the OBDD for f . /g is B0. • Test for satisfiability: • f(x1, x2, …, xn) is satisfiable (computes 1 for at list one input) if its reduced OBDD is not B0. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 372
The reduce algorithm • Step 1: Label each node in the OBDD • two nodes receive the same id if and only if they represent the same function. • Step 2: Traverse the OBDD bottom up • collapse all nodes with the same label onto a single node and redirect the edges. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 372
The reduce algorithm • Step 1:Label • Initialization: Assign (at bottom of BDD): • label #0 to all 0-nodes; • label #1 to all 1-nodes; • Let: • lo(n): node pointed by the dashed line from node n • hi(n): node pointed by the solid line. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 372
The reduce algorithm • Step 1:Label • Rules to label an internal xi-node n : • ifid(lo(n)) = id(hi(n))then id(n) = id(lo(n)); • else if there is another node m such that: • m has variable xiand • id(lo(n)) = id(lo(m))and • id(hi(n)) = id(hi(m)) • then id(n) = id(m) • elseid(n) = next unused integer. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 373
The reduce algorithm(example) Rules to label an internal xi-node n : • ifid(lo(n)) = id(hi(n))then id(n) = id(lo(n)); • else if there is another node m such that: • m has variable xiand • id(lo(n)) = id(lo(m))and • id(hi(n)) = id(hi(m)) • then id(n) = id(m) • elseid(n) = next unused integer. x1 x2 x2 x3 x3 x3 x3 ? ? ? ? 0 1 0 1 #0 #1 #0 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 373
The reduce algorithm(example) Rules to label an internal xi-node n : • ifid(lo(n)) = id(hi(n))then id(n) = id(lo(n)); • else if there is another node m such that: • m has variable xiand • id(lo(n)) = id(lo(m))and • id(hi(n)) = id(hi(m)) • then id(n) = id(m) • elseid(n) = next unused integer. x1 x2 x2 ? ? x3 x3 x3 x3 #2 #2 #2 #3 0 1 0 1 #0 #1 #0 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 373
The reduce algorithm(example) Rules to label an internal xi-node n : • ifid(lo(n)) = id(hi(n))then id(n) = id(lo(n)); • else if there is another node m such that: • m has variable xiand • id(lo(n)) = id(hi(m))and • id(hi(n)) = id(hi(m)) • then id(n) = id(m) • elseid(n) = next unused integer. x1 ? x2 x2 #2 #4 x3 x3 x3 x3 #2 #2 #2 #3 0 1 0 1 #0 #1 #0 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 373
The reduce algorithm(example) Rules to label an internal xi-node n : • ifid(lo(n)) = id(hi(n))then id(n) = id(lo(n)); • else if there is another node m such that: • m has variable xiand • id(lo(n)) = id(lo(m))and • id(hi(n)) = id(hi(m)) • then id(n) = id(m) • elseid(n) = next unused integer. x1 #5 x2 x2 #2 #4 x3 x3 x3 x3 #2 #2 #2 #3 0 1 0 1 #0 #1 #0 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 373
x1 x2 x3 x3 1 0 The reduce algorithm(example) #5 x1 #5 x2 x2 #2 #4 #4 x3 x3 x3 x3 #2 #3 #2 #2 #2 #3 0 1 0 1 #0 #1 #0 #1 #1 #0 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 373
The apply algorithm • Implement operations on boolean functions such as +, , , and complement. • Let f and g be represented by OBDDs Bf and Bg. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 373
The apply algorithm • Let v be the highest variable in Bf or Bg. • Split the problem into two sub-problems, for v = 0 and for v = 1, and solve recursively. • At the leaves, apply the Boolean operation directly. CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 374
The apply algorithm • f[0/x] and f[1/x] are restrictions of f. • f[0/x] is obtained by replacing x in f by 0. • For all functions f and all variables x: f x f[0/x] + x f[1/x] CMPUT 680 - Compiler Design and Optimization 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 Bfand Bg be the OBDDs of f and g, we want to compute Bf op g Let rfand rg be the root node of Bf and Bg CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 374
The apply algorithm • ifrf and rg are terminals with labels lf and lg, then compute lfop lg (the result is B0 or B1). • ifrf and rg are xi-nodes, then create an xi-node n with • a dashed line to apply(op, lo(rf), lo(rg)) • a solid line to apply(op, hi(rf), hi(rg)) • ifrf is an xi-nodes and (rg is a terminal node or (rj is an xj-node andj>i)) then create an xi-node n with: • A dashed line to apply(op, lo(rf), rg) • A solid line to apply(op, hi(rf), rg) CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 374
A(R1,S1) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 S1 A(R1,S1) R2 + R3 S2 R4 S3 R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 R2 A(R2,S3) A(R3,S2) + R3 S2 R4 S3 R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 A(R4,S3) A(R3,S3) R4 S3 R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R5,S4) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 x4 A(R3,S3) T3 R4 S3 A(R5,S4) A(R6,S5) R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R6,S5) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 x4 A(R3,S3) T3 R4 S3 0 T4 A(R6,S5) R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 x4 A(R3,S3) T3 R4 S3 0 1 T4 T5 R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R4,S3) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 T6 x4 x3 T3 R4 S3 0 1 T4 T5 A(R4,S3) A(R6,S3) R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 T6 x4 x3 T3 R4 S3 0 1 T4 T5 A(R6,S3) R5 R6 S4 S5 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R6,S4) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 T6 x4 x3 T3 R4 S3 0 1 x4 T4 T5 T7 R5 R6 S4 S5 A(R6,S4) A(R6,S5) CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R6,S5) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 T6 x4 x3 T3 R4 S3 0 1 x4 T4 T5 T7 R5 R6 S4 S5 A(R6,S5) 1 T8 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x2 R2 T2 A(R3,S2) + R3 S2 T6 x4 x3 T3 R4 S3 0 1 x4 T4 T5 T7 R5 R6 S4 S5 1 1 T8 T9 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 A(R4,S3) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 T10 R2 T2 + A(R4,S3) A(R6,S5) R3 S2 T6 x4 x3 T3 R4 S3 0 1 x4 T4 T5 T7 R5 R6 S4 S5 1 1 T8 T9 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 A(R6,S5) = ? x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 T10 R2 T2 + A(R6,S5) R3 S2 T6 x4 x3 T3 R4 S3 0 1 x4 T4 T5 T7 R5 R6 S4 S5 1 1 T8 T9 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 T10 R2 T2 + 1 #1 R3 S2 T6 x4 x3 T3 R4 S3 0 1 x4 T4 T5 T7 R5 R6 S4 S5 1 1 T8 T9 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 T10 R2 T2 + 1 #1 R3 S2 T6 x4 x3 T3 R4 S3 0 1 x4 #1 #0 ? T7 R5 R6 S4 S5 1 1 #1 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 T10 R2 T2 + 1 #1 R3 S2 T6 ? x4 x3 T3 R4 S3 0 1 x4 #1 #0 #1 R5 R6 S4 S5 1 1 #1 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 T10 R2 T2 + 1 #1 R3 S2 T6 x4 x3 ? #2 R4 S3 0 1 x4 #1 #0 #1 R5 R6 S4 S5 1 1 #1 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 T10 R2 ? T2 + 1 #1 R3 S2 #3 x4 x3 #2 R4 S3 0 1 x4 #1 #0 #1 R5 R6 S4 S5 1 1 #1 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375
A(R1,S1) = T1 A(R2,S3) = T2 A(R4,S3) = T3 A(R3,S3) = T6 A(R6,S3) = T7 A(R3,S2) = T10 x1 x1 x2 x3 x3 x4 x4 1 1 0 0 The apply algorithm(example) R1 x1 S1 T1 x3 x2 ? T10 R2 #4 + 1 #1 R3 S2 #3 x4 x3 #2 R4 S3 0 1 x4 #1 #0 #1 R5 R6 S4 S5 1 1 #1 #1 CMPUT 680 - Compiler Design and Optimization Huth-Ryan, pp. 375