Removed C99 features, adopt to Firm style.
[libfirm] / ir / ir / irgopt.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    Optimizations for a whole ir graph, i.e., a procedure.
23  * @author   Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
24  *           Michael Beck
25  * @version  $Id$
26  */
27 #include "config.h"
28
29 #include <assert.h>
30
31 #include "irnode_t.h"
32 #include "irgraph_t.h"
33
34 #include "iroptimize.h"
35 #include "iropt_t.h"
36 #include "irgopt.h"
37 #include "irgmod.h"
38 #include "irgwalk.h"
39
40 #include "adt/pdeq.h"
41
42 #include "irpass_t.h"
43 #include "irflag_t.h"
44 #include "iredges_t.h"
45 #include "irtools.h"
46
47 /*------------------------------------------------------------------*/
48 /* apply optimizations of iropt to all nodes.                       */
49 /*------------------------------------------------------------------*/
50
51 /**
52  * A wrapper around optimize_inplace_2() to be called from a walker.
53  */
54 static void optimize_in_place_wrapper(ir_node *n, void *env)
55 {
56         ir_node *optimized = optimize_in_place_2(n);
57         (void) env;
58
59         if (optimized != n) {
60                 exchange(n, optimized);
61         }
62 }
63
64 /**
65  * Do local optimizations for a node.
66  *
67  * @param n  the IR-node where to start. Typically the End node
68  *           of a graph
69  *
70  * @note current_ir_graph must be set
71  */
72 static inline void do_local_optimize(ir_node *n)
73 {
74         ir_graph *irg = get_irn_irg(n);
75
76         /* Handle graph state */
77         assert(get_irg_phase_state(irg) != phase_building);
78
79         if (get_opt_global_cse())
80                 set_irg_pinned(irg, op_pin_state_floats);
81         set_irg_doms_inconsistent(irg);
82
83         /* Clean the value_table in irg for the CSE. */
84         new_identities(irg);
85
86         /* walk over the graph */
87         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
88 }
89
90 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
91 void local_optimize_node(ir_node *n)
92 {
93         ir_graph *rem = current_ir_graph;
94         current_ir_graph = get_irn_irg(n);
95
96         do_local_optimize(n);
97
98         current_ir_graph = rem;
99 }
100
101 static void enqueue_node(ir_node *node, pdeq *waitq)
102 {
103         if (get_irn_link(node) == waitq)
104                 return;
105         pdeq_putr(waitq, node);
106         set_irn_link(node, waitq);
107 }
108
109 /**
110  * Enqueue all users of a node to a wait queue.
111  * Handles mode_T nodes.
112  */
113 static void enqueue_users(ir_node *n, pdeq *waitq)
114 {
115         const ir_edge_t *edge;
116
117         foreach_out_edge(n, edge) {
118                 ir_node *succ = get_edge_src_irn(edge);
119
120                 enqueue_node(succ, waitq);
121                 if (get_irn_mode(succ) == mode_T) {
122                 /* A mode_T node has Proj's. Because most optimizations
123                         run on the Proj's we have to enqueue them also. */
124                         enqueue_users(succ, waitq);
125                 }
126         }
127 }
128
129 /**
130  * Block-Walker: uses dominance depth to mark dead blocks.
131  */
132 static void find_unreachable_blocks(ir_node *block, void *env)
133 {
134         pdeq *waitq = (pdeq*) env;
135
136         if (get_Block_dom_depth(block) < 0) {
137                 ir_graph *irg = get_irn_irg(block);
138                 ir_node  *end = get_irg_end(irg);
139
140                 const ir_edge_t *edge;
141                 foreach_block_succ(block, edge) {
142                         const ir_edge_t *edge2;
143                         ir_node *succ_block = get_edge_src_irn(edge);
144                         enqueue_node(succ_block, waitq);
145                         foreach_out_edge(succ_block, edge2) {
146                                 ir_node *succ = get_edge_src_irn(edge2);
147                                 if (is_Phi(succ))
148                                         enqueue_node(succ, waitq);
149                         }
150                 }
151                 enqueue_node(end, waitq);
152         }
153 }
154
155 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
156 void local_optimize_graph(ir_graph *irg)
157 {
158         ir_graph *rem = current_ir_graph;
159         current_ir_graph = irg;
160
161         do_local_optimize(get_irg_end(irg));
162
163         current_ir_graph = rem;
164 }
165
166 /**
167  * Data flow optimization walker.
168  * Optimizes all nodes and enqueue its users
169  * if done.
170  */
171 static void opt_walker(ir_node *n, void *env)
172 {
173         pdeq *waitq = (pdeq*)env;
174         ir_node *optimized;
175
176         optimized = optimize_in_place_2(n);
177         set_irn_link(optimized, NULL);
178
179         if (optimized != n) {
180                 enqueue_users(n, waitq);
181                 exchange(n, optimized);
182         }
183 }
184
185 /**
186  * Return the number of non-Bad predecessors of the given node.
187  */
188 static int count_non_bads(ir_node *node)
189 {
190         int arity = get_irn_arity(node);
191         int count = 0;
192         int i;
193         for (i = 0; i < arity; ++i) {
194                 if (!is_Bad(get_irn_n(node, i)))
195                         ++count;
196         }
197         return count;
198 }
199
200 /**
201  * Block-walker, remove Bad block predecessors and shorten Phis.
202  * Phi links must be uptodate.
203  */
204 static void block_remove_bads(ir_node *block, void *env)
205 {
206         int *changed = (int *)env;
207         int i, j;
208         ir_node **new_in, *new_block, *phi;
209         const int max = get_irn_arity(block);
210         const int new_max = count_non_bads(block);
211         assert(max >= new_max);
212
213         if (is_Bad(block) || max == new_max)
214                 return;
215
216         new_in = ALLOCAN(ir_node*, new_max);
217         *changed = 1;
218
219         assert(get_Block_dom_depth(block) >= 0);
220
221         /* 1. Create a new block without Bad inputs */
222         for (i = j = 0; i < max; ++i) {
223                 ir_node *block_pred = get_irn_n(block, i);
224                 if (!is_Bad(block_pred)) {
225                         new_in[j++] = block_pred;
226                 }
227         }
228         assert(j == new_max);
229
230         /* If the end block is unreachable, it might have zero predecessors. */
231         if (new_max == 0) {
232                 ir_node *end_block = get_irg_end_block(get_irn_irg(block));
233                 if (block == end_block) {
234                         set_irn_in(block, new_max, new_in);
235                         return;
236                 }
237         }
238
239         new_block = new_r_Block(get_irn_irg(block), new_max, new_in);
240
241         /* 2. Remove inputs on Phis, where the block input is Bad. */
242         phi = get_Block_phis(block);
243         if (phi != NULL) {
244                 do {
245                         ir_node *next = get_Phi_next(phi);
246                         if (get_irn_arity(phi) != new_max) {
247                                 ir_node *new_phi;
248
249                                 for (i = j = 0; i < max; ++i) {
250                                         ir_node *block_pred = get_irn_n(block, i);
251
252                                         if (!is_Bad(block_pred)) {
253                                                 ir_node *pred = get_irn_n(phi, i);
254                                                 new_in[j++] = pred;
255                                         }
256                                 }
257                                 assert(j == new_max);
258
259                                 new_phi = new_r_Phi(new_block, new_max, new_in, get_irn_mode(phi));
260                                 exchange(phi, new_phi);
261                         }
262                         phi = next;
263                 } while (phi != NULL);
264         }
265
266         exchange(block, new_block);
267 }
268
269 /* Remove Bad nodes from Phi and Block inputs.
270  *
271  * Precondition: No unreachable code.
272  * Postcondition: No Bad nodes.
273  */
274 static int remove_Bads(ir_graph *irg)
275 {
276         int changed = 0;
277         /* build phi list per block */
278         irg_walk_graph(irg, firm_clear_block_phis, firm_collect_block_phis, NULL);
279
280         /* actually remove Bads */
281         irg_block_walk_graph(irg, NULL, block_remove_bads, (void *)&changed);
282
283         return changed;
284 }
285
286 /* Applies local optimizations to all nodes in the graph until fixpoint. */
287 int optimize_graph_df(ir_graph *irg)
288 {
289         pdeq     *waitq = new_pdeq();
290         ir_graph *rem = current_ir_graph;
291         ir_node  *end;
292         int      state, changed;
293
294         current_ir_graph = irg;
295
296         state = edges_assure(irg);
297
298         /* Clean the value_table in irg for the CSE. */
299         new_identities(irg);
300
301         if (get_opt_global_cse()) {
302                 set_irg_pinned(irg, op_pin_state_floats);
303         }
304
305         /* The following enables unreachable code elimination (=Blocks may be
306          * Bad). */
307         set_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
308
309         /* invalidate info */
310         set_irg_doms_inconsistent(irg);
311
312         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
313
314         /* Calculate dominance so we can kill unreachable code */
315         assure_doms(irg);
316
317         /* walk over the graph, but don't touch keep-alives */
318         irg_walk_graph(irg, NULL, opt_walker, waitq);
319
320         /* any optimized nodes are stored in the wait queue,
321          * so if it's not empty, the graph has been changed */
322         changed = !pdeq_empty(waitq);
323
324         while (!pdeq_empty(waitq)) {
325                 /* finish the wait queue */
326                 while (! pdeq_empty(waitq)) {
327                         ir_node *n = (ir_node*)pdeq_getl(waitq);
328                         opt_walker(n, waitq);
329                 }
330                 /* Calculate dominance so we can kill unreachable code */
331                 compute_doms(irg);
332                 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
333         }
334         set_irg_doms_inconsistent(irg);
335
336         del_pdeq(waitq);
337
338         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
339
340         if (! state)
341                 edges_deactivate(irg);
342
343         if (remove_Bads(irg)) {
344                 edges_deactivate(irg);
345         }
346
347         /* Finally kill BAD and doublets from the keep alives.
348            Doing this AFTER edges where deactivated saves cycles */
349         end = get_irg_end(irg);
350         remove_End_Bads_and_doublets(end);
351
352         clear_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
353
354         current_ir_graph = rem;
355         return changed;
356 }
357
358 /* Creates an ir_graph pass for optimize_graph_df. */
359 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
360 {
361         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
362 }  /* optimize_graph_df_pass */