2 * Copyright (C) 1995-2009 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 /*Add a graph pass to a graph pass manager. */
38 void ir_graph_pass_mgr_add(ir_graph_pass_manager_t *mgr, ir_graph_pass_t *pass)
40 list_add_tail(&pass->list, &mgr->passes);
43 pass->add_to_mgr(pass->context);
46 /* Add an irprog pass to an irprog pass manager. */
47 void ir_prog_pass_mgr_add(ir_prog_pass_manager_t *mgr, ir_prog_pass_t *pass)
49 list_add_tail(&pass->list, &mgr->passes);
52 pass->add_to_mgr(pass->context);
56 * wrapper for running a graph pass manager as a pass on an irprog
59 static int run_wrapper(ir_prog *prog, void *ctx)
61 ir_graph_pass_manager_t *mgr = ctx;
64 return ir_graph_pass_mgr_run(mgr);
67 /* Ensure that no verifier is run an ir_prog pass. */
68 int ir_prog_no_verify(ir_prog *prog, void *ctx)
75 /* Ensure that no dumper is run from an ir_prog pass. */
76 void ir_prog_no_dump(ir_prog *prog, void *ctx, unsigned idx)
84 * Term wrapper for a wrapped ir_graph pass manager.
86 static void term_wrapper(void *context)
88 ir_graph_pass_manager_t *mgr = context;
89 term_graph_pass_mgr(mgr);
93 * Create a wrapper ir_prog pass for an ir_graph manager.
95 static ir_prog_pass_t *create_wrapper_pass(ir_graph_pass_manager_t *graph_mgr)
97 /* create a wrapper pass */
98 ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
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 /* Add an ir_graph_pass as a pass to an ir_prog pass manager. */
116 void ir_prog_pass_mgr_add_graph_pass(
117 ir_prog_pass_manager_t *mgr, ir_graph_pass_t *pass)
119 ir_graph_pass_manager_t *graph_mgr;
120 ir_prog_pass_t *wrapper;
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 = wrapper->context;
128 ir_graph_pass_mgr_add(graph_mgr, pass);
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;
139 ir_graph_pass_mgr_add(graph_mgr, pass);
141 wrapper = create_wrapper_pass(graph_mgr);
142 ir_prog_pass_mgr_add(mgr, wrapper);
145 /* Add an ir_graph_pass_manager as a pass to an ir_prog pass manager. */
146 void ir_prog_pass_mgr_add_graph_mgr(
147 ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
149 ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
152 graph_mgr->dump_all = 1;
154 graph_mgr->verify_all = 1;
155 graph_mgr->run_idx = mgr->n_passes;
157 ir_prog_pass_mgr_add(mgr, pass);
160 static void create_suffix(char *suffix, size_t n, const char *pass_name)
162 snprintf(suffix, n, "%s.svg", pass_name);
165 /* Run all passes of an ir_graph pass manager. */
166 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
168 ir_graph_pass_t *pass;
170 ir_graph *rem = current_ir_graph;
172 /* on all graphs: beware: number of irgs might be changed */
173 for (i = 0; i < get_irp_n_irgs(); ++i) {
174 ir_graph *irg = current_ir_graph = get_irp_irg(i);
175 unsigned idx = mgr->run_idx;
176 /* run every pass on every graph */
177 list_for_each_entry(ir_graph_pass_t, pass, &mgr->passes, list) {
178 int pass_res = pass->run_on_irg(irg, pass->context);
181 /* verify is necessary */
182 if (mgr->verify_all) {
183 if (pass->verify_irg) {
184 pass->verify_irg(irg, pass->context);
191 if (pass->dump_irg) {
192 pass->dump_irg(irg, pass->context, idx);
195 create_suffix(buf, sizeof(buf), pass->name);
196 dump_ir_graph(irg, buf);
202 current_ir_graph = rem;
207 * Verify all graphs on the given ir_prog.
209 static int irp_verify_irgs(void)
213 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
214 res &= irg_verify(get_irp_irg(i), 0);
218 /* Run all passes of an ir_prog pass manager. */
219 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
221 ir_prog_pass_t *pass;
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);
230 /* verify is necessary */
231 if (mgr->verify_all) {
232 if (pass->verify_irprog) {
233 pass->verify_irprog(irp, pass->context);
240 if (pass->dump_irprog) {
241 pass->dump_irprog(irp, pass->context, idx);
244 create_suffix(buf, sizeof(buf), pass->name);
245 dump_all_ir_graphs(buf);
248 if (pass->is_wrapper) {
249 ir_graph_pass_manager_t *graph_mgr = pass->context;
250 idx += graph_mgr->n_passes;
257 /* Creates a new ir_graph pass manager. */
258 ir_graph_pass_manager_t *new_graph_pass_mgr(
259 const char *name, int verify_all, int dump_all)
261 ir_graph_pass_manager_t *res = XMALLOCZ(ir_graph_pass_manager_t);
263 INIT_LIST_HEAD(&res->passes);
264 res->kind = k_ir_graph_pass_mgr;
267 res->verify_all = verify_all != 0;
268 res->dump_all = dump_all != 0;
273 /* Creates a new ir_prog pass manager. */
274 ir_prog_pass_manager_t *new_prog_pass_mgr(
275 const char *name, int verify_all, int dump_all)
277 ir_prog_pass_manager_t *res = XMALLOCZ(ir_prog_pass_manager_t);
279 INIT_LIST_HEAD(&res->passes);
280 res->kind = k_ir_prog_pass_mgr;
283 res->verify_all = verify_all != 0;
284 res->dump_all = dump_all != 0;
289 /* Terminate an ir_graph pass manager and all owned passes. */
290 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
292 ir_graph_pass_t *pass, *next;
294 list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
295 if (pass->rem_from_mgr)
296 pass->rem_from_mgr(pass->context);
304 /* Terminate an ir_prog pass manager and all owned passes. */
305 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
307 ir_prog_pass_t *pass, *next;
309 list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
310 if (pass->rem_from_mgr)
311 pass->rem_from_mgr(pass->context);
320 * Set the run index for an irgraph pass manager.
322 * @param mgr the manager
323 * @param run_idx the index for the first pass of this manager
325 void ir_graph_pass_mgr_set_run_idx(
326 ir_graph_pass_manager_t *mgr, unsigned run_idx)
328 mgr->run_idx = run_idx;
332 * Set the run index for an irprog pass manager.
334 * @param mgr the manager
335 * @param run_idx the index for the first pass of this manager
337 void ir_prog_pass_mgr_set_run_idx(
338 ir_prog_pass_manager_t *mgr, unsigned run_idx)
340 mgr->run_idx = run_idx;
344 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
346 static int void_graph_wrapper(ir_graph *irg, void *context)
348 void (*function)(ir_graph *irg) = context;
351 } /* void_graph_wrapper */
353 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
354 ir_graph_pass_t *def_graph_pass(
355 const char *name, void (*function)(ir_graph *irg))
357 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
359 pass->kind = k_ir_graph_pass;
360 pass->run_on_irg = void_graph_wrapper;
361 pass->context = function;
364 INIT_LIST_HEAD(&pass->list);
367 } /* def_graph_pass */
370 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
372 static int int_graph_wrapper(ir_graph *irg, void *context)
374 int (*function)(ir_graph *irg) = context;
375 return function(irg);
376 } /* int_graph_wrapper */
378 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
379 ir_graph_pass_t *def_graph_pass_ret(
380 const char *name, int (*function)(ir_graph *irg))
382 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
384 pass->kind = k_ir_graph_pass;
385 pass->run_on_irg = int_graph_wrapper;
386 pass->context = function;
389 INIT_LIST_HEAD(&pass->list);
392 } /* def_graph_pass_ret */
394 /* constructor for a default graph pass */
395 ir_graph_pass_t *def_graph_pass_constructor(
396 ir_graph_pass_t *pass,
397 const char *name, int (*function)(ir_graph *irg, void *context)) {
399 pass = XMALLOCZ(ir_graph_pass_t);
401 memset(pass, 0, sizeof(ir_graph_pass_t));
402 pass->kind = k_ir_graph_pass;
403 pass->run_on_irg = function;
404 pass->context = pass;
407 INIT_LIST_HEAD(&pass->list);
410 } /* def_graph_pass_constructor */
412 /* set the run parallel property */
413 void ir_graph_pass_set_parallel(ir_graph_pass_t *pass, int flag)
415 pass->run_parallel = flag != 0;
416 } /* ir_graph_pass_set_parallel */
419 * Wrapper for running void function(void) as an ir_prog pass.
421 static int void_prog_wrapper(ir_prog *irp, void *context)
423 void (*function)(void) = context;
428 } /* void_graph_wrapper */
430 /* Creates an ir_prog pass for running void function(void). */
431 ir_prog_pass_t *def_prog_pass(
433 void (*function)(void))
435 struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
437 pass->kind = k_ir_prog_pass;
438 pass->run_on_irprog = void_prog_wrapper;
439 pass->context = function;
442 INIT_LIST_HEAD(&pass->list);
445 } /* def_prog_pass */
447 /* Creates an ir_prog pass for running void function(void). */
448 ir_prog_pass_t *def_prog_pass_constructor(
449 ir_prog_pass_t *pass,
451 int (*function)(ir_prog *irp, void *context))
454 pass = XMALLOCZ(ir_prog_pass_t);
456 memset(pass, 0, sizeof(ir_prog_pass_t));
458 pass->kind = k_ir_prog_pass;
459 pass->run_on_irprog = function;
460 pass->context = pass;
463 INIT_LIST_HEAD(&pass->list);
466 } /* def_prog_pass_constructor */
471 void (*function)(void *context);
475 * Wrapper for the call_function pass.
477 static int call_function_wrapper(ir_prog *irp, void *context)
479 struct pass_t *pass = context;
482 pass->function(pass->context);
484 } /* call_function_wrapper */
486 ir_prog_pass_t *call_function_pass(
487 const char *name, void (*function)(void *context), void *context) {
488 struct pass_t *pass = XMALLOCZ(struct pass_t);
490 def_prog_pass_constructor(
491 &pass->pass, name ? name : "set_function", call_function_wrapper);
493 pass->pass.verify_irprog = ir_prog_no_verify;
494 pass->pass.dump_irprog = ir_prog_no_dump;
495 pass->pass.context = pass;
497 pass->function = function;
498 pass->context = context;
501 } /* call_function_pass */