fix weak external functions
[libfirm] / ir / ir / irgopt.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Optimizations for a whole ir graph, i.e., a procedure.
23  * @author   Christian Schaefer, Goetz Lindenmaier, Sebastian Felis,
24  *           Michael Beck
25  * @version  $Id$
26  */
27 #include "config.h"
28
29 #include <assert.h>
30
31 #include "irnode_t.h"
32 #include "irgraph_t.h"
33
34 #include "iroptimize.h"
35 #include "iropt_t.h"
36 #include "irgopt.h"
37 #include "irgmod.h"
38 #include "irgwalk.h"
39
40 #include "adt/pdeq.h"
41
42 #include "irpass_t.h"
43 #include "irflag_t.h"
44 #include "iredges_t.h"
45 #include "irtools.h"
46
47 /*------------------------------------------------------------------*/
48 /* apply optimizations of iropt to all nodes.                       */
49 /*------------------------------------------------------------------*/
50
51 /**
52  * A wrapper around optimize_inplace_2() to be called from a walker.
53  */
54 static void optimize_in_place_wrapper(ir_node *n, void *env) {
55         ir_node *optimized = optimize_in_place_2(n);
56         (void) env;
57
58         if (optimized != n) {
59                 exchange(n, optimized);
60         }
61 }
62
63 /**
64  * Do local optimizations for a node.
65  *
66  * @param n  the IR-node where to start. Typically the End node
67  *           of a graph
68  *
69  * @note current_ir_graph must be set
70  */
71 static inline void do_local_optimize(ir_node *n) {
72         /* Handle graph state */
73         assert(get_irg_phase_state(current_ir_graph) != phase_building);
74
75         if (get_opt_global_cse())
76                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
77         set_irg_outs_inconsistent(current_ir_graph);
78         set_irg_doms_inconsistent(current_ir_graph);
79         set_irg_loopinfo_inconsistent(current_ir_graph);
80
81         /* Clean the value_table in irg for the CSE. */
82         del_identities(current_ir_graph->value_table);
83         current_ir_graph->value_table = new_identities();
84
85         /* walk over the graph */
86         irg_walk(n, firm_clear_link, optimize_in_place_wrapper, NULL);
87 }
88
89 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n */
90 void local_optimize_node(ir_node *n) {
91         ir_graph *rem = current_ir_graph;
92         current_ir_graph = get_irn_irg(n);
93
94         do_local_optimize(n);
95
96         current_ir_graph = rem;
97 }
98
99 /**
100  * Block-Walker: uses dominance depth to mark dead blocks.
101  */
102 static void kill_dead_blocks(ir_node *block, void *env) {
103         (void) env;
104
105         if (get_Block_dom_depth(block) < 0) {
106                 /*
107                  * Note that the new dominance code correctly handles
108                  * the End block, i.e. it is always reachable from Start
109                  */
110                 set_Block_dead(block);
111         }
112 }
113
114 /* Applies local optimizations (see iropt.h) to all nodes reachable from node n. */
115 void local_optimize_graph(ir_graph *irg) {
116         ir_graph *rem = current_ir_graph;
117         current_ir_graph = irg;
118
119         if (get_irg_dom_state(irg) == dom_consistent)
120                 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
121
122         do_local_optimize(get_irg_end(irg));
123
124         current_ir_graph = rem;
125 }
126
127 /**
128  * Enqueue all users of a node to a wait queue.
129  * Handles mode_T nodes.
130  */
131 static void enqueue_users(ir_node *n, pdeq *waitq) {
132         const ir_edge_t *edge;
133
134         foreach_out_edge(n, edge) {
135                 ir_node *succ = get_edge_src_irn(edge);
136
137                 if (get_irn_link(succ) != waitq) {
138                         pdeq_putr(waitq, succ);
139                         set_irn_link(succ, waitq);
140                 }
141                 if (get_irn_mode(succ) == mode_T) {
142                 /* A mode_T node has Proj's. Because most optimizations
143                         run on the Proj's we have to enqueue them also. */
144                         enqueue_users(succ, waitq);
145                 }
146         }
147 }
148
149 /**
150  * Data flow optimization walker.
151  * Optimizes all nodes and enqueue it's users
152  * if done.
153  */
154 static void opt_walker(ir_node *n, void *env) {
155         pdeq *waitq = env;
156         ir_node *optimized;
157
158         optimized = optimize_in_place_2(n);
159         set_irn_link(optimized, NULL);
160
161         if (optimized != n) {
162                 enqueue_users(n, waitq);
163                 exchange(n, optimized);
164         }
165 }
166
167 /* Applies local optimizations to all nodes in the graph until fixpoint. */
168 int optimize_graph_df(ir_graph *irg) {
169         pdeq     *waitq = new_pdeq();
170         ir_graph *rem = current_ir_graph;
171         ir_node  *end;
172         int      state, changed;
173
174         current_ir_graph = irg;
175
176         state = edges_assure(irg);
177
178         if (get_opt_global_cse())
179                 set_irg_pinned(current_ir_graph, op_pin_state_floats);
180
181         /* Clean the value_table in irg for the CSE. */
182         del_identities(irg->value_table);
183         irg->value_table = new_identities();
184
185         if (get_irg_dom_state(irg) == dom_consistent)
186                 irg_block_walk_graph(irg, NULL, kill_dead_blocks, NULL);
187
188         /* invalidate info */
189         set_irg_outs_inconsistent(irg);
190         set_irg_doms_inconsistent(irg);
191         set_irg_loopinfo_inconsistent(irg);
192
193         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
194
195         /* walk over the graph, but don't touch keep-alives */
196         irg_walk_graph(irg, NULL, opt_walker, waitq);
197
198         /* any optimized nodes are stored in the wait queue,
199          * so if it's not empty, the graph has been changed */
200         changed = !pdeq_empty(waitq);
201
202         /* finish the wait queue */
203         while (! pdeq_empty(waitq)) {
204                 ir_node *n = pdeq_getl(waitq);
205                 if (! is_Bad(n))
206                         opt_walker(n, waitq);
207         }
208
209         del_pdeq(waitq);
210
211         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
212
213         if (! state)
214                 edges_deactivate(irg);
215
216         /* Finally kill BAD and doublets from the keep alives.
217            Doing this AFTER edges where deactivated saves cycles */
218         end  = get_irg_end(irg);
219         remove_End_Bads_and_doublets(end);
220
221         current_ir_graph = rem;
222         return changed;
223 }
224
225 /* Creates an ir_graph pass for optimize_graph_df. */
226 ir_graph_pass_t *optimize_graph_df_pass(const char *name)
227 {
228         return def_graph_pass_ret(name ? name : "optimize_graph_df", optimize_graph_df);
229 }  /* optimize_graph_df_pass */