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"
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) {
54 ir_node *optimized = optimize_in_place_2(n);
58 exchange(n, optimized);
63 * Do local optimizations for a node.
65 * @param n the IR-node where to start. Typically the End node
68 * @note current_ir_graph must be set
70 static inline void do_local_optimize(ir_node *n) {
71 /* Handle graph state */
72 assert(get_irg_phase_state(current_ir_graph) != phase_building);
74 if (get_opt_global_cse())
75 set_irg_pinned(current_ir_graph, op_pin_state_floats);
76 set_irg_outs_inconsistent(current_ir_graph);
77 set_irg_doms_inconsistent(current_ir_graph);
78 set_irg_loopinfo_inconsistent(current_ir_graph);
80 /* Clean the value_table in irg for the CSE. */
81 del_identities(current_ir_graph->value_table);
82 current_ir_graph->value_table = new_identities();
84 /* walk over the graph */
85 irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
88 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
89 void local_optimize_node(ir_node *n) {
90 ir_graph *rem = current_ir_graph;
91 current_ir_graph = get_irn_irg(n);
95 current_ir_graph = rem;
99 * Block-Walker: uses dominance depth to mark dead blocks.
101 static void kill_dead_blocks(ir_node *block, void *env) {
104 if (get_Block_dom_depth(block) < 0) {
106 * Note that the new dominance code correctly handles
107 * the End block, i.e. it is always reachable from Start
109 set_Block_dead(block);
113 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
114 void local_optimize_graph(ir_graph *irg) {
115 ir_graph *rem = current_ir_graph;
116 current_ir_graph = irg;
118 if (get_irg_dom_state(irg) == dom_consistent)
119 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
121 do_local_optimize(get_irg_end(irg));
123 current_ir_graph = rem;
127 * Enqueue all users of a node to a wait queue.
128 * Handles mode_T nodes.
130 static void enqueue_users(ir_node *n, pdeq *waitq) {
131 const ir_edge_t *edge;
133 foreach_out_edge(n, edge) {
134 ir_node *succ = get_edge_src_irn(edge);
136 if (get_irn_link(succ) != waitq) {
137 pdeq_putr(waitq, succ);
138 set_irn_link(succ, waitq);
140 if (get_irn_mode(succ) == mode_T) {
141 /* A mode_T node has Proj's. Because most optimizations
142 run on the Proj's we have to enqueue them also. */
143 enqueue_users(succ, waitq);
149 * Data flow optimization walker.
150 * Optimizes all nodes and enqueue it's users
153 static void opt_walker(ir_node *n, void *env) {
157 optimized = optimize_in_place_2(n);
158 set_irn_link(optimized, NULL);
160 if (optimized != n) {
161 enqueue_users(n, waitq);
162 exchange(n, optimized);
166 /* Applies local optimizations to all nodes in the graph until fixpoint. */
167 int optimize_graph_df(ir_graph *irg) {
168 pdeq *waitq = new_pdeq();
169 ir_graph *rem = current_ir_graph;
173 current_ir_graph = irg;
175 state = edges_assure(irg);
177 if (get_opt_global_cse())
178 set_irg_pinned(current_ir_graph, op_pin_state_floats);
180 /* Clean the value_table in irg for the CSE. */
181 del_identities(irg->value_table);
182 irg->value_table = new_identities();
184 if (get_irg_dom_state(irg) == dom_consistent)
185 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
187 /* invalidate info */
188 set_irg_outs_inconsistent(irg);
189 set_irg_doms_inconsistent(irg);
190 set_irg_loopinfo_inconsistent(irg);
192 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
194 /* walk over the graph, but don't touch keep-alives */
195 irg_walk_graph(irg, NULL, opt_walker, waitq);
197 /* any optimized nodes are stored in the wait queue,
198 * so if it's not empty, the graph has been changed */
199 changed = !pdeq_empty(waitq);
201 /* finish the wait queue */
202 while (! pdeq_empty(waitq)) {
203 ir_node *n = pdeq_getl(waitq);
205 opt_walker(n, waitq);
210 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
213 edges_deactivate(irg);
215 /* Finally kill BAD and doublets from the keep alives.
216 Doing this AFTER edges where deactivated saves cycles */
217 end = get_irg_end(irg);
218 remove_End_Bads_and_doublets(end);
220 current_ir_graph = rem;