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;
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 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
219 res &= irg_verify(get_irp_irg(i), 0);
223 /* Run all passes of an ir_prog pass manager. */
224 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
226 ir_prog_pass_t *pass;
229 /* run every pass on every graph */
230 unsigned idx = mgr->run_idx;
231 list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
232 int pass_res = pass->run_on_irprog(irp, pass->context);
235 /* verify is necessary */
236 if (mgr->verify_all) {
237 if (pass->verify_irprog) {
238 pass->verify_irprog(irp, pass->context);
245 if (pass->dump_irprog) {
246 pass->dump_irprog(irp, pass->context, idx);
249 create_suffix(buf, sizeof(buf), pass->name);
250 dump_all_ir_graphs(buf);
253 if (pass->is_wrapper) {
254 ir_graph_pass_manager_t *graph_mgr = (ir_graph_pass_manager_t*)pass->context;
255 idx += graph_mgr->n_passes;
262 /* Creates a new ir_graph pass manager. */
263 ir_graph_pass_manager_t *new_graph_pass_mgr(
264 const char *name, int verify_all, int dump_all)
266 ir_graph_pass_manager_t *res = XMALLOCZ(ir_graph_pass_manager_t);
268 INIT_LIST_HEAD(&res->passes);
269 res->kind = k_ir_graph_pass_mgr;
272 res->verify_all = verify_all != 0;
273 res->dump_all = dump_all != 0;
278 /* Creates a new ir_prog pass manager. */
279 ir_prog_pass_manager_t *new_prog_pass_mgr(
280 const char *name, int verify_all, int dump_all)
282 ir_prog_pass_manager_t *res = XMALLOCZ(ir_prog_pass_manager_t);
284 INIT_LIST_HEAD(&res->passes);
285 res->kind = k_ir_prog_pass_mgr;
288 res->verify_all = verify_all != 0;
289 res->dump_all = dump_all != 0;
294 /* Terminate an ir_graph pass manager and all owned passes. */
295 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
297 ir_graph_pass_t *pass, *next;
299 list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
300 if (pass->rem_from_mgr)
301 pass->rem_from_mgr(pass->context);
309 /* Terminate an ir_prog pass manager and all owned passes. */
310 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
312 ir_prog_pass_t *pass, *next;
314 list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
315 if (pass->rem_from_mgr)
316 pass->rem_from_mgr(pass->context);
325 * Set the run index for an irgraph pass manager.
327 * @param mgr the manager
328 * @param run_idx the index for the first pass of this manager
330 void ir_graph_pass_mgr_set_run_idx(
331 ir_graph_pass_manager_t *mgr, unsigned run_idx)
333 mgr->run_idx = run_idx;
337 * Set the run index for an irprog pass manager.
339 * @param mgr the manager
340 * @param run_idx the index for the first pass of this manager
342 void ir_prog_pass_mgr_set_run_idx(
343 ir_prog_pass_manager_t *mgr, unsigned run_idx)
345 mgr->run_idx = run_idx;
349 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
351 static int void_graph_wrapper(ir_graph *irg, void *context)
353 void_pass_func_irg function = (void_pass_func_irg)context;
356 } /* void_graph_wrapper */
358 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
359 ir_graph_pass_t *def_graph_pass(
360 const char *name, void (*function)(ir_graph *irg))
362 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
364 pass->kind = k_ir_graph_pass;
365 pass->run_on_irg = void_graph_wrapper;
366 pass->context = (void*)function;
369 INIT_LIST_HEAD(&pass->list);
372 } /* def_graph_pass */
375 * Wrapper for running int function(ir_graph *irg) as an ir_graph pass.
377 static int int_graph_wrapper(ir_graph *irg, void *context)
379 int_pass_func_irg function = (int_pass_func_irg)context;
380 return function(irg);
381 } /* int_graph_wrapper */
383 /* Creates an ir_graph pass for running void function(ir_graph *irg). */
384 ir_graph_pass_t *def_graph_pass_ret(
385 const char *name, int (*function)(ir_graph *irg))
387 struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);
389 pass->kind = k_ir_graph_pass;
390 pass->run_on_irg = int_graph_wrapper;
391 pass->context = (void*)function;
394 INIT_LIST_HEAD(&pass->list);
397 } /* def_graph_pass_ret */
399 /* constructor for a default graph pass */
400 ir_graph_pass_t *def_graph_pass_constructor(
401 ir_graph_pass_t *pass,
402 const char *name, int (*function)(ir_graph *irg, void *context)) {
404 pass = XMALLOCZ(ir_graph_pass_t);
406 memset(pass, 0, sizeof(ir_graph_pass_t));
407 pass->kind = k_ir_graph_pass;
408 pass->run_on_irg = function;
409 pass->context = pass;
412 INIT_LIST_HEAD(&pass->list);
415 } /* def_graph_pass_constructor */
417 /* set the run parallel property */
418 void ir_graph_pass_set_parallel(ir_graph_pass_t *pass, int flag)
420 pass->run_parallel = flag != 0;
421 } /* ir_graph_pass_set_parallel */
424 * Wrapper for running void function(void) as an ir_prog pass.
426 static int void_prog_wrapper(ir_prog *irp, void *context)
428 void_pass_func function = (void_pass_func)context;
433 } /* void_graph_wrapper */
435 /* Creates an ir_prog pass for running void function(void). */
436 ir_prog_pass_t *def_prog_pass(
438 void (*function)(void))
440 struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
442 pass->kind = k_ir_prog_pass;
443 pass->run_on_irprog = void_prog_wrapper;
444 pass->context = (void*)function;
447 INIT_LIST_HEAD(&pass->list);
450 } /* def_prog_pass */
452 /* Creates an ir_prog pass for running void function(void). */
453 ir_prog_pass_t *def_prog_pass_constructor(
454 ir_prog_pass_t *pass,
456 int (*function)(ir_prog *irp, void *context))
459 pass = XMALLOCZ(ir_prog_pass_t);
461 memset(pass, 0, sizeof(ir_prog_pass_t));
463 pass->kind = k_ir_prog_pass;
464 pass->run_on_irprog = function;
465 pass->context = pass;
468 INIT_LIST_HEAD(&pass->list);
471 } /* def_prog_pass_constructor */
473 typedef struct pass_t {
476 void (*function)(void *context);
480 * Wrapper for the call_function pass.
482 static int call_function_wrapper(ir_prog *irp, void *context)
484 pass_t *pass = (pass_t*)context;
487 pass->function(pass->context);
489 } /* call_function_wrapper */
491 ir_prog_pass_t *call_function_pass(
492 const char *name, void (*function)(void *context), void *context) {
493 struct pass_t *pass = XMALLOCZ(struct pass_t);
495 def_prog_pass_constructor(
496 &pass->pass, name ? name : "set_function", call_function_wrapper);
498 pass->pass.verify_irprog = ir_prog_no_verify;
499 pass->pass.dump_irprog = ir_prog_no_dump;
500 pass->pass.context = pass;
502 pass->function = function;
503 pass->context = context;
506 } /* call_function_pass */