- Changed keep logic: all nodes can be keeped yet, thi is necessary to fix fehler152
[libfirm] / ir / ir / irgwalk.c
index cb3935c..cb6795d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
 #endif
 
 #include "irnode_t.h"
-#include "irgraph_t.h" /* visited flag */
+#include "irgraph_t.h"
 #include "irprog.h"
 #include "irgwalk.h"
 #include "irhooks.h"
 #include "ircgcons.h"
+#include "entity_t.h"
 
+#include "error.h"
 #include "pset_new.h"
 #include "array.h"
 
@@ -251,6 +253,7 @@ void irg_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
 {
        assert(is_ir_node(node));
 
+#ifdef INTERPROCEDURAL_VIEW
        if (get_interprocedural_view()) {
                pset_new_t           irg_set;
                pset_new_iterator_t  iter;
@@ -271,12 +274,14 @@ void irg_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
                irg_walk_cg(node, visited, &irg_set, pre, post, env);
                pset_new_destroy(&irg_set);
        } else {
-               set_using_visited(current_ir_graph);
+#endif
+               ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
                inc_irg_visited(current_ir_graph);
                nodes_touched = irg_walk_2(node, pre, post, env);
-               clear_using_visited(current_ir_graph);
+               ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
+#ifdef INTERPROCEDURAL_VIEW
        }
-       return;
+#endif
 }
 
 /*
@@ -421,12 +426,11 @@ void irg_walk_in_or_dep(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
        if (get_interprocedural_view()) {
                assert(0 && "This is not yet implemented.");
        } else {
-               set_using_visited(current_ir_graph);
+               ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
                inc_irg_visited(current_ir_graph);
                nodes_touched = irg_walk_in_or_dep_2(node, pre, post, env);
-               clear_using_visited(current_ir_graph);
+               ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
        }
-       return;
 }
 
 /*
@@ -490,10 +494,9 @@ cg_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env)
 
                if (post) post(node, env);
        }
-       return;
 }
 
-
+#ifdef INTERPROCEDURAL_VIEW
 /* Walks all irgs in interprocedural view.  Visits each node only once. */
 void cg_walk(irg_walk_func *pre, irg_walk_func *post, void *env) {
        int i;
@@ -552,21 +555,19 @@ void cg_walk(irg_walk_func *pre, irg_walk_func *post, void *env) {
        set_interprocedural_view(rem_view);
        current_ir_graph = rem;
 }
+#endif
 
 
 /***************************************************************************/
 
 /* Walks back from n until it finds a real cf op. */
 static ir_node *get_cf_op(ir_node *n) {
-       ir_node *pred;
-
-       n = skip_Id(n);
-       n = skip_Tuple(n);
-       pred = skip_Proj(n);
-       if (!(is_cfop(pred) || is_fragile_op(pred) || is_Bad(pred)))
-               n = get_cf_op(n);
-
-       return skip_Proj(n);
+       while (!is_cfop(n) && !is_fragile_op(n) && !is_Bad(n)) {
+               n = skip_Id(n);
+               n = skip_Tuple(n);
+               n = skip_Proj(n);
+       }
+       return n;
 }
 
 static void irg_block_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
@@ -600,16 +601,17 @@ static void irg_block_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *p
    flag, so that it can be interleaved with the other walker.         */
 void irg_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
 {
+       ir_graph *irg = current_ir_graph;
        ir_node *block, *pred;
        int i;
 
-       hook_irg_block_walk(current_ir_graph, node, (generic_func *)pre, (generic_func *)post);
+       hook_irg_block_walk(irg, node, (generic_func *)pre, (generic_func *)post);
 
        assert(node);
        assert(!get_interprocedural_view());   /* interprocedural_view not implemented, because it
                                               * interleaves with irg_walk */
-       set_using_block_visited(current_ir_graph);
-       inc_irg_block_visited(current_ir_graph);
+       ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
+       inc_irg_block_visited(irg);
        block = is_Block(node) ? node : get_nodes_block(node);
        assert(get_irn_op(block) == op_Block);
        irg_block_walk_2(block, pre, post, env);
@@ -635,7 +637,7 @@ void irg_block_walk(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void
                }
        }
 
-       clear_using_block_visited(current_ir_graph);
+       ir_free_resources(irg, IR_RESOURCE_BLOCK_VISITED);
 }
 
 /*
@@ -653,15 +655,12 @@ void irg_block_walk_graph(ir_graph *irg, irg_walk_func *pre,
  * Additionally walk over all anchors. Do NOT increase the visit flag.
  */
 void irg_walk_anchors(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
-       int i;
        ir_graph * rem = current_ir_graph;
        current_ir_graph = irg;
 
-       for (i = 0; i < anchor_max; ++i) {
-               ir_node *anchor = irg->anchors[i];
+       inc_irg_visited(irg);
+       irg_walk_2(irg->anchor, pre, post, env);
 
-               irg_walk_2(anchor, pre, post, env);
-       }
        current_ir_graph = rem;
 }
 
@@ -673,6 +672,29 @@ typedef struct walk_env {
        void *env;
 } walk_env;
 
+static void walk_initializer(ir_initializer_t *initializer, walk_env *env)
+{
+       switch(initializer->kind) {
+    case IR_INITIALIZER_CONST:
+       irg_walk(initializer->consti.value, env->pre, env->post, env->env);
+        return;
+    case IR_INITIALIZER_TARVAL:
+    case IR_INITIALIZER_NULL:
+        return;
+
+    case IR_INITIALIZER_COMPOUND: {
+        size_t i;
+        for(i = 0; i < initializer->compound.n_initializers; ++i) {
+            ir_initializer_t *subinitializer
+                = initializer->compound.initializers[i];
+            walk_initializer(subinitializer, env);
+        }
+        return;
+    }
+       }
+       panic("invalid initializer found");
+}
+
 /**
  * Walk to all constant expressions in this entity.
  */
@@ -681,7 +703,9 @@ static void walk_entity(ir_entity *ent, void *env)
        walk_env *my_env = (walk_env *)env;
 
        if (get_entity_variability(ent) != variability_uninitialized) {
-               if (is_atomic_entity(ent)) {
+               if (ent->has_initializer) {
+                       walk_initializer(ent->attr.initializer, my_env);
+               } else if (is_atomic_entity(ent)) {
                        irg_walk(get_atomic_ent_value(ent), my_env->pre, my_env->post, my_env->env);
                } else {
                        int i, n_vals = get_compound_ent_n_values(ent);
@@ -694,7 +718,7 @@ static void walk_entity(ir_entity *ent, void *env)
 
 /* Walks over all code in const_code_irg. */
 void walk_const_code(irg_walk_func *pre, irg_walk_func *post, void *env) {
-       int i, j;
+       int i, j, n_types;
        walk_env my_env;
 
        ir_graph *rem = current_ir_graph;
@@ -707,16 +731,18 @@ void walk_const_code(irg_walk_func *pre, irg_walk_func *post, void *env) {
 
        /* Walk all types that can contain constant entities.  */
        walk_types_entities(get_glob_type(), &walk_entity, &my_env);
-       for (i = 0; i < get_irp_n_types(); i++)
+       n_types = get_irp_n_types();
+       for (i = 0; i < n_types; i++)
                walk_types_entities(get_irp_type(i), &walk_entity, &my_env);
        for (i = 0; i < get_irp_n_irgs(); i++)
                walk_types_entities(get_irg_frame_type(get_irp_irg(i)), &walk_entity, &my_env);
 
        /* Walk constant array bounds. */
-       for (i = 0; i < get_irp_n_types(); i++) {
+       for (i = 0; i < n_types; i++) {
                ir_type *tp = get_irp_type(i);
                if (is_Array_type(tp)) {
-                       for (j = 0; j < get_array_n_dimensions(tp); j++) {
+                       int n_dim = get_array_n_dimensions(tp);
+                       for (j = 0; j < n_dim; j++) {
                                ir_node *n = get_array_lower_bound(tp, j);
                                if (n) irg_walk(n, pre, post, env);
                                n = get_array_upper_bound(tp, j);