Просмотр исходного кода

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

Jayke Meijer 14 лет назад
Родитель
Сommit
55c2f6c7f0
4 измененных файлов с 76 добавлено и 8 удалено
  1. 33 7
      benchmarks/build/hello.s
  2. 5 0
      benchmarks/hello.c
  3. 1 1
      src/optimize/__init__.py
  4. 37 0
      src/optimize/advanced.py

+ 33 - 7
benchmarks/build/hello.s

@@ -10,6 +10,15 @@
 
 gcc2_compiled.:
 __gnu_compiled_c:
+	.sdata
+	.align	3
+$LC0:
+	.word	0x00000000		# 2
+	.word	0x40000000
+	.align	3
+$LC1:
+	.word	0x00000000		# 3.5
+	.word	0x400c0000
 	.text
 	.align	2
 	.globl	main
@@ -19,20 +28,37 @@ __gnu_compiled_c:
 	.loc	1 2
 	.ent	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
 	.fmask	0x00000000,0
-	subu	$sp,$sp,24
-	sw	$31,20($sp)
-	sw	$fp,16($sp)
+	subu	$sp,$sp,64
+	sw	$31,60($sp)
+	sw	$fp,56($sp)
 	move	$fp,$sp
 	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
 	j	$L1
 $L1:
 	move	$sp,$fp			# sp not trusted here
-	lw	$31,20($sp)
-	lw	$fp,16($sp)
-	addu	$sp,$sp,24
+	lw	$31,60($sp)
+	lw	$fp,56($sp)
+	addu	$sp,$sp,64
 	j	$31
 	.end	main

+ 5 - 0
benchmarks/hello.c

@@ -1,4 +1,9 @@
 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;
 }

+ 1 - 1
src/optimize/__init__.py

@@ -61,7 +61,7 @@ def optimize_block(block):
         pass
 
 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."""
     # Optimize on a global level
     o = len(statements)

+ 37 - 0
src/optimize/advanced.py

@@ -1,6 +1,10 @@
 from statement import Statement as S
 
 
+def create_variable():
+    return '$15'
+
+
 def eliminate_common_subexpressions(block):
     """
     Common subexpression elimination:
@@ -21,6 +25,7 @@ def eliminate_common_subexpressions(block):
         if s.is_arith():
             pointer = block.pointer
             last = False
+            new_reg = False
             args = s[1:]
 
             # Collect similar statements
@@ -33,6 +38,9 @@ def eliminate_common_subexpressions(block):
 
                 # Replace a similar expression by a move instruction
                 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)])
                     last = block.pointer
 
@@ -49,10 +57,39 @@ def eliminate_common_subexpressions(block):
     return found
 
 
+def to_hex(value):
+    """Create the hexadecimal string of an integer."""
+    return '0x%08x' % value
+
+
 def fold_constants(block):
     """
     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
     
 def copy_propagation(block):