bespilldaemel.c 10.6 KB
Newer Older
Matthias Braun's avatar
Matthias Braun committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Matthias Braun's avatar
Matthias Braun committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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
Michael Beck's avatar
Michael Beck committed
22
 * @brief       Naive spilling algorithm
Matthias Braun's avatar
Matthias Braun committed
23
24
25
 * @author      Matthias Braun
 * @date        20.09.2005
 * @version     $Id: bespillbelady.c 13913 2007-05-18 12:48:56Z matze $
yb9976's avatar
yb9976 committed
26
 * @brief
27
28
29
 *   This implements a naive spilling algorithm. It is designed to produce
 *   similar effects to the spill decisions produced by traditional graph
 *   coloring register allocators that spill while they are coloring the graph.
Matthias Braun's avatar
Matthias Braun committed
30
31
32
33
34
35
 *
 *   This spiller walks over all blocks and looks for places with too high
 *   register pressure where it spills the values that are cheapest to spill.
 *   Spilling in this context means placing a spill instruction behind the
 *   definition of the value and a reload before each usage.
 */
36
37
38
39
40
41
42
43
#include "config.h"

#include "debug.h"

#include "irnodeset.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "iredges_t.h"
Matthias Braun's avatar
Matthias Braun committed
44
#include "error.h"
45
46
47

#include "beirg.h"
#include "bespill.h"
48
#include "bespillutil.h"
49
50
#include "bemodule.h"
#include "besched.h"
51
#include "bearch.h"
52
#include "be_t.h"
53
#include "benode.h"
54
#include "beirg.h"
55
#include "belive.h"
56
57
58

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

59
static spill_env_t                 *spill_env;
60
static unsigned                     n_regs;
61
62
63
static const arch_register_class_t *cls;
static const be_lv_t               *lv;
static bitset_t                    *spilled_nodes;
64
65
66
67
68
69
70

typedef struct spill_candidate_t spill_candidate_t;
struct spill_candidate_t {
	double   costs;
	ir_node *node;
};

71
static int compare_spill_candidates_desc(const void *d1, const void *d2)
72
{
73
74
	const spill_candidate_t *c1 = (const spill_candidate_t*)d1;
	const spill_candidate_t *c2 = (const spill_candidate_t*)d2;
75

Matthias Braun's avatar
Matthias Braun committed
76
	return (int) (c1->costs - c2->costs);
77
78
}

79
static double get_spill_costs(ir_node *node)
80
81
{
	const ir_edge_t *edge;
82
83
84
	ir_node         *spill_place = skip_Proj(node);
	double           costs       = be_get_spill_costs(spill_env, node,
	                                                  spill_place);
85
86
87
88

	foreach_out_edge(node, edge) {
		ir_node *use = get_edge_src_irn(edge);

89
		/* keeps should be directly below the node */
90
		if (be_is_Keep(use)) {
91
92
93
			continue;
		}

94
		if (is_Phi(use)) {
Matthias Braun's avatar
Matthias Braun committed
95
96
			int      in    = get_edge_src_pos(edge);
			ir_node *block = get_nodes_block(use);
97
98
99
100
101
102
103
104
105
106
107
108
109

			costs += be_get_reload_costs_on_edge(spill_env, node, block, in);
		} else {
			costs += be_get_reload_costs(spill_env, node, use);
		}
	}

	return costs;
}

/**
 * spills a node by placing a reload before each usage
 */
110
static void spill_node(ir_node *node)
111
112
113
{
	const ir_edge_t *edge;

Matthias Braun's avatar
Matthias Braun committed
114
115
	DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));

116
117
	foreach_out_edge(node, edge) {
		ir_node *use = get_edge_src_irn(edge);
118
		if (is_Anchor(use))
119
			continue;
120
		if (be_is_Keep(use))
121
			continue;
122

123
		if (is_Phi(use)) {
124
125
126
127
			int      in         = get_edge_src_pos(edge);
			ir_node *block      = get_nodes_block(use);

			be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
128
		} else {
129
130
131
132
			be_add_reload(spill_env, node, use, cls, 1);
		}
	}

133
	bitset_set(spilled_nodes, get_irn_idx(node));
134
135
}

136
137
138
139
140
141
static unsigned get_value_width(const ir_node *node)
{
	const arch_register_req_t *req = arch_get_register_req_out(node);
	return req->width;
}

142
143
/**
 * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
144
 * sets the spilled bits in spilled_nodes.
145
 */
146
static void do_spilling(ir_nodeset_t *live_nodes, ir_node *node)
147
{
148
149
150
151
152
	size_t                 n_live_nodes     = ir_nodeset_size(live_nodes);
	size_t                 values_defined   = 0;
	size_t                 free_regs_needed = 0;
	spill_candidate_t     *candidates;
	ir_nodeset_iterator_t  iter;
153
	int                    i, arity;
154
155
156
	int                    spills_needed;
	size_t                 cand_idx;
	ir_node               *n;
157
	ir_node               *value;
Matthias Braun's avatar
Matthias Braun committed
158

159
	be_foreach_definition(node, cls, value,
160
161
		assert(req_->width >= 1);
		values_defined += req_->width;
162
	);
163

164
	/* we need registers for the non-live argument values */
165
	arity = get_irn_arity(node);
166
	for (i = 0; i < arity; ++i) {
167
		ir_node *pred = get_irn_n(node, i);
168
		if (arch_irn_consider_in_reg_alloc(cls, pred)
169
				&& !ir_nodeset_contains(live_nodes, pred)) {
170
			free_regs_needed += get_value_width(pred);
171
		}
172
173
	}

174
	/* we can reuse all reloaded values for the defined values, but we might
175
	 * need even more registers */
176
	if (values_defined > free_regs_needed)
177
		free_regs_needed = values_defined;
178

179
	spills_needed = (n_live_nodes + free_regs_needed) - n_regs;
180
	if (spills_needed <= 0)
181
		return;
Matthias Braun's avatar
Matthias Braun committed
182
	DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
183

184
	candidates = ALLOCAN(spill_candidate_t, n_live_nodes);
185
186
187

	/* construct array with spill candidates and calculate their costs */
	i = 0;
188
	foreach_ir_nodeset(live_nodes, n, iter) {
189
190
		spill_candidate_t *candidate = & candidates[i];

191
192
		assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));

Matthias Braun's avatar
Matthias Braun committed
193
		candidate->node  = n;
194
		candidate->costs = get_spill_costs(n);
Matthias Braun's avatar
Matthias Braun committed
195
		++i;
196
	}
197
	assert(i == n_live_nodes);
198
199

	/* sort spill candidates */
200
	qsort(candidates, n_live_nodes, sizeof(candidates[0]),
201
202
203
	      compare_spill_candidates_desc);

	/* spill cheapest ones */
Matthias Braun's avatar
Matthias Braun committed
204
	cand_idx = 0;
205
	while (spills_needed > 0) {
206
207
208
		bool                       is_use = false;
		spill_candidate_t         *candidate;
		ir_node                   *cand_node;
Michael Beck's avatar
Michael Beck committed
209

210
		if (cand_idx >= n_live_nodes) {
211
			panic("can't spill enough values for node %+F", node);
Matthias Braun's avatar
Matthias Braun committed
212
213
		}

Michael Beck's avatar
Michael Beck committed
214
215
		candidate = &candidates[cand_idx];
		cand_node = candidate->node;
Matthias Braun's avatar
Matthias Braun committed
216
217
		++cand_idx;

218
		if (arch_irn_is(skip_Proj_const(cand_node), dont_spill))
219
220
221
			continue;

		/* make sure the node is not an argument of the instruction */
222
		for (i = 0; i < arity; ++i) {
Matthias Braun's avatar
Matthias Braun committed
223
			ir_node *in = get_irn_n(node, i);
224
			if (in == cand_node) {
225
				is_use = true;
Matthias Braun's avatar
Matthias Braun committed
226
227
228
				break;
			}
		}
229
		if (is_use)
Matthias Braun's avatar
Matthias Braun committed
230
			continue;
231

232
		spill_node(cand_node);
233
		ir_nodeset_remove(live_nodes, cand_node);
234
		spills_needed -= get_value_width(cand_node);
235
236
237
	}
}

Matthias Braun's avatar
Matthias Braun committed
238
/**
239
 * removes all values from the nodeset that are defined by node
Matthias Braun's avatar
Matthias Braun committed
240
 */
241
static void remove_defs(ir_node *node, ir_nodeset_t *nodeset)
Matthias Braun's avatar
Matthias Braun committed
242
{
243
	ir_node *value;
244
245
	/* You must break out of your loop when hitting the first phi function. */
	assert(!is_Phi(node));
Matthias Braun's avatar
Matthias Braun committed
246

247
248
249
	be_foreach_definition(node, cls, value,
		ir_nodeset_remove(nodeset, value);
	);
250
251
}

252
static void add_uses(ir_node *node, ir_nodeset_t *nodeset)
253
254
{
	int i, arity;
Matthias Braun's avatar
Matthias Braun committed
255

256
	arity = get_irn_arity(node);
257
	for (i = 0; i < arity; ++i) {
258
		ir_node *op = get_irn_n(node, i);
Matthias Braun's avatar
Matthias Braun committed
259

260
261
262
		if (arch_irn_consider_in_reg_alloc(cls, op) &&
				!bitset_is_set(spilled_nodes, get_irn_idx(op))) {
			ir_nodeset_insert(nodeset, op);
Matthias Braun's avatar
Matthias Braun committed
263
		}
264
	}
Matthias Braun's avatar
Matthias Braun committed
265
266
}

267
268
269
270
271
272
273
274
275
276
277
278
static __attribute__((unused))
void print_nodeset(ir_nodeset_t *nodeset)
{
	ir_nodeset_iterator_t  iter;
	ir_node               *node;

	foreach_ir_nodeset(nodeset, node, iter) {
		ir_fprintf(stderr, "%+F ", node);
	}
	fprintf(stderr, "\n");
}

279
280
281
282
/**
 * make sure register pressure in a block is always equal or below the number
 * of available registers
 */
283
static void spill_block(ir_node *block, void *data)
284
{
Matthias Braun's avatar
Matthias Braun committed
285
286
287
288
289
	ir_nodeset_t           live_nodes;
	ir_nodeset_iterator_t  iter;
	ir_node               *node;
	int                    n_phi_values_spilled;
	int                    regpressure;
290
	int                    live_nodes_pressure;
Matthias Braun's avatar
Matthias Braun committed
291
	int                    phi_spills_needed;
292
	(void) data;
293
294
295

	DBG((dbg, LEVEL_1, "spilling block %+F\n", block));

296
	/* construct set of live nodes at end of block */
297
	ir_nodeset_init(&live_nodes);
298
	be_liveness_end_of_block(lv, cls, block, &live_nodes);
299

300
	/* remove already spilled nodes from liveset */
301
	foreach_ir_nodeset(&live_nodes, node, iter) {
302
		DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
303
		if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
304
			DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
305
			ir_nodeset_remove_iterator(&live_nodes, &iter);
306
307
308
309
310
		} else {
			DBG((dbg, LEVEL_2, "keeping.\n"));
		}
	}

311
312
	/* walk schedule backwards and spill until register pressure is fine at
	 * each node */
313
	sched_foreach_reverse(block, node) {
314
		if (is_Phi(node))
315
316
			break;

317
318
319
		remove_defs(node, &live_nodes);
		do_spilling(&live_nodes, node);
		add_uses(node, &live_nodes);
320
	}
321

322
323
	/* until now only the values of some phis have been spilled the phis itself
	 * are still there and occupy registers, so we need to count them and might
324
	 * have to spill some of them. */
325
	n_phi_values_spilled = 0;
326
	sched_foreach(block, node) {
327
		if (!is_Phi(node))
328
			break;
329

330
		if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
331
			n_phi_values_spilled += get_value_width(node);
332
333
		}
	}
334

335
336
337
338
339
	live_nodes_pressure = 0;
	foreach_ir_nodeset(&live_nodes, node, iter) {
		live_nodes_pressure += get_value_width(node);
	}

340
	/* calculate how many of the phis need to be spilled */
341
	regpressure       = live_nodes_pressure + n_phi_values_spilled;
342
	phi_spills_needed = regpressure - n_regs;
343
344
	DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
	     regpressure, phi_spills_needed));
345
346
347
348

	/* spill as many phis as needed */
	/* TODO: we should really estimate costs of the phi spill as well...
	 * and preferably spill phis with lower costs... */
349
	sched_foreach(block, node) {
350
		if (!is_Phi(node))
351
			break;
352
		if (phi_spills_needed <= 0)
353
354
			break;

355
356
357
358
359
		if (!bitset_is_set(spilled_nodes, get_irn_idx(node)))
			continue;

		be_spill_phi(spill_env, node);
		phi_spills_needed -= get_value_width(node);
360
361
	}
	assert(phi_spills_needed <= 0);
362
363
364
365

	ir_nodeset_destroy(&live_nodes);
}

366
static void be_spill_daemel(ir_graph *irg, const arch_register_class_t *new_cls)
367
{
368
	n_regs = be_get_n_allocatable_regs(irg, new_cls);
369
	if (n_regs == 0)
370
371
		return;

372
	be_liveness_assure_sets(be_assure_liveness(irg));
373

374
	spill_env     = be_new_spill_env(irg);
375
	cls           = new_cls;
376
	lv            = be_get_irg_liveness(irg);
377
	spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
378

379
380
	DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));

381
	irg_block_walk_graph(irg, spill_block, NULL, NULL);
382

383
	bitset_free(spilled_nodes);
384

385
386
	be_insert_spills_reloads(spill_env);
	be_delete_spill_env(spill_env);
387
388
}

389
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_daemelspill);
390
391
392
393
394
395
396
void be_init_daemelspill(void)
{
	static be_spiller_t daemel_spiller = {
		be_spill_daemel
	};

	be_register_spiller("daemel", &daemel_spiller);
Matthias Braun's avatar
Matthias Braun committed
397
	FIRM_DBG_REGISTER(dbg, "firm.be.spilldaemel");
398
}