瀏覽代碼

Try to use fewer local variables in intrinsics

jonathanvdc 8 年之前
父節點
當前提交
64031d996f
共有 2 個文件被更改,包括 37 次插入26 次删除
  1. 26 25
      kernel/modelverse_jit/intrinsics.py
  2. 11 1
      kernel/modelverse_jit/tree_ir.py

+ 26 - 25
kernel/modelverse_jit/intrinsics.py

@@ -62,22 +62,22 @@ def create_get_length(expression):
 # get them right.
 # pylint: disable=I0011,C0103
 def __set_add(a, b):
-    tmp = tree_ir.StoreLocalInstruction(None, a)
+    store_a, load_a = tree_ir.evaluate_and_load(a)
     return tree_ir.create_block(
-        tmp,
-        tree_ir.CreateEdgeInstruction(tmp.create_load(), b),
-        tmp.create_load())
+        store_a,
+        tree_ir.CreateEdgeInstruction(load_a, b),
+        load_a)
 
 def __dict_add(a, b, c):
-    a_tmp = tree_ir.StoreLocalInstruction(None, a)
-    b_tmp = tree_ir.StoreLocalInstruction(None, b)
+    store_a, load_a = tree_ir.evaluate_and_load(a)
+    store_b, load_b = tree_ir.evaluate_and_load(b)
     return tree_ir.create_block(
-        a_tmp,
-        b_tmp,
+        store_a,
+        store_b,
         tree_ir.CreateEdgeInstruction(
-            tree_ir.CreateEdgeInstruction(a_tmp.create_load(), c),
-            b_tmp.create_load()),
-        a_tmp.create_load())
+            tree_ir.CreateEdgeInstruction(load_a, c),
+            load_b),
+        load_a)
 
 def __list_read(a, b):
     # The statements in this function generate the following code:
@@ -89,17 +89,17 @@ def __list_read(a, b):
     #     raise Exception("List read out of bounds: %s" % b_value)
     # result
 
-    a_tmp = tree_ir.StoreLocalInstruction(None, a)
+    store_a, load_a = tree_ir.evaluate_and_load(a)
     b_val = tree_ir.StoreLocalInstruction(
         None,
         tree_ir.ReadValueInstruction(b))
     result = tree_ir.StoreLocalInstruction(
         None,
         tree_ir.ReadDictionaryValueInstruction(
-            a_tmp.create_load(), b_val.create_load()))
+            load_a.create_load(), b_val.create_load()))
 
     return tree_ir.create_block(
-        a_tmp,
+        store_a,
         b_val,
         result,
         tree_ir.SelectInstruction(
@@ -126,17 +126,18 @@ def __list_append(a, b):
     # _ = yield [("CD", [a_tmp, len(a_outgoing), b_tmp])]
     # a
 
-    a_tmp = tree_ir.StoreLocalInstruction(None, a)
-    b_tmp = tree_ir.StoreLocalInstruction(None, b)
+    store_a, load_a = tree_ir.evaluate_and_load(a)
+    store_b, load_b = tree_ir.evaluate_and_load(b)
     return tree_ir.create_block(
-        a_tmp,
+        store_a,
+        store_b,
         tree_ir.CreateDictionaryEdgeInstruction(
-            a_tmp.create_load(),
+            load_a,
             create_get_length(
                 tree_ir.ReadOutgoingEdgesInstruction(
-                    a_tmp.create_load())),
-            b_tmp),
-        a_tmp.create_load())
+                    load_a)),
+            load_b),
+        load_a)
 
 def __log(a):
     # Original definition:
@@ -146,18 +147,18 @@ def __log(a):
     #     print("== LOG == " + str(a_value))
     #     raise PrimitiveFinished(a)
 
-    a_tmp = tree_ir.StoreLocalInstruction(None, a)
+    store_a, load_a = tree_ir.evaluate_and_load(a)
     return tree_ir.CompoundInstruction(
         tree_ir.create_block(
-            a_tmp,
+            store_a,
             tree_ir.PrintInstruction(
                 tree_ir.BinaryInstruction(
                     tree_ir.LiteralInstruction("== LOG == "),
                     '+',
                     tree_ir.CallInstruction(
                         tree_ir.LoadGlobalInstruction('str'),
-                        [tree_ir.ReadValueInstruction(a_tmp.create_load())])))),
-        a_tmp.create_load())
+                        [tree_ir.ReadValueInstruction(load_a)])))),
+        load_a)
 
 MISC_INTRINSICS = {
     # Reference equality

+ 11 - 1
kernel/modelverse_jit/tree_ir.py

@@ -1543,6 +1543,16 @@ def create_jit_call(target, named_arguments, kwargs):
         RunGeneratorFunctionInstruction(
             target, arg_dict.create_load(), NODE_RESULT_TYPE))
 
+def evaluate_and_load(value):
+    """Creates a statement that evaluates the given tree, and creates
+       an expression that loads the result. These instructions are returned
+       as a pair of trees."""
+    if isinstance(value, (LoadLocalInstruction, LiteralInstruction)):
+        return EmptyInstruction(), value
+    else:
+        store = StoreLocalInstruction(None, value)
+        return IgnoreInstruction(store), store.create_load()
+
 def create_new_local_node(local_variable, connected_node, edge_variable=None):
     """Creates a local node that is the backing storage for a local variable.
        This node is connected to a given node to make sure it's not perceived
@@ -1553,7 +1563,7 @@ def create_new_local_node(local_variable, connected_node, edge_variable=None):
     if edge_variable is not None:
         create_edge = StoreLocalInstruction(edge_variable, create_edge)
 
-    return create_block(local_store, create_edge)
+    return create_block(IgnoreInstruction(local_store), IgnoreInstruction(create_edge))
 
 def map_instruction_tree_top_down(function, instruction):
     """Applies the given mapping function to every instruction in the tree