Commit 47ddae61 authored by Taddeus Kroes's avatar Taddeus Kroes

Merge branch 'master' of github.com:taddeus/peephole

parents d9813d87 28c6e498
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
# -mgas -mgpOPT # -mgas -mgpOPT
# Cc1 arguments (-G value = 8, Cpu = default, ISA = 1): # Cc1 arguments (-G value = 8, Cpu = default, ISA = 1):
# -quiet -dumpbase -o # -quiet -dumpbase -O0 -o
gcc2_compiled.: gcc2_compiled.:
__gnu_compiled_c: __gnu_compiled_c:
......
...@@ -35,7 +35,7 @@ the keywords in to an action. ...@@ -35,7 +35,7 @@ the keywords in to an action.
\section{Design} \section{Design}
There are two general types of of optimizations of the assembly code, global There are two general types of optimizations of the assembly code, global
optimizations and optimizations on a so-called basic block. These optimizations optimizations and optimizations on a so-called basic block. These optimizations
will be discussed separately will be discussed separately
...@@ -99,6 +99,16 @@ Appendix \ref{opt}. ...@@ -99,6 +99,16 @@ Appendix \ref{opt}.
A more advanced optimization is common subexpression elimination. This means A more advanced optimization is common subexpression elimination. This means
that expensive operations as a multiplication or addition are performed only that expensive operations as a multiplication or addition are performed only
once and the result is then `copied' into variables where needed. once and the result is then `copied' into variables where needed.
\begin{verbatim}
addu $2,$4,$3 addu = $t1, $4, $3
... mov = $2, $t1
... -> ...
... ...
addu $5,$4,$3 mov = $4, $t1
\end{verbatim}
A standard method for doing this is the creation of a DAG or Directed Acyclic A standard method for doing this is the creation of a DAG or Directed Acyclic
Graph. However, this requires a fairly advanced implementation. Our Graph. However, this requires a fairly advanced implementation. Our
...@@ -112,27 +122,13 @@ We now add the instruction above the first use, and write the result in a new ...@@ -112,27 +122,13 @@ We now add the instruction above the first use, and write the result in a new
variable. Then all occurrences of this expression can be replaced by a move of variable. Then all occurrences of this expression can be replaced by a move of
from new variable into the original destination variable of the instruction. from new variable into the original destination variable of the instruction.
This is a less efficient method then the DAG, but because the basic blocks are This is a less efficient method then the dag, but because the basic blocks are
in general not very large and the execution time of the optimizer is not a in general not very large and the execution time of the optimizer is not a
primary concern, this is not a big problem. primary concern, this is not a big problem.
\subsubsection*{Constant folding} \subsubsection*{Fold constants}
Another optimization is to do constant folding. Constant folding is replacing
a expensive step like addition with a more simple step like loading a constant.
Of course, this is not always possible. It is possible in cases where you apply
an operation on two constants, or a constant and a variable of which you know
for sure that it always has a certain value at that point. For example:
\begin{verbatim}
li $regA, 1 li $regA, 1
addu $regB, $regA, 2 -> li $regB, 3
\end{verbatim}
Of course, if \texttt{\$regA} is not used after this, it can be removed, which
will be done by the dead code elimination.
One problem we encountered with this is that the use of a \texttt{li} is that
the program often also stores this in the memory, so we had to check whether
this was necessary here as well.
\subsubsection*{Copy propagation} \subsubsection*{Copy propagation}
...@@ -159,11 +155,12 @@ of the move operation. ...@@ -159,11 +155,12 @@ of the move operation.
An example would be the following: An example would be the following:
\begin{verbatim} \begin{verbatim}
move $regA, $regB move $regA, $regB move $regA, $regB move $regA, $regB
... ... ... ...
Code not writing $regA, $regB -> ... Code not writing $regA, -> ...
... ... $regB ...
addu $regC, $regA, ... addu $regC, $regB, ... ... ...
addu $regC, $regA, ... addu $regC, $regB, ...
\end{verbatim} \end{verbatim}
This code shows that \texttt{\$regA} is replaced with \texttt{\$regB}. This This code shows that \texttt{\$regA} is replaced with \texttt{\$regB}. This
way, the move instruction might have become useless, and it will then be way, the move instruction might have become useless, and it will then be
...@@ -171,18 +168,7 @@ removed by the dead code elimination. ...@@ -171,18 +168,7 @@ removed by the dead code elimination.
\subsubsection*{Algebraic transformations} \subsubsection*{Algebraic transformations}
Some expression can easily be replaced with more simple once if you look at
what they are saying algebraically. An example is the statement $x = y + 0$, or
in Assembly \texttt{addu \$1, \$2, 0}. This can easily be changed into $x = y$
or \texttt{move \$1, \$2}.
Another case is the multiplication with a power of two. This can be done way
more efficiently by shifting left a number of times. An example:
\texttt{mult \$regA, \$regB, 4 -> sll \$regA, \$regB, 2}. We perform this
optimization for any multiplication with a power of two.
There are a number of such cases, all of which are once again stated in
appendix \ref{opt}.
\section{Implementation} \section{Implementation}
...@@ -206,7 +192,7 @@ languages like we should do otherwise since Lex and Yacc are coupled with C. ...@@ -206,7 +192,7 @@ languages like we should do otherwise since Lex and Yacc are coupled with C.
The decision was made to not recognize exactly every possible instruction in The decision was made to not recognize exactly every possible instruction in
the parser, but only if something is for example a command, a comment or a gcc the parser, but only if something is for example a command, a comment or a gcc
directive. We then transform per line to a object called a Statement. A directive. We then transform per line to an object called a Statement. A
statement has a type, a name and optionally a list of arguments. These statement has a type, a name and optionally a list of arguments. These
statements together form a statement list, which is placed in another object statements together form a statement list, which is placed in another object
called a Block. In the beginning there is one block for the entire program, but called a Block. In the beginning there is one block for the entire program, but
...@@ -219,7 +205,7 @@ The optimizations are done in two different steps. First the global ...@@ -219,7 +205,7 @@ The optimizations are done in two different steps. First the global
optimizations are performed, which are only the optimizations on branch-jump optimizations are performed, which are only the optimizations on branch-jump
constructions. This is done repeatedly until there are no more changes. constructions. This is done repeatedly until there are no more changes.
After all possible global optimizations are done, the program is separated into After all possible global optimizations are done, the program is seperated into
basic blocks. The algorithm to do this is described earlier, and means all basic blocks. The algorithm to do this is described earlier, and means all
jump and branch instructions are called leaders, as are their targets. A basic jump and branch instructions are called leaders, as are their targets. A basic
block then goes from leader to leader. block then goes from leader to leader.
...@@ -231,7 +217,8 @@ steps can be done to optimize something. ...@@ -231,7 +217,8 @@ steps can be done to optimize something.
\subsection{Writing} \subsection{Writing}
Once all the optimizations have been done, the IR needs to be rewritten into Once all the optimizations have been done, the IR needs to be rewritten into
Assembly code, so the xgcc cross compiler can make binary code out of it. Assembly code. After this step the xgcc crosscompiler can make binary code from
the generated Assembly code.
The writer expects a list of statements, so first the blocks have to be The writer expects a list of statements, so first the blocks have to be
concatenated again into a list. After this is done, the list is passed on to concatenated again into a list. After this is done, the list is passed on to
......
...@@ -65,6 +65,11 @@ class Statement: ...@@ -65,6 +65,11 @@ class Statement:
return self.is_command() \ return self.is_command() \
and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \ and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
self.name) self.name)
def is_branch_zero(self):
"""Check if statement is a branch that compares with zero."""
return self.is_command() \
and re.match('^blez|bgtz|bltz|bgez$', self.name)
def is_shift(self): def is_shift(self):
"""Check if the statement is a shift operation.""" """Check if the statement is a shift operation."""
...@@ -75,6 +80,11 @@ class Statement: ...@@ -75,6 +80,11 @@ class Statement:
return self.is_command() and self.name in ['lw', 'li', 'dlw', 'l.s', \ return self.is_command() and self.name in ['lw', 'li', 'dlw', 'l.s', \
'l.d'] 'l.d']
def is_store(self):
"""Check if the statement is a store instruction."""
return self.is_command() and self.name in ['sw', 's.d', 'dsw', 's.s', \
's.b']
def is_arith(self): def is_arith(self):
"""Check if the statement is an arithmetic operation.""" """Check if the statement is an arithmetic operation."""
return self.is_command() \ return self.is_command() \
...@@ -102,7 +112,7 @@ class Statement: ...@@ -102,7 +112,7 @@ class Statement:
return self.is_command() and re.match('^(xor|or|and)i?$', self.name) return self.is_command() and re.match('^(xor|or|and)i?$', self.name)
def is_double_aritmethic(self): def is_double_aritmethic(self):
"""Check if the statement is a arithmetic .d operator.""" """Check if the statement is a aritmethic .d operator."""
return self.is_command() and \ return self.is_command() and \
re.match('^(add|sub|div|mul)\.d$', self.name) re.match('^(add|sub|div|mul)\.d$', self.name)
...@@ -127,6 +137,10 @@ class Statement: ...@@ -127,6 +137,10 @@ class Statement:
"""Check if the statement is a convert operator.""" """Check if the statement is a convert operator."""
return self.is_command() and re.match('^trunc\.[a-z\.]*$', self.name) return self.is_command() and re.match('^trunc\.[a-z\.]*$', self.name)
def is_compare(self):
"""Check if the statement is a comparison."""
return self.is_command() and re.match('^c\.[a-z\.]*$', self.name)
def jump_target(self): def jump_target(self):
"""Get the jump target of this statement.""" """Get the jump target of this statement."""
if not self.is_jump(): if not self.is_jump():
...@@ -136,37 +150,51 @@ class Statement: ...@@ -136,37 +150,51 @@ class Statement:
def get_def(self): def get_def(self):
"""Get the variable that this statement defines, if any.""" """Get the variable that this statement defines, if any."""
instr = ['move', 'addu', 'subu', 'li', 'mtc1', 'dmfc1'] instr = ['move', 'addu', 'subu', 'li', 'mtc1', 'dmfc1', 'mov.d']
if self.is_load_non_immediate() or self.is_arith() \ if self.is_load_non_immediate() or self.is_arith() \
or self.is_logical() or self.is_double_arithmetic() \ or self.is_logical() or self.is_double_aritmethic() \
or self.is_move_from_spec() or self.is_double_unary() \ or self.is_move_from_spec() or self.is_double_unary() \
or self.is_set_if_less() or self.is_convert() \ or self.is_set_if_less() or self.is_convert() \
or self.is_truncate() or self.is_load() \ or self.is_truncate() or self.is_load() \
or (self.is_command and self.name in instr): or self.is_command(*instr):
return self[0] return [self[0]]
return [] return []
def get_use(self): def get_use(self):
# TODO: Finish with ALL the available commands! """Get the variables that this statement uses, if any."""
instr = ['addu', 'subu', 'mult', 'div', 'move', 'mtc1', 'mov.d', \
'dmfc1']
use = [] use = []
if self.is_binop(): # Case arg0
use += self[1:] if self.is_branch() or self.is_store() or self.is_compare()\
elif self.is_command('move'): or self.is_command(*['mult', 'div', 'dsz']):
if self.name == 'dsz':
m = re.match('^\d+\(([^)]+)\)$', self[0])
use.append(m)
else:
use.append(self[0])
# Case arg1 direct adressing
if (self.is_branch() and not self.is_branch_zero()) or self.is_shift()\
or self.is_double_arithmetic() or self.is_double_unary() \
or self.is_logical() or self.is_convert() \
or self.is_truncate() or self.is_set_if_less() \
or self.is_command(*instr):
use.append(self[1]) use.append(self[1])
elif self.is_command('lw', 'sb', 'sw', 'dsw', 's.s', 's.d'): # Case arg1 relative adressing
if self.is_load_non_immediate() or self.is_store():
m = re.match('^\d+\(([^)]+)\)$', self[1]) m = re.match('^\d+\(([^)]+)\)$', self[1])
if m: if m:
use.append(m.group(1)) use.append(m)
else:
# 'sw' also uses its first argument use.append(self[1])
if self.name in ['sw', 'dsw']: # Case arg2
use.append(self[0]) if self.is_double_arithmetic() or self.is_set_if_less() \
elif len(self) == 2: # FIXME: temporary fix, manually add all commands or self.is_logical() \
use.append(self[1]) or self.is_command(*['addu', 'subu']):
use.append(self[2])
return use return use
......
...@@ -93,3 +93,9 @@ class TestStatement(unittest.TestCase): ...@@ -93,3 +93,9 @@ class TestStatement(unittest.TestCase):
self.assertTrue(S('command', 'addu', '$1', '$2', '$3').is_arith()) self.assertTrue(S('command', 'addu', '$1', '$2', '$3').is_arith())
self.assertFalse(S('command', 'foo').is_arith()) self.assertFalse(S('command', 'foo').is_arith())
self.assertFalse(S('label', 'addu').is_arith()) self.assertFalse(S('label', 'addu').is_arith())
# def test_get_def(self):
# self.assertEqual(S('command', 'addu', '$1', '$2', '$3'), '$1')
#
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment