2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Dead node elimination
23 * @author Michael Beck, Goetz Lindenmaier
24 * @version $Id: opt_inline.c 27265 2010-03-07 15:13:00Z matze $
26 * Strictly speaking dead node elimination is unnecessary in firm - everthying
27 * which is not used can't be found by any walker.
28 * The only drawback is that the nodes still take up memory. This phase fixes
29 * this by copying all (reachable) nodes to a new obstack and throwing away
34 #include "iroptimize.h"
36 #include "irgraph_t.h"
37 #include "iredges_t.h"
49 * Reroute the inputs of a node from nodes in the old graph to copied nodes in
52 static void rewire_inputs(ir_node *node, void *env)
55 irn_rewire_inputs(node);
58 static void copy_node_dce(ir_node *node, void *env)
60 ir_node *new_node = exact_copy(node);
61 ir_graph *irg = get_irn_irg(new_node);
64 /* preserve the node numbers for easier debugging */
65 new_node->node_nr = node->node_nr;
67 set_irn_link(node, new_node);
68 hook_dead_node_elim_subst(irg, node, new_node);
72 * Copies the graph reachable from the End node to the obstack
73 * in irg. Then fixes the fields containing nodes of the graph.
75 * @param copy_node_nr If non-zero, the node number will be copied
77 static void copy_graph_env(ir_graph *irg)
82 irg_walk_anchors(irg, copy_node_dce, rewire_inputs, NULL);
85 new_anchor = (ir_node*)get_irn_link(irg->anchor);
86 assert(new_anchor != NULL);
87 irg->anchor = new_anchor;
91 * Copies all reachable nodes to a new obstack. Removes bad inputs
92 * from block nodes and the corresponding inputs from Phi nodes.
93 * Merges single exit blocks with single entry blocks and removes
95 * Adds all new nodes to a new hash table for CSE. Does not
96 * perform CSE, so the hash table might contain common subexpressions.
98 void dead_node_elimination(ir_graph *irg)
100 struct obstack *graveyard_obst = NULL;
101 struct obstack *rebirth_obst = NULL;
103 edges_deactivate(irg);
105 /* inform statistics that we started a dead-node elimination run */
106 hook_dead_node_elim(irg, 1);
108 assert(get_irg_phase_state(irg) != phase_building);
110 /* Handle graph state */
111 free_callee_info(irg);
114 free_loop_information(irg);
116 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
118 /* A quiet place, where the old obstack can rest in peace,
119 until it will be cremated. */
120 graveyard_obst = irg->obst;
122 /* A new obstack, where the reachable nodes will be copied to. */
123 rebirth_obst = XMALLOC(struct obstack);
124 irg->obst = rebirth_obst;
125 obstack_init(irg->obst);
126 irg->last_node_idx = 0;
128 /* We also need a new value table for CSE */
131 /* Copy the graph from the old to the new obstack */
134 /* Free memory from old unoptimized obstack */
135 obstack_free(graveyard_obst, 0); /* First empty the obstack ... */
136 xfree(graveyard_obst); /* ... then free it. */
138 /* inform statistics that the run is over */
139 hook_dead_node_elim(irg, 0);
142 ir_graph_pass_t *dead_node_elimination_pass(const char *name)
144 return def_graph_pass(name ? name : "dce", dead_node_elimination);