same fix again on another place
[libfirm] / ir / ir / iredges.c
index 3ac96e0..f920896 100644 (file)
@@ -1,30 +1,34 @@
 /*
- * Project:     libFIRM
- * File name:   ir/ir/iredges.c
- * Purpose:     Always available outs.
- * Author:      Sebastian Hack
- * Modified by: Michael Beck, Andreas Schoesser
- * Created:     14.1.2005
- * CVS-ID:      $Id$
- * Copyright:   (c) 1998-2006 Universität Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
 /**
- * Always available outs.
- * @author Sebastian Hack
- * @date 14.1.2005
+ * @file
+ * @brief   Always available outs.
+ * @author  Sebastian Hack, Michael Beck, Andreas Schoesser
+ * @date    14.1.2005
+ * @version $Id$
+ * @summary
+ *   This are out-edges (also called def-use edges) that are dynamically
+ *   updated as the graph changes.
  */
-
 #ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#ifdef HAVE_ALLOCA_H
-#include <alloca.h>
-#endif
-#ifdef HAVE_MALLOC_H
-#include <malloc.h>
+#include "config.h"
 #endif
 
 #include "irnode_t.h"
 #include "debug.h"
 #include "set.h"
 #include "bitset.h"
+#include "xmalloc.h"
 
 /**
-* A function that allows for setting an edge.
-* This abstraction is necessary since different edge kind have
-* different methods of setting edges.
-*/
+ * A function that allows for setting an edge.
+ * This abstraction is necessary since different edge kind have
+ * different methods of setting edges.
+ */
 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
 
 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
@@ -53,8 +58,8 @@ typedef int (get_edge_src_first_func_t)(const ir_node *src);
 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
 
 /**
-* Additional data for an edge kind.
-*/
+ * Additional data for an edge kind.
+ */
 typedef struct {
        const char                *name;
        set_edge_func_t           *set_edge;
@@ -65,6 +70,7 @@ typedef struct {
 
 static int get_zero(const ir_node *irn)
 {
+       (void) irn;
        return 0;
 }
 
@@ -108,8 +114,10 @@ static int edges_private_size = 0;
  */
 static int edges_dbg = 0;
 
+#ifdef DEBUG_libfirm
 /* a static variable holding the last number assigned to a new edge */
 static long last_edge_num = -1;
+#endif
 
 static INLINE long edge_get_id(const ir_edge_t *e) {
 #ifdef DEBUG_libfirm
@@ -162,6 +170,7 @@ static int edge_cmp(const void *p1, const void *p2, size_t len)
 {
        const ir_edge_t *e1 = p1;
        const ir_edge_t *e2 = p2;
+       (void) len;
 
        if(e1->src != e2->src)
                return 1;
@@ -229,10 +238,33 @@ const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
 
 /**
  * Change the out count
+ *
+ * @param tgt  the edge target
+ * @param kind the kind of the edge
  */
 static INLINE void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs) {
        irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
        info->out_count += ofs;
+
+#if 0
+       assert(info->out_count >= 0);
+       if (info->out_count == 0 && kind == EDGE_KIND_NORMAL) {
+               /* tgt lost it's last user */
+               int i;
+
+               for (i = get_irn_arity(tgt) - 1; i >= -1; --i) {
+                       ir_node *prev = get_irn_n(tgt, i);
+
+                       edges_notify_edge(tgt, i, NULL, prev, current_ir_graph);
+               }
+               for (i = get_irn_deps(tgt) - 1; i >= 0; --i) {
+                       ir_node *prev = get_irn_dep(tgt, i);
+
+                       edges_notify_edge_kind(tgt, i, NULL, prev, EDGE_KIND_DEP, current_ir_graph);
+
+               }
+       }
+#endif
 }
 
 /**
@@ -254,7 +286,7 @@ static INLINE void vrfy_list_head(ir_node *irn, ir_edge_kind_t kind) {
                        fprintf(stderr, "- at list entry %d\n", num);
                        if (edge->invalid)
                                fprintf(stderr, "- edge(%ld) is invalid\n", edge_get_id(edge));
-                       if (edge->src);
+                       if (edge->src)
                                ir_fprintf(stderr, "- edge(%ld) %+F(%d)\n", edge_get_id(edge), edge->src, edge->pos);
                        err = 1;
                        break;
@@ -368,6 +400,7 @@ void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
                edge_change_cnt(tgt, kind, +1);
        } /* else */
 
+#ifndef DEBUG_libfirm
        /* verify list heads */
        if (edges_dbg) {
                if (tgt)
@@ -375,6 +408,7 @@ void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
                if (old_tgt)
                        vrfy_list_head(old_tgt, kind);
        }
+#endif
 
        DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
 }
@@ -397,8 +431,14 @@ void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir
        }
 }
 
-
-void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
+/**
+ * Delete all in edges of a given kind from the node old.
+ *
+ * @param old   the node
+ * @param kind  the kind of edges to remove
+ * @param irg   the irg of the old node
+ */
+static void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
 {
        int i, n;
 
@@ -487,8 +527,6 @@ void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
 
        info->activated = 1;
        edges_init_graph_kind(irg, kind);
-       //irg_walk_graph(irg, init_lh_walker, build_edges_walker, &w);
-       inc_irg_visited(irg);
        irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
        visit_all_identities(irg, visitor, &w);
 }
@@ -643,6 +681,7 @@ static void clear_links(ir_node *irn, void *env) {
 static void count_user(ir_node *irn, void *env) {
        int i;
        int first;
+       (void) env;
 
        first = get_irn_first(irn);
        for (i = get_irn_arity(irn) - 1; i >= first; --i) {
@@ -707,7 +746,7 @@ static void verify_edge_counter(ir_node *irn, void *env) {
                ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d node(s), but the list contains %d edge(s)\n",
                        irn, ref_cnt, list_cnt);
 
-               // Matze: buggy if a node has multiple ins pointing at irn
+               /* Matze: buggy if a node has multiple ins pointing at irn */
 #if 0
                list_for_each(pos, head) {
                        ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
@@ -738,9 +777,6 @@ int edges_verify(ir_graph *irg) {
        struct build_walker w;
        int    problem_found = 0;
 
-       if (! edges_dbg)
-               return 0;
-
        /* verify normal edges only */
        problem_found  = edges_verify_kind(irg, EDGE_KIND_NORMAL);
 
@@ -749,9 +785,7 @@ int edges_verify(ir_graph *irg) {
        w.problem_found = 0;
 
        /* verify counter */
-       inc_irg_visited(irg);
        irg_walk_anchors(irg, clear_links, count_user, &w);
-       inc_irg_visited(irg);
        irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
 
        return problem_found ? 1 : w.problem_found;
@@ -833,3 +867,38 @@ void dump_all_out_edges(ir_node *irn)
                }
        }
 }
+
+static void irg_block_edges_walk2(ir_node *bl,
+                                irg_walk_func *pre, irg_walk_func *post,
+                                void *env) {
+       const ir_edge_t *edge, *next;
+
+       if (Block_not_block_visited(bl)) {
+               mark_Block_block_visited(bl);
+
+               if (pre)
+                       pre(bl, env);
+
+               foreach_out_edge_kind_safe(bl, edge, next, EDGE_KIND_BLOCK) {
+                       /* find the corresponding successor block. */
+                       ir_node *pred = get_edge_src_irn(edge);
+                       irg_block_edges_walk2(pred, pre, post, env);
+               }
+
+               if (post)
+                       post(bl, env);
+       }
+}
+
+/* Walks only over Block nodes in the graph.  Has it's own visited
+   flag, so that it can be interleaved with the other walker.         */
+void irg_block_edges_walk(ir_node *node,
+                          irg_walk_func *pre, irg_walk_func *post,
+                          void *env) {
+
+       assert(edges_activated(current_ir_graph));
+       assert(is_Block(node));
+
+       inc_irg_block_visited(current_ir_graph);
+       irg_block_edges_walk2(node, pre, post, env);
+}