bestate.c 16.2 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
39
/**
 * @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
 * @version     $Id$
 */
#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
40
41
42
#include "irnodeset.h"
#include "irnodemap.h"
#include "adt/cpset.h"
43

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

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;
Matthias Braun's avatar
Matthias Braun committed
70
	ir_nodemap_t           spill_infos;
71
72
73
74
75
76
77
78
79

	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;

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

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

	return res;
}

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

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

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

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

	return spill_info;
}

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

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

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

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

	return spill_info;
}

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

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

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

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

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

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

	/* create a Phi-M */
186
	spill_info->spill = be_new_Phi(block, arity, phi_in, mode_M, NULL);
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
228
{
	block_info_t  *block_info;
	be_next_use_t  next_use;
	ir_loop       *loop;
	ir_node       *best_starter, *first;
	ir_node       *node;
	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 */
229
	if (n_cfgpreds == 0) {
230
231
232
233
234
		block_info->start_state = NULL;
		return block_info;
	}

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

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

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

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

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

298
		if (best_starter != node) {
299
300
301
302
303
304
305
306
307
			/* 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) {
		node = be_lv_get_irn(env->lv, block, i);

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

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

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

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

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

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

	/* Don't do a block twice */
366
	if (irn_visited(block))
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
392
393
		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);
394
		for (i = 0; i < arity; ++i) {
Michael Beck's avatar
Michael Beck committed
395
			const arch_register_t *reg;
396
			ir_node *in = get_irn_n(node, i);
397

398
			if (!mode_is_data(get_irn_mode(in)))
399
400
				continue;

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

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

		/* record state changes by the node */
		if (get_irn_mode(node) == mode_T) {
420
421
422
			const ir_edge_t *edge;

			foreach_out_edge(node, edge) {
Michael Beck's avatar
Michael Beck committed
423
				const arch_register_t *reg;
424
				ir_node *proj = get_edge_src_irn(edge);
425

426
				if (!mode_is_data(get_irn_mode(proj)))
427
428
					continue;

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

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

451
static void belady_walker(ir_node *block, void *data)
452
453
454
455
{
	belady((minibelady_env_t*) data, block);
}

456
static ir_node *get_end_of_block_insertion_point(ir_node *block)
457
458
459
{
	ir_node *last = sched_last(block);

460
	/* skip Projs and Keep-alikes behind the jump... */
461
	while (is_Proj(last) || be_is_Keep(last)) {
462
463
464
		last = sched_prev(last);
	}

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

	return last;
}

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

486
	if (block == startblock)
487
488
489
490
491
492
		return;

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

	block_info = get_block_info(block);

493
494
495
	DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
	     block_info->start_state));

496
497
498
	/* process all pred blocks */
	arity = get_irn_arity(block);
	for (i = 0; i < arity; ++i) {
499
500
501
502
		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;

503
		if (need_state == NULL)
504
505
			continue;

506
		if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
507
508
509
510
511
			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));
512

513
		if (pred_info->end_state != need_state) {
514
			ir_node *insert_point = get_end_of_block_insertion_point(pred);
515
516


517
			DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
518
			create_reload(env, need_state, insert_point, pred_info->end_state);
519
520
521
522
		}
	}
}

523
void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
524
                     create_spill_func create_spill,
525
526
                     create_reload_func create_reload)
{
527
528
	minibelady_env_t env;
	spill_info_t *info;
529
	be_lv_t *lv = be_assure_liveness(irg);
530

Sebastian Hack's avatar
Sebastian Hack committed
531
	be_liveness_assure_sets(lv);
532
	/* construct control flow loop tree */
533
	if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
534
535
536
537
538
539
540
541
		construct_cf_backedges(irg);
	}

	obstack_init(&env.obst);
	env.reg           = reg;
	env.func_env      = func_env;
	env.create_spill  = create_spill;
	env.create_reload = create_reload;
542
	env.lv            = be_get_irg_liveness(irg);
543
544
	env.uses          = be_begin_uses(irg, env.lv);
	env.spills        = NULL;
Matthias Braun's avatar
Matthias Braun committed
545
	ir_nodemap_init(&env.spill_infos);
546

547
	assure_doms(irg);
548
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
549
550
551
552
553
554
555
556
	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);

557
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
558
559
560

	/* reconstruct ssa-form */
	info = env.spills;
561
	while (info != NULL) {
562
		be_ssa_construction_env_t senv;
Michael Beck's avatar
Michael Beck committed
563
		size_t i, len;
564
		ir_node **phis;
565

566
		be_ssa_construction_init(&senv, irg);
567
		if (sched_is_scheduled(info->value))
568
569
570
571
572
			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);

573
		if (lv != NULL) {
574
575
576
577
			be_ssa_construction_update_liveness_phis(&senv, lv);

			be_liveness_update(lv, info->value);
			len = ARR_LEN(info->reloads);
578
			for (i = 0; i < len; ++i) {
579
580
581
582
583
584
				ir_node *reload = info->reloads[i];
				be_liveness_update(lv, reload);
			}
		}

		phis = be_ssa_construction_get_new_phis(&senv);
585
586
587

		/* set register requirements for phis */
		len = ARR_LEN(phis);
588
		for (i = 0; i < len; ++i) {
589
			ir_node *phi = phis[i];
590
			arch_set_irn_register(phi, env.reg);
591
		}
592
		be_ssa_construction_destroy(&senv);
593
594
595
596
597

		info = info->next;
	}

	/* some nodes might be dead now. */
598
	be_remove_dead_nodes_from_schedule(irg);
599

Matthias Braun's avatar
Matthias Braun committed
600
	ir_nodemap_destroy(&env.spill_infos);
601
602
603
604
	be_end_uses(env.uses);
	obstack_free(&env.obst, NULL);
}

Matthias Braun's avatar
Matthias Braun committed
605
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state)
606
607
608
609
void be_init_state(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.state");
}