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 void ir_graph_pass_mgr_add(ir_graph_pass_manager_t *mgr, ir_graph_pass_t *pass)
42 list_add_tail(&pass->list, &mgr->passes);
45 pass->add_to_mgr(pass->context);
48 void ir_prog_pass_mgr_add(ir_prog_pass_manager_t *mgr, ir_prog_pass_t *pass)
50 list_add_tail(&pass->list, &mgr->passes);
53 pass->add_to_mgr(pass->context);
57 * wrapper for running a graph pass manager as a pass on an irprog
60 static int run_wrapper(ir_prog *prog, void *ctx)
62 ir_graph_pass_manager_t *mgr = (ir_graph_pass_manager_t*)ctx;
65 return ir_graph_pass_mgr_run(mgr);
68 int ir_prog_no_verify(ir_prog *prog, void *ctx)
75 void ir_prog_no_dump(ir_prog *prog, void *ctx, unsigned idx)
83 * Term wrapper for a wrapped ir_graph pass manager.
85 static void term_wrapper(void *context)
87 ir_graph_pass_manager_t *mgr = (ir_graph_pass_manager_t*)context;
88 term_graph_pass_mgr(mgr);
92 * Create a wrapper ir_prog pass for an ir_graph manager.
94 static ir_prog_pass_t *create_wrapper_pass(ir_graph_pass_manager_t *graph_mgr)
96 /* create a wrapper pass */
97 ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
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;
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;
109 pass->add_to_mgr = NULL;
110 pass->rem_from_mgr = term_wrapper;
115 void ir_prog_pass_mgr_add_graph_pass(
116 ir_prog_pass_manager_t *mgr, ir_graph_pass_t *pass)
118 ir_graph_pass_manager_t *graph_mgr;
119 ir_prog_pass_t *wrapper;
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;
127 ir_graph_pass_mgr_add(graph_mgr, pass);
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;
138 ir_graph_pass_mgr_add(graph_mgr, pass);
140 wrapper = create_wrapper_pass(graph_mgr);
141 ir_prog_pass_mgr_add(mgr, wrapper);
144 void ir_prog_pass_mgr_add_graph_mgr(
145 ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
147 ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
150 graph_mgr->dump_all = 1;
152 graph_mgr->verify_all = 1;
153 graph_mgr->run_idx = mgr->n_passes;
155 ir_prog_pass_mgr_add(mgr, pass);
158 static void create_suffix(char *suffix, size_t n, const char *pass_name)
160 snprintf(suffix, n, "%s.svg", pass_name);
163 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
165 ir_graph_pass_t *pass;
168 ir_graph *rem = current_ir_graph;
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);
179 /* verify is necessary */
180 if (mgr->verify_all) {
181 if (pass->verify_irg) {
182 pass->verify_irg(irg, pass->context);
189 if (pass->dump_irg) {
190 pass->dump_irg(irg, pass->context, idx);
193 create_suffix(buf, sizeof(buf), pass->name);
194 dump_ir_graph(irg, buf);
200 current_ir_graph = rem;
205 * Verify all graphs on the given ir_prog.
207 static int irp_verify_irgs(void)
211 size_t n_irgs = get_irp_n_irgs();
213 for (i = 0; i < n_irgs; ++i)
214 res &= irg_verify(get_irp_irg(i), 0);
218 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
220 ir_prog_pass_t *pass;
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);
229 /* verify is necessary */
230 if (mgr->verify_all) {
231 if (pass->verify_irprog) {
232 pass->verify_irprog(irp, pass->context);
239 if (pass->dump_irprog) {
240 pass->dump_irprog(irp, pass->context, idx);
243 create_suffix(buf, sizeof(buf), pass->name);
244 dump_all_ir_graphs(buf);
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;
256 ir_graph_pass_manager_t *new_graph_pass_mgr(
257 const char *name, int verify_all, int dump_all)
259 ir_graph_pass_manager_t *res = XMALLOCZ(ir_graph_pass_manager_t);
261 INIT_LIST_HEAD(&res->passes);
262 res->kind = k_ir_graph_pass_mgr;
265 res->verify_all = verify_all != 0;
266 res->dump_all = dump_all != 0;
271 ir_prog_pass_manager_t *new_prog_pass_mgr(
272 const char *name, int verify_all, int dump_all)
274 ir_prog_pass_manager_t *res = XMALLOCZ(ir_prog_pass_manager_t);
276 INIT_LIST_HEAD(&res->passes);
277 res->kind = k_ir_prog_pass_mgr;
280 res->verify_all = verify_all != 0;
281 res->dump_all = dump_all != 0;
286 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
288 ir_graph_pass_t *pass, *next;
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);
300 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
302 ir_prog_pass_t *pass, *next;
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);
314 void ir_graph_pass_mgr_set_run_idx(
315 ir_graph_pass_manager_t *mgr, unsigned run_idx)
317 mgr->run_idx = run_idx;
320 void ir_prog_pass_mgr_set_run_idx(
321 ir_prog_pass_manager_t *mgr, unsigned run_idx)
323 mgr->run_idx = run_idx;
327 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
329 static int void_graph_wrapper(ir_graph *irg, void *context)
331 void_pass_func_irg function = (void_pass_func_irg)context;
336 ir_graph_pass_t *def_graph_pass(
337 const char *name, void (*function)(ir_graph *irg))
339 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
341 pass->kind = k_ir_graph_pass;
342 pass->run_on_irg = void_graph_wrapper;
343 pass->context = (void*)function;
346 INIT_LIST_HEAD(&pass->list);
352 * Wrapper for running int function(ir_graph *irg) as an ir_graph pass.
354 static int int_graph_wrapper(ir_graph *irg, void *context)
356 int_pass_func_irg function = (int_pass_func_irg)context;
357 return function(irg);
360 ir_graph_pass_t *def_graph_pass_ret(
361 const char *name, int (*function)(ir_graph *irg))
363 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
365 pass->kind = k_ir_graph_pass;
366 pass->run_on_irg = int_graph_wrapper;
367 pass->context = (void*)function;
370 INIT_LIST_HEAD(&pass->list);
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)) {
379 pass = XMALLOCZ(ir_graph_pass_t);
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;
387 INIT_LIST_HEAD(&pass->list);
392 void ir_graph_pass_set_parallel(ir_graph_pass_t *pass, int flag)
394 pass->run_parallel = flag != 0;
398 * Wrapper for running void function(void) as an ir_prog pass.
400 static int void_prog_wrapper(ir_prog *irp, void *context)
402 void_pass_func function = (void_pass_func)context;
409 ir_prog_pass_t *def_prog_pass(
411 void (*function)(void))
413 struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
415 pass->kind = k_ir_prog_pass;
416 pass->run_on_irprog = void_prog_wrapper;
417 pass->context = (void*)function;
420 INIT_LIST_HEAD(&pass->list);
425 ir_prog_pass_t *def_prog_pass_constructor(
426 ir_prog_pass_t *pass,
428 int (*function)(ir_prog *irp, void *context))
431 pass = XMALLOCZ(ir_prog_pass_t);
433 memset(pass, 0, sizeof(ir_prog_pass_t));
435 pass->kind = k_ir_prog_pass;
436 pass->run_on_irprog = function;
437 pass->context = pass;
440 INIT_LIST_HEAD(&pass->list);
445 typedef struct pass_t {
448 void (*function)(void *context);
452 * Wrapper for the call_function pass.
454 static int call_function_wrapper(ir_prog *irp, void *context)
456 pass_t *pass = (pass_t*)context;
459 pass->function(pass->context);
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);
467 def_prog_pass_constructor(
468 &pass->pass, name ? name : "set_function", call_function_wrapper);
470 pass->pass.verify_irprog = ir_prog_no_verify;
471 pass->pass.dump_irprog = ir_prog_no_dump;
472 pass->pass.context = pass;
474 pass->function = function;
475 pass->context = context;