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