1b280f2ec1caa123e86b0d0daadd2f8a258fbc62
[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  */
26 #include "config.h"
27
28 #include <assert.h>
29
30 #include "irnode_t.h"
31 #include "irgraph_t.h"
32
33 #include "iroptimize.h"
34 #include "iropt_t.h"
35 #include "irgopt.h"
36 #include "irgmod.h"
37 #include "irgwalk.h"
38
39 #include "adt/pdeq.h"
40
41 #include "irpass_t.h"
42 #include "irflag_t.h"
43 #include "iredges_t.h"
44 #include "irtools.h"
45
46 /*------------------------------------------------------------------*/
47 /* apply optimizations of iropt to all nodes.                       */
48 /*------------------------------------------------------------------*/
49
50 /**
51  * A wrapper around optimize_inplace_2() to be called from a walker.
52  */
53 static void optimize_in_place_wrapper(ir_node *n, void *env)
54 {
55         ir_node *optimized = optimize_in_place_2(n);
56         (void) env;
57
58         if (optimized != n) {
59                 exchange(n, optimized);
60         }
61 }
62
63 /**
64  * Do local optimizations for a node.
65  *
66  * @param n  the IR-node where to start. Typically the End node
67  *           of a graph
68  *
69  * @note current_ir_graph must be set
70  */
71 static inline void do_local_optimize(ir_node *n)
72 {
73         ir_graph *irg = get_irn_irg(n);
74
75         /* Handle graph state */
76         assert(get_irg_phase_state(irg) != phase_building);
77
78         if (get_opt_global_cse())
79                 set_irg_pinned(irg, op_pin_state_floats);
80         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
81
82         /* Clean the value_table in irg for the CSE. */
83         new_identities(irg);
84
85         /* walk over the graph */
86         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
87 }
88
89 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
90 void local_optimize_node(ir_node *n)
91 {
92         ir_graph *rem = current_ir_graph;
93         current_ir_graph = get_irn_irg(n);
94
95         do_local_optimize(n);
96
97         current_ir_graph = rem;
98 }
99
100 static void enqueue_node(ir_node *node, pdeq *waitq)
101 {
102         if (get_irn_link(node) == waitq)
103                 return;
104         pdeq_putr(waitq, node);
105         set_irn_link(node, waitq);
106 }
107
108 /**
109  * Enqueue all users of a node to a wait queue.
110  * Handles mode_T nodes.
111  */
112 static void enqueue_users(ir_node *n, pdeq *waitq)
113 {
114         const ir_edge_t *edge;
115
116         foreach_out_edge(n, edge) {
117                 ir_node         *succ  = get_edge_src_irn(edge);
118                 const ir_edge_t *edge2;
119
120                 enqueue_node(succ, waitq);
121
122                 /* Also enqueue Phis to prevent inconsistencies. */
123                 if (is_Block(succ)) {
124                         foreach_out_edge(succ, edge2) {
125                                 ir_node *succ2 = get_edge_src_irn(edge2);
126
127                                 if (is_Phi(succ2)) {
128                                         enqueue_node(succ2, waitq);
129                                 }
130                         }
131                 } else if (get_irn_mode(succ) == mode_T) {
132                 /* A mode_T node has Proj's. Because most optimizations
133                         run on the Proj's we have to enqueue them also. */
134                         enqueue_users(succ, waitq);
135                 }
136         }
137 }
138
139 /**
140  * Block-Walker: uses dominance depth to mark dead blocks.
141  */
142 static void find_unreachable_blocks(ir_node *block, void *env)
143 {
144         pdeq *waitq = (pdeq*) env;
145
146         if (get_Block_dom_depth(block) < 0) {
147                 ir_graph *irg = get_irn_irg(block);
148                 ir_node  *end = get_irg_end(irg);
149
150                 const ir_edge_t *edge;
151                 foreach_block_succ(block, edge) {
152                         const ir_edge_t *edge2;
153                         ir_node *succ_block = get_edge_src_irn(edge);
154                         enqueue_node(succ_block, waitq);
155                         foreach_out_edge(succ_block, edge2) {
156                                 ir_node *succ = get_edge_src_irn(edge2);
157                                 if (is_Phi(succ))
158                                         enqueue_node(succ, waitq);
159                         }
160                 }
161                 enqueue_node(end, waitq);
162         }
163 }
164
165 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
166 void local_optimize_graph(ir_graph *irg)
167 {
168         ir_graph *rem = current_ir_graph;
169         current_ir_graph = irg;
170
171         do_local_optimize(get_irg_end(irg));
172
173         current_ir_graph = rem;
174 }
175
176 /**
177  * Data flow optimization walker.
178  * Optimizes all nodes and enqueue its users
179  * if done.
180  */
181 static void opt_walker(ir_node *n, void *env)
182 {
183         pdeq *waitq = (pdeq*)env;
184         ir_node *optimized;
185
186         optimized = optimize_in_place_2(n);
187         set_irn_link(optimized, NULL);
188
189         if (optimized != n) {
190                 enqueue_users(n, waitq);
191                 exchange(n, optimized);
192         }
193 }
194
195 int optimize_graph_df(ir_graph *irg)
196 {
197         pdeq     *waitq = new_pdeq();
198         ir_graph *rem = current_ir_graph;
199         ir_node  *end;
200
201         current_ir_graph = irg;
202
203         if (get_opt_global_cse())
204                 set_irg_pinned(irg, op_pin_state_floats);
205
206         /* enable unreachable code elimination */
207         assert(!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE));
208         set_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
209
210         new_identities(irg);
211         edges_assure(irg);
212         assure_doms(irg);
213
214
215         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
216         irg_walk_graph(irg, NULL, opt_walker, waitq);
217
218         /* any optimized nodes are stored in the wait queue,
219          * so if it's not empty, the graph has been changed */
220         while (!pdeq_empty(waitq)) {
221                 /* finish the wait queue */
222                 while (! pdeq_empty(waitq)) {
223                         ir_node *n = (ir_node*)pdeq_getl(waitq);
224                         opt_walker(n, waitq);
225                 }
226                 /* Calculate dominance so we can kill unreachable code
227                  * We want this intertwined with localopts for better optimization (phase coupling) */
228                 compute_doms(irg);
229                 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
230         }
231         del_pdeq(waitq);
232         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
233
234         /* disable unreachable code elimination */
235         clear_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
236         set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
237
238         /* invalidate infos */
239         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
240         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
241         clear_irg_state(irg, IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
242         edges_deactivate(irg);
243
244         /* Finally kill BAD and doublets from the keep alives.
245          * Doing this AFTER edges where deactivated saves cycles */
246         end = get_irg_end(irg);
247         remove_End_Bads_and_doublets(end);
248
249         current_ir_graph = rem;
250
251         /* Note we do not have a reliable way to detect changes, since some
252          * localopt rules change the inputs of a node and do not return a new
253          * node, so we conservatively say true here */
254         return true;
255 }
256
257 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
258 {
259         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
260 }