betranshlp.c 24 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
 */

/**
 * @file
 * @brief       be transform helper extracted from the ia32 backend.
Michael Beck's avatar
Michael Beck committed
9
 * @author      Matthias Braun, Michael Beck
10
11
 * @date        14.06.2007
 */
Matthias Braun's avatar
Matthias Braun committed
12
#include "bearch.h"
13
14
#include "beirg.h"
#include "belive.h"
15
#include "benode.h"
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "betranshlp.h"
#include "cgana.h"
#include "debug.h"
#include "execfreq_t.h"
#include "heights.h"
#include "ircons_t.h"
#include "iredges.h"
#include "irgmod.h"
#include "irgraph_t.h"
#include "irgwalk.h"
#include "irhooks.h"
#include "irnodemap.h"
#include "irnode_t.h"
#include "irop_t.h"
#include "iropt_t.h"
#include "irouts.h"
#include "irtools.h"
#include "pdeq.h"
34
#include "util.h"
35
36
37
38
39
40
41
42
43

typedef struct be_transform_env_t {
	waitq    *worklist;    /**< worklist of nodes that still need to be
	                            transformed */
} be_transform_env_t;


static be_transform_env_t env;

44
45
void be_set_transformed_node(ir_node *old_node, ir_node *new_node)
{
46
	set_irn_link(old_node, new_node);
47
	mark_irn_visited(old_node);
48
49
}

50
51
int be_is_transformed(const ir_node *node)
{
52
53
54
	return irn_visited(node);
}

55
56
static inline ir_node *be_get_transformed_node(ir_node *old_node)
{
57
	if (irn_visited(old_node)) {
58
		ir_node *new_node = (ir_node*)get_irn_link(old_node);
59
60
61
62
		assert(new_node != NULL);
		return new_node;
	}
	return NULL;
63
64
}

65
66
void be_duplicate_deps(ir_node *old_node, ir_node *new_node)
{
67
	int deps = get_irn_n_deps(old_node);
Matthias Braun's avatar
Matthias Braun committed
68
	for (int i = 0; i < deps; ++i) {
69
70
71
72
73
74
75
		ir_node *dep     = get_irn_dep(old_node, i);
		ir_node *new_dep = be_transform_node(dep);

		add_irn_dep(new_node, new_dep);
	}
}

76
77
78
ir_node *be_transform_phi(ir_node *node, const arch_register_req_t *req)
{
	ir_node  *block = be_transform_node(get_nodes_block(node));
79
	ir_graph *irg   = get_irn_irg(block);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	dbg_info *dbgi  = get_irn_dbg_info(node);

	/* phi nodes allow loops, so we use the old arguments for now
	 * and fix this later */
	ir_node **ins   = get_irn_in(node)+1;
	int       arity = get_irn_arity(node);
	ir_mode  *mode  = req->cls != NULL ? req->cls->mode : get_irn_mode(node);
	ir_node  *phi   = new_ir_node(dbgi, irg, block, op_Phi, mode, arity, ins);
	copy_node_attr(irg, node, phi);
	be_duplicate_deps(node, phi);

	backend_info_t *info = be_get_info(phi);
	struct obstack *obst = be_get_be_obst(irg);
	info->in_reqs = OALLOCN(obst, const arch_register_req_t*, arity);
	for (int i = 0; i < arity; ++i) {
		info->in_reqs[i] = req;
	}

	arch_set_irn_register_req_out(phi, 0, req);
	be_enqueue_preds(node);

	return phi;
}

104
105
106
107
108
109
110
111
112
void be_set_transform_function(ir_op *op, be_transform_func func)
{
	/* shouldn't be assigned twice (except for exchanging the default
	 * be_duplicate_node entries) */
	assert(op->ops.generic == NULL
			|| op->ops.generic == (op_func) be_duplicate_node);
	op->ops.generic = (op_func) func;
}

113
114
115
116
117
void be_set_transform_proj_function(ir_op *op, be_transform_func func)
{
	op->ops.generic1 = (op_func) func;
}

118
119
120
121
122
/**
 * Transform helper for blocks.
 */
static ir_node *transform_block(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
123
124
125
126
127
	ir_graph *irg   = get_irn_irg(node);
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_mode  *mode  = get_irn_mode(node);
	ir_node  *block = new_ir_node(dbgi, irg, NULL, get_irn_op(node), mode,
	                              get_irn_arity(node), get_irn_in(node) + 1);
128
129
130
	copy_node_attr(irg, node, block);
	block->node_nr = node->node_nr;

131
132
133
134
	/* transfer execfreq value */
	double execfreq = get_block_execfreq(node);
	set_block_execfreq(block, execfreq);

135
136
137
138
139
140
141
142
143
	/* put the preds in the worklist */
	be_enqueue_preds(node);

	return block;
}

static ir_node *transform_end(ir_node *node)
{
	/* end has to be duplicated manually because we need a dynamic in array */
Matthias Braun's avatar
Matthias Braun committed
144
145
146
147
	ir_graph *irg     = get_irn_irg(node);
	dbg_info *dbgi    = get_irn_dbg_info(node);
	ir_node  *block   = be_transform_node(get_nodes_block(node));
	ir_node  *new_end = new_ir_node(dbgi, irg, block, op_End, mode_X, -1, NULL);
148
149
150
151
152
153
154
	copy_node_attr(irg, node, new_end);
	be_duplicate_deps(node, new_end);

	set_irg_end(irg, new_end);

	/* do not transform predecessors yet to keep the pre-transform
	 * phase from visiting all the graph */
155
	foreach_irn_in(node, i, in) {
156
157
158
159
160
161
162
		add_End_keepalive(new_end, in);
	}
	be_enqueue_preds(node);

	return new_end;
}

163
164
165
166
167
168
169
170
171
172
173
static ir_node *transform_proj(ir_node *node)
{
	ir_node *pred    = get_Proj_pred(node);
	ir_op   *pred_op = get_irn_op(pred);
	be_transform_func *proj_transform
		= (be_transform_func*)pred_op->ops.generic1;
	/* we should have a Proj transformer registered */
	assert(proj_transform != NULL);
	return proj_transform(node);
}

174
175
ir_node *be_duplicate_node(ir_node *node)
{
176
177
	ir_node  *block = be_transform_node(get_nodes_block(node));
	dbg_info *dbgi  = get_irn_dbg_info(node);
178
	ir_graph *irg   = get_irn_irg(node);
179
180
181
	ir_mode  *mode  = get_irn_mode(node);
	ir_op    *op    = get_irn_op(node);

Matthias Braun's avatar
Matthias Braun committed
182
	ir_node *new_node;
183
184
	if (op->opar == oparity_dynamic) {
		new_node = new_ir_node(dbgi, irg, block, op, mode, -1, NULL);
185
186
		foreach_irn_in(node, i, in) {
			add_irn_n(new_node, be_transform_node(in));
187
188
		}
	} else {
189
190
191
		int       arity = get_irn_arity(node);
		ir_node **ins   = ALLOCAN(ir_node*, arity);
		foreach_irn_in(node, i, in) {
192
193
194
195
196
197
			ins[i] = be_transform_node(in);
		}

		new_node = new_ir_node(dbgi, irg, block, op, mode, arity, ins);
	}

198
	copy_node_attr(irg, node, new_node);
199
200
201
202
203
204
	be_duplicate_deps(node, new_node);

	new_node->node_nr = node->node_nr;
	return new_node;
}

205
206
ir_node *be_transform_node(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
207
	ir_node *new_node = be_get_transformed_node(node);
208
	if (new_node != NULL)
209
210
		return new_node;

211
	DEBUG_ONLY(be_set_transformed_node(node, NULL);)
212

Matthias Braun's avatar
Matthias Braun committed
213
214
	ir_op *op = get_irn_op(node);
	be_transform_func *transform = (be_transform_func *)op->ops.generic;
215
216
217

	new_node = transform(node);
	assert(new_node != NULL);
218
219
220
221
222

	be_set_transformed_node(node, new_node);
	return new_node;
}

223
224
void be_enqueue_preds(ir_node *node)
{
225
	/* put the preds in the worklist */
226
	foreach_irn_in(node, i, pred) {
227
228
229
230
231
232
233
		pdeq_putr(env.worklist, pred);
	}
}

/**
 * Rewire nodes which are potential loops (like Phis) to avoid endless loops.
 */
234
235
static void fix_loops(ir_node *node)
{
236
	if (irn_visited_else_mark(node))
237
238
		return;

Matthias Braun's avatar
Matthias Braun committed
239
	bool changed = false;
240
241
	if (! is_Block(node)) {
		ir_node *block     = get_nodes_block(node);
242
		ir_node *new_block = (ir_node*)get_irn_link(block);
243
244
245
246

		if (new_block != NULL) {
			set_nodes_block(node, new_block);
			block = new_block;
Matthias Braun's avatar
Matthias Braun committed
247
			changed = true;
248
249
250
251
252
		}

		fix_loops(block);
	}

253
254
	foreach_irn_in(node, i, pred) {
		ir_node *in = pred;
255
		ir_node *nw = (ir_node*)get_irn_link(in);
256
257
258
259

		if (nw != NULL && nw != in) {
			set_irn_n(node, i, nw);
			in = nw;
Matthias Braun's avatar
Matthias Braun committed
260
			changed = true;
261
262
263
264
		}

		fix_loops(in);
	}
265
	/* fix proj block */
266
	if (is_Proj(node)) {
267
		set_nodes_block(node, get_nodes_block(get_Proj_pred(node)));
Matthias Braun's avatar
Matthias Braun committed
268
		changed = true;
269
	}
270

271
	for (int i = 0, arity = get_irn_n_deps(node); i < arity; ++i) {
272
		ir_node *in = get_irn_dep(node, i);
273
		ir_node *nw = (ir_node*)get_irn_link(in);
274
275
276
277

		if (nw != NULL && nw != in) {
			set_irn_dep(node, i, nw);
			in = nw;
Matthias Braun's avatar
Matthias Braun committed
278
			changed = true;
279
280
281
282
		}

		fix_loops(in);
	}
283

284
	if (changed) {
285
		identify_remember(node);
286
	}
287
288
}

289
290
ir_node *be_pre_transform_node(ir_node *place)
{
291
292
293
294
295
296
297
298
299
	if (place == NULL)
		return NULL;

	return be_transform_node(place);
}

/**
 * Transforms all nodes. Deletes the old obstack and creates a new one.
 */
300
static void transform_nodes(ir_graph *irg, arch_pretrans_nodes *pre_transform)
301
{
302
303
304
305
	hook_dead_node_elim(irg, 1);

	inc_irg_visited(irg);

306
	env.worklist = new_waitq();
307

308
309
	ir_node *const old_anchor = irg->anchor;
	ir_node *const old_end    = get_irg_end(irg);
310
311

	/* put all anchor nodes in the worklist */
Matthias Braun's avatar
Matthias Braun committed
312
	for (int i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
313
		ir_node *anchor = get_irg_anchor(irg, i);
314
315
316
		waitq_put(env.worklist, anchor);
	}

Matthias Braun's avatar
Matthias Braun committed
317
	ir_node *new_anchor  = new_r_Anchor(irg);
318
	irg->anchor = new_anchor;
319

320
321
	/* pre transform some anchors (so they are available in the other transform
	 * functions) */
322
323
324
325
326
327
328
329
330
331
	static irg_anchors const pre_anchors[] = {
		anchor_no_mem,
		anchor_end_block,
		anchor_end,
		anchor_start_block,
		anchor_start,
		anchor_frame,
	};
	for (size_t i = 0; i != ARRAY_SIZE(pre_anchors); ++i) {
		irg_anchors const idx = pre_anchors[i];
332
		ir_node    *const old = get_irn_n(old_anchor, idx);
333
		ir_node    *const nw  = be_transform_node(old);
334
		set_irn_n(new_anchor, idx, nw);
335
	}
336
337

	if (pre_transform)
338
		pre_transform(irg);
339
340
341

	/* process worklist (this should transform all nodes in the graph) */
	while (! waitq_empty(env.worklist)) {
342
		ir_node *node = (ir_node*)waitq_get(env.worklist);
343
344
345
346
347
		be_transform_node(node);
	}

	/* fix loops and set new anchors*/
	inc_irg_visited(irg);
Matthias Braun's avatar
Matthias Braun committed
348
	for (int i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
349
		ir_node *anchor = get_irn_n(old_anchor, i);
350
		anchor = (ir_node*)get_irn_link(anchor);
351
		fix_loops(anchor);
352
		set_irn_n(new_anchor, i, anchor);
353
354
355
356
357
358
359
	}

	del_waitq(env.worklist);
	free_End(old_end);
	hook_dead_node_elim(irg, 0);
}

360
void be_transform_graph(ir_graph *irg, arch_pretrans_nodes *func)
361
362
{
	/* create a new obstack */
363
364
	struct obstack old_obst = irg->obst;
	obstack_init(&irg->obst);
365
366
	irg->last_node_idx = 0;

367
	free_vrp_data(irg);
368

369
	/* create new value table for CSE */
370
	new_identities(irg);
371
372

	/* do the main transformation */
373
	transform_nodes(irg, func);
374
375

	/* free the old obstack */
376
	obstack_free(&old_obst, 0);
377

378
	/* most analysis info is wrong after transformation */
379
	be_invalidate_live_chk(irg);
Matthias Braun's avatar
Matthias Braun committed
380
	confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
381

382
383
384
	/* recalculate edges */
	edges_activate(irg);
}
385
386
387
388

bool be_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	ir_op *op = get_irn_op(node);
389
	if (op->ops.generic2 == NULL)
390
		return false;
391
	upper_bits_clean_func func = (upper_bits_clean_func)op->ops.generic2;
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
	return func(node, mode);
}

static bool bit_binop_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	return be_upper_bits_clean(get_binop_left(node), mode)
	    && be_upper_bits_clean(get_binop_right(node), mode);
}

static bool mux_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	return be_upper_bits_clean(get_Mux_true(node), mode)
	    && be_upper_bits_clean(get_Mux_false(node), mode);
}

static bool and_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	if (!mode_is_signed(mode)) {
		return be_upper_bits_clean(get_And_left(node), mode)
		    || be_upper_bits_clean(get_And_right(node), mode);
	} else {
		return bit_binop_upper_bits_clean(node, mode);
	}
}

static bool shr_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	if (mode_is_signed(mode)) {
		return false;
	} else {
		const ir_node *right = get_Shr_right(node);
		if (is_Const(right)) {
424
			long const val = get_Const_long(right);
425
426
427
428
429
430
431
432
433
434
435
436
437
438
			if (val >= 32 - (long)get_mode_size_bits(mode))
				return true;
		}
		return be_upper_bits_clean(get_Shr_left(node), mode);
	}
}

static bool shrs_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	return be_upper_bits_clean(get_Shrs_left(node), mode);
}

static bool const_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
439
	long const val = get_Const_long(node);
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
	if (mode_is_signed(mode)) {
		long    shifted = val >> (get_mode_size_bits(mode)-1);
		return shifted == 0 || shifted == -1;
	} else {
		unsigned long shifted = (unsigned long)val;
		shifted >>= get_mode_size_bits(mode)-1;
		shifted >>= 1;
		return shifted == 0;
	}
}

static bool conv_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	ir_mode       *dest_mode = get_irn_mode(node);
	const ir_node *op        = get_Conv_op(node);
	ir_mode       *src_mode  = get_irn_mode(op);
	if (mode_is_float(src_mode))
		return true;

	unsigned src_bits  = get_mode_size_bits(src_mode);
	unsigned dest_bits = get_mode_size_bits(dest_mode);
	/* downconvs are a nop */
	if (src_bits >= dest_bits)
		return be_upper_bits_clean(op, mode);
	/* upconvs are fine if src is big enough or if sign matches */
	if (src_bits <= get_mode_size_bits(mode)
		&& mode_is_signed(src_mode) == mode_is_signed(mode))
		return true;
	return false;
}

static bool proj_upper_bits_clean(const ir_node *node, ir_mode *mode)
{
	const ir_node *pred = get_Proj_pred(node);
	switch (get_irn_opcode(pred)) {
	case iro_Load: {
		ir_mode *load_mode = get_Load_mode(pred);
		unsigned load_bits = get_mode_size_bits(load_mode);
		if (load_bits > get_mode_size_bits(mode))
			return false;
		if (mode_is_signed(load_mode) != mode_is_signed(mode))
			return false;
		return true;
	}
	default:
		break;
	}
	return false;
}

void be_set_upper_bits_clean_function(ir_op *op, upper_bits_clean_func func)
{
492
	op->ops.generic2 = (op_func)func;
493
494
495
496
497
498
499
500
501
502
503
504
505
506
}

void be_start_transform_setup(void)
{
	ir_clear_opcodes_generic_func();

	be_set_transform_function(op_Bad,         be_duplicate_node);
	be_set_transform_function(op_be_CopyKeep, be_duplicate_node);
	be_set_transform_function(op_be_IncSP,    be_duplicate_node);
	be_set_transform_function(op_be_Keep,     be_duplicate_node);
	be_set_transform_function(op_Block,       transform_block);
	be_set_transform_function(op_End,         transform_end);
	be_set_transform_function(op_NoMem,       be_duplicate_node);
	be_set_transform_function(op_Pin,         be_duplicate_node);
507
	be_set_transform_function(op_Proj,        transform_proj);
508
509
510
511
512
513
514
515
516
517
518
519
520
	be_set_transform_function(op_Start,       be_duplicate_node);
	be_set_transform_function(op_Sync,        be_duplicate_node);

	be_set_upper_bits_clean_function(op_And,   and_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Const, const_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Conv,  conv_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Eor,   bit_binop_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Mux,   mux_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Or,    bit_binop_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Proj,  proj_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Shr,   shr_upper_bits_clean);
	be_set_upper_bits_clean_function(op_Shrs,  shrs_upper_bits_clean);
}
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

bool be_pattern_is_rotl(ir_node const *const irn_or, ir_node **const left,
                        ir_node **const right)
{
	assert(is_Add(irn_or) || is_Or(irn_or));

	ir_mode *mode = get_irn_mode(irn_or);
	if (!mode_is_int(mode))
		return false;

	ir_node *shl = get_binop_left(irn_or);
	ir_node *shr = get_binop_right(irn_or);
	if (is_Shr(shl)) {
		if (!is_Shl(shr))
			return false;

		ir_node *tmp = shl;
		shl = shr;
		shr = tmp;
	} else if (!is_Shl(shl)) {
		return false;
	} else if (!is_Shr(shr)) {
		return false;
	}

	ir_node *x = get_Shl_left(shl);
	if (x != get_Shr_left(shr))
		return false;

	ir_node *c1 = get_Shl_right(shl);
	ir_node *c2 = get_Shr_right(shr);
	if (is_Const(c1) && is_Const(c2)) {
		ir_tarval *tv1 = get_Const_tarval(c1);
		if (!tarval_is_long(tv1))
			return false;

		ir_tarval *tv2 = get_Const_tarval(c2);
		if (!tarval_is_long(tv2))
			return false;

		if (get_tarval_long(tv1) + get_tarval_long(tv2)
		    != (long) get_mode_size_bits(mode))
			return false;

		*left  = x;
		*right = c1;
		return true;
	}

	/* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
	 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
	if (!ir_is_negated_value(c1, c2))
		return false;

	*left  = x;
	*right = c1;
	return true;
}
Matthias Braun's avatar
Matthias Braun committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592

void be_map_exc_node_to_runtime_call(ir_node *node, ir_mode *res_mode,
                                     ir_entity *runtime_entity,
                                     long pn_M, long pn_X_regular,
                                     long pn_X_except, long pn_res)
{
	assert(is_memop(node));

	size_t    n_in = get_irn_arity(node)-1;
	ir_node **in   = ALLOCAN(ir_node*, n_in);
	ir_type  *mtp  = get_entity_type(runtime_entity);

	assert(get_method_n_params(mtp) == n_in);
	size_t p = 0;
593
	foreach_irn_in(node, i, n) {
Matthias Braun's avatar
Matthias Braun committed
594
595
596
597
598
599
600
601
		if (get_irn_mode(n) == mode_M)
			continue;
		in[p++] = n;
	}
	assert(p == n_in);

	ir_graph *irg   = get_irn_irg(node);
	dbg_info *dbgi  = get_irn_dbg_info(node);
602
	ir_node  *addr  = new_r_Address(irg, runtime_entity);
Matthias Braun's avatar
Matthias Braun committed
603
604
605
606
607
608
609
	ir_node  *block = get_nodes_block(node);
	ir_node  *mem   = get_memop_mem(node);
	ir_node  *call  = new_rd_Call(dbgi, block, mem, addr, n_in, in, mtp);
	set_irn_pinned(call, get_irn_pinned(node));
	int throws_exception = ir_throws_exception(node);
	ir_set_throws_exception(call, throws_exception);

610
611
612
613
	assert(pn_M < 2 && pn_res < 2 && pn_X_regular < 4 && pn_X_except < 4);
	int const         n_proj     = 4;
	int               n_operands = 2;
	ir_node   **const tuple_in   = ALLOCAN(ir_node*, n_proj);
Matthias Braun's avatar
Matthias Braun committed
614
615
616
617
	tuple_in[pn_M] = new_r_Proj(call, mode_M, pn_Call_M);
	ir_node *ress = new_r_Proj(call, mode_T, pn_Call_T_result);
	tuple_in[pn_res] = new_r_Proj(ress, res_mode, 0);
	if (throws_exception) {
618
619
620
		tuple_in[pn_X_regular]  = new_r_Proj(call, mode_X, pn_Call_X_regular);
		tuple_in[pn_X_except]   = new_r_Proj(call, mode_X, pn_Call_X_except);
		n_operands             += 2;
Matthias Braun's avatar
Matthias Braun committed
621
622
	}

623
	turn_into_tuple(node, n_operands, tuple_in);
Matthias Braun's avatar
Matthias Braun committed
624
}
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892

/**
 * Link the node into its block list as a new head.
 */
static void collect_node(ir_node *node)
{
	ir_node *block = get_nodes_block(node);
	ir_node *old   = (ir_node*)get_irn_link(block);

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

/**
 * Post-walker: link all nodes that probably access the stack into lists of their block.
 */
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 */
		collect_node(node);
		break;
	case iro_Free:
		collect_node(node);
		break;
	case iro_Builtin:
		if (get_Builtin_kind(node) == ir_bk_return_address) {
			ir_node *const param = get_Builtin_param(node, 0);
			long     const value = get_Const_long(param); /* must be Const */
			if (value > 0) {
				/* not the return address of the current function:
				 * we need the stack pointer for the frame climbing */
				collect_node(node);
			}
		}
		break;
	default:
		break;
	}
}

static ir_heights_t *heights;

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

/**
 * 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)
*/
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;
	unsigned h1, h2;

	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.
	 *
	 * Additionally, we need to respect transitive dependencies. Consider a
	 * Call a depending on Call b and an independent Call c.
	 * We MUST NOT order c > a and b > c. */
	h1 = get_irn_height(heights, n1);
	h2 = get_irn_height(heights, n2);
	if (h1 < h2) return  1;
	if (h1 > h2) return -1;
	/* Same height, so use a random (but stable) order */
	return get_irn_idx(n2) - get_irn_idx(n1);
}

/**
 * Block-walker: sorts dependencies and remember them into a phase
 */
static void process_ops_in_block(ir_node *block, void *data)
{
	ir_nodemap *map = (ir_nodemap*)data;
	unsigned    n;
	unsigned    n_nodes;
	ir_node    *node;
	ir_node   **nodes;

	n_nodes = 0;
	for (node = (ir_node*)get_irn_link(block); node != NULL;
	     node = (ir_node*)get_irn_link(node)) {
		++n_nodes;
	}

	if (n_nodes == 0)
		return;

	nodes = XMALLOCN(ir_node*, n_nodes);
	n = 0;
	for (node = (ir_node*)get_irn_link(block); node != NULL;
	     node = (ir_node*)get_irn_link(node)) {
		nodes[n++] = node;
	}
	assert(n == n_nodes);

	/* order nodes according to their data dependencies */
	QSORT(nodes, n_nodes, cmp_call_dependency);

	/* remember the calculated dependency into a phase */
	for (n = n_nodes-1; n > 0; --n) {
		ir_node *node = nodes[n];
		ir_node *pred = nodes[n-1];

		ir_nodemap_insert(map, node, pred);
	}
	free(nodes);
}

struct be_stackorder_t {
	ir_nodemap stack_order; /**< a phase to handle stack dependencies. */
};

be_stackorder_t *be_collect_stacknodes(ir_graph *irg)
{
	be_stackorder_t *env = XMALLOCZ(be_stackorder_t);

	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);

	/* collect all potential^stack accessing nodes */
	irg_walk_graph(irg, firm_clear_link, link_ops_in_block_walker, NULL);

	ir_nodemap_init(&env->stack_order, irg);

	/* use heights to create a total order for those nodes: this order is stored
	 * in the created phase */
	heights = heights_new(irg);
	irg_block_walk_graph(irg, NULL, process_ops_in_block, &env->stack_order);
	heights_free(heights);

	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);

	return env;
}

ir_node *be_get_stack_pred(const be_stackorder_t *env, const ir_node *node)
{
	return ir_nodemap_get(ir_node, &env->stack_order, node);
}

void be_free_stackorder(be_stackorder_t *env)
{
	ir_nodemap_destroy(&env->stack_order);
	free(env);
}

static void create_stores_for_type(ir_graph *irg, ir_type *type)
{
	size_t   n           = get_compound_n_members(type);
	ir_node *frame       = get_irg_frame(irg);
	ir_node *initial_mem = get_irg_initial_mem(irg);
	ir_node *mem         = initial_mem;
	ir_node *first_store = NULL;
	ir_node *start_block = get_irg_start_block(irg);
	ir_node *args        = get_irg_args(irg);
	size_t   i;

	/* all parameter entities left in the frame type require stores.
	 * (The ones passed on the stack have been moved to the arg type) */
	for (i = 0; i < n; ++i) {
		ir_entity *entity = get_compound_member(type, i);
		ir_type   *tp     = get_entity_type(entity);
		ir_node   *addr;
		size_t     arg;
		if (!is_parameter_entity(entity))
			continue;

		arg = get_entity_parameter_number(entity);
		if (arg == IR_VA_START_PARAMETER_NUMBER)
			continue;

		addr = new_r_Member(start_block, frame, entity);
		if (entity->attr.parameter.doubleword_low_mode != NULL) {
			ir_mode *mode      = entity->attr.parameter.doubleword_low_mode;
			ir_node *val0      = new_r_Proj(args, mode, arg);
			ir_node *val1      = new_r_Proj(args, mode, arg+1);
			ir_node *store0    = new_r_Store(start_block, mem, addr, val0,
			                                 tp, cons_none);
			ir_node *mem0      = new_r_Proj(store0, mode_M, pn_Store_M);
			size_t   offset    = get_mode_size_bits(mode)/8;
			ir_mode *mode_ref  = get_irn_mode(addr);
			ir_mode *mode_offs = get_reference_mode_unsigned_eq(mode_ref);
			ir_node *cnst      = new_r_Const_long(irg, mode_offs, offset);
			ir_node *next_addr = new_r_Add(start_block, addr, cnst, mode_ref);
			ir_node *store1    = new_r_Store(start_block, mem0, next_addr, val1,
			                                 tp, cons_none);
			mem = new_r_Proj(store1, mode_M, pn_Store_M);
			if (first_store == NULL)
				first_store = store0;
		} else {
			ir_mode *mode  = is_compound_type(tp) ? mode_P : get_type_mode(tp);
			ir_node *val   = new_r_Proj(args, mode, arg);
			ir_node *store = new_r_Store(start_block, mem, addr, val, tp, cons_none);
			mem = new_r_Proj(store, mode_M, pn_Store_M);
			if (first_store == NULL)
				first_store = store;
		}
	}

	if (mem != initial_mem) {
		edges_reroute_except(initial_mem, mem, first_store);
		set_irg_initial_mem(irg, initial_mem);
	}
}

void be_add_parameter_entity_stores(ir_graph *irg)
{
	ir_type           *frame_type   = get_irg_frame_type(irg);
	be_stack_layout_t *layout       = be_get_irg_stack_layout(irg);
	ir_type           *between_type = layout->between_type;

	create_stores_for_type(irg, frame_type);
	if (between_type != NULL) {
		create_stores_for_type(irg, between_type);
	}
}

unsigned be_get_n_allocatable_regs(const ir_graph *irg,
                                   const arch_register_class_t *cls)
{
	unsigned *const bs = rbitset_alloca(cls->n_regs);
	be_get_allocatable_regs(irg, cls, bs);
	return rbitset_popcount(bs, cls->n_regs);
}

void be_get_allocatable_regs(ir_graph const *const irg,
                             arch_register_class_t const *const cls,
                             unsigned *const raw_bitset)
{
	be_irg_t *birg             = be_birg_from_irg(irg);
	unsigned *allocatable_regs = birg->allocatable_regs;

	rbitset_clear_all(raw_bitset, cls->n_regs);
	for (unsigned i = 0; i < cls->n_regs; ++i) {
		const arch_register_t *reg = &cls->regs[i];
		if (rbitset_is_set(allocatable_regs, reg->global_index))
			rbitset_set(raw_bitset, i);
	}
}