2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Optimizations for a whole ir graph, i.e., a procedure.
23 * @author Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
32 #include "irgraph_t.h"
34 #include "iroptimize.h"
44 #include "iredges_t.h"
47 /*------------------------------------------------------------------*/
48 /* apply optimizations of iropt to all nodes. */
49 /*------------------------------------------------------------------*/
52 * A wrapper around optimize_inplace_2() to be called from a walker.
54 static void optimize_in_place_wrapper(ir_node *n, void *env)
56 ir_node *optimized = optimize_in_place_2(n);
60 exchange(n, optimized);
65 * Do local optimizations for a node.
67 * @param n the IR-node where to start. Typically the End node
70 * @note current_ir_graph must be set
72 static inline void do_local_optimize(ir_node *n)
74 ir_graph *irg = get_irn_irg(n);
76 /* Handle graph state */
77 assert(get_irg_phase_state(irg) != phase_building);
79 if (get_opt_global_cse())
80 set_irg_pinned(irg, op_pin_state_floats);
81 set_irg_doms_inconsistent(irg);
82 set_irg_loopinfo_inconsistent(irg);
84 /* Clean the value_table in irg for the CSE. */
87 /* walk over the graph */
88 irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
91 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
92 void local_optimize_node(ir_node *n)
94 ir_graph *rem = current_ir_graph;
95 current_ir_graph = get_irn_irg(n);
99 current_ir_graph = rem;
103 * Enqueue all users of a node to a wait queue.
104 * Handles mode_T nodes.
106 static void enqueue_users(ir_node *n, pdeq *waitq)
108 const ir_edge_t *edge;
110 foreach_out_edge(n, edge) {
111 ir_node *succ = get_edge_src_irn(edge);
113 if (get_irn_link(succ) != waitq) {
114 pdeq_putr(waitq, succ);
115 set_irn_link(succ, waitq);
117 if (get_irn_mode(succ) == mode_T) {
118 /* A mode_T node has Proj's. Because most optimizations
119 run on the Proj's we have to enqueue them also. */
120 enqueue_users(succ, waitq);
126 * Block-Walker: uses dominance depth to mark dead blocks.
128 static void kill_dead_blocks(ir_node *block, void *env)
130 pdeq *waitq = (pdeq*) env;
132 if (get_Block_dom_depth(block) < 0) {
134 * Note that the new dominance code correctly handles
135 * the End block, i.e. it is always reachable from Start
137 ir_graph *irg = get_irn_irg(block);
138 enqueue_users(block, waitq);
139 exchange(block, new_r_Bad(irg, mode_BB));
143 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
144 void local_optimize_graph(ir_graph *irg)
146 ir_graph *rem = current_ir_graph;
147 current_ir_graph = irg;
149 do_local_optimize(get_irg_end(irg));
151 current_ir_graph = rem;
155 * Data flow optimization walker.
156 * Optimizes all nodes and enqueue its users
159 static void opt_walker(ir_node *n, void *env)
161 pdeq *waitq = (pdeq*)env;
164 optimized = optimize_in_place_2(n);
165 set_irn_link(optimized, NULL);
167 if (optimized != n) {
168 enqueue_users(n, waitq);
169 exchange(n, optimized);
173 static void clear_block_phis(ir_node *node, void *env) {
175 if (is_Block(node)) {
176 set_Block_phis(node, NULL);
180 static void collect_block_phis(ir_node *node, void *env) {
183 add_Block_phi(get_nodes_block(node), node);
187 static int count_non_bads(ir_node *node) {
188 int arity = get_irn_arity(node);
191 for (i=0; i<arity; ++i) {
192 if (!is_Bad(get_irn_n(node, i)))
198 static void block_remove_bads(ir_node *block, int *changed) {
201 const int max = get_irn_arity(block);
202 const int new_max = count_non_bads(block);
203 assert (max >= new_max);
205 if (is_Bad(block) || max == new_max) return;
207 new_in = ALLOCAN(ir_node*, new_max);
210 assert (get_Block_dom_depth(block) >= 0);
212 /* 1. Create a new block without Bad inputs */
214 for (i = 0; i < max; ++i) {
215 ir_node *block_pred = get_irn_n(block, i);
216 if (!is_Bad(block_pred)) {
217 new_in[j++] = block_pred;
220 assert (j == new_max);
222 /* If the end block is unreachable, it might have zero predecessors. */
223 ir_node *end_block = get_irg_end_block(get_irn_irg(block));
224 if (new_max == 0 && block == end_block) {
225 set_irn_in(block, new_max, new_in);
229 ir_node *new_block = new_r_Block(get_irn_irg(block), new_max, new_in);
231 /* 2. Remove inputs on Phis, where the block input is Bad. */
232 ir_node *phi = get_Block_phis(block);
235 ir_node* next = get_Phi_next(phi);
236 if (get_irn_arity(phi) != new_max) {
238 for (i = 0; i < max; ++i) {
239 ir_node *block_pred = get_irn_n(block, i);
241 if (!is_Bad(block_pred)) {
242 ir_node *pred = get_irn_n(phi, i);
246 assert (j == new_max);
248 ir_node *new_phi = new_r_Phi(new_block, new_max, new_in, get_irn_mode(phi));
249 exchange(phi, new_phi);
252 } while (phi != NULL);
255 exchange(block, new_block);
258 /* Remove Bad nodes from Phi and Block inputs.
260 * Precondition: No unreachable code.
261 * Postcondition: No Bad nodes.
263 static int remove_Bads(ir_graph *irg) {
265 /* build phi list per block */
266 irg_walk_graph(irg, clear_block_phis, collect_block_phis, NULL);
268 /* actually remove Bads */
269 irg_block_walk_graph(irg, NULL, (void (*)(struct ir_node *, void *)) block_remove_bads, &changed);
274 /* Applies local optimizations to all nodes in the graph until fixpoint. */
275 int optimize_graph_df(ir_graph *irg)
277 pdeq *waitq = new_pdeq();
278 ir_graph *rem = current_ir_graph;
282 current_ir_graph = irg;
284 state = edges_assure(irg);
286 /* Clean the value_table in irg for the CSE. */
289 if (get_opt_global_cse()) {
290 set_irg_pinned(irg, op_pin_state_floats);
293 /* The following enables unreachable code elimination (=Blocks may be
295 set_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
297 /* invalidate info */
298 set_irg_doms_inconsistent(irg);
299 set_irg_loopinfo_inconsistent(irg);
301 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
303 /* walk over the graph, but don't touch keep-alives */
304 irg_walk_graph(irg, NULL, opt_walker, waitq);
306 /* any optimized nodes are stored in the wait queue,
307 * so if it's not empty, the graph has been changed */
308 changed = !pdeq_empty(waitq);
311 /* finish the wait queue */
312 while (! pdeq_empty(waitq)) {
313 ir_node *n = (ir_node*)pdeq_getl(waitq);
314 opt_walker(n, waitq);
316 /* kill newly generated unreachable code */
318 irg_block_walk_graph(irg, NULL, kill_dead_blocks, waitq);
319 } while (! pdeq_empty(waitq));
323 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
326 edges_deactivate(irg);
328 /* Finally kill BAD and doublets from the keep alives.
329 Doing this AFTER edges where deactivated saves cycles */
330 end = get_irg_end(irg);
331 remove_End_Bads_and_doublets(end);
333 if (remove_Bads(irg)) {
334 edges_deactivate(irg);
337 clear_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK);
339 current_ir_graph = rem;
343 /* Creates an ir_graph pass for optimize_graph_df. */
344 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
346 return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
347 } /* optimize_graph_df_pass */