1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
-
date post
20-Dec-2015 -
Category
Documents
-
view
215 -
download
1
Transcript of 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
![Page 1: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/1.jpg)
1
CS 201Compiler Construction
Lecture 9Static Single Assignment Form
![Page 2: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/2.jpg)
Program Representations
Why develop Advanced Program Representations?
-To develop faster algorithms-To develop more powerful algorithms
Superior representation for Data FlowStatic Single Assignment Form (SSA Form)
superior to def-use chains
Superior representation for Control FlowControl Dependence Graph
superior to control flow graph2
![Page 3: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/3.jpg)
SSA-Form
A program in SSA-form satisfies the following two properties:
1. A use of a variable is reached by exactly one definition of that variable.
1. The program is augmented with ϕ–nodes that distinguish values of variables transmitted on distinct incoming control flow edges.
3
![Page 4: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/4.jpg)
Example
K 1L 1Repeat
if (P) thenif (Q) then
L2else L3KK+1
else KK+2Until (T)
4
K11L11Repeat
K2ϕ(K1,K5)L2ϕ(L1,L6)if (P) then
if (Q) thenL32
else L43L5ϕ(L3,L4)K3K2+1
else K4K2+2K5ϕ(K3,K4)L6ϕ(L2,L5)
Until (T)
![Page 5: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/5.jpg)
SSA-Form
Observations:
1. SSA-form has def-use information textually embedded in it. Given a use, we know where the definition comes from.
1. SSA-form is more compact representation of def-use chains.
– Def-use chains: #defs x #uses – O(n2)– SSA-form: 2 x #defs or #uses – O(n)
5
![Page 6: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/6.jpg)
Constructing SSA-Form
Step 1: Introduce functions at certain points in the program -- v ϕ (v,v,….) where of operands equals number of control predecessors and ith operand corresponds to ith predecessor.
6
![Page 7: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/7.jpg)
Contd..
Step 2: Each variable v is given several new names v1, v2, …. Such that every name appears exactly once on the left hand side of an assignment.
7
![Page 8: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/8.jpg)
Step 1: Introducing ϕ-functions
Node Z needs a ϕ-function for variable V if Z is the first node common to two non-null paths that originate at two different nodes each containing: an assignment to V; or a ϕ-function for V.
8
![Page 9: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/9.jpg)
Step 1 Contd..
Definition: X strictly dominates Y ≅ X dominates Y & X != Y.
Definition: Immediate dominator of a node is its closest strict dominator. Notation: X = idom(Y).
Definition: Dominance Frontier DF(X) = {Y: there exists P εpred(Y) such that X
dominates P & X does not strictly dominate Y} ϕ-functions are placed at nodes in DF nodes of
nodes with assignments.
9
![Page 10: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/10.jpg)
Step 1 Contd..
Strict Domination Does Not StrictlyDomination Dominate
10
Y ε DF(X)
![Page 11: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/11.jpg)
Step 1 Contd..
Observation: if Y εDF(X) then there may or may not be a direct edge from X to Y.
11
![Page 12: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/12.jpg)
Step 1 Contd..
12
Computing Dominance Frontier:for each Y εsucc(X) do
if idom(Y) != X thenDF(X) = DF(X) U {Y}
for each Z εChidren(X) in the dominator tree dofor each Y εDF(Z) do
if idom(Y) != X then DF(X) = DF(X) U {Y}
Compute bottom-up order According to dominator tree
![Page 13: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/13.jpg)
Step 1 Contd..
13
Dominator Tree
![Page 14: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/14.jpg)
Step 1 Contd..
Dominance Frontier of a Set of Nodes S DF(S) = DF(X)
Iterated Dominance Frontier DF+(S):DF1 = DF(S)
DFi+1 = DF(S U DFi)
S – set of nodes which assign to variable V
DF+(S) – set of nodes including those where ϕ-functions must be placed.
14
![Page 15: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/15.jpg)
Step 2: Rename the Variables
Step 2: For each variable v rename its left hand side occurrences as v1, v2, …. Perform reaching definition analysis to identify names to use in the right hand side occurrences of v.
15
![Page 16: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/16.jpg)
16
Sample ProblemsStatic Single Assignment
![Page 17: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/17.jpg)
17
SSA Form
![Page 18: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/18.jpg)
18
Applications of SSA Form
![Page 19: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/19.jpg)
Global Value Numbering
A technique for determining when two computations in a program are equivalent can be used for redundancy removal.
Constant Propagation -- by computing values of two computations they can be shown to be equivalent.
Common Subexpression Elimination -- lexically identical expressions can be shown to be equivalent.
Value Numbering -- lexically different expressions can be shown to be equivalent without computing their values.
19
![Page 20: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/20.jpg)
Examples
20
![Page 21: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/21.jpg)
Examples
21
![Page 22: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/22.jpg)
Value Numbering Algorithm
22
![Page 23: 1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.](https://reader036.fdocuments.us/reader036/viewer/2022062714/56649d4e5503460f94a2e703/html5/thumbnails/23.jpg)
Value Numbering Algorithm
23