Fixed some typos.
[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 #include "ircons.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  * A wrapper around optimize_inplace_2() to be called from a walker.
49  */
50 static void optimize_in_place_wrapper(ir_node *n, void *env)
51 {
52         ir_node *optimized = optimize_in_place_2(n);
53         (void) env;
54
55         if (optimized != n) {
56                 exchange(n, optimized);
57         }
58 }
59
60 /**
61  * Do local optimizations for a node.
62  *
63  * @param n  the IR-node where to start. Typically the End node
64  *           of a graph
65  *
66  * @note current_ir_graph must be set
67  */
68 static inline void do_local_optimize(ir_node *n)
69 {
70         ir_graph *irg = get_irn_irg(n);
71
72         if (get_opt_global_cse())
73                 set_irg_pinned(irg, op_pin_state_floats);
74         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
75
76         /* Clean the value_table in irg for the CSE. */
77         new_identities(irg);
78
79         /* walk over the graph */
80         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
81 }
82
83 void local_optimize_node(ir_node *n)
84 {
85         ir_graph *rem = current_ir_graph;
86         current_ir_graph = get_irn_irg(n);
87
88         do_local_optimize(n);
89
90         current_ir_graph = rem;
91 }
92
93 static void enqueue_node(ir_node *node, pdeq *waitq)
94 {
95         if (get_irn_link(node) == waitq)
96                 return;
97         pdeq_putr(waitq, node);
98         set_irn_link(node, waitq);
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         foreach_out_edge(n, edge) {
108                 ir_node *succ  = get_edge_src_irn(edge);
109
110                 enqueue_node(succ, waitq);
111
112                 /* Also enqueue Phis to prevent inconsistencies. */
113                 if (is_Block(succ)) {
114                         foreach_out_edge(succ, edge2) {
115                                 ir_node *succ2 = get_edge_src_irn(edge2);
116
117                                 if (is_Phi(succ2)) {
118                                         enqueue_node(succ2, waitq);
119                                 }
120                         }
121                 } else 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                 foreach_block_succ(block, edge) {
141                         ir_node *succ_block = get_edge_src_irn(edge);
142                         enqueue_node(succ_block, waitq);
143                         foreach_out_edge(succ_block, edge2) {
144                                 ir_node *succ = get_edge_src_irn(edge2);
145                                 if (is_Phi(succ))
146                                         enqueue_node(succ, waitq);
147                         }
148                 }
149                 enqueue_node(end, waitq);
150         }
151 }
152
153 void local_optimize_graph(ir_graph *irg)
154 {
155         ir_graph *rem = current_ir_graph;
156         current_ir_graph = irg;
157
158         do_local_optimize(get_irg_end(irg));
159
160         current_ir_graph = rem;
161 }
162
163 /**
164  * Data flow optimization walker.
165  * Optimizes all nodes and enqueue its users
166  * if done.
167  */
168 static void opt_walker(ir_node *n, void *env)
169 {
170         pdeq *waitq = (pdeq*)env;
171         ir_node *optimized;
172
173         optimized = optimize_in_place_2(n);
174         set_irn_link(optimized, NULL);
175
176         if (optimized != n) {
177                 enqueue_users(n, waitq);
178                 exchange(n, optimized);
179         }
180 }
181
182 int optimize_graph_df(ir_graph *irg)
183 {
184         pdeq     *waitq = new_pdeq();
185         ir_graph *rem = current_ir_graph;
186         ir_node  *end;
187
188         current_ir_graph = irg;
189
190         if (get_opt_global_cse())
191                 set_irg_pinned(irg, op_pin_state_floats);
192
193         /* enable unreachable code elimination */
194         assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE));
195         add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
196
197         new_identities(irg);
198         assure_edges(irg);
199         assure_doms(irg);
200
201
202         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
203         irg_walk_graph(irg, NULL, opt_walker, waitq);
204
205         /* any optimized nodes are stored in the wait queue,
206          * so if it's not empty, the graph has been changed */
207         while (!pdeq_empty(waitq)) {
208                 /* finish the wait queue */
209                 while (! pdeq_empty(waitq)) {
210                         ir_node *n = (ir_node*)pdeq_getl(waitq);
211                         opt_walker(n, waitq);
212                 }
213                 /* Calculate dominance so we can kill unreachable code
214                  * We want this intertwined with localopts for better optimization (phase coupling) */
215                 compute_doms(irg);
216                 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
217         }
218         del_pdeq(waitq);
219         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
220
221         /* disable unreachable code elimination */
222         clear_irg_constraints(irg, IR_GRAPH_CONSTRAINT_OPTIMIZE_UNREACHABLE_CODE);
223         add_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE);
224
225         /* invalidate infos */
226         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
227         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
228         edges_deactivate(irg);
229
230         /* Finally kill BAD and doublets from the keep alives.
231          * Doing this AFTER edges where deactivated saves cycles */
232         end = get_irg_end(irg);
233         remove_End_Bads_and_doublets(end);
234
235         current_ir_graph = rem;
236
237         /* Note we do not have a reliable way to detect changes, since some
238          * localopt rules change the inputs of a node and do not return a new
239          * node, so we conservatively say true here */
240         return true;
241 }
242
243 void local_opts_const_code(void)
244 {
245         ir_graph *irg = get_const_code_irg();
246         /* Clean the value_table in irg for the CSE. */
247         new_identities(irg);
248
249         walk_const_code(firm_clear_link, optimize_in_place_wrapper, NULL);
250 }
251
252 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
253 {
254         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
255 }