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