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_outs_inconsistent(irg);
82 set_irg_doms_inconsistent(irg);
83 set_irg_loopinfo_inconsistent(irg);
85 /* Clean the value_table in irg for the CSE. */
88 /* walk over the graph */
89 irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
92 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
93 void local_optimize_node(ir_node *n)
95 ir_graph *rem = current_ir_graph;
96 current_ir_graph = get_irn_irg(n);
100 current_ir_graph = rem;
104 * Block-Walker: uses dominance depth to mark dead blocks.
106 static void kill_dead_blocks(ir_node *block, void *env)
110 if (get_Block_dom_depth(block) < 0) {
112 * Note that the new dominance code correctly handles
113 * the End block, i.e. it is always reachable from Start
115 set_Block_dead(block);
119 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
120 void local_optimize_graph(ir_graph *irg)
122 ir_graph *rem = current_ir_graph;
123 current_ir_graph = irg;
125 if (get_irg_dom_state(irg) == dom_consistent)
126 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
128 do_local_optimize(get_irg_end(irg));
130 current_ir_graph = rem;
134 * Enqueue all users of a node to a wait queue.
135 * Handles mode_T nodes.
137 static void enqueue_users(ir_node *n, pdeq *waitq)
139 const ir_edge_t *edge;
141 foreach_out_edge(n, edge) {
142 ir_node *succ = get_edge_src_irn(edge);
144 if (get_irn_link(succ) != waitq) {
145 pdeq_putr(waitq, succ);
146 set_irn_link(succ, waitq);
148 if (get_irn_mode(succ) == mode_T) {
149 /* A mode_T node has Proj's. Because most optimizations
150 run on the Proj's we have to enqueue them also. */
151 enqueue_users(succ, waitq);
157 * Data flow optimization walker.
158 * Optimizes all nodes and enqueue its users
161 static void opt_walker(ir_node *n, void *env)
163 pdeq *waitq = (pdeq*)env;
166 optimized = optimize_in_place_2(n);
167 set_irn_link(optimized, NULL);
169 if (optimized != n) {
170 enqueue_users(n, waitq);
171 exchange(n, optimized);
175 /* Applies local optimizations to all nodes in the graph until fixpoint. */
176 int optimize_graph_df(ir_graph *irg)
178 pdeq *waitq = new_pdeq();
179 ir_graph *rem = current_ir_graph;
183 current_ir_graph = irg;
185 state = edges_assure(irg);
187 if (get_opt_global_cse())
188 set_irg_pinned(irg, op_pin_state_floats);
190 /* Clean the value_table in irg for the CSE. */
193 if (get_irg_dom_state(irg) == dom_consistent)
194 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
196 /* invalidate info */
197 set_irg_outs_inconsistent(irg);
198 set_irg_doms_inconsistent(irg);
199 set_irg_loopinfo_inconsistent(irg);
201 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
203 /* walk over the graph, but don't touch keep-alives */
204 irg_walk_graph(irg, NULL, opt_walker, waitq);
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);
210 /* finish the wait queue */
211 while (! pdeq_empty(waitq)) {
212 ir_node *n = (ir_node*)pdeq_getl(waitq);
214 opt_walker(n, waitq);
219 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
222 edges_deactivate(irg);
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);
229 current_ir_graph = rem;
233 /* Creates an ir_graph pass for optimize_graph_df. */
234 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
236 return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
237 } /* optimize_graph_df_pass */