|
@@ -2478,33 +2478,32 @@ stack locations or registers.
|
|
|
\begin{figure}
|
|
|
\begin{minipage}{0.45\textwidth}
|
|
|
Example $R_1$ program:
|
|
|
-% s0_22.rkt
|
|
|
+% s0_28.rkt
|
|
|
\begin{lstlisting}
|
|
|
(let ([v 1])
|
|
|
-(let ([w 46])
|
|
|
-(let ([x (+ v 7)])
|
|
|
-(let ([y (+ 4 x)])
|
|
|
-(let ([z (+ x w)])
|
|
|
- (+ z (- y)))))))
|
|
|
+ (let ([w 42])
|
|
|
+ (let ([x (+ v 7)])
|
|
|
+ (let ([y x])
|
|
|
+ (let ([z (+ x w)])
|
|
|
+ (+ z (- y)))))))
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
|
\begin{minipage}{0.45\textwidth}
|
|
|
After instruction selection:
|
|
|
\begin{lstlisting}
|
|
|
-locals: v w x y z t.1
|
|
|
+locals: (v w x y z t)
|
|
|
start:
|
|
|
movq $1, v
|
|
|
- movq $46, w
|
|
|
+ movq $42, w
|
|
|
movq v, x
|
|
|
addq $7, x
|
|
|
- movq $4, y
|
|
|
- addq x, y
|
|
|
+ movq x, y
|
|
|
movq x, z
|
|
|
addq w, z
|
|
|
- movq y, t.1
|
|
|
- negq t.1
|
|
|
+ movq y, t
|
|
|
+ negq t
|
|
|
movq z, %rax
|
|
|
- addq t.1, %rax
|
|
|
+ addq t, %rax
|
|
|
jmp conclusion
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
@@ -2681,29 +2680,27 @@ sets shown between each instruction to make the figure easy to read.
|
|
|
\begin{lstlisting}
|
|
|
|$\{\}$|
|
|
|
movq $1, v
|
|
|
- |$\{v\}$|
|
|
|
- movq $46, w
|
|
|
+ |$\{v\}$|
|
|
|
+ movq $42, w
|
|
|
|$\{v,w\}$|
|
|
|
movq v, x
|
|
|
|$\{w,x\}$|
|
|
|
addq $7, x
|
|
|
|$\{w,x\}$|
|
|
|
- movq $4, y
|
|
|
- |$\{w,x,y\}$|
|
|
|
- addq x, y
|
|
|
+ movq x, y
|
|
|
|$\{w,x,y\}$|
|
|
|
movq x, z
|
|
|
|$\{w,y,z\}$|
|
|
|
addq w, z
|
|
|
|$\{y,z\}$|
|
|
|
- movq y, t.1
|
|
|
- |$\{t.1,z\}$|
|
|
|
- negq t.1
|
|
|
- |$\{t.1,z\}$|
|
|
|
+ movq y, t
|
|
|
+ |$\{t,z\}$|
|
|
|
+ negq t
|
|
|
+ |$\{t,z\}$|
|
|
|
movq z, %rax
|
|
|
- |$\{t.1\}$|
|
|
|
- addq t.1, %rax
|
|
|
- |$\{\}$|
|
|
|
+ |$\{t\}$|
|
|
|
+ addq t, %rax
|
|
|
+ |$\{\}$|
|
|
|
jmp conclusion
|
|
|
|$\{\}$|
|
|
|
\end{lstlisting}
|
|
@@ -2788,17 +2785,16 @@ the following interference for each instruction.
|
|
|
\begin{quote}
|
|
|
\begin{tabular}{ll}
|
|
|
\lstinline{movq $1, v}& no interference by rule 3,\\
|
|
|
-\lstinline{movq $46, w}& $w$ interferes with $v$ by rule 3,\\
|
|
|
+\lstinline{movq $42, w}& $w$ interferes with $v$ by rule 3,\\
|
|
|
\lstinline{movq v, x}& $x$ interferes with $w$ by rule 3,\\
|
|
|
\lstinline{addq $7, x}& $x$ interferes with $w$ by rule 1,\\
|
|
|
-\lstinline{movq $4, y}& $y$ interferes with $w$ and $x$ by rule 3,\\
|
|
|
-\lstinline{addq x, y}& $y$ interferes with $w$ and $x$ by rule 1,\\
|
|
|
+\lstinline{movq x, y}& $y$ interferes with $w$ but not $x$ by rule 3,\\
|
|
|
\lstinline{movq x, z}& $z$ interferes with $w$ and $y$ by rule 3,\\
|
|
|
\lstinline{addq w, z}& $z$ interferes with $y$ by rule 1, \\
|
|
|
-\lstinline{movq y, t.1}& $t.1$ interferes with $z$ by rule 3, \\
|
|
|
-\lstinline{negq t.1}& $t.1$ interferes with $z$ by rule 1, \\
|
|
|
+\lstinline{movq y, t}& $t$ interferes with $z$ by rule 3, \\
|
|
|
+\lstinline{negq t}& $t$ interferes with $z$ by rule 1, \\
|
|
|
\lstinline{movq z, %rax} & no interference (ignore rax), \\
|
|
|
-\lstinline{addq t.1, %rax} & no interference (ignore rax). \\
|
|
|
+\lstinline{addq t, %rax} & no interference (ignore rax). \\
|
|
|
\lstinline{jmp conclusion}& no interference.
|
|
|
\end{tabular}
|
|
|
\end{quote}
|
|
@@ -2809,7 +2805,7 @@ Figure~\ref{fig:interfere}.
|
|
|
\large
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1$};
|
|
|
+\node (t1) at (0,2) {$t$};
|
|
|
\node (z) at (3,2) {$z$};
|
|
|
\node (x) at (6,2) {$x$};
|
|
|
\node (y) at (3,0) {$y$};
|
|
@@ -2821,7 +2817,6 @@ Figure~\ref{fig:interfere}.
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
@@ -2983,7 +2978,7 @@ colored and they are unsaturated, so we annotate each of them with a
|
|
|
dash for their color and an empty set for the saturation.
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1:-,\{\}$};
|
|
|
+\node (t1) at (0,2) {$t:-,\{\}$};
|
|
|
\node (z) at (3,2) {$z:-,\{\}$};
|
|
|
\node (x) at (6,2) {$x:-,\{\}$};
|
|
|
\node (y) at (3,0) {$y:-,\{\}$};
|
|
@@ -2994,18 +2989,17 @@ dash for their color and an empty set for the saturation.
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
|
\]
|
|
|
The algorithm says to select a maximally saturated vertex and color it
|
|
|
$0$. In this case we have a 6-way tie, so we arbitrarily pick
|
|
|
-$t.1$. We then mark color $0$ as no longer available for $z$ because
|
|
|
-it interferes with $t.1$.
|
|
|
+$t$. We then mark color $0$ as no longer available for $z$ because
|
|
|
+it interferes with $t$.
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1:0,\{\}$};
|
|
|
+\node (t1) at (0,2) {$t:0,\{\}$};
|
|
|
\node (z) at (3,2) {$z:-,\{0\}$};
|
|
|
\node (x) at (6,2) {$x:-,\{\}$};
|
|
|
\node (y) at (3,0) {$y:-,\{\}$};
|
|
@@ -3016,7 +3010,6 @@ it interferes with $t.1$.
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
@@ -3026,7 +3019,7 @@ vertex, which is $z$, and color it with the first available number,
|
|
|
which is $1$.
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1:0,\{\}$};
|
|
|
+\node (t1) at (0,2) {$t:0,\{1\}$};
|
|
|
\node (z) at (3,2) {$z:1,\{0\}$};
|
|
|
\node (x) at (6,2) {$x:-,\{\}$};
|
|
|
\node (y) at (3,0) {$y:-,\{1\}$};
|
|
@@ -3037,87 +3030,82 @@ which is $1$.
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
|
\]
|
|
|
-The most saturated vertices are now $w$ and $y$. We color $y$ with the
|
|
|
+The most saturated vertices are now $w$ and $y$. We color $w$ with the
|
|
|
first available color, which is $0$.
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1:0,\{\}$};
|
|
|
+\node (t1) at (0,2) {$t:0,\{1\}$};
|
|
|
\node (z) at (3,2) {$z:1,\{0\}$};
|
|
|
\node (x) at (6,2) {$x:-,\{0\}$};
|
|
|
-\node (y) at (3,0) {$y:0,\{1\}$};
|
|
|
-\node (w) at (6,0) {$w:-,\{0,1\}$};
|
|
|
-\node (v) at (9,0) {$v:-,\{\}$};
|
|
|
+\node (y) at (3,0) {$y:-,\{0,1\}$};
|
|
|
+\node (w) at (6,0) {$w:0,\{1\}$};
|
|
|
+\node (v) at (9,0) {$v:-,\{0\}$};
|
|
|
|
|
|
\draw (t1) to (z);
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
|
\]
|
|
|
-Vertex $w$ is now the most highly saturated, so we color $w$ with $2$.
|
|
|
-We cannot choose $0$ or $1$ because those numbers are in $w$'s
|
|
|
-saturation set. Indeed, $w$ interferes with $y$ and $z$, whose colors
|
|
|
+Vertex $y$ is now the most highly saturated, so we color $y$ with $2$.
|
|
|
+We cannot choose $0$ or $1$ because those numbers are in $y$'s
|
|
|
+saturation set. Indeed, $y$ interferes with $w$ and $z$, whose colors
|
|
|
are $0$ and $1$ respectively.
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1:0,\{\}$};
|
|
|
+\node (t1) at (0,2) {$t:0,\{1\}$};
|
|
|
\node (z) at (3,2) {$z:1,\{0,2\}$};
|
|
|
-\node (x) at (6,2) {$x:-,\{0,2\}$};
|
|
|
-\node (y) at (3,0) {$y:0,\{1,2\}$};
|
|
|
-\node (w) at (6,0) {$w:2,\{0,1\}$};
|
|
|
-\node (v) at (9,0) {$v:-,\{2\}$};
|
|
|
+\node (x) at (6,2) {$x:-,\{0\}$};
|
|
|
+\node (y) at (3,0) {$y:2,\{0,1\}$};
|
|
|
+\node (w) at (6,0) {$w:0,\{1,2\}$};
|
|
|
+\node (v) at (9,0) {$v:-,\{0\}$};
|
|
|
|
|
|
\draw (t1) to (z);
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
|
\]
|
|
|
-Now $x$ has the highest saturation, so we color it $1$.
|
|
|
+Now $x$ and $v$ are the most saturated, so we color $v$ it $1$.
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1:0,\{\}$};
|
|
|
+\node (t1) at (0,2) {$t:0,\{1\}$};
|
|
|
\node (z) at (3,2) {$z:1,\{0,2\}$};
|
|
|
-\node (x) at (6,2) {$x:1,\{0,2\}$};
|
|
|
-\node (y) at (3,0) {$y:0,\{1,2\}$};
|
|
|
-\node (w) at (6,0) {$w:2,\{0,1\}$};
|
|
|
-\node (v) at (9,0) {$v:-,\{2\}$};
|
|
|
+\node (x) at (6,2) {$x:-,\{0\}$};
|
|
|
+\node (y) at (3,0) {$y:2,\{0,1\}$};
|
|
|
+\node (w) at (6,0) {$w:0,\{1,2\}$};
|
|
|
+\node (v) at (9,0) {$v:1,\{0\}$};
|
|
|
|
|
|
\draw (t1) to (z);
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
|
\]
|
|
|
-In the last step of the algorithm, we color $v$ with $0$.
|
|
|
+In the last step of the algorithm, we color $x$ with $1$.
|
|
|
\[
|
|
|
\begin{tikzpicture}[baseline=(current bounding box.center)]
|
|
|
-\node (t1) at (0,2) {$t.1:0,\{\}$};
|
|
|
+\node (t1) at (0,2) {$t:0,\{1,\}$};
|
|
|
\node (z) at (3,2) {$z:1,\{0,2\}$};
|
|
|
-\node (x) at (6,2) {$x:1,\{0,2\}$};
|
|
|
-\node (y) at (3,0) {$y:0,\{1,2\}$};
|
|
|
-\node (w) at (6,0) {$w:2,\{0,1\}$};
|
|
|
-\node (v) at (9,0) {$v:0,\{2\}$};
|
|
|
+\node (x) at (6,2) {$x:1,\{0\}$};
|
|
|
+\node (y) at (3,0) {$y:2,\{0,1\}$};
|
|
|
+\node (w) at (6,0) {$w:0,\{1,2\}$};
|
|
|
+\node (v) at (9,0) {$v:1,\{0\}$};
|
|
|
|
|
|
\draw (t1) to (z);
|
|
|
\draw (z) to (y);
|
|
|
\draw (z) to (w);
|
|
|
\draw (x) to (w);
|
|
|
-\draw (y) to (x);
|
|
|
\draw (y) to (w);
|
|
|
\draw (v) to (w);
|
|
|
\end{tikzpicture}
|
|
@@ -3137,11 +3125,11 @@ variables, we arrive at the following assignment of variables to
|
|
|
registers and stack locations.
|
|
|
\begin{gather*}
|
|
|
\{ v \mapsto \key{\%rcx}, \,
|
|
|
- w \mapsto \key{-16(\%rbp)}, \,
|
|
|
- x \mapsto \key{-8(\%rbp)}, \\
|
|
|
- y \mapsto \key{\%rcx}, \,
|
|
|
- z\mapsto \key{-8(\%rbp)},
|
|
|
- t.1\mapsto \key{\%rcx} \}
|
|
|
+ w \mapsto \key{\%rcx}, \,
|
|
|
+ x \mapsto \key{-8(\%rbp)}, \\
|
|
|
+ y \mapsto \key{-16(\%rbp)}, \,
|
|
|
+ z\mapsto \key{-8(\%rbp)},
|
|
|
+ t\mapsto \key{\%rcx} \}
|
|
|
\end{gather*}
|
|
|
Applying this assignment to our running example, on the left, yields
|
|
|
the program on the right.
|
|
@@ -3150,17 +3138,16 @@ the program on the right.
|
|
|
\begin{minipage}{0.3\textwidth}
|
|
|
\begin{lstlisting}
|
|
|
movq $1, v
|
|
|
-movq $46, w
|
|
|
+movq $42, w
|
|
|
movq v, x
|
|
|
addq $7, x
|
|
|
-movq $4, y
|
|
|
-addq x, y
|
|
|
+movq x, y
|
|
|
movq x, z
|
|
|
addq w, z
|
|
|
-movq y, t.1
|
|
|
-negq t.1
|
|
|
+movq y, t
|
|
|
+negq t
|
|
|
movq z, %rax
|
|
|
-addq t.1, %rax
|
|
|
+addq t, %rax
|
|
|
jmp conclusion
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|
|
@@ -3168,14 +3155,13 @@ $\Rightarrow\qquad$
|
|
|
\begin{minipage}{0.45\textwidth}
|
|
|
\begin{lstlisting}
|
|
|
movq $1, %rcx
|
|
|
-movq $46, -16(%rbp)
|
|
|
+movq $42, %rcx
|
|
|
movq %rcx, -8(%rbp)
|
|
|
addq $7, -8(%rbp)
|
|
|
-movq $4, %rcx
|
|
|
-addq -8(%rbp), %rcx
|
|
|
+movq -8(%rbp), -16(%rbp)
|
|
|
movq -8(%rbp), -8(%rbp)
|
|
|
-addq -16(%rbp), -8(%rbp)
|
|
|
-movq %rcx, %rcx
|
|
|
+addq %rcx, -8(%rbp)
|
|
|
+movq -16(%rbp), %rcx
|
|
|
negq %rcx
|
|
|
movq -8(%rbp), %rax
|
|
|
addq %rcx, %rax
|
|
@@ -3187,11 +3173,11 @@ jmp conclusion
|
|
|
The resulting program is almost an x86 program. The remaining step is
|
|
|
the patch instructions pass. In this example, the trivial move of
|
|
|
\code{-8(\%rbp)} to itself is deleted and the addition of
|
|
|
-\code{-16(\%rbp)} to \key{-8(\%rbp)} is fixed by going through
|
|
|
+\code{-8(\%rbp)} to \key{-16(\%rbp)} is fixed by going through
|
|
|
\code{rax} as follows.
|
|
|
\begin{lstlisting}
|
|
|
-movq -16(%rbp), %rax
|
|
|
-addq %rax, -8(%rbp)
|
|
|
+movq -8(%rbp), %rax
|
|
|
+addq %rax, -16(%rbp)
|
|
|
\end{lstlisting}
|
|
|
|
|
|
An overview of all of the passes involved in register allocation is
|
|
@@ -3303,10 +3289,10 @@ movq x, y
|
|
|
addq $4, y
|
|
|
movq x, z
|
|
|
addq w, z
|
|
|
-movq y, t.1
|
|
|
-negq t.1
|
|
|
+movq y, t
|
|
|
+negq t
|
|
|
movq z, %rax
|
|
|
-addq t.1, %rax
|
|
|
+addq t, %rax
|
|
|
jmp conclusion
|
|
|
\end{lstlisting}
|
|
|
\end{minipage}
|