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 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
83 /* Clean the value_table in irg for the CSE. */
86 /* walk over the graph */
87 irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
90 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
91 void local_optimize_node(ir_node *n)
93 ir_graph *rem = current_ir_graph;
94 current_ir_graph = get_irn_irg(n);
98 current_ir_graph = rem;
101 static void enqueue_node(ir_node *node, pdeq *waitq)
103 if (get_irn_link(node) == waitq)
105 pdeq_putr(waitq, node);
106 set_irn_link(node, waitq);
110 * Enqueue all users of a node to a wait queue.
111 * Handles mode_T nodes.
113 static void enqueue_users(ir_node *n, pdeq *waitq)
115 const ir_edge_t *edge;
117 foreach_out_edge(n, edge) {
118 ir_node *succ = get_edge_src_irn(edge);
120 enqueue_node(succ, waitq);
121 if (get_irn_mode(succ) == mode_T) {
122 /* A mode_T node has Proj's. Because most optimizations
123 run on the Proj's we have to enqueue them also. */
124 enqueue_users(succ, waitq);
130 * Block-Walker: uses dominance depth to mark dead blocks.
132 static void find_unreachable_blocks(ir_node *block, void *env)
134 pdeq *waitq = (pdeq*) env;
136 if (get_Block_dom_depth(block) < 0) {
137 ir_graph *irg = get_irn_irg(block);
138 ir_node *end = get_irg_end(irg);
140 const ir_edge_t *edge;
141 foreach_block_succ(block, edge) {
142 const ir_edge_t *edge2;
143 ir_node *succ_block = get_edge_src_irn(edge);
144 enqueue_node(succ_block, waitq);
145 foreach_out_edge(succ_block, edge2) {
146 ir_node *succ = get_edge_src_irn(edge2);
148 enqueue_node(succ, waitq);
151 enqueue_node(end, waitq);
155 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
156 void local_optimize_graph(ir_graph *irg)
158 ir_graph *rem = current_ir_graph;
159 current_ir_graph = irg;
161 do_local_optimize(get_irg_end(irg));
163 current_ir_graph = rem;
167 * Data flow optimization walker.
168 * Optimizes all nodes and enqueue its users
171 static void opt_walker(ir_node *n, void *env)
173 pdeq *waitq = (pdeq*)env;
176 optimized = optimize_in_place_2(n);
177 set_irn_link(optimized, NULL);
179 if (optimized != n) {
180 enqueue_users(n, waitq);
181 exchange(n, optimized);
185 /* Applies local optimizations to all nodes in the graph until fixpoint. */
186 int optimize_graph_df(ir_graph *irg)
188 pdeq *waitq = new_pdeq();
189 ir_graph *rem = current_ir_graph;
193 current_ir_graph = irg;
195 state = edges_assure(irg);
197 /* Clean the value_table in irg for the CSE. */
200 if (get_opt_global_cse()) {
201 set_irg_pinned(irg, op_pin_state_floats);
204 /* The following enables unreachable code elimination (=Blocks may be
206 clear_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS);
208 /* invalidate info */
209 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
211 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
213 /* Calculate dominance so we can kill unreachable code */
216 /* walk over the graph, but don't touch keep-alives */
217 irg_walk_graph(irg, NULL, opt_walker, waitq);
219 /* any optimized nodes are stored in the wait queue,
220 * so if it's not empty, the graph has been changed */
221 changed = !pdeq_empty(waitq);
223 while (!pdeq_empty(waitq)) {
224 /* finish the wait queue */
225 while (! pdeq_empty(waitq)) {
226 ir_node *n = (ir_node*)pdeq_getl(waitq);
227 opt_walker(n, waitq);
229 /* Calculate dominance so we can kill unreachable code */
231 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
233 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
237 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
240 edges_deactivate(irg);
242 if (remove_bads(irg)) {
243 edges_deactivate(irg);
246 /* Finally kill BAD and doublets from the keep alives.
247 Doing this AFTER edges where deactivated saves cycles */
248 end = get_irg_end(irg);
249 remove_End_Bads_and_doublets(end);
251 set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_BLOCKS);
253 current_ir_graph = rem;
257 /* Creates an ir_graph pass for optimize_graph_df. */
258 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
260 return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
261 } /* optimize_graph_df_pass */