report.tex 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. \documentclass[10pt,a4paper]{article}
  2. \usepackage[latin1]{inputenc}
  3. \usepackage{amsmath}
  4. \usepackage{amsfonts}
  5. \usepackage{amssymb}
  6. \usepackage{booktabs}
  7. \usepackage{graphicx}
  8. \usepackage{listings}
  9. \usepackage{subfigure}
  10. \usepackage{float}
  11. \usepackage{hyperref}
  12. \title{Peephole Optimizer}
  13. \author{Jayke Meijer (6049885), Richard Torenvliet (6138861), Tadde\"us Kroes
  14. (6054129)}
  15. \begin{document}
  16. \maketitle
  17. \pagebreak
  18. \tableofcontents
  19. \pagebreak
  20. \section{Introduction}
  21. The goal of the assignment is to implement the optimization stage of the
  22. compiler. To reach this goal the parser and the optimizer part of the compiler
  23. have to be implemented.
  24. The output of the xgcc cross compiler on a C program is our input. The output
  25. of the xgcc cross compiler is in the form of Assembly code, but not optimized.
  26. Our assignment includes a number of C programs. An important part of the
  27. assignment is parsing the data. Parsing the data is done with Lex and Yacc. The
  28. Lexer is a program that finds keywords that meets the regular expression
  29. provided in the Lexer. After the Lexer, the Yaccer takes over. Yacc can turn
  30. the keywords in to an action.
  31. \section{Design}
  32. There are two general types of of optimizations of the assembly code, global
  33. optimizations and optimizations on a so-called basic block. These optimizations
  34. will be discussed seperatly
  35. \subsection{Global optimizations}
  36. We only perform one global optimization, which is optimizing branch-jump
  37. statements. The unoptimized Assembly code contains sequences of code of the
  38. following structure:
  39. \begin{lstlisting}
  40. beq ...,$Lx
  41. j $Ly
  42. $Lx: ...\end{lstlisting}
  43. This is inefficient, since there is a jump to a label that follows this code.
  44. It would be more efficient to replace the branch statement with a \texttt{bne}
  45. (the opposite case) to the label used in the jump statement. This way the jump
  46. statement can be eliminated, since the next label follows anyway. The same can
  47. of course be done for the opposite case, where a \texttt{bne} is changed into a
  48. \texttt{beq}.
  49. Since this optimization is done between two series of codes with jumps and
  50. labels, we can not perform this code during the basic block optimizations. The
  51. reason for this will become clearer in the following section.
  52. \subsection{Basic Block Optimizations}
  53. Optimizations on basic blocks are a more important part of the optimizer.
  54. First, what is a basic block? A basic block is a sequence of statements
  55. guaranteed to be executed in that order, and that order alone. This is the case
  56. for a piece of code not containing any branches or jumps.
  57. To create a basic block, you need to define what is the leader of a basic
  58. block. We call a statement a leader if it is either a jump/branch statement, or
  59. the target of such a statement. Then a basic block runs from one leader
  60. (not including this leader) until the next leader (including this leader). !!!!
  61. There are quite a few optimizations we perform on these basic blocks, so we
  62. will describe the types of optimizations here in stead of each optimization.
  63. \subsubsection*{Standard peephole optimizations}
  64. These are optimizations that simply look for a certain statement or pattern of
  65. statements, and optimize these. For example,
  66. \begin{lstlisting}
  67. mov $regA,$regB
  68. instr $regA, $regA,...
  69. \end{lstlisting}
  70. can be optimized into
  71. \begin{lstlisting}
  72. instr $regA, $regB,...
  73. \end{lstlisting}
  74. since the register \texttt{\$regA} gets overwritten by the second instruction
  75. anyway, and the instruction can easily use \texttt{\$regB} in stead of
  76. \texttt{\$regA}. There are a few more of these cases, which are the same as
  77. those described on the practicum page
  78. \footnote{\url{http://staff.science.uva.nl/~andy/compiler/prac.html}} and in
  79. Appendix \ref{opt}.
  80. \subsubsection*{Common subexpression elimination}
  81. A more advanced optimization is common subexpression elimination. This means
  82. that expensive operations as a multiplication or addition are performed only
  83. once and the result is then `copied' into variables where needed.
  84. A standard method for doing this is the creation of a DAG or Directed Acyclic
  85. Graph. However, this requires a fairly advanced implementation. Our
  86. implementation is a slightly less fancy, but easier to implement.
  87. We search from the end of the block up for instructions that are eligible for
  88. CSE. If we find one, we check further up in the code for the same instruction,
  89. and add that to a temporary storage list. This is done until the beginning of
  90. the block or until one of the arguments of this expression is assigned. Now all
  91. occurences of this expression can be replaced by a move of a new variable that
  92. is generated above the first occurence, which contains the value of the
  93. expression.
  94. This is a less efficient method, but because the basic blocks are in general
  95. not very large and the exectution time of the optimizer is not a primary
  96. concern, this is not a big problem.
  97. \section{Implementation}
  98. We decided to implement the optimization in Python. We chose this programming
  99. language because Python is an easy language to manipulate strings, work
  100. object-oriented etc.
  101. It turns out that a Lex and Yacc are also available as a Python module,
  102. named PLY(Python Lex-Yacc). This allows us to use one language, Python, instead
  103. of two, i.e. C and Python. Also no debugging is needed in C, only in Python
  104. which makes our assignment more feasible.
  105. The program has three steps, parsing the Assembly code into a datastructure we
  106. can use, the so-called Intermediate Representation, performing optimizations on
  107. this IR and writing the IR back to Assembly.
  108. \subsection{Parsing with PLY}
  109. \subsection{Optimizations}
  110. \subsection{Writing}
  111. \section{Results}
  112. \subsection{pi.c}
  113. \subsection{arcron.c}
  114. \subsection{whet.c}
  115. \subsection{slalom.c}
  116. \subsection{clinpack.c}
  117. \section{Conclusion}
  118. \appendix
  119. \section{Total list of optimizations}
  120. \label{opt}
  121. \textbf{Global optimizations}
  122. \begin{tabular}{| c c c |}
  123. \hline
  124. \begin{lstlisting}
  125. beq ...,$Lx
  126. j $Ly
  127. $Lx: ...\end{lstlisting} & $\Rightarrow$ & \begin{lstlisting}
  128. bne ...,$Ly
  129. $Lx: ...\end{lstlisting}\\
  130. \hline
  131. \begin{lstlisting}
  132. bne ...,$Lx
  133. j $Ly
  134. $Lx: ...\end{lstlisting} & $\Rightarrow$ & \begin{lstlisting}
  135. beq ...,$Ly
  136. $Lx: ...\end{lstlisting}\\
  137. \hline
  138. \end{tabular}\\
  139. \\
  140. \textbf{Simple basic block optimizations}
  141. \begin{tabular}{|c c c|}
  142. \hline
  143. \begin{lstlisting}
  144. beq ...,$Lx
  145. j $Ly
  146. $Lx: ...\end{lstlisting} & $\Rightarrow$ & \begin{lstlisting}
  147. bne ...,$Ly
  148. $Lx: ...\end{lstlisting}\\
  149. \hline
  150. \end{tabular}\\
  151. \\
  152. \textbf{Advanced basic block optimizations}
  153. \end{document}