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