\documentclass[10pt,a4paper]{article}
\usepackage{float}

% aliases
\newcommand{\tab}{\hspace*{1cm}}

\title{Compiler Optimalisation Assignment 1: Loops}
\author{Sander van Veen (6167969)}

\begin{document}

\maketitle

\section{Natural loops} % (fold)
\label{sec:Natural loops}

Given flow graph $G = (V,E,v_0)$, where $V$ is a collection of all vertices
(typically basic blocks), $E$ is a collection of edges (which represent the
relation between the basic blocks) and $v_0$ is the entry node. A natural loop
has the following definition:

\begin{itemize}
    \item A loop has a single entry point, which dominates the loop.
    \item There must be a path back to the entry point of the loop.
    \item Loops can be found by searching for edges of which their heads
          dominate their tails, also know as backedges.
    \item Given an backedge $n \rightarrow d$, the natural loop is $d$ plus the
          nodes that can reach $n$ without going through $d$.
\end{itemize}

\noindent \textbf{Assignment: Give the natural loop(s) of the assignment's flow graph.}
\\
To answer this assignment, I'll go through the definition of a natural loop and
discard the basic blocks which do not met the criteria.

\begin{enumerate}
    \item A loop has a single entry point, which dominates the loop.\\
          Blocks $B_1$ and $B_2$ met this criteria. Block $B_5$
          does not, since $B_3$ and $B_4$ both have an edge to $B_5$, which
          makes $B_5$ a multiple entrance point. $B_3$ and $B_4$ do not
          dominate the loop, since they are both a successor of $B_2$.
    \item There must be a path back to the entry point of the loop.\\
          Block $B_1$ does not have a path back to itself, so only block $B_2$
          remains.
    \item Loops can be found by searching for edges of which their heads
          dominate their tails, also know as backedges. \\
          Block $B_2$ has a backedge $B_5 \rightarrow B_2$.
    \item Given an backedge $n \rightarrow d$, the natural loop is $d$ plus the
          nodes that can reach $n$ without going through $d$.\\ Since $B_2$ is
          the only remaining header, the assignment's flow graph has one
          natural loop: $\{B_2, B_3, B_4, B_5\}$. Note: $B_6$ is not part of
          the loop, because it is not stated if $B_6$ has a path back to $B_2$.
\end{enumerate}

% section Natural loops (end)

\section{Reaching definition} % (fold)
\label{sec:Reaching definition}

\textbf{Assignment: Give the \texttt{gen} and \texttt{kill} sets of the basic blocks.}

%A typical dataflow equation has the form:
%\[\texttt{out}[S] = \texttt{gen}[S] \cup ( \texttt{in}[S] - \texttt{kill}[S])\]

\begin{table}[H]
\begin{tabular}{|l|l|l|} \hline
$B_i$ & \texttt{gen} $[B_i]$  & \texttt{kill} $[B_i]$ \\ \hline
1     & $\{d_1,d_2,d_3,d_4\}$ & $\{d_5,d_6,d_7,d_8,d_9\}$ \\ \hline
2     & $\{d_5\}$             & $\{d_4,d_7,d_8\}$ \\ \hline
3     & $\{d_6,7\}$           & $\{d_1,d_3,d_4,d_5,d_8\}$ \\ \hline
4     & $\{d_8\}$             & $\{d_4,d_5\}$ \\ \hline
5     & $\{d_9\}$             & $\{\}$ \\ \hline
6     & undefined             & undefined \\ \hline
\end{tabular}
\end{table}

\noindent Note: I'm not sure about the \texttt{gen} and \texttt{kill} set of
$B_1$. I noticed $d_3$ will overwrite $d_1$, but I thought that doesn't mean
that $d_1$ should be added to this \texttt{kill} set.
% section Reaching definition (end)

\section{Iterative algorithm for reaching definitions} % (fold)
\label{sec:Iterative algorithm for reaching definitions}

\textbf{algorithm} \\
for each block B: \\
\tab out[B] = gen[B] \\
\\
changed = true \\
while changed: \\
\tab changed = false \\
\tab for each block B: \\
\tab \tab in[B] = $\bigcup_{p \in pred(B)}$ out[p] \\
\tab \tab oldout = out[B] \\
\tab \tab out[B] = gen[B] $\cup$ (in[B] $-$ kill[B]) \\
\tab \tab if out[B] $\neq$ oldout: \\
\tab \tab \tab changed = true \\

\noindent Using this algorithm, the following result is generated.

\begin{table}[H]
\begin{tabular}{|l|l|l|l|l|l|} \hline
      & start   & first     & first       & second        & second      \\ \hline
$B_i$ & \texttt{out}[$B_i$] & \texttt{in}[$B_i$] & \texttt{out}[$B_i$] &
    \texttt{in}[$B_i$] & \texttt{out}[$B_i$] \\ \hline
1     & 1,2,3,4 & $\emptyset$ & 1,2,3,4     & $\emptyset$   & 1,2,3,4     \\ \hline
2     & 5       & 1,2,3,4,9   & 1,2,3,5,9   & 1,2,3,4,5,8,9 & 1,2,3,5,9   \\ \hline
3     & 6,7     & 1,2,3,5,9   & 2,6,7,9     & 1,2,3,5,9     & 2,6,7,9     \\ \hline
4     & 8       & 1,2,3,5,9   & 1,2,3,8,9   & 1,2,3,5,9     & 1,2,3,8,9   \\ \hline
5     & 9       & 1,2,3,5,8,9 & 1,2,3,5,8,9 & 1,2,3,5,8,9   & 1,2,3,5,8,9 \\ \hline
6     & undef.  & 1,2,3,8,9   & undef.      & 1,2,3,8,9     & undef.      \\ \hline
\end{tabular}
\end{table}

% section Iterative algorithm for reaching definitions (end)

\end{document}
