bechordal_main.c 9.38 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
2
#include "irdump_t.h"
Christian Würdig's avatar
Christian Würdig committed
3
 * This file is part of libFirm.
4
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
5
6
 */

Sebastian Hack's avatar
Sebastian Hack committed
7
/**
Christian Würdig's avatar
Christian Würdig committed
8
9
10
11
 * @file
 * @brief       Driver for the chordal register allocator.
 * @author      Sebastian Hack
 * @date        29.11.2005
Sebastian Hack's avatar
Sebastian Hack committed
12
 */
Christian Würdig's avatar
Christian Würdig committed
13
#include <stdlib.h>
14
15
#include <time.h>

Sebastian Hack's avatar
Sebastian Hack committed
16
17
18
#include "obst.h"
#include "bitset.h"

Matthias Braun's avatar
Matthias Braun committed
19
20
#include "lc_opts.h"
#include "lc_opts_enum.h"
21

Michael Beck's avatar
Michael Beck committed
22
#include "ircons_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
23
24
25
#include "irmode_t.h"
#include "irgraph_t.h"
#include "irgwalk.h"
Sebastian Hack's avatar
Sebastian Hack committed
26
#include "ircons.h"
Sebastian Hack's avatar
Sebastian Hack committed
27
28
#include "irdump.h"
#include "irdom.h"
Christian Würdig's avatar
Christian Würdig committed
29
#include "ircons.h"
30
31
#include "irnode.h"
#include "ircons.h"
32
#include "irtools.h"
Sebastian Hack's avatar
Sebastian Hack committed
33
#include "debug.h"
Sebastian Hack's avatar
Sebastian Hack committed
34
#include "execfreq.h"
Christian Würdig's avatar
Christian Würdig committed
35
#include "iredges_t.h"
Matthias Braun's avatar
Matthias Braun committed
36
#include "panic.h"
Sebastian Hack's avatar
Sebastian Hack committed
37
38

#include "bechordal_t.h"
39
#include "beabi.h"
Sebastian Hack's avatar
Sebastian Hack committed
40
41
#include "beutil.h"
#include "besched.h"
42
#include "besched.h"
Sebastian Hack's avatar
Sebastian Hack committed
43
#include "belive_t.h"
44
#include "bearch.h"
45
#include "beifg.h"
46
#include "benode.h"
Matthias Braun's avatar
Matthias Braun committed
47
#include "statev_t.h"
48
#include "bestat.h"
49
#include "bemodule.h"
50
#include "be_t.h"
Christian Würdig's avatar
Christian Würdig committed
51
#include "bera.h"
52
#include "beirg.h"
53
#include "bestack.h"
Sebastian Hack's avatar
Sebastian Hack committed
54

Matthias Braun's avatar
Matthias Braun committed
55
#include "bespillslots.h"
56
#include "bespill.h"
57
58
#include "belower.h"

Daniel Grund's avatar
Daniel Grund committed
59
#include "becopystat.h"
Daniel Grund's avatar
Daniel Grund committed
60
#include "becopyopt.h"
Christian Würdig's avatar
Christian Würdig committed
61
#include "bessadestr.h"
62
#include "beverify.h"
63
#include "benode.h"
Christian Würdig's avatar
Christian Würdig committed
64

65
66
#include "bepbqpcoloring.h"

Sebastian Hack's avatar
Sebastian Hack committed
67
static be_ra_chordal_opts_t options = {
Sebastian Hack's avatar
Sebastian Hack committed
68
	BE_CH_DUMP_NONE,
Christian Würdig's avatar
Christian Würdig committed
69
	BE_CH_LOWER_PERM_SWAP,
Sebastian Hack's avatar
Sebastian Hack committed
70
71
};

72
73
static const lc_opt_enum_int_items_t lower_perm_items[] = {
	{ "copy", BE_CH_LOWER_PERM_COPY },
74
75
76
77
	{ "swap", BE_CH_LOWER_PERM_SWAP },
	{ NULL, 0 }
};

78
static const lc_opt_enum_mask_items_t dump_items[] = {
79
	{ "none",       BE_CH_DUMP_NONE       },
Matthias Braun's avatar
Matthias Braun committed
80
81
82
83
84
85
	{ "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  },
86
	{ "split",      BE_CH_DUMP_SPLIT      },
Matthias Braun's avatar
Matthias Braun committed
87
88
89
90
91
	{ "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
92
93
94
	{ NULL, 0 }
};

95
static lc_opt_enum_int_var_t lower_perm_var = {
96
	&options.lower_perm_opt, lower_perm_items
97
98
};

99
static lc_opt_enum_mask_var_t dump_var = {
Sebastian Hack's avatar
Sebastian Hack committed
100
101
102
	&options.dump_flags, dump_items
};

103
static const lc_opt_table_entry_t be_chordal_options[] = {
104
	LC_OPT_ENT_ENUM_INT ("perm",          "perm lowering options", &lower_perm_var),
Sebastian Hack's avatar
Sebastian Hack committed
105
	LC_OPT_ENT_ENUM_MASK("dump",          "select dump phases", &dump_var),
106
	LC_OPT_LAST
107
108
};

109
110
111
112
113
114
115
116
117
118
119
static be_module_list_entry_t *colorings = NULL;
static const be_ra_chordal_coloring_t *selected_coloring = NULL;

void be_register_chordal_coloring(const char *name, be_ra_chordal_coloring_t *coloring)
{
	if (selected_coloring == NULL)
		selected_coloring = coloring;

	be_add_module_to_list(&colorings, name, coloring);
}

120
static void be_ra_chordal_coloring(be_chordal_env_t *env)
121
{
Andreas Zwinkau's avatar
Andreas Zwinkau committed
122
	selected_coloring->allocate(env);
123
124
}

Sebastian Hack's avatar
Sebastian Hack committed
125
static void dump(unsigned mask, ir_graph *irg,
126
				 const arch_register_class_t *cls,
127
				 const char *suffix)
Sebastian Hack's avatar
Sebastian Hack committed
128
{
129
	if ((options.dump_flags & mask) == mask) {
Christian Würdig's avatar
Christian Würdig committed
130
		if (cls) {
131
			char buf[256];
132
133
134
135
			snprintf(buf, sizeof(buf), "%s-%s", cls->name, suffix);
			dump_ir_graph(irg, buf);
		} else {
			dump_ir_graph(irg, suffix);
136
137
		}
	}
Sebastian Hack's avatar
Sebastian Hack committed
138
139
}

Christian Würdig's avatar
Christian Würdig committed
140
/**
141
142
 * Post-Walker: Checks for the given reload if has only one user that can
 * perform the reload as part of its address mode.
unknown's avatar
unknown committed
143
 * Fold the reload into the user it that is possible.
Christian Würdig's avatar
Christian Würdig committed
144
 */
145
146
147
static void memory_operand_walker(ir_node *irn, void *env)
{
	(void)env;
148
149
150
151
152
153
154
155
156
157
158
	for (int i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
		ir_node *in = get_irn_n(irn, i);
		if (!arch_irn_is(skip_Proj(in), reload))
			continue;
		if (get_nodes_block(in) != get_nodes_block(irn))
			continue;
		/* only use memory operands, if the reload is only used by 1 node */
		if (get_irn_n_edges(in) > 1)
			continue;
		if (arch_possible_memory_operand(irn, i)) {
			arch_perform_memory_operand(irn, i);
Christian Würdig's avatar
Christian Würdig committed
159
160
161
162
163
164
165
		}
	}
}

/**
 * Starts a walk for memory operands if supported by the backend.
 */
Matthias Braun's avatar
Matthias Braun committed
166
void check_for_memory_operands(ir_graph *irg)
167
168
{
	irg_walk_graph(irg, NULL, memory_operand_walker, NULL);
Christian Würdig's avatar
Christian Würdig committed
169
170
}

171

Matthias Braun's avatar
Matthias Braun committed
172
static be_node_stats_t last_node_stats;
173

174
/**
175
 * Perform things which need to be done per register class before spilling.
176
 */
177
static void pre_spill(be_chordal_env_t *const chordal_env, arch_register_class_t const *const cls, ir_graph *const irg)
178
{
179
180
	chordal_env->cls              = cls;
	chordal_env->border_heads     = pmap_create();
181
	chordal_env->allocatable_regs = bitset_malloc(cls->n_regs);
182

183
	be_assure_live_chk(irg);
184

185
	/* put all ignore registers into the ignore register set. */
186
	be_get_allocatable_regs(irg, cls, chordal_env->allocatable_regs->data);
187
}
188

189
190
191
/**
 * Perform things which need to be done per register class after spilling.
 */
192
static void post_spill(be_chordal_env_t *const chordal_env, ir_graph *const irg)
193
{
194
195
196
197
198
199
200
201
202
203
	/*
		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.
	*/
	be_timer_push(T_RA_SPILL_APPLY);
	check_for_memory_operands(irg);
	be_timer_pop(T_RA_SPILL_APPLY);

	/* verify schedule and register pressure */
Matthias Braun's avatar
Matthias Braun committed
204
205
	if (be_options.do_verify) {
		be_timer_push(T_VERIFY);
206
207
		be_verify_schedule(irg);
		be_verify_register_pressure(irg, chordal_env->cls);
Matthias Braun's avatar
Matthias Braun committed
208
		be_timer_pop(T_VERIFY);
209
	}
210

211
212
213
214
	/* Color the graph. */
	be_timer_push(T_RA_COLOR);
	be_ra_chordal_coloring(chordal_env);
	be_timer_pop(T_RA_COLOR);
215

216
	dump(BE_CH_DUMP_COLOR, irg, chordal_env->cls, "color");
Christian Würdig's avatar
Christian Würdig committed
217

218
219
220
221
	/* Create the ifg with the selected flavor */
	be_timer_push(T_RA_IFG);
	chordal_env->ifg = be_create_ifg(chordal_env);
	be_timer_pop(T_RA_IFG);
222

223
224
225
	if (stat_ev_enabled) {
		be_ifg_stat_t   stat;
		be_node_stats_t node_stats;
226

227
228
229
230
		be_ifg_stat(irg, chordal_env->ifg, &stat);
		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);
Matthias Braun's avatar
Matthias Braun committed
231

232
233
		be_collect_node_stats(&node_stats, irg);
		be_subtract_node_stats(&node_stats, &last_node_stats);
Christian Würdig's avatar
Christian Würdig committed
234

235
236
237
		stat_ev_dbl("bechordal_perms_before_coal",  node_stats[BE_STAT_PERMS]);
		stat_ev_dbl("bechordal_copies_before_coal", node_stats[BE_STAT_COPIES]);
	}
238

239
240
241
	be_timer_push(T_RA_COPYMIN);
	co_driver(chordal_env);
	be_timer_pop(T_RA_COPYMIN);
242

243
	dump(BE_CH_DUMP_COPYMIN, irg, chordal_env->cls, "copymin");
244

245
246
	/* ssa destruction */
	be_timer_push(T_RA_SSA);
247
	be_ssa_destruction(chordal_env->irg, chordal_env->cls);
248
	be_timer_pop(T_RA_SSA);
249

250
	dump(BE_CH_DUMP_SSADESTR, irg, chordal_env->cls, "ssadestr");
251

Matthias Braun's avatar
Matthias Braun committed
252
	if (be_options.do_verify) {
253
		be_timer_push(T_VERIFY);
254
		be_ssa_destruction_check(chordal_env->irg, chordal_env->cls);
255
		be_timer_pop(T_VERIFY);
256
	}
Sebastian Hack's avatar
Sebastian Hack committed
257

258
259
260
	/* the ifg exists only if there are allocatable regs */
	be_ifg_free(chordal_env->ifg);

261
	/* free some always allocated data structures */
262
	pmap_destroy(chordal_env->border_heads);
263
	free(chordal_env->allocatable_regs);
264
}
265

266
267
268
/**
 * Performs chordal register allocation for each register class on given irg.
 *
269
270
271
 * @param irg    the graph
 * @return Structure containing timer for the single phases or NULL if no
 *         timing requested.
272
 */
273
static void be_ra_chordal_main(ir_graph *irg)
274
{
275
	const arch_env_t *arch_env = be_get_irg_arch_env(irg);
276
277
	int               j;
	int               m;
Matthias Braun's avatar
Matthias Braun committed
278

279
	be_timer_push(T_RA_OTHER);
Matthias Braun's avatar
Matthias Braun committed
280

281
282
	be_chordal_env_t chordal_env;
	obstack_init(&chordal_env.obst);
283
284
285
286
287
	chordal_env.opts             = &options;
	chordal_env.irg              = irg;
	chordal_env.border_heads     = NULL;
	chordal_env.ifg              = NULL;
	chordal_env.allocatable_regs = NULL;
288

289
	if (stat_ev_enabled) {
290
		be_collect_node_stats(&last_node_stats, irg);
Matthias Braun's avatar
Matthias Braun committed
291
	}
Matthias Braun's avatar
Matthias Braun committed
292

293
	/* use one of the generic spiller */
Christian Würdig's avatar
Christian Würdig committed
294

295
	/* Perform the following for each register class. */
296
297
	for (j = 0, m = arch_env->n_register_classes; j < m; ++j) {
		const arch_register_class_t *cls = &arch_env->register_classes[j];
298

299
300
		if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
			continue;
301

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

303
		stat_ev_ctx_push_str("bechordal_cls", cls->name);
304

305
		double pre_spill_cost = 0;
306
		if (stat_ev_enabled) {
307
			be_do_stat_reg_pressure(irg, cls);
308
			pre_spill_cost = be_estimate_irg_costs(irg);
309
		}
Matthias Braun's avatar
Matthias Braun committed
310

311
		pre_spill(&chordal_env, cls, irg);
Matthias Braun's avatar
Matthias Braun committed
312

313
314
315
		be_timer_push(T_RA_SPILL);
		be_do_spill(irg, cls);
		be_timer_pop(T_RA_SPILL);
Christian Würdig's avatar
Christian Würdig committed
316

317
		dump(BE_CH_DUMP_SPILL, irg, cls, "spill");
Christian Würdig's avatar
Christian Würdig committed
318

319
320
		stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg) - pre_spill_cost);

321
		post_spill(&chordal_env, irg);
322

323
		if (stat_ev_enabled) {
324
			be_node_stats_t node_stats;
Sebastian Hack's avatar
Sebastian Hack committed
325

326
327
328
			be_collect_node_stats(&node_stats, irg);
			be_subtract_node_stats(&node_stats, &last_node_stats);
			be_emit_node_stats(&node_stats, "bechordal_");
329

330
331
			be_copy_node_stats(&last_node_stats, &node_stats);
			stat_ev_ctx_pop("bechordal_cls");
Sebastian Hack's avatar
Sebastian Hack committed
332
		}
Sebastian Hack's avatar
Sebastian Hack committed
333
334
	}

335
	be_timer_push(T_RA_EPILOG);
336
	lower_nodes_after_ra(irg, options.lower_perm_opt == BE_CH_LOWER_PERM_COPY);
337
	dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
338

339
	obstack_free(&chordal_env.obst, NULL);
340
	be_invalidate_live_sets(irg);
341
	be_timer_pop(T_RA_EPILOG);
342

343
	be_timer_pop(T_RA_OTHER);
Sebastian Hack's avatar
Sebastian Hack committed
344
345
}

Matthias Braun's avatar
Matthias Braun committed
346
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main)
347
void be_init_chordal_main(void)
348
{
349
350
351
352
	static be_ra_t be_ra_chordal_allocator = {
		be_ra_chordal_main,
	};

353
354
355
356
	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");

Michael Beck's avatar
Michael Beck committed
357
358
	be_register_allocator("chordal", &be_ra_chordal_allocator);

359
	lc_opt_add_table(chordal_grp, be_chordal_options);
Manuel Mohr's avatar
Manuel Mohr committed
360
	be_add_module_list_opt(chordal_grp, "coloring", "select coloring method", &colorings, (void**) &selected_coloring);
361
}