Jeremy Siek 6 gadi atpakaļ
vecāks
revīzija
96346c5265
1 mainītis faili ar 107 papildinājumiem un 124 dzēšanām
  1. 107 124
      book.tex

+ 107 - 124
book.tex

@@ -2519,7 +2519,7 @@ registers.  If we think of registers as colors, the register
 allocation problem becomes the widely-studied graph coloring
 problem~\citep{Balakrishnan:1996ve,Rosen:2002bh}.
 
-The reader may be more familiar with the graph coloring problem then he
+The reader may be more familiar with the graph coloring problem than he
 or she realizes; the popular game of Sudoku is an instance of the
 graph coloring problem. The following describes how to build a graph
 out of an initial Sudoku board.
@@ -2549,40 +2549,42 @@ edges for all of the vertices would make the graph unreadable.
 \end{figure}
 
 
-Given that Sudoku is graph coloring, one can use Sudoku strategies to
-come up with an algorithm for allocating registers. For example, one
-of the basic techniques for Sudoku is called Pencil Marks. The idea is
-that you use a process of elimination to determine what numbers no
-longer make sense for a square, and write down those numbers in the
-square (writing very small). For example, if the number $1$ is
-assigned to a square, then by process of elimination, you can write
-the pencil mark $1$ in all the squares in the same row, column, and
-region. Many Sudoku computer games provide automatic support for
-Pencil Marks. This heuristic also reduces the degree of branching in
-the search tree.
-
+Given that Sudoku is an instance of graph coloring, one can use Sudoku
+strategies to come up with an algorithm for allocating registers. For
+example, one of the basic techniques for Sudoku is called Pencil
+Marks. The idea is that you use a process of elimination to determine
+what numbers no longer make sense for a square, and write down those
+numbers in the square (writing very small). For example, if the number
+$1$ is assigned to a square, then by process of elimination, you can
+write the pencil mark $1$ in all the squares in the same row, column,
+and region. Many Sudoku computer games provide automatic support for
+Pencil Marks. 
+%
 The Pencil Marks technique corresponds to the notion of color
 \emph{saturation} due to \cite{Brelaz:1979eu}.  The saturation of a
 vertex, in Sudoku terms, is the set of colors that are no longer
 available. In graph terminology, we have the following definition:
 \begin{equation*}
-  \mathrm{saturation}(u) = \{ c \;|\; \exists v. v \in \mathrm{adjacent}(u)
+  \mathrm{saturation}(u) = \{ c \;|\; \exists v. v \in \mathrm{neighbors}(u)
      \text{ and } \mathrm{color}(v) = c \}
 \end{equation*}
-where $\mathrm{adjacent}(u)$ is the set of vertices adjacent to $u$.
+where $\mathrm{neighbors}(u)$ is the set of vertices that share an
+edge with $u$.
 
 Using the Pencil Marks technique leads to a simple strategy for
 filling in numbers: if there is a square with only one possible number
 left, then write down that number! But what if there are no squares
 with only one possibility left? One brute-force approach is to just
 make a guess. If that guess ultimately leads to a solution, great.  If
-not, backtrack to the guess and make a different guess.  Of course,
-backtracking can be horribly time consuming. One standard way to
-reduce the amount of backtracking is to use the most-constrained-first
-heuristic. That is, when making a guess, always choose a square with
-the fewest possibilities left (the vertex with the highest saturation).
-The idea is that choosing highly constrained squares earlier rather
-than later is better because later there may not be any possibilities.
+not, backtrack to the guess and make a different guess.  One good
+thing about Pencil Marks is that it reduces the degree of branching in
+the search tree. Nevertheless, backtracking can be horribly time
+consuming. One way to reduce the amount of backtracking is to use the
+most-constrained-first heuristic. That is, when making a guess, always
+choose a square with the fewest possibilities left (the vertex with
+the highest saturation).  The idea is that choosing highly constrained
+squares earlier rather than later is better because later there may
+not be any possibilities.
 
 In some sense, register allocation is easier than Sudoku because we
 can always cheat and add more numbers by mapping variables to the
@@ -2633,8 +2635,7 @@ with a dash for their color and an empty set for the saturation.
 \node (x) at (6,0)    {$x:-,\{\}$};
 \node (y) at (3,-1.5) {$y:-,\{\}$};
 \node (z) at (6,-1.5) {$z:-,\{\}$};
-\node (t1) at (9,0)   {$t.1:-,\{\}$};
-\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\node (t1) at (9,-1.5)   {$t.1:-,\{\}$};
 
 \draw (v) to (w);
 \foreach \i in {w,x,y}
@@ -2647,22 +2648,20 @@ with a dash for their color and an empty set for the saturation.
 \draw (z) to (w);
 \draw (z) to (y);
 \draw (t1) to (z);
-\draw (t2) to (t1);
 \end{tikzpicture}
 \]
 We select a maximally saturated vertex and color it $0$. In this case we
-have a 7-way tie, so we arbitrarily pick $y$. The then mark color $0$
-as no longer available for $w$, $x$, and $z$ because they interfere
-with $y$.
+have a 7-way tie, so we arbitrarily pick $t.1$. The then mark color $0$
+as no longer available for $z$ because it interferes
+with $t.1$.
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
 \node (v) at (0,0)    {$v:-,\{\}$};
-\node (w) at (3,0)    {$w:-,\{0\}$};
-\node (x) at (6,0)    {$x:-,\{0\}$};
-\node (y) at (3,-1.5) {$y:0,\{\}$};
+\node (w) at (3,0)    {$w:-,\{\}$};
+\node (x) at (6,0)    {$x:-,\{\}$};
+\node (y) at (3,-1.5) {$y:-,\{\}$};
 \node (z) at (6,-1.5) {$z:-,\{0\}$};
-\node (t1) at (9,0)   {$t.1:-,\{\}$};
-\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\node (t1) at (9,-1.5)   {$t.1:0,\{\}$};
 \draw (v) to (w);
 \foreach \i in {w,x,y}
 {
@@ -2674,23 +2673,19 @@ with $y$.
 \draw (z) to (w);
 \draw (z) to (y);
 \draw (t1) to (z);
-\draw (t2) to (t1);
 \end{tikzpicture}
 \]
-Now we repeat the process, selecting another maximally saturated vertex.
-This time there is a three-way tie between $w$, $x$, and $z$. We color
-$w$ with $1$.
+Now we repeat the process, selecting another maximally saturated
+vertex, which in this case is $z$. We color $z$ with $1$.
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
-\node (v) at (0,0)    {$v:-,\{1\}$};
-\node (w) at (3,0)    {$w:1,\{0\}$};
-\node (x) at (6,0)    {$x:-,\{0,1\}$};
-\node (y) at (3,-1.5) {$y:0,\{1\}$};
-\node (z) at (6,-1.5) {$z:-,\{0,1\}$};
-\node (t1) at (9,0)   {$t.1:-,\{\}$};
-\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\node (v) at (0,0)    {$v:-,\{\}$};
+\node (w) at (3,0)    {$w:-,\{1\}$};
+\node (x) at (6,0)    {$x:-,\{\}$};
+\node (y) at (3,-1.5) {$y:-,\{1\}$};
+\node (z) at (6,-1.5) {$z:1,\{0\}$};
+\node (t1) at (9,-1.5)   {$t.1:0,\{\}$};
 \draw (t1) to (z);
-\draw (t2) to (t1);
 \draw (v) to (w);
 \foreach \i in {w,x,y}
 {
@@ -2703,19 +2698,17 @@ $w$ with $1$.
 \draw (z) to (y);
 \end{tikzpicture}
 \]
-The most saturated vertices are now $x$ and $z$. We color $x$ with the
-next available color which is $2$.
+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 (v) at (0,0)    {$v:-,\{1\}$};
-\node (w) at (3,0)    {$w:1,\{0,2\}$};
-\node (x) at (6,0)    {$x:2,\{0,1\}$};
-\node (y) at (3,-1.5) {$y:0,\{1,2\}$};
-\node (z) at (6,-1.5) {$z:-,\{0,1\}$};
-\node (t1) at (9,0)   {$t.1:-,\{\}$};
-\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\node (v) at (0,0)    {$v:-,\{0\}$};
+\node (w) at (3,0)    {$w:0,\{1\}$};
+\node (x) at (6,0)    {$x:-,\{0,\}$};
+\node (y) at (3,-1.5) {$y:-,\{0,1\}$};
+\node (z) at (6,-1.5) {$z:1,\{0\}$};
+\node (t1) at (9,-1.5)   {$t.1:0,\{\}$};
 \draw (t1) to (z);
-\draw (t2) to (t1);
 \draw (v) to (w);
 \foreach \i in {w,x,y}
 {
@@ -2728,18 +2721,16 @@ next available color which is $2$.
 \draw (z) to (y);
 \end{tikzpicture}
 \]
-Vertex $z$ is the next most highly saturated, so we color $z$ with $2$.
+Vertex $y$ is the next most highly saturated, so we color $y$ with $2$.
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
-\node (v) at (0,0)   {$v:-,\{1\}$};
-\node (w) at (3,0)   {$w:1,\{0,2\}$};
-\node (x) at (6,0)   {$x:2,\{0,1\}$};
-\node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
-\node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
-\node (t1) at (9,0)   {$t.1:-,\{2\}$};
-\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\node (v) at (0,0)   {$v:-,\{0\}$};
+\node (w) at (3,0)   {$w:0,\{1,2\}$};
+\node (x) at (6,0)   {$x:-,\{0,2\}$};
+\node (y) at (3,-1.5)  {$y:2,\{0,1\}$};
+\node (z) at (6,-1.5)  {$z:1,\{0,2\}$};
+\node (t1) at (9,-1.5)   {$t.1:0,\{\}$};
 \draw (t1) to (z);
-\draw (t2) to (t1);
 \draw (v) to (w);
 \foreach \i in {w,x,y}
 {
@@ -2752,19 +2743,16 @@ Vertex $z$ is the next most highly saturated, so we color $z$ with $2$.
 \draw (z) to (y);
 \end{tikzpicture}
 \]
-We have a 2-way tie between $v$ and $t.1$. We choose to color $v$ with
-$0$.
+Now $x$ has the highest saturation, so we color it $1$.
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
-\node (v) at (0,0)   {$v:0,\{1\}$};
-\node (w) at (3,0)   {$w:1,\{0,2\}$};
-\node (x) at (6,0)   {$x:2,\{0,1\}$};
-\node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
-\node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
-\node (t1) at (9,0)   {$t.1:-,\{2\}$};
-\node (t2) at (9,-1.5) {$t.2:-,\{\}$};
+\node (v) at (0,0)   {$v:-,\{0\}$};
+\node (w) at (3,0)   {$w:0,\{1,2\}$};
+\node (x) at (6,0)   {$x:1,\{0,2\}$};
+\node (y) at (3,-1.5)  {$y:2,\{0,1\}$};
+\node (z) at (6,-1.5)  {$z:1,\{0,2\}$};
+\node (t1) at (9,-1.5)   {$t.1:0,\{\}$};
 \draw (t1) to (z);
-\draw (t2) to (t1);
 \draw (v) to (w);
 \foreach \i in {w,x,y}
 {
@@ -2777,19 +2765,16 @@ $0$.
 \draw (z) to (y);
 \end{tikzpicture}
 \]
-In the last two steps of the algorithm, we color $t.1$ with $0$
-then $t.2$ with $1$.
+In the last two steps of the algorithm, we color $v$ with $1$.
 \[
 \begin{tikzpicture}[baseline=(current  bounding  box.center)]
-\node (v) at (0,0)   {$v:0,\{1\}$};
-\node (w) at (3,0)   {$w:1,\{0,2\}$};
-\node (x) at (6,0)   {$x:2,\{0,1\}$};
-\node (y) at (3,-1.5)  {$y:0,\{1,2\}$};
-\node (z) at (6,-1.5)  {$z:2,\{0,1\}$};
-\node (t1) at (9,0)   {$t.1:0,\{2,1\}$};
-\node (t2) at (9,-1.5) {$t.2:1,\{0\}$};
+\node (v) at (0,0)   {$v:1,\{0\}$};
+\node (w) at (3,0)   {$w:0,\{1,2\}$};
+\node (x) at (6,0)   {$x:1,\{0,2\}$};
+\node (y) at (3,-1.5)  {$y:2,\{0,1\}$};
+\node (z) at (6,-1.5)  {$z:1,\{0,2\}$};
+\node (t1) at (9,-1.5)   {$t.1:0,\{\}$};
 \draw (t1) to (z);
-\draw (t2) to (t1);
 \draw (v) to (w);
 \foreach \i in {w,x,y}
 {
@@ -2803,65 +2788,63 @@ then $t.2$ with $1$.
 \end{tikzpicture}
 \]
 
-
 With the coloring complete, we can finalize the assignment of
 variables to registers and stack locations. Recall that if we have $k$
 registers, we map the first $k$ colors to registers and the rest to
-stack locations.  Suppose for the moment that we just have one extra
-register to use for register allocation, just \key{rbx}. Then the
-following is the mapping of colors to registers and stack allocations.
+stack locations.  Suppose for the moment that we have just one
+register to use for register allocation, \key{rcx}. Then the following
+is the mapping of colors to registers and stack allocations.
 \[
-  \{ 0 \mapsto \key{\%rbx}, \; 1 \mapsto \key{-8(\%rbp)}, \; 2 \mapsto \key{-16(\%rbp)}, \ldots \}
+  \{ 0 \mapsto \key{\%rcx}, \; 1 \mapsto \key{-8(\%rbp)}, \; 2 \mapsto \key{-16(\%rbp)}, \ldots \}
 \]
 Putting this mapping together with the above coloring of the variables, we
 arrive at the assignment:
 \begin{gather*}
-  \{ v \mapsto \key{\%rbx}, \,
-  w \mapsto \key{-8(\%rbp)},  \,
-  x \mapsto \key{-16(\%rbp)}, \,
-  y \mapsto \key{\%rbx},  \,
-  z\mapsto \key{-16(\%rbp)}, \\
-  t.1\mapsto \key{\%rbx} ,\,
-  t.2\mapsto \key{-8(\%rbp)} \}
+  \{ v \mapsto \key{-8(\%rbp)}, \,
+  w \mapsto \key{\%rcx},  \,
+  x \mapsto \key{-8(\%rbp)}, \,
+  y \mapsto \key{-16(\%rbp)},  \,
+  z\mapsto \key{-8(\%rbp)}, 
+  t.1\mapsto \key{\%rcx} \}
 \end{gather*}
 Applying this assignment to our running example
 (Figure~\ref{fig:reg-eg}) yields the program on the right.\\
 % why frame size of 32? -JGS
 \begin{minipage}{0.4\textwidth}
 \begin{lstlisting}
-  (program (v w x y z)
-    (movq (int 1) (var v))
-    (movq (int 46) (var w))
-    (movq (var v) (var x))
-    (addq (int 7) (var x))
-    (movq (var x) (var y))
-    (addq (int 4) (var y))
-    (movq (var x) (var z))
-    (addq (var w) (var z))
-    (movq (var y) (var t.1))
-    (negq (var t.1))
-    (movq (var z) (var t.2))
-    (addq (var t.1) (var t.2))
-    (movq (var t.2) (reg rax)))
+(block ()
+  (movq (int 1) (var v))
+  (movq (int 46) (var w))
+  (movq (var v) (var x))
+  (addq (int 7) (var x))
+  (movq (var x) (var y))
+  (addq (int 4) (var y))
+  (movq (var x) (var z))
+  (addq (var w) (var z))
+  (movq (var y) (var t.1))
+  (negq (var t.1))
+  (movq (var z) (reg rax))
+  (addq (var t.1) (reg rax))
+  (jmp conclusion))
 \end{lstlisting}
 \end{minipage}
 $\Rightarrow$
 \begin{minipage}{0.45\textwidth}
 \begin{lstlisting}
-(program 16
-  (movq (int 1) (reg rbx))
-  (movq (int 46) (deref rbp -8))
-  (movq (reg rbx) (deref rbp -16))
-  (addq (int 7) (deref rbp -16))
-  (movq (deref rbp -16) (reg rbx))
-  (addq (int 4) (reg rbx))
-  (movq (deref rbp -16) (deref rbp -16))
-  (addq (deref rbp -8) (deref rbp -16))
-  (movq (reg rbx) (reg rbx))
-  (negq (reg rbx))
-  (movq (deref rbp -16) (deref rbp -8))
-  (addq (reg rbx) (deref rbp -8))
-  (movq (deref rbp -8) (reg rax)))
+(block ()
+  (movq (int 1) (reg rdx))
+  (movq (int 46) (reg rcx))
+  (movq (reg rdx) (reg rdx))
+  (addq (int 7) (reg rdx))
+  (movq (reg rdx) (deref rbp -8))
+  (addq (int 4) (deref rbp -8))
+  (movq (reg rdx) (reg rdx))
+  (addq (reg rcx) (reg rdx))
+  (movq (deref rbp -8) (reg rcx))
+  (negq (reg rcx))
+  (movq (reg rdx) (reg rax))
+  (addq (reg rcx) (reg rax))
+  (jmp conclusion))
 \end{lstlisting}
 \end{minipage}