bechordal_main.c 16 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */

Sebastian Hack's avatar
Sebastian Hack committed
20
/**
Christian Würdig's avatar
Christian Würdig committed
21
22
23
24
25
 * @file
 * @brief       Driver for the chordal register allocator.
 * @author      Sebastian Hack
 * @date        29.11.2005
 * @version     $Id$
Sebastian Hack's avatar
Sebastian Hack committed
26
 */
Christian Würdig's avatar
Christian Würdig committed
27
#ifdef HAVE_CONFIG_H
28
#include "config.h"
Christian Würdig's avatar
Christian Würdig committed
29
#endif
Sebastian Hack's avatar
Sebastian Hack committed
30

Christian Würdig's avatar
Christian Würdig committed
31
#include <stdlib.h>
32
33
#include <time.h>

Sebastian Hack's avatar
Sebastian Hack committed
34
35
36
37
38
#include "obst.h"
#include "pset.h"
#include "list.h"
#include "bitset.h"
#include "iterator.h"
39
#include "firm_config.h"
Sebastian Hack's avatar
Sebastian Hack committed
40

Matthias Braun's avatar
Matthias Braun committed
41
42
#include "lc_opts.h"
#include "lc_opts_enum.h"
43

Michael Beck's avatar
Michael Beck committed
44
#include "ircons_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
45
46
47
48
#include "irmode_t.h"
#include "irgraph_t.h"
#include "irprintf_t.h"
#include "irgwalk.h"
Sebastian Hack's avatar
Sebastian Hack committed
49
#include "ircons.h"
Sebastian Hack's avatar
Sebastian Hack committed
50
51
#include "irdump.h"
#include "irdom.h"
Christian Würdig's avatar
Christian Würdig committed
52
#include "ircons.h"
Sebastian Hack's avatar
Sebastian Hack committed
53
#include "irbitset.h"
54
55
#include "irnode.h"
#include "ircons.h"
Sebastian Hack's avatar
Sebastian Hack committed
56
57
#include "debug.h"
#include "xmalloc.h"
Sebastian Hack's avatar
Sebastian Hack committed
58
#include "execfreq.h"
Christian Würdig's avatar
Christian Würdig committed
59
#include "iredges_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
60
61

#include "bechordal_t.h"
62
#include "beabi.h"
Sebastian Hack's avatar
Sebastian Hack committed
63
#include "bejavacoal.h"
Sebastian Hack's avatar
Sebastian Hack committed
64
65
66
67
#include "beutil.h"
#include "besched.h"
#include "besched_t.h"
#include "belive_t.h"
68
#include "bearch_t.h"
69
#include "beifg_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
70
#include "beifg_impl.h"
Christian Würdig's avatar
Christian Würdig committed
71
#include "benode_t.h"
72
#include "bestatevent.h"
73
#include "bestat.h"
74
#include "bemodule.h"
75
#include "be_t.h"
Christian Würdig's avatar
Christian Würdig committed
76
77
#include "bera.h"
#include "beirg_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
78

Matthias Braun's avatar
Matthias Braun committed
79
#include "bespillslots.h"
80
#include "bespilloptions.h"
81
82
#include "belower.h"

Christian Würdig's avatar
Christian Würdig committed
83
#ifdef WITH_ILP
84
#include "bespillremat.h"
Christian Würdig's avatar
Christian Würdig committed
85
86
#endif /* WITH_ILP */

87
#include "bejavacoal.h"
Daniel Grund's avatar
Daniel Grund committed
88
#include "becopystat.h"
Daniel Grund's avatar
Daniel Grund committed
89
#include "becopyopt.h"
Christian Würdig's avatar
Christian Würdig committed
90
#include "bessadestr.h"
91
#include "beverify.h"
92
#include "benode_t.h"
Christian Würdig's avatar
Christian Würdig committed
93

Sebastian Hack's avatar
Sebastian Hack committed
94
static be_ra_chordal_opts_t options = {
Sebastian Hack's avatar
Sebastian Hack committed
95
	BE_CH_DUMP_NONE,
Christian Würdig's avatar
Christian Würdig committed
96
	BE_CH_LOWER_PERM_SWAP,
Christian Würdig's avatar
Christian Würdig committed
97
	BE_CH_VRFY_WARN,
98
99
	"",
	""
Sebastian Hack's avatar
Sebastian Hack committed
100
101
};

102
typedef struct _post_spill_env_t {
103
104
105
106
	be_chordal_env_t            cenv;
	be_irg_t                    *birg;
	const arch_register_class_t *cls;
	double                      pre_spill_cost;
107
108
} post_spill_env_t;

109
static be_options_t  *main_opts;
110

111
112
static const lc_opt_enum_int_items_t lower_perm_items[] = {
	{ "copy", BE_CH_LOWER_PERM_COPY },
113
114
115
116
117
	{ "swap", BE_CH_LOWER_PERM_SWAP },
	{ NULL, 0 }
};

static const lc_opt_enum_int_items_t lower_perm_stat_items[] = {
118
119
120
	{ NULL, 0 }
};

Sebastian Hack's avatar
Sebastian Hack committed
121
static const lc_opt_enum_int_items_t dump_items[] = {
122
	{ "none",       BE_CH_DUMP_NONE       },
Matthias Braun's avatar
Matthias Braun committed
123
124
125
126
127
128
129
130
131
132
133
	{ "spill",      BE_CH_DUMP_SPILL      },
	{ "live",       BE_CH_DUMP_LIVE       },
	{ "color",      BE_CH_DUMP_COLOR      },
	{ "copymin",    BE_CH_DUMP_COPYMIN    },
	{ "ssadestr",   BE_CH_DUMP_SSADESTR   },
	{ "tree",       BE_CH_DUMP_TREE_INTV  },
	{ "constr",     BE_CH_DUMP_CONSTR     },
	{ "lower",      BE_CH_DUMP_LOWER      },
	{ "spillslots", BE_CH_DUMP_SPILLSLOTS },
	{ "appel",      BE_CH_DUMP_APPEL      },
	{ "all",        BE_CH_DUMP_ALL        },
Christian Würdig's avatar
Christian Würdig committed
134
135
136
137
138
139
140
	{ NULL, 0 }
};

static const lc_opt_enum_int_items_t be_ch_vrfy_items[] = {
	{ "off",    BE_CH_VRFY_OFF    },
	{ "warn",   BE_CH_VRFY_WARN   },
	{ "assert", BE_CH_VRFY_ASSERT },
Sebastian Hack's avatar
Sebastian Hack committed
141
142
143
	{ NULL, 0 }
};

144
static lc_opt_enum_int_var_t lower_perm_var = {
145
	&options.lower_perm_opt, lower_perm_items
146
147
};

Sebastian Hack's avatar
Sebastian Hack committed
148
149
150
151
static lc_opt_enum_int_var_t dump_var = {
	&options.dump_flags, dump_items
};

Christian Würdig's avatar
Christian Würdig committed
152
153
154
155
static lc_opt_enum_int_var_t be_ch_vrfy_var = {
	&options.vrfy_option, be_ch_vrfy_items
};

156
static const lc_opt_table_entry_t be_chordal_options[] = {
157
	LC_OPT_ENT_ENUM_PTR ("perm",          "perm lowering options", &lower_perm_var),
Sebastian Hack's avatar
Sebastian Hack committed
158
	LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
159
	LC_OPT_ENT_ENUM_PTR ("vrfy",          "verify options", &be_ch_vrfy_var),
160
	LC_OPT_LAST
161
162
};

Sebastian Hack's avatar
Sebastian Hack committed
163
static void dump(unsigned mask, ir_graph *irg,
164
165
166
				 const arch_register_class_t *cls,
				 const char *suffix,
				 void (*dump_func)(ir_graph *, const char *))
Sebastian Hack's avatar
Sebastian Hack committed
167
{
Christian Würdig's avatar
Christian Würdig committed
168
169
	if((options.dump_flags & mask) == mask) {
		if (cls) {
170
171
			char buf[256];
			snprintf(buf, sizeof(buf), "-%s%s", cls->name, suffix);
Christian Würdig's avatar
Christian Würdig committed
172
			be_dump(irg, buf, dump_func);
173
174
		}
		else
Christian Würdig's avatar
Christian Würdig committed
175
			be_dump(irg, suffix, dump_func);
176
	}
Sebastian Hack's avatar
Sebastian Hack committed
177
178
}

Christian Würdig's avatar
Christian Würdig committed
179
180
181
182
183
/**
 * Checks for every reload if it's user can perform the load on itself.
 */
static void memory_operand_walker(ir_node *irn, void *env) {
	be_chordal_env_t *cenv = env;
184
	const arch_env_t *aenv = cenv->birg->main_env->arch_env;
Christian Würdig's avatar
Christian Würdig committed
185
186
	const ir_edge_t  *edge, *ne;
	ir_node          *block;
187
	ir_node          *spill;
Christian Würdig's avatar
Christian Würdig committed
188
189
190
191

	if (! be_is_Reload(irn))
		return;

192
	/* only use memory operands, if the reload is only used by 1 node */
193
194
195
196
	if(get_irn_n_edges(irn) > 1)
		return;

	spill = be_get_Reload_mem(irn);
Christian Würdig's avatar
Christian Würdig committed
197
198
199
200
201
202
	block = get_nodes_block(irn);

	foreach_out_edge_safe(irn, edge, ne) {
		ir_node *src = get_edge_src_irn(edge);
		int     pos  = get_edge_src_pos(edge);

203
		assert(src && "outedges broken!");
Christian Würdig's avatar
Christian Würdig committed
204
205

		if (get_nodes_block(src) == block && arch_possible_memory_operand(aenv, src, pos)) {
206
			arch_perform_memory_operand(aenv, src, spill, pos);
Christian Würdig's avatar
Christian Würdig committed
207
208
209
210
211
212
		}
	}

	/* kill the Reload */
	if (get_irn_n_edges(irn) == 0) {
		sched_remove(irn);
213
		set_irn_n(irn, be_pos_Reload_mem, new_Bad());
214
		set_irn_n(irn, be_pos_Reload_frame, new_Bad());
Christian Würdig's avatar
Christian Würdig committed
215
216
217
218
219
220
221
222
223
224
	}
}

/**
 * Starts a walk for memory operands if supported by the backend.
 */
static INLINE void check_for_memory_operands(be_chordal_env_t *chordal_env) {
	irg_walk_graph(chordal_env->irg, NULL, memory_operand_walker, chordal_env);
}

225
226
227
228
229
230
/**
 * Sorry for doing stats again...
 */
typedef struct _node_stat_t {
	unsigned int n_phis;      /**< Phis of the current register class. */
	unsigned int n_mem_phis;  /**< Memory Phis (Phis with spill operands). */
Sebastian Hack's avatar
Sebastian Hack committed
231
232
	unsigned int n_copies;    /**< Copies */
	unsigned int n_perms;     /**< Perms */
233
	unsigned int n_spills;    /**< Spill nodes */
Sebastian Hack's avatar
Sebastian Hack committed
234
	unsigned int n_reloads;   /**< Reloads */
Matthias Braun's avatar
Matthias Braun committed
235
	unsigned int n_remats;    /**< Remats */
236
237
238
} node_stat_t;

struct node_stat_walker {
239
240
	node_stat_t      *stat;
	const arch_env_t *arch_env;
241
242
243
244
};

static void node_stat_walker(ir_node *irn, void *data)
{
245
246
	struct node_stat_walker *env  = data;
	const arch_env_t        *aenv = env->arch_env;
247

248
249
250
251
252
	/* if the node is a normal phi */
	if(is_Phi(irn)) {
		if (get_irn_mode(irn) == mode_M) {
			env->stat->n_mem_phis++;
		} else {
253
254
			env->stat->n_phis++;
		}
255
256
257
258
259
260
261
	} else {
		arch_irn_class_t classify = arch_irn_classify(aenv, irn);

		if(classify & arch_irn_class_spill)
			++env->stat->n_spills;
		if(classify & arch_irn_class_reload)
			++env->stat->n_reloads;
Matthias Braun's avatar
Matthias Braun committed
262
263
		if(classify & arch_irn_class_remat)
			++env->stat->n_remats;
264
265
266
267
		if(classify & arch_irn_class_copy)
			++env->stat->n_copies;
		if(classify & arch_irn_class_perm)
			++env->stat->n_perms;
268
269
270
	}
}

271
static void node_stats(be_irg_t *birg, node_stat_t *stat)
272
273
274
{
	struct node_stat_walker env;

275
	memset(stat, 0, sizeof(*stat));
276
	env.arch_env = birg->main_env->arch_env;
277
	env.stat     = stat;
278
	irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
279
280
281
282
}

static void insn_count_walker(ir_node *irn, void *data)
{
283
	unsigned long *cnt = data;
284
285
286
287
288
289
290
291
292
293
294
295

	switch(get_irn_opcode(irn)) {
	case iro_Proj:
	case iro_Phi:
	case iro_Start:
	case iro_End:
		break;
	default:
		(*cnt)++;
	}
}

296
static unsigned long count_insns(ir_graph *irg)
297
{
298
	unsigned long cnt = 0;
299
300
301
302
	irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
	return cnt;
}

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
static void block_count_walker(ir_node *node, void *data)
{
	unsigned long *cnt = data;
	if (node == get_irg_end_block(current_ir_graph))
		return;
	(*cnt)++;
}

static unsigned long count_blocks(ir_graph *irg)
{
	unsigned long cnt = 0;
	irg_block_walk_graph(irg, block_count_walker, NULL, &cnt);
	return cnt;
}

static node_stat_t last_node_stat;

320
/**
321
 * Perform things which need to be done per register class before spilling.
322
 */
323
324
static void pre_spill(post_spill_env_t *pse, const arch_register_class_t *cls)
{
Matthias Braun's avatar
Matthias Braun committed
325
326
327
328
	be_chordal_env_t    *chordal_env = &pse->cenv;
	be_irg_t            *birg        = pse->birg;
	ir_graph            *irg         = be_get_birg_irg(birg);
	const be_main_env_t *main_env    = birg->main_env;
329

330
331
	pse->cls                   = cls;
	chordal_env->cls           = cls;
332
333
	chordal_env->border_heads  = pmap_create();
	chordal_env->ignore_colors = bitset_malloc(chordal_env->cls->n_regs);
334

Sebastian Hack's avatar
Sebastian Hack committed
335
336
	be_assure_liveness(birg);
	be_liveness_assure_chk(be_get_birg_liveness(birg));
337

338
	stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq));
Sebastian Hack's avatar
Sebastian Hack committed
339

340
	/* put all ignore registers into the ignore register set. */
341
	be_put_ignore_regs(birg, pse->cls, chordal_env->ignore_colors);
Sebastian Hack's avatar
Sebastian Hack committed
342

Matthias Braun's avatar
Matthias Braun committed
343
	BE_TIMER_PUSH(t_ra_constr);
344
	be_pre_spill_prepare_constr(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
345
346
	BE_TIMER_POP(t_ra_constr);

347
	dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
348
}
349

350
351
352
/**
 * Perform things which need to be done per register class after spilling.
 */
353
static void post_spill(post_spill_env_t *pse, int iteration) {
354
	be_chordal_env_t    *chordal_env = &pse->cenv;
355
356
	be_irg_t            *birg        = pse->birg;
	ir_graph            *irg         = birg->irg;
357
	const be_main_env_t *main_env    = birg->main_env;
358
	int                  colors_n     = arch_register_class_n_regs(chordal_env->cls);
359
360
361
	int             allocatable_regs = colors_n - be_put_ignore_regs(birg, chordal_env->cls, NULL);

	/* some special classes contain only ignore regs, no work to be done */
362
	if (allocatable_regs > 0) {
363
		stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost);
Adam Szalkowski's avatar
Adam Szalkowski committed
364

365
366
367
368
369
		/*
			If we have a backend provided spiller, post spill is
			called in a loop after spilling for each register class.
			But we only need to fix stack nodes once in this case.
		*/
Matthias Braun's avatar
Matthias Braun committed
370
371
		BE_TIMER_PUSH(t_ra_spill);
		check_for_memory_operands(chordal_env);
372
373
374
		if (iteration == 0) {
			be_abi_fix_stack_nodes(birg->abi);
		}
Matthias Braun's avatar
Matthias Braun committed
375
		BE_TIMER_POP(t_ra_spill);
Sebastian Hack's avatar
Sebastian Hack committed
376

Matthias Braun's avatar
Matthias Braun committed
377
		BE_TIMER_PUSH(t_verify);
Sebastian Hack's avatar
Sebastian Hack committed
378

379
380
381
382
		/* verify schedule and register pressure */
		if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) {
			be_verify_schedule(birg);
			be_verify_register_pressure(birg, pse->cls, irg);
Matthias Braun's avatar
Matthias Braun committed
383
		} else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
384
385
386
387
			assert(be_verify_schedule(birg) && "Schedule verification failed");
			assert(be_verify_register_pressure(birg, pse->cls, irg)
				&& "Register pressure verification failed");
		}
Matthias Braun's avatar
Matthias Braun committed
388
		BE_TIMER_POP(t_verify);
389

390
		/* Color the graph. */
Matthias Braun's avatar
Matthias Braun committed
391
		BE_TIMER_PUSH(t_ra_color);
392
		be_ra_chordal_color(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
393
		BE_TIMER_POP(t_ra_color);
394

395
		dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
396

397
		/* Create the ifg with the selected flavor */
Matthias Braun's avatar
Matthias Braun committed
398
		BE_TIMER_PUSH(t_ra_ifg);
399
		chordal_env->ifg = be_create_ifg(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
400
		BE_TIMER_POP(t_ra_ifg);
Christian Würdig's avatar
Christian Würdig committed
401

402
		stat_ev_if {
403
			be_ifg_stat_t stat;
404
			node_stat_t   node_stat;
405

406
			be_ifg_stat(birg, chordal_env->ifg, &stat);
407
408
409
			stat_ev_dbl("bechordal_ifg_nodes", stat.n_nodes);
			stat_ev_dbl("bechordal_ifg_edges", stat.n_edges);
			stat_ev_dbl("bechordal_ifg_comps", stat.n_comps);
410

411
412
413
414
415
			node_stats(birg, &node_stat);
			stat_ev_dbl("bechordal_perms_before_coal",
					node_stat.n_perms - last_node_stat.n_perms);
			stat_ev_dbl("bechordal_copies_before_coal",
					node_stat.n_copies - last_node_stat.n_copies);
416
		}
Christian Würdig's avatar
Christian Würdig committed
417

418
		/* copy minimization */
Matthias Braun's avatar
Matthias Braun committed
419
		BE_TIMER_PUSH(t_ra_copymin);
420
		co_driver(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
421
		BE_TIMER_POP(t_ra_copymin);
422
423
424

		dump(BE_CH_DUMP_COPYMIN, irg, pse->cls, "-copymin", dump_ir_block_graph_sched);

425

426
		/* ssa destruction */
Matthias Braun's avatar
Matthias Braun committed
427
		BE_TIMER_PUSH(t_ra_ssa);
428
		be_ssa_destruction(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
429
		BE_TIMER_POP(t_ra_ssa);
430

431
		dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
432

433
		if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
Matthias Braun's avatar
Matthias Braun committed
434
			BE_TIMER_PUSH(t_verify);
435
			be_ssa_destruction_check(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
436
			BE_TIMER_POP(t_verify);
437
		}
438

439
440
		/* the ifg exists only if there are allocatable regs */
		be_ifg_free(chordal_env->ifg);
441
	}
Sebastian Hack's avatar
Sebastian Hack committed
442

443
	/* free some always allocated data structures */
444
445
446
	pmap_destroy(chordal_env->border_heads);
	bitset_free(chordal_env->ignore_colors);
}
447

448
449
450
451
452
453
/**
 * Performs chordal register allocation for each register class on given irg.
 *
 * @param birg  Backend irg object
 * @return Structure containing timer for the single phases or NULL if no timing requested.
 */
454
static void be_ra_chordal_main(be_irg_t *birg)
455
{
456
457
458
	const be_main_env_t *main_env = birg->main_env;
	const arch_env_t    *arch_env = main_env->arch_env;
	ir_graph            *irg      = birg->irg;
459
460
	int                 j, m;
	be_chordal_env_t    chordal_env;
Michael Beck's avatar
Michael Beck committed
461
	struct obstack      obst;
Christian Würdig's avatar
Christian Würdig committed
462

Matthias Braun's avatar
Matthias Braun committed
463
464
	main_opts = main_env->options;

Matthias Braun's avatar
Matthias Braun committed
465
466
467
	BE_TIMER_PUSH(t_ra_other);

	BE_TIMER_PUSH(t_ra_prolog);
Sebastian Hack's avatar
Sebastian Hack committed
468

469
	be_assure_liveness(birg);
470

Michael Beck's avatar
Michael Beck committed
471
	chordal_env.obst          = &obst;
472
473
474
475
476
477
	chordal_env.opts          = &options;
	chordal_env.irg           = irg;
	chordal_env.birg          = birg;
	chordal_env.border_heads  = NULL;
	chordal_env.ifg           = NULL;
	chordal_env.ignore_colors = NULL;
478

Michael Beck's avatar
Michael Beck committed
479
	obstack_init(&obst);
480

Matthias Braun's avatar
Matthias Braun committed
481
	BE_TIMER_POP(t_ra_prolog);
Matthias Braun's avatar
Matthias Braun committed
482

Matthias Braun's avatar
Matthias Braun committed
483
	stat_ev_if {
484
		be_stat_ev("bechordal_insns_before", count_insns(irg));
485
486
		be_stat_ev("bechordal_blocks_before", count_blocks(irg));
		node_stats(birg, &last_node_stat);
Matthias Braun's avatar
Matthias Braun committed
487
	}
Matthias Braun's avatar
Matthias Braun committed
488

489
490
	if (! arch_code_generator_has_spiller(birg->cg)) {
		/* use one of the generic spiller */
Christian Würdig's avatar
Christian Würdig committed
491

492
		/* Perform the following for each register class. */
493
		for (j = 0, m = arch_env_get_n_reg_class(arch_env); j < m; ++j) {
494
			post_spill_env_t pse;
495
			const arch_register_class_t *cls
496
				= arch_env_get_reg_class(arch_env, j);
497
498
499
500

			if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
				continue;

Christian Würdig's avatar
Christian Würdig committed
501

502
503
			stat_ev_ctx_push_str("bechordal_cls", cls->name);

504
505
506
507
			stat_ev_if {
				be_do_stat_reg_pressure(birg, cls);
			}

508
			memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
509
			pse.birg = birg;
510
			pre_spill(&pse, cls);
511

Matthias Braun's avatar
Matthias Braun committed
512
			BE_TIMER_PUSH(t_ra_spill);
513
			be_do_spill(birg, cls);
Matthias Braun's avatar
Matthias Braun committed
514
			BE_TIMER_POP(t_ra_spill);
Sebastian Hack's avatar
Sebastian Hack committed
515

516
517
			dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
			     dump_ir_block_graph_sched);
Christian Würdig's avatar
Christian Würdig committed
518

519
			post_spill(&pse, 0);
520
521
522
523
524
525
526
527
528
529
530

			stat_ev_if {
				node_stat_t node_stat;

				node_stats(birg, &node_stat);
				stat_ev_dbl("bechordal_phis",
						node_stat.n_phis - last_node_stat.n_phis);
				stat_ev_dbl("bechordal_mem_phis",
						node_stat.n_mem_phis - last_node_stat.n_mem_phis);
				stat_ev_dbl("bechordal_reloads",
						node_stat.n_reloads - last_node_stat.n_reloads);
Matthias Braun's avatar
Matthias Braun committed
531
532
				stat_ev_dbl("bechordal_remats",
						node_stat.n_remats - last_node_stat.n_remats);
533
534
535
536
537
538
539
540
541
542
				stat_ev_dbl("bechordal_spills",
						node_stat.n_spills - last_node_stat.n_spills);
				stat_ev_dbl("bechordal_perms_after_coal",
						node_stat.n_perms - last_node_stat.n_perms);
				stat_ev_dbl("bechordal_copies_after_coal",
						node_stat.n_copies - last_node_stat.n_copies);

				last_node_stat = node_stat;
				stat_ev_ctx_pop("bechordal_cls");
			}
543
		}
544
	} else {
545
		post_spill_env_t *pse;
Christian Würdig's avatar
Christian Würdig committed
546

Christoph Mallon's avatar
Christoph Mallon committed
547
		/* the backend has its own spiller */
548
		m = arch_env_get_n_reg_class(arch_env);
Christian Würdig's avatar
Christian Würdig committed
549

550
551
552
553
		pse = alloca(m * sizeof(pse[0]));

		for (j = 0; j < m; ++j) {
			memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
554
			pse[j].birg = birg;
555
			pre_spill(&pse[j], pse[j].cls);
556
		}
Sebastian Hack's avatar
Sebastian Hack committed
557

Matthias Braun's avatar
Matthias Braun committed
558
		BE_TIMER_PUSH(t_ra_spill);
559
		arch_code_generator_spill(birg->cg, birg);
Matthias Braun's avatar
Matthias Braun committed
560
		BE_TIMER_POP(t_ra_spill);
561
		dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
562

563
		for (j = 0; j < m; ++j) {
564
			post_spill(&pse[j], j);
Sebastian Hack's avatar
Sebastian Hack committed
565
		}
Sebastian Hack's avatar
Sebastian Hack committed
566
567
	}

Matthias Braun's avatar
Matthias Braun committed
568
	BE_TIMER_PUSH(t_verify);
Sebastian Hack's avatar
Sebastian Hack committed
569
	be_verify_register_allocation(birg);
Matthias Braun's avatar
Matthias Braun committed
570
	BE_TIMER_POP(t_verify);
Sebastian Hack's avatar
Sebastian Hack committed
571

Matthias Braun's avatar
Matthias Braun committed
572
	BE_TIMER_PUSH(t_ra_epilog);
573
	lower_nodes_after_ra(birg, options.lower_perm_opt & BE_CH_LOWER_PERM_COPY ? 1 : 0);
574
	dump(BE_CH_DUMP_LOWER, irg, NULL, "-belower-after-ra", dump_ir_block_graph_sched);
575

Michael Beck's avatar
Michael Beck committed
576
	obstack_free(&obst, NULL);
Sebastian Hack's avatar
Sebastian Hack committed
577
	be_liveness_invalidate(be_get_birg_liveness(birg));
Matthias Braun's avatar
Matthias Braun committed
578
	BE_TIMER_POP(t_ra_epilog);
579

Matthias Braun's avatar
Matthias Braun committed
580
	BE_TIMER_POP(t_ra_other);
581

Matthias Braun's avatar
Matthias Braun committed
582
	stat_ev_if {
583
		be_stat_ev("bechordal_insns_after", count_insns(irg));
Matthias Braun's avatar
Matthias Braun committed
584
	}
Sebastian Hack's avatar
Sebastian Hack committed
585
586
}

587
static be_ra_t be_ra_chordal_allocator = {
Christian Würdig's avatar
Christian Würdig committed
588
	be_ra_chordal_main,
Sebastian Hack's avatar
Sebastian Hack committed
589
};
590

591
void be_init_chordal_main(void)
592
593
594
595
596
597
{
	lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
	lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
	lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");

	lc_opt_add_table(chordal_grp, be_chordal_options);
598

599
600
601
	be_register_allocator("chordal", &be_ra_chordal_allocator);
}

602
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);