-static void
-copy_graph_env (int copy_node_nr) {
- ir_node *old_end;
- /* Not all nodes remembered in current_ir_graph might be reachable
- from the end node. Assure their link is set to NULL, so that
- we can test whether new nodes have been computed. */
- set_irn_link(get_irg_frame (current_ir_graph), NULL);
- set_irn_link(get_irg_globals (current_ir_graph), NULL);
- set_irn_link(get_irg_args (current_ir_graph), NULL);
- set_irn_link(get_irg_initial_mem(current_ir_graph), NULL);
- set_irn_link(get_irg_bad (current_ir_graph), NULL);
- set_irn_link(get_irg_no_mem (current_ir_graph), NULL);
-
- /* we use the block walk flag for removing Bads from Blocks ins. */
- inc_irg_block_visited(current_ir_graph);
-
- /* copy the graph */
- copy_graph(copy_node_nr);
-
- /* fix the fields in current_ir_graph */
- old_end = get_irg_end(current_ir_graph);
- set_irg_end (current_ir_graph, get_new_node(old_end));
- set_irg_end_except (current_ir_graph, get_irg_end(current_ir_graph));
- set_irg_end_reg (current_ir_graph, get_irg_end(current_ir_graph));
- free_End(old_end);
- set_irg_end_block (current_ir_graph, get_new_node(get_irg_end_block(current_ir_graph)));
-
- if (get_irn_link(get_irg_frame(current_ir_graph)) == NULL) {
- copy_node (get_irg_frame(current_ir_graph), INT_TO_PTR(copy_node_nr));
- copy_preds(get_irg_frame(current_ir_graph), NULL);
- }
- if (get_irn_link(get_irg_globals(current_ir_graph)) == NULL) {
- copy_node (get_irg_globals(current_ir_graph), INT_TO_PTR(copy_node_nr));
- copy_preds(get_irg_globals(current_ir_graph), NULL);
- }
- if (get_irn_link(get_irg_initial_mem(current_ir_graph)) == NULL) {
- copy_node (get_irg_initial_mem(current_ir_graph), INT_TO_PTR(copy_node_nr));
- copy_preds(get_irg_initial_mem(current_ir_graph), NULL);
- }
- if (get_irn_link(get_irg_args(current_ir_graph)) == NULL) {
- copy_node (get_irg_args(current_ir_graph), INT_TO_PTR(copy_node_nr));
- copy_preds(get_irg_args(current_ir_graph), NULL);
- }
- if (get_irn_link(get_irg_bad(current_ir_graph)) == NULL) {
- copy_node(get_irg_bad(current_ir_graph), INT_TO_PTR(copy_node_nr));
- copy_preds(get_irg_bad(current_ir_graph), NULL);
- }
- if (get_irn_link(get_irg_no_mem(current_ir_graph)) == NULL) {
- copy_node(get_irg_no_mem(current_ir_graph), INT_TO_PTR(copy_node_nr));
- copy_preds(get_irg_no_mem(current_ir_graph), NULL);
- }
- set_irg_start (current_ir_graph, get_new_node(get_irg_start(current_ir_graph)));
- set_irg_start_block(current_ir_graph, get_new_node(get_irg_start_block(current_ir_graph)));
- set_irg_frame (current_ir_graph, get_new_node(get_irg_frame(current_ir_graph)));
- set_irg_globals (current_ir_graph, get_new_node(get_irg_globals(current_ir_graph)));
- set_irg_initial_mem(current_ir_graph, get_new_node(get_irg_initial_mem(current_ir_graph)));
- set_irg_args (current_ir_graph, get_new_node(get_irg_args(current_ir_graph)));
- set_irg_bad (current_ir_graph, get_new_node(get_irg_bad(current_ir_graph)));
- set_irg_no_mem (current_ir_graph, get_new_node(get_irg_no_mem(current_ir_graph)));
-}
-
-/**
- * Copies all reachable nodes to a new obstack. Removes bad inputs
- * from block nodes and the corresponding inputs from Phi nodes.
- * Merges single exit blocks with single entry blocks and removes
- * 1-input Phis.
- * Adds all new nodes to a new hash table for CSE. Does not
- * perform CSE, so the hash table might contain common subexpressions.
- */
-void
-dead_node_elimination(ir_graph *irg) {
- ir_graph *rem;
- int rem_ipview = get_interprocedural_view();
- struct obstack *graveyard_obst = NULL;
- struct obstack *rebirth_obst = NULL;
-
- if (get_opt_optimize() && get_opt_dead_node_elimination()) {
- assert(! edges_activated(irg) && "dead node elimination requieres disabled edges");
-
- /* inform statistics that we started a dead-node elimination run */
- hook_dead_node_elim(irg, 1);
-
- /* Remember external state of current_ir_graph. */
- rem = current_ir_graph;
- current_ir_graph = irg;
- set_interprocedural_view(0);
-
- assert(get_irg_phase_state(current_ir_graph) != phase_building);
-
- /* Handle graph state */
- free_callee_info(current_ir_graph);
- free_irg_outs(current_ir_graph);
- free_trouts();
-
- /* @@@ so far we loose loops when copying */
- free_loop_information(current_ir_graph);
-
- set_irg_doms_inconsistent(irg);
-
- /* A quiet place, where the old obstack can rest in peace,
- until it will be cremated. */
- graveyard_obst = irg->obst;
-
- /* A new obstack, where the reachable nodes will be copied to. */
- rebirth_obst = xmalloc (sizeof(*rebirth_obst));
- current_ir_graph->obst = rebirth_obst;
- obstack_init (current_ir_graph->obst);
-
- /* We also need a new hash table for cse */
- del_identities (irg->value_table);
- irg->value_table = new_identities ();
-
- /* Copy the graph from the old to the new obstack */
- copy_graph_env(1);
-
- /* Free memory from old unoptimized obstack */
- obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
- xfree (graveyard_obst); /* ... then free it. */
-
- /* inform statistics that the run is over */
- hook_dead_node_elim(irg, 0);
-
- current_ir_graph = rem;
- set_interprocedural_view(rem_ipview);
- }
-}
-
-/**
- * Relink bad predecessors of a block and store the old in array to the
- * link field. This function is called by relink_bad_predecessors().
- * The array of link field starts with the block operand at position 0.
- * If block has bad predecessors, create a new in array without bad preds.
- * Otherwise let in array untouched.
- */
-static void relink_bad_block_predecessors(ir_node *n, void *env) {
- ir_node **new_in, *irn;
- int i, new_irn_n, old_irn_arity, new_irn_arity = 0;
-
- /* if link field of block is NULL, look for bad predecessors otherwise
- this is already done */
- if (get_irn_op(n) == op_Block &&
- get_irn_link(n) == NULL) {
-
- /* save old predecessors in link field (position 0 is the block operand)*/
- set_irn_link(n, get_irn_in(n));
-
- /* count predecessors without bad nodes */
- old_irn_arity = get_irn_arity(n);
- for (i = 0; i < old_irn_arity; i++)
- if (!is_Bad(get_irn_n(n, i))) new_irn_arity++;
-
- /* arity changing: set new predecessors without bad nodes */
- if (new_irn_arity < old_irn_arity) {
- /* Get new predecessor array. We do not resize the array, as we must
- keep the old one to update Phis. */
- new_in = NEW_ARR_D (ir_node *, current_ir_graph->obst, (new_irn_arity+1));
-
- /* set new predecessors in array */
- new_in[0] = NULL;
- new_irn_n = 1;
- for (i = 0; i < old_irn_arity; i++) {
- irn = get_irn_n(n, i);
- if (!is_Bad(irn)) {
- new_in[new_irn_n] = irn;
- is_backedge(n, i) ? set_backedge(n, new_irn_n-1) : set_not_backedge(n, new_irn_n-1);
- ++new_irn_n;
- }
- }
- //ARR_SETLEN(int, n->attr.block.backedge, new_irn_arity);
- ARR_SHRINKLEN(n->attr.block.backedge, new_irn_arity);
- n->in = new_in;
-
- } /* ir node has bad predecessors */
-
- } /* Block is not relinked */
-}
-
-/**
- * Relinks Bad predecessors from Blocks and Phis called by walker
- * remove_bad_predecesors(). If n is a Block, call
- * relink_bad_block_redecessors(). If n is a Phi-node, call also the relinking
- * function of Phi's Block. If this block has bad predecessors, relink preds
- * of the Phi-node.
- */
-static void relink_bad_predecessors(ir_node *n, void *env) {
- ir_node *block, **old_in;
- int i, old_irn_arity, new_irn_arity;
-
- /* relink bad predecessors of a block */
- if (get_irn_op(n) == op_Block)
- relink_bad_block_predecessors(n, env);
-
- /* If Phi node relink its block and its predecessors */
- if (get_irn_op(n) == op_Phi) {
-
- /* Relink predecessors of phi's block */
- block = get_nodes_block(n);
- if (get_irn_link(block) == NULL)
- relink_bad_block_predecessors(block, env);
-
- old_in = (ir_node **)get_irn_link(block); /* Of Phi's Block */
- old_irn_arity = ARR_LEN(old_in);
-
- /* Relink Phi predecessors if count of predecessors changed */
- if (old_irn_arity != ARR_LEN(get_irn_in(block))) {
- /* set new predecessors in array
- n->in[0] remains the same block */
- new_irn_arity = 1;
- for(i = 1; i < old_irn_arity; i++)
- if (!is_Bad((ir_node *)old_in[i])) {
- n->in[new_irn_arity] = n->in[i];
- is_backedge(n, i) ? set_backedge(n, new_irn_arity) : set_not_backedge(n, new_irn_arity);
- ++new_irn_arity;
- }
-
- ARR_SETLEN(ir_node *, n->in, new_irn_arity);
- ARR_SETLEN(int, n->attr.phi_backedge, new_irn_arity);
- }
-
- } /* n is a Phi node */
-}
-
-/*
- * Removes Bad Bad predecessors from Blocks and the corresponding
- * inputs to Phi nodes as in dead_node_elimination but without
- * copying the graph.
- * On walking up set the link field to NULL, on walking down call
- * relink_bad_predecessors() (This function stores the old in array
- * to the link field and sets a new in array if arity of predecessors
- * changes).
- */
-void remove_bad_predecessors(ir_graph *irg) {
- irg_walk_graph(irg, init_link, relink_bad_predecessors, NULL);
-}
-
-
-/*--------------------------------------------------------------------*/
-/* Functionality for inlining */
-/*--------------------------------------------------------------------*/
-
-/**
- * Copy node for inlineing. Updates attributes that change when
- * inlineing but not for dead node elimination.
- *
- * Copies the node by calling copy_node() and then updates the entity if
- * it's a local one. env must be a pointer of the frame type of the
- * inlined procedure. The new entities must be in the link field of
- * the entities.
- */
-static INLINE void
-copy_node_inline (ir_node *n, void *env) {
- ir_node *new;
- ir_type *frame_tp = (ir_type *)env;
-
- copy_node(n, NULL);
- if (get_irn_op(n) == op_Sel) {
- new = get_new_node (n);
- assert(get_irn_op(new) == op_Sel);
- if (get_entity_owner(get_Sel_entity(n)) == frame_tp) {
- set_Sel_entity(new, get_entity_link(get_Sel_entity(n)));
- }
- } else if (get_irn_op(n) == op_Block) {
- new = get_new_node (n);
- new->attr.block.irg = current_ir_graph;
- }
-}
-
-static void find_addr(ir_node *node, void *env)
-{
- if (get_irn_opcode(node) == iro_Proj) {
- if (get_Proj_proj(node) == pn_Start_P_value_arg_base)
- *(int *)env = 0;
- }
-}
-
-/*
- * currently, we cannot inline two cases:
- * - call with compound arguments
- * - graphs that take the address of a parameter
- *
- * check these conditions here
- */
-static int can_inline(ir_node *call, ir_graph *called_graph)