bestate.c 14.4 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
23
24
/**
 * @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 "config.h"

#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"
25
#include "irnodehashmap.h"
26
#include "cpset.h"
27

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

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;
56
	ir_nodehashmap_t       spill_infos;
57
58
59
60
61
62
63
64
65

	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;

66
static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block)
67
{
68
	block_info_t *res = OALLOCZ(obst, block_info_t);
69

Matthias Braun's avatar
Matthias Braun committed
70
	assert(is_Block(block));
71
72
73
74
75
76
	set_irn_link(block, res);
	mark_irn_visited(block);

	return res;
}

77
static inline block_info_t *get_block_info(ir_node *block)
78
79
80
81
82
{
	assert(irn_visited(block));
	return (block_info_t*) get_irn_link(block);
}

83
static inline spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
84
{
85
	spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t);
86
	spill_info->value = state;
87
88
	spill_info->reloads = NEW_ARR_F(ir_node*, 0);

89
	ir_nodehashmap_insert(&env->spill_infos, state, spill_info);
Matthias Braun's avatar
Matthias Braun committed
90
	//ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
91
92
93
94
95
96
97

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

	return spill_info;
}

98
static inline spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
99
{
100
	spill_info_t *spill_info = ir_nodehashmap_get(spill_info_t, &env->spill_infos, node);
Matthias Braun's avatar
Matthias Braun committed
101
102
103
104
	//ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
	return spill_info;
}

105
static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
106
107
{
	spill_info_t *spill_info;
108
109
	ir_node *next;
	ir_node *after;
110

Matthias Braun's avatar
Matthias Braun committed
111
	spill_info = get_spill_info(env, state);
112
	if (spill_info == NULL) {
113
		spill_info = create_spill_info(env, state);
114
	} else if (spill_info->spill != NULL) {
Matthias Braun's avatar
Matthias Braun committed
115
		return spill_info;
116
117
	}

118
	if (sched_is_scheduled(state)) {
119
120
121
122
		next = state;
		do {
			after = next;
			next = sched_next(after);
123
		} while (is_Phi(next) || be_is_Keep(next));
124
125
126
127
	} else {
		after = state;
	}
	spill_info->spill = env->create_spill(env->func_env, state, force, after);
128
129
130
131

	return spill_info;
}

132
133
static void create_reload(minibelady_env_t *env, ir_node *state,
                          ir_node *before, ir_node *last_state)
134
{
135
	spill_info_t *spill_info = create_spill(env, state, 0);
136
137
138
	ir_node *spill = spill_info->spill;
	ir_node *reload;

139
140
	reload = env->create_reload(env->func_env, state, spill, before,
	                            last_state);
141
142
143
	ARR_APP1(ir_node*, spill_info->reloads, reload);
}

144
static void spill_phi(minibelady_env_t *env, ir_node *phi)
145
{
146
147
148
	ir_graph     *irg           = get_irn_irg(phi);
	ir_node      *block         = get_nodes_block(phi);
	int           arity         = get_irn_arity(phi);
149
	ir_node     **phi_in        = ALLOCAN(ir_node*, arity);
150
	ir_node      *dummy         = new_r_Dummy(irg, mode_M);
151
	ir_node      *spill_to_kill = NULL;
152
	spill_info_t *spill_info;
153
	int           i;
154
155

	/* does a spill exist for the phis value? */
Matthias Braun's avatar
Matthias Braun committed
156
	spill_info = get_spill_info(env, phi);
157
	if (spill_info != NULL) {
158
159
160
161
162
163
		spill_to_kill = spill_info->spill;
	} else {
		spill_info = create_spill_info(env, phi);
	}

	/* create a new phi-M with bad preds */
164
	for (i = 0; i < arity; ++i) {
165
		phi_in[i] = dummy;
166
167
168
169
170
	}

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

	/* create a Phi-M */
171
172
	spill_info->spill = be_new_Phi(block, arity, phi_in, mode_M,
	                               arch_no_register_req);
Matthias Braun's avatar
Matthias Braun committed
173
	sched_add_after(block, spill_info->spill);
174

175
	if (spill_to_kill != NULL) {
176
177
178
179
180
		exchange(spill_to_kill, spill_info->spill);
		sched_remove(spill_to_kill);
	}

	/* create spills for the phi values */
181
	for (i = 0; i < arity; ++i) {
182
183
184
185
186
187
		ir_node *in = get_irn_n(phi, i);
		spill_info_t *pred_spill = create_spill(env, in, 1);
		set_irn_n(spill_info->spill, i, pred_spill->spill);
	}
}

188
static void belady(minibelady_env_t *env, ir_node *block);
189
190
191
192
193

/**
 * 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
194
 * The phis among the remaining values get spilled: Introduce pseudo-copies of
195
196
197
 * their args to break interference and make it possible to spill them to the
 * same spill slot.
 */
198
static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{
	block_info_t  *block_info;
	be_next_use_t  next_use;
	ir_loop       *loop;
	ir_node       *best_starter, *first;
	int            n_cfgpreds;
	unsigned       best_time;
	int            outer_loop_allowed;

	/* Create the block info for this block. */
	block_info = new_block_info(&env->obst, block);
	n_cfgpreds = get_Block_n_cfgpreds(block);

	/* no cfgpred -> no value active */
213
	if (n_cfgpreds == 0) {
214
215
216
217
218
		block_info->start_state = NULL;
		return block_info;
	}

	/* for 1 pred only: simply take the the end-state of the pred */
219
	if (n_cfgpreds == 1) {
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
		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);

		DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block, pred_info->end_state));
		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));
	first = sched_first(block);
	loop = get_irn_loop(block);
	best_starter = NULL;
	best_time = USES_INFINITY;
	outer_loop_allowed = 1;

	/* check all Phis first */
	sched_foreach(block, node) {
		if (!is_Phi(node))
			break;
245
		if (arch_get_irn_register(node) != env->reg)
246
247
248
			continue;

		DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
249
		next_use = be_get_next_use(env->uses, first, node, 0);
250

251
		if (USES_IS_INFINITE(next_use.time)) {
252
253
254
255
			DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
			continue;
		}

256
257
		if (next_use.outermost_loop >= get_loop_depth(loop)) {
			if (outer_loop_allowed || next_use.time < best_time) {
258
259
260
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));

261
				if (best_starter != NULL) {
262
263
264
265
266
267
268
269
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
				outer_loop_allowed = 0;
			}
		} else {
270
			if (outer_loop_allowed && next_use.time < best_time) {
271
272
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
273
				if (best_starter != NULL) {
274
275
276
277
278
279
280
281
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
			}
		}

282
		if (best_starter != node) {
283
284
285
286
287
288
			/* spill the phi as it is not used */
			spill_phi(env, best_starter);
		}
	}

	/* check all Live-Ins */
289
	be_lv_foreach_cls(env->lv, block, be_lv_state_in, env->reg->reg_class, node) {
290
		if (arch_get_irn_register(node) != env->reg)
291
292
293
			continue;

		DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
294
		next_use = be_get_next_use(env->uses, first, node, 0);
295

296
		if (USES_IS_INFINITE(next_use.time)) {
297
298
299
300
			DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
			continue;
		}

301
302
		if (next_use.outermost_loop >= get_loop_depth(loop)) {
			if (outer_loop_allowed || next_use.time < best_time) {
303
304
305
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));

306
				if (best_starter != NULL && is_Phi(best_starter)) {
307
308
309
310
311
312
313
314
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
				outer_loop_allowed = 0;
			}
		} else {
315
			if (outer_loop_allowed && next_use.time < best_time) {
316
317
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
318
				if (best_starter != NULL && is_Phi(best_starter)) {
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
					/* 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.
 */
338
static void belady(minibelady_env_t *env, ir_node *block)
339
340
341
342
343
{
	ir_node *current_state;
	block_info_t *block_info;

	/* Don't do a block twice */
344
	if (irn_visited(block))
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
		return;

	/* compute value to start with */
	block_info = compute_block_start_state(env, block);

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

	current_state = block_info->start_state;
	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) {
		int i, arity;
		ir_node *need_val = NULL;

364
365
		/* Phis are no real instr (see insert_starters()) */
		if (is_Phi(node))
366
367
368
369
			continue;

		/* check which state is desired for the node */
		arity = get_irn_arity(node);
370
		for (i = 0; i < arity; ++i) {
Michael Beck's avatar
Michael Beck committed
371
			const arch_register_t *reg;
372
			ir_node *in = get_irn_n(node, i);
373

374
			if (!mode_is_data(get_irn_mode(in)))
375
376
				continue;

377
			reg = arch_get_irn_register(in);
378
			if (reg == env->reg) {
379
380
381
382
383
384
				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 */
385
		if (need_val != NULL && need_val != current_state) {
386
			ir_node *before = node;
Michael Beck's avatar
Michael Beck committed
387
			DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
388
			create_reload(env, need_val, before, current_state);
389
			current_state = need_val;
390
391
392
393
394
		}

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

		/* record state changes by the node */
395
396
397
398
399
400
401
402
403
		be_foreach_value(node, value,
			if (!mode_is_data(get_irn_mode(value)))
				continue;
			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));
		);
404
405
406
407
408
409
410
	}

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

411
static void belady_walker(ir_node *block, void *data)
412
413
414
415
416
417
418
{
	belady((minibelady_env_t*) data, block);
}

/**
 * We must adapt the live-outs to the live-ins at each block-border.
 */
419
static void fix_block_borders(ir_node *block, void *data)
420
{
421
	minibelady_env_t *env = (minibelady_env_t*)data;
422
423
424
425
426
427
428
429
	int i;
	int arity;
	block_info_t *block_info;

	DBG((dbg, LEVEL_3, "\n"));

	block_info = get_block_info(block);

430
431
432
	DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
	     block_info->start_state));

433
434
435
	/* process all pred blocks */
	arity = get_irn_arity(block);
	for (i = 0; i < arity; ++i) {
436
437
438
439
		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;

440
		if (need_state == NULL)
441
442
			continue;

443
		if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
444
445
446
447
448
			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));
449

450
		if (pred_info->end_state != need_state) {
451
			DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
452
			ir_node *const insert_point = be_get_end_of_block_insertion_point(pred);
453
			create_reload(env, need_state, insert_point, pred_info->end_state);
454
455
456
457
		}
	}
}

458
void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
459
                     create_spill_func create_spill,
460
461
                     create_reload_func create_reload)
{
462
463
	minibelady_env_t env;
	spill_info_t *info;
464
	be_lv_t *lv = be_get_irg_liveness(irg);
465

466
	be_assure_live_sets(irg);
467
	assure_loopinfo(irg);
468
469
470
471
472
473

	obstack_init(&env.obst);
	env.reg           = reg;
	env.func_env      = func_env;
	env.create_spill  = create_spill;
	env.create_reload = create_reload;
474
	env.lv            = be_get_irg_liveness(irg);
475
476
	env.uses          = be_begin_uses(irg, env.lv);
	env.spills        = NULL;
477
	ir_nodehashmap_init(&env.spill_infos);
478

479
	assure_doms(irg);
480
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
481
482
483
484
485
486
487
488
	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);

489
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
490
491
492

	/* reconstruct ssa-form */
	info = env.spills;
493
	while (info != NULL) {
494
		be_ssa_construction_env_t senv;
Michael Beck's avatar
Michael Beck committed
495
		size_t i, len;
496
		ir_node **phis;
497

498
		be_ssa_construction_init(&senv, irg);
499
		if (sched_is_scheduled(info->value))
500
501
502
503
504
			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);

505
		if (lv != NULL) {
506
507
508
509
			be_ssa_construction_update_liveness_phis(&senv, lv);

			be_liveness_update(lv, info->value);
			len = ARR_LEN(info->reloads);
510
			for (i = 0; i < len; ++i) {
511
512
513
514
515
516
				ir_node *reload = info->reloads[i];
				be_liveness_update(lv, reload);
			}
		}

		phis = be_ssa_construction_get_new_phis(&senv);
517
518
519

		/* set register requirements for phis */
		len = ARR_LEN(phis);
520
		for (i = 0; i < len; ++i) {
521
			ir_node *phi = phis[i];
522
			arch_set_irn_register(phi, env.reg);
523
		}
524
		be_ssa_construction_destroy(&senv);
525
526
527
528
529

		info = info->next;
	}

	/* some nodes might be dead now. */
530
	be_remove_dead_nodes_from_schedule(irg);
531

532
	ir_nodehashmap_destroy(&env.spill_infos);
533
534
535
536
	be_end_uses(env.uses);
	obstack_free(&env.obst, NULL);
}

Matthias Braun's avatar
Matthias Braun committed
537
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state)
538
539
540
541
void be_init_state(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.state");
}