2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Manager for optimization passes.
23 * @author Michael Beck
30 #include "irgraph_t.h"
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);
40 /*Add a graph pass to a graph pass manager. */
41 void ir_graph_pass_mgr_add(ir_graph_pass_manager_t *mgr, ir_graph_pass_t *pass)
43 list_add_tail(&pass->list, &mgr->passes);
46 pass->add_to_mgr(pass->context);
49 /* Add an irprog pass to an irprog pass manager. */
50 void ir_prog_pass_mgr_add(ir_prog_pass_manager_t *mgr, ir_prog_pass_t *pass)
52 list_add_tail(&pass->list, &mgr->passes);
55 pass->add_to_mgr(pass->context);
59 * wrapper for running a graph pass manager as a pass on an irprog
62 static int run_wrapper(ir_prog *prog, void *ctx)
64 ir_graph_pass_manager_t *mgr = (ir_graph_pass_manager_t*)ctx;
67 return ir_graph_pass_mgr_run(mgr);
70 /* Ensure that no verifier is run an ir_prog pass. */
71 int ir_prog_no_verify(ir_prog *prog, void *ctx)
78 /* Ensure that no dumper is run from an ir_prog pass. */
79 void ir_prog_no_dump(ir_prog *prog, void *ctx, unsigned idx)
87 * Term wrapper for a wrapped ir_graph pass manager.
89 static void term_wrapper(void *context)
91 ir_graph_pass_manager_t *mgr = (ir_graph_pass_manager_t*)context;
92 term_graph_pass_mgr(mgr);
96 * Create a wrapper ir_prog pass for an ir_graph manager.
98 static ir_prog_pass_t *create_wrapper_pass(ir_graph_pass_manager_t *graph_mgr)
100 /* create a wrapper pass */
101 ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
103 pass->kind = k_ir_prog_pass;
104 pass->run_on_irprog = run_wrapper;
105 pass->context = graph_mgr;
106 pass->name = graph_mgr->name;
108 /* do not verify nor dump: this is handled by the graph manager */
109 pass->verify_irprog = ir_prog_no_verify;
110 pass->dump_irprog = ir_prog_no_dump;
111 pass->is_wrapper = 1;
113 pass->add_to_mgr = NULL;
114 pass->rem_from_mgr = term_wrapper;
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)
123 ir_graph_pass_manager_t *graph_mgr;
124 ir_prog_pass_t *wrapper;
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 = (ir_graph_pass_manager_t*)wrapper->context;
132 ir_graph_pass_mgr_add(graph_mgr, pass);
138 /* not found, create a new wrapper */
139 graph_mgr = new_graph_pass_mgr(
140 "graph_pass_wrapper", mgr->verify_all, mgr->dump_all);
141 graph_mgr->run_idx = mgr->run_idx + mgr->n_passes;
143 ir_graph_pass_mgr_add(graph_mgr, pass);
145 wrapper = create_wrapper_pass(graph_mgr);
146 ir_prog_pass_mgr_add(mgr, wrapper);
149 /* Add an ir_graph_pass_manager as a pass to an ir_prog pass manager. */
150 void ir_prog_pass_mgr_add_graph_mgr(
151 ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
153 ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
156 graph_mgr->dump_all = 1;
158 graph_mgr->verify_all = 1;
159 graph_mgr->run_idx = mgr->n_passes;
161 ir_prog_pass_mgr_add(mgr, pass);
164 static void create_suffix(char *suffix, size_t n, const char *pass_name)
166 snprintf(suffix, n, "%s.svg", pass_name);
169 /* Run all passes of an ir_graph pass manager. */
170 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
172 ir_graph_pass_t *pass;
175 ir_graph *rem = current_ir_graph;
177 /* on all graphs: beware: number of irgs might be changed */
178 for (i = 0; i < get_irp_n_irgs(); ++i) {
179 ir_graph *irg = current_ir_graph = get_irp_irg(i);
180 unsigned idx = mgr->run_idx;
181 /* run every pass on every graph */
182 list_for_each_entry(ir_graph_pass_t, pass, &mgr->passes, list) {
183 int pass_res = pass->run_on_irg(irg, pass->context);
186 /* verify is necessary */
187 if (mgr->verify_all) {
188 if (pass->verify_irg) {
189 pass->verify_irg(irg, pass->context);
196 if (pass->dump_irg) {
197 pass->dump_irg(irg, pass->context, idx);
200 create_suffix(buf, sizeof(buf), pass->name);
201 dump_ir_graph(irg, buf);
207 current_ir_graph = rem;
212 * Verify all graphs on the given ir_prog.
214 static int irp_verify_irgs(void)
218 size_t n_irgs = get_irp_n_irgs();
220 for (i = 0; i < n_irgs; ++i)
221 res &= irg_verify(get_irp_irg(i), 0);
225 /* Run all passes of an ir_prog pass manager. */
226 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
228 ir_prog_pass_t *pass;
231 /* run every pass on every graph */
232 unsigned idx = mgr->run_idx;
233 list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
234 int pass_res = pass->run_on_irprog(irp, pass->context);
237 /* verify is necessary */
238 if (mgr->verify_all) {
239 if (pass->verify_irprog) {
240 pass->verify_irprog(irp, pass->context);
247 if (pass->dump_irprog) {
248 pass->dump_irprog(irp, pass->context, idx);
251 create_suffix(buf, sizeof(buf), pass->name);
252 dump_all_ir_graphs(buf);
255 if (pass->is_wrapper) {
256 ir_graph_pass_manager_t *graph_mgr = (ir_graph_pass_manager_t*)pass->context;
257 idx += graph_mgr->n_passes;
264 /* Creates a new ir_graph pass manager. */
265 ir_graph_pass_manager_t *new_graph_pass_mgr(
266 const char *name, int verify_all, int dump_all)
268 ir_graph_pass_manager_t *res = XMALLOCZ(ir_graph_pass_manager_t);
270 INIT_LIST_HEAD(&res->passes);
271 res->kind = k_ir_graph_pass_mgr;
274 res->verify_all = verify_all != 0;
275 res->dump_all = dump_all != 0;
280 /* Creates a new ir_prog pass manager. */
281 ir_prog_pass_manager_t *new_prog_pass_mgr(
282 const char *name, int verify_all, int dump_all)
284 ir_prog_pass_manager_t *res = XMALLOCZ(ir_prog_pass_manager_t);
286 INIT_LIST_HEAD(&res->passes);
287 res->kind = k_ir_prog_pass_mgr;
290 res->verify_all = verify_all != 0;
291 res->dump_all = dump_all != 0;
296 /* Terminate an ir_graph pass manager and all owned passes. */
297 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
299 ir_graph_pass_t *pass, *next;
301 list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
302 if (pass->rem_from_mgr)
303 pass->rem_from_mgr(pass->context);
311 /* Terminate an ir_prog pass manager and all owned passes. */
312 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
314 ir_prog_pass_t *pass, *next;
316 list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
317 if (pass->rem_from_mgr)
318 pass->rem_from_mgr(pass->context);
327 * Set the run index for an irgraph pass manager.
329 * @param mgr the manager
330 * @param run_idx the index for the first pass of this manager
332 void ir_graph_pass_mgr_set_run_idx(
333 ir_graph_pass_manager_t *mgr, unsigned run_idx)
335 mgr->run_idx = run_idx;
339 * Set the run index for an irprog pass manager.
341 * @param mgr the manager
342 * @param run_idx the index for the first pass of this manager
344 void ir_prog_pass_mgr_set_run_idx(
345 ir_prog_pass_manager_t *mgr, unsigned run_idx)
347 mgr->run_idx = run_idx;
351 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
353 static int void_graph_wrapper(ir_graph *irg, void *context)
355 void_pass_func_irg function = (void_pass_func_irg)context;
360 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
361 ir_graph_pass_t *def_graph_pass(
362 const char *name, void (*function)(ir_graph *irg))
364 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
366 pass->kind = k_ir_graph_pass;
367 pass->run_on_irg = void_graph_wrapper;
368 pass->context = (void*)function;
371 INIT_LIST_HEAD(&pass->list);
377 * Wrapper for running int function(ir_graph *irg) as an ir_graph pass.
379 static int int_graph_wrapper(ir_graph *irg, void *context)
381 int_pass_func_irg function = (int_pass_func_irg)context;
382 return function(irg);
385 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
386 ir_graph_pass_t *def_graph_pass_ret(
387 const char *name, int (*function)(ir_graph *irg))
389 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
391 pass->kind = k_ir_graph_pass;
392 pass->run_on_irg = int_graph_wrapper;
393 pass->context = (void*)function;
396 INIT_LIST_HEAD(&pass->list);
401 /* constructor for a default graph pass */
402 ir_graph_pass_t *def_graph_pass_constructor(
403 ir_graph_pass_t *pass,
404 const char *name, int (*function)(ir_graph *irg, void *context)) {
406 pass = XMALLOCZ(ir_graph_pass_t);
408 memset(pass, 0, sizeof(ir_graph_pass_t));
409 pass->kind = k_ir_graph_pass;
410 pass->run_on_irg = function;
411 pass->context = pass;
414 INIT_LIST_HEAD(&pass->list);
419 /* set the run parallel property */
420 void ir_graph_pass_set_parallel(ir_graph_pass_t *pass, int flag)
422 pass->run_parallel = flag != 0;
426 * Wrapper for running void function(void) as an ir_prog pass.
428 static int void_prog_wrapper(ir_prog *irp, void *context)
430 void_pass_func function = (void_pass_func)context;
437 /* Creates an ir_prog pass for running void function(void). */
438 ir_prog_pass_t *def_prog_pass(
440 void (*function)(void))
442 struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
444 pass->kind = k_ir_prog_pass;
445 pass->run_on_irprog = void_prog_wrapper;
446 pass->context = (void*)function;
449 INIT_LIST_HEAD(&pass->list);
454 /* Creates an ir_prog pass for running void function(void). */
455 ir_prog_pass_t *def_prog_pass_constructor(
456 ir_prog_pass_t *pass,
458 int (*function)(ir_prog *irp, void *context))
461 pass = XMALLOCZ(ir_prog_pass_t);
463 memset(pass, 0, sizeof(ir_prog_pass_t));
465 pass->kind = k_ir_prog_pass;
466 pass->run_on_irprog = function;
467 pass->context = pass;
470 INIT_LIST_HEAD(&pass->list);
475 typedef struct pass_t {
478 void (*function)(void *context);
482 * Wrapper for the call_function pass.
484 static int call_function_wrapper(ir_prog *irp, void *context)
486 pass_t *pass = (pass_t*)context;
489 pass->function(pass->context);
493 ir_prog_pass_t *call_function_pass(
494 const char *name, void (*function)(void *context), void *context) {
495 struct pass_t *pass = XMALLOCZ(struct pass_t);
497 def_prog_pass_constructor(
498 &pass->pass, name ? name : "set_function", call_function_wrapper);
500 pass->pass.verify_irprog = ir_prog_no_verify;
501 pass->pass.dump_irprog = ir_prog_no_dump;
502 pass->pass.context = pass;
504 pass->function = function;
505 pass->context = context;