- fixed off-by-one error after phase refactoring
[libfirm] / ir / opt / dead_code_elimination.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
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 $
25  *
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
30  * the old one.
31  */
32 #include "config.h"
33
34 #include "iroptimize.h"
35 #include "irnode_t.h"
36 #include "irgraph_t.h"
37 #include "irphase_t.h"
38 #include "iredges_t.h"
39 #include "irhooks.h"
40 #include "irtools.h"
41 #include "irgwalk.h"
42 #include "cgana.h"
43 #include "irouts.h"
44 #include "trouts.h"
45 #include "iropt_t.h"
46 #include "irpass.h"
47 #include "pmap.h"
48
49 /** a pointer to the new phases */
50 static ir_phase *new_phases[PHASE_LAST + 1];
51
52 /**
53  * Reroute the inputs of a node from nodes in the old graph to copied nodes in
54  * the new graph
55  */
56 static void rewire_inputs(ir_node *node, void *env)
57 {
58         (void) env;
59         irn_rewire_inputs(node);
60 }
61
62 static void copy_node_dce(ir_node *node, void *env)
63 {
64         int       i;
65         ir_node  *new_node = exact_copy(node);
66         ir_graph *irg      = get_irn_irg(new_node);
67         (void) env;
68
69         /* preserve the node numbers for easier debugging */
70         new_node->node_nr = node->node_nr;
71
72         /* copy phase information for this node */
73         for (i = 0; i <= PHASE_LAST; i++) {
74                 ir_phase *phase = irg_get_phase(irg, i);
75                 if (phase == NULL)
76                         continue;
77                 if (!phase_get_irn_data(phase, node))
78                         continue;
79                 phase_set_irn_data(new_phases[i], new_node,
80                                    phase_get_irn_data(phase, node));
81         }
82
83         set_irn_link(node, new_node);
84         hook_dead_node_elim_subst(irg, node, new_node);
85 }
86
87 /**
88  * Copies the graph reachable from current_ir_graph->end to the obstack
89  * in current_ir_graph and fixes the environment.
90  * Then fixes the fields in current_ir_graph containing nodes of the
91  * graph.
92  *
93  * @param copy_node_nr  If non-zero, the node number will be copied
94  */
95 static void copy_graph_env(ir_graph *irg)
96 {
97         ir_node *new_anchor;
98         int i;
99
100         /* init the new_phases array */
101         /* TODO: this is wrong, it should only allocate a new data_ptr inside
102          * the phase! */
103         for (i = 0; i <= PHASE_LAST; i++) {
104                 ir_phase *old_ph = irg_get_phase(irg, i);
105                 if (old_ph == NULL) {
106                         new_phases[i] = NULL;
107                 } else {
108                         new_phases[i] = new_phase(irg, old_ph->data_init);
109                         new_phases[i]->priv = old_ph->priv;
110                 }
111         }
112
113         /* copy nodes */
114         irg_walk_anchors(irg, copy_node_dce, rewire_inputs, NULL);
115
116         /* fix the anchor */
117         new_anchor = get_irn_link(irg->anchor);
118         assert(new_anchor != NULL);
119         irg->anchor = new_anchor;
120
121         /* copy the new phases into the irg */
122         for (i = 0; i <= PHASE_LAST; i++) {
123                 ir_phase *old_ph = irg_get_phase(irg, i);
124                 if (old_ph == NULL)
125                         continue;
126
127                 phase_free(old_ph);
128                 irg->phases[i] = new_phases[i];
129         }
130 }
131
132 /**
133  * Copies all reachable nodes to a new obstack.  Removes bad inputs
134  * from block nodes and the corresponding inputs from Phi nodes.
135  * Merges single exit blocks with single entry blocks and removes
136  * 1-input Phis.
137  * Adds all new nodes to a new hash table for CSE.  Does not
138  * perform CSE, so the hash table might contain common subexpressions.
139  */
140 void dead_node_elimination(ir_graph *irg)
141 {
142         ir_graph *rem;
143 #ifdef INTERPROCEDURAL_VIEW
144         int rem_ipview = get_interprocedural_view();
145 #endif
146         struct obstack *graveyard_obst = NULL;
147         struct obstack *rebirth_obst   = NULL;
148
149         edges_deactivate(irg);
150
151         /* inform statistics that we started a dead-node elimination run */
152         hook_dead_node_elim(irg, 1);
153
154         /* Remember external state of current_ir_graph. */
155         rem = current_ir_graph;
156         current_ir_graph = irg;
157 #ifdef INTERPROCEDURAL_VIEW
158         set_interprocedural_view(0);
159 #endif
160
161         assert(get_irg_phase_state(irg) != phase_building);
162
163         /* Handle graph state */
164         free_callee_info(irg);
165         free_irg_outs(irg);
166         free_trouts();
167         free_loop_information(irg);
168         set_irg_doms_inconsistent(irg);
169
170         /* A quiet place, where the old obstack can rest in peace,
171            until it will be cremated. */
172         graveyard_obst = irg->obst;
173
174         /* A new obstack, where the reachable nodes will be copied to. */
175         rebirth_obst = XMALLOC(struct obstack);
176         irg->obst = rebirth_obst;
177         obstack_init(irg->obst);
178         irg->last_node_idx = 0;
179
180         /* We also need a new value table for CSE */
181         del_identities(irg->value_table);
182         irg->value_table = new_identities();
183
184         /* Copy the graph from the old to the new obstack */
185         copy_graph_env(irg);
186
187         /* Free memory from old unoptimized obstack */
188         obstack_free(graveyard_obst, 0);  /* First empty the obstack ... */
189         xfree(graveyard_obst);            /* ... then free it.           */
190
191         /* inform statistics that the run is over */
192         hook_dead_node_elim(irg, 0);
193
194         current_ir_graph = rem;
195 #ifdef INTERPROCEDURAL_VIEW
196         set_interprocedural_view(rem_ipview);
197 #endif
198 }
199
200 ir_graph_pass_t *dead_node_elimination_pass(const char *name)
201 {
202         return def_graph_pass(name ? name : "dce", dead_node_elimination);
203 }
204
205
206 /*
207    __                      _  __ __
208   (_     __    o     _    | \/  |_
209   __)|_| | \_/ | \_/(/_   |_/\__|__
210
211   The following stuff implements a facility that automatically patches
212   registered ir_node pointers to the new node when a dead node elimination occurs.
213
214
215   Warning: This is considered a hack - try hard to avoid this!
216 */
217
218 struct _survive_dce_t {
219         struct obstack obst;
220         pmap *places;
221         pmap *new_places;
222         hook_entry_t dead_node_elim;
223         hook_entry_t dead_node_elim_subst;
224 };
225
226 typedef struct _survive_dce_list_t {
227         struct _survive_dce_list_t *next;
228         ir_node **place;
229 } survive_dce_list_t;
230
231 static void dead_node_hook(void *context, ir_graph *irg, int start)
232 {
233         survive_dce_t *sd = context;
234         (void) irg;
235
236         /* Create a new map before the dead node elimination is performed. */
237         if (start) {
238                 sd->new_places = pmap_create_ex(pmap_count(sd->places));
239         } else {
240                 /* Patch back all nodes if dead node elimination is over and something is to be done. */
241                 pmap_destroy(sd->places);
242                 sd->places     = sd->new_places;
243                 sd->new_places = NULL;
244         }
245 }
246
247 /**
248  * Hook called when dead node elimination replaces old by nw.
249  */
250 static void dead_node_subst_hook(void *context, ir_graph *irg, ir_node *old, ir_node *nw)
251 {
252         survive_dce_t *sd = context;
253         survive_dce_list_t *list = pmap_get(sd->places, old);
254         (void) irg;
255
256         /* If the node is to be patched back, write the new address to all registered locations. */
257         if (list) {
258                 survive_dce_list_t *p;
259
260                 for (p = list; p; p = p->next)
261                         *(p->place) = nw;
262
263                 pmap_insert(sd->new_places, nw, list);
264         }
265 }
266
267 /**
268  * Make a new Survive DCE environment.
269  */
270 survive_dce_t *new_survive_dce(void)
271 {
272         survive_dce_t *res = XMALLOC(survive_dce_t);
273         obstack_init(&res->obst);
274         res->places     = pmap_create();
275         res->new_places = NULL;
276
277         res->dead_node_elim.hook._hook_dead_node_elim = dead_node_hook;
278         res->dead_node_elim.context                   = res;
279         res->dead_node_elim.next                      = NULL;
280
281         res->dead_node_elim_subst.hook._hook_dead_node_elim_subst = dead_node_subst_hook;
282         res->dead_node_elim_subst.context = res;
283         res->dead_node_elim_subst.next    = NULL;
284
285         register_hook(hook_dead_node_elim, &res->dead_node_elim);
286         register_hook(hook_dead_node_elim_subst, &res->dead_node_elim_subst);
287         return res;
288 }
289
290 /**
291  * Free a Survive DCE environment.
292  */
293 void free_survive_dce(survive_dce_t *sd)
294 {
295         obstack_free(&sd->obst, NULL);
296         pmap_destroy(sd->places);
297         unregister_hook(hook_dead_node_elim, &sd->dead_node_elim);
298         unregister_hook(hook_dead_node_elim_subst, &sd->dead_node_elim_subst);
299         xfree(sd);
300 }
301
302 /**
303  * Register a node pointer to be patched upon DCE.
304  * When DCE occurs, the node pointer specified by @p place will be
305  * patched to the new address of the node it is pointing to.
306  *
307  * @param sd    The Survive DCE environment.
308  * @param place The address of the node pointer.
309  */
310 void survive_dce_register_irn(survive_dce_t *sd, ir_node **place)
311 {
312         if (*place != NULL) {
313                 ir_node *irn      = *place;
314                 survive_dce_list_t *curr = pmap_get(sd->places, irn);
315                 survive_dce_list_t *nw   = OALLOC(&sd->obst, survive_dce_list_t);
316
317                 nw->next  = curr;
318                 nw->place = place;
319
320                 pmap_insert(sd->places, irn, nw);
321         }
322 }