bechordal_main.c 9.59 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"
Sebastian Hack's avatar
Sebastian Hack committed
36
37
38

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

Matthias Braun's avatar
Matthias Braun committed
52
#include "bespillslots.h"
53
#include "bespill.h"
54
#include "bespillutil.h"
55
56
#include "belower.h"

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

63
64
#include "bepbqpcoloring.h"

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

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

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

92
static lc_opt_enum_int_var_t lower_perm_var = {
93
	&options.lower_perm_opt, lower_perm_items
94
95
};

96
static lc_opt_enum_mask_var_t dump_var = {
Sebastian Hack's avatar
Sebastian Hack committed
97
98
99
	&options.dump_flags, dump_items
};

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

106
107
108
109
110
111
112
113
114
115
116
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);
}

117
static void be_ra_chordal_coloring(be_chordal_env_t *env)
118
{
Andreas Zwinkau's avatar
Andreas Zwinkau committed
119
	selected_coloring->allocate(env);
120
121
}

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

Christian Würdig's avatar
Christian Würdig committed
137
/**
138
139
 * 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
140
 * Fold the reload into the user it that is possible.
Christian Würdig's avatar
Christian Würdig committed
141
 */
142
143
static void memory_operand_walker(ir_node *irn, void *env)
{
144
	const regalloc_if_t *regif = (const regalloc_if_t*)env;
145
	foreach_irn_in(irn, i, in) {
146
147
148
149
150
151
152
		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;
153
		regif->perform_memory_operand(irn, i);
Christian Würdig's avatar
Christian Würdig committed
154
155
156
157
158
159
	}
}

/**
 * Starts a walk for memory operands if supported by the backend.
 */
160
void check_for_memory_operands(ir_graph *irg, const regalloc_if_t *regif)
161
{
162
163
164
	if (regif->perform_memory_operand == NULL)
		return;
	irg_walk_graph(irg, NULL, memory_operand_walker, (void*)regif);
Christian Würdig's avatar
Christian Würdig committed
165
166
}

Matthias Braun's avatar
Matthias Braun committed
167
static be_node_stats_t last_node_stats;
168

169
/**
170
 * Perform things which need to be done per register class before spilling.
171
 */
Matthias Braun's avatar
Matthias Braun committed
172
173
174
static void pre_spill(be_chordal_env_t *const chordal_env,
                      arch_register_class_t const *const cls,
                      ir_graph *const irg)
175
{
176
177
	chordal_env->cls              = cls;
	chordal_env->border_heads     = pmap_create();
178
	chordal_env->allocatable_regs = bitset_malloc(cls->n_regs);
179
	/* put all ignore registers into the ignore register set. */
180
	be_get_allocatable_regs(irg, cls, chordal_env->allocatable_regs->data);
Matthias Braun's avatar
Matthias Braun committed
181
	be_assure_live_chk(irg);
182
}
183

184
185
186
/**
 * Perform things which need to be done per register class after spilling.
 */
187
188
static void post_spill(be_chordal_env_t *const chordal_env, ir_graph *const irg,
					   const regalloc_if_t *regif)
189
{
Matthias Braun's avatar
Matthias Braun committed
190
191
192
	/* 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. */
193
	be_timer_push(T_RA_SPILL_APPLY);
194
	check_for_memory_operands(irg, regif);
195
196
197
	be_timer_pop(T_RA_SPILL_APPLY);

	/* verify schedule and register pressure */
Matthias Braun's avatar
Matthias Braun committed
198
199
	if (be_options.do_verify) {
		be_timer_push(T_VERIFY);
200
201
202
203
		bool check_schedule = be_verify_schedule(irg);
		be_check_verify_result(check_schedule, irg);
		bool check_pressure = be_verify_register_pressure(irg, chordal_env->cls);
		be_check_verify_result(check_pressure, irg);
Matthias Braun's avatar
Matthias Braun committed
204
		be_timer_pop(T_VERIFY);
205
	}
206

207
208
209
210
	/* Color the graph. */
	be_timer_push(T_RA_COLOR);
	be_ra_chordal_coloring(chordal_env);
	be_timer_pop(T_RA_COLOR);
211

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

214
215
216
217
	/* 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);
218

219
	if (stat_ev_enabled) {
Matthias Braun's avatar
Matthias Braun committed
220
		be_ifg_stat_t stat;
221
222
223
224
		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
225

Matthias Braun's avatar
Matthias Braun committed
226
		be_node_stats_t node_stats;
227
228
		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
229

230
231
232
		stat_ev_dbl("bechordal_perms_before_coal",  node_stats[BE_STAT_PERMS]);
		stat_ev_dbl("bechordal_copies_before_coal", node_stats[BE_STAT_COPIES]);
	}
233

234
235
236
	be_timer_push(T_RA_COPYMIN);
	co_driver(chordal_env);
	be_timer_pop(T_RA_COPYMIN);
237

238
	dump(BE_CH_DUMP_COPYMIN, irg, chordal_env->cls, "copymin");
239

240
241
	/* ssa destruction */
	be_timer_push(T_RA_SSA);
242
	be_ssa_destruction(chordal_env->irg, chordal_env->cls);
243
	be_timer_pop(T_RA_SSA);
244

245
	dump(BE_CH_DUMP_SSADESTR, irg, chordal_env->cls, "ssadestr");
246

Matthias Braun's avatar
Matthias Braun committed
247
	if (be_options.do_verify) {
248
		be_timer_push(T_VERIFY);
249
250
		bool fine = be_ssa_destruction_check(chordal_env->irg, chordal_env->cls);
		be_check_verify_result(fine, chordal_env->irg);
251
		be_timer_pop(T_VERIFY);
252
	}
Sebastian Hack's avatar
Sebastian Hack committed
253

254
255
256
	/* the ifg exists only if there are allocatable regs */
	be_ifg_free(chordal_env->ifg);

257
	/* free some always allocated data structures */
258
	pmap_destroy(chordal_env->border_heads);
259
	free(chordal_env->allocatable_regs);
260
}
261

262
263
264
/**
 * Performs chordal register allocation for each register class on given irg.
 *
265
266
267
 * @param irg    the graph
 * @return Structure containing timer for the single phases or NULL if no
 *         timing requested.
268
 */
269
static void be_ra_chordal_main(ir_graph *irg, const regalloc_if_t *regif)
270
{
271
	be_timer_push(T_RA_OTHER);
Matthias Braun's avatar
Matthias Braun committed
272

273
274
	be_spill_prepare_for_constraints(irg);

275
276
	be_chordal_env_t chordal_env;
	obstack_init(&chordal_env.obst);
277
278
279
280
281
	chordal_env.opts             = &options;
	chordal_env.irg              = irg;
	chordal_env.border_heads     = NULL;
	chordal_env.ifg              = NULL;
	chordal_env.allocatable_regs = NULL;
282

Matthias Braun's avatar
Matthias Braun committed
283
	if (stat_ev_enabled)
284
		be_collect_node_stats(&last_node_stats, irg);
Matthias Braun's avatar
Matthias Braun committed
285

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

288
	/* Perform the following for each register class. */
289
290
291
	arch_register_class_t const *const reg_classes = isa_if->register_classes;
	for (int j = 0, m = isa_if->n_register_classes; j < m; ++j) {
		arch_register_class_t const *const cls = &reg_classes[j];
292
		if (cls->flags & arch_register_class_flag_manual_ra)
293
			continue;
294

295
		stat_ev_ctx_push_str("bechordal_cls", cls->name);
296

297
		double pre_spill_cost = 0;
298
		if (stat_ev_enabled) {
299
			be_do_stat_reg_pressure(irg, cls);
300
			pre_spill_cost = be_estimate_irg_costs(irg);
301
		}
Matthias Braun's avatar
Matthias Braun committed
302

303
		pre_spill(&chordal_env, cls, irg);
Matthias Braun's avatar
Matthias Braun committed
304

305
		be_timer_push(T_RA_SPILL);
306
		be_do_spill(irg, cls, regif);
307
		be_timer_pop(T_RA_SPILL);
308
		dump(BE_CH_DUMP_SPILL, irg, cls, "spill");
309
310
		stat_ev_dbl("bechordal_spillcosts", be_estimate_irg_costs(irg) - pre_spill_cost);

311
		post_spill(&chordal_env, irg, regif);
312

313
		if (stat_ev_enabled) {
314
315
316
317
318
319
			be_node_stats_t node_stats;
			be_collect_node_stats(&node_stats, irg);
			be_subtract_node_stats(&node_stats, &last_node_stats);
			be_emit_node_stats(&node_stats, "bechordal_");
			be_copy_node_stats(&last_node_stats, &node_stats);
			stat_ev_ctx_pop("bechordal_cls");
Sebastian Hack's avatar
Sebastian Hack committed
320
		}
Sebastian Hack's avatar
Sebastian Hack committed
321
322
	}

323
	be_timer_push(T_RA_EPILOG);
324
	lower_nodes_after_ra(irg, options.lower_perm_opt == BE_CH_LOWER_PERM_COPY);
325
	dump(BE_CH_DUMP_LOWER, irg, NULL, "belower-after-ra");
326

327
	obstack_free(&chordal_env.obst, NULL);
328
	be_invalidate_live_sets(irg);
329
	be_timer_pop(T_RA_EPILOG);
330

331
	be_timer_pop(T_RA_OTHER);
Sebastian Hack's avatar
Sebastian Hack committed
332
333
}

Matthias Braun's avatar
Matthias Braun committed
334
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal_main)
335
void be_init_chordal_main(void)
336
{
Matthias Braun's avatar
Matthias Braun committed
337
338
339
340
	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");

341
	be_register_allocator("chordal", be_ra_chordal_main);
Michael Beck's avatar
Michael Beck committed
342

343
	lc_opt_add_table(chordal_grp, be_chordal_options);
Matthias Braun's avatar
Matthias Braun committed
344
345
	be_add_module_list_opt(chordal_grp, "coloring", "select coloring method",
	                       &colorings, (void**) &selected_coloring);
346
}