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)
87 /* Add an ir_graph_pass_manager as a pass to an irprog pass manager. */
88 void ir_prog_pass_mgr_add_graph_mgr(
89 ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
91 ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
93 pass->run_on_irprog = run_wrapper;
94 pass->context = graph_mgr;
95 pass->name = graph_mgr->name;
97 /* do not verify nor dump: this is handled by the graph manager */
98 pass->verify_irprog = no_verify;
99 pass->dump_irprog = no_dump;
103 pass->add_to_mgr = NULL;
104 pass->rem_from_mgr = NULL;
107 graph_mgr->dump_all = 1;
109 graph_mgr->verify_all = 1;
110 graph_mgr->run_idx = mgr->n_passes;
112 ir_prog_pass_mgr_add(mgr, pass);
116 * Create a suffix for dumping.
118 void create_suffix(char *suffix, size_t n, const char *pass_name, unsigned index)
120 snprintf(suffix, n, "-%02u_%s", index, pass_name);
123 /* Run all passes of an ir_graph pass manager. */
124 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
126 ir_graph_pass_t *pass;
128 ir_graph *rem = current_ir_graph;
130 /* on all graphs: beware: number of irgs might be changed */
131 for (i = 0; i < get_irp_n_irgs(); ++i) {
132 ir_graph *irg = current_ir_graph = get_irp_irg(i);
133 unsigned idx = mgr->run_idx;
134 /* run every pass on every graph */
135 list_for_each_entry(ir_graph_pass_t, pass, &mgr->passes, list) {
136 int pass_res = pass->run_on_irg(irg, pass->context);
139 /* verify is necessary */
140 if (mgr->verify_all || pass->verify) {
141 if (pass->verify_irg) {
142 pass->verify_irg(irg, pass->context);
148 if (mgr->dump_all || pass->dump) {
149 if (pass->dump_irg) {
150 pass->dump_irg(irg, pass->context, idx);
153 create_suffix(suffix, sizeof(suffix), pass->name, idx);
154 dump_ir_block_graph(irg, suffix);
160 current_ir_graph = rem;
165 * Verify all graphs on the given ir_prog.
167 static int irp_verify_irgs(ir_prog *irp, int flags) {
170 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
171 res &= irg_verify(get_irp_irg(i), flags);
175 /* Run all passes of an irprog pass manager. */
176 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
178 ir_prog_pass_t *pass;
181 /* run every pass on every graph */
182 unsigned idx = mgr->run_idx;
183 list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
184 int pass_res = pass->run_on_irprog(irp, pass->context);
187 /* verify is necessary */
188 if (mgr->verify_all || pass->verify) {
189 if (pass->verify_irprog) {
190 pass->verify_irprog(irp, pass->context);
192 irp_verify_irgs(irp, 0);
196 if (mgr->dump_all || pass->dump) {
197 if (pass->dump_irprog) {
198 pass->dump_irprog(irp, pass->context, idx);
201 create_suffix(suffix, sizeof(suffix), pass->name, idx);
202 dump_all_ir_graphs(dump_ir_block_graph, suffix);
210 /* Creates a new ir_graph pass manager. */
211 ir_graph_pass_manager_t *new_graph_pass_mgr(
212 const char *name, int verify_all, int dump_all)
214 ir_graph_pass_manager_t *res = xmalloc(sizeof(res));
216 INIT_LIST_HEAD(&res->passes);
219 res->verify_all = verify_all != 0;
220 res->dump_all = dump_all != 0;
225 /* Creates a new ir_prog pass manager. */
226 ir_prog_pass_manager_t *new_prog_pass_mgr(
227 const char *name, int verify_all, int dump_all)
229 ir_prog_pass_manager_t *res = xmalloc(sizeof(res));
231 INIT_LIST_HEAD(&res->passes);
234 res->verify_all = verify_all != 0;
235 res->dump_all = dump_all != 0;
240 /* Terminate a graph pass manager and all owned passes. */
241 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
243 ir_graph_pass_t *pass, *next;
245 list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
246 if (pass->rem_from_mgr)
247 pass->rem_from_mgr(pass->context);
255 /* Terminate an ir_prog pass manager and all owned passes. */
256 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
258 ir_prog_pass_t *pass, *next;
260 list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
261 if (pass->rem_from_mgr)
262 pass->rem_from_mgr(pass->context);
271 * Set the run index for an irgraph pass manager.
273 * @param mgr the manager
274 * @param run_idx the index for the first pass of this manager
276 void ir_graph_pass_manager_set_run_idx(
277 ir_graph_pass_manager_t *mgr, unsigned run_idx)
279 mgr->run_idx = run_idx;
283 * Set the run index for an irprog pass manager.
285 * @param mgr the manager
286 * @param run_idx the index for the first pass of this manager
288 void ir_prog_pass_manager_set_run_idx(
289 ir_prog_pass_manager_t *mgr, unsigned run_idx)
291 mgr->run_idx = run_idx;