report.tex 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. \tableofcontents
  18. \pagebreak
  19. \section{Introduction}
  20. The goal of the assignment is to implement the optimization stage of the
  21. compiler. To reach this goal the parser and the optimizer part of the compiler
  22. have to be implemented.
  23. The output of the xgcc cross compiler on a C program is our input. The output
  24. of the xgcc cross compiler is in the form of Assembly code, but not optimized.
  25. Our assignment includes a number of C programs. An important part of the
  26. assignment is parsing the data. Parsing the data is done with Lex and Yacc. The
  27. Lexer is a program that finds keywords that meets the regular expression
  28. provided in the Lexer. After the Lexer, the Yaccer takes over. Yacc can turn
  29. the keywords in to an action.
  30. \section{Design}
  31. There are two general types of optimizations of the assembly code, global
  32. optimizations and optimizations on a so-called basic block. These optimizations
  33. will be discussed separately
  34. \subsection{Global optimizations}
  35. We only perform one global optimization, which is optimizing branch-jump
  36. statements. The unoptimized Assembly code contains sequences of code of the
  37. following structure:
  38. \begin{verbatim}
  39. beq ...,$Lx
  40. j $Ly
  41. $Lx: ...
  42. \end{verbatim}
  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 until
  60. the next 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{verbatim}
  67. mov $regA,$regB
  68. instr $regA, $regA,...
  69. \end{verbatim}
  70. can be optimized into
  71. \begin{verbatim}
  72. instr $regA, $regB,...
  73. \end{verbatim}
  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. \begin{verbatim}
  85. addu $2,$4,$3 addu = $t1, $4, $3
  86. ... mov = $2, $t1
  87. ... -> ...
  88. ... ...
  89. addu $5,$4,$3 mov = $4, $t1
  90. \end{verbatim}
  91. A standard method for doing this is the creation of a DAG or Directed Acyclic
  92. Graph. However, this requires a fairly advanced implementation. Our
  93. implementation is a slightly less fancy, but easier to implement.
  94. We search from the end of the block up for instructions that are eligible for
  95. CSE. If we find one, we check further up in the code for the same instruction,
  96. and add that to a temporary storage list. This is done until the beginning of
  97. the block or until one of the arguments of this expression is assigned.
  98. We now add the instruction above the first use, and write the result in a new
  99. variable. Then all occurrences of this expression can be replaced by a move of
  100. from new variable into the original destination variable of the instruction.
  101. This is a less efficient method then the DAG, but because the basic blocks are
  102. in general not very large and the execution time of the optimizer is not a
  103. primary concern, this is not a big problem.
  104. \subsubsection*{Fold constants}
  105. Constant folding is an optimization where the outcome of arithmetics are
  106. calculated at compile time. If a value x is assigned to a certain value, lets
  107. say 10, than all next occurences of \texttt{x} are replaced by 10 until a
  108. redefinition of x. Arithmetics in Assembly are always performed between two
  109. variables or a variable and a constant. If this is not the case the calculation
  110. is not possible. See the example for a more clear explanation of constant
  111. folding(will come). In other words until the current definition of \texttt{x}
  112. becomes dead. Therefore reaching definitions analysis is needed.
  113. \subsubsection*{Copy propagation}
  114. Copy propagation `unpacks' a move instruction, by replacing its destination
  115. address with its source address in the code following the move instruction.
  116. This is not a direct optimization, but this does allow for a more effective
  117. dead code elimination.
  118. The code of the block is checked linearly. When a move operation is
  119. encountered, the source and destination address of this move are stored. When
  120. a normal operation with a source and a destination address are found, a number
  121. of checks are performed.
  122. The first check is whether the destination address is stored as a destination
  123. address of a move instruction. If so, this move instruction is no longer valid,
  124. so the optimizations can not be done. Otherwise, continue with the second
  125. check.
  126. In the second check, the source address is compared to the destination
  127. addresses of all still valid move operations. If these are the same, in the
  128. current operation the found source address is replaced with the source address
  129. of the move operation.
  130. An example would be the following:
  131. \begin{verbatim}
  132. move $regA, $regB move $regA, $regB
  133. ... ...
  134. Code not writing $regA, -> ...
  135. $regB ...
  136. ... ...
  137. addu $regC, $regA, ... addu $regC, $regB, ...
  138. \end{verbatim}
  139. This code shows that \texttt{\$regA} is replaced with \texttt{\$regB}. This
  140. way, the move instruction might have become useless, and it will then be
  141. removed by the dead code elimination.
  142. \subsubsection*{Algebraic transformations}
  143. Some expression can easily be replaced with more simple once if you look at
  144. what they are saying algebraically. An example is the statement $x = y + 0$, or
  145. in Assembly \texttt{addu \$1, \$2, 0}. This can easily be changed into $x = y$
  146. or \texttt{move \$1, \$2}.
  147. Another case is the multiplication with a power of two. This can be done way
  148. more efficiently by shifting left a number of times. An example:
  149. \texttt{mult \$regA, \$regB, 4 -> sll \$regA, \$regB, 2}. We perform this
  150. optimization for any multiplication with a power of two.
  151. There are a number of such cases, all of which are once again stated in
  152. appendix \ref{opt}.
  153. \section{Implementation}
  154. We decided to implement the optimization in Python. We chose this programming
  155. language because Python is an easy language to manipulate strings, work
  156. object-oriented etc.
  157. It turns out that a Lex and Yacc are also available as a Python module,
  158. named PLY(Python Lex-Yacc). This allows us to use one language, Python, instead
  159. of two, i.e. C and Python. Also no debugging is needed in C, only in Python
  160. which makes our assignment more feasible.
  161. The program has three steps, parsing the Assembly code into a datastructure we
  162. can use, the so-called Intermediate Representation, performing optimizations on
  163. this IR and writing the IR back to Assembly.
  164. \subsection{Parsing}
  165. The parsing is done with PLY, which allows us to perform Lex-Yacc tasks in
  166. Python by using a Lex-Yacc like syntax. This way there is no need to combine
  167. languages like we should do otherwise since Lex and Yacc are coupled with C.
  168. The decision was made to not recognize exactly every possible instruction in
  169. the parser, but only if something is for example a command, a comment or a gcc
  170. directive. We then transform per line to an object called a Statement. A
  171. statement has a type, a name and optionally a list of arguments. These
  172. statements together form a statement list, which is placed in another object
  173. called a Block. In the beginning there is one block for the entire program, but
  174. after global optimizations this will be separated in several blocks that are
  175. the basic blocks.
  176. \subsection{Optimizations}
  177. The optimizations are done in two different steps. First the global
  178. optimizations are performed, which are only the optimizations on branch-jump
  179. constructions. This is done repeatedly until there are no more changes.
  180. After all possible global optimizations are done, the program is separated into
  181. basic blocks. The algorithm to do this is described earlier, and means all
  182. jump and branch instructions are called leaders, as are their targets. A basic
  183. block then goes from leader to leader.
  184. After the division in basic blocks, optimizations are performed on each of
  185. these basic blocks. This is also done repeatedly, since some times several
  186. steps can be done to optimize something.
  187. \subsection{Writing}
  188. Once all the optimizations have been done, the IR needs to be rewritten into
  189. Assembly code. After this step the xgcc crosscompiler can make binary code from
  190. the generated Assembly code.
  191. The writer expects a list of statements, so first the blocks have to be
  192. concatenated again into a list. After this is done, the list is passed on to
  193. the writer, which writes the instructions back to Assembly and saves the file
  194. so we can let xgcc compile it.
  195. \section{Testing}
  196. Of course, it has to be guaranteed that the optimized code still functions
  197. exactly the same as the none-optimized code. To do this, testing is an
  198. important part of out program. We have two stages of testing. The first stage
  199. is unit testing. The second stage is to test whether the compiled code has
  200. exactly the same output.
  201. \subsection{Unit testing}
  202. For almost every piece of important code, unit tests are available. Unit tests
  203. give the possibility to check whether each small part of the program, for
  204. instance each small function, is performing as expected. This way bugs are
  205. found early and very exactly. Otherwise, one would only see that there is a
  206. mistake in the program, not knowing where this bug is. Naturally, this means
  207. debugging is a lot easier.
  208. The unit tests can be run by executing \texttt{make test} in the root folder of
  209. the project. This does require the \texttt{textrunner} module.
  210. Also available is a coverage report. This report shows how much of the code has
  211. been unit tested. To make this report, the command \texttt{make coverage} can
  212. be run in the root folder. The report is than added as a folder \emph{coverage}
  213. in which a \emph{index.html} can be used to see the entire report.
  214. \subsection{Ouput comparison}
  215. In order to check whether the optimization does not change the functioning of
  216. the program, the output of the provided benchmark programs has to be compared
  217. to the output after optimization. If any of these outputs is not equal to the
  218. original output, our optimizations are to aggressive, or there is a bug
  219. somewhere in the code.
  220. \section{Results}
  221. The following results have been obtained:\\
  222. \begin{tabular}{|c|c|c|c|c|c|}
  223. \hline
  224. Benchmark & Original & Optimized & Original & Optimized & Performance \\
  225. & Instructions & instructions & cycles & cycles & boost(cycles)\\
  226. \hline
  227. pi & 134 & & 13011 & & \\
  228. acron & & & 4435687 & & \\
  229. dhrystone & & & 2887710 & & \\
  230. whet & & & 2864089 & & \\
  231. slalom & & & 27270 & & \\
  232. clinpack & & & 1547941 & & \\
  233. \hline
  234. \end{tabular}\\
  235. \\
  236. The imput for slalom was 1000 seconds and a minimum of $n = 100$
  237. \section{Conclusion}
  238. \appendix
  239. \section{List of all optimizations}
  240. \label{opt}
  241. \textbf{Global optimizations}
  242. \begin{verbatim}
  243. beq ...,$Lx bne ...,$Ly
  244. j $Ly -> $Lx: ...
  245. $Lx: ...
  246. bne ...,$Lx beq ...,$Ly
  247. j $Ly -> $Lx: ...
  248. $Lx: ...
  249. \end{verbatim}
  250. \textbf{Standard basic block optimizations}
  251. \begin{verbatim}
  252. mov $regA,$regA -> --- // remove it
  253. mov $regA,$regB -> instr $regA, $regB,...
  254. instr $regA, $regA,...
  255. instr $regA,... instr $4,...
  256. mov [$4-$7], $regA -> jal XXX
  257. jal XXX
  258. sw $regA,XXX -> sw $regA, XXX
  259. ld $regA,XXX
  260. shift $regA,$regA,0 -> --- // remove it
  261. add $regA,$regA,X -> lw ...,X($regA)
  262. lw ...,0($regA)
  263. \end{verbatim}
  264. \textbf{Advanced basic block optimizations}
  265. \begin{verbatim}
  266. # Common subexpression elimination
  267. addu $regA, $regB, 4 addu $regD, $regB, 4
  268. ... move $regA, $regD
  269. Code not writing $regB -> ...
  270. ... ...
  271. addu $regC, $regB, 4 move $regC, $regD
  272. # Constant folding
  273. # Copy propagation
  274. move $regA, $regB move $regA, $regB
  275. ... ...
  276. Code not writing $regA, -> ...
  277. $regB ...
  278. ... ...
  279. addu $regC, $regA, ... addu $regC, $regB, ...
  280. # Algebraic transformations
  281. addu $regA, $regB, 0 -> move $regA, $regB
  282. subu $regA, $regB, 0 -> move $regA, $regB
  283. mult $regA, $regB, 1 -> move $regA, $regB
  284. mult $regA, $regB, 0 -> li $regA, 0
  285. mult $regA, $regB, 2 -> sll $regA, $regB, 1
  286. \end{verbatim}
  287. \end{document}