+/***************************************************************************/
+
+/* 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 (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 = interprocedural_view;
+
+ interprocedural_view = true;
+
+ 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);
+ }
+ }
+
+ interprocedural_view = rem_view;
+ current_ir_graph = rem;
+}
+
+