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