de85ed1b444f2d6bc65205efc8d3594f9ae09dc1
[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         set_irg_outs_inconsistent(irg);
82         set_irg_doms_inconsistent(irg);
83         set_irg_loopinfo_inconsistent(irg);
84
85         /* Clean the value_table in irg for the CSE. */
86         new_identities(irg);
87
88         /* walk over the graph */
89         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
90 }
91
92 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
93 void local_optimize_node(ir_node *n)
94 {
95         ir_graph *rem = current_ir_graph;
96         current_ir_graph = get_irn_irg(n);
97
98         do_local_optimize(n);
99
100         current_ir_graph = rem;
101 }
102
103 /**
104  * Block-Walker: uses dominance depth to mark dead blocks.
105  */
106 static void kill_dead_blocks(ir_node *block, void *env)
107 {
108         (void) env;
109
110         if (get_Block_dom_depth(block) < 0) {
111                 /*
112                  * Note that the new dominance code correctly handles
113                  * the End block, i.e. it is always reachable from Start
114                  */
115                 ir_graph *irg = get_irn_irg(block);
116                 exchange(block, get_irg_bad(irg));
117         }
118 }
119
120 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
121 void local_optimize_graph(ir_graph *irg)
122 {
123         ir_graph *rem = current_ir_graph;
124         current_ir_graph = irg;
125
126         do_local_optimize(get_irg_end(irg));
127
128         current_ir_graph = rem;
129 }
130
131 /**
132  * Enqueue all users of a node to a wait queue.
133  * Handles mode_T nodes.
134  */
135 static void enqueue_users(ir_node *n, pdeq *waitq)
136 {
137         const ir_edge_t *edge;
138
139         foreach_out_edge(n, edge) {
140                 ir_node *succ = get_edge_src_irn(edge);
141
142                 if (get_irn_link(succ) != waitq) {
143                         pdeq_putr(waitq, succ);
144                         set_irn_link(succ, waitq);
145                 }
146                 if (get_irn_mode(succ) == mode_T) {
147                 /* A mode_T node has Proj's. Because most optimizations
148                         run on the Proj's we have to enqueue them also. */
149                         enqueue_users(succ, waitq);
150                 }
151         }
152 }
153
154 /**
155  * Data flow optimization walker.
156  * Optimizes all nodes and enqueue its users
157  * if done.
158  */
159 static void opt_walker(ir_node *n, void *env)
160 {
161         pdeq *waitq = (pdeq*)env;
162         ir_node *optimized;
163
164         optimized = optimize_in_place_2(n);
165         set_irn_link(optimized, NULL);
166
167         if (optimized != n) {
168                 enqueue_users(n, waitq);
169                 exchange(n, optimized);
170         }
171 }
172
173 /* Applies local optimizations to all nodes in the graph until fixpoint. */
174 int optimize_graph_df(ir_graph *irg)
175 {
176         pdeq     *waitq = new_pdeq();
177         ir_graph *rem = current_ir_graph;
178         ir_node  *end;
179         int      state, changed;
180
181         current_ir_graph = irg;
182
183         state = edges_assure(irg);
184
185         /* Clean the value_table in irg for the CSE. */
186         new_identities(irg);
187
188         if (get_opt_global_cse()) {
189                 set_irg_pinned(irg, op_pin_state_floats);
190         } else {
191                 /* The following enables unreachable code elimination (=Blocks may be
192                  * Bad). We cannot enable it in global_cse nodes since we can't
193                  * determine a nodes block there and therefore can't remove all code
194                  * in unreachable blocks */
195                 set_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
196                 if (get_irg_dom_state(irg) == dom_consistent)
197                         irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
198         }
199
200         /* invalidate info */
201         set_irg_outs_inconsistent(irg);
202         set_irg_doms_inconsistent(irg);
203         set_irg_loopinfo_inconsistent(irg);
204
205         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
206
207         /* walk over the graph, but don't touch keep-alives */
208         irg_walk_graph(irg, NULL, opt_walker, waitq);
209
210         /* any optimized nodes are stored in the wait queue,
211          * so if it's not empty, the graph has been changed */
212         changed = !pdeq_empty(waitq);
213
214         /* finish the wait queue */
215         while (! pdeq_empty(waitq)) {
216                 ir_node *n = (ir_node*)pdeq_getl(waitq);
217                 if (! is_Bad(n))
218                         opt_walker(n, waitq);
219         }
220
221         del_pdeq(waitq);
222
223         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
224
225         if (! state)
226                 edges_deactivate(irg);
227
228         /* Finally kill BAD and doublets from the keep alives.
229            Doing this AFTER edges where deactivated saves cycles */
230         end  = get_irg_end(irg);
231         remove_End_Bads_and_doublets(end);
232
233         clear_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
234
235         current_ir_graph = rem;
236         return changed;
237 }
238
239 /* Creates an ir_graph pass for optimize_graph_df. */
240 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
241 {
242         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
243 }  /* optimize_graph_df_pass */