CMPUT 680 - Compiler Design and Optimization
1
CMPUT680 - Winter 2006
Topic Q: Binary Decision DiagramsJosé Nelson Amaral
http://www.cs.ualberta.ca/~amaral/courses/680
CMPUT 680 - Compiler Design and Optimization
2
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
3
Binary Decision Tree
x
y
1 0
y
0 0
f(x,y) = x+y
x y f
0 0 1
0 1 0
1 0 0
1 1 0
Boolean Function
Truth Table Binary Decision Tree
Huth-Ryan, pp. 361
CMPUT 680 - Compiler Design and Optimization
4
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
CMPUT 680 - Compiler Design and Optimization
5
Binary Decision Tree Binary Decision Diagram
x
y
1 0
y
Binary Decision Diagram
x
y
1 0
Removal of redundanttests.
Binary Decision Diagram
Huth-Ryan, pp. 362
CMPUT 680 - Compiler Design and Optimization
6
Removal of duplicate non-terminals
z
0 1
x
y y
x
y y
z
0 1
x
y y
x
y
Huth-Ryan, pp. 363
CMPUT 680 - Compiler Design and Optimization
7
Removal of duplicate non-terminals
z
0 1
x
y y
x
y
z
0 1
x
y
x
y
Huth-Ryan, pp. 363
CMPUT 680 - Compiler Design and Optimization
8
Removal of redundant decision point
z
0 1
x
y
x
y
z
0 1
y
x
y
Huth-Ryan, pp. 363
CMPUT 680 - Compiler Design and Optimization
9
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.
Huth-Ryan, pp. 365
CMPUT 680 - Compiler Design and Optimization
10
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
Huth-Ryan, pp. 365
CMPUT 680 - Compiler Design and Optimization
11
Ordered BDDs
Let L = [x1, … xn] be an ordered list of variables without duplications. A BDD B is ordered for L if
x all variable labels of B occur in L; and x for every occurrence of xi followed by xj
along a path in B, i < j.
1. An ordered BDD (OBDD) is a BDD that is ordered according to some list of variables.
Huth-Ryan, pp. 367
CMPUT 680 - Compiler Design and Optimization
12
Example of a BDD that is not ordered
z
0 1
y
x
x
y
Huth-Ryan, pp. 368
CMPUT 680 - Compiler Design and Optimization
13
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 f is unique OBDDs have a canonical form. We can apply C1-C3 in any order.
Huth-Ryan, pp. 368
CMPUT 680 - Compiler Design and Optimization
14
OBDDs can be compact
x2 x2
x3 x3
x4 x4
1 0
x1
The function feven(x1, x2, …, xn)which is 1 if there is an even numberof inputs with value 1 has an OBDDrepresentation with only 2n+1 nodes.
Huth-Ryan, pp. 370
CMPUT 680 - Compiler Design and Optimization
15
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 thenumber of nodes in the OBDD.
Huth-Ryan, pp. 371
CMPUT 680 - Compiler Design and Optimization
16
Impact of Variable Ordering
x2 x2 x2 x2
x5 x5
x3
x4 x4
x6
x1
x5 x5
x3
1 0
x1
x3
x5
x2
x4
x6
0 1
Huth-Ryan, pp. 371
CMPUT 680 - Compiler Design and Optimization
17
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 Bg and check if they have identical structures.
Text for validity: f(x1, x2, …, xn) is valid (always computes 1) if its
reduced OBDD is B1.
Huth-Ryan, pp. 370
CMPUT 680 - Compiler Design and Optimization
18
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.
Huth-Ryan, pp. 372
CMPUT 680 - Compiler Design and Optimization
19
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.
Huth-Ryan, pp. 372
CMPUT 680 - Compiler Design and Optimization
20
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.
Huth-Ryan, pp. 372
CMPUT 680 - Compiler Design and Optimization
21
The reduce algorithm
Step 1: Label Rules to label an internal xi-node n :
if id(lo(n)) = id(hi(n)) then id(n) = id(lo(n));else 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))
then id(n) = id(m) else id(n) = next unused integer.
Huth-Ryan, pp. 373
CMPUT 680 - Compiler Design and Optimization
22
The reduce algorithm(example)
x1
0 1
x2
x3 x3
x2
x3 x3
0 1#1 #1#0#0
? ???
Rules to label an internal xi-node n : if id(lo(n)) = id(hi(n)) then
id(n) = id(lo(n));else 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))
then id(n) = id(m) else id(n) = next unused
integer.
Huth-Ryan, pp. 373
CMPUT 680 - Compiler Design and Optimization
23
The reduce algorithm(example)
x1
0 1
x2
x3 x3
x2
x3 x3
0 1#1 #1#0#0
#2 #3#2#2
? ?
Rules to label an internal xi-node n : if id(lo(n)) = id(hi(n)) then
id(n) = id(lo(n));else 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))
then id(n) = id(m) else id(n) = next unused
integer.
Huth-Ryan, pp. 373
CMPUT 680 - Compiler Design and Optimization
24
The reduce algorithm(example)
x1
0 1
x2
x3 x3
x2
x3 x3
0 1#1 #1#0#0
#2 #3#2#2
#2 #4
?Rules to label an internal
xi-node n : if id(lo(n)) = id(hi(n)) then
id(n) = id(lo(n));else if there is another
node m such that:• m has variable xi and
• id(lo(n)) = id(hi(m)) and • id(hi(n)) = id(hi(m))
then id(n) = id(m) else id(n) = next unused
integer.
Huth-Ryan, pp. 373
CMPUT 680 - Compiler Design and Optimization
25
The reduce algorithm(example)
x1
0 1
x2
x3 x3
x2
x3 x3
0 1#1 #1#0#0
#2 #3#2#2
#2 #4
#5Rules to label an internal
xi-node n : if id(lo(n)) = id(hi(n)) then
id(n) = id(lo(n));else 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))
then id(n) = id(m) else id(n) = next unused
integer.
Huth-Ryan, pp. 373
CMPUT 680 - Compiler Design and Optimization
26
The reduce algorithm(example)
x1
0 1
x2
x3 x3
x2
x3 x3
0 1#1 #1#0#0
#2 #3#2#2
#2 #4
#5 x1
0 1
x3
x2
x3
#0 #1
#3#2
#4
#5
Huth-Ryan, pp. 373
CMPUT 680 - Compiler Design and Optimization
27
The apply algorithm
Implement operations on boolean functions such as +, , , and complement.
Let f and g be represented by OBDDs Bf and Bg.
Huth-Ryan, pp. 373
CMPUT 680 - Compiler Design and Optimization
28
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.
Huth-Ryan, pp. 374
CMPUT 680 - Compiler Design and Optimization
29
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]
Huth-Ryan, pp. 374
CMPUT 680 - Compiler Design and Optimization
30
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
Huth-Ryan, pp. 374
CMPUT 680 - Compiler Design and Optimization
31
The apply algorithm
if rf and rg are terminals with labels lf and lg, then compute lf op lg (the result is B0 or B1).
if rf 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))
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: A dashed line to apply(op, lo(rf), rg) A solid line to apply(op, hi(rf), rg)
Huth-Ryan, pp. 374
CMPUT 680 - Compiler Design and Optimization
32
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
A(R1,S1)
A(R1,S1) = ?
CMPUT 680 - Compiler Design and Optimization
33
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
A(R2,S3)
x1
A(R3,S2)
T1
A(R1,S1) = T1
A(R2,S3) = ?
CMPUT 680 - Compiler Design and Optimization
34
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
A(R4,S3)
x1
A(R3,S2)x2
A(R3,S3)
T1
A(R1,S1) = T1A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = ?
T2
CMPUT 680 - Compiler Design and Optimization
35
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
A(R3,S3)
A(R5,S4) A(R6,S5)
x4
T1
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R5,S4) = ?
T2
T3
CMPUT 680 - Compiler Design and Optimization
36
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
A(R3,S3)
A(R6,S5)
x4
0
T1
T2
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R6,S5) = ?
T3
T4
CMPUT 680 - Compiler Design and Optimization
37
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
A(R3,S3)x4
0 1
T1
T2
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = ?
T3
T4 T5
CMPUT 680 - Compiler Design and Optimization
38
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
x4
0 1 A(R4,S3) A(R6,S3)
x3
T1
T2
T3
T4
A(R4,S3) = ?
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5
CMPUT 680 - Compiler Design and Optimization
39
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
x4
0 1 A(R6,S3)
x3
T1
T2
T3
T4
A(R6,S3) = ?
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5
CMPUT 680 - Compiler Design and Optimization
40
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
x4
0 1
x3
T1
T2
T3
T4
A(R6,S3) = T7
A(R6,S4) = ?
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5
A(R6,S4) A(R6,S5)
x4T7
CMPUT 680 - Compiler Design and Optimization
41
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
x4
0 1
x3
T1
T2
T3
T4
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5
A(R6,S5)
x4T7
1T8
A(R6,S3) = T7
A(R6,S5) = ?
CMPUT 680 - Compiler Design and Optimization
42
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
A(R3,S2)x2
x4
0 1
x3
T1
T2
T3
T4
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5 x4T7
1T8
A(R6,S3) = T7
A(R3,S2) = ?
1T9
CMPUT 680 - Compiler Design and Optimization
43
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
T2
T3
T4
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5 x4T7
1T8
A(R6,S3) = T7
A(R3,S2) = T10
A(R4,S3) = ?
1T9
A(R4,S3) A(R6,S5)
x3T10
CMPUT 680 - Compiler Design and Optimization
44
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
T2
T3
T4
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5 x4T7
1T8 1T9
A(R6,S5)
x3T10
A(R6,S3) = T7
A(R3,S2) = T10
A(R6,S5) = ?
CMPUT 680 - Compiler Design and Optimization
45
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
T2
T3
T4
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
T5 x4T7
1T8 1T9
x3T10
A(R6,S3) = T7
A(R3,S2) = T10
1#1
CMPUT 680 - Compiler Design and Optimization
46
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
T2
T3
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
x4T7
1 1
x3T10
A(R6,S3) = T7
A(R3,S2) = T10
1
#0 #1
#1 #1
?
#1
CMPUT 680 - Compiler Design and Optimization
47
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
T2
T3
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
x4
1 1
x3T10
A(R6,S3) = T7
A(R3,S2) = T10
1
#0 #1
#1 #1
#1
?#1
CMPUT 680 - Compiler Design and Optimization
48
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
T2
T6
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
x4
1 1
x3T10
A(R6,S3) = T7
A(R3,S2) = T10
1
#0 #1
#1 #1
#1
#2 ?
#1
CMPUT 680 - Compiler Design and Optimization
49
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
T2
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
x4
1 1
x3T10
A(R6,S3) = T7
A(R3,S2) = T10
1
#0 #1
#1 #1
#1
#2 #3
?
#1
CMPUT 680 - Compiler Design and Optimization
50
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
x4
1 1
x3T10
A(R6,S3) = T7
A(R3,S2) = T10
1
#0 #1
#1 #1
#1
#2 #3
?#4
#1
CMPUT 680 - Compiler Design and Optimization
51
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
T1
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
x4
1 1
x3
A(R6,S3) = T7
A(R3,S2) = T10
1
#0 #1
#1 #1
#1
#2 #3
#4
#1
#3
?
CMPUT 680 - Compiler Design and Optimization
52
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0 1
x3
A(R1,S1) = T1
A(R2,S3) = T2
A(R4,S3) = T3
A(R3,S3) = T6
x4
1 1
x3
A(R6,S3) = T7
A(R3,S2) = T10
1
#0 #1
#1 #1
#1
#2 #3
#4
#1
#3
#5
CMPUT 680 - Compiler Design and Optimization
53
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0
1
x3
x3
#0
#1
#2 #3
#3#4
#5
CMPUT 680 - Compiler Design and Optimization
54
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
x2
x4
0
1
x3
#0
#1
#2
#3
#4
#5
CMPUT 680 - Compiler Design and Optimization
55
The apply algorithm(example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
+
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x1
0 1
x3
x2
x4
=
CMPUT 680 - Compiler Design and Optimization
56Huth-Ryan, pp. 377
The restrict algorithm(example)
Restrict(0, x, Bf) computes the OBDD for f[0/x] with the variable ordering of Bf. For each node n labelled with x:
Redirect incoming edges to lo(n);Remove n;Call reduce on the resulting OBDD;
Restrict(1, x, Bf) is analogous, but incoming edges are redirected to hi(n).
CMPUT 680 - Compiler Design and Optimization
57
The exists quantifier
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]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]
Huth-Ryan, pp. 377
CMPUT 680 - Compiler Design and Optimization
58
The exists algorithm
The exists algorithms can be defined in terms of apply and restrict:
apply(+, restrict(0,x,Bf), restrict(1,x,Bf))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 Bf with each x-node n replaced with the result of apply(+,lo(n),hi(n)).
Huth-Ryan, pp. 377
CMPUT 680 - Compiler Design and Optimization
59
The exists algorithm(example)
Bf
x3.f = ?
apply(+,R7,R6) = ?
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
A(R7,R6) y3
A(R7,R7) A(R7,R8)
y3
0 1
Huth-Ryan, pp. 379
CMPUT 680 - Compiler Design and Optimization
60
The exists algorithm(example)
Bf
x3.f
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
Huth-Ryan, pp. 379
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
CMPUT 680 - Compiler Design and Optimization
61
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x3.x2.f = ?
apply(+,R6,R4) = ?
Huth-Ryan, pp. 379
A(R6,R4)
CMPUT 680 - Compiler Design and Optimization
62
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x3.x2.f = ?
apply(+,R6,R4) = ?
Huth-Ryan, pp. 379
y2
A(R6,R6) A(R6,R8)
CMPUT 680 - Compiler Design and Optimization
63
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x3.x2.f = ?
apply(+,R6,R4) = ?
Huth-Ryan, pp. 379
y2
A(R6,R8)y3
0 1
CMPUT 680 - Compiler Design and Optimization
64
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x3.x2.f = ?
apply(+,R6,R4) = ?
Huth-Ryan, pp. 379
y2
A(R7,R8)
y3
0 1
y3
A(R8,R8)
CMPUT 680 - Compiler Design and Optimization
65
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x3.x2.f = ?
apply(+,R6,R4) = ?
Huth-Ryan, pp. 379
y2
y3
0 1
y3
A(R8,R8)
CMPUT 680 - Compiler Design and Optimization
66
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x3.x2.f = ?
apply(+,R6,R4) = ?
Huth-Ryan, pp. 379
y2
y3
0 1
y3
CMPUT 680 - Compiler Design and Optimization
67
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x3.x2.f = ?
apply(+,R6,R4) = ?
Huth-Ryan, pp. 379
y2
y3
0 1
CMPUT 680 - Compiler Design and Optimization
68
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
0 1
y3
R1
R2
R4
R6
R7 R8
x3.x2.f
Huth-Ryan, pp. 379
CMPUT 680 - Compiler Design and Optimization
69
The exists algorithm(example)
Bf
x1
y1
y2
x2
x3
0 1
y3
R1
R2
R3
R4
R5
R6
R7 R8
x1
y1
y2
x2
0 1
y3
R1
R2
R3
R4
R6
R7 R8
Bx3.f
x1
y1
y2
0 1
y3
R1
R2
R4
R6
R7 R8
Bx2.x3.fHuth-Ryan, pp. 379
CMPUT 680 - Compiler Design and Optimization
70
Translation of Boolean formulas into OBDDs
Formula f OBDD Bf
0
1
x
B0
B1
Bx
f Swap 0- and 1-nodes in Bf
f + g
f g
f g
apply(+, Bf, Bg)
apply(, Bf, Bg)
apply(, Bf, Bg)
f[1/x]
f[0/x]
restrict(1, x, Bf)
restrict(0, x, Bf)
x.f
x.f
apply(+, Bf[0/x], Bf[1/x]),
apply(, Bf[0/x], Bf[1/x]),
Huth-Ryan, pp. 380
CMPUT 680 - Compiler Design and Optimization
71
Upper bounds for running times
AlgorithmInput OBDD(s)
Output OBDD
Time-complexity
reduce B reduced B O(|B|.log|B|)
applyBf, Bg (reduced)
Bf op g (reduced)
O(|Bf|.|Bg|)
restrictBf
(reduced)
Bf[0/x] or Bf[1/x] (reduced)
O(|Bf|.log|Bf|)
Bf
(reduced)
Bx1. x2…xn.f (reduced)
NP-complete
Huth-Ryan, pp. 380
CMPUT 680 - Compiler Design and Optimization
72
Relational Product
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] This 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:
Bf•g = apply (•, Bf, Bg)
apply(+, restrict(0,x, Bf•g), restrict(1,x, Bf•g)) But computing the relational product this way would
be too expensive.
CMPUT 680 - Compiler Design and Optimization
73
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
(R4,S3)
(R5,S4)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
CMPUT 680 - Compiler Design and Optimization
74
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
0
0: T1
(R6,S5)T1
(R4,S3)
CMPUT 680 - Compiler Design and Optimization
75
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
1T2
0: T1
1: T2 A(•,R4,S3): ?
0T1
(R4,S3)
CMPUT 680 - Compiler Design and Optimization
76
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S3)
0: T1
1: T2 A(•,R4,S3): T3
x4T3
1T20T1
CMPUT 680 - Compiler Design and Optimization
77
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S3)
(R4,S3)
0: T1
1: T2 A(•,R4,S3): T3
x4T3
1T20T1
CMPUT 680 - Compiler Design and Optimization
78
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S3)
(R6,S3)
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): ?
x4T3
1T20T1
CMPUT 680 - Compiler Design and Optimization
79
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S3)
(R6,S3)
R6 is a terminal node (B1)thus (R6,S3) results
in S3, which already existsin the BDD.
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): ?
x4T3
1T20T1
CMPUT 680 - Compiler Design and Optimization
80
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S3)
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): ?
x4T3
1T20T1
CMPUT 680 - Compiler Design and Optimization
81
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
(R2,S3)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
x4T3
1T20T1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): ?
CMPUT 680 - Compiler Design and Optimization
82
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
x4T3
1T20T1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
CMPUT 680 - Compiler Design and Optimization
83
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S2)
x4T3
1T20T1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
CMPUT 680 - Compiler Design and Optimization
84
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S2)
(R4,S3)
x4T3
1T20T1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
CMPUT 680 - Compiler Design and Optimization
85
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S2)
(R6,S5)
x4T3
1T20T1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
A(•,R6,S5): ?
CMPUT 680 - Compiler Design and Optimization
86
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
(R3,S2)
x4T3
1T20T1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
A(•,R6,S5): T2
A(•,R3,S2): ?
CMPUT 680 - Compiler Design and Optimization
87
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
x4T3
1T20T1
x3
X3 is a variable in therelational product,
thus we must apply the existence quantifier
to it.
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
A(•,R6,S5): T2
A(•,R3,S2): ?
CMPUT 680 - Compiler Design and Optimization
88
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
x4T3
1T20T1
A(+,T3,T2)
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
A(•,R6,S5): T2
A(•,R3,S2): ?A(+,T3,T2): ?
CMPUT 680 - Compiler Design and Optimization
89
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
(R1,S1)
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
x4T3
1T20T1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
A(•,R6,S5): T2
A(•,R3,S2): T2
A(+,T3,T2): T2
A(•,R1,S1): ?
CMPUT 680 - Compiler Design and Optimization
90
A relational product (example)
Huth-Ryan, pp. 375
x1
0 1
x3
x2
x4
x1
0 1
x3
x4
R1
R2
R3
R4
R5 R6
S1
S2
S3
S4 S5
x3.(f•g) = (f•g)[0/x3] + (f•g)[1/x3]
x4T3
1T20T1
x1
0: T1
1: T2 A(•,R4,S3): T3
A(•,R6,S3): T3
A(•,R3,S3): T3 A(•,R2,S3): T3
A(•,R6,S5): T2
A(•,R3,S2): T2
A(+,T3,T2): T2
A(•,R1,S1): ?
T4
CMPUT 680 - Compiler Design and Optimization
91
BDD packages
BDDs are becoming popular for several applications thanks to the existence of efficient BDD packages : BuDDy: A Binary Decision Diagram Package by JØrn
Lind-Nielsen from Technical University of Denmarkhttp://www.itu.dk/research/buddy
CUDD: CU Decision diagram package, by Fabio Somenzi from University of Colorado at Boulder
http://vlsi.colorado.edu/~fabio/CUDD Jedd: A BDD-based relational extension of Java by
Ondrej Lhoták from McGill Universityhttp://www.sable.mcgill.ca/jedd
CMPUT 680 - Compiler Design and Optimization
92
Representing Subsets with OBDDs
Let S be a finite set.Assign a unique Boolean vector (v1, v2, …,
vn) to each element s S: 2n-1 < |S| 2n
vi {0, 1}A subset T S is represented by a
Boolean function fT. fT maps onto 1 if s T, and maps it onto 0
otherwise.
Huth-Ryan, pp. 383
CMPUT 680 - Compiler Design and Optimization
93
Using OBDDs to represent points-to relations
This program segment has: three allocation
statements (objects): L1, L2 and L3;
three pointers: a, b, and c; We can encode the
pointers with two bits; We can encode the
objects with two bits;
Object
Code
L1 00
L2 01
L3 10
Pointer
Code
a 00
b 01
c 10
Berndl-Lhotak et al., PLDI03
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
CMPUT 680 - Compiler Design and Optimization
94
Using OBDDs to represent points-to relations
We compute the may point-to relation:T = {(a,L1); (a,L2); (b,L1); (b,L2); (c,L1); (c,L2);
(c,L3)}
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Object
Code
L1 00
L2 01
L3 10
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
95
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v2v2
v4v4 v4v4 v4v4 v4v4
v3v3 v3v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
96
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v2v2
v4v4 v4v4 v4v4 v4v4
v3v3 v3v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
97
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v2v2
v4v4 v4v4 v4v4
v3v3 v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
98
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v2v2
v4v4 v4
v3v3 v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
99
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v2
v3v3 v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
100
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v2
v3v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
101
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v2
v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
102
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Using OBDDs to represent points-to relations
Codev1v2v3v
4
fT
0000 1
0001 1
0010 0
0011 x
0100 1
0101 1
0110 0
0111 x
Pointer
Code
a 00
b 01
c 10
Codev1v2v3v
4
fT
1000 1
1001 1
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
Object
Code
L1 00
L2 01
L3 10
v1
v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
103
Using OBDDs to represent points-to relations
v1
v3
1 0
v1
v3
1 0
v2
v3
v4
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
CMPUT 680 - Compiler Design and Optimization
104
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Effect of bit ordering
Codev4v2v3v
1
fT
0000 1
1000 1
0001 0
1001 x
0010 1
1010 1
0011 0
1011 x
Pointer
Code
a 00
b 01
c 02
Codev4v2v3v
1
fT
0100 1
1100 1
0101 1
1101 x
0110 x
1110 x
0111 x
1111 x
Object
Code
L1 00
L2 01
L3 02
v4
v2v2
v1v1 v1v1 v1v1 v1v1
v3v3 v3v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
105
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Effect of bit ordering
Codev4v2v3v
1
fT
0000 1
1000 1
0001 0
1001 x
0010 1
1010 1
0011 0
1011 x
Pointer
Code
a 00
b 01
c 02
Codev4v2v3v
1
fT
0100 1
1100 1
0101 1
1101 x
0110 x
1110 x
0111 x
1111 x
Object
Code
L1 00
L2 01
L3 02
v4
v2v2
v1v1 v1v1 v1v1 v1v1
v3v3 v3v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
106
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Effect of bit ordering
Codev4v2v3v
1
fT
0000 1
1000 1
0001 0
1001 x
0010 1
1010 1
0011 0
1011 x
Pointer
Code
a 00
b 01
c 02
Codev4v2v3v
1
fT
0100 1
1100 1
0101 1
1101 x
0110 x
1110 x
0111 x
1111 x
Object
Code
L1 00
L2 01
L3 02
v4
v2
v1
v3
1 0
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
107
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Effect of bit ordering
Codev4v2v3v
1
fT
0000 1
1000 1
0001 0
1001 x
0010 1
1010 1
0011 0
1011 x
Pointer
Code
a 00
b 01
c 02
Codev4v2v3v
1
fT
0100 1
1100 1
0101 1
1101 x
0110 x
1110 x
0111 x
1111 x
Object
Code
L1 00
L2 01
L3 02
After Berndl-Lhotak et al., PLDI03
v4
v2
v1
v3
10
CMPUT 680 - Compiler Design and Optimization
108
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
v4
v2
v1
v3
10
v4
v3
1 0
v2
v3
v1
v2
v3
v1
CMPUT 680 - Compiler Design and Optimization
109
Effect of bit ordering and use of don’t cares
After Berndl-Lhotak et al., PLDI03
v4
v2
v1
v3
10
v4
v3
1 0
v2
v3
v1
v2
v3
v1
v1
v3
1 0
v1
v3
1 0
v2
v3
v4
CMPUT 680 - Compiler Design and Optimization
110
Relations
Reference analysis can be defined in terms of relations.
A domain is a set of elements. To encode the relations in the
code on the left we need two domains:domain of pointers = {a, b, c}domain of abstract objects = {L1, L2,
L3}
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
111
Relations
An attribute is a domain along with an associated name. To encode the relations in the
code on the left we need four attributes:pointer = {a, b, c}object = {L1, L2, L3}source = {a, b, c}destination = {a, b, c}
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
112
Relations
A tuple is a list of elements indexed by attributes.
Examples of tuples include:L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
pointer
object
b L2
sourcedestinatio
n
a bAfter Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
113
Relations
A relation is a collection of tuples that share the same attributes. Examples of relations for this
code:
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
pointer object
a L1
b L2
c L3
sourcedestinatio
n
b a
a b
b cInitial points-to pair.Assignments.
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
114
Point-to Algorithm
The goal is to compute a point-to relation between variables of pointer time and allocation sites (or abstract objects).
pointer object
? ?
••• •••
? ?
pointer object
a L1
b L2
c L3
Initial points-to pair.
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
115
Point-to Algorithm
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
? ?
••• •••
? ?
pointer object
a L1
b L2
c L3
Initial points-to pairs.
constraints
Final points-to pairs.
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
116
Interference rules
€
l1 → l2 o∈ pt(l1)o∈ pt(l2)
if l1 points to o,and l1 is assigned to l2,then l2 also points to o.
Simple assignment:
€
o2 ∈ pt(l) l→ q. f o1 ∈ pt(q)o2 ∈ pt(o1. f )
if l points to o2,and l is stored into q.f,then for each o1 pointed to by q,o1.f also points to o2.
Field store:
€
p. f → l o1 ∈ pt(p) o2 ∈ pt(o1. f )
o2 ∈ pt(l)
if l is loaded from p.f,and p points to o1,then for each o2 pointed to by o1.f,l points to o2.
Field load:
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
117
Domains, Relations, and BDDs (example)
The general algorithm uses five attributes, but this examples only needs three attributes:
V = {a, b, c}W = {a, b, c}H = {L1, L2, L3}
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
Variables
Heap Locations
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
118
Domains, Relations, and BDDs (example)
The allocations generate the initial points-to relation:
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
V H
a L1
b L2
c L3
V Code
a 00
b 01
c 01
H Code
L1 00
L2 01
L3 01
CodeVH
initialpt
0000 1
0001 0
0010 0
0011 x
0100 0
0101 1
0110 0
0111 x
CodeVH
initialpt
1000 0
1001 0
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
119
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?
V0
H1
1
H0 H0
0
V1
H1
CodeVH
initialpt
0000 1
0001 0
0010 0
0011 x
0100 0
0101 1
0110 0
0111 x
CodeVH
initialpt
1000 0
1001 0
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
CodeVH
initialpt
0011 x
0111 x
1011 x
1100 x
1101 x
1110 x
1111 x
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
120
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?
V0
H1
1
H0 H0
0
V1
H1
CodeVH
initialpt
0000 1
0001 0
0010 0
0011 x
0100 0
0101 1
0110 0
0111 x
CodeVH
initialpt
1000 0
1001 0
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
CodeVH
initialpt
0011 0
0111 x
1011 x
1100 x
1101 x
1110 x
1111 x
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
121
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?
V0
H1
1
H0 H0
0
V1
H1
CodeVH
initialpt
0000 1
0001 0
0010 0
0011 x
0100 0
0101 1
0110 0
0111 x
CodeVH
initialpt
1000 0
1001 0
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
CodeVH
initialpt
0011 0
0111 1
1011 x
1100 x
1101 x
1110 x
1111 x
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
122
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?
V0
H1
1
H0 H0
0
V1
H1
CodeVH
initialpt
0000 1
0001 0
0010 0
0011 x
0100 0
0101 1
0110 0
0111 x
CodeVH
initialpt
1000 0
1001 0
1010 1
1011 x
1100 x
1101 x
1110 x
1111 x
CodeVH
initialpt
0011 0
0111 1
1011 1
1100 0
1101 0
1110 1
1111 1
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
123
Domains, Relations, and BDDs (example)
The assignments generate the edge-set relation:
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
V W
b a
a b
b c
V Code
a 00
b 01
c 01
W Code
a 00
b 01
c 01
CodeVW
edge-set
0000 0
0001 1
0010 0
0011 x
0100 1
0101 0
0110 1
0111 x
CodeVW
edge-set
1000 0
1001 0
1010 0
1011 x
1100 x
1101 x
1110 x
1111 x
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
124
Domains, Relations, and BDDs (example)
V0
V1
W1
01
W1
EDGE-SET CodeVW
edge-set
0000 0
0001 1
0010 0
0011 x
0100 1
0101 0
0110 1
0111 x
CodeVW
edge-set
1000 0
1001 0
1010 0
1011 x
1100 x
1101 x
1110 x
1111 x
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
126
Domains, Relations, and BDDs (example)
W1
H1
H0
H1
10
RELPROD(V)
AFTERRELPROD(Init, Edge, V) AFTER
REPLACE(W to V)INITIAL
POINTS-TO EDGE-SET
V0
V1
W1
01
W1
V0
H1
1
H0 H0
0
V1
H1
V1
H1
H0
H1
10
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
127
Domains, Relations, and BDDs (example)
AFTERREPLACE(W to V)
It represents propagation of the points-to
relation caused by the assignments.
V1
H1
H0
H1
10
{(a,L2),(b,L1), (c,L2)}
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 (L1 = 00, L2 = 01, L3 = 10)
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
128
Domains, Relations, and BDDs (example)
UNION
INITIAL POINTS-TO
POINTS-TODUE TO EDGE-SET
V1
H1
H0
H1
10
V0
H1
1
H0 H0
0
V1
H1
V0
V1
H0
H1
1 0
H0
POINTS-TOAfter 1st Propagation
H1
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
129
Domains, Relations, and BDDs (example)
Elements in the Points-to set:{(a,L1), (a,L2),,(b,L1), (b,L2), (c,L2), (c,L3)}
V0V1H0H1
0000 (a,L1)
0001 (a,L2)
0100 (b,L1)
0101 (b,L2)
1001 (c,L2)
1010 (c,L3)
POINTS-TOAfter 1st Propagation
After Berndl-Lhotak et al., PLDI03
V0
V1
H0
H1
1 0
H0
H1
H0
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
-Points-to relations caused by more than 1 propagation: {(a,L1)}
What’s Missing?
CMPUT 680 - Compiler Design and Optimization
130
Domains, Relations, and BDDs (example)
UNION
V0
V1
H0
10
H0
POINTS-TODUE TO EDGE-SET
POINTS-TOAfter 2nd Propagation
V1
H1
H0
H1
10
H1
After Berndl-Lhotak et al., PLDI03
V0
V1
H0
H1
1 0
H0
H1
H0
POINTS-TOAfter 1st Propagation
CMPUT 680 - Compiler Design and Optimization
131
Domains, Relations, and BDDs (example)
V0
V1
H0
10
H0
POINTS-TOAfter 3nd Propagation
(Unchanged)
H1
POINTS-TOAfter 2nd Propagation
UNION
V0
V1
H0
10
H0
POINTS-TODUE TO EDGE-SET
V1
H1
H0
H1
10
H1
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
132
Domains, Relations, and BDDs (example)
V0
V1
H0
10
H0
H1
Final Points-to Set
Final Points-to Set
(a,L1) 0000 1
(a,L2) 0001 1
(b,L1) 0100 1
(b,L2) 0101 1
(c,L2) 1001 1
(c,L3) 1010 1
(c,L1) 1000 1
(a,L3) 0010 0
(b,L3) 0110 0
L1: a = new O();L2: b = new O();L3: c = new O(); a = b; b = a; c = b;
After Berndl-Lhotak et al., PLDI03
CMPUT 680 - Compiler Design and Optimization
133
Why Use BDDs for Program Analysis?
SpaceLarge 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.
TimeFor 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