25c54f3684faf18aaa7617942ee28721a768f253
[libfirm] / ir / ir / irpass.c
1 /*
2  * Copyright (C) 1995-2009 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     Manager for optimization passes.
23  * @author    Michael Beck
24  * @version   $Id: $
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include "adt/list.h"
30 #include "irpass_t.h"
31 #include "irgraph_t.h"
32 #include "irprog_t.h"
33 #include "irdump.h"
34 #include "irvrfy.h"
35 #include "xmalloc.h"
36
37 /*Add a graph pass to a graph pass manager. */
38 void ir_graph_pass_mgr_add(ir_graph_pass_manager_t *mgr, ir_graph_pass_t *pass)
39 {
40         list_add(&pass->list, &mgr->passes);
41         ++mgr->n_passes;
42         if (pass->add_to_mgr)
43                 pass->add_to_mgr(pass->context);
44 }
45
46 /* Add an irprog pass to an irprog pass manager. */
47 void ir_prog_pass_mgr_add(ir_prog_pass_manager_t *mgr, ir_prog_pass_t *pass)
48 {
49         list_add(&pass->list, &mgr->passes);
50         ++mgr->n_passes;
51         if (pass->add_to_mgr)
52                 pass->add_to_mgr(pass->context);
53 }
54
55 /**
56  * wrapper for running a graph pass manager as a pass on an irprog
57  * pass manager.
58  */
59 static int run_wrapper(ir_prog *prog, void *ctx)
60 {
61         ir_graph_pass_manager_t *mgr = ctx;
62
63         (void)prog;
64         return ir_graph_pass_mgr_run(mgr);
65 }
66
67 /**
68  * Ensure that no verifier is run from the wrapper.
69  */
70 static int no_verify(ir_prog *prog, void *ctx)
71 {
72         (void)prog;
73         (void)ctx;
74         return 0;
75 }
76
77 /**
78  * Ensure that no dumper is run from the wrapper.
79  */
80 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
81 {
82         (void)prog;
83         (void)ctx;
84         (void)idx;
85 }
86
87 /**
88  * Term warpper for a wrapped ir_graph pass manager.
89  */
90 static void term_wrapper(void *context)
91 {
92         ir_graph_pass_manager_t *mgr = context;
93         term_graph_pass_mgr(mgr);
94 }
95
96 /**
97  * Create a wrapper ir_prog pass for an ir_graph manager.
98  */
99 static ir_prog_pass_t *create_wrapper_pass(ir_graph_pass_manager_t *graph_mgr)
100 {
101         /* create a wrapper pass */
102         ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
103
104         pass->run_on_irprog = run_wrapper;
105         pass->context       = graph_mgr;
106         pass->name          = graph_mgr->name;
107
108         /* do not verify nor dump: this is handled by the graph manager */
109         pass->verify_irprog = no_verify;
110         pass->dump_irprog   = no_dump;
111         pass->dump          = 0;
112         pass->verify        = 0;
113         pass->is_wrapper    = 1;
114
115         pass->add_to_mgr   = NULL;
116         pass->rem_from_mgr = term_wrapper;
117
118         return pass;
119 }
120
121 /* Add an ir_graph_pass as a pass to an ir_prog pass manager. */
122 void ir_prog_pass_mgr_add_graph_pass(
123         ir_prog_pass_manager_t *mgr, ir_graph_pass_t *pass)
124 {
125         ir_graph_pass_manager_t *graph_mgr;
126         ir_prog_pass_t          *wrapper;
127
128         /* check if the last pass is a graph_pass wrapper */
129         if (! list_empty(&mgr->passes)) {
130                 wrapper = list_entry(mgr->passes.prev, ir_prog_pass_t, list);
131                 if (wrapper->is_wrapper) {
132                         graph_mgr = wrapper->context;
133
134                         ir_graph_pass_mgr_add(graph_mgr, pass);
135                         return;
136                 }
137         }
138
139         /* not found, create a new wrapper */
140         graph_mgr = new_graph_pass_mgr("wrapper", mgr->verify_all, mgr->dump_all);
141         ir_graph_pass_mgr_add(graph_mgr, pass);
142
143         wrapper = create_wrapper_pass(graph_mgr);
144         ir_prog_pass_mgr_add(mgr, wrapper);
145 }
146
147 /* Add an ir_graph_pass_manager as a pass to an ir_prog pass manager. */
148 void ir_prog_pass_mgr_add_graph_mgr(
149         ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
150 {
151         ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
152
153         if (mgr->dump_all)
154                 graph_mgr->dump_all = 1;
155         if (mgr->verify_all)
156                 graph_mgr->verify_all = 1;
157         graph_mgr->run_idx = mgr->n_passes;
158
159         ir_prog_pass_mgr_add(mgr, pass);
160 }
161
162 /**
163  * Create a suffix for dumping.
164  */
165 void create_suffix(char *suffix, size_t n, const char *pass_name, unsigned index)
166 {
167         snprintf(suffix, n, "-%02u_%s", index, pass_name);
168 }
169
170 /* Run all passes of an ir_graph pass manager. */
171 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
172 {
173         ir_graph_pass_t *pass;
174         int             i, res = 0;
175         ir_graph        *rem = current_ir_graph;
176
177         /* on all graphs: beware: number of irgs might be changed */
178         for (i = 0; i < get_irp_n_irgs(); ++i) {
179                 ir_graph *irg = current_ir_graph = get_irp_irg(i);
180                 unsigned idx = mgr->run_idx;
181                 /* run every pass on every graph */
182                 list_for_each_entry(ir_graph_pass_t, pass, &mgr->passes, list) {
183                         int pass_res = pass->run_on_irg(irg, pass->context);
184                         if (pass_res != 0)
185                                 res = 1;
186                         /* verify is necessary */
187                         if (mgr->verify_all || pass->verify) {
188                                 if (pass->verify_irg) {
189                                         pass->verify_irg(irg, pass->context);
190                                 } else {
191                                         irg_verify(irg, 0);
192                                 }
193                         }
194                         /* dump */
195                         if (mgr->dump_all || pass->dump) {
196                                 if (pass->dump_irg) {
197                                         pass->dump_irg(irg, pass->context, idx);
198                                 } else {
199                                         char suffix[1024];
200                                         create_suffix(suffix, sizeof(suffix), pass->name, idx);
201                                         dump_ir_block_graph(irg, suffix);
202                                 }
203                         }
204                         ++idx;
205                 }
206         }
207         current_ir_graph = rem;
208         return res;
209 }
210
211 /**
212  * Verify all graphs on the given ir_prog.
213  */
214 static int irp_verify_irgs(ir_prog *irp, int flags) {
215         int i, res = 1;
216
217         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
218                 res &= irg_verify(get_irp_irg(i), flags);
219         return res;
220 }
221
222 /* Run all passes of an ir_prog pass manager. */
223 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
224 {
225         ir_prog_pass_t *pass;
226         int            res = 0;
227
228         /* run every pass on every graph */
229         unsigned idx = mgr->run_idx;
230         list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
231                 int pass_res = pass->run_on_irprog(irp, pass->context);
232                 if (pass_res != 0)
233                         res = 1;
234                 /* verify is necessary */
235                 if (mgr->verify_all || pass->verify) {
236                         if (pass->verify_irprog) {
237                                 pass->verify_irprog(irp, pass->context);
238                         } else {
239                                 irp_verify_irgs(irp, 0);
240                         }
241                 }
242                 /* dump */
243                 if (mgr->dump_all || pass->dump) {
244                         if (pass->dump_irprog) {
245                                 pass->dump_irprog(irp, pass->context, idx);
246                         } else {
247                                 char suffix[1024];
248                                 create_suffix(suffix, sizeof(suffix), pass->name, idx);
249                                 dump_all_ir_graphs(dump_ir_block_graph, suffix);
250                         }
251                 }
252                 if (pass->is_wrapper) {
253                         ir_graph_pass_manager_t *graph_mgr = pass->context;
254                         idx += graph_mgr->n_passes;
255                 } else
256                         ++idx;
257         }
258         return res;
259 }
260
261 /* Creates a new ir_graph pass manager. */
262 ir_graph_pass_manager_t *new_graph_pass_mgr(
263         const char *name, int verify_all, int dump_all)
264 {
265         ir_graph_pass_manager_t *res = xmalloc(sizeof(res));
266
267         INIT_LIST_HEAD(&res->passes);
268         res->name       = name;
269         res->run_idx    = 0;
270         res->verify_all = verify_all != 0;
271         res->dump_all   = dump_all   != 0;
272
273         return res;
274 }
275
276 /* Creates a new ir_prog pass manager. */
277 ir_prog_pass_manager_t *new_prog_pass_mgr(
278         const char *name, int verify_all, int dump_all)
279 {
280         ir_prog_pass_manager_t *res = xmalloc(sizeof(res));
281
282         INIT_LIST_HEAD(&res->passes);
283         res->name       = name;
284         res->run_idx    = 0;
285         res->verify_all = verify_all != 0;
286         res->dump_all   = dump_all   != 0;
287
288         return res;
289 }
290
291 /* Terminate an ir_graph pass manager and all owned passes. */
292 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
293 {
294         ir_graph_pass_t *pass, *next;
295
296         list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
297                 if (pass->rem_from_mgr)
298                         pass->rem_from_mgr(pass->context);
299                 pass->kind = k_BAD;
300                 xfree(pass);
301         }
302         mgr->kind = k_BAD;
303         xfree(mgr);
304 }
305
306 /* Terminate an ir_prog pass manager and all owned passes. */
307 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
308 {
309         ir_prog_pass_t *pass, *next;
310
311         list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
312                 if (pass->rem_from_mgr)
313                         pass->rem_from_mgr(pass->context);
314                 pass->kind = k_BAD;
315                 xfree(pass);
316         }
317         mgr->kind = k_BAD;
318         xfree(mgr);
319 }
320
321 /**
322  * Set the run index for an irgraph pass manager.
323  *
324  * @param mgr      the manager
325  * @param run_idx  the index for the first pass of this manager
326  */
327 void ir_graph_pass_manager_set_run_idx(
328         ir_graph_pass_manager_t *mgr, unsigned run_idx)
329 {
330         mgr->run_idx = run_idx;
331 }
332
333 /**
334  * Set the run index for an irprog pass manager.
335  *
336  * @param mgr      the manager
337  * @param run_idx  the index for the first pass of this manager
338  */
339 void ir_prog_pass_manager_set_run_idx(
340         ir_prog_pass_manager_t *mgr, unsigned run_idx)
341 {
342         mgr->run_idx = run_idx;
343 }