ab655dc6bdc0dd7fade28b0b99b2b098ed1114c9
[libfirm] / ir / ir / irpass.c
1 /*
2  * Copyright (C) 1995-2009 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief     Manager for optimization passes.
23  * @author    Michael Beck
24  * @version   $Id: $
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include "adt/list.h"
30 #include "irpass_t.h"
31 #include "irgraph_t.h"
32 #include "irprog_t.h"
33 #include "irdump.h"
34 #include "irvrfy.h"
35 #include "xmalloc.h"
36
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)
39 {
40         list_add(&pass->list, &mgr->passes);
41         ++mgr->n_passes;
42         if (pass->add_to_mgr)
43                 pass->add_to_mgr(pass->context);
44 }
45
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)
48 {
49         list_add(&pass->list, &mgr->passes);
50         ++mgr->n_passes;
51         if (pass->add_to_mgr)
52                 pass->add_to_mgr(pass->context);
53 }
54
55 /**
56  * wrapper for running a graph pass manager as a pass on an irprog
57  * pass manager.
58  */
59 static int run_wrapper(ir_prog *prog, void *ctx)
60 {
61         ir_graph_pass_manager_t *mgr = ctx;
62
63         (void)prog;
64         return ir_graph_pass_mgr_run(mgr);
65 }
66
67 /**
68  * Ensure that no verifier is run from the wrapper.
69  */
70 static int no_verify(ir_prog *prog, void *ctx)
71 {
72         (void)prog;
73         (void)ctx;
74         return 0;
75 }
76
77 /**
78  * Ensure that no dumper is run from the wrapper.
79  */
80 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
81 {
82         (void)prog;
83         (void)ctx;
84         (void)idx;
85 }
86
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)
90 {
91         ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);
92
93         pass->run_on_irprog = run_wrapper;
94         pass->context       = graph_mgr;
95         pass->name          = graph_mgr->name;
96
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;
100         pass->dump          = 0;
101         pass->verify        = 0;
102
103         pass->add_to_mgr   = NULL;
104         pass->rem_from_mgr = NULL;
105
106         if (mgr->dump_all)
107                 graph_mgr->dump_all = 1;
108         if (mgr->verify_all)
109                 graph_mgr->verify_all = 1;
110         graph_mgr->run_idx = mgr->n_passes;
111
112         ir_prog_pass_mgr_add(mgr, pass);
113 }
114
115 /**
116  * Create a suffix for dumping.
117  */
118 void create_suffix(char *suffix, size_t n, const char *pass_name, unsigned index)
119 {
120         snprintf(suffix, n, "-%02u_%s", index, pass_name);
121 }
122
123 /* Run all passes of an ir_graph pass manager. */
124 int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
125 {
126         ir_graph_pass_t *pass;
127         int             i, res = 0;
128         ir_graph        *rem = current_ir_graph;
129
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);
137                         if (pass_res != 0)
138                                 res = 1;
139                         /* verify is necessary */
140                         if (mgr->verify_all || pass->verify) {
141                                 if (pass->verify_irg) {
142                                         pass->verify_irg(irg, pass->context);
143                                 } else {
144                                         irg_verify(irg, 0);
145                                 }
146                         }
147                         /* dump */
148                         if (mgr->dump_all || pass->dump) {
149                                 if (pass->dump_irg) {
150                                         pass->dump_irg(irg, pass->context, idx);
151                                 } else {
152                                         char suffix[1024];
153                                         create_suffix(suffix, sizeof(suffix), pass->name, idx);
154                                         dump_ir_block_graph(irg, suffix);
155                                 }
156                         }
157                         ++idx;
158                 }
159         }
160         current_ir_graph = rem;
161         return res;
162 }
163
164 /**
165  * Verify all graphs on the given ir_prog.
166  */
167 static int irp_verify_irgs(ir_prog *irp, int flags) {
168         int i, res = 1;
169
170         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
171                 res &= irg_verify(get_irp_irg(i), flags);
172         return res;
173 }
174
175 /* Run all passes of an irprog pass manager. */
176 int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
177 {
178         ir_prog_pass_t *pass;
179         int            res = 0;
180
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);
185                 if (pass_res != 0)
186                         res = 1;
187                 /* verify is necessary */
188                 if (mgr->verify_all || pass->verify) {
189                         if (pass->verify_irprog) {
190                                 pass->verify_irprog(irp, pass->context);
191                         } else {
192                                 irp_verify_irgs(irp, 0);
193                         }
194                 }
195                 /* dump */
196                 if (mgr->dump_all || pass->dump) {
197                         if (pass->dump_irprog) {
198                                 pass->dump_irprog(irp, pass->context, idx);
199                         } else {
200                                 char suffix[1024];
201                                 create_suffix(suffix, sizeof(suffix), pass->name, idx);
202                                 dump_all_ir_graphs(dump_ir_block_graph, suffix);
203                         }
204                 }
205                 ++idx;
206         }
207         return res;
208 }
209
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)
213 {
214         ir_graph_pass_manager_t *res = xmalloc(sizeof(res));
215
216         INIT_LIST_HEAD(&res->passes);
217         res->name       = name;
218         res->run_idx    = 0;
219         res->verify_all = verify_all != 0;
220         res->dump_all   = dump_all   != 0;
221
222         return res;
223 }
224
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)
228 {
229         ir_prog_pass_manager_t *res = xmalloc(sizeof(res));
230
231         INIT_LIST_HEAD(&res->passes);
232         res->name       = name;
233         res->run_idx    = 0;
234         res->verify_all = verify_all != 0;
235         res->dump_all   = dump_all   != 0;
236
237         return res;
238 }
239
240 /* Terminate a graph pass manager and all owned passes. */
241 void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
242 {
243         ir_graph_pass_t *pass, *next;
244
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);
248                 pass->kind = k_BAD;
249                 xfree(pass);
250         }
251         mgr->kind = k_BAD;
252         xfree(mgr);
253 }
254
255 /* Terminate an ir_prog pass manager and all owned passes. */
256 void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
257 {
258         ir_prog_pass_t *pass, *next;
259
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);
263                 pass->kind = k_BAD;
264                 xfree(pass);
265         }
266         mgr->kind = k_BAD;
267         xfree(mgr);
268 }
269
270 /**
271  * Set the run index for an irgraph pass manager.
272  *
273  * @param mgr      the manager
274  * @param run_idx  the index for the first pass of this manager
275  */
276 void ir_graph_pass_manager_set_run_idx(
277         ir_graph_pass_manager_t *mgr, unsigned run_idx)
278 {
279         mgr->run_idx = run_idx;
280 }
281
282 /**
283  * Set the run index for an irprog pass manager.
284  *
285  * @param mgr      the manager
286  * @param run_idx  the index for the first pass of this manager
287  */
288 void ir_prog_pass_manager_set_run_idx(
289         ir_prog_pass_manager_t *mgr, unsigned run_idx)
290 {
291         mgr->run_idx = run_idx;
292 }