+/***************************************************************************/
+
+/**
+ * specialized version of irg_walk_in_or_dep_2, called if only pre callback exists
+ *
+ * @return number of visited nodes
+ */
+static unsigned
+irg_walk_in_or_dep_2_pre(ir_node *node, irg_walk_func *pre, void *env) {
+ int i;
+ unsigned cnt = 1;
+ ir_graph *irg = current_ir_graph;
+
+ set_irn_visited(node, irg->visited);
+
+ pre(node, env);
+
+ if (node->op != op_Block) {
+ ir_node *pred = get_irn_n(node, -1);
+ if (pred->visited < irg->visited)
+ cnt += irg_walk_in_or_dep_2_pre(pred, pre, env);
+ }
+ for (i = get_irn_ins_or_deps(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_in_or_dep(node, i);
+ if (pred->visited < irg->visited)
+ cnt += irg_walk_in_or_dep_2_pre(pred, pre, env);
+ }
+ return cnt;
+}
+
+/**
+ * specialized version of irg_walk_in_or_dep_2, called if only post callback exists
+ *
+ * @return number of visited nodes
+ */
+static unsigned
+irg_walk_in_or_dep_2_post(ir_node *node, irg_walk_func *post, void *env) {
+ int i;
+ unsigned cnt = 1;
+ ir_graph *irg = current_ir_graph;
+
+ set_irn_visited(node, irg->visited);
+
+ if (node->op != op_Block) {
+ ir_node *pred = get_irn_n(node, -1);
+ if (pred->visited < irg->visited)
+ cnt += irg_walk_in_or_dep_2_post(pred, post, env);
+ }
+ for (i = get_irn_ins_or_deps(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_in_or_dep(node, i);
+ if (pred->visited < irg->visited)
+ cnt += irg_walk_in_or_dep_2_post(pred, post, env);
+ }
+
+ post(node, env);
+
+ return cnt;
+}
+
+/**
+ * specialized version of irg_walk_in_or_dep_2, called if pre and post callbacks exist
+ *
+ * @return number of visited nodes
+ */
+static unsigned
+irg_walk_in_or_dep_2_both(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env) {
+ int i;
+ unsigned cnt = 1;
+ ir_graph *irg = current_ir_graph;
+
+ set_irn_visited(node, irg->visited);
+
+ pre(node, env);
+
+ if (node->op != op_Block) {
+ ir_node *pred = get_irn_n(node, -1);
+ if (pred->visited < irg->visited)
+ cnt += irg_walk_in_or_dep_2_both(pred, pre, post, env);
+ }
+ for (i = get_irn_ins_or_deps(node) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_in_or_dep(node, i);
+ if (pred->visited < irg->visited)
+ cnt += irg_walk_in_or_dep_2_both(pred, pre, post, env);
+ }
+
+ post(node, env);
+
+ return cnt;
+}
+
+/**
+ * Intraprozedural graph walker. Follows dependency edges as well.
+ *
+ * @return number of visited nodes
+ */
+static unsigned
+irg_walk_in_or_dep_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
+ if (node->visited < current_ir_graph->visited) {
+ if (! post) return irg_walk_in_or_dep_2_pre (node, pre, env);
+ else if (! pre) return irg_walk_in_or_dep_2_post(node, post, env);
+ else return irg_walk_in_or_dep_2_both(node, pre, post, env);
+ }
+ return 0;
+}
+
+/*
+ * Generic graph walker. Follows dependency edges as well.
+ */
+void irg_walk_in_or_dep(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void *env)
+{
+ assert(is_ir_node(node));
+
+ if (get_interprocedural_view()) {
+ assert(0 && "This is not yet implemented.");
+ } else {
+ inc_irg_visited(current_ir_graph);
+ nodes_touched = irg_walk_in_or_dep_2(node, pre, post, env);
+ }
+ return;
+}
+
+/*
+ * Walk over a graph. Follow all edges (including dependencies)
+ */
+void irg_walk_in_or_dep_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
+ ir_graph * rem = current_ir_graph;
+
+ hook_irg_walk(irg, (generic_func *)pre, (generic_func *)post);
+ current_ir_graph = irg;
+ irg_walk_in_or_dep(get_irg_end(irg), pre, post, env);
+ irg->estimated_node_count = nodes_touched;
+ current_ir_graph = rem;
+}
+
+/***************************************************************************/
+
+/**
+ * Returns current_ir_graph and sets it to the irg of predecessor index
+ * of node n.
+ */
+static INLINE ir_graph *
+switch_irg (ir_node *n, int index) {
+ ir_graph *old_current = current_ir_graph;
+
+ if (get_interprocedural_view()) {
+ /* Only Filter and Block nodes can have predecessors in other graphs. */
+ if (get_irn_op(n) == op_Filter)
+ n = get_nodes_block(n);
+ if (get_irn_op(n) == op_Block) {
+ ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
+ if (is_ip_cfop(cfop)) {
+ current_ir_graph = get_irn_irg(cfop);
+ }
+ }
+ }
+
+ return old_current;
+}
+
+static void
+cg_walk_2(ir_node *node, irg_walk_func *pre, irg_walk_func *post, void * env)
+{
+ int i;
+ ir_graph *rem = NULL;
+ assert(node);
+
+ if (get_irn_visited(node) < get_irg_visited(current_ir_graph)) {
+ set_irn_visited(node, get_irg_visited(current_ir_graph));
+
+ if (pre) pre(node, env);
+
+ if (is_no_Block(node))
+ cg_walk_2(get_nodes_block(node), pre, post, env);
+ for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+ rem = switch_irg(node, i); /* @@@ AS: Is this wrong? We do have to
+ switch to the irg of the predecessor, don't we? */
+ cg_walk_2(get_irn_n(node, i), pre, post, env);
+ current_ir_graph = rem;
+ }
+
+ if (post) post(node, env);
+ }
+ return;
+}
+
+
+/* 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;
+ ir_graph *rem = current_ir_graph;
+ int rem_view = get_interprocedural_view();
+
+ set_interprocedural_view(1);
+
+ inc_max_irg_visited();
+ /* Fix all irg_visited flags */
+ for (i = 0; i < get_irp_n_irgs(); i++)
+ set_irg_visited(get_irp_irg(i), get_max_irg_visited());
+
+ /* Walk starting at unreachable procedures. Only these
+ * have End blocks visible in interprocedural view. */
+ for (i = 0; i < get_irp_n_irgs(); i++) {
+ ir_node *sb;
+ current_ir_graph = get_irp_irg(i);
+
+ sb = get_irg_start_block(current_ir_graph);
+
+ if ((get_Block_n_cfgpreds(sb) > 1) ||
+ (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
+
+ cg_walk_2(get_irg_end(current_ir_graph), pre, post, env);
+ }
+
+ /* Check whether we walked all procedures: there could be procedures
+ with cyclic calls but no call from the outside. */
+ for (i = 0; i < get_irp_n_irgs(); i++) {
+ ir_node *sb;
+ current_ir_graph = get_irp_irg(i);
+
+ /* Test start block: if inner procedure end and end block are not
+ * visible and therefore not marked. */
+ sb = get_irg_start_block(current_ir_graph);
+ if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) {
+ cg_walk_2(sb, pre, post, env);
+ }
+ }
+
+ /* Walk all endless loops in inner procedures.
+ * We recognize an inner procedure if the End node is not visited. */
+ for (i = 0; i < get_irp_n_irgs(); i++) {
+ ir_node *e;
+ current_ir_graph = get_irp_irg(i);
+ e = get_irg_end(current_ir_graph);
+ if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
+ int j;
+ /* Don't visit the End node. */
+ for (j = 0; j < get_End_n_keepalives(e); j++)
+ cg_walk_2(get_End_keepalive(e, j), pre, post, env);
+ }
+ }
+
+ set_interprocedural_view(rem_view);
+ current_ir_graph = rem;
+}
+
+