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