Correct comment: xvcg wants LF, not CR.
[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         /* Handle graph state */
73         assert(get_irg_phase_state(irg) != phase_building);
74
75         if (get_opt_global_cse())
76                 set_irg_pinned(irg, op_pin_state_floats);
77         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
78
79         /* Clean the value_table in irg for the CSE. */
80         new_identities(irg);
81
82         /* walk over the graph */
83         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
84 }
85
86 void local_optimize_node(ir_node *n)
87 {
88         ir_graph *rem = current_ir_graph;
89         current_ir_graph = get_irn_irg(n);
90
91         do_local_optimize(n);
92
93         current_ir_graph = rem;
94 }
95
96 static void enqueue_node(ir_node *node, pdeq *waitq)
97 {
98         if (get_irn_link(node) == waitq)
99                 return;
100         pdeq_putr(waitq, node);
101         set_irn_link(node, waitq);
102 }
103
104 /**
105  * Enqueue all users of a node to a wait queue.
106  * Handles mode_T nodes.
107  */
108 static void enqueue_users(ir_node *n, pdeq *waitq)
109 {
110         const ir_edge_t *edge;
111
112         foreach_out_edge(n, edge) {
113                 ir_node         *succ  = get_edge_src_irn(edge);
114                 const ir_edge_t *edge2;
115
116                 enqueue_node(succ, waitq);
117
118                 /* Also enqueue Phis to prevent inconsistencies. */
119                 if (is_Block(succ)) {
120                         foreach_out_edge(succ, edge2) {
121                                 ir_node *succ2 = get_edge_src_irn(edge2);
122
123                                 if (is_Phi(succ2)) {
124                                         enqueue_node(succ2, waitq);
125                                 }
126                         }
127                 } else if (get_irn_mode(succ) == mode_T) {
128                 /* A mode_T node has Proj's. Because most optimizations
129                         run on the Proj's we have to enqueue them also. */
130                         enqueue_users(succ, waitq);
131                 }
132         }
133 }
134
135 /**
136  * Block-Walker: uses dominance depth to mark dead blocks.
137  */
138 static void find_unreachable_blocks(ir_node *block, void *env)
139 {
140         pdeq *waitq = (pdeq*) env;
141
142         if (get_Block_dom_depth(block) < 0) {
143                 ir_graph *irg = get_irn_irg(block);
144                 ir_node  *end = get_irg_end(irg);
145
146                 const ir_edge_t *edge;
147                 foreach_block_succ(block, edge) {
148                         const ir_edge_t *edge2;
149                         ir_node *succ_block = get_edge_src_irn(edge);
150                         enqueue_node(succ_block, waitq);
151                         foreach_out_edge(succ_block, edge2) {
152                                 ir_node *succ = get_edge_src_irn(edge2);
153                                 if (is_Phi(succ))
154                                         enqueue_node(succ, waitq);
155                         }
156                 }
157                 enqueue_node(end, waitq);
158         }
159 }
160
161 void local_optimize_graph(ir_graph *irg)
162 {
163         ir_graph *rem = current_ir_graph;
164         current_ir_graph = irg;
165
166         do_local_optimize(get_irg_end(irg));
167
168         current_ir_graph = rem;
169 }
170
171 /**
172  * Data flow optimization walker.
173  * Optimizes all nodes and enqueue its users
174  * if done.
175  */
176 static void opt_walker(ir_node *n, void *env)
177 {
178         pdeq *waitq = (pdeq*)env;
179         ir_node *optimized;
180
181         optimized = optimize_in_place_2(n);
182         set_irn_link(optimized, NULL);
183
184         if (optimized != n) {
185                 enqueue_users(n, waitq);
186                 exchange(n, optimized);
187         }
188 }
189
190 int optimize_graph_df(ir_graph *irg)
191 {
192         pdeq     *waitq = new_pdeq();
193         ir_graph *rem = current_ir_graph;
194         ir_node  *end;
195
196         current_ir_graph = irg;
197
198         if (get_opt_global_cse())
199                 set_irg_pinned(irg, op_pin_state_floats);
200
201         /* enable unreachable code elimination */
202         assert(!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE));
203         set_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
204
205         new_identities(irg);
206         edges_assure(irg);
207         assure_doms(irg);
208
209
210         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
211         irg_walk_graph(irg, NULL, opt_walker, waitq);
212
213         /* any optimized nodes are stored in the wait queue,
214          * so if it's not empty, the graph has been changed */
215         while (!pdeq_empty(waitq)) {
216                 /* finish the wait queue */
217                 while (! pdeq_empty(waitq)) {
218                         ir_node *n = (ir_node*)pdeq_getl(waitq);
219                         opt_walker(n, waitq);
220                 }
221                 /* Calculate dominance so we can kill unreachable code
222                  * We want this intertwined with localopts for better optimization (phase coupling) */
223                 compute_doms(irg);
224                 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
225         }
226         del_pdeq(waitq);
227         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
228
229         /* disable unreachable code elimination */
230         clear_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
231         set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
232
233         /* invalidate infos */
234         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
235         clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
236         clear_irg_state(irg, IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
237         edges_deactivate(irg);
238
239         /* Finally kill BAD and doublets from the keep alives.
240          * Doing this AFTER edges where deactivated saves cycles */
241         end = get_irg_end(irg);
242         remove_End_Bads_and_doublets(end);
243
244         current_ir_graph = rem;
245
246         /* Note we do not have a reliable way to detect changes, since some
247          * localopt rules change the inputs of a node and do not return a new
248          * node, so we conservatively say true here */
249         return true;
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 }