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
31 #include "irgraph_t.h"
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);
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)
44 list_add_tail(&pass->list, &mgr->passes);
47 pass->add_to_mgr(pass->context);
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)
53 list_add_tail(&pass->list, &mgr->passes);
56 pass->add_to_mgr(pass->context);
60 * wrapper for running a graph pass manager as a pass on an irprog
63 static int run_wrapper(ir_prog *prog, void *ctx)
65 ir_graph_pass_manager_t *mgr = (ir_graph_pass_manager_t*)ctx;
68 return ir_graph_pass_mgr_run(mgr);
71 /* Ensure that no verifier is run an ir_prog pass. */
72 int ir_prog_no_verify(ir_prog *prog, void *ctx)
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)
88 * Term wrapper for a wrapped ir_graph pass manager.
90 static void term_wrapper(void *context)
92 ir_graph_pass_manager_t *mgr = (ir_graph_pass_manager_t*)context;
93 term_graph_pass_mgr(mgr);
97 * Create a wrapper ir_prog pass for an ir_graph manager.
99 static ir_prog_pass_t *create_wrapper_pass(ir_graph_pass_manager_t *graph_mgr)
101 /* create a wrapper pass */
102 ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
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;
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;
114 pass->add_to_mgr = NULL;
115 pass->rem_from_mgr = term_wrapper;
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)
124 ir_graph_pass_manager_t *graph_mgr;
125 ir_prog_pass_t *wrapper;
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;
133 ir_graph_pass_mgr_add(graph_mgr, pass);
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;
144 ir_graph_pass_mgr_add(graph_mgr, pass);
146 wrapper = create_wrapper_pass(graph_mgr);
147 ir_prog_pass_mgr_add(mgr, wrapper);
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)
154 ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
157 graph_mgr->dump_all = 1;
159 graph_mgr->verify_all = 1;
160 graph_mgr->run_idx = mgr->n_passes;
162 ir_prog_pass_mgr_add(mgr, pass);
165 static void create_suffix(char *suffix, size_t n, const char *pass_name)
167 snprintf(suffix, n, "%s.svg", pass_name);
170 /* Run all passes of an ir_graph pass manager. */
171 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
173 ir_graph_pass_t *pass;
176 ir_graph *rem = current_ir_graph;
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);
187 /* verify is necessary */
188 if (mgr->verify_all) {
189 if (pass->verify_irg) {
190 pass->verify_irg(irg, pass->context);
197 if (pass->dump_irg) {
198 pass->dump_irg(irg, pass->context, idx);
201 create_suffix(buf, sizeof(buf), pass->name);
202 dump_ir_graph(irg, buf);
208 current_ir_graph = rem;
213 * Verify all graphs on the given ir_prog.
215 static int irp_verify_irgs(void)
219 size_t n_irgs = get_irp_n_irgs();
221 for (i = 0; i < n_irgs; ++i)
222 res &= irg_verify(get_irp_irg(i), 0);
226 /* Run all passes of an ir_prog pass manager. */
227 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
229 ir_prog_pass_t *pass;
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);
238 /* verify is necessary */
239 if (mgr->verify_all) {
240 if (pass->verify_irprog) {
241 pass->verify_irprog(irp, pass->context);
248 if (pass->dump_irprog) {
249 pass->dump_irprog(irp, pass->context, idx);
252 create_suffix(buf, sizeof(buf), pass->name);
253 dump_all_ir_graphs(buf);
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;
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)
269 ir_graph_pass_manager_t *res = XMALLOCZ(ir_graph_pass_manager_t);
271 INIT_LIST_HEAD(&res->passes);
272 res->kind = k_ir_graph_pass_mgr;
275 res->verify_all = verify_all != 0;
276 res->dump_all = dump_all != 0;
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)
285 ir_prog_pass_manager_t *res = XMALLOCZ(ir_prog_pass_manager_t);
287 INIT_LIST_HEAD(&res->passes);
288 res->kind = k_ir_prog_pass_mgr;
291 res->verify_all = verify_all != 0;
292 res->dump_all = dump_all != 0;
297 /* Terminate an ir_graph pass manager and all owned passes. */
298 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
300 ir_graph_pass_t *pass, *next;
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);
312 /* Terminate an ir_prog pass manager and all owned passes. */
313 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
315 ir_prog_pass_t *pass, *next;
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);
328 * Set the run index for an irgraph pass manager.
330 * @param mgr the manager
331 * @param run_idx the index for the first pass of this manager
333 void ir_graph_pass_mgr_set_run_idx(
334 ir_graph_pass_manager_t *mgr, unsigned run_idx)
336 mgr->run_idx = run_idx;
340 * Set the run index for an irprog pass manager.
342 * @param mgr the manager
343 * @param run_idx the index for the first pass of this manager
345 void ir_prog_pass_mgr_set_run_idx(
346 ir_prog_pass_manager_t *mgr, unsigned run_idx)
348 mgr->run_idx = run_idx;
352 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
354 static int void_graph_wrapper(ir_graph *irg, void *context)
356 void_pass_func_irg function = (void_pass_func_irg)context;
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))
365 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
367 pass->kind = k_ir_graph_pass;
368 pass->run_on_irg = void_graph_wrapper;
369 pass->context = (void*)function;
372 INIT_LIST_HEAD(&pass->list);
378 * Wrapper for running int function(ir_graph *irg) as an ir_graph pass.
380 static int int_graph_wrapper(ir_graph *irg, void *context)
382 int_pass_func_irg function = (int_pass_func_irg)context;
383 return function(irg);
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))
390 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
392 pass->kind = k_ir_graph_pass;
393 pass->run_on_irg = int_graph_wrapper;
394 pass->context = (void*)function;
397 INIT_LIST_HEAD(&pass->list);
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)) {
407 pass = XMALLOCZ(ir_graph_pass_t);
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;
415 INIT_LIST_HEAD(&pass->list);
420 /* set the run parallel property */
421 void ir_graph_pass_set_parallel(ir_graph_pass_t *pass, int flag)
423 pass->run_parallel = flag != 0;
427 * Wrapper for running void function(void) as an ir_prog pass.
429 static int void_prog_wrapper(ir_prog *irp, void *context)
431 void_pass_func function = (void_pass_func)context;
438 /* Creates an ir_prog pass for running void function(void). */
439 ir_prog_pass_t *def_prog_pass(
441 void (*function)(void))
443 struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
445 pass->kind = k_ir_prog_pass;
446 pass->run_on_irprog = void_prog_wrapper;
447 pass->context = (void*)function;
450 INIT_LIST_HEAD(&pass->list);
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,
459 int (*function)(ir_prog *irp, void *context))
462 pass = XMALLOCZ(ir_prog_pass_t);
464 memset(pass, 0, sizeof(ir_prog_pass_t));
466 pass->kind = k_ir_prog_pass;
467 pass->run_on_irprog = function;
468 pass->context = pass;
471 INIT_LIST_HEAD(&pass->list);
476 typedef struct pass_t {
479 void (*function)(void *context);
483 * Wrapper for the call_function pass.
485 static int call_function_wrapper(ir_prog *irp, void *context)
487 pass_t *pass = (pass_t*)context;
490 pass->function(pass->context);
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);
498 def_prog_pass_constructor(
499 &pass->pass, name ? name : "set_function", call_function_wrapper);
501 pass->pass.verify_irprog = ir_prog_no_verify;
502 pass->pass.dump_irprog = ir_prog_no_dump;
503 pass->pass.context = pass;
505 pass->function = function;
506 pass->context = context;