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,
31 #include "irgraph_t.h"
33 #include "iroptimize.h"
43 #include "iredges_t.h"
46 /*------------------------------------------------------------------*/
47 /* apply optimizations of iropt to all nodes. */
48 /*------------------------------------------------------------------*/
51 * A wrapper around optimize_inplace_2() to be called from a walker.
53 static void optimize_in_place_wrapper(ir_node *n, void *env)
55 ir_node *optimized = optimize_in_place_2(n);
59 exchange(n, optimized);
64 * Do local optimizations for a node.
66 * @param n the IR-node where to start. Typically the End node
69 * @note current_ir_graph must be set
71 static inline void do_local_optimize(ir_node *n)
73 ir_graph *irg = get_irn_irg(n);
75 /* Handle graph state */
76 assert(get_irg_phase_state(irg) != phase_building);
78 if (get_opt_global_cse())
79 set_irg_pinned(irg, op_pin_state_floats);
80 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
82 /* Clean the value_table in irg for the CSE. */
85 /* walk over the graph */
86 irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
89 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
90 void local_optimize_node(ir_node *n)
92 ir_graph *rem = current_ir_graph;
93 current_ir_graph = get_irn_irg(n);
97 current_ir_graph = rem;
100 static void enqueue_node(ir_node *node, pdeq *waitq)
102 if (get_irn_link(node) == waitq)
104 pdeq_putr(waitq, node);
105 set_irn_link(node, waitq);
109 * Enqueue all users of a node to a wait queue.
110 * Handles mode_T nodes.
112 static void enqueue_users(ir_node *n, pdeq *waitq)
114 const ir_edge_t *edge;
116 foreach_out_edge(n, edge) {
117 ir_node *succ = get_edge_src_irn(edge);
118 const ir_edge_t *edge2;
120 enqueue_node(succ, waitq);
122 /* Also enqueue Phis to prevent inconsistencies. */
123 if (is_Block(succ)) {
124 foreach_out_edge(succ, edge2) {
125 ir_node *succ2 = get_edge_src_irn(edge2);
128 enqueue_node(succ2, waitq);
131 } else if (get_irn_mode(succ) == mode_T) {
132 /* A mode_T node has Proj's. Because most optimizations
133 run on the Proj's we have to enqueue them also. */
134 enqueue_users(succ, waitq);
140 * Block-Walker: uses dominance depth to mark dead blocks.
142 static void find_unreachable_blocks(ir_node *block, void *env)
144 pdeq *waitq = (pdeq*) env;
146 if (get_Block_dom_depth(block) < 0) {
147 ir_graph *irg = get_irn_irg(block);
148 ir_node *end = get_irg_end(irg);
150 const ir_edge_t *edge;
151 foreach_block_succ(block, edge) {
152 const ir_edge_t *edge2;
153 ir_node *succ_block = get_edge_src_irn(edge);
154 enqueue_node(succ_block, waitq);
155 foreach_out_edge(succ_block, edge2) {
156 ir_node *succ = get_edge_src_irn(edge2);
158 enqueue_node(succ, waitq);
161 enqueue_node(end, waitq);
165 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
166 void local_optimize_graph(ir_graph *irg)
168 ir_graph *rem = current_ir_graph;
169 current_ir_graph = irg;
171 do_local_optimize(get_irg_end(irg));
173 current_ir_graph = rem;
177 * Data flow optimization walker.
178 * Optimizes all nodes and enqueue its users
181 static void opt_walker(ir_node *n, void *env)
183 pdeq *waitq = (pdeq*)env;
186 optimized = optimize_in_place_2(n);
187 set_irn_link(optimized, NULL);
189 if (optimized != n) {
190 enqueue_users(n, waitq);
191 exchange(n, optimized);
195 int optimize_graph_df(ir_graph *irg)
197 pdeq *waitq = new_pdeq();
198 ir_graph *rem = current_ir_graph;
201 current_ir_graph = irg;
203 if (get_opt_global_cse())
204 set_irg_pinned(irg, op_pin_state_floats);
206 /* enable unreachable code elimination */
207 assert(!is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE));
208 set_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
215 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
216 irg_walk_graph(irg, NULL, opt_walker, waitq);
218 /* any optimized nodes are stored in the wait queue,
219 * so if it's not empty, the graph has been changed */
220 while (!pdeq_empty(waitq)) {
221 /* finish the wait queue */
222 while (! pdeq_empty(waitq)) {
223 ir_node *n = (ir_node*)pdeq_getl(waitq);
224 opt_walker(n, waitq);
226 /* Calculate dominance so we can kill unreachable code
227 * We want this intertwined with localopts for better optimization (phase coupling) */
229 irg_block_walk_graph(irg, NULL, find_unreachable_blocks, waitq);
232 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
234 /* disable unreachable code elimination */
235 clear_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE);
236 set_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE);
238 /* invalidate infos */
239 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
240 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
241 clear_irg_state(irg, IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
242 edges_deactivate(irg);
244 /* Finally kill BAD and doublets from the keep alives.
245 * Doing this AFTER edges where deactivated saves cycles */
246 end = get_irg_end(irg);
247 remove_End_Bads_and_doublets(end);
249 current_ir_graph = rem;
251 /* Note we do not have a reliable way to detect changes, since some
252 * localopt rules change the inputs of a node and do not return a new
253 * node, so we conservatively say true here */
257 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
259 return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);