-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodule70.tex
More file actions
95 lines (76 loc) · 3.3 KB
/
module70.tex
File metadata and controls
95 lines (76 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
\section{Module 70 : Static Single Assignment IR}
Consider the following example
\[x = y+z\]
\[x = x+1\]
\[w=y+z\]
\[z=x+3\]
In this example, the computation $y+z$ could be reused.
In SSA, we assign versions to variables(as below) and each version has only one assignment to it. SSA stands for single assignment in a static program.
\[x_1 = y+z\]
\[x_2 = x_1+1\]
\[w_1=y+z\]
\[v_1=x_2+3\]
Using SSA, we can not optimize the code by rewriting $w_1 = y+z$ as $w_1 = x_1$.
\[x_1 = y+z\]
\[x_2 = x_1+1\]
\[w_1=x_1\]
\[v_1=x_2+3\]
Why SSA? Advantages of SSA:
\begin{itemize}
\item Optimization algorithms become simpler if each variable has only one definition.
\item Unrelated uses of same variable become independent.
\item More values become available at each program point.
\end{itemize}
Therefore, SSA is a very popular method of IR design. LLVM IR is also an SSA IR.
\subsection{Converting to SSA}
\begin{itemize}
\item Replace the target of each assignment with a new variable.
\item Replace each use of a variable with the version of the variable reaching that point.
\end{itemize}
Again, taking an example:
\[x = y+z\]
\[a = b+x\]
\[x=a+3\]
\[y=x-a\]
On applying the two rules for conversion to SSA, these statements change to :
\[x_1 = y+z\]
\[a = b+x_1\]
\[x_2=a+3\]
\[y=x_2-a\]
\begin{figure*}
\centering
\includegraphics[height=8cm]{images/Example1.png}
\caption{Converting to SSA Example}
\end{figure*}
Consider another example as shown in Figure 1:
In this example, there are 2 variables of interest. $x$ is assigned thrice and $y$ is assigned thrice. On applying the rules of SSA conversion, here is how the code snippet looks like.
The versioning is straightforward for variable $x$. For $y$ in this example, there are two versions reaching at the bottom block from two different paths. The question is how we version the variable $y$ at bottom block. To answer that , we need to understand about basic block
\begin{figure*}
\centering
\includegraphics[height=8cm]{images/Example2.png}
\caption{Converting to SSA Example - With Versions}
\end{figure*}
\subsection{Basic Block}:
A basic block is a maximal set of instructions with
\begin{itemize}
\item no labels( except at the first instruction)
\item no jumps (except in the last instruction)
\end{itemize}
In the example, each rectangle is a basic block.
Idea:
\begin{itemize}
\item Cannot jump into a basic block (except at beginning)
\item cannot jump out of a basic block (except at end)
\item Single-entry single-exit straight line code segment
\end{itemize}
At the bottom block, there are two version reaching for variable $y$. To represent that we use $\Phi$ node or $\Phi$ function. So the version of y can be represented as a function of $y_1$ and $y_2$ as shown in figure XXX. This is an ordered set.
\begin{figure*}
\centering
\includegraphics[height=8cm]{images/Example3.png}
\caption{Converting to SSA Example - PHI Nodes}
\end{figure*}
\subsection{PHI($\Phi$) Nodes}
\begin{itemize}
\item $\Phi$ function chooses the version depending on the incoming edge.
\item Present only at the beginning of a basic block. Since this is the only place where there are multiple versions flowing through different incoming edges.
\end{itemize}