+/*
+ * Project: libFIRM
+ * File name: ir/ir/iredges.c
+ * Purpose: Always available outs.
+ * Author: Sebastian Hack
+ * Modified by: Michael Beck
+ * Created: 14.1.2005
+ * CVS-ID: $Id$
+ * Copyright: (c) 1998-2006 Universität Karlsruhe
+ * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
+ */
+
/**
* Always available outs.
* @author Sebastian Hack
#endif
#include "irnode_t.h"
+#include "iropt_t.h"
#include "iredges_t.h"
#include "irgwalk.h"
#include "irdump_t.h"
#include "debug.h"
#include "set.h"
-static firm_dbg_module_t *dbg;
-
-#if FIRM_EDGES_INPLACE
+DEBUG_ONLY(static firm_dbg_module_t *dbg;)
/**
* This flag is set to 1, if the edges get initialized for an irg.
/**
* Initialize the out information for a graph.
- * @note Dead node elim can call this on an already initialized graph.
+ * @note Dead node elimination can call this on an already initialized graph.
*/
void edges_init_graph(ir_graph *irg)
{
if(edges_activated(irg)) {
irg_edge_info_t *info = _get_irg_edge_info(irg);
size_t size = EDGE_SIZE(src);
- ir_edge_t *templ = alloca(size);
+ ir_edge_t key;
- memset(templ, 0, size);
- templ->src = (ir_node *) src;
- templ->pos = pos;
- return set_find(info->edges, templ, size, edge_hash(templ));
+ key.src = (ir_node *) src;
+ key.pos = pos;
+ return set_find(info->edges, &key, size, edge_hash(&key));
}
return NULL;
}
+/**
+ * Change the out count
+ */
+static INLINE void edge_change_cnt(ir_node *tgt, int ofs) {
+ irn_edge_info_t *info = _get_irn_edge_info(tgt);
+ info->out_count += ofs;
+}
+
+/* The edge from (src, pos) -> old_tgt is redirected to tgt */
void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
{
const char *msg = "";
ir_edge_t *edge;
/*
- * This is scray, but:
+ * This is scary, but:
* If two entries in a set do not have the same size, they are
* treated as unequal, ignoring the comparison function.
* So, edges from blocks have extra storage (they are
/* Initialize the edge template to search in the set. */
memset(templ, 0, size);
-#ifdef DEBUG_libfirm
- templ->src_nr = get_irn_node_nr(src);
-#endif
templ->src = src;
templ->pos = pos;
templ->invalid = 0;
templ->present = 0;
+ DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
/*
* If the target is NULL, the edge shall be deleted.
*/
- if(tgt == NULL) {
+ if (tgt == NULL) {
/* search the edge in the set. */
edge = set_find(edges, templ, size, edge_hash(templ));
* If the edge is a cf edge, we delete it also
* from the list of all block successor edges.
*/
- if(is_block_edge)
+ if(is_block_edge) {
list_del(&block_edge->succ_list);
+ edge_change_cnt(old_tgt, -1);
+ }
}
/* If the edge was not found issue a warning on the debug stream */
/* If the old target is not null, the edge is moved. */
if(old_tgt) {
msg = "redirecting";
+
list_move(&edge->list, head);
/* If the edge is a cf edge, move it from the successor list. */
if(is_block_edge)
list_move(&block_edge->succ_list, succ_head);
- _get_irn_edge_info(old_tgt)->out_count -= 1;
+ edge_change_cnt(old_tgt, -1);
}
/* The old target was null, thus, the edge is newly created. */
list_add(&block_edge->succ_list, succ_head);
}
- _get_irn_edge_info(tgt)->out_count += 1;
+ edge_change_cnt(tgt, +1);
} /* else */
}
/* If the target and the old target are equal, nothing is done. */
- DBG((dbg, LEVEL_5, "announce out edge: %n[%p] %d-> %n[%p](%n[%p]): %s\n",
- src, src, pos, tgt, tgt, old_tgt, old_tgt, msg));
+ DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n",
+ src, pos, tgt, old_tgt, msg));
}
void edges_node_deleted(ir_node *old, ir_graph *irg)
{
if(edges_activated(irg)) {
int not_a_block = !is_Block(old);
- ir_edge_t templ;
int i, n;
- templ.src = old;
- DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
+ DBG((dbg, LEVEL_5, "node deleted: %+F\n", old));
/* Change to get_irn_n */
for(i = -not_a_block, n = get_irn_arity(old); i < n; ++i) {
ir_node *old_tgt = get_irn_n(old, i);
- DBG((dbg, LEVEL_5, "\tdelete to old target %n\n", old_tgt));
+ DBG((dbg, LEVEL_5, "\tdelete to old target %+F\n", old_tgt));
edges_notify_edge(old, i, NULL, old_tgt, irg);
}
}
}
-void edges_invalidate(ir_node *irn, ir_graph *irg)
-{
+void edges_invalidate(ir_node *irn, ir_graph *irg) {
edges_node_deleted(irn, irg);
}
-static void build_edges_walker(ir_node *irn, void *data)
-{
+/**
+ * Post-Walker: notify all edges
+ */
+static void build_edges_walker(ir_node *irn, void *data) {
ir_graph *irg = data;
int not_a_block = !is_Block(irn);
int i, n;
- for(i = -not_a_block, n = get_irn_arity(irn); i < n; ++i)
+ for (i = -not_a_block, n = get_irn_arity(irn); i < n; ++i)
edges_notify_edge(irn, i, get_irn_n(irn, i), NULL, irg);
}
-static void init_lh_walker(ir_node *irn, void *data)
-{
+/**
+ * Pre-Walker: initializes the list-heads and set the out-count
+ * of all nodes to 0.
+ */
+static void init_lh_walker(ir_node *irn, void *data) {
INIT_LIST_HEAD(_get_irn_outs_head(irn));
- if(is_Block(irn))
+ if (is_Block(irn))
INIT_LIST_HEAD(_get_block_succ_head(irn));
+ _get_irn_edge_info(irn)->out_count = 0;
+}
+
+/**
+ * Visitor: initializes the list-heads and set the out-count
+ * of all nodes to 0 of nodes that are not seen so far.
+ */
+static void visitor(ir_node *irn, void *data) {
+ if (irn_not_visited(irn)) {
+ mark_irn_visited(irn);
+ init_lh_walker(irn, data);
+ }
}
+/*
+ * Build the initial edge set.
+ * Beware, this is not a simple task because it suffers from two
+ * difficulties:
+ * - the anchor set allows access to Nodes that may not be reachable from
+ * the End node
+ * - the identities add nodes to the "root set" that are not yet reachable
+ * from End. However, after some transformations, the CSE may revival these
+ * nodes
+ *
+ * These problems can be fixed using different strategies:
+ * - Add an age flag to every node. Whenever the edge of a node is older
+ * then the current edge, invalidate the edges of this node.
+ * While this would help for revivaled nodes, it increases memory and runtime.
+ * - Delete the identities set.
+ * Solves the revival problem, but may increase the memory consumption, as
+ * nodes cannot be revivaled at all.
+ * - Manually iterate over the identities root set. This did not consume more memory
+ * but increase the computation time because the |identies| >= |V|
+ *
+ * Currently, we use the last option.
+ */
void edges_activate(ir_graph *irg)
{
irg_edge_info_t *info = _get_irg_edge_info(irg);
info->activated = 1;
edges_init_graph(irg);
irg_walk_graph(irg, init_lh_walker, build_edges_walker, irg);
+ irg_walk_anchors(irg, init_lh_walker, NULL, irg);
+ visit_all_identities(irg, visitor, irg);
}
void edges_deactivate(ir_graph *irg)
irg_edge_info_t *info = _get_irg_edge_info(irg);
info->activated = 0;
- if(info->edges) {
+ if (info->edges) {
del_set(info->edges);
- info->edges = NULL;
- }
+ info->edges = NULL;
+ }
}
int (edges_activated)(const ir_graph *irg)
struct list_head *head = _get_irn_outs_head(from);
DBG((dbg, LEVEL_5,
- "reroute from %n to %n\n", from, to));
+ "reroute from %+F to %+F\n", from, to));
while(head != head->next) {
ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
int i, n;
for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
- ir_block_edge_t space;
+ ir_block_edge_t space;
ir_edge_t *templ = (ir_edge_t *) &space;
ir_edge_t *e;
- size_t size = not_a_block ? sizeof(ir_edge_t) : sizeof(ir_block_edge_t);
+ size_t size = not_a_block ? sizeof(ir_edge_t) : sizeof(ir_block_edge_t);
templ->src = irn;
templ->pos = i - not_a_block;
if(e != NULL)
e->present = 1;
else
- DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is missing\n", irn, templ->pos));
+ DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is missing\n", irn, templ->pos));
}
}
foreach_out_edge(irn, e) {
ir_node *tgt = get_irn_n(e->src, e->pos);
if(irn != tgt)
- DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is no out edge of %n but of %n\n",
+ DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is no out edge of %+F but of %+F\n",
e->src, e->pos, irn, tgt));
}
*/
for(e = set_first(edges); e; e = set_next(edges)) {
if(!e->invalid && !e->present)
- DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is superfluous\n", e->src, e->pos));
+ DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is superfluous\n", e->src, e->pos));
}
}
int (get_irn_n_edges)(const ir_node *irn)
{
- return _get_irn_n_edges(irn);
+ return _get_irn_n_edges(irn);
}
-
-
-#endif /* FIRM_EDGES_INPLACE */