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

41
42
43
44
#include <libcore/lc_opts.h>
#include <libcore/lc_opts_enum.h>
#include <libcore/lc_timing.h>

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

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

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

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

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

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

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

110
static be_options_t  *main_opts;
111

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

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

Sebastian Hack's avatar
Sebastian Hack committed
122
static const lc_opt_enum_int_items_t dump_items[] = {
123
	{ "none",       BE_CH_DUMP_NONE       },
Matthias Braun's avatar
Matthias Braun committed
124
125
126
127
128
129
130
131
132
133
134
	{ "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
135
136
137
138
139
140
141
	{ 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
142
143
144
	{ NULL, 0 }
};

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

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

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

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

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

Christian Würdig's avatar
Christian Würdig committed
180
181
182
183
184
185
186
187
/**
 * 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;
	const arch_env_t *aenv = cenv->birg->main_env->arch_env;
	const ir_edge_t  *edge, *ne;
	ir_node          *block;
188
	ir_node          *spill;
Christian Würdig's avatar
Christian Würdig committed
189
190
191
192

	if (! be_is_Reload(irn))
		return;

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

	spill = be_get_Reload_mem(irn);
Christian Würdig's avatar
Christian Würdig committed
198
199
200
201
202
203
	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);

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

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

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

/**
 * 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);
}

226
227
228
229
230
231
/**
 * 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
232
233
	unsigned int n_copies;    /**< Copies */
	unsigned int n_perms;     /**< Perms */
234
	unsigned int n_spills;    /**< Spill nodes */
Sebastian Hack's avatar
Sebastian Hack committed
235
	unsigned int n_reloads;   /**< Reloads */
236
237
238
} node_stat_t;

struct node_stat_walker {
239
240
241
242
	node_stat_t      *stat;
	const arch_env_t *arch_env;
	bitset_t         *mem_phis;
	const arch_register_class_t *cls;
243
244
245
246
};

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

250
	if (arch_irn_consider_in_reg_alloc(aenv, env->cls, irn)) {
251
252
253
254
255
256
257
258
259
260
261

		/* if the node is a normal phi */
		if(is_Phi(irn))
			env->stat->n_phis++;

		else if(arch_irn_classify(aenv, irn) & arch_irn_class_spill)
			++env->stat->n_spills;

		else if(arch_irn_classify(aenv, irn) & arch_irn_class_reload)
			++env->stat->n_reloads;

Sebastian Hack's avatar
Sebastian Hack committed
262
263
264
265
266
		else if(arch_irn_classify(aenv, irn) & arch_irn_class_copy)
			++env->stat->n_copies;

		else if(arch_irn_classify(aenv, irn) & arch_irn_class_perm)
			++env->stat->n_perms;
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
	}

	/* a mem phi is a PhiM with a mem phi operand or a Spill operand */
	else if(is_Phi(irn) && get_irn_mode(irn) == mode_M) {
		int i;

		for(i = get_irn_arity(irn) - 1; i >= 0; --i) {
			ir_node *op = get_irn_n(irn, i);

			if((is_Phi(op) && bitset_contains_irn(env->mem_phis, op)) || (arch_irn_classify(aenv, op) & arch_irn_class_spill)) {
				bitset_add_irn(env->mem_phis, irn);
				env->stat->n_mem_phis++;
				break;
			}
		}
	}
}

285
static void node_stats(be_irg_t *birg, const arch_register_class_t *cls, node_stat_t *stat)
286
287
288
289
{
	struct node_stat_walker env;

	memset(stat, 0, sizeof(stat[0]));
290
291
	env.arch_env = birg->main_env->arch_env;
	env.mem_phis = bitset_irg_malloc(birg->irg);
292
	env.stat     = stat;
293
294
	env.cls      = cls;
	irg_walk_graph(birg->irg, NULL, node_stat_walker, &env);
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
	bitset_free(env.mem_phis);
}

static void insn_count_walker(ir_node *irn, void *data)
{
	int *cnt = data;

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

static unsigned int count_insns(ir_graph *irg)
{
	int cnt = 0;
	irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
	return cnt;
}

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
329
	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;
	node_stat_t          node_stat;
330

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

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

339
	stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
Sebastian Hack's avatar
Sebastian Hack committed
340
	stat_ev_do(node_stats(birg, pse->cls, &node_stat));
Matthias Braun's avatar
Matthias Braun committed
341
	stat_ev_do(pse->pre_spill_cost = be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq));
342
	stat_ev_dbl("phis_before_spill", node_stat.n_phis);
Sebastian Hack's avatar
Sebastian Hack committed
343

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

Matthias Braun's avatar
Matthias Braun committed
347
	BE_TIMER_PUSH(t_ra_constr);
348
	be_pre_spill_prepare_constr(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
349
350
	BE_TIMER_POP(t_ra_constr);

351
	dump(BE_CH_DUMP_CONSTR, birg->irg, pse->cls, "-constr-pre", dump_ir_block_graph_sched);
Sebastian Hack's avatar
Sebastian Hack committed
352

353
	stat_ev_ctx_pop("bechordal_cls");
354
}
355

356
357
358
/**
 * Perform things which need to be done per register class after spilling.
 */
359
static void post_spill(post_spill_env_t *pse, int iteration) {
360
	be_chordal_env_t    *chordal_env = &pse->cenv;
361
362
	be_irg_t            *birg        = pse->birg;
	ir_graph            *irg         = birg->irg;
363
	const be_main_env_t *main_env    = birg->main_env;
364
365
	node_stat_t          node_stat;
	int                  colors_n     = arch_register_class_n_regs(chordal_env->cls);
366
367
368
	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 */
369
	if (allocatable_regs > 0) {
370

371
		stat_ev_ctx_push_str("bechordal_cls", pse->cls->name);
Sebastian Hack's avatar
Sebastian Hack committed
372
373
374
375
376
		stat_ev_do(node_stats(birg, pse->cls, &node_stat));
		stat_ev_dbl("phis_after_spill", node_stat.n_phis);
		stat_ev_dbl("mem_phis", node_stat.n_mem_phis);
		stat_ev_dbl("reloads", node_stat.n_reloads);
		stat_ev_dbl("spills", node_stat.n_spills);
Matthias Braun's avatar
Matthias Braun committed
377
		stat_ev_dbl("spillcosts", be_estimate_irg_costs(irg, main_env->arch_env, birg->exec_freq) - pse->pre_spill_cost);
Adam Szalkowski's avatar
Adam Szalkowski committed
378

379
380
381
382
383
		/*
			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
384
385
		BE_TIMER_PUSH(t_ra_spill);
		check_for_memory_operands(chordal_env);
386
387
388
		if (iteration == 0) {
			be_abi_fix_stack_nodes(birg->abi);
		}
Matthias Braun's avatar
Matthias Braun committed
389
		BE_TIMER_POP(t_ra_spill);
Sebastian Hack's avatar
Sebastian Hack committed
390

Matthias Braun's avatar
Matthias Braun committed
391
		BE_TIMER_PUSH(t_verify);
Sebastian Hack's avatar
Sebastian Hack committed
392

393
394
395
396
397
398
399
400
401
402
		/* 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);
		}
		else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) {
			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
403
		BE_TIMER_POP(t_verify);
404

405
		/* Color the graph. */
Matthias Braun's avatar
Matthias Braun committed
406
		BE_TIMER_PUSH(t_ra_color);
407
		be_ra_chordal_color(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
408
		BE_TIMER_POP(t_ra_color);
409

410
		dump(BE_CH_DUMP_CONSTR, irg, pse->cls, "-color", dump_ir_block_graph_sched);
411

412
		/* Create the ifg with the selected flavor */
Matthias Braun's avatar
Matthias Braun committed
413
		BE_TIMER_PUSH(t_ra_ifg);
414
		chordal_env->ifg = be_create_ifg(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
415
		BE_TIMER_POP(t_ra_ifg);
Christian Würdig's avatar
Christian Würdig committed
416

Sebastian Hack's avatar
Sebastian Hack committed
417
		{
418
			be_ifg_stat_t stat;
419

Sebastian Hack's avatar
Sebastian Hack committed
420
421
422
423
			stat_ev_do(be_ifg_stat(birg, chordal_env->ifg, &stat));
			stat_ev_dbl("ifg_nodes", stat.n_nodes);
			stat_ev_dbl("ifg_edges", stat.n_edges);
			stat_ev_dbl("ifg_comps", stat.n_comps);
424

Sebastian Hack's avatar
Sebastian Hack committed
425
426
427
			stat_ev_do(node_stats(birg, pse->cls, &node_stat));
			stat_ev_dbl("perms_before_coal", node_stat.n_perms);
			stat_ev_dbl("copies_before_coal", node_stat.n_copies);
428
		}
Christian Würdig's avatar
Christian Würdig committed
429

430
		/* copy minimization */
Matthias Braun's avatar
Matthias Braun committed
431
		BE_TIMER_PUSH(t_ra_copymin);
432
		co_driver(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
433
		BE_TIMER_POP(t_ra_copymin);
434
435
436

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

437

438
		/* ssa destruction */
Matthias Braun's avatar
Matthias Braun committed
439
		BE_TIMER_PUSH(t_ra_ssa);
440
		stat_ev_ctx_push_str("berachordal_phase", "ssadestr");
441
		be_ssa_destruction(chordal_env);
442
		stat_ev_ctx_pop("berachordal_phase");
Matthias Braun's avatar
Matthias Braun committed
443
		BE_TIMER_POP(t_ra_ssa);
444

445
		dump(BE_CH_DUMP_SSADESTR, irg, pse->cls, "-ssadestr", dump_ir_block_graph_sched);
446

447
		if (chordal_env->opts->vrfy_option != BE_CH_VRFY_OFF) {
Matthias Braun's avatar
Matthias Braun committed
448
			BE_TIMER_PUSH(t_verify);
449
			be_ssa_destruction_check(chordal_env);
Matthias Braun's avatar
Matthias Braun committed
450
			BE_TIMER_POP(t_verify);
451
		}
452

Sebastian Hack's avatar
Sebastian Hack committed
453
454
455
		stat_ev_do(node_stats(birg, pse->cls, &node_stat));
		stat_ev_dbl("perms_after_coal", node_stat.n_perms);
		stat_ev_dbl("copies_after_coal", node_stat.n_copies);
456
		stat_ev_ctx_pop("bechordal_cls");
Christian Würdig's avatar
Christian Würdig committed
457

458
459
		/* the ifg exists only if there are allocatable regs */
		be_ifg_free(chordal_env->ifg);
460
	}
Sebastian Hack's avatar
Sebastian Hack committed
461

462
	/* free some always allocated data structures */
463
464
465
	pmap_destroy(chordal_env->border_heads);
	bitset_free(chordal_env->ignore_colors);
}
466

467
468
469
470
471
472
/**
 * 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.
 */
473
static void be_ra_chordal_main(be_irg_t *birg)
474
475
476
477
478
479
{
	const be_main_env_t *main_env  = birg->main_env;
	const arch_isa_t    *isa       = arch_env_get_isa(main_env->arch_env);
	ir_graph            *irg       = birg->irg;
	int                 j, m;
	be_chordal_env_t    chordal_env;
Michael Beck's avatar
Michael Beck committed
480
	struct obstack      obst;
Christian Würdig's avatar
Christian Würdig committed
481

Matthias Braun's avatar
Matthias Braun committed
482
483
	main_opts = main_env->options;

Matthias Braun's avatar
Matthias Braun committed
484
485
486
	BE_TIMER_PUSH(t_ra_other);

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

488
489
	be_assure_dom_front(birg);
	be_assure_liveness(birg);
490

Michael Beck's avatar
Michael Beck committed
491
	chordal_env.obst          = &obst;
492
493
494
495
496
497
	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;
498

Michael Beck's avatar
Michael Beck committed
499
	obstack_init(&obst);
500

Matthias Braun's avatar
Matthias Braun committed
501
	BE_TIMER_POP(t_ra_prolog);
Matthias Braun's avatar
Matthias Braun committed
502

Matthias Braun's avatar
Matthias Braun committed
503
504
505
	stat_ev_if {
		be_stat_ev("insns_before", count_insns(irg));
	}
Matthias Braun's avatar
Matthias Braun committed
506

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

510
511
512
		/* Perform the following for each register class. */
		for (j = 0, m = arch_isa_get_n_reg_class(isa); j < m; ++j) {
			post_spill_env_t pse;
513
514
515
516
517
518
			const arch_register_class_t *cls
				= arch_isa_get_reg_class(isa, j);

			if(arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
				continue;

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

520
			memcpy(&pse.cenv, &chordal_env, sizeof(chordal_env));
521
			pse.birg = birg;
522
			pre_spill(&pse, cls);
523

Matthias Braun's avatar
Matthias Braun committed
524
			BE_TIMER_PUSH(t_ra_spill);
525
			be_do_spill(birg, cls);
Matthias Braun's avatar
Matthias Braun committed
526
			BE_TIMER_POP(t_ra_spill);
Sebastian Hack's avatar
Sebastian Hack committed
527

528
529
			dump(BE_CH_DUMP_SPILL, irg, pse.cls, "-spill",
			     dump_ir_block_graph_sched);
Christian Würdig's avatar
Christian Würdig committed
530

531
			post_spill(&pse, 0);
532
		}
533
	} else {
534
		post_spill_env_t *pse;
Christian Würdig's avatar
Christian Würdig committed
535

536
537
		/* the backend has it's own spiller */
		m = arch_isa_get_n_reg_class(isa);
Christian Würdig's avatar
Christian Würdig committed
538

539
540
541
542
		pse = alloca(m * sizeof(pse[0]));

		for (j = 0; j < m; ++j) {
			memcpy(&pse[j].cenv, &chordal_env, sizeof(chordal_env));
543
			pse[j].birg = birg;
544
			pre_spill(&pse[j], pse[j].cls);
545
		}
Sebastian Hack's avatar
Sebastian Hack committed
546

Matthias Braun's avatar
Matthias Braun committed
547
		BE_TIMER_PUSH(t_ra_spill);
548
		arch_code_generator_spill(birg->cg, birg);
Matthias Braun's avatar
Matthias Braun committed
549
		BE_TIMER_POP(t_ra_spill);
550
		dump(BE_CH_DUMP_SPILL, irg, NULL, "-spill", dump_ir_block_graph_sched);
551

552
		for (j = 0; j < m; ++j) {
553
			post_spill(&pse[j], j);
Sebastian Hack's avatar
Sebastian Hack committed
554
		}
Sebastian Hack's avatar
Sebastian Hack committed
555
556
	}

Matthias Braun's avatar
Matthias Braun committed
557
	BE_TIMER_PUSH(t_verify);
Sebastian Hack's avatar
Sebastian Hack committed
558
	be_verify_register_allocation(birg);
Matthias Braun's avatar
Matthias Braun committed
559
	BE_TIMER_POP(t_verify);
Sebastian Hack's avatar
Sebastian Hack committed
560

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

Michael Beck's avatar
Michael Beck committed
565
	obstack_free(&obst, NULL);
Sebastian Hack's avatar
Sebastian Hack committed
566
	be_liveness_invalidate(be_get_birg_liveness(birg));
Matthias Braun's avatar
Matthias Braun committed
567
	BE_TIMER_POP(t_ra_epilog);
568

Matthias Braun's avatar
Matthias Braun committed
569
	BE_TIMER_POP(t_ra_other);
570

Matthias Braun's avatar
Matthias Braun committed
571
572
573
	stat_ev_if {
		be_stat_ev("insns_after", count_insns(irg));
	}
574

575
	return;
Sebastian Hack's avatar
Sebastian Hack committed
576
577
}

578
static be_ra_t be_ra_chordal_allocator = {
Christian Würdig's avatar
Christian Würdig committed
579
	be_ra_chordal_main,
Sebastian Hack's avatar
Sebastian Hack committed
580
};
581

582
void be_init_chordal_main(void)
583
584
585
586
587
588
{
	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);
589

590
591
592
	be_register_allocator("chordal", &be_ra_chordal_allocator);
}

593
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main);