Commit 55c2f6c7 authored by Jayke Meijer's avatar Jayke Meijer

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

parents ed8637b8 1912905a
...@@ -10,6 +10,15 @@ ...@@ -10,6 +10,15 @@
gcc2_compiled.: gcc2_compiled.:
__gnu_compiled_c: __gnu_compiled_c:
.sdata
.align 3
$LC0:
.word 0x00000000 # 2
.word 0x40000000
.align 3
$LC1:
.word 0x00000000 # 3.5
.word 0x400c0000
.text .text
.align 2 .align 2
.globl main .globl main
...@@ -19,20 +28,37 @@ __gnu_compiled_c: ...@@ -19,20 +28,37 @@ __gnu_compiled_c:
.loc 1 2 .loc 1 2
.ent main .ent main
main: main:
.frame $fp,24,$31 # vars= 0, regs= 2/0, args= 16, extra= 0 .frame $fp,64,$31 # vars= 40, regs= 2/0, args= 16, extra= 0
.mask 0xc0000000,-4 .mask 0xc0000000,-4
.fmask 0x00000000,0 .fmask 0x00000000,0
subu $sp,$sp,24 subu $sp,$sp,64
sw $31,20($sp) sw $31,60($sp)
sw $fp,16($sp) sw $fp,56($sp)
move $fp,$sp move $fp,$sp
jal __main jal __main
li $2,0x00000001 # 1
sw $2,16($fp)
li $2,0x00000005 # 5
sw $2,20($fp)
lw $2,16($fp)
lw $3,20($fp)
addu $2,$2,$3
sw $2,24($fp)
lw $2,16($fp)
addu $3,$2,10
sw $3,28($fp)
l.d $f0,$LC0
s.d $f0,32($fp)
l.d $f0,$LC1
s.d $f0,40($fp)
li $2,0x00000061 # 97
sb $2,48($fp)
move $2,$0 move $2,$0
j $L1 j $L1
$L1: $L1:
move $sp,$fp # sp not trusted here move $sp,$fp # sp not trusted here
lw $31,20($sp) lw $31,60($sp)
lw $fp,16($sp) lw $fp,56($sp)
addu $sp,$sp,24 addu $sp,$sp,64
j $31 j $31
.end main .end main
int main(void) int main(void)
{ {
int x = 1, b = 5, d = x + b, e = x + 10;
double y = 2., z = 3.5;
char c = 'a';
return 0; return 0;
} }
...@@ -61,7 +61,7 @@ def optimize_block(block): ...@@ -61,7 +61,7 @@ def optimize_block(block):
pass pass
def optimize(statements, verbose=0): def optimize(statements, verbose=0):
"""optimization wrapper function, calls global and basic-block level """Optimization wrapper function, calls global and basic-block level
optimization functions.""" optimization functions."""
# Optimize on a global level # Optimize on a global level
o = len(statements) o = len(statements)
......
from statement import Statement as S from statement import Statement as S
def create_variable():
return '$15'
def eliminate_common_subexpressions(block): def eliminate_common_subexpressions(block):
""" """
Common subexpression elimination: Common subexpression elimination:
...@@ -21,6 +25,7 @@ def eliminate_common_subexpressions(block): ...@@ -21,6 +25,7 @@ def eliminate_common_subexpressions(block):
if s.is_arith(): if s.is_arith():
pointer = block.pointer pointer = block.pointer
last = False last = False
new_reg = False
args = s[1:] args = s[1:]
# Collect similar statements # Collect similar statements
...@@ -33,6 +38,9 @@ def eliminate_common_subexpressions(block): ...@@ -33,6 +38,9 @@ def eliminate_common_subexpressions(block):
# Replace a similar expression by a move instruction # Replace a similar expression by a move instruction
if s2.name == s.name and s2[1:] == args: if s2.name == s.name and s2[1:] == args:
if not new_reg:
new_reg = create_variable()
block.replace(1, [S('command', 'move', s2[0], new_reg)]) block.replace(1, [S('command', 'move', s2[0], new_reg)])
last = block.pointer last = block.pointer
...@@ -49,10 +57,39 @@ def eliminate_common_subexpressions(block): ...@@ -49,10 +57,39 @@ def eliminate_common_subexpressions(block):
return found return found
def to_hex(value):
"""Create the hexadecimal string of an integer."""
return '0x%08x' % value
def fold_constants(block): def fold_constants(block):
""" """
Constant folding: Constant folding:
""" """
constants = {}
while not block.end():
s = block.read()
if s.is_load():
constants[s[0]] = s[1]
elif s.is_command() and len(s) == 3:
d, s, t = s
if s in constants and t in constants:
if s.name == 'addu':
result = s + t
elif s.name == 'subu':
result = s - t
elif s.name == 'mult':
result = s * t
elif s.name == 'div':
result = s / t
block.replace(1, [S('command', 'li', to_hex(result))])
constants[d] = result
#else:
return False return False
def copy_propagation(block): def copy_propagation(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