b34e01525ccabe4c1cd4d965d140152263fe74be
[libfirm] / ir / ir / irpass.c
1 /*
2  * Copyright (C) 1995-2010 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 "irverify.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_tail(&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_tail(&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 /* Ensure that no verifier is run an ir_prog pass. */
68 int ir_prog_no_verify(ir_prog *prog, void *ctx)
69 {
70         (void)prog;
71         (void)ctx;
72         return 0;
73 }
74
75 /* Ensure that no dumper is run from an ir_prog pass. */
76 void ir_prog_no_dump(ir_prog *prog, void *ctx, unsigned idx)
77 {
78         (void)prog;
79         (void)ctx;
80         (void)idx;
81 }
82
83 /**
84  * Term wrapper for a wrapped ir_graph pass manager.
85  */
86 static void term_wrapper(void *context)
87 {
88         ir_graph_pass_manager_t *mgr = context;
89         term_graph_pass_mgr(mgr);
90 }
91
92 /**
93  * Create a wrapper ir_prog pass for an ir_graph manager.
94  */
95 static ir_prog_pass_t *create_wrapper_pass(ir_graph_pass_manager_t *graph_mgr)
96 {
97         /* create a wrapper pass */
98         ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
99
100         pass->kind          = k_ir_prog_pass;
101         pass->run_on_irprog = run_wrapper;
102         pass->context       = graph_mgr;
103         pass->name          = graph_mgr->name;
104
105         /* do not verify nor dump: this is handled by the graph manager */
106         pass->verify_irprog = ir_prog_no_verify;
107         pass->dump_irprog   = ir_prog_no_dump;
108         pass->is_wrapper    = 1;
109
110         pass->add_to_mgr   = NULL;
111         pass->rem_from_mgr = term_wrapper;
112
113         return pass;
114 }
115
116 /* Add an ir_graph_pass as a pass to an ir_prog pass manager. */
117 void ir_prog_pass_mgr_add_graph_pass(
118         ir_prog_pass_manager_t *mgr, ir_graph_pass_t *pass)
119 {
120         ir_graph_pass_manager_t *graph_mgr;
121         ir_prog_pass_t          *wrapper;
122
123         /* check if the last pass is a graph_pass wrapper */
124         if (! list_empty(&mgr->passes)) {
125                 wrapper = list_entry(mgr->passes.prev, ir_prog_pass_t, list);
126                 if (wrapper->is_wrapper) {
127                         graph_mgr = wrapper->context;
128
129                         ir_graph_pass_mgr_add(graph_mgr, pass);
130                         ++mgr->n_passes;
131                         return;
132                 }
133         }
134
135         /* not found, create a new wrapper */
136         graph_mgr = new_graph_pass_mgr(
137                 "graph_pass_wrapper", mgr->verify_all, mgr->dump_all);
138         graph_mgr->run_idx = mgr->run_idx + mgr->n_passes;
139
140         ir_graph_pass_mgr_add(graph_mgr, pass);
141
142         wrapper = create_wrapper_pass(graph_mgr);
143         ir_prog_pass_mgr_add(mgr, wrapper);
144 }
145
146 /* Add an ir_graph_pass_manager as a pass to an ir_prog pass manager. */
147 void ir_prog_pass_mgr_add_graph_mgr(
148         ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
149 {
150         ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
151
152         if (mgr->dump_all)
153                 graph_mgr->dump_all = 1;
154         if (mgr->verify_all)
155                 graph_mgr->verify_all = 1;
156         graph_mgr->run_idx = mgr->n_passes;
157
158         ir_prog_pass_mgr_add(mgr, pass);
159 }
160
161 static void create_suffix(char *suffix, size_t n, const char *pass_name)
162 {
163         snprintf(suffix, n, "%s.svg", pass_name);
164 }
165
166 /* Run all passes of an ir_graph pass manager. */
167 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
168 {
169         ir_graph_pass_t *pass;
170         int             i, res = 0;
171         ir_graph        *rem = current_ir_graph;
172
173         /* on all graphs: beware: number of irgs might be changed */
174         for (i = 0; i < get_irp_n_irgs(); ++i) {
175                 ir_graph *irg = current_ir_graph = get_irp_irg(i);
176                 unsigned idx = mgr->run_idx;
177                 /* run every pass on every graph */
178                 list_for_each_entry(ir_graph_pass_t, pass, &mgr->passes, list) {
179                         int pass_res = pass->run_on_irg(irg, pass->context);
180                         if (pass_res != 0)
181                                 res = 1;
182                         /* verify is necessary */
183                         if (mgr->verify_all) {
184                                 if (pass->verify_irg) {
185                                         pass->verify_irg(irg, pass->context);
186                                 } else {
187                                         irg_verify(irg, 0);
188                                 }
189                         }
190                         /* dump */
191                         if (mgr->dump_all) {
192                                 if (pass->dump_irg) {
193                                         pass->dump_irg(irg, pass->context, idx);
194                                 } else {
195                                         char buf[1024];
196                                         create_suffix(buf, sizeof(buf), pass->name);
197                                         dump_ir_graph(irg, buf);
198                                 }
199                         }
200                         ++idx;
201                 }
202         }
203         current_ir_graph = rem;
204         return res;
205 }
206
207 /**
208  * Verify all graphs on the given ir_prog.
209  */
210 static int irp_verify_irgs(void)
211 {
212         int i, res = 1;
213
214         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
215                 res &= irg_verify(get_irp_irg(i), 0);
216         return res;
217 }
218
219 /* Run all passes of an ir_prog pass manager. */
220 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
221 {
222         ir_prog_pass_t *pass;
223         int            res = 0;
224
225         /* run every pass on every graph */
226         unsigned idx = mgr->run_idx;
227         list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
228                 int pass_res = pass->run_on_irprog(irp, pass->context);
229                 if (pass_res != 0)
230                         res = 1;
231                 /* verify is necessary */
232                 if (mgr->verify_all) {
233                         if (pass->verify_irprog) {
234                                 pass->verify_irprog(irp, pass->context);
235                         } else {
236                                 irp_verify_irgs();
237                         }
238                 }
239                 /* dump */
240                 if (mgr->dump_all) {
241                         if (pass->dump_irprog) {
242                                 pass->dump_irprog(irp, pass->context, idx);
243                         } else {
244                                 char buf[1024];
245                                 create_suffix(buf, sizeof(buf), pass->name);
246                                 dump_all_ir_graphs(buf);
247                         }
248                 }
249                 if (pass->is_wrapper) {
250                         ir_graph_pass_manager_t *graph_mgr = pass->context;
251                         idx += graph_mgr->n_passes;
252                 } else
253                         ++idx;
254         }
255         return res;
256 }
257
258 /* Creates a new ir_graph pass manager. */
259 ir_graph_pass_manager_t *new_graph_pass_mgr(
260         const char *name, int verify_all, int dump_all)
261 {
262         ir_graph_pass_manager_t *res = XMALLOCZ(ir_graph_pass_manager_t);
263
264         INIT_LIST_HEAD(&res->passes);
265         res->kind       = k_ir_graph_pass_mgr;
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 = XMALLOCZ(ir_prog_pass_manager_t);
279
280         INIT_LIST_HEAD(&res->passes);
281         res->kind       = k_ir_prog_pass_mgr;
282         res->name       = name;
283         res->run_idx    = 0;
284         res->verify_all = verify_all != 0;
285         res->dump_all   = dump_all   != 0;
286
287         return res;
288 }
289
290 /* Terminate an ir_graph pass manager and all owned passes. */
291 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
292 {
293         ir_graph_pass_t *pass, *next;
294
295         list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
296                 if (pass->rem_from_mgr)
297                         pass->rem_from_mgr(pass->context);
298                 pass->kind = k_BAD;
299                 xfree(pass);
300         }
301         mgr->kind = k_BAD;
302         xfree(mgr);
303 }
304
305 /* Terminate an ir_prog pass manager and all owned passes. */
306 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
307 {
308         ir_prog_pass_t *pass, *next;
309
310         list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
311                 if (pass->rem_from_mgr)
312                         pass->rem_from_mgr(pass->context);
313                 pass->kind = k_BAD;
314                 xfree(pass);
315         }
316         mgr->kind = k_BAD;
317         xfree(mgr);
318 }
319
320 /**
321  * Set the run index for an irgraph pass manager.
322  *
323  * @param mgr      the manager
324  * @param run_idx  the index for the first pass of this manager
325  */
326 void ir_graph_pass_mgr_set_run_idx(
327         ir_graph_pass_manager_t *mgr, unsigned run_idx)
328 {
329         mgr->run_idx = run_idx;
330 }
331
332 /**
333  * Set the run index for an irprog pass manager.
334  *
335  * @param mgr      the manager
336  * @param run_idx  the index for the first pass of this manager
337  */
338 void ir_prog_pass_mgr_set_run_idx(
339         ir_prog_pass_manager_t *mgr, unsigned run_idx)
340 {
341         mgr->run_idx = run_idx;
342 }
343
344 /**
345  * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
346  */
347 static int void_graph_wrapper(ir_graph *irg, void *context)
348 {
349         void (*function)(ir_graph *irg) = context;
350         function(irg);
351         return 0;
352 }  /* void_graph_wrapper */
353
354 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
355 ir_graph_pass_t *def_graph_pass(
356         const char *name, void (*function)(ir_graph *irg))
357 {
358         struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
359
360         pass->kind       = k_ir_graph_pass;
361         pass->run_on_irg = void_graph_wrapper;
362         pass->context    = function;
363         pass->name       = name;
364
365         INIT_LIST_HEAD(&pass->list);
366
367         return pass;
368 }  /* def_graph_pass */
369
370 /**
371  * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
372  */
373 static int int_graph_wrapper(ir_graph *irg, void *context)
374 {
375         int (*function)(ir_graph *irg) = context;
376         return function(irg);
377 }  /* int_graph_wrapper */
378
379 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
380 ir_graph_pass_t *def_graph_pass_ret(
381                 const char *name, int (*function)(ir_graph *irg))
382 {
383         struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
384
385         pass->kind       = k_ir_graph_pass;
386         pass->run_on_irg = int_graph_wrapper;
387         pass->context    = function;
388         pass->name       = name;
389
390         INIT_LIST_HEAD(&pass->list);
391
392         return pass;
393 }  /* def_graph_pass_ret */
394
395 /* constructor for a default graph pass */
396 ir_graph_pass_t *def_graph_pass_constructor(
397         ir_graph_pass_t *pass,
398         const char *name, int (*function)(ir_graph *irg, void *context)) {
399         if (pass == NULL)
400                 pass = XMALLOCZ(ir_graph_pass_t);
401         else
402                 memset(pass, 0, sizeof(ir_graph_pass_t));
403         pass->kind       = k_ir_graph_pass;
404         pass->run_on_irg = function;
405         pass->context    = pass;
406         pass->name       = name;
407
408         INIT_LIST_HEAD(&pass->list);
409
410         return pass;
411 }  /* def_graph_pass_constructor */
412
413 /* set the run parallel property */
414 void ir_graph_pass_set_parallel(ir_graph_pass_t *pass, int flag)
415 {
416         pass->run_parallel = flag != 0;
417 }  /* ir_graph_pass_set_parallel */
418
419 /**
420  * Wrapper for running void function(void) as an ir_prog pass.
421  */
422 static int void_prog_wrapper(ir_prog *irp, void *context)
423 {
424         void (*function)(void) = context;
425
426         (void)irp;
427         function();
428         return 0;
429 }  /* void_graph_wrapper */
430
431 /* Creates an ir_prog pass for running void function(void). */
432 ir_prog_pass_t *def_prog_pass(
433         const char *name,
434         void (*function)(void))
435 {
436         struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
437
438         pass->kind          = k_ir_prog_pass;
439         pass->run_on_irprog = void_prog_wrapper;
440         pass->context       = function;
441         pass->name          = name;
442
443         INIT_LIST_HEAD(&pass->list);
444
445         return pass;
446 }  /* def_prog_pass */
447
448 /* Creates an ir_prog pass for running void function(void). */
449 ir_prog_pass_t *def_prog_pass_constructor(
450         ir_prog_pass_t *pass,
451         const char *name,
452         int (*function)(ir_prog *irp, void *context))
453 {
454         if (pass == NULL)
455                 pass = XMALLOCZ(ir_prog_pass_t);
456         else
457                 memset(pass, 0, sizeof(ir_prog_pass_t));
458
459         pass->kind          = k_ir_prog_pass;
460         pass->run_on_irprog = function;
461         pass->context       = pass;
462         pass->name          = name;
463
464         INIT_LIST_HEAD(&pass->list);
465
466         return pass;
467 }  /* def_prog_pass_constructor */
468
469 struct pass_t {
470         ir_prog_pass_t pass;
471         void           *context;
472         void (*function)(void *context);
473 };
474
475 /**
476  * Wrapper for the call_function pass.
477  */
478 static int call_function_wrapper(ir_prog *irp, void *context)
479 {
480         struct pass_t *pass = context;
481
482         (void)irp;
483         pass->function(pass->context);
484         return 0;
485 }  /* call_function_wrapper */
486
487 ir_prog_pass_t *call_function_pass(
488         const char *name, void (*function)(void *context), void *context) {
489         struct pass_t *pass = XMALLOCZ(struct pass_t);
490
491         def_prog_pass_constructor(
492                 &pass->pass, name ? name : "set_function", call_function_wrapper);
493
494         pass->pass.verify_irprog = ir_prog_no_verify;
495         pass->pass.dump_irprog   = ir_prog_no_dump;
496         pass->pass.context       = pass;
497
498         pass->function = function;
499         pass->context  = context;
500
501         return &pass->pass;
502 }  /* call_function_pass */