betranshlp.c 10.7 KB
Newer Older
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 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
42
43
44
45
46
47
48
#include "irhooks.h"
#include "iredges.h"
#include "irouts.h"
#include "trouts.h"
#include "cgana.h"
#include "debug.h"

#include "beirg_t.h"
#include "betranshlp.h"

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 */
49
	ir_node  *old_anchor;  /**< the old anchor node in the old irg */
50
51
52
53
54
} be_transform_env_t;


static be_transform_env_t env;

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

61
62
63
64
int be_is_transformed(const ir_node *node) {
	return irn_visited(node);
}

65
static inline ir_node *be_get_transformed_node(ir_node *old_node) {
66
67
68
69
70
71
	if (irn_visited(old_node)) {
		ir_node *new_node = get_irn_link(old_node);
		assert(new_node != NULL);
		return new_node;
	}
	return NULL;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
}

void be_duplicate_deps(ir_node *old_node, ir_node *new_node) {
	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);
	}
}

86
87
88
89
90
91
92
93
void be_dep_on_frame(ir_node *const node)
{
	ir_graph *const irg = current_ir_graph;

	if (get_irg_start_block(irg) == get_nodes_block(node))
		add_irn_dep(node, get_irg_frame(irg));
}

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
ir_node *be_duplicate_node(ir_node *node) {
	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 {
112
		ir_node **ins = ALLOCAN(ir_node*, arity);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
		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);
	}

	copy_node_attr(node, new_node);
	be_duplicate_deps(node, new_node);

#ifdef DEBUG_libfirm
	new_node->node_nr = node->node_nr;
#endif

	return new_node;
}

/**
 * Calls transformation function for given node and marks it visited.
 */
ir_node *be_transform_node(ir_node *node) {
	ir_op   *op;
136
	ir_node *new_node = be_get_transformed_node(node);
137

138
	if (new_node != NULL)
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		return new_node;

	DEBUG_ONLY(be_set_transformed_node(node, NULL));

	op = get_irn_op(node);
	if (op->ops.generic) {
		be_transform_func *transform = (be_transform_func *)op->ops.generic;

		new_node = transform(node);
		assert(new_node != NULL);
	} else {
		new_node = be_duplicate_node(node);
	}

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

/**
 * enqueue all inputs into the transform queue.
 */
void be_enqueue_preds(ir_node *node) {
	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.
 */
static void fix_loops(ir_node *node) {
	int i, arity;
177
	int changed;
178

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

181
	if (irn_visited_else_mark(node))
182
183
		return;

184
	changed = 0;
185
186
187
188
189
190
191
	if (! is_Block(node)) {
		ir_node *block     = get_nodes_block(node);
		ir_node *new_block = get_irn_link(block);

		if (new_block != NULL) {
			set_nodes_block(node, new_block);
			block = new_block;
192
			changed = 1;
193
194
195
196
197
198
199
200
201
202
203
204
205
		}

		fix_loops(block);
	}

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

		if (nw != NULL && nw != in) {
			set_irn_n(node, i, nw);
			in = nw;
206
			changed = 1;
207
208
209
210
		}

		fix_loops(in);
	}
211
212
213
	/* fix proj block */
	if(is_Proj(node)) {
		set_nodes_block(node, get_nodes_block(get_Proj_pred(node)));
214
		changed = 1;
215
	}
216
217
218
219
220
221
222
223
224

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

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

		fix_loops(in);
	}
230
231
232
233

	if(changed) {
		identify_remember(current_ir_graph->value_table, node);
	}
234
235
236
237
238
239
240
241
242
}

ir_node *be_pre_transform_node(ir_node *place) {
	if (place == NULL)
		return NULL;

	return be_transform_node(place);
}

243
244
245
246
247
248
249
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);
}

250
251
252
253
254
255
256
257
258
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);
	if(old_anchor_node != NULL && get_irn_n_edges(old_anchor_node) <= 1) {
		set_irn_n(env.old_anchor, anchor, old_bad);
	}
}

259
260
261
/**
 * Transforms all nodes. Deletes the old obstack and creates a new one.
 */
262
static void transform_nodes(ir_graph *irg, arch_pretrans_nodes *pre_transform)
263
264
{
	int       i;
265
	ir_node  *old_end, *new_anchor;
266
267
268
269
270
271
272

	hook_dead_node_elim(irg, 1);

	inc_irg_visited(irg);

	env.irg         = irg;
	env.worklist    = new_waitq();
273
	env.old_anchor  = irg->anchor;
274
275
276
277

	old_end = get_irg_end(irg);

	/* put all anchor nodes in the worklist */
278
279
	for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
		ir_node *anchor = get_irg_anchor(irg, i);
280
281
282
283
284
285

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

286
287
	new_anchor  = new_Anchor(irg);
	irg->anchor = new_anchor;
288

289
290
	/* pre transform some anchors (so they are available in the other transform
	 * functions) */
291
292
293
294
295
	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);
296
	kill_unused_anchor(anchor_tls);
297
298

	if (pre_transform)
299
		pre_transform();
300
301
302
303
304
305
306
307
308

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

	/* fix loops and set new anchors*/
	inc_irg_visited(irg);
309
	for (i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
310
		ir_node *anchor = get_irn_n(env.old_anchor, i);
311
312
313
314
315
316

		if (anchor == NULL)
			continue;

		anchor = get_irn_link(anchor);
		fix_loops(anchor);
317
		set_irn_n(new_anchor, i, anchor);
318
	}
319
	set_nodes_block(new_anchor, get_irg_anchor(irg, anchor_end_block));
320
321
322
323
324
325

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

326
327
328
329
/**
 * Transform helper for blocks.
 */
static ir_node *gen_Block(ir_node *node) {
330
331
332
	ir_graph *irg             = current_ir_graph;
	dbg_info *dbgi            = get_irn_dbg_info(node);
	ir_node  *old_start_block = get_irn_n(env.old_anchor, anchor_start_block);
333
	ir_node  *macroblock      = get_Block_MacroBlock(node);
334
335
336
337
338
339
	ir_node  *block;

	/*
	 * We replace the ProjX from the start node with a jump,
	 * so the startblock has no preds anymore now
	 */
340
	if (node == old_start_block) {
341
342
343
344
345
346
347
348
349
350
351
		return new_rd_Block(dbgi, irg, 0, NULL);
	}

	/* we use the old blocks for now, because jumps allow cycles in the graph
	 * we have to fix this later */
	block = new_ir_node(dbgi, irg, NULL, get_irn_op(node), get_irn_mode(node),
	                    get_irn_arity(node), get_irn_in(node) + 1);
	copy_node_attr(node, block);
#ifdef DEBUG_libfirm
	block->node_nr = node->node_nr;
#endif
352
353
	if (node == macroblock) {
		/* this node is a macroblock header */
354
		set_Block_MacroBlock(block, block);
355
356
	} else {
		macroblock = be_transform_node(macroblock);
357
		set_Block_MacroBlock(block, macroblock);
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
	/* put the preds in the worklist */
	be_enqueue_preds(node);

	return block;
}

static ir_node *gen_End(ir_node *node) {
	/* 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);
	copy_node_attr(node, new_end);
	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;
}

392
void be_transform_graph(be_irg_t *birg, arch_pretrans_nodes *func)
393
394
395
396
397
398
399
400
401
402
{
	ir_graph *irg = birg->irg;
	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;
403
	new_obst = XMALLOC(struct obstack);
404
405
406
407
408
409
410
411
	obstack_init(new_obst);
	irg->obst = new_obst;
	irg->last_node_idx = 0;

	/* create new value table for CSE */
	del_identities(irg->value_table);
	irg->value_table = new_identities();

412
413
414
415
	/* enter special helper */
	op_Block->ops.generic = (op_func)gen_Block;
	op_End->ops.generic   = (op_func)gen_End;

416
	/* do the main transformation */
417
	transform_nodes(irg, func);
418
419
420
421
422
423
424
425

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

	/* restore state */
	current_ir_graph = old_current_ir_graph;

426
427
428
429
430
431
432
433
434
435
436
437
	/* 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);

	be_liveness_invalidate(be_get_birg_liveness(birg));
	/* Hack for now, something is buggy with invalidate liveness... */
	birg->lv = NULL;
	be_invalidate_dom_front(birg);

438
439
440
	/* recalculate edges */
	edges_deactivate(irg);
	edges_activate(irg);
Sebastian Hack's avatar
Sebastian Hack committed
441
442
443

	if (birg->lv) {
		be_liveness_free(birg->lv);
Sebastian Hack's avatar
Sebastian Hack committed
444
		birg->lv = be_liveness(birg);
Sebastian Hack's avatar
Sebastian Hack committed
445
	}
446
}