ae08bfbb49ad52f958102493518ee433abc0a69d
[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->is_wrapper    = 1;
112
113         pass->add_to_mgr   = NULL;
114         pass->rem_from_mgr = term_wrapper;
115
116         return pass;
117 }
118
119 /* Add an ir_graph_pass as a pass to an ir_prog pass manager. */
120 void ir_prog_pass_mgr_add_graph_pass(
121         ir_prog_pass_manager_t *mgr, ir_graph_pass_t *pass)
122 {
123         ir_graph_pass_manager_t *graph_mgr;
124         ir_prog_pass_t          *wrapper;
125
126         /* check if the last pass is a graph_pass wrapper */
127         if (! list_empty(&mgr->passes)) {
128                 wrapper = list_entry(mgr->passes.prev, ir_prog_pass_t, list);
129                 if (wrapper->is_wrapper) {
130                         graph_mgr = wrapper->context;
131
132                         ir_graph_pass_mgr_add(graph_mgr, pass);
133                         return;
134                 }
135         }
136
137         /* not found, create a new wrapper */
138         graph_mgr = new_graph_pass_mgr("wrapper", mgr->verify_all, mgr->dump_all);
139         ir_graph_pass_mgr_add(graph_mgr, pass);
140
141         wrapper = create_wrapper_pass(graph_mgr);
142         ir_prog_pass_mgr_add(mgr, wrapper);
143 }
144
145 /* Add an ir_graph_pass_manager as a pass to an ir_prog pass manager. */
146 void ir_prog_pass_mgr_add_graph_mgr(
147         ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
148 {
149         ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
150
151         if (mgr->dump_all)
152                 graph_mgr->dump_all = 1;
153         if (mgr->verify_all)
154                 graph_mgr->verify_all = 1;
155         graph_mgr->run_idx = mgr->n_passes;
156
157         ir_prog_pass_mgr_add(mgr, pass);
158 }
159
160 /**
161  * Create a suffix for dumping.
162  */
163 void create_suffix(char *suffix, size_t n, const char *pass_name, unsigned index)
164 {
165         snprintf(suffix, n, "-%02u_%s", index, pass_name);
166 }
167
168 /* Run all passes of an ir_graph pass manager. */
169 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
170 {
171         ir_graph_pass_t *pass;
172         int             i, res = 0;
173         ir_graph        *rem = current_ir_graph;
174
175         /* on all graphs: beware: number of irgs might be changed */
176         for (i = 0; i < get_irp_n_irgs(); ++i) {
177                 ir_graph *irg = current_ir_graph = get_irp_irg(i);
178                 unsigned idx = mgr->run_idx;
179                 /* run every pass on every graph */
180                 list_for_each_entry(ir_graph_pass_t, pass, &mgr->passes, list) {
181                         int pass_res = pass->run_on_irg(irg, pass->context);
182                         if (pass_res != 0)
183                                 res = 1;
184                         /* verify is necessary */
185                         if (mgr->verify_all) {
186                                 if (pass->verify_irg) {
187                                         pass->verify_irg(irg, pass->context);
188                                 } else {
189                                         irg_verify(irg, 0);
190                                 }
191                         }
192                         /* dump */
193                         if (mgr->dump_all) {
194                                 if (pass->dump_irg) {
195                                         pass->dump_irg(irg, pass->context, idx);
196                                 } else {
197                                         char suffix[1024];
198                                         create_suffix(suffix, sizeof(suffix), pass->name, idx);
199                                         dump_ir_block_graph(irg, suffix);
200                                 }
201                         }
202                         ++idx;
203                 }
204         }
205         current_ir_graph = rem;
206         return res;
207 }
208
209 /**
210  * Verify all graphs on the given ir_prog.
211  */
212 static int irp_verify_irgs(ir_prog *irp, int flags) {
213         int i, res = 1;
214
215         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
216                 res &= irg_verify(get_irp_irg(i), flags);
217         return res;
218 }
219
220 /* Run all passes of an ir_prog pass manager. */
221 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
222 {
223         ir_prog_pass_t *pass;
224         int            res = 0;
225
226         /* run every pass on every graph */
227         unsigned idx = mgr->run_idx;
228         list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
229                 int pass_res = pass->run_on_irprog(irp, pass->context);
230                 if (pass_res != 0)
231                         res = 1;
232                 /* verify is necessary */
233                 if (mgr->verify_all) {
234                         if (pass->verify_irprog) {
235                                 pass->verify_irprog(irp, pass->context);
236                         } else {
237                                 irp_verify_irgs(irp, 0);
238                         }
239                 }
240                 /* dump */
241                 if (mgr->dump_all) {
242                         if (pass->dump_irprog) {
243                                 pass->dump_irprog(irp, pass->context, idx);
244                         } else {
245                                 char suffix[1024];
246                                 create_suffix(suffix, sizeof(suffix), pass->name, idx);
247                                 dump_all_ir_graphs(dump_ir_block_graph, suffix);
248                         }
249                 }
250                 if (pass->is_wrapper) {
251                         ir_graph_pass_manager_t *graph_mgr = pass->context;
252                         idx += graph_mgr->n_passes;
253                 } else
254                         ++idx;
255         }
256         return res;
257 }
258
259 /* Creates a new ir_graph pass manager. */
260 ir_graph_pass_manager_t *new_graph_pass_mgr(
261         const char *name, int verify_all, int dump_all)
262 {
263         ir_graph_pass_manager_t *res = xmalloc(sizeof(res));
264
265         INIT_LIST_HEAD(&res->passes);
266         res->name       = name;
267         res->run_idx    = 0;
268         res->verify_all = verify_all != 0;
269         res->dump_all   = dump_all   != 0;
270
271         return res;
272 }
273
274 /* Creates a new ir_prog pass manager. */
275 ir_prog_pass_manager_t *new_prog_pass_mgr(
276         const char *name, int verify_all, int dump_all)
277 {
278         ir_prog_pass_manager_t *res = xmalloc(sizeof(res));
279
280         INIT_LIST_HEAD(&res->passes);
281         res->name       = name;
282         res->run_idx    = 0;
283         res->verify_all = verify_all != 0;
284         res->dump_all   = dump_all   != 0;
285
286         return res;
287 }
288
289 /* Terminate an ir_graph pass manager and all owned passes. */
290 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
291 {
292         ir_graph_pass_t *pass, *next;
293
294         list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
295                 if (pass->rem_from_mgr)
296                         pass->rem_from_mgr(pass->context);
297                 pass->kind = k_BAD;
298                 xfree(pass);
299         }
300         mgr->kind = k_BAD;
301         xfree(mgr);
302 }
303
304 /* Terminate an ir_prog pass manager and all owned passes. */
305 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
306 {
307         ir_prog_pass_t *pass, *next;
308
309         list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
310                 if (pass->rem_from_mgr)
311                         pass->rem_from_mgr(pass->context);
312                 pass->kind = k_BAD;
313                 xfree(pass);
314         }
315         mgr->kind = k_BAD;
316         xfree(mgr);
317 }
318
319 /**
320  * Set the run index for an irgraph pass manager.
321  *
322  * @param mgr      the manager
323  * @param run_idx  the index for the first pass of this manager
324  */
325 void ir_graph_pass_manager_set_run_idx(
326         ir_graph_pass_manager_t *mgr, unsigned run_idx)
327 {
328         mgr->run_idx = run_idx;
329 }
330
331 /**
332  * Set the run index for an irprog pass manager.
333  *
334  * @param mgr      the manager
335  * @param run_idx  the index for the first pass of this manager
336  */
337 void ir_prog_pass_manager_set_run_idx(
338         ir_prog_pass_manager_t *mgr, unsigned run_idx)
339 {
340         mgr->run_idx = run_idx;
341 }
342
343 /**
344  * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
345  */
346 static int void_graph_wrapper(ir_graph *irg, void *context) {
347         void (*function)(ir_graph *irg) = context;
348         function(irg);
349         return 0;
350 }  /* void_graph_wrapper */
351
352 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
353 ir_graph_pass_t *def_graph_pass(
354         const char *name, void (*function)(ir_graph *irg))
355 {
356         struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
357
358         pass->kind       = k_ir_graph_pass;
359         pass->run_on_irg = void_graph_wrapper;
360         pass->context    = function;
361         pass->name       = name;
362
363         INIT_LIST_HEAD(&pass->list);
364
365         return pass;
366 }  /* def_graph_pass */
367
368 /**
369  * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
370  */
371 static int int_graph_wrapper(ir_graph *irg, void *context) {
372         int (*function)(ir_graph *irg) = context;
373         return function(irg);
374 }  /* int_graph_wrapper */
375
376 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
377 ir_graph_pass_t *def_graph_pass_ret(
378                 const char *name, int (*function)(ir_graph *irg))
379 {
380         struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
381
382         pass->kind       = k_ir_graph_pass;
383         pass->run_on_irg = int_graph_wrapper;
384         pass->context    = function;
385         pass->name       = name;
386
387         INIT_LIST_HEAD(&pass->list);
388
389         return pass;
390 }  /* def_graph_pass_ret */
391
392 /* constructor for a default graph pass */
393 ir_graph_pass_t *def_graph_pass_constructor(
394         ir_graph_pass_t *pass,
395         const char *name, int (*function)(ir_graph *irg, void *context)) {
396         if (pass == NULL)
397                 pass = XMALLOCZ(ir_graph_pass_t);
398         pass->kind       = k_ir_graph_pass;
399         pass->run_on_irg = function;
400         pass->context    = pass;
401         pass->name       = name;
402
403         INIT_LIST_HEAD(&pass->list);
404
405         return pass;
406 }  /* def_graph_pass_constructor */
407
408
409 /**
410  * Wrapper for running void function(void) as an ir_prog pass.
411  */
412 static int void_prog_wrapper(ir_prog *irp, void *context) {
413         void (*function)(void) = context;
414
415         (void)irp;
416         function();
417         return 0;
418 }  /* void_graph_wrapper */
419
420 /* Creates an ir_prog pass for running void function(void). */
421 ir_prog_pass_t *def_prog_pass(
422         const char *name,
423         void (*function)(void))
424 {
425         struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
426
427         pass->kind          = k_ir_prog_pass;
428         pass->run_on_irprog = void_prog_wrapper;
429         pass->context       = function;
430         pass->name          = name;
431
432         INIT_LIST_HEAD(&pass->list);
433
434         return pass;
435 }  /* def_prog_pass */
436
437 /* Creates an ir_prog pass for running void function(void). */
438 ir_prog_pass_t *def_prog_pass_constructor(
439         ir_prog_pass_t *pass,
440         const char *name,
441         void (*function)(ir_prog *irp, void *context))
442 {
443         if (pass == NULL)
444                 pass = XMALLOCZ(ir_prog_pass_t);
445
446         pass->kind          = k_ir_prog_pass;
447         pass->run_on_irprog = function;
448         pass->context       = pass;
449         pass->name          = name;
450
451         INIT_LIST_HEAD(&pass->list);
452
453         return pass;
454 }  /* def_prog_pass_constructor */