Commit 600b38de authored by Taddeus Kroes's avatar Taddeus Kroes

Merged conflicts.

parents 950105c0 2473f617
......@@ -112,11 +112,11 @@ 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
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
primary concern, this is not a big problem.
\subsubsection*{Fold constants}
\subsubsection*{Constant folding}
......@@ -158,7 +158,18 @@ removed by the dead code elimination.
\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}
......@@ -195,7 +206,7 @@ The optimizations are done in two different steps. First the global
optimizations are performed, which are only the optimizations on branch-jump
constructions. This is done repeatedly until there are no more changes.
After all possible global optimizations are done, the program is seperated into
After all possible global optimizations are done, the program is separated into
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
block then goes from leader to leader.
......@@ -207,7 +218,7 @@ steps can be done to optimize something.
\subsection{Writing}
Once all the optimizations have been done, the IR needs to be rewritten into
Assembly code, so the xgcc crosscompiler can make binary code out of it.
Assembly code, so the xgcc cross compiler can make binary code out of it.
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
......
......@@ -25,38 +25,38 @@ class BasicBlock(Block):
if block not in self.dominates:
self.dominates.append(block)
block.dominated_by.append(self)
def get_gen(self):
for s in self.statements:
if s.is_arith():
self.gen_set.add(s[0])
print 'added: ', s[0]
return self.gen_set
def get_kill(self):
# if self.edges_from != []:
for backw in self.edges_from:
self.kill_set = self.kill_set | backw.get_kill()
self.kill_set = self.kill_set - self.get_gen()
print 'get_kill_set', self.kill_set
return self.kill_set
def get_in(self):
for backw in self.edges_from:
self.in_set = self.in_set | backw.get_out()
print 'in_set', self.in_set
return self.in_set
def get_out(self):
print 'gen_set', self.gen_set
print 'get_in', self.get_in()
print 'get_kill', self.get_kill()
self.out_set = self.gen_set | (self.get_in() - self.get_kill())
# def get_gen(self):
# for s in self.statements:
# if s.is_arith():
# self.gen_set.add(s[0])
# print 'added: ', s[0]
#
# return self.gen_set
#
# def get_kill(self):
## if self.edges_from != []:
#
# for backw in self.edges_from:
# self.kill_set = self.gen_set & backw.kill_set
#
# self.kill_set = self.kill_set - self.get_gen()
# print 'get_kill_set', self.kill_set
# return self.kill_set
# def get_in(self):
# for backw in self.edges_from:
# self.in_set = self.in_set | backw.out_set
# print 'in_set', self.in_set
# return self.in_set
# def get_out(self):
# print 'gen_set', self.gen_set
# print 'get_in', self.get_in()
# print 'get_kill', self.get_kill()
# self.out_set = self.gen_set | (self.get_in() - self.get_kill())
def reaching_definition(blocks):
generate_flow_graph(blocks)
......
......@@ -149,7 +149,7 @@ def fold_constants(block):
register[s[0]] = constants[s[1]]
elif s.name in ['addu', 'subu', 'mult', 'div']:
# Calculation with constants
rd, rs, rt = s
rd, rs, rt = s[0], s[1], s[2]
rs_known = rs in register
rt_known = rt in register
......@@ -223,11 +223,13 @@ def copy_propagation(block):
if moves_to[i] == s[0]:
moves_from[i] = s[1]
break
elif len(s) == 3 and s[0] in moves_to:
# The result gets overwritten, so remove the data from the list.
elif (len(s) == 3 or s.is_command('mlfo') or s.is_load()) \
and (s[0] in moves_to or s[0] in moves_from):
# One of the registers gets overwritten, so remove the data from
# the list.
i = 0
while i < len(moves_to):
if moves_to[i] == s[0]:
if moves_to[i] == s[0] or moves_to[i] == s[1]:
del moves_to[i]
del moves_from[i]
else:
......@@ -251,7 +253,7 @@ def copy_propagation(block):
def algebraic_transformations(block):
"""
Change ineffective or useless algebraic transformations. Handled are:
Change ineffective or useless algebraic expressions. Handled are:
- x = y + 0 -> x = y
- x = y - 0 -> x = y
- x = y * 1 -> x = y
......@@ -266,17 +268,22 @@ def algebraic_transformations(block):
if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0:
block.replace(1, [S('command', 'move', s[0], s[1])])
changed = True
elif s.is_command('mult') and s[2] == 1:
block.replace(1, [S('command', 'move', s[0], s[1])])
changed = True
elif s.is_command('mult') and s[2] == 0:
block.replace(1, [S('command', 'li', '$1', to_hex(0))])
changed = True
elif s.is_command('mult'):
shift_amount = log(s[2], 2)
if shift_amount.is_integer():
new_command = S('command', 'sll', s[0], s[1], shift_amount)
block.replace(1, [new_command])
changed = True
next = block.peek()
if next.is_command('mflo'):
if s[1] == 1:
block.replace(2, [S('command', 'move', next[0], s[0])])
changed = True
break
elif s[1] == 0:
block.replace(2, [S('command', 'li', '$1', to_hex(0))])
changed = True
break
shift_amount = log(s[1], 2)
if shift_amount.is_integer():
new_command = S('command', 'sll', next[0], s[0], int(shift_amount))
block.replace(2, [new_command])
changed = True
return changed
......@@ -43,14 +43,21 @@ class TestDataflow(unittest.TestCase):
#
# blocks = [b1, b2]
#
# # initialize out[B] = gen[B] for every block
# for block in blocks:
# block.out_set = block.get_gen()
# print 'block.out_set', block.out_set
# generate_flow_graph(blocks)
# print b1.get_gen()
# print b2.get_gen()
# print b2.get_out()
# print 'block.out_set', block.out_set
#
# generate_flow_graph(blocks)
# change = True
# while change:
# for i, block in enumerate(blocks):
# block.get_in()
# oldout = block.out_set
# newout = block.get_out()
# if (block.out_set == block.get_out()):
# change = False
def test_generate_flow_graph_simple(self):
......
......@@ -125,7 +125,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult0(self):
block = B([self.foo,
S('command', 'mult', '$1', '$2', 0),
S('command', 'mult', '$2', 0),
S('command', 'mflo', '$1'),
self.bar])
self.assertTrue(algebraic_transformations(block))
......@@ -135,7 +136,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult1(self):
block = B([self.foo,
S('command', 'mult', '$1', '$2', 1),
S('command', 'mult', '$2', 1),
S('command', 'mflo', '$1'),
self.bar])
self.assertTrue(algebraic_transformations(block))
......@@ -145,7 +147,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult2(self):
block = B([self.foo,
S('command', 'mult', '$1', '$2', 2),
S('command', 'mult', '$2', 2),
S('command', 'mflo', '$1'),
self.bar])
self.assertTrue(algebraic_transformations(block))
......@@ -155,7 +158,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult16(self):
block = B([self.foo,
S('command', 'mult', '$1', '$2', 16),
S('command', 'mult', '$2', 16),
S('command', 'mflo', '$1'),
self.bar])
self.assertTrue(algebraic_transformations(block))
......
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