irpass.c 11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Copyright (C) 1995-2009 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief     Manager for optimization passes.
 * @author    Michael Beck
Michael Beck's avatar
Michael Beck committed
24
 * @version   $Id$
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 */
#include "config.h"

#include <string.h>
#include "adt/list.h"
#include "irpass_t.h"
#include "irgraph_t.h"
#include "irprog_t.h"
#include "irdump.h"
#include "irvrfy.h"
#include "xmalloc.h"

/*Add a graph pass to a graph pass manager. */
void ir_graph_pass_mgr_add(ir_graph_pass_manager_t *mgr, ir_graph_pass_t *pass)
{
	list_add(&pass->list, &mgr->passes);
	++mgr->n_passes;
	if (pass->add_to_mgr)
		pass->add_to_mgr(pass->context);
}

/* Add an irprog pass to an irprog pass manager. */
void ir_prog_pass_mgr_add(ir_prog_pass_manager_t *mgr, ir_prog_pass_t *pass)
{
	list_add(&pass->list, &mgr->passes);
	++mgr->n_passes;
	if (pass->add_to_mgr)
		pass->add_to_mgr(pass->context);
}

/**
 * wrapper for running a graph pass manager as a pass on an irprog
 * pass manager.
 */
static int run_wrapper(ir_prog *prog, void *ctx)
{
	ir_graph_pass_manager_t *mgr = ctx;

	(void)prog;
	return ir_graph_pass_mgr_run(mgr);
}

/**
 * Ensure that no verifier is run from the wrapper.
 */
static int no_verify(ir_prog *prog, void *ctx)
{
	(void)prog;
	(void)ctx;
	return 0;
}

/**
 * Ensure that no dumper is run from the wrapper.
 */
static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
{
	(void)prog;
	(void)ctx;
	(void)idx;
}

87
88
89
90
/**
 * Term warpper for a wrapped ir_graph pass manager.
 */
static void term_wrapper(void *context)
91
{
92
93
94
95
96
97
98
99
100
101
	ir_graph_pass_manager_t *mgr = context;
	term_graph_pass_mgr(mgr);
}

/**
 * Create a wrapper ir_prog pass for an ir_graph manager.
 */
static ir_prog_pass_t *create_wrapper_pass(ir_graph_pass_manager_t *graph_mgr)
{
	/* create a wrapper pass */
102
103
104
105
106
107
108
109
110
	ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);

	pass->run_on_irprog = run_wrapper;
	pass->context       = graph_mgr;
	pass->name          = graph_mgr->name;

	/* do not verify nor dump: this is handled by the graph manager */
	pass->verify_irprog = no_verify;
	pass->dump_irprog   = no_dump;
111
	pass->is_wrapper    = 1;
112
113

	pass->add_to_mgr   = NULL;
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	pass->rem_from_mgr = term_wrapper;

	return pass;
}

/* Add an ir_graph_pass as a pass to an ir_prog pass manager. */
void ir_prog_pass_mgr_add_graph_pass(
	ir_prog_pass_manager_t *mgr, ir_graph_pass_t *pass)
{
	ir_graph_pass_manager_t *graph_mgr;
	ir_prog_pass_t          *wrapper;

	/* check if the last pass is a graph_pass wrapper */
	if (! list_empty(&mgr->passes)) {
		wrapper = list_entry(mgr->passes.prev, ir_prog_pass_t, list);
		if (wrapper->is_wrapper) {
			graph_mgr = wrapper->context;

			ir_graph_pass_mgr_add(graph_mgr, pass);
			return;
		}
	}

	/* not found, create a new wrapper */
	graph_mgr = new_graph_pass_mgr("wrapper", mgr->verify_all, mgr->dump_all);
	ir_graph_pass_mgr_add(graph_mgr, pass);

	wrapper = create_wrapper_pass(graph_mgr);
	ir_prog_pass_mgr_add(mgr, wrapper);
}

/* Add an ir_graph_pass_manager as a pass to an ir_prog pass manager. */
void ir_prog_pass_mgr_add_graph_mgr(
	ir_prog_pass_manager_t *mgr, ir_graph_pass_manager_t *graph_mgr)
{
	ir_prog_pass_t *pass = create_wrapper_pass(graph_mgr);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

	if (mgr->dump_all)
		graph_mgr->dump_all = 1;
	if (mgr->verify_all)
		graph_mgr->verify_all = 1;
	graph_mgr->run_idx = mgr->n_passes;

	ir_prog_pass_mgr_add(mgr, pass);
}

/**
 * Create a suffix for dumping.
 */
void create_suffix(char *suffix, size_t n, const char *pass_name, unsigned index)
{
	snprintf(suffix, n, "-%02u_%s", index, pass_name);
}

/* Run all passes of an ir_graph pass manager. */
int ir_graph_pass_mgr_run(ir_graph_pass_manager_t *mgr)
{
	ir_graph_pass_t *pass;
	int             i, res = 0;
	ir_graph        *rem = current_ir_graph;

	/* on all graphs: beware: number of irgs might be changed */
	for (i = 0; i < get_irp_n_irgs(); ++i) {
		ir_graph *irg = current_ir_graph = get_irp_irg(i);
		unsigned idx = mgr->run_idx;
		/* run every pass on every graph */
		list_for_each_entry(ir_graph_pass_t, pass, &mgr->passes, list) {
			int pass_res = pass->run_on_irg(irg, pass->context);
			if (pass_res != 0)
				res = 1;
			/* verify is necessary */
185
			if (mgr->verify_all) {
186
187
188
189
190
191
192
				if (pass->verify_irg) {
					pass->verify_irg(irg, pass->context);
				} else {
					irg_verify(irg, 0);
				}
			}
			/* dump */
193
			if (mgr->dump_all) {
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
				if (pass->dump_irg) {
					pass->dump_irg(irg, pass->context, idx);
				} else {
					char suffix[1024];
					create_suffix(suffix, sizeof(suffix), pass->name, idx);
					dump_ir_block_graph(irg, suffix);
				}
			}
			++idx;
		}
	}
	current_ir_graph = rem;
	return res;
}

/**
 * Verify all graphs on the given ir_prog.
 */
static int irp_verify_irgs(ir_prog *irp, int flags) {
	int i, res = 1;

	for (i = get_irp_n_irgs() - 1; i >= 0; --i)
		res &= irg_verify(get_irp_irg(i), flags);
	return res;
}

220
/* Run all passes of an ir_prog pass manager. */
221
222
223
224
225
226
227
228
229
230
231
232
int ir_prog_pass_mgr_run(ir_prog_pass_manager_t *mgr)
{
	ir_prog_pass_t *pass;
	int            res = 0;

	/* run every pass on every graph */
	unsigned idx = mgr->run_idx;
	list_for_each_entry(ir_prog_pass_t, pass, &mgr->passes, list) {
		int pass_res = pass->run_on_irprog(irp, pass->context);
		if (pass_res != 0)
			res = 1;
		/* verify is necessary */
233
		if (mgr->verify_all) {
234
235
236
237
238
239
240
			if (pass->verify_irprog) {
				pass->verify_irprog(irp, pass->context);
			} else {
				irp_verify_irgs(irp, 0);
			}
		}
		/* dump */
241
		if (mgr->dump_all) {
242
243
244
245
246
247
248
249
			if (pass->dump_irprog) {
				pass->dump_irprog(irp, pass->context, idx);
			} else {
				char suffix[1024];
				create_suffix(suffix, sizeof(suffix), pass->name, idx);
				dump_all_ir_graphs(dump_ir_block_graph, suffix);
			}
		}
250
251
252
253
254
		if (pass->is_wrapper) {
			ir_graph_pass_manager_t *graph_mgr = pass->context;
			idx += graph_mgr->n_passes;
		} else
			++idx;
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
	}
	return res;
}

/* Creates a new ir_graph pass manager. */
ir_graph_pass_manager_t *new_graph_pass_mgr(
	const char *name, int verify_all, int dump_all)
{
	ir_graph_pass_manager_t *res = xmalloc(sizeof(res));

	INIT_LIST_HEAD(&res->passes);
	res->name       = name;
	res->run_idx    = 0;
	res->verify_all = verify_all != 0;
	res->dump_all   = dump_all   != 0;

	return res;
}

/* Creates a new ir_prog pass manager. */
ir_prog_pass_manager_t *new_prog_pass_mgr(
	const char *name, int verify_all, int dump_all)
{
	ir_prog_pass_manager_t *res = xmalloc(sizeof(res));

	INIT_LIST_HEAD(&res->passes);
	res->name       = name;
	res->run_idx    = 0;
	res->verify_all = verify_all != 0;
	res->dump_all   = dump_all   != 0;

	return res;
}

289
/* Terminate an ir_graph pass manager and all owned passes. */
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
void term_graph_pass_mgr(ir_graph_pass_manager_t *mgr)
{
	ir_graph_pass_t *pass, *next;

	list_for_each_entry_safe(ir_graph_pass_t, pass, next, &mgr->passes, list) {
		if (pass->rem_from_mgr)
			pass->rem_from_mgr(pass->context);
		pass->kind = k_BAD;
		xfree(pass);
	}
	mgr->kind = k_BAD;
	xfree(mgr);
}

/* Terminate an ir_prog pass manager and all owned passes. */
void term_prog_pass_mgr(ir_prog_pass_manager_t *mgr)
{
	ir_prog_pass_t *pass, *next;

	list_for_each_entry_safe(ir_prog_pass_t, pass, next, &mgr->passes, list) {
		if (pass->rem_from_mgr)
			pass->rem_from_mgr(pass->context);
		pass->kind = k_BAD;
		xfree(pass);
	}
	mgr->kind = k_BAD;
	xfree(mgr);
}

/**
 * Set the run index for an irgraph pass manager.
 *
 * @param mgr      the manager
 * @param run_idx  the index for the first pass of this manager
 */
void ir_graph_pass_manager_set_run_idx(
	ir_graph_pass_manager_t *mgr, unsigned run_idx)
{
	mgr->run_idx = run_idx;
}

/**
 * Set the run index for an irprog pass manager.
 *
 * @param mgr      the manager
 * @param run_idx  the index for the first pass of this manager
 */
void ir_prog_pass_manager_set_run_idx(
	ir_prog_pass_manager_t *mgr, unsigned run_idx)
{
	mgr->run_idx = run_idx;
}
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

/**
 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
 */
static int void_graph_wrapper(ir_graph *irg, void *context) {
	void (*function)(ir_graph *irg) = context;
	function(irg);
	return 0;
}  /* void_graph_wrapper */

/* Creates an ir_graph pass for running void function(ir_graph *irg). */
ir_graph_pass_t *def_graph_pass(
	const char *name, void (*function)(ir_graph *irg))
{
	struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);

	pass->kind       = k_ir_graph_pass;
	pass->run_on_irg = void_graph_wrapper;
	pass->context    = function;
	pass->name       = name;

	INIT_LIST_HEAD(&pass->list);

	return pass;
}  /* def_graph_pass */

/**
 * Wrapper for running void function(ir_graph *irg) as an ir_graph pass.
 */
static int int_graph_wrapper(ir_graph *irg, void *context) {
	int (*function)(ir_graph *irg) = context;
	return function(irg);
}  /* int_graph_wrapper */

/* Creates an ir_graph pass for running void function(ir_graph *irg). */
ir_graph_pass_t *def_graph_pass_ret(
		const char *name, int (*function)(ir_graph *irg))
{
	struct ir_graph_pass_t *pass = XMALLOCZ(ir_graph_pass_t);

	pass->kind       = k_ir_graph_pass;
	pass->run_on_irg = int_graph_wrapper;
	pass->context    = function;
	pass->name       = name;

	INIT_LIST_HEAD(&pass->list);

	return pass;
}  /* def_graph_pass_ret */

/* constructor for a default graph pass */
ir_graph_pass_t *def_graph_pass_constructor(
	ir_graph_pass_t *pass,
	const char *name, int (*function)(ir_graph *irg, void *context)) {
	if (pass == NULL)
		pass = XMALLOCZ(ir_graph_pass_t);
	pass->kind       = k_ir_graph_pass;
	pass->run_on_irg = function;
	pass->context    = pass;
	pass->name       = name;

	INIT_LIST_HEAD(&pass->list);

	return pass;
}  /* def_graph_pass_constructor */


/**
 * Wrapper for running void function(void) as an ir_prog pass.
 */
static int void_prog_wrapper(ir_prog *irp, void *context) {
	void (*function)(void) = context;

	(void)irp;
	function();
	return 0;
}  /* void_graph_wrapper */

/* Creates an ir_prog pass for running void function(void). */
ir_prog_pass_t *def_prog_pass(
	const char *name,
	void (*function)(void))
{
	struct ir_prog_pass_t *pass = XMALLOCZ(ir_prog_pass_t);

	pass->kind          = k_ir_prog_pass;
	pass->run_on_irprog = void_prog_wrapper;
	pass->context       = function;
	pass->name          = name;

	INIT_LIST_HEAD(&pass->list);

	return pass;
}  /* def_prog_pass */
Michael Beck's avatar
Michael Beck committed
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454

/* Creates an ir_prog pass for running void function(void). */
ir_prog_pass_t *def_prog_pass_constructor(
	ir_prog_pass_t *pass,
	const char *name,
	void (*function)(ir_prog *irp, void *context))
{
	if (pass == NULL)
		pass = XMALLOCZ(ir_prog_pass_t);

	pass->kind          = k_ir_prog_pass;
	pass->run_on_irprog = function;
	pass->context       = pass;
	pass->name          = name;

	INIT_LIST_HEAD(&pass->list);

	return pass;
}  /* def_prog_pass_constructor */