bestate.c 16.1 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 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
49
#include "belive_t.h"
#include "bemodule.h"
#include "benode_t.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;
Matthias Braun's avatar
Matthias Braun committed
69
	ir_nodemap_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
80
81
82
83
84
block_info_t *new_block_info(struct obstack *obst, ir_node *block)
{
	block_info_t *res = obstack_alloc(obst, sizeof(*res));
	memset(res, 0, sizeof(res[0]));

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

	return res;
}

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

99
static inline
100
spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
101
102
103
{
	spill_info_t *spill_info = obstack_alloc(&env->obst, sizeof(spill_info[0]));
	memset(spill_info, 0, sizeof(spill_info[0]));
104
	spill_info->value = state;
105
106
	spill_info->reloads = NEW_ARR_F(ir_node*, 0);

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

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

	return spill_info;
}

116
static inline
Matthias Braun's avatar
Matthias Braun committed
117
118
119
120
121
122
123
124
spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
{
	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;
}

125
static
126
spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
127
128
{
	spill_info_t *spill_info;
129
130
	ir_node *next;
	ir_node *after;
131

Matthias Braun's avatar
Matthias Braun committed
132
133
	spill_info = get_spill_info(env, state);
	if(spill_info == NULL) {
134
		spill_info = create_spill_info(env, state);
Matthias Braun's avatar
Matthias Braun committed
135
136
	} else if(spill_info->spill != NULL) {
		return spill_info;
137
138
	}

139
140
141
142
143
144
145
146
147
148
	if(sched_is_scheduled(state)) {
		next = state;
		do {
			after = next;
			next = sched_next(after);
		} while(is_Proj(next) || is_Phi(next) || be_is_Keep(next));
	} else {
		after = state;
	}
	spill_info->spill = env->create_spill(env->func_env, state, force, after);
149
150
151
152
153

	return spill_info;
}

static
154
155
void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before,
                   ir_node *last_state)
156
{
157
	spill_info_t *spill_info = create_spill(env, state, 0);
158
159
160
	ir_node *spill = spill_info->spill;
	ir_node *reload;

161
162
	reload = env->create_reload(env->func_env, state, spill, before,
	                            last_state);
163
164
165
166
167
168
	ARR_APP1(ir_node*, spill_info->reloads, reload);
}

static
void spill_phi(minibelady_env_t *env, ir_node *phi)
{
169
170
171
172
173
	ir_graph     *irg           = get_irn_irg(phi);
	ir_node      *block         = get_nodes_block(phi);
	int           arity         = get_irn_arity(phi);
	ir_node     **in            = ALLOCAN(ir_node*, arity);
	ir_node      *spill_to_kill = NULL;
174
	spill_info_t *spill_info;
175
	int           i;
176
177

	/* does a spill exist for the phis value? */
Matthias Braun's avatar
Matthias Braun committed
178
179
	spill_info = get_spill_info(env, phi);
	if(spill_info != NULL) {
180
181
182
183
184
185
186
		spill_to_kill = spill_info->spill;
	} else {
		spill_info = create_spill_info(env, phi);
	}

	/* create a new phi-M with bad preds */
	for(i = 0; i < arity; ++i) {
187
		in[i] = new_r_Unknown(irg, mode_M);
188
189
190
191
192
	}

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

	/* create a Phi-M */
193
	spill_info->spill = new_r_Phi(block, arity, in, mode_M);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

	if(spill_to_kill != NULL) {
		exchange(spill_to_kill, spill_info->spill);
		sched_remove(spill_to_kill);
	}

	/* create spills for the phi values */
	for(i = 0; i < arity; ++i) {
		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);
	}
}

static
void belady(minibelady_env_t *env, ir_node *block);

/**
 * 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.
 * The phis among the remaining values get spilled: Introduce psudo-copies of
 * their args to break interference and make it possible to spill them to the
 * same spill slot.
 */
static
block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
{
	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 */
	if(n_cfgpreds == 0) {
		block_info->start_state = NULL;
		return block_info;
	}

	/* for 1 pred only: simply take the the end-state of the pred */
	if(n_cfgpreds == 1) {
		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;
269
		if (arch_get_irn_register(node) != env->reg)
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
			continue;

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

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

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

				if(best_starter != NULL) {
					/* 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 {
			if(outer_loop_allowed && next_use.time < best_time) {
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
				if(best_starter != NULL) {
					/* spill the phi as it is not used */
					spill_phi(env, best_starter);
				}
				best_starter = node;
				best_time = next_use.time;
			}
		}

		if(best_starter != node) {
			/* 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);

Matthias Braun's avatar
Matthias Braun committed
316
317
318
		if(!mode_is_data(get_irn_mode(node)))
			continue;

319
		if (arch_get_irn_register(node) != env->reg)
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
392
393
394
395
396
397
398
399
400
401
402
403
			continue;

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

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

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

				if(best_starter != NULL && is_Phi(best_starter)) {
					/* 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 {
			if(outer_loop_allowed && next_use.time < best_time) {
				DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
				     next_use.outermost_loop));
				if(best_starter != NULL && is_Phi(best_starter)) {
					/* 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.
 */
static
void belady(minibelady_env_t *env, ir_node *block)
{
	ir_node *current_state;
	ir_node *node;
	block_info_t *block_info;

	/* Don't do a block twice */
	if(irn_visited(block))
		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);
		for(i = 0; i < arity; ++i) {
Michael Beck's avatar
Michael Beck committed
404
			const arch_register_t *reg;
405
			ir_node *in = get_irn_n(node, i);
406
407
408
409

			if(!mode_is_data(get_irn_mode(in)))
				continue;

410
			reg = arch_get_irn_register(in);
411
412
413
414
415
416
417
418
			if(reg == env->reg) {
				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 */
		if(need_val != NULL && need_val != current_state) {
419
420
421
			DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
			create_reload(env, need_val, node, current_state);
			current_state = need_val;
422
423
424
425
426
427
		}

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

		/* record state changes by the node */
		if (get_irn_mode(node) == mode_T) {
428
429
430
			const ir_edge_t *edge;

			foreach_out_edge(node, edge) {
Michael Beck's avatar
Michael Beck committed
431
				const arch_register_t *reg;
432
				ir_node *proj = get_edge_src_irn(edge);
433
434
435
436

				if(!mode_is_data(get_irn_mode(proj)))
					continue;

437
				reg = arch_get_irn_register(proj);
438
439
440
441
442
443
				if(reg == env->reg) {
					current_state = proj;
					DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
				}
			}
		} else {
444
			if(mode_is_data(get_irn_mode(node))) {
445
				const arch_register_t *reg = arch_get_irn_register(node);
446
447
448
449
				if(reg == env->reg) {
					current_state = node;
					DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
				}
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
			}
		}
	}

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

static
void belady_walker(ir_node *block, void *data)
{
	belady((minibelady_env_t*) data, block);
}

static
ir_node *get_end_of_block_insertion_point(ir_node *block)
{
	ir_node *last = sched_last(block);

470
	/* skip Projs and Keep-alikes behind the jump... */
471
472
473
474
475
476
	while(is_Proj(last) || be_is_Keep(last)) {
		last = sched_prev(last);
	}

	if(!is_cfop(last)) {
		last = sched_next(last);
477
		/* last node must be a cfop, only exception is the start block */
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
		assert(last	== get_irg_start_block(get_irn_irg(block)));
	}

	return last;
}

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

	if(block == startblock)
		return;

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

	block_info = get_block_info(block);

503
504
505
	DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
	     block_info->start_state));

506
507
508
	/* process all pred blocks */
	arity = get_irn_arity(block);
	for (i = 0; i < arity; ++i) {
509
510
511
512
513
514
515
516
517
518
519
520
521
		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;

		if(need_state == NULL)
			continue;

		if(is_Phi(need_state) && get_nodes_block(need_state) == block) {
			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));
522

523
524
		if(pred_info->end_state != need_state) {
			ir_node *insert_point =	get_end_of_block_insertion_point(pred);
525
526


527
			DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
528
			create_reload(env, need_state, insert_point, pred_info->end_state);
529
530
531
532
533
534
535
536
537
538
		}
	}
}

void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
                     create_spill_func create_spill,
                     create_reload_func create_reload) {
	minibelady_env_t env;
	ir_graph *irg = be_get_birg_irg(birg);
	spill_info_t *info;
Sebastian Hack's avatar
Sebastian Hack committed
539
	be_lv_t *lv = be_assure_liveness(birg);
540

Sebastian Hack's avatar
Sebastian Hack committed
541
	be_liveness_assure_sets(lv);
542
543
544
545
546
547
548
549
550
551
552
553
554
	/* construct control flow loop tree */
	if(! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
		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;
	env.lv            = be_get_birg_liveness(birg);
	env.uses          = be_begin_uses(irg, env.lv);
	env.spills        = NULL;
Matthias Braun's avatar
Matthias Braun committed
555
	ir_nodemap_init(&env.spill_infos);
556

557
	assure_doms(irg);
558
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
559
560
561
562
563
564
565
566
	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);

567
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
568
569
570
571

	/* reconstruct ssa-form */
	info = env.spills;
	while(info != NULL) {
572
		be_ssa_construction_env_t senv;
573
574
		int i, len;
		ir_node **phis;
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

		be_ssa_construction_init(&senv, birg);
		if(sched_is_scheduled(info->value))
			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);

		if(lv != NULL) {
			be_ssa_construction_update_liveness_phis(&senv, lv);

			be_liveness_update(lv, info->value);
			len = ARR_LEN(info->reloads);
			for(i = 0; i < len; ++i) {
				ir_node *reload = info->reloads[i];
				be_liveness_update(lv, reload);
			}
		}

		phis = be_ssa_construction_get_new_phis(&senv);
595
596
597
598
599

		/* set register requirements for phis */
		len = ARR_LEN(phis);
		for(i = 0; i < len; ++i) {
			ir_node *phi = phis[i];
600
			arch_set_irn_register(phi, env.reg);
601
		}
602
		be_ssa_construction_destroy(&senv);
603
604
605
606
607

		info = info->next;
	}

	/* some nodes might be dead now. */
608
	be_remove_dead_nodes_from_schedule(birg);
609

Matthias Braun's avatar
Matthias Braun committed
610
	ir_nodemap_destroy(&env.spill_infos);
611
612
613
614
615
616
617
618
619
620
	be_end_uses(env.uses);
	obstack_free(&env.obst, NULL);
}

void be_init_state(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.state");
}

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);