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(&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(&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);
68 * Ensure that no verifier is run from the wrapper.
70 static int no_verify(ir_prog *prog, void *ctx)
78 * Ensure that no dumper is run from the wrapper.
80 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
88 * Term warpper for a wrapped ir_graph pass manager.
90 static void term_wrapper(void *context)
92 ir_graph_pass_manager_t *mgr = 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->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 = no_verify;
110 pass->dump_irprog = no_dump;
113 pass->is_wrapper = 1;
115 pass->add_to_mgr = NULL;
116 pass->rem_from_mgr = term_wrapper;
121 /* Add an ir_graph_pass as a pass to an ir_prog pass manager. */
122 void ir_prog_pass_mgr_add_graph_pass(
123 ir_prog_pass_manager_t *mgr, ir_graph_pass_t *pass)
125 ir_graph_pass_manager_t *graph_mgr;
126 ir_prog_pass_t *wrapper;
128 /* check if the last pass is a graph_pass wrapper */
129 if (! list_empty(&mgr->passes)) {
130 wrapper = list_entry(mgr->passes.prev, ir_prog_pass_t, list);
131 if (wrapper->is_wrapper) {
132 graph_mgr = wrapper->context;
134 ir_graph_pass_mgr_add(graph_mgr, pass);
139 /* not found, create a new wrapper */
140 graph_mgr = new_graph_pass_mgr("wrapper", mgr->verify_all, mgr->dump_all);
141 ir_graph_pass_mgr_add(graph_mgr, pass);
143 wrapper = create_wrapper_pass(graph_mgr);
144 ir_prog_pass_mgr_add(mgr, wrapper);
147 /* Add an ir_graph_pass_manager as a pass to an ir_prog pass manager. */
148 void ir_prog_pass_mgr_add_graph_mgr(
149 ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
151 ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
154 graph_mgr->dump_all = 1;
156 graph_mgr->verify_all = 1;
157 graph_mgr->run_idx = mgr->n_passes;
159 ir_prog_pass_mgr_add(mgr, pass);
163 * Create a suffix for dumping.
165 void create_suffix(char *suffix, size_t n, const char *pass_name, unsigned index)
167 snprintf(suffix, n, "-%02u_%s", index, 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 || pass->verify) {
188 if (pass->verify_irg) {
189 pass->verify_irg(irg, pass->context);
195 if (mgr->dump_all || pass->dump) {
196 if (pass->dump_irg) {
197 pass->dump_irg(irg, pass->context, idx);
200 create_suffix(suffix, sizeof(suffix), pass->name, idx);
201 dump_ir_block_graph(irg, suffix);
207 current_ir_graph = rem;
212 * Verify all graphs on the given ir_prog.
214 static int irp_verify_irgs(ir_prog *irp, int flags) {
217 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
218 res &= irg_verify(get_irp_irg(i), flags);
222 /* Run all passes of an ir_prog pass manager. */
223 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
225 ir_prog_pass_t *pass;
228 /* run every pass on every graph */
229 unsigned idx = mgr->run_idx;
230 list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
231 int pass_res = pass->run_on_irprog(irp, pass->context);
234 /* verify is necessary */
235 if (mgr->verify_all || pass->verify) {
236 if (pass->verify_irprog) {
237 pass->verify_irprog(irp, pass->context);
239 irp_verify_irgs(irp, 0);
243 if (mgr->dump_all || pass->dump) {
244 if (pass->dump_irprog) {
245 pass->dump_irprog(irp, pass->context, idx);
248 create_suffix(suffix, sizeof(suffix), pass->name, idx);
249 dump_all_ir_graphs(dump_ir_block_graph, suffix);
252 if (pass->is_wrapper) {
253 ir_graph_pass_manager_t *graph_mgr = pass->context;
254 idx += graph_mgr->n_passes;
261 /* Creates a new ir_graph pass manager. */
262 ir_graph_pass_manager_t *new_graph_pass_mgr(
263 const char *name, int verify_all, int dump_all)
265 ir_graph_pass_manager_t *res = xmalloc(sizeof(res));
267 INIT_LIST_HEAD(&res->passes);
270 res->verify_all = verify_all != 0;
271 res->dump_all = dump_all != 0;
276 /* Creates a new ir_prog pass manager. */
277 ir_prog_pass_manager_t *new_prog_pass_mgr(
278 const char *name, int verify_all, int dump_all)
280 ir_prog_pass_manager_t *res = xmalloc(sizeof(res));
282 INIT_LIST_HEAD(&res->passes);
285 res->verify_all = verify_all != 0;
286 res->dump_all = dump_all != 0;
291 /* Terminate an ir_graph pass manager and all owned passes. */
292 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
294 ir_graph_pass_t *pass, *next;
296 list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
297 if (pass->rem_from_mgr)
298 pass->rem_from_mgr(pass->context);
306 /* Terminate an ir_prog pass manager and all owned passes. */
307 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
309 ir_prog_pass_t *pass, *next;
311 list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
312 if (pass->rem_from_mgr)
313 pass->rem_from_mgr(pass->context);
322 * Set the run index for an irgraph pass manager.
324 * @param mgr the manager
325 * @param run_idx the index for the first pass of this manager
327 void ir_graph_pass_manager_set_run_idx(
328 ir_graph_pass_manager_t *mgr, unsigned run_idx)
330 mgr->run_idx = run_idx;
334 * Set the run index for an irprog pass manager.
336 * @param mgr the manager
337 * @param run_idx the index for the first pass of this manager
339 void ir_prog_pass_manager_set_run_idx(
340 ir_prog_pass_manager_t *mgr, unsigned run_idx)
342 mgr->run_idx = run_idx;