Commit d2a9252f authored by Taddeus Kroes's avatar Taddeus Kroes

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

parents 876d7a98 ef3babb5
.file 1 "acron.c"
# GNU C 2.7.2.3 [AL 1.1, MM 40, tma 0.1] SimpleScalar running sstrix compiled by GNU C
# Cc1 defaults:
# -mgas -mgpOPT
# Cc1 arguments (-G value = 8, Cpu = default, ISA = 1):
# -quiet -dumpbase -O0 -o
gcc2_compiled.:
__gnu_compiled_c:
.globl w
.data
.align 2
w:
.word $LC0
.word $LC1
.word $LC2
.word $LC3
.word $LC4
.word $LC5
.sdata
.align 2
$LC5:
.ascii "Eephole\000"
.rdata
.align 2
$LC4:
.ascii "Peephole\000"
.align 2
$LC3:
.ascii "Optimization\000"
.align 2
$LC2:
.ascii "Practicum\000"
.align 2
$LC1:
.ascii "Ertalerbouw\000"
.align 2
$LC0:
.ascii "Vertalerbouw\000"
.text
.align 2
.globl is_vowel
.sdata
.align 2
$LC6:
.ascii "%s\000"
.align 2
$LC7:
.ascii " %s\000"
.align 2
$LC8:
.ascii "\n\000"
.text
.align 2
.globl do_perm
.align 2
.globl main
.comm acron,12
.comm command,100
.comm done,24
.comm pindex,28
.text
.loc 1 10
.ent is_vowel
is_vowel:
.frame $fp,16,$31 # vars= 8, regs= 1/0, args= 0, extra= 0
.mask 0x40000000,-8
.fmask 0x00000000,0
subu $sp,$sp,16
sw $fp,8($sp)
move $fp,$sp
move $3,$4
sb $3,0($fp)
move $2,$0
lb $4,0($fp)
li $5,0x00000041 # $5 = 65
beq $4,$5,$L3
lb $4,0($fp)
li $5,0x00000045 # $5 = 69
beq $4,$5,$L3
lb $4,0($fp)
li $5,0x00000049 # $5 = 73
beq $4,$5,$L3
lb $4,0($fp)
li $5,0x0000004f # $5 = 79
beq $4,$5,$L3
lb $4,0($fp)
li $5,0x00000055 # $5 = 85
beq $4,$5,$L3
lb $4,0($fp)
li $5,0x00000059 # $5 = 89
bne $4,$5,$L2
$L3:
li $2,0x00000001 # $2 = 1
$L2:
j $L1
$L1:
move $sp,$fp # sp not trusted here
lw $fp,8($sp)
addu $sp,$sp,16
j $31
.end is_vowel
.loc 1 15
.ent do_perm
do_perm:
.frame $fp,56,$31 # vars= 24, regs= 4/0, args= 16, extra= 0
.mask 0xc0030000,-4
.fmask 0x00000000,0
subu $sp,$sp,56
sw $31,52($sp)
sw $fp,48($sp)
sw $17,44($sp)
sw $16,40($sp)
move $fp,$sp
sw $4,56($fp)
sw $5,60($fp)
sw $6,64($fp)
sw $7,68($fp)
sw $0,24($fp)
lw $2,64($fp)
li $3,0x00000001 # $3 = 1
bne $2,$3,$L5
lw $2,pindex
move $3,$2
sll $2,$3,2
la $3,w
addu $2,$2,$3
lw $3,0($2)
lb $4,0($3)
jal is_vowel
bne $2,$0,$L5
lw $2,56($fp)
move $3,$2
sll $2,$3,2
la $3,w
addu $2,$2,$3
lw $3,0($2)
lb $4,0($3)
jal is_vowel
beq $2,$0,$L4
$L5:
lw $2,64($fp)
slt $3,$2,2
bne $3,$0,$L6
lw $2,64($fp)
move $3,$2
sll $2,$3,2
la $3,pindex-8
addu $2,$2,$3
lw $3,0($2)
move $2,$3
sll $3,$2,2
la $4,w
addu $2,$3,$4
lw $3,0($2)
lb $4,0($3)
jal is_vowel
move $16,$2
lw $2,64($fp)
move $3,$2
sll $2,$3,2
la $3,pindex-4
addu $2,$2,$3
lw $3,0($2)
move $2,$3
sll $3,$2,2
la $4,w
addu $2,$3,$4
lw $3,0($2)
lb $4,0($3)
jal is_vowel
move $17,$2
lw $2,56($fp)
move $3,$2
sll $2,$3,2
la $3,w
addu $2,$2,$3
lw $3,0($2)
lb $4,0($3)
jal is_vowel
addu $3,$16,$17
addu $2,$3,$2
sw $2,24($fp)
beq $2,$0,$L8
lw $2,24($fp)
li $3,0x00000003 # $3 = 3
bne $2,$3,$L7
$L8:
j $L4
$L7:
$L6:
lw $2,64($fp)
addu $3,$2,1
sw $3,64($fp)
sll $3,$2,2
la $4,pindex
addu $2,$3,$4
lw $3,56($fp)
sw $3,0($2)
lw $2,64($fp)
slt $3,$2,6
beq $3,$0,$L9
lw $3,68($fp)
subu $2,$3,1
move $3,$2
sw $3,68($fp)
beq $3,$0,$L9
sw $0,16($fp)
$L10:
lw $2,16($fp)
slt $3,$2,6
beq $3,$0,$L11
$L13:
lw $2,16($fp)
move $3,$2
sll $2,$3,2
lw $3,60($fp)
addu $2,$2,$3
lw $3,0($2)
bne $3,$0,$L14
lw $2,16($fp)
move $3,$2
sll $2,$3,2
lw $3,60($fp)
addu $2,$2,$3
li $3,0x00000001 # $3 = 1
sw $3,0($2) # 0($2) = 1
lw $4,16($fp)
lw $5,60($fp)
lw $6,64($fp)
lw $7,68($fp)
jal do_perm
lw $2,16($fp)
move $3,$2
sll $2,$3,2
lw $3,60($fp)
addu $2,$2,$3
sw $0,0($2)
$L14:
$L12:
lw $3,16($fp)
addu $2,$3,1
move $3,$2
sw $3,16($fp)
j $L10
$L11:
j $L15
$L9:
sw $0,28($fp)
sw $0,20($fp)
$L16:
lw $2,20($fp)
lw $3,64($fp)
slt $2,$2,$3
beq $2,$0,$L17
$L19:
sw $0,32($fp)
$L20:
lw $2,20($fp)
move $3,$2
sll $2,$3,2
la $3,pindex
addu $2,$2,$3
lw $3,0($2)
move $2,$3
sll $3,$2,2
la $4,w
addu $2,$3,$4
lw $3,0($2)
lw $4,32($fp)
addu $2,$3,$4
lb $4,0($2)
jal isupper
beq $2,$0,$L21
$L22:
lw $2,28($fp)
addu $3,$2,1
sw $3,28($fp)
lw $3,20($fp)
move $4,$3
sll $3,$4,2
la $4,pindex
addu $3,$3,$4
lw $4,0($3)
move $3,$4
sll $4,$3,2
la $5,w
addu $3,$4,$5
lw $4,32($fp)
addu $5,$4,1
sw $5,32($fp)
lw $5,0($3)
addu $3,$4,$5
lbu $4,0($3)
sb $4,acron($2)
j $L20
$L21:
$L18:
lw $3,20($fp)
addu $2,$3,1
move $3,$2
sw $3,20($fp)
j $L16
$L17:
lw $2,28($fp)
la $3,acron
addu $2,$2,$3
sb $0,0($2)
la $4,$LC6
la $5,acron
jal printf
sw $0,20($fp)
$L23:
lw $2,20($fp)
lw $3,64($fp)
slt $2,$2,$3
beq $2,$0,$L24
$L26:
lw $2,20($fp)
move $3,$2
sll $2,$3,2
la $3,pindex
addu $2,$2,$3
lw $3,0($2)
move $2,$3
sll $3,$2,2
la $4,w
addu $2,$3,$4
la $4,$LC7
lw $5,0($2)
jal printf
$L25:
lw $3,20($fp)
addu $2,$3,1
move $3,$2
sw $3,20($fp)
j $L23
$L24:
la $4,$LC8
jal printf
$L15:
$L4:
move $sp,$fp # sp not trusted here
lw $31,52($sp)
lw $fp,48($sp)
lw $17,44($sp)
lw $16,40($sp)
addu $sp,$sp,56
j $31
.end do_perm
.loc 1 53
.ent main
main:
.frame $fp,32,$31 # vars= 8, regs= 2/0, args= 16, extra= 0
.mask 0xc0000000,-4
.fmask 0x00000000,0
subu $sp,$sp,32
sw $31,28($sp)
sw $fp,24($sp)
move $fp,$sp
jal __main
li $2,0x00000004 # $2 = 4
sw $2,20($fp) # 20($fp) = 4
$L28:
lw $2,20($fp)
slt $3,$2,7
beq $3,$0,$L29
$L31:
sw $0,16($fp)
$L32:
lw $2,16($fp)
slt $3,$2,6
beq $3,$0,$L33
$L35:
lw $2,16($fp)
move $3,$2
sll $2,$3,2
la $3,done
addu $2,$2,$3
li $3,0x00000001 # $3 = 1
sw $3,0($2) # 0($2) = 1
lw $4,16($fp)
la $5,done
move $6,$0
lw $7,20($fp)
jal do_perm
lw $2,16($fp)
move $3,$2
sll $2,$3,2
la $3,done
addu $2,$2,$3
sw $0,0($2)
$L34:
lw $3,16($fp)
addu $2,$3,1
move $3,$2
sw $3,16($fp)
j $L32
$L33:
$L30:
lw $3,20($fp)
addu $2,$3,1
move $3,$2
sw $3,20($fp)
j $L28
$L29:
$L27:
move $sp,$fp # sp not trusted here
lw $31,28($sp)
lw $fp,24($sp)
addu $sp,$sp,32
j $31
.end main
......@@ -126,9 +126,10 @@ calculated at compile time. If a value x is assigned to a certain value, lets
say 10, than all next occurences of \texttt{x} are replaced by 10 until a
redefinition of x. Arithmetics in Assembly are always performed between two
variables or a variable and a constant. If this is not the case the calculation
is not possible. See the example for a more clear explanation of constant
folding(will come). In other words until the current definition of \texttt{x}
becomes dead. Therefore reaching definitions analysis is needed.
is not possible. See \ref{opt} for an example. In other words until the current
definition of \texttt{x} becomes dead. Therefore reaching definitions analysis is
needed. Reaching definitions is a form of liveness analysis, we use the liveness
analysis within a block and not between blocks.
\subsubsection*{Copy propagation}
......@@ -305,7 +306,13 @@ addu $regC, $regB, 4 move $regC, $regD
# Constant folding
li $regA, constA ""
sw $regA, 16($fp) ""
li $regA, constB -> ""
sw $regA, 20($fp) ""
lw $regA, 16($fp) ""
lw $regB, 20($fp) ""
addu $regA, $regA, $regA $li regA, (constA + constB) at compile time
# Copy propagation
move $regA, $regB move $regA, $regB
......
......@@ -3,7 +3,8 @@ import re
def remove_redundancies(block):
"""Execute all functions that remove redundant statements."""
callbacks = [move_1, move_2, move_3, move_4, load, shift, add]
callbacks = [move_aa, move_inst, instr_move_jal, move_move, sw_ld, shift,
add_lw]
old_len = -1
changed = False
......@@ -23,9 +24,9 @@ def remove_redundancies(block):
return changed
def move_1(mov, statements):
def move_aa(mov, statements):
"""
mov $regA, $regA -> --- remove it
move $regA, $regA -> --- remove it
"""
if mov.is_command('move') and mov[0] == mov[1]:
statements.replace(1, [])
......@@ -33,9 +34,9 @@ def move_1(mov, statements):
return True
def move_2(mov, statements):
def move_inst(mov, statements):
"""
mov $regA, $regB -> instr $regA, $regB, ...
move $regA, $regB -> instr $regA, $regB, ...
instr $regA, $regA, ...
"""
if mov.is_command('move'):
......@@ -48,10 +49,10 @@ def move_2(mov, statements):
return True
def move_3(ins, statements):
def instr_move_jal(ins, statements):
"""
instr $regA, ... -> instr $4, ...
mov $4, $regA jal XX
move $4, $regA jal XX
jal XX
"""
if ins.is_command() and len(ins):
......@@ -69,10 +70,10 @@ def move_3(ins, statements):
return True
def move_4(mov1, statements):
def move_move(mov1, statements):
"""
mov $RegA, $RegB -> move $RegA, $RegB
mov $RegB, $RegA
move $RegA, $RegB -> move $RegA, $RegB
move $RegB, $RegA
"""
if mov1.is_command('move'):
mov2 = statements.peek()
......@@ -84,7 +85,7 @@ def move_4(mov1, statements):
return True
def load(sw, statements):
def sw_ld(sw, statements):
"""
sw $regA, XX -> sw $regA, XX
ld $regA, XX
......@@ -108,7 +109,7 @@ def shift(shift, statements):
return True
def add(add, statements):
def add_lw(add, statements):
"""
add $regA, $regA, X -> lw ..., X($regA)
lw ..., 0($regA)
......
import unittest
from src.optimize.redundancies import remove_redundant_jumps
from src.optimize.redundancies import remove_redundancies, remove_redundant_jumps
from src.program import Program
from src.statement import Statement as S, Block as B
def optimize_block(block):
"""Optimize a basic block using a Program object."""
program = Program([])
# program = Program([])
program.blocks = [block]
del program.statements
program.optimize_blocks()
# program.blocks = [block]
# del program.statements
# program.optimize_blocks()
remove_redundancies(block)
eliminate_common_subexpressions(block)
fold_constants(block)
copy_propagation(block)
return program.blocks
......@@ -30,7 +36,7 @@ class TestOptimize(unittest.TestCase):
block = B([self.foo,
S('command', 'move', '$regA', '$regA'),
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo, self.bar])
def test_optimize_block_movab(self):
......@@ -38,7 +44,7 @@ class TestOptimize(unittest.TestCase):
block = B([self.foo,
move,
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo, move, self.bar])
def test_optimize_block_movinst_true(self):
......@@ -46,7 +52,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'move', '$regA', '$regB'),
S('command', 'addu', '$regA', '$regA', 2),
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo,
S('command', 'addu', '$regA', '$regB', 2),
self.bar])
......@@ -58,7 +64,7 @@ class TestOptimize(unittest.TestCase):
self.bar]
block = B(statements)
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, statements)
def test_optimize_block_instr_mov_jal_true(self):
......@@ -67,7 +73,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'move', '$4', '$regA'),
S('command', 'jal', 'L1'),
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo,
S('command', 'addu', '$4', '$regC', 2),
......@@ -81,7 +87,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'jal', 'L1'), \
self.bar]
block = B(arguments)
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, arguments)
......@@ -90,7 +96,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'sw', '$regA', '$regB'),
S('command', 'lw', '$regA', '$regB'),
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo,
S('command', 'sw', '$regA', '$regB'),
......@@ -102,7 +108,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'lw', '$regD', '$regC'), \
self.bar]
block = B(arguments)
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, arguments)
......@@ -110,7 +116,7 @@ class TestOptimize(unittest.TestCase):
block = B([self.foo,
S('command', 'sll', '$regA', '$regA', 0),
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo, self.bar])
......@@ -119,7 +125,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'sll', '$regA', '$regB', 0), \
self.bar]
block = B(arguments)
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, arguments)
......@@ -127,7 +133,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'sll', '$regA', '$regA', 1), \
self.bar]
block2 = B(arguments2)
optimize_block(block2)
remove_redundancies(block2)
self.assertEquals(block2.statements, arguments2)
......@@ -136,7 +142,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'addu', '$regA', '$regA', 10),
S('command', 'lw', '$regB', '0($regA)'),
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo,
S('command', 'lw', '$regB', '10($regA)'),
......@@ -148,7 +154,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'lw', '$regB', '0($regC)'), \
self.bar]
block = B(arguments)
optimize_block(block)
remove_redundancies(block)
arguments2 = [self.foo, \
S('command', 'addu', '$regA', '$regB', 10), \
......@@ -161,7 +167,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'lw', '$regB', '1($regA)'), \
self.bar]
block3 = B(arguments3)
optimize_block(block3)
remove_redundancies(block3)
self.assertEquals(block.statements, arguments)
self.assertEquals(block2.statements, arguments2)
......@@ -220,7 +226,7 @@ class TestOptimize(unittest.TestCase):
S('command', 'move', '$regA', '$regB'),
S('command', 'move', '$regB', '$regA'),
self.bar])
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, [self.foo,
S('command', 'move', '$regA', '$regB'),
......@@ -232,6 +238,6 @@ class TestOptimize(unittest.TestCase):
S('command', 'move', '$regB', '$regC'), \
self.bar]
block = B(arguments)
optimize_block(block)
remove_redundancies(block)
self.assertEquals(block.statements, arguments)
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