Browse Source

added global optimizations and fixed bugs in basic blocks.

Jayke Meijer 14 years ago
parent
commit
d429809c94
3 changed files with 36 additions and 6 deletions
  1. 7 5
      src/basic_block.py
  2. 12 1
      src/optimize.py
  3. 17 0
      src/optimizer.py

+ 7 - 5
src/basic_block.py

@@ -18,12 +18,12 @@ def find_leaders(statements):
     for i, statement in enumerate(statements[1:]):
     for i, statement in enumerate(statements[1:]):
         if is_jump(statement):
         if is_jump(statement):
             leaders.append(i + 2)
             leaders.append(i + 2)
-            print statement[2]['args'][-1]
+#            print statement[2]['args'][-1]
             jump_target_labels.append(statement[2]['args'][-1])
             jump_target_labels.append(statement[2]['args'][-1])
             #print 'found jump:', i, statement
             #print 'found jump:', i, statement
 
 
-    print 'target labels:', jump_target_labels
-    print 'leaders:', leaders
+#    print 'target labels:', jump_target_labels
+#    print 'leaders:', leaders
 
 
     # Append jump targets
     # Append jump targets
     for i, statement in enumerate(statements[1:]):
     for i, statement in enumerate(statements[1:]):
@@ -32,7 +32,9 @@ def find_leaders(statements):
                 and statement[1] in jump_target_labels:
                 and statement[1] in jump_target_labels:
             leaders.append(i + 1)
             leaders.append(i + 1)
             #print 'target:', i + 1, statements[i + 1]
             #print 'target:', i + 1, statements[i + 1]
-
+    
+    leaders.sort()
+    
     return leaders
     return leaders
 
 
 def find_basic_blocks(statements):
 def find_basic_blocks(statements):
@@ -45,5 +47,5 @@ def find_basic_blocks(statements):
         blocks.append(statements[leaders[i]:leaders[i + 1]])
         blocks.append(statements[leaders[i]:leaders[i + 1]])
 
 
     blocks.append(statements[leaders[-1]:])
     blocks.append(statements[leaders[-1]:])
-
+    
     return blocks
     return blocks

+ 12 - 1
src/optimize.py

@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #!/usr/bin/python
 from parser import parse_file
 from parser import parse_file
 from basic_block import find_basic_blocks
 from basic_block import find_basic_blocks
-from optimizer import optimize_blocks
+from optimizer import optimize_blocks, optimize_global
 from writer import write_statements
 from writer import write_statements
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
@@ -13,6 +13,12 @@ if __name__ == '__main__':
 
 
     # Parse File
     # Parse File
     statements = parse_file(argv[1])
     statements = parse_file(argv[1])
+    st_original = len(statements)
+    
+    # Optimize on a global level
+    statements = optimize_global(statements)
+    
+    st_aft_global = len(statements)
 
 
     # Create basic blocks
     # Create basic blocks
     blocks = find_basic_blocks(statements)
     blocks = find_basic_blocks(statements)
@@ -22,6 +28,11 @@ if __name__ == '__main__':
 
 
     # Rewrite to assembly
     # Rewrite to assembly
     out = write_statements(statements)
     out = write_statements(statements)
+    
+    print "Optimization:"
+    print "Original statements:" + str(st_original)
+    print "After global optimization:" + str(st_aft_global)
+    print "After basic blocks optimization:" + str(len(statements))
 
 
     if len(argv) > 2:
     if len(argv) > 2:
         # Save output assembly
         # Save output assembly

+ 17 - 0
src/optimizer.py

@@ -1,3 +1,20 @@
+def equal_mov(statement):
+    '''Check for useless move operations.'''
+    stype, name, args = statement
+    return stype == 'command' and name == 'move' \
+        and args['args'][0] == args['args'][1]
+    
+def empty_shift(statement):
+    '''Check for useless shift operations.'''
+    shift_types = ['sll', 'sla', 'srl', 'sra']
+    stype, name, args = statement
+    return stype == 'command' and name in shift_types and \
+        args['args'][0] == args['args'][1] and args['args'][2] == 0
+
+def optimize_global(statements):
+    '''Optimize one-line statements in entire code.'''
+    return filter(lambda s: not equal_mov(s) and not empty_shift(s), statements)
+
 def optimize_blocks(blocks):
 def optimize_blocks(blocks):
     '''Call the optimizer for each basic block. Do this several times until
     '''Call the optimizer for each basic block. Do this several times until
     no more optimizations are achieved.'''
     no more optimizations are achieved.'''