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 ...@@ -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 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*{Fold constants} \subsubsection*{Constant folding}
...@@ -158,7 +158,18 @@ removed by the dead code elimination. ...@@ -158,7 +158,18 @@ 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}
...@@ -195,7 +206,7 @@ The optimizations are done in two different steps. First the global ...@@ -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 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 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 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.
...@@ -207,7 +218,7 @@ steps can be done to optimize something. ...@@ -207,7 +218,7 @@ 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 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 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
......
...@@ -26,37 +26,37 @@ class BasicBlock(Block): ...@@ -26,37 +26,37 @@ class BasicBlock(Block):
self.dominates.append(block) self.dominates.append(block)
block.dominated_by.append(self) block.dominated_by.append(self)
def get_gen(self):
for s in self.statements: # def get_gen(self):
if s.is_arith(): # for s in self.statements:
self.gen_set.add(s[0]) # if s.is_arith():
print 'added: ', s[0] # self.gen_set.add(s[0])
# print 'added: ', s[0]
return self.gen_set #
# return self.gen_set
def get_kill(self): #
# if self.edges_from != []: # def get_kill(self):
for backw in self.edges_from: ## if self.edges_from != []:
self.kill_set = self.kill_set | backw.get_kill() #
# for backw in self.edges_from:
self.kill_set = self.kill_set - self.get_gen() # self.kill_set = self.gen_set & backw.kill_set
print 'get_kill_set', self.kill_set #
return self.kill_set # self.kill_set = self.kill_set - self.get_gen()
# print 'get_kill_set', self.kill_set
def get_in(self): # return self.kill_set
for backw in self.edges_from:
self.in_set = self.in_set | backw.get_out() # def get_in(self):
print 'in_set', self.in_set # for backw in self.edges_from:
return self.in_set # self.in_set = self.in_set | backw.out_set
# print 'in_set', self.in_set
def get_out(self): # return self.in_set
print 'gen_set', self.gen_set
print 'get_in', self.get_in() # def get_out(self):
print 'get_kill', self.get_kill() # print 'gen_set', self.gen_set
self.out_set = self.gen_set | (self.get_in() - self.get_kill()) # print 'get_in', self.get_in()
# print 'get_kill', self.get_kill()
def reaching_definition(blocks): # self.out_set = self.gen_set | (self.get_in() - self.get_kill())
generate_flow_graph(blocks)
......
...@@ -149,7 +149,7 @@ def fold_constants(block): ...@@ -149,7 +149,7 @@ def fold_constants(block):
register[s[0]] = constants[s[1]] register[s[0]] = constants[s[1]]
elif s.name in ['addu', 'subu', 'mult', 'div']: elif s.name in ['addu', 'subu', 'mult', 'div']:
# Calculation with constants # Calculation with constants
rd, rs, rt = s rd, rs, rt = s[0], s[1], s[2]
rs_known = rs in register rs_known = rs in register
rt_known = rt in register rt_known = rt in register
...@@ -223,11 +223,13 @@ def copy_propagation(block): ...@@ -223,11 +223,13 @@ def copy_propagation(block):
if moves_to[i] == s[0]: if moves_to[i] == s[0]:
moves_from[i] = s[1] moves_from[i] = s[1]
break break
elif len(s) == 3 and s[0] in moves_to: elif (len(s) == 3 or s.is_command('mlfo') or s.is_load()) \
# The result gets overwritten, so remove the data from the list. 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 i = 0
while i < len(moves_to): 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_to[i]
del moves_from[i] del moves_from[i]
else: else:
...@@ -251,7 +253,7 @@ def copy_propagation(block): ...@@ -251,7 +253,7 @@ def copy_propagation(block):
def algebraic_transformations(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 - 0 -> x = y - x = y - 0 -> x = y
- x = y * 1 -> x = y - x = y * 1 -> x = y
...@@ -266,17 +268,22 @@ def algebraic_transformations(block): ...@@ -266,17 +268,22 @@ def algebraic_transformations(block):
if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0: if (s.is_command('addu') or s.is_command('subu')) and s[2] == 0:
block.replace(1, [S('command', 'move', s[0], s[1])]) block.replace(1, [S('command', 'move', s[0], s[1])])
changed = True changed = True
elif s.is_command('mult') and s[2] == 1: elif s.is_command('mult'):
block.replace(1, [S('command', 'move', s[0], s[1])]) next = block.peek()
if next.is_command('mflo'):
if s[1] == 1:
block.replace(2, [S('command', 'move', next[0], s[0])])
changed = True changed = True
elif s.is_command('mult') and s[2] == 0: break
block.replace(1, [S('command', 'li', '$1', to_hex(0))]) elif s[1] == 0:
block.replace(2, [S('command', 'li', '$1', to_hex(0))])
changed = True changed = True
elif s.is_command('mult'): break
shift_amount = log(s[2], 2)
shift_amount = log(s[1], 2)
if shift_amount.is_integer(): if shift_amount.is_integer():
new_command = S('command', 'sll', s[0], s[1], shift_amount) new_command = S('command', 'sll', next[0], s[0], int(shift_amount))
block.replace(1, [new_command]) block.replace(2, [new_command])
changed = True changed = True
return changed return changed
...@@ -43,14 +43,21 @@ class TestDataflow(unittest.TestCase): ...@@ -43,14 +43,21 @@ class TestDataflow(unittest.TestCase):
# #
# blocks = [b1, b2] # blocks = [b1, b2]
# #
# # initialize out[B] = gen[B] for every block
# for block in blocks: # for block in blocks:
# block.out_set = block.get_gen() # block.out_set = block.get_gen()
# print 'block.out_set', block.out_set # print 'block.out_set', block.out_set
#
# generate_flow_graph(blocks) # generate_flow_graph(blocks)
# print b1.get_gen()
# print b2.get_gen() # change = True
# print b2.get_out() # 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): def test_generate_flow_graph_simple(self):
......
...@@ -125,7 +125,8 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -125,7 +125,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult0(self): def test_algebraic_transforms_mult0(self):
block = B([self.foo, block = B([self.foo,
S('command', 'mult', '$1', '$2', 0), S('command', 'mult', '$2', 0),
S('command', 'mflo', '$1'),
self.bar]) self.bar])
self.assertTrue(algebraic_transformations(block)) self.assertTrue(algebraic_transformations(block))
...@@ -135,7 +136,8 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -135,7 +136,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult1(self): def test_algebraic_transforms_mult1(self):
block = B([self.foo, block = B([self.foo,
S('command', 'mult', '$1', '$2', 1), S('command', 'mult', '$2', 1),
S('command', 'mflo', '$1'),
self.bar]) self.bar])
self.assertTrue(algebraic_transformations(block)) self.assertTrue(algebraic_transformations(block))
...@@ -145,7 +147,8 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -145,7 +147,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult2(self): def test_algebraic_transforms_mult2(self):
block = B([self.foo, block = B([self.foo,
S('command', 'mult', '$1', '$2', 2), S('command', 'mult', '$2', 2),
S('command', 'mflo', '$1'),
self.bar]) self.bar])
self.assertTrue(algebraic_transformations(block)) self.assertTrue(algebraic_transformations(block))
...@@ -155,7 +158,8 @@ class TestOptimizeAdvanced(unittest.TestCase): ...@@ -155,7 +158,8 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_algebraic_transforms_mult16(self): def test_algebraic_transforms_mult16(self):
block = B([self.foo, block = B([self.foo,
S('command', 'mult', '$1', '$2', 16), S('command', 'mult', '$2', 16),
S('command', 'mflo', '$1'),
self.bar]) self.bar])
self.assertTrue(algebraic_transformations(block)) 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