e14267a91cc374dec637a7b73196dc088463d2ec
[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_outs_inconsistent(irg);
82         set_irg_doms_inconsistent(irg);
83         set_irg_loopinfo_inconsistent(irg);
84
85         /* Clean the value_table in irg for the CSE. */
86         new_identities(irg);
87
88         /* walk over the graph */
89         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
90 }
91
92 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
93 void local_optimize_node(ir_node *n)
94 {
95         ir_graph *rem = current_ir_graph;
96         current_ir_graph = get_irn_irg(n);
97
98         do_local_optimize(n);
99
100         current_ir_graph = rem;
101 }
102
103 /**
104  * Enqueue all users of a node to a wait queue.
105  * Handles mode_T nodes.
106  */
107 static void enqueue_users(ir_node *n, pdeq *waitq)
108 {
109         const ir_edge_t *edge;
110
111         foreach_out_edge(n, edge) {
112                 ir_node *succ = get_edge_src_irn(edge);
113
114                 if (get_irn_link(succ) != waitq) {
115                         pdeq_putr(waitq, succ);
116                         set_irn_link(succ, waitq);
117                 }
118                 if (get_irn_mode(succ) == mode_T) {
119                 /* A mode_T node has Proj's. Because most optimizations
120                         run on the Proj's we have to enqueue them also. */
121                         enqueue_users(succ, waitq);
122                 }
123         }
124 }
125
126 /**
127  * Block-Walker: uses dominance depth to mark dead blocks.
128  */
129 static void kill_dead_blocks(ir_node *block, void *env)
130 {
131         pdeq *waitq = (pdeq*) env;
132
133         if (get_Block_dom_depth(block) < 0) {
134                 /*
135                  * Note that the new dominance code correctly handles
136                  * the End block, i.e. it is always reachable from Start
137                  */
138                 ir_graph *irg = get_irn_irg(block);
139                 enqueue_users(block, waitq);
140                 exchange(block, new_r_Bad(irg, mode_BB));
141         }
142 }
143
144 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
145 void local_optimize_graph(ir_graph *irg)
146 {
147         ir_graph *rem = current_ir_graph;
148         current_ir_graph = irg;
149
150         do_local_optimize(get_irg_end(irg));
151
152         current_ir_graph = rem;
153 }
154
155 /**
156  * Data flow optimization walker.
157  * Optimizes all nodes and enqueue its users
158  * if done.
159  */
160 static void opt_walker(ir_node *n, void *env)
161 {
162         pdeq *waitq = (pdeq*)env;
163         ir_node *optimized;
164
165         optimized = optimize_in_place_2(n);
166         set_irn_link(optimized, NULL);
167
168         if (optimized != n) {
169                 enqueue_users(n, waitq);
170                 exchange(n, optimized);
171         }
172 }
173
174 static void clear_block_phis(ir_node *node, void *env) {
175         (void) env;
176         if (is_Block(node)) {
177                 set_Block_phis(node, NULL);
178         }
179 }
180
181 static void collect_block_phis(ir_node *node, void *env) {
182         (void) env;
183         if (is_Phi(node)) {
184                 add_Block_phi(get_nodes_block(node), node);
185         }
186 }
187
188 static int count_non_bads(ir_node *node) {
189         int arity = get_irn_arity(node);
190         int count = 0;
191         int i;
192         for (i=0; i<arity; ++i) {
193                 if (!is_Bad(get_irn_n(node, i)))
194                         count++;
195         }
196         return count;
197 }
198
199 static void block_remove_bads(ir_node *block, int *changed) {
200         int i, j;
201         ir_node **new_in;
202         const int max = get_irn_arity(block);
203         const int new_max = count_non_bads(block);
204         assert (max >= new_max);
205
206         if (is_Bad(block) || max == new_max) return;
207
208         new_in = ALLOCAN(ir_node*, new_max);
209         *changed = 1;
210
211         assert (get_Block_dom_depth(block) >= 0);
212
213         /* 1. Create a new block without Bad inputs */
214         j = 0;
215         for (i = 0; i < max; ++i) {
216                 ir_node *block_pred = get_irn_n(block, i);
217                 if (!is_Bad(block_pred)) {
218                         new_in[j++] = block_pred;
219                 }
220         }
221         assert (j == new_max);
222
223         /* If the end block is unreachable, it might have zero predecessors. */
224         ir_node *end_block = get_irg_end_block(get_irn_irg(block));
225         if (new_max == 0 && block == end_block) {
226                 set_irn_in(block, new_max, new_in);
227                 return;
228         }
229
230         ir_node *new_block =  new_r_Block(get_irn_irg(block), new_max, new_in);
231
232         /* 2. Remove inputs on Phis, where the block input is Bad. */
233         ir_node *phi = get_Block_phis(block);
234         if (phi != NULL) {
235                 do {
236                         ir_node* next = get_Phi_next(phi);
237                         if (get_irn_arity(phi) != new_max) {
238                                 j = 0;
239                                 for (i = 0; i < max; ++i) {
240                                         ir_node *block_pred = get_irn_n(block, i);
241
242                                         if (!is_Bad(block_pred)) {
243                                                 ir_node *pred = get_irn_n(phi, i);
244                                                 new_in[j++] = pred;
245                                         }
246                                 }
247                                 assert (j == new_max);
248
249                                 ir_node *new_phi = new_r_Phi(new_block, new_max, new_in, get_irn_mode(phi));
250                                 exchange(phi, new_phi);
251                         }
252                         phi = next;
253                 } while (phi != NULL);
254         }
255
256         exchange(block, new_block);
257 }
258
259 /* Remove Bad nodes from Phi and Block inputs.
260  *
261  * Precondition: No unreachable code.
262  * Postcondition: No Bad nodes.
263  */
264 static int remove_Bads(ir_graph *irg) {
265         int changed = 0;
266         /* build phi list per block */
267         irg_walk_graph(irg, clear_block_phis, collect_block_phis, NULL);
268
269         /* actually remove Bads */
270         irg_block_walk_graph(irg, NULL, (void (*)(struct ir_node *, void *)) block_remove_bads, &changed);
271
272         return changed;
273 }
274
275 /* Applies local optimizations to all nodes in the graph until fixpoint. */
276 int optimize_graph_df(ir_graph *irg)
277 {
278         pdeq     *waitq = new_pdeq();
279         ir_graph *rem = current_ir_graph;
280         ir_node  *end;
281         int      state, changed;
282
283         current_ir_graph = irg;
284
285         state = edges_assure(irg);
286
287         /* Clean the value_table in irg for the CSE. */
288         new_identities(irg);
289
290         if (get_opt_global_cse()) {
291                 set_irg_pinned(irg, op_pin_state_floats);
292         }
293
294         /* The following enables unreachable code elimination (=Blocks may be
295          * Bad). */
296         set_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
297
298         /* invalidate info */
299         set_irg_outs_inconsistent(irg);
300         set_irg_doms_inconsistent(irg);
301         set_irg_loopinfo_inconsistent(irg);
302
303         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
304
305         /* walk over the graph, but don't touch keep-alives */
306         irg_walk_graph(irg, NULL, opt_walker, waitq);
307
308         /* any optimized nodes are stored in the wait queue,
309          * so if it's not empty, the graph has been changed */
310         changed = !pdeq_empty(waitq);
311
312         do {
313                 /* finish the wait queue */
314                 while (! pdeq_empty(waitq)) {
315                         ir_node *n = (ir_node*)pdeq_getl(waitq);
316                         opt_walker(n, waitq);
317                 }
318                 /* kill newly generated unreachable code */
319                 set_irg_outs_inconsistent(irg);
320                 compute_doms(irg);
321                 irg_block_walk_graph(irg, NULL, kill_dead_blocks, waitq);
322         } while (! pdeq_empty(waitq));
323
324         del_pdeq(waitq);
325
326         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
327
328         if (! state)
329                 edges_deactivate(irg);
330
331         /* Finally kill BAD and doublets from the keep alives.
332            Doing this AFTER edges where deactivated saves cycles */
333         end = get_irg_end(irg);
334         remove_End_Bads_and_doublets(end);
335
336         if (remove_Bads(irg)) {
337                 edges_deactivate(irg);
338                 set_irg_outs_inconsistent(irg);
339         }
340
341         clear_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
342
343         current_ir_graph = rem;
344         return changed;
345 }
346
347 /* Creates an ir_graph pass for optimize_graph_df. */
348 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
349 {
350         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
351 }  /* optimize_graph_df_pass */