beabihelper.c 16.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
 *
 * 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.
 */

/**
 * @file
 * @brief       Helper functions for handling ABI constraints in the code
 *              selection phase.
 * @author      Matthias Braun
 * @version     $Id$
 */
#include "config.h"

#include "beabihelper.h"
#include "bearch.h"
#include "benode.h"
#include "besched.h"
#include "ircons.h"
#include "iredges.h"
#include "irgwalk.h"
36
#include "irphase_t.h"
37
#include "heights.h"
38

39
40
41
/**
 * An entry in the register state map.
 */
42
typedef struct reg_flag_t {
43
44
45
	const arch_register_t *reg;     /**< register at an input position.
	                                     may be NULL in case of memory input */
	arch_register_req_type_t flags; /**< requirement flags for this register. */
46
47
48
49
} reg_flag_t;

/**
 * A register state mapping keeps track of the symbol values (=firm nodes)
50
51
 * to registers. This is useful when constructing straight line code
 * like the function prolog or epilog in some architectures.
52
53
54
 */
typedef struct register_state_mapping_t {
	ir_node   **value_map;     /**< mapping of state indices to values */
Matthias Braun's avatar
Matthias Braun committed
55
	size_t    **reg_index_map; /**< mapping of regclass,regnum to an index
56
57
58
59
60
61
	                                into the value_map */
	reg_flag_t *regs;          /**< registers (and memory values) that form a
	                                state */
	ir_node    *last_barrier;
} register_state_mapping_t;

62
63
64
/**
 * The environment for all helper functions.
 */
65
struct beabi_helper_env_t {
66
67
68
69
	ir_graph                 *irg;         /**< the graph we operate on */
	register_state_mapping_t  prolog;      /**< the register state map for the prolog */
	register_state_mapping_t  epilog;      /**< the register state map for the epilog */
	ir_phase                 *stack_order; /**< a phase to handle stack dependencies. */
70
71
};

72
73
74
75
76
77
78
79
80
/**
 * Create a new empty register state map for the given
 * architecture.
 *
 * @param rsm       the register state map to be initialized
 * @param arch_env  the architecture environment
 *
 * After this call, the register map is initialized to empty.
 */
81
82
83
static void prepare_rsm(register_state_mapping_t *rsm,
                        const arch_env_t *arch_env)
{
84
	unsigned   n_reg_classes = arch_env->n_register_classes;
85
	unsigned   c;
86
	reg_flag_t memory = { NULL, arch_register_req_type_none };
87
88
89
90
91
92

	rsm->regs = NEW_ARR_F(reg_flag_t, 0);
	/* memory input at 0 */
	ARR_APP1(reg_flag_t, rsm->regs, memory);

	rsm->value_map     = NULL;
Matthias Braun's avatar
Matthias Braun committed
93
	rsm->reg_index_map = XMALLOCN(size_t*, n_reg_classes);
94
	for (c = 0; c < n_reg_classes; ++c) {
95
		const arch_register_class_t *cls    = &arch_env->register_classes[c];
96
97
98
		unsigned                     n_regs = arch_register_class_n_regs(cls);
		unsigned                     r;

Matthias Braun's avatar
Matthias Braun committed
99
		rsm->reg_index_map[c] = XMALLOCN(size_t, n_regs);
100
		for (r = 0; r < n_regs; ++r) {
Matthias Braun's avatar
Matthias Braun committed
101
			rsm->reg_index_map[c][r] = (size_t)-1;
102
103
104
105
		}
	}
}

106
107
108
109
110
111
112
113
114
/**
 * Destroy a register state map for the given
 * architecture.
 *
 * @param rsm       the register state map to be destroyed
 * @param arch_env  the architecture environment
 *
 * After this call, the register map is initialized to empty.
 */
115
116
static void free_rsm(register_state_mapping_t *rsm, const arch_env_t *arch_env)
{
117
	unsigned n_reg_classes = arch_env->n_register_classes;
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	unsigned c;

	for (c = 0; c < n_reg_classes; ++c) {
		free(rsm->reg_index_map[c]);
	}

	free(rsm->reg_index_map);
	if (rsm->value_map != NULL)
		DEL_ARR_F(rsm->value_map);
	DEL_ARR_F(rsm->regs);

	rsm->regs          = NULL;
	rsm->reg_index_map = NULL;
	rsm->value_map     = NULL;
}

134
135
136
137
138
139
/**
 * Remove all registers from a register state map.
 *
 * @param rsm       the register state map to be destroyed
 * @param arch_env  the architecture environment
 */
140
141
142
static void rsm_clear_regs(register_state_mapping_t *rsm,
                           const arch_env_t *arch_env)
{
143
	unsigned   n_reg_classes = arch_env->n_register_classes;
144
	unsigned   c;
145
	reg_flag_t memory = { NULL, arch_register_req_type_none };
146
147

	for (c = 0; c < n_reg_classes; ++c) {
148
		const arch_register_class_t *cls    = &arch_env->register_classes[c];
149
150
151
152
		unsigned                     n_regs = arch_register_class_n_regs(cls);
		unsigned                     r;

		for (r = 0; r < n_regs; ++r) {
Matthias Braun's avatar
Matthias Braun committed
153
			rsm->reg_index_map[c][r] = (size_t)-1;
154
155
156
157
158
159
160
161
162
163
164
		}
	}
	ARR_RESIZE(reg_flag_t, rsm->regs, 0);
	ARR_APP1(reg_flag_t, rsm->regs, memory);

	if (rsm->value_map != NULL) {
		DEL_ARR_F(rsm->value_map);
		rsm->value_map = NULL;
	}
}

165
166
167
168
/**
 * Add a register and its constraint flags to a register state map
 * and return its index inside the map.
 */
169
static int rsm_add_reg(register_state_mapping_t *rsm,
170
171
                       const arch_register_t *reg,
                       arch_register_req_type_t flags)
172
{
Matthias Braun's avatar
Matthias Braun committed
173
	size_t     input_idx = ARR_LEN(rsm->regs);
174
175
176
177
178
	int        cls_idx   = reg->reg_class->index;
	int        reg_idx   = reg->index;
	reg_flag_t regflag   = { reg, flags };

	/* we must not have used get_value yet */
Matthias Braun's avatar
Matthias Braun committed
179
	assert(rsm->reg_index_map[cls_idx][reg_idx] == (size_t)-1);
180
181
	rsm->reg_index_map[cls_idx][reg_idx] = input_idx;
	ARR_APP1(reg_flag_t, rsm->regs, regflag);
182
183
184
185
186
187

	if (rsm->value_map != NULL) {
		ARR_APP1(ir_node*, rsm->value_map, NULL);
		assert(ARR_LEN(rsm->value_map) == ARR_LEN(rsm->regs));
	}
	return input_idx;
188
189
}

190
191
192
/**
 * Retrieve the ir_node stored at the given index in the register state map.
 */
Matthias Braun's avatar
Matthias Braun committed
193
static ir_node *rsm_get_value(register_state_mapping_t *rsm, size_t index)
194
{
Matthias Braun's avatar
Matthias Braun committed
195
	assert(index < ARR_LEN(rsm->value_map));
196
197
198
	return rsm->value_map[index];
}

199
200
201
/**
 * Retrieve the ir_node occupying the given register in the register state map.
 */
202
203
204
static ir_node *rsm_get_reg_value(register_state_mapping_t *rsm,
                                  const arch_register_t *reg)
{
Matthias Braun's avatar
Matthias Braun committed
205
206
207
	int    cls_idx   = reg->reg_class->index;
	int    reg_idx   = reg->index;
	size_t input_idx = rsm->reg_index_map[cls_idx][reg_idx];
208
209
210
211

	return rsm_get_value(rsm, input_idx);
}

212
213
214
/**
 * Enter a ir_node at the given index in the register state map.
 */
Matthias Braun's avatar
Matthias Braun committed
215
static void rsm_set_value(register_state_mapping_t *rsm, size_t index,
216
217
                          ir_node *value)
{
Matthias Braun's avatar
Matthias Braun committed
218
	assert(index < ARR_LEN(rsm->value_map));
219
220
221
	rsm->value_map[index] = value;
}

222
223
224
/**
 * Enter a ir_node at the given register in the register state map.
 */
225
226
227
static void rsm_set_reg_value(register_state_mapping_t *rsm,
                              const arch_register_t *reg, ir_node *value)
{
Matthias Braun's avatar
Matthias Braun committed
228
229
230
	int    cls_idx   = reg->reg_class->index;
	int    reg_idx   = reg->index;
	size_t input_idx = rsm->reg_index_map[cls_idx][reg_idx];
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	rsm_set_value(rsm, input_idx, value);
}


beabi_helper_env_t *be_abihelper_prepare(ir_graph *irg)
{
	const arch_env_t   *arch_env = be_get_irg_arch_env(irg);
	beabi_helper_env_t *env      = XMALLOCZ(beabi_helper_env_t);

	env->irg = irg;
	prepare_rsm(&env->prolog, arch_env);
	prepare_rsm(&env->epilog, arch_env);

	return env;
}

void be_abihelper_finish(beabi_helper_env_t *env)
{
	const arch_env_t *arch_env = be_get_irg_arch_env(env->irg);

	free_rsm(&env->prolog, arch_env);
	if (env->epilog.reg_index_map != NULL) {
		free_rsm(&env->epilog, arch_env);
	}
255
	xfree(env);
256
257
258
}

void be_prolog_add_reg(beabi_helper_env_t *env, const arch_register_t *reg,
259
                       arch_register_req_type_t flags)
260
261
262
263
264
265
266
267
268
269
270
{
	rsm_add_reg(&env->prolog, reg, flags);
}

ir_node *be_prolog_create_start(beabi_helper_env_t *env, dbg_info *dbgi,
                                ir_node *block)
{
	int      n_start_outs = ARR_LEN(env->prolog.regs);
	ir_node *start        = be_new_Start(dbgi, block, n_start_outs);
	int      o;

271
272
	arch_irn_add_flags(start, arch_irn_flags_prolog);

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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
	assert(env->prolog.value_map == NULL);
	env->prolog.value_map = NEW_ARR_F(ir_node*, n_start_outs);

	for (o = 0; o < n_start_outs; ++o) {
		const reg_flag_t      *regflag = &env->prolog.regs[o];
		const arch_register_t *reg     = regflag->reg;
		ir_node               *proj;
		if (reg == NULL) {
			arch_set_out_register_req(start, o, arch_no_register_req);
			proj = new_r_Proj(start, mode_M, o);
		} else {
			be_set_constr_single_reg_out(start, o, regflag->reg,
			                             regflag->flags);
			arch_irn_set_register(start, o, regflag->reg);
			proj = new_r_Proj(start, reg->reg_class->mode, o);
		}
		env->prolog.value_map[o] = proj;
	}

	/* start node should really be the first thing constructed */
	env->prolog.last_barrier = start;

	return start;
}

ir_node *be_prolog_get_reg_value(beabi_helper_env_t *env,
                                 const arch_register_t *reg)
{
	return rsm_get_reg_value(&env->prolog, reg);
}

ir_node *be_prolog_get_memory(beabi_helper_env_t *env)
{
	return rsm_get_value(&env->prolog, 0);
}

void be_prolog_set_reg_value(beabi_helper_env_t *env,
                             const arch_register_t *reg, ir_node *value)
{
	rsm_set_reg_value(&env->prolog, reg, value);
}

void be_prolog_set_memory(beabi_helper_env_t *env, ir_node *value)
{
	rsm_set_value(&env->prolog, 0, value);
}



void be_epilog_begin(beabi_helper_env_t *env)
{
	const arch_env_t *arch_env = be_get_irg_arch_env(env->irg);
	rsm_clear_regs(&env->epilog, arch_env);
	env->epilog.value_map    = NEW_ARR_F(ir_node*, 1);
	env->epilog.value_map[0] = NULL;
}

void be_epilog_add_reg(beabi_helper_env_t *env, const arch_register_t *reg,
331
                       arch_register_req_type_t flags, ir_node *value)
332
{
333
334
	int index = rsm_add_reg(&env->epilog, reg, flags);
	rsm_set_value(&env->epilog, index, value);
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
}

void be_epilog_set_reg_value(beabi_helper_env_t *env,
                             const arch_register_t *reg, ir_node *value)
{
	rsm_set_reg_value(&env->epilog, reg, value);
}

void be_epilog_set_memory(beabi_helper_env_t *env, ir_node *value)
{
	rsm_set_value(&env->epilog, 0, value);
}

ir_node *be_epilog_get_reg_value(beabi_helper_env_t *env,
                                 const arch_register_t *reg)
{
	return rsm_get_reg_value(&env->epilog, reg);
}

ir_node *be_epilog_get_memory(beabi_helper_env_t *env)
{
	return rsm_get_value(&env->epilog, 0);
}

ir_node *be_epilog_create_return(beabi_helper_env_t *env, dbg_info *dbgi,
                                 ir_node *block)
{
362
	size_t    n_return_in = ARR_LEN(env->epilog.regs);
363
364
365
	ir_node **in          = env->epilog.value_map;
	int       n_res       = 1; /* TODO */
	unsigned  pop         = 0; /* TODO */
366
	size_t    i;
367
368
369
370
371
372
	ir_node  *ret;

	assert(ARR_LEN(env->epilog.value_map) == n_return_in);

	ret = be_new_Return(dbgi, get_irn_irg(block), block, n_res, pop,
	                    n_return_in, in);
373
	arch_irn_add_flags(ret, arch_irn_flags_epilog);
374
375
376
377
	for (i = 0; i < n_return_in; ++i) {
		const reg_flag_t      *regflag = &env->epilog.regs[i];
		const arch_register_t *reg     = regflag->reg;
		if (reg != NULL) {
378
379
			be_set_constr_single_reg_in(ret, i, reg,
			                            arch_register_req_type_none);
380
381
382
383
384
385
386
387
388
389
390
391
		}
	}

	rsm_clear_regs(&env->epilog, be_get_irg_arch_env(env->irg));
	env->epilog.last_barrier = NULL;

	return ret;
}

static void add_missing_keep_walker(ir_node *node, void *data)
{
	int              n_outs, i;
392
	unsigned        *found_projs;
393
394
395
396
397
398
399
400
401
402
403
	const ir_edge_t *edge;
	ir_mode         *mode = get_irn_mode(node);
	ir_node         *last_keep;
	(void) data;
	if (mode != mode_T)
		return;

	n_outs = arch_irn_get_n_outs(node);
	if (n_outs <= 0)
		return;

404
	rbitset_alloca(found_projs, n_outs);
405
	foreach_out_edge(node, edge) {
406
		ir_node *succ = get_edge_src_irn(edge);
407
		ir_mode *mode = get_irn_mode(succ);
408
409
410
		int      pn;

		/* The node could be kept */
411
		if (is_End(succ) || is_Anchor(succ))
412
413
			continue;

414
		if (mode == mode_M || mode == mode_X)
415
416
			continue;

417
		pn = get_Proj_proj(succ);
418
		assert(pn < n_outs);
419
		rbitset_set(found_projs, pn);
420
421
422
423
424
425
426
427
428
429
430
	}


	/* are keeps missing? */
	last_keep = NULL;
	for (i = 0; i < n_outs; ++i) {
		ir_node                     *block;
		ir_node                     *in[1];
		const arch_register_req_t   *req;
		const arch_register_class_t *cls;

431
		if (rbitset_is_set(found_projs, i)) {
432
433
434
435
436
			continue;
		}

		req = arch_get_out_register_req(node, i);
		cls = req->cls;
437
		if (cls == NULL || (cls->flags & arch_register_class_flag_manual_ra)) {
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
			continue;
		}

		block = get_nodes_block(node);
		in[0] = new_r_Proj(node, arch_register_class_mode(cls), i);
		if (last_keep != NULL) {
			be_Keep_add_node(last_keep, cls, in[0]);
		} else {
			last_keep = be_new_Keep(block, 1, in);
			if (sched_is_scheduled(node)) {
				sched_add_after(node, last_keep);
			}
		}
	}
}

void be_add_missing_keeps(ir_graph *irg)
{
	irg_walk_graph(irg, add_missing_keep_walker, NULL, NULL);
}
458
459


Michael Beck's avatar
Michael Beck committed
460
461
462
/**
 * Link the node into its block list as a new head.
 */
463
464
465
static void collect_node(ir_node *node)
{
	ir_node *block = get_nodes_block(node);
466
	ir_node *old   = (ir_node*)get_irn_link(block);
467
468
469
470
471

	set_irn_link(node, old);
	set_irn_link(block, node);
}

Michael Beck's avatar
Michael Beck committed
472
473
474
/**
 * Post-walker: link all nodes that probably access the stack into lists of their block.
 */
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
static void link_ops_in_block_walker(ir_node *node, void *data)
{
	(void) data;

	switch (get_irn_opcode(node)) {
	case iro_Return:
	case iro_Call:
		collect_node(node);
		break;
	case iro_Alloc:
		/** all non-stack alloc nodes should be lowered before the backend */
		assert(get_Alloc_where(node) == stack_alloc);
		collect_node(node);
		break;
	case iro_Free:
		assert(get_Free_where(node) == stack_alloc);
		collect_node(node);
		break;
	case iro_Builtin:
		if (get_Builtin_kind(node) == ir_bk_return_address) {
Matthias Braun's avatar
Matthias Braun committed
495
496
497
			ir_node   *param = get_Builtin_param(node, 0);
			ir_tarval *tv    = get_Const_tarval(param); /* must be Const */
			long       value = get_tarval_long(tv);
498
			if (value > 0) {
Michael Beck's avatar
Michael Beck committed
499
500
				/* not the return address of the current function:
				 * we need the stack pointer for the frame climbing */
501
502
503
504
505
506
507
508
509
				collect_node(node);
			}
		}
		break;
	default:
		break;
	}
}

510
static ir_heights_t *heights;
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525

/**
 * Check if a node is somehow data dependent on another one.
 * both nodes must be in the same basic block.
 * @param n1 The first node.
 * @param n2 The second node.
 * @return 1, if n1 is data dependent (transitively) on n2, 0 if not.
 */
static int dependent_on(const ir_node *n1, const ir_node *n2)
{
	assert(get_nodes_block(n1) == get_nodes_block(n2));

	return heights_reachable_in_block(heights, n1, n2);
}

Michael Beck's avatar
Michael Beck committed
526
527
528
529
530
531
532
/**
 * Classical qsort() comparison function behavior:
 *
 * 0  if both elements are equal, no node depend on the other
 * +1 if first depends on second (first is greater)
 * -1 if second depends on first (second is greater)
*/
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
static int cmp_call_dependency(const void *c1, const void *c2)
{
	const ir_node *n1 = *(const ir_node **) c1;
	const ir_node *n2 = *(const ir_node **) c2;

	if (dependent_on(n1, n2))
		return 1;

	if (dependent_on(n2, n1))
		return -1;

	/* The nodes have no depth order, but we need a total order because qsort()
	 * is not stable. */
	return get_irn_idx(n2) - get_irn_idx(n1);
}

549
/**
Michael Beck's avatar
Michael Beck committed
550
 * Block-walker: sorts dependencies and remember them into a phase
551
 */
552
553
static void process_ops_in_block(ir_node *block, void *data)
{
554
	ir_phase *phase = (ir_phase*)data;
555
556
557
558
559
560
	unsigned  n;
	unsigned  n_nodes;
	ir_node  *node;
	ir_node **nodes;

	n_nodes = 0;
561
562
	for (node = (ir_node*)get_irn_link(block); node != NULL;
	     node = (ir_node*)get_irn_link(node)) {
563
564
565
566
567
568
569
570
		++n_nodes;
	}

	if (n_nodes == 0)
		return;

	nodes = XMALLOCN(ir_node*, n_nodes);
	n = 0;
571
572
	for (node = (ir_node*)get_irn_link(block); node != NULL;
	     node = (ir_node*)get_irn_link(node)) {
573
		nodes[n++] = node;
574
575
576
577
578
579
	}
	assert(n == n_nodes);

	/* order nodes according to their data dependencies */
	qsort(nodes, n_nodes, sizeof(nodes[0]), cmp_call_dependency);

Michael Beck's avatar
Michael Beck committed
580
	/* remember the calculated dependency into a phase */
581
582
583
584
585
586
	for (n = n_nodes-1; n > 0; --n) {
		ir_node *node = nodes[n];
		ir_node *pred = nodes[n-1];

		phase_set_irn_data(phase, node, pred);
	}
587
	xfree(nodes);
588
589
590
591
592
}

void be_collect_stacknodes(beabi_helper_env_t *env)
{
	ir_graph *irg = env->irg;
Michael Beck's avatar
Michael Beck committed
593
594

	/* collect all potential^stack accessing nodes */
595
596
597
598
599
	irg_walk_graph(irg, firm_clear_link, link_ops_in_block_walker, NULL);

	assert(env->stack_order == NULL);
	env->stack_order = new_phase(irg, phase_irn_init_default);

Michael Beck's avatar
Michael Beck committed
600
601
	/* use heights to create a total order for those nodes: this order is stored
	 * in the created phase */
602
603
604
605
606
607
608
	heights = heights_new(irg);
	irg_block_walk_graph(irg, NULL, process_ops_in_block, env->stack_order);
	heights_free(heights);
}

ir_node *be_get_stack_pred(const beabi_helper_env_t *env, const ir_node *node)
{
609
	return (ir_node*)phase_get_irn_data(env->stack_order, node);
610
}