introduce Switch node
[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         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
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                 const ir_edge_t *edge2;
120
121                 enqueue_node(succ, waitq);
122
123                 /* Also enqueue Phis to prevent inconsistencies. */
124                 if (is_Block(succ)) {
125                         foreach_out_edge(succ, edge2) {
126                                 ir_node *succ2 = get_edge_src_irn(edge2);
127
128                                 if (is_Phi(succ2)) {
129                                         enqueue_node(succ2, waitq);
130                                 }
131                         }
132                 } else if (get_irn_mode(succ) == mode_T) {
133                 /* A mode_T node has Proj's. Because most optimizations
134                         run on the Proj's we have to enqueue them also. */
135                         enqueue_users(succ, waitq);
136                 }
137         }
138 }
139
140 /**
141  * Block-Walker: uses dominance depth to mark dead blocks.
142  */
143 static void find_unreachable_blocks(ir_node *block, void *env)
144 {
145         pdeq *waitq = (pdeq*) env;
146
147         if (get_Block_dom_depth(block) < 0) {
148                 ir_graph *irg = get_irn_irg(block);
149                 ir_node  *end = get_irg_end(irg);
150
151                 const ir_edge_t *edge;
152                 foreach_block_succ(block, edge) {
153                         const ir_edge_t *edge2;
154                         ir_node *succ_block = get_edge_src_irn(edge);
155                         enqueue_node(succ_block, waitq);
156                         foreach_out_edge(succ_block, edge2) {
157                                 ir_node *succ = get_edge_src_irn(edge2);
158                                 if (is_Phi(succ))
159                                         enqueue_node(succ, waitq);
160                         }
161                 }
162                 enqueue_node(end, waitq);
163         }
164 }
165
166 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
167 void local_optimize_graph(ir_graph *irg)
168 {
169         ir_graph *rem = current_ir_graph;
170         current_ir_graph = irg;
171
172         do_local_optimize(get_irg_end(irg));
173
174         current_ir_graph = rem;
175 }
176
177 /**
178  * Data flow optimization walker.
179  * Optimizes all nodes and enqueue its users
180  * if done.
181  */
182 static void opt_walker(ir_node *n, void *env)
183 {
184         pdeq *waitq = (pdeq*)env;
185         ir_node *optimized;
186
187         optimized = optimize_in_place_2(n);
188         set_irn_link(optimized, NULL);
189
190         if (optimized != n) {
191                 enqueue_users(n, waitq);
192                 exchange(n, optimized);
193         }
194 }
195
196 int optimize_graph_df(ir_graph *irg)
197 {
198         pdeq     *waitq = new_pdeq();
199         ir_graph *rem = current_ir_graph;
200         ir_node  *end;
201
202         current_ir_graph = irg;
203
204         if (get_opt_global_cse())
205                 set_irg_pinned(irg, op_pin_state_floats);
206
207         /* enable unreachable code elimination */
208         assert(!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE));
209         set_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
210
211         new_identities(irg);
212         edges_assure(irg);
213         assure_doms(irg);
214
215
216         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
217         irg_walk_graph(irg, NULL, opt_walker, waitq);
218
219         /* any optimized nodes are stored in the wait queue,
220          * so if it's not empty, the graph has been changed */
221         while (!pdeq_empty(waitq)) {
222                 /* finish the wait queue */
223                 while (! pdeq_empty(waitq)) {
224                         ir_node *n = (ir_node*)pdeq_getl(waitq);
225                         opt_walker(n, waitq);
226                 }
227                 /* Calculate dominance so we can kill unreachable code
228                  * We want this intertwined with localopts for better optimization (phase coupling) */
229                 compute_doms(irg);
230                 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
231         }
232         del_pdeq(waitq);
233         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
234
235         /* disable unreachable code elimination */
236         clear_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
237         set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
238
239         /* invalidate infos */
240         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
241         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
242         clear_irg_state(irg, IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
243         edges_deactivate(irg);
244
245         /* Finally kill BAD and doublets from the keep alives.
246          * Doing this AFTER edges where deactivated saves cycles */
247         end = get_irg_end(irg);
248         remove_End_Bads_and_doublets(end);
249
250         current_ir_graph = rem;
251
252         /* Note we do not have a reliable way to detect changes, since some
253          * localopt rules change the inputs of a node and do not return a new
254          * node, so we conservatively say true here */
255         return true;
256 }
257
258 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
259 {
260         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
261 }