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