From 423040bacd15de038a8bc88e304516c116f11cab Mon Sep 17 00:00:00 2001
From: Martin Bauer <martin.bauer@fau.de>
Date: Wed, 25 Apr 2018 12:29:57 +0200
Subject: [PATCH] Removed restriction for move_constants_before_loop
 optimization

- previously moved only SympyAssignments
- relevant for custom boundary nodes, which are now pulled before loops
---
 transformations.py | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/transformations.py b/transformations.py
index 2347fb7e0..81dd22c29 100644
--- a/transformations.py
+++ b/transformations.py
@@ -455,11 +455,9 @@ def resolve_field_accesses(ast_node, read_only_field_names=set(),
 
 
 def move_constants_before_loop(ast_node):
-    """
-    Moves :class:`pystencils.ast.SympyAssignment` nodes out of loop body if they are iteration independent.
+    """Moves :class:`pystencils.ast.SympyAssignment` nodes out of loop body if they are iteration independent.
+
     Call this after creating the loop structure with :func:`make_loop_over_domain`
-    :param ast_node:
-    :return:
     """
     def find_block_to_move_to(node):
         """
@@ -468,7 +466,6 @@ def move_constants_before_loop(ast_node):
         :param node: SympyAssignment inside a Block
         :return blockToInsertTo, childOfBlockToInsertBefore
         """
-        assert isinstance(node, ast.SympyAssignment)
         assert isinstance(node.parent, ast.Block)
 
         last_block = node.parent
@@ -510,18 +507,18 @@ def move_constants_before_loop(ast_node):
     for block in all_blocks:
         children = block.take_child_nodes()
         for child in children:
-            if not isinstance(child, ast.SympyAssignment):
-                block.append(child)
+            target, child_to_insert_before = find_block_to_move_to(child)
+            if target == block:     # movement not possible
+                target.append(child)
             else:
-                target, child_to_insert_before = find_block_to_move_to(child)
-                if target == block:     # movement not possible
-                    target.append(child)
+                if isinstance(child, ast.SympyAssignment):
+                    exists_already = check_if_assignment_already_in_block(child, target)
                 else:
-                    existing_assignment = check_if_assignment_already_in_block(child, target)
-                    if not existing_assignment:
-                        target.insert_before(child, child_to_insert_before)
-                    else:
-                        assert existing_assignment.rhs == child.rhs, "Symbol with same name exists already"
+                    exists_already = False
+                if not exists_already:
+                    target.insert_before(child, child_to_insert_before)
+                else:
+                    assert exists_already.rhs == child.rhs, "Symbol with same name exists already"
 
 
 def split_inner_loop(ast_node: ast.Node, symbol_groups):
-- 
GitLab