bestate.c 13.5 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * @file
 * @brief       Handles state switching. This is basically the belady spill
 *              algorithm optimized for the 1-register case.
 * @author      Matthias Braun
 * @date        26.03.2007
 */
#include "bestate.h"

#include "obst.h"
#include "irgraph_t.h"
#include "irnode_t.h"
#include "irgwalk.h"
#include "irloop.h"
#include "iredges_t.h"
#include "ircons_t.h"
#include "irgmod.h"
23
#include "irnodehashmap.h"
24
#include "cpset.h"
25

26
#include "bearch.h"
27
#include "beirg.h"
Christian Würdig's avatar
Christian Würdig committed
28
#include "beuses.h"
29
#include "besched.h"
Matthias Braun's avatar
Matthias Braun committed
30
#include "belive.h"
31
#include "bemodule.h"
32
#include "benode.h"
33
#include "bespillutil.h"
34
#include "bessaconstr.h"
35
#include "beutil.h"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

typedef struct spill_info_t {
	struct spill_info_t *next;
	ir_node *value;
	ir_node *spill;
	ir_node **reloads;
} spill_info_t;

typedef struct minibelady_env_t {
	struct obstack         obst;
	const arch_register_t *reg;
	const be_lv_t         *lv;
	void                  *func_env;
	create_reload_func     create_reload;
	create_spill_func      create_spill;
	spill_info_t          *spills;
54
	ir_nodehashmap_t       spill_infos;
55
56
57
58
59
60
61
62
63

	be_uses_t             *uses;           /**< env for the next-use magic */
} minibelady_env_t;

typedef struct block_info_t {
	ir_node *start_state;
	ir_node *end_state;
} block_info_t;

64
static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block)
65
{
66
	block_info_t *res = OALLOCZ(obst, block_info_t);
Matthias Braun's avatar
Matthias Braun committed
67
	assert(is_Block(block));
68
69
70
71
72
	set_irn_link(block, res);
	mark_irn_visited(block);
	return res;
}

73
static inline block_info_t *get_block_info(ir_node *block)
74
75
76
77
78
{
	assert(irn_visited(block));
	return (block_info_t*) get_irn_link(block);
}

Matthias Braun's avatar
Matthias Braun committed
79
80
static inline spill_info_t *create_spill_info(minibelady_env_t *env,
                                              ir_node *state)
81
{
82
	spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t);
83
	spill_info->value = state;
84
	spill_info->reloads = NEW_ARR_F(ir_node*, 0);
85
	ir_nodehashmap_insert(&env->spill_infos, state, spill_info);
86
87
88
89
90
91
92

	spill_info->next = env->spills;
	env->spills = spill_info;

	return spill_info;
}

Matthias Braun's avatar
Matthias Braun committed
93
94
static inline spill_info_t *get_spill_info(minibelady_env_t *env,
                                           const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
95
{
Matthias Braun's avatar
Matthias Braun committed
96
97
	spill_info_t *spill_info = ir_nodehashmap_get(spill_info_t,
	                                              &env->spill_infos, node);
Matthias Braun's avatar
Matthias Braun committed
98
99
100
	return spill_info;
}

Matthias Braun's avatar
Matthias Braun committed
101
102
static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state,
                                  bool force)
103
{
Matthias Braun's avatar
Matthias Braun committed
104
	spill_info_t *spill_info = get_spill_info(env, state);
105
	if (spill_info == NULL) {
106
		spill_info = create_spill_info(env, state);
107
	} else if (spill_info->spill != NULL) {
Matthias Braun's avatar
Matthias Braun committed
108
		return spill_info;
109
110
	}

Matthias Braun's avatar
Matthias Braun committed
111
	ir_node *after;
112
	if (sched_is_scheduled(state)) {
113
		after = be_move_after_schedule_first(state);
114
115
116
117
	} else {
		after = state;
	}
	spill_info->spill = env->create_spill(env->func_env, state, force, after);
118
119
120
121

	return spill_info;
}

122
123
static void create_reload(minibelady_env_t *env, ir_node *state,
                          ir_node *before, ir_node *last_state)
124
{
Matthias Braun's avatar
Matthias Braun committed
125
126
127
128
	spill_info_t *spill_info = create_spill(env, state, false);
	ir_node *spill  = spill_info->spill;
	ir_node *reload = env->create_reload(env->func_env, state, spill, before,
	                                     last_state);
129
130
131
	ARR_APP1(ir_node*, spill_info->reloads, reload);
}

132
static void spill_phi(minibelady_env_t *env, ir_node *phi)
133
{
134
	ir_node *spill_to_kill = NULL;
135
136

	/* does a spill exist for the phis value? */
Matthias Braun's avatar
Matthias Braun committed
137
	spill_info_t *spill_info = get_spill_info(env, phi);
138
	if (spill_info != NULL) {
139
140
141
142
143
144
145
146
		spill_to_kill = spill_info->spill;
	} else {
		spill_info = create_spill_info(env, phi);
	}

	DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));

	/* create a Phi-M */
147
148
149
150
	ir_node *const block = get_nodes_block(phi);
	ir_node *const phim  = be_new_Phi0(block, mode_M, arch_no_register_req);
	spill_info->spill = phim;
	sched_add_after(block, phim);
151

152
	if (spill_to_kill != NULL) {
153
		sched_remove(spill_to_kill);
154
		exchange(spill_to_kill, phim);
155
156
157
	}

	/* create spills for the phi values */
158
159
	unsigned  const arity  = get_irn_arity(phi);
	ir_node **const phi_in = ALLOCAN(ir_node*, arity);
160
	foreach_irn_in(phi, i, in) {
Matthias Braun's avatar
Matthias Braun committed
161
		spill_info_t *pred_spill = create_spill(env, in, true);
162
		phi_in[i] = pred_spill->spill;
163
	}
164
	be_complete_Phi(phim, arity, phi_in);
165
166
}

167
static void belady(minibelady_env_t *env, ir_node *block);
168
169
170
171
172

/**
 * Collects all values live-in at block @p block and all phi results in this
 * block.
 * Then it adds the best values (at most n_regs) to the blocks start_workset.
Michael Beck's avatar
Michael Beck committed
173
 * The phis among the remaining values get spilled: Introduce pseudo-copies of
174
175
176
 * their args to break interference and make it possible to spill them to the
 * same spill slot.
 */
Matthias Braun's avatar
Matthias Braun committed
177
178
static block_info_t *compute_block_start_state(minibelady_env_t *env,
                                               ir_node *block)
179
180
{
	/* Create the block info for this block. */
Matthias Braun's avatar
Matthias Braun committed
181
182
	block_info_t *block_info = new_block_info(&env->obst, block);
	int           n_cfgpreds = get_Block_n_cfgpreds(block);
183
184

	/* no cfgpred -> no value active */
185
	if (n_cfgpreds == 0) {
186
187
188
189
190
		block_info->start_state = NULL;
		return block_info;
	}

	/* for 1 pred only: simply take the the end-state of the pred */
191
	if (n_cfgpreds == 1) {
192
193
194
195
196
197
198
199
		ir_node *pred_block = get_Block_cfgpred_block(block, 0);
		block_info_t *pred_info;

		/* process pred block */
		belady(env, pred_block);

		pred_info = get_block_info(pred_block);

Matthias Braun's avatar
Matthias Braun committed
200
201
		DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block,
		     pred_info->end_state));
202
203
204
205
206
207
		block_info->start_state = pred_info->end_state;
		return block_info;
	}

	/* Collect all values living at start of block */
	DBG((dbg, LEVEL_2, "Living at start of %+F:\n", block));
Matthias Braun's avatar
Matthias Braun committed
208
209
210
211
212
	ir_node *first        = sched_first(block);
	ir_loop *loop         = get_irn_loop(block);
	ir_node *best_starter = NULL;
	unsigned best_time    = USES_INFINITY;
	bool     outer_loop_allowed = true;
213
214
215
216
217

	/* check all Phis first */
	sched_foreach(block, node) {
		if (!is_Phi(node))
			break;
218
		if (arch_get_irn_register(node) != env->reg)
219
220
221
			continue;

		DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
Matthias Braun's avatar
Matthias Braun committed
222
		be_next_use_t next_use = be_get_next_use(env->uses, first, node, 0);
223

224
		if (USES_IS_INFINITE(next_use.time)) {
225
226
227
228
			DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
			continue;
		}

229
230
		if (next_use.outermost_loop >= get_loop_depth(loop)) {
			if (outer_loop_allowed || next_use.time < best_time) {
231
232
233
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));

234
				if (best_starter != NULL) {
235
236
237
238
239
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
Matthias Braun's avatar
Matthias Braun committed
240
				outer_loop_allowed = false;
241
242
			}
		} else {
243
			if (outer_loop_allowed && next_use.time < best_time) {
244
245
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
246
				if (best_starter != NULL) {
247
248
249
250
251
252
253
254
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
			}
		}

255
		if (best_starter != node) {
256
257
258
259
260
261
			/* spill the phi as it is not used */
			spill_phi(env, best_starter);
		}
	}

	/* check all Live-Ins */
262
	be_lv_foreach_cls(env->lv, block, be_lv_state_in, env->reg->cls, node) {
263
		if (arch_get_irn_register(node) != env->reg)
264
265
266
			continue;

		DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
Matthias Braun's avatar
Matthias Braun committed
267
		be_next_use_t next_use = be_get_next_use(env->uses, first, node, 0);
268

269
		if (USES_IS_INFINITE(next_use.time)) {
270
271
272
273
			DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
			continue;
		}

274
275
		if (next_use.outermost_loop >= get_loop_depth(loop)) {
			if (outer_loop_allowed || next_use.time < best_time) {
276
277
278
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));

279
				if (best_starter != NULL && is_Phi(best_starter)) {
280
281
282
283
284
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
Matthias Braun's avatar
Matthias Braun committed
285
				outer_loop_allowed = false;
286
287
			}
		} else {
288
			if (outer_loop_allowed && next_use.time < best_time) {
289
290
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
291
				if (best_starter != NULL && is_Phi(best_starter)) {
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
			}
		}
	}

	block_info->start_state = best_starter;

	return block_info;
}

/**
 * For the given block @p block, decide for each values
 * whether it is used from a register or is reloaded
 * before the use.
 */
311
static void belady(minibelady_env_t *env, ir_node *block)
312
313
{
	/* Don't do a block twice */
314
	if (irn_visited(block))
315
316
317
		return;

	/* compute value to start with */
Matthias Braun's avatar
Matthias Braun committed
318
	block_info_t *block_info = compute_block_start_state(env, block);
319
320
321
322
323

	/* get the starting workset for this block */
	DBG((dbg, LEVEL_3, "\n"));
	DBG((dbg, LEVEL_3, "Decide for %+F\n", block));

Matthias Braun's avatar
Matthias Braun committed
324
	ir_node *current_state = block_info->start_state;
325
326
327
328
329
330
	DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));

	/* process the block from start to end */
	DBG((dbg, LEVEL_3, "Processing...\n"));

	sched_foreach(block, node) {
331
332
		/* Phis are no real instr (see insert_starters()) */
		if (is_Phi(node))
333
334
335
			continue;

		/* check which state is desired for the node */
Matthias Braun's avatar
Matthias Braun committed
336
		ir_node *need_val = NULL;
337
		foreach_irn_in(node, i, in) {
Matthias Braun's avatar
Matthias Braun committed
338
			const arch_register_t *reg = arch_get_irn_register(in);
339
			if (reg == env->reg) {
340
341
342
343
344
345
				assert(need_val == NULL);
				need_val = in;
				DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
			}
		}
		/* create a reload to match state if necessary */
346
		if (need_val != NULL && need_val != current_state) {
347
			ir_node *before = node;
Michael Beck's avatar
Michael Beck committed
348
			DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
349
			create_reload(env, need_val, before, current_state);
350
			current_state = need_val;
351
352
353
354
355
		}

		DBG((dbg, LEVEL_3, "  ...%+F\n", node));

		/* record state changes by the node */
356
357
358
359
360
361
362
		be_foreach_value(node, value,
			arch_register_t const *const reg = arch_get_irn_register(value);
			if (reg != env->reg)
				continue;
			current_state = value;
			DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
		);
363
364
365
366
367
368
369
	}

	/* Remember end-workset for this block */
	block_info->end_state = current_state;
	DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
}

370
static void belady_walker(ir_node *block, void *data)
371
372
373
374
375
376
377
{
	belady((minibelady_env_t*) data, block);
}

/**
 * We must adapt the live-outs to the live-ins at each block-border.
 */
378
static void fix_block_borders(ir_node *block, void *data)
379
{
Matthias Braun's avatar
Matthias Braun committed
380
381
	minibelady_env_t *env        = (minibelady_env_t*)data;
	block_info_t     *block_info = get_block_info(block);
382
383

	DBG((dbg, LEVEL_3, "\n"));
384
385
386
	DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
	     block_info->start_state));

387
	/* process all pred blocks */
Matthias Braun's avatar
Matthias Braun committed
388
	for (int i = 0, arity = get_irn_arity(block); i < arity; ++i) {
389
390
391
392
		ir_node      *pred       = get_Block_cfgpred_block(block, i);
		block_info_t *pred_info  = get_block_info(pred);
		ir_node      *need_state = block_info->start_state;

393
		if (need_state == NULL)
394
395
			continue;

396
		if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
397
398
399
400
401
			need_state = get_irn_n(need_state, i);
		}

		DBG((dbg, LEVEL_3, "  Pred %+F (ends in %+F, we need %+F)\n", pred,
		     pred_info->end_state, need_state));
402

403
		if (pred_info->end_state != need_state) {
404
			DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
405
			ir_node *const insert_point = be_get_end_of_block_insertion_point(pred);
406
			create_reload(env, need_state, insert_point, pred_info->end_state);
407
408
409
410
		}
	}
}

411
void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
412
                     create_spill_func create_spill,
413
414
                     create_reload_func create_reload)
{
415
416
	assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO
	                         | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
417
	be_assure_live_sets(irg);
418
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
419

Matthias Braun's avatar
Matthias Braun committed
420
	minibelady_env_t env;
421
422
423
424
425
	obstack_init(&env.obst);
	env.reg           = reg;
	env.func_env      = func_env;
	env.create_spill  = create_spill;
	env.create_reload = create_reload;
426
	env.lv            = be_get_irg_liveness(irg);
427
428
	env.uses          = be_begin_uses(irg, env.lv);
	env.spills        = NULL;
429
	ir_nodehashmap_init(&env.spill_infos);
430
431
432
433
434
435
436
437
438

	inc_irg_visited(irg);

	/* process blocks */
	irg_block_walk_graph(irg, NULL, belady_walker, &env);

	/* fix block end_states that don't match the next blocks start_state */
	irg_block_walk_graph(irg, fix_block_borders, NULL, &env);

439
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
440
441

	/* reconstruct ssa-form */
Matthias Braun's avatar
Matthias Braun committed
442
	spill_info_t *info = env.spills;
443
	while (info != NULL) {
444
		be_ssa_construction_env_t senv;
445
		be_ssa_construction_init(&senv, irg);
446
		if (sched_is_scheduled(info->value))
447
448
449
450
451
			be_ssa_construction_add_copy(&senv, info->value);
		be_ssa_construction_add_copies(&senv,
		                               info->reloads, ARR_LEN(info->reloads));
		be_ssa_construction_fix_users(&senv, info->value);

Matthias Braun's avatar
Matthias Braun committed
452
		ir_node **phis = be_ssa_construction_get_new_phis(&senv);
453
454

		/* set register requirements for phis */
Matthias Braun's avatar
Matthias Braun committed
455
		for (size_t i = 0, len = ARR_LEN(phis); i < len; ++i) {
456
			ir_node *phi = phis[i];
457
			arch_set_irn_register(phi, env.reg);
458
		}
459
		be_ssa_construction_destroy(&senv);
460
461
462
463
464

		info = info->next;
	}

	/* some nodes might be dead now. */
465
	be_remove_dead_nodes_from_schedule(irg);
466

467
	ir_nodehashmap_destroy(&env.spill_infos);
468
469
	be_end_uses(env.uses);
	obstack_free(&env.obst, NULL);
470
	be_invalidate_live_sets(irg);
471
472
}

Matthias Braun's avatar
Matthias Braun committed
473
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state)
474
475
476
477
void be_init_state(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.state");
}