bestate.c 16 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * @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"
Matthias Braun's avatar
Matthias Braun committed
39
#include "irnodeset.h"
40
#include "irnodehashmap.h"
Matthias Braun's avatar
Matthias Braun committed
41
#include "adt/cpset.h"
42

43
#include "bearch.h"
Christian Würdig's avatar
Christian Würdig committed
44
#include "beuses.h"
45
#include "besched.h"
46
47
#include "belive_t.h"
#include "bemodule.h"
48
#include "benode.h"
Matthias Braun's avatar
Matthias Braun committed
49
#include "beirgmod.h"
50
#include "bessaconstr.h"
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

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;
69
	ir_nodehashmap_t       spill_infos;
70
71
72
73
74
75
76
77
78

	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;

79
static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block)
80
{
81
	block_info_t *res = OALLOCZ(obst, block_info_t);
82

Matthias Braun's avatar
Matthias Braun committed
83
	assert(is_Block(block));
84
85
86
87
88
89
	set_irn_link(block, res);
	mark_irn_visited(block);

	return res;
}

90
static inline block_info_t *get_block_info(ir_node *block)
91
92
93
94
95
{
	assert(irn_visited(block));
	return (block_info_t*) get_irn_link(block);
}

96
static inline spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
97
{
98
	spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t);
99
	spill_info->value = state;
100
101
	spill_info->reloads = NEW_ARR_F(ir_node*, 0);

102
	ir_nodehashmap_insert(&env->spill_infos, state, spill_info);
Matthias Braun's avatar
Matthias Braun committed
103
	//ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
104
105
106
107
108
109
110

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

	return spill_info;
}

111
static inline spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
112
113
{
	spill_info_t *spill_info
114
		= (spill_info_t*) ir_nodehashmap_get(&env->spill_infos, node);
Matthias Braun's avatar
Matthias Braun committed
115
116
117
118
	//ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
	return spill_info;
}

119
static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
120
121
{
	spill_info_t *spill_info;
122
123
	ir_node *next;
	ir_node *after;
124

Matthias Braun's avatar
Matthias Braun committed
125
	spill_info = get_spill_info(env, state);
126
	if (spill_info == NULL) {
127
		spill_info = create_spill_info(env, state);
128
	} else if (spill_info->spill != NULL) {
Matthias Braun's avatar
Matthias Braun committed
129
		return spill_info;
130
131
	}

132
	if (sched_is_scheduled(state)) {
133
134
135
136
		next = state;
		do {
			after = next;
			next = sched_next(after);
137
		} while (is_Proj(next) || is_Phi(next) || be_is_Keep(next));
138
139
140
141
	} else {
		after = state;
	}
	spill_info->spill = env->create_spill(env->func_env, state, force, after);
142
143
144
145

	return spill_info;
}

146
147
static void create_reload(minibelady_env_t *env, ir_node *state,
                          ir_node *before, ir_node *last_state)
148
{
149
	spill_info_t *spill_info = create_spill(env, state, 0);
150
151
152
	ir_node *spill = spill_info->spill;
	ir_node *reload;

153
154
	reload = env->create_reload(env->func_env, state, spill, before,
	                            last_state);
155
156
157
	ARR_APP1(ir_node*, spill_info->reloads, reload);
}

158
static void spill_phi(minibelady_env_t *env, ir_node *phi)
159
{
160
161
162
	ir_graph     *irg           = get_irn_irg(phi);
	ir_node      *block         = get_nodes_block(phi);
	int           arity         = get_irn_arity(phi);
163
	ir_node     **phi_in        = ALLOCAN(ir_node*, arity);
164
	ir_node      *dummy         = new_r_Dummy(irg, mode_M);
165
	ir_node      *spill_to_kill = NULL;
166
	spill_info_t *spill_info;
167
	int           i;
168
169

	/* does a spill exist for the phis value? */
Matthias Braun's avatar
Matthias Braun committed
170
	spill_info = get_spill_info(env, phi);
171
	if (spill_info != NULL) {
172
173
174
175
176
177
		spill_to_kill = spill_info->spill;
	} else {
		spill_info = create_spill_info(env, phi);
	}

	/* create a new phi-M with bad preds */
178
	for (i = 0; i < arity; ++i) {
179
		phi_in[i] = dummy;
180
181
182
183
184
	}

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

	/* create a Phi-M */
185
186
	spill_info->spill = be_new_Phi(block, arity, phi_in, mode_M,
	                               arch_no_register_req);
Matthias Braun's avatar
Matthias Braun committed
187
	sched_add_after(block, spill_info->spill);
188

189
	if (spill_to_kill != NULL) {
190
191
192
193
194
		exchange(spill_to_kill, spill_info->spill);
		sched_remove(spill_to_kill);
	}

	/* create spills for the phi values */
195
	for (i = 0; i < arity; ++i) {
196
197
198
199
200
201
		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);
	}
}

202
static void belady(minibelady_env_t *env, ir_node *block);
203
204
205
206
207

/**
 * 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
208
 * The phis among the remaining values get spilled: Introduce pseudo-copies of
209
210
211
 * their args to break interference and make it possible to spill them to the
 * same spill slot.
 */
212
static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
{
	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;
	int            i;

	/* 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 */
228
	if (n_cfgpreds == 0) {
229
230
231
232
233
		block_info->start_state = NULL;
		return block_info;
	}

	/* for 1 pred only: simply take the the end-state of the pred */
234
	if (n_cfgpreds == 1) {
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
		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;
260
		if (arch_get_irn_register(node) != env->reg)
261
262
263
			continue;

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

266
		if (USES_IS_INFINITE(next_use.time)) {
267
268
269
270
			DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
			continue;
		}

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

276
				if (best_starter != NULL) {
277
278
279
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;
				outer_loop_allowed = 0;
			}
		} else {
285
			if (outer_loop_allowed && next_use.time < best_time) {
286
287
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
288
				if (best_starter != NULL) {
289
290
291
292
293
294
295
296
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
			}
		}

297
		if (best_starter != node) {
298
299
300
301
302
303
304
			/* spill the phi as it is not used */
			spill_phi(env, best_starter);
		}
	}

	/* check all Live-Ins */
	be_lv_foreach(env->lv, block, be_lv_state_in, i) {
305
		ir_node *const node = be_lv_get_irn(env->lv, block, i);
306

307
		if (!mode_is_data(get_irn_mode(node)))
Matthias Braun's avatar
Matthias Braun committed
308
309
			continue;

310
		if (arch_get_irn_register(node) != env->reg)
311
312
313
			continue;

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

316
		if (USES_IS_INFINITE(next_use.time)) {
317
318
319
320
			DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
			continue;
		}

321
322
		if (next_use.outermost_loop >= get_loop_depth(loop)) {
			if (outer_loop_allowed || next_use.time < best_time) {
323
324
325
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));

326
				if (best_starter != NULL && is_Phi(best_starter)) {
327
328
329
330
331
332
333
334
					/* 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 {
335
			if (outer_loop_allowed && next_use.time < best_time) {
336
337
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
338
				if (best_starter != NULL && is_Phi(best_starter)) {
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
					/* 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.
 */
358
static void belady(minibelady_env_t *env, ir_node *block)
359
360
361
362
363
{
	ir_node *current_state;
	block_info_t *block_info;

	/* Don't do a block twice */
364
	if (irn_visited(block))
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
		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;

		/* projs are handled with the tuple value.
		 * Phis are no real instr (see insert_starters()) */
		if (is_Proj(node) || is_Phi(node)) {
			continue;
		}

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

396
			if (!mode_is_data(get_irn_mode(in)))
397
398
				continue;

399
			reg = arch_get_irn_register(in);
400
			if (reg == env->reg) {
401
402
403
404
405
406
				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 */
407
		if (need_val != NULL && need_val != current_state) {
408
			ir_node *before = node;
Michael Beck's avatar
Michael Beck committed
409
			DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
410
			create_reload(env, need_val, before, current_state);
411
			current_state = need_val;
412
413
414
415
416
417
		}

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

		/* record state changes by the node */
		if (get_irn_mode(node) == mode_T) {
418
			foreach_out_edge(node, edge) {
Michael Beck's avatar
Michael Beck committed
419
				const arch_register_t *reg;
420
				ir_node *proj = get_edge_src_irn(edge);
421

422
				if (!mode_is_data(get_irn_mode(proj)))
423
424
					continue;

425
				reg = arch_get_irn_register(proj);
426
				if (reg == env->reg) {
427
428
429
430
431
					current_state = proj;
					DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
				}
			}
		} else {
432
			if (mode_is_data(get_irn_mode(node))) {
433
				const arch_register_t *reg = arch_get_irn_register(node);
434
				if (reg == env->reg) {
435
436
437
					current_state = node;
					DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
				}
438
439
440
441
442
443
444
445
446
			}
		}
	}

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

447
static void belady_walker(ir_node *block, void *data)
448
449
450
451
{
	belady((minibelady_env_t*) data, block);
}

452
static ir_node *get_end_of_block_insertion_point(ir_node *block)
453
454
455
{
	ir_node *last = sched_last(block);

456
	/* skip Projs and Keep-alikes behind the jump... */
457
	while (is_Proj(last) || be_is_Keep(last)) {
458
459
460
		last = sched_prev(last);
	}

461
	if (!is_cfop(last)) {
462
		last = sched_next(last);
463
		/* last node must be a cfop, only exception is the start block */
464
		assert(last == get_irg_start_block(get_irn_irg(block)));
465
466
467
468
469
470
471
472
	}

	return last;
}

/**
 * We must adapt the live-outs to the live-ins at each block-border.
 */
473
static void fix_block_borders(ir_node *block, void *data)
474
{
475
	minibelady_env_t *env = (minibelady_env_t*)data;
476
477
478
479
480
481
	ir_graph *irg = get_irn_irg(block);
	ir_node *startblock = get_irg_start_block(irg);
	int i;
	int arity;
	block_info_t *block_info;

482
	if (block == startblock)
483
484
485
486
487
488
		return;

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

	block_info = get_block_info(block);

489
490
491
	DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
	     block_info->start_state));

492
493
494
	/* process all pred blocks */
	arity = get_irn_arity(block);
	for (i = 0; i < arity; ++i) {
495
496
497
498
		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;

499
		if (need_state == NULL)
500
501
			continue;

502
		if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
503
504
505
506
507
			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));
508

509
		if (pred_info->end_state != need_state) {
510
			ir_node *insert_point = get_end_of_block_insertion_point(pred);
511
512


513
			DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
514
			create_reload(env, need_state, insert_point, pred_info->end_state);
515
516
517
518
		}
	}
}

519
void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
520
                     create_spill_func create_spill,
521
522
                     create_reload_func create_reload)
{
523
524
	minibelady_env_t env;
	spill_info_t *info;
525
	be_lv_t *lv = be_get_irg_liveness(irg);
526

527
	be_assure_live_sets(irg);
528
	assure_loopinfo(irg);
529
530
531
532
533
534

	obstack_init(&env.obst);
	env.reg           = reg;
	env.func_env      = func_env;
	env.create_spill  = create_spill;
	env.create_reload = create_reload;
535
	env.lv            = be_get_irg_liveness(irg);
536
537
	env.uses          = be_begin_uses(irg, env.lv);
	env.spills        = NULL;
538
	ir_nodehashmap_init(&env.spill_infos);
539

540
	assure_doms(irg);
541
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
542
543
544
545
546
547
548
549
	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);

550
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
551
552
553

	/* reconstruct ssa-form */
	info = env.spills;
554
	while (info != NULL) {
555
		be_ssa_construction_env_t senv;
Michael Beck's avatar
Michael Beck committed
556
		size_t i, len;
557
		ir_node **phis;
558

559
		be_ssa_construction_init(&senv, irg);
560
		if (sched_is_scheduled(info->value))
561
562
563
564
565
			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);

566
		if (lv != NULL) {
567
568
569
570
			be_ssa_construction_update_liveness_phis(&senv, lv);

			be_liveness_update(lv, info->value);
			len = ARR_LEN(info->reloads);
571
			for (i = 0; i < len; ++i) {
572
573
574
575
576
577
				ir_node *reload = info->reloads[i];
				be_liveness_update(lv, reload);
			}
		}

		phis = be_ssa_construction_get_new_phis(&senv);
578
579
580

		/* set register requirements for phis */
		len = ARR_LEN(phis);
581
		for (i = 0; i < len; ++i) {
582
			ir_node *phi = phis[i];
583
			arch_set_irn_register(phi, env.reg);
584
		}
585
		be_ssa_construction_destroy(&senv);
586
587
588
589
590

		info = info->next;
	}

	/* some nodes might be dead now. */
591
	be_remove_dead_nodes_from_schedule(irg);
592

593
	ir_nodehashmap_destroy(&env.spill_infos);
594
595
596
597
	be_end_uses(env.uses);
	obstack_free(&env.obst, NULL);
}

Matthias Braun's avatar
Matthias Braun committed
598
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state)
599
600
601
602
void be_init_state(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.state");
}