betranshlp.c 12.6 KB
Newer Older
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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       be transform helper extracted from the ia32 backend.
Michael Beck's avatar
Michael Beck committed
23
 * @author      Matthias Braun, Michael Beck
24
25
26
27
28
29
30
31
32
33
 * @date        14.06.2007
 * @version     $Id$
 */
#include "config.h"

#include "pdeq.h"
#include "irop_t.h"
#include "iropt_t.h"
#include "irnode_t.h"
#include "irgraph_t.h"
34
#include "ircons_t.h"
35
36
37
38
39
40
41
#include "irhooks.h"
#include "iredges.h"
#include "irouts.h"
#include "trouts.h"
#include "cgana.h"
#include "debug.h"

42
#include "beirg.h"
43
#include "betranshlp.h"
44
#include "belive.h"
45
#include "benode.h"
46
47
48
49
50

typedef struct be_transform_env_t {
	ir_graph *irg;         /**< The irg, the node should be created in */
	waitq    *worklist;    /**< worklist of nodes that still need to be
	                            transformed */
51
	ir_node  *old_anchor;  /**< the old anchor node in the old irg */
52
53
54
55
56
} be_transform_env_t;


static be_transform_env_t env;

57
58
void be_set_transformed_node(ir_node *old_node, ir_node *new_node)
{
59
	set_irn_link(old_node, new_node);
60
	mark_irn_visited(old_node);
61
62
}

63
64
int be_is_transformed(const ir_node *node)
{
65
66
67
	return irn_visited(node);
}

68
69
static inline ir_node *be_get_transformed_node(ir_node *old_node)
{
70
	if (irn_visited(old_node)) {
71
		ir_node *new_node = (ir_node*)get_irn_link(old_node);
72
73
74
75
		assert(new_node != NULL);
		return new_node;
	}
	return NULL;
76
77
}

78
79
void be_duplicate_deps(ir_node *old_node, ir_node *new_node)
{
80
81
82
83
84
85
86
87
88
89
90
	int i;
	int deps = get_irn_deps(old_node);

	for (i = 0; i < deps; ++i) {
		ir_node *dep     = get_irn_dep(old_node, i);
		ir_node *new_dep = be_transform_node(dep);

		add_irn_dep(new_node, new_dep);
	}
}

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
}

void be_start_transform_setup(void)
{
	clear_irp_opcodes_generic_func();

	be_set_transform_function(op_Bad,         be_duplicate_node);
	be_set_transform_function(op_be_Copy,     be_duplicate_node);
	be_set_transform_function(op_be_CopyKeep, be_duplicate_node);
107
	be_set_transform_function(op_be_IncSP,    be_duplicate_node);
108
	be_set_transform_function(op_be_Keep,     be_duplicate_node);
109
110
111
112
113
114
	be_set_transform_function(op_be_Return,   be_duplicate_node);
	be_set_transform_function(op_be_Start,    be_duplicate_node);
	be_set_transform_function(op_NoMem,       be_duplicate_node);
	be_set_transform_function(op_Pin,         be_duplicate_node);
	be_set_transform_function(op_Start,       be_duplicate_node);
	be_set_transform_function(op_Sync,        be_duplicate_node);
115
116
}

117
118
ir_node *be_duplicate_node(ir_node *node)
{
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	ir_node  *block = be_transform_node(get_nodes_block(node));
	ir_graph *irg   = env.irg;
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_mode  *mode  = get_irn_mode(node);
	ir_op    *op    = get_irn_op(node);
	ir_node  *new_node;
	int      i, arity;

	arity = get_irn_arity(node);
	if (op->opar == oparity_dynamic) {
		new_node = new_ir_node(dbgi, irg, block, op, mode, -1, NULL);
		for (i = 0; i < arity; ++i) {
			ir_node *in = get_irn_n(node, i);
			in = be_transform_node(in);
			add_irn_n(new_node, in);
		}
	} else {
136
		ir_node **ins = ALLOCAN(ir_node*, arity);
137
138
139
140
141
142
143
144
		for (i = 0; i < arity; ++i) {
			ir_node *in = get_irn_n(node, i);
			ins[i] = be_transform_node(in);
		}

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

145
	copy_node_attr(irg, node, new_node);
146
147
148
149
150
151
	be_duplicate_deps(node, new_node);

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

152
153
ir_node *be_transform_node(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
154
155
156
	ir_op             *op;
	ir_node           *new_node = be_get_transformed_node(node);
	be_transform_func *transform;
157

158
	if (new_node != NULL)
159
160
161
162
163
		return new_node;

	DEBUG_ONLY(be_set_transformed_node(node, NULL));

	op = get_irn_op(node);
164
165
	if (op->ops.generic == NULL) {
		panic("No transform function registered for node %+F.", node);
166
	}
Michael Beck's avatar
Michael Beck committed
167
	transform = (be_transform_func *)op->ops.generic;
168
169
170

	new_node = transform(node);
	assert(new_node != NULL);
171
172
173
174
175
176

	be_set_transformed_node(node, new_node);
	hook_dead_node_elim_subst(current_ir_graph, node, new_node);
	return new_node;
}

177
178
void be_enqueue_preds(ir_node *node)
{
179
180
181
182
183
184
185
186
187
188
189
190
191
	int i, arity;

	/* put the preds in the worklist */
	arity = get_irn_arity(node);
	for (i = 0; i < arity; ++i) {
		ir_node *pred = get_irn_n(node, i);
		pdeq_putr(env.worklist, pred);
	}
}

/**
 * Rewire nodes which are potential loops (like Phis) to avoid endless loops.
 */
192
193
static void fix_loops(ir_node *node)
{
194
	int i, arity;
195
	int changed;
196

Matthias Braun's avatar
Matthias Braun committed
197
198
	assert(node_is_in_irgs_storage(env.irg, node));

199
	if (irn_visited_else_mark(node))
200
201
		return;

202
	changed = 0;
203
204
	if (! is_Block(node)) {
		ir_node *block     = get_nodes_block(node);
205
		ir_node *new_block = (ir_node*)get_irn_link(block);
206
207
208
209

		if (new_block != NULL) {
			set_nodes_block(node, new_block);
			block = new_block;
210
			changed = 1;
211
212
213
214
215
216
217
218
		}

		fix_loops(block);
	}

	arity = get_irn_arity(node);
	for (i = 0; i < arity; ++i) {
		ir_node *in = get_irn_n(node, i);
219
		ir_node *nw = (ir_node*)get_irn_link(in);
220
221
222
223

		if (nw != NULL && nw != in) {
			set_irn_n(node, i, nw);
			in = nw;
224
			changed = 1;
225
226
227
228
		}

		fix_loops(in);
	}
229
	/* fix proj block */
230
	if (is_Proj(node)) {
231
		set_nodes_block(node, get_nodes_block(get_Proj_pred(node)));
232
		changed = 1;
233
	}
234
235
236
237

	arity = get_irn_deps(node);
	for (i = 0; i < arity; ++i) {
		ir_node *in = get_irn_dep(node, i);
238
		ir_node *nw = (ir_node*)get_irn_link(in);
239
240
241
242

		if (nw != NULL && nw != in) {
			set_irn_dep(node, i, nw);
			in = nw;
243
			changed = 1;
244
245
246
247
		}

		fix_loops(in);
	}
248

249
	if (changed) {
250
		identify_remember(node);
251
	}
252
253
}

254
255
ir_node *be_pre_transform_node(ir_node *place)
{
256
257
258
259
260
261
	if (place == NULL)
		return NULL;

	return be_transform_node(place);
}

262
263
264
265
266
267
268
static void pre_transform_anchor(int anchor)
{
	ir_node *old_anchor_node = get_irn_n(env.old_anchor, anchor);
	ir_node *transformed     = be_transform_node(old_anchor_node);
	set_irg_anchor(current_ir_graph, anchor, transformed);
}

269
270
271
272
static void kill_unused_anchor(int anchor)
{
	ir_node *old_anchor_node = get_irn_n(env.old_anchor, anchor);
	ir_node *old_bad         = get_irn_n(env.old_anchor, anchor_bad);
273
	if (old_anchor_node != NULL && get_irn_n_edges(old_anchor_node) <= 1) {
274
275
276
277
		set_irn_n(env.old_anchor, anchor, old_bad);
	}
}

278
279
static ir_node *new_be_Anchor(ir_graph *irg)
{
280
	struct obstack *obst = be_get_be_obst(irg);
281
282
283
284
285
	backend_info_t *info;
	ir_node        *new_anchor;

	/* Hack: some places in the code ask the Anchor for its register
	   requirements */
286
	new_anchor = new_r_Anchor(irg);
287
288
289
290
291
292
293
294
	info = be_get_info(new_anchor);
	info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
	memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
	info->out_infos[0].req = arch_no_register_req;

	return new_anchor;
}

295
296
297
/**
 * Transforms all nodes. Deletes the old obstack and creates a new one.
 */
298
static void transform_nodes(ir_graph *irg, arch_pretrans_nodes *pre_transform)
299
300
{
	int       i;
301
	ir_node  *old_end, *new_anchor;
302
303
304
305
306
307
308

	hook_dead_node_elim(irg, 1);

	inc_irg_visited(irg);

	env.irg         = irg;
	env.worklist    = new_waitq();
309
	env.old_anchor  = irg->anchor;
310
311
312
313

	old_end = get_irg_end(irg);

	/* put all anchor nodes in the worklist */
314
315
	for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
		ir_node *anchor = get_irg_anchor(irg, i);
316
317
318
319
320
321

		if (anchor == NULL)
			continue;
		waitq_put(env.worklist, anchor);
	}

322
	new_anchor  = new_be_Anchor(irg);
323
	irg->anchor = new_anchor;
324

325
326
	/* pre transform some anchors (so they are available in the other transform
	 * functions) */
327
328
329
330
331
	pre_transform_anchor(anchor_bad);
	pre_transform_anchor(anchor_no_mem);
	pre_transform_anchor(anchor_start_block);
	pre_transform_anchor(anchor_start);
	pre_transform_anchor(anchor_frame);
332
	kill_unused_anchor(anchor_tls);
333
334

	if (pre_transform)
335
		pre_transform();
336
337
338

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

	/* fix loops and set new anchors*/
	inc_irg_visited(irg);
345
	for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
346
		ir_node *anchor = get_irn_n(env.old_anchor, i);
347
348
349
350

		if (anchor == NULL)
			continue;

351
		anchor = (ir_node*)get_irn_link(anchor);
352
		fix_loops(anchor);
353
		set_irn_n(new_anchor, i, anchor);
354
355
356
357
358
359
360
	}

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

361
362
363
/**
 * Transform helper for blocks.
 */
364
365
static ir_node *gen_Block(ir_node *node)
{
366
367
	ir_graph *irg             = current_ir_graph;
	dbg_info *dbgi            = get_irn_dbg_info(node);
368
369
370
371
	ir_node  *block;

	block = new_ir_node(dbgi, irg, NULL, get_irn_op(node), get_irn_mode(node),
	                    get_irn_arity(node), get_irn_in(node) + 1);
372
	copy_node_attr(irg, node, block);
373
	block->node_nr = node->node_nr;
374

375
376
377
378
379
380
	/* put the preds in the worklist */
	be_enqueue_preds(node);

	return block;
}

381
382
static ir_node *gen_End(ir_node *node)
{
383
384
385
386
387
388
389
390
	/* end has to be duplicated manually because we need a dynamic in array */
	ir_graph *irg   = current_ir_graph;
	dbg_info *dbgi  = get_irn_dbg_info(node);
	ir_node  *block = be_transform_node(get_nodes_block(node));
	int      i, arity;
	ir_node  *new_end;

	new_end = new_ir_node(dbgi, irg, block, op_End, mode_X, -1, NULL);
391
	copy_node_attr(irg, node, new_end);
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
	be_duplicate_deps(node, new_end);

	set_irg_end(irg, new_end);

	/* transform preds */
	arity = get_irn_arity(node);
	for (i = 0; i < arity; ++i) {
		ir_node *in     = get_irn_n(node, i);
		ir_node *new_in = be_transform_node(in);

		add_End_keepalive(new_end, new_in);
	}

	return new_end;
}

408
void be_transform_graph(ir_graph *irg, arch_pretrans_nodes *func)
409
410
411
412
413
414
415
416
417
{
	ir_graph *old_current_ir_graph = current_ir_graph;
	struct obstack *old_obst = NULL;
	struct obstack *new_obst = NULL;

	current_ir_graph = irg;

	/* create a new obstack */
	old_obst = irg->obst;
418
	new_obst = XMALLOC(struct obstack);
419
420
421
422
	obstack_init(new_obst);
	irg->obst = new_obst;
	irg->last_node_idx = 0;

423
424
425
426
427
	/* invalidate phase info as (at least vrp info) is used inside the
	 * equivalent/compute_value functions and might replace our newly
	 * created nodes with middleend nodes */
	irg_invalidate_phases(irg);

428
	/* create new value table for CSE */
429
	new_identities(irg);
430

431
432
433
434
	/* enter special helper */
	op_Block->ops.generic = (op_func)gen_Block;
	op_End->ops.generic   = (op_func)gen_End;

435
	/* do the main transformation */
436
	transform_nodes(irg, func);
437
438
439
440
441
442
443
444

	/* free the old obstack */
	obstack_free(old_obst, 0);
	xfree(old_obst);

	/* restore state */
	current_ir_graph = old_current_ir_graph;

445
446
447
448
449
450
451
	/* most analysis info is wrong after transformation */
	free_callee_info(irg);
	free_irg_outs(irg);
	free_trouts();
	free_loop_information(irg);
	set_irg_doms_inconsistent(irg);

452
	be_liveness_invalidate(be_get_irg_liveness(irg));
453
	/* Hack for now, something is buggy with invalidate liveness... */
454
	be_birg_from_irg(irg)->lv = NULL;
455
	be_invalidate_dom_front(irg);
456

457
458
459
460
	/* recalculate edges */
	edges_deactivate(irg);
	edges_activate(irg);
}
461
462
463

int be_mux_is_abs(ir_node *sel, ir_node *mux_true, ir_node *mux_false)
{
464
465
466
467
	ir_node    *cmp_left;
	ir_node    *cmp_right;
	ir_mode    *mode;
	ir_relation relation;
468

469
	if (!is_Cmp(sel))
470
471
472
473
474
475
476
477
478
479
480
481
482
		return 0;

	/**
	 * Note further that these optimization work even for floating point
	 * with NaN's because -NaN == NaN.
	 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
	 * transformations.
	 */
	mode = get_irn_mode(mux_true);
	if (mode_honor_signed_zeros(mode))
		return 0;

	/* must be <, <=, >=, > */
483
484
	relation = get_Cmp_relation(sel);
	if ((relation & ir_relation_less_greater) == 0)
485
486
		return 0;

487
	if (!ir_is_negated_value(mux_true, mux_false))
488
489
490
		return 0;

	/* must be x cmp 0 */
491
	cmp_right = get_Cmp_right(sel);
492
493
494
	if (!is_Const(cmp_right) || !is_Const_null(cmp_right))
		return 0;

495
	cmp_left = get_Cmp_left(sel);
496
	if (cmp_left == mux_false) {
497
		if (relation & ir_relation_less) {
498
499
			return 1;
		} else {
500
			assert(relation & ir_relation_greater);
501
502
503
			return -1;
		}
	} else if (cmp_left == mux_true) {
504
		if (relation & ir_relation_less) {
505
506
			return -1;
		} else {
507
			assert(relation & ir_relation_greater);
508
509
510
511
512
513
514
515
516
			return 1;
		}
	}

	return 0;
}

ir_node *be_get_abs_op(ir_node *sel)
{
517
	ir_node *cmp_left = get_Cmp_left(sel);
518
519
	return cmp_left;
}