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,
34 #include "irgraph_t.h"
36 #include "iroptimize.h"
45 #include "iredges_t.h"
48 /*------------------------------------------------------------------*/
49 /* apply optimizations of iropt to all nodes. */
50 /*------------------------------------------------------------------*/
53 * A wrapper around optimize_inplace_2() to be called from a walker.
55 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) {
73 /* Handle graph state */
74 assert(get_irg_phase_state(current_ir_graph) != phase_building);
76 if (get_opt_global_cse())
77 set_irg_pinned(current_ir_graph, op_pin_state_floats);
78 set_irg_outs_inconsistent(current_ir_graph);
79 set_irg_doms_inconsistent(current_ir_graph);
80 set_irg_loopinfo_inconsistent(current_ir_graph);
82 /* Clean the value_table in irg for the CSE. */
83 del_identities(current_ir_graph->value_table);
84 current_ir_graph->value_table = new_identities();
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) {
92 ir_graph *rem = current_ir_graph;
93 current_ir_graph = get_irn_irg(n);
97 current_ir_graph = rem;
101 * Block-Walker: uses dominance depth to mark dead blocks.
103 static void kill_dead_blocks(ir_node *block, void *env) {
106 if (get_Block_dom_depth(block) < 0) {
108 * Note that the new dominance code correctly handles
109 * the End block, i.e. it is always reachable from Start
111 set_Block_dead(block);
115 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
116 void local_optimize_graph(ir_graph *irg) {
117 ir_graph *rem = current_ir_graph;
118 current_ir_graph = irg;
120 if (get_irg_dom_state(irg) == dom_consistent)
121 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
123 do_local_optimize(get_irg_end(irg));
125 current_ir_graph = rem;
129 * Enqueue all users of a node to a wait queue.
130 * Handles mode_T nodes.
132 static void enqueue_users(ir_node *n, pdeq *waitq) {
133 const ir_edge_t *edge;
135 foreach_out_edge(n, edge) {
136 ir_node *succ = get_edge_src_irn(edge);
138 if (get_irn_link(succ) != waitq) {
139 pdeq_putr(waitq, succ);
140 set_irn_link(succ, waitq);
142 if (get_irn_mode(succ) == mode_T) {
143 /* A mode_T node has Proj's. Because most optimizations
144 run on the Proj's we have to enqueue them also. */
145 enqueue_users(succ, waitq);
151 * Data flow optimization walker.
152 * Optimizes all nodes and enqueue it's users
155 static void opt_walker(ir_node *n, void *env) {
159 optimized = optimize_in_place_2(n);
160 set_irn_link(optimized, NULL);
162 if (optimized != n) {
163 enqueue_users(n, waitq);
164 exchange(n, optimized);
168 /* Applies local optimizations to all nodes in the graph until fixpoint. */
169 void optimize_graph_df(ir_graph *irg) {
170 pdeq *waitq = new_pdeq();
171 ir_graph *rem = current_ir_graph;
175 current_ir_graph = irg;
177 state = edges_assure(irg);
179 if (get_opt_global_cse())
180 set_irg_pinned(current_ir_graph, op_pin_state_floats);
182 /* Clean the value_table in irg for the CSE. */
183 del_identities(irg->value_table);
184 irg->value_table = new_identities();
186 if (get_irg_dom_state(irg) == dom_consistent)
187 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
189 /* invalidate info */
190 set_irg_outs_inconsistent(irg);
191 set_irg_doms_inconsistent(irg);
192 set_irg_loopinfo_inconsistent(irg);
194 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
196 end = get_irg_end(irg);
197 n_ka = get_End_n_keepalives(end);
199 /* walk over the graph, but don't touch keep-alives */
200 irg_walk(get_irg_end_block(irg), NULL, opt_walker, waitq);
203 * Optimize keep-alives by removing superfluous ones.
204 * Beware: the last transformation might add new keep-alives
205 * that keep blocks that are where visited! So, check only the
206 * "old" keep-alives, not the new ones!
208 * FIXME: it might be better to completely remove this
209 * optimization here ...
211 for (i = n_ka - 1; i >= 0; --i) {
212 ir_node *ka = get_End_keepalive(end, i);
214 if (irn_visited(ka) && !is_irn_keep(ka)) {
215 /* this node can be regularly visited, no need to keep it */
216 set_End_keepalive(end, i, get_irg_bad(irg));
219 /* now walk again and visit all not yet visited nodes */
220 set_irg_visited(current_ir_graph, get_irg_visited(irg) - 1);
221 irg_walk(get_irg_end(irg), NULL, opt_walker, waitq);
223 /* finish the wait queue */
224 while (! pdeq_empty(waitq)) {
225 ir_node *n = pdeq_getl(waitq);
227 opt_walker(n, waitq);
232 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
235 edges_deactivate(irg);
237 current_ir_graph = rem;