betranshlp.c 10.2 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
 * @date        14.06.2007
 */
#include "config.h"

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

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

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


static be_transform_env_t env;

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

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

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

79
80
void be_duplicate_deps(ir_node *old_node, ir_node *new_node)
{
81
	int deps = get_irn_deps(old_node);
Matthias Braun's avatar
Matthias Braun committed
82
	for (int i = 0; i < deps; ++i) {
83
84
85
86
87
88
89
		ir_node *dep     = get_irn_dep(old_node, i);
		ir_node *new_dep = be_transform_node(dep);

		add_irn_dep(new_node, new_dep);
	}
}

90
91
92
93
94
95
96
97
98
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;
}

99
100
101
102
103
/**
 * Transform helper for blocks.
 */
static ir_node *transform_block(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
104
105
106
107
108
	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);
109
110
111
	copy_node_attr(irg, node, block);
	block->node_nr = node->node_nr;

112
113
114
115
	/* transfer execfreq value */
	double execfreq = get_block_execfreq(node);
	set_block_execfreq(block, execfreq);

116
117
118
119
120
121
122
123
124
	/* 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
125
126
127
128
	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);
129
130
131
132
133
134
135
	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 */
Matthias Braun's avatar
Matthias Braun committed
136
137
	int arity = get_irn_arity(node);
	for (int i = 0; i < arity; ++i) {
138
139
140
141
142
143
144
145
		ir_node *in = get_irn_n(node, i);
		add_End_keepalive(new_end, in);
	}
	be_enqueue_preds(node);

	return new_end;
}

146
147
void be_start_transform_setup(void)
{
Matthias Braun's avatar
Matthias Braun committed
148
	ir_clear_opcodes_generic_func();
149
150
151
152

	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);
153
	be_set_transform_function(op_be_IncSP,    be_duplicate_node);
154
	be_set_transform_function(op_be_Keep,     be_duplicate_node);
155
156
	be_set_transform_function(op_be_Return,   be_duplicate_node);
	be_set_transform_function(op_be_Start,    be_duplicate_node);
157
158
	be_set_transform_function(op_Block,       transform_block);
	be_set_transform_function(op_End,         transform_end);
159
160
161
162
	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);
163
164
}

165
166
ir_node *be_duplicate_node(ir_node *node)
{
167
168
169
170
171
172
	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);

Matthias Braun's avatar
Matthias Braun committed
173
174
	ir_node *new_node;
	int      arity = get_irn_arity(node);
175
176
	if (op->opar == oparity_dynamic) {
		new_node = new_ir_node(dbgi, irg, block, op, mode, -1, NULL);
Matthias Braun's avatar
Matthias Braun committed
177
		for (int i = 0; i < arity; ++i) {
178
179
180
181
182
			ir_node *in = get_irn_n(node, i);
			in = be_transform_node(in);
			add_irn_n(new_node, in);
		}
	} else {
183
		ir_node **ins = ALLOCAN(ir_node*, arity);
Matthias Braun's avatar
Matthias Braun committed
184
		for (int i = 0; i < arity; ++i) {
185
186
187
188
189
190
191
			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);
	}

192
	copy_node_attr(irg, node, new_node);
193
194
195
196
197
198
	be_duplicate_deps(node, new_node);

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

199
200
ir_node *be_transform_node(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
201
	ir_node *new_node = be_get_transformed_node(node);
202
	if (new_node != NULL)
203
204
		return new_node;

205
	DEBUG_ONLY(be_set_transformed_node(node, NULL);)
206

Matthias Braun's avatar
Matthias Braun committed
207
	ir_op *op = get_irn_op(node);
208
209
	if (op->ops.generic == NULL) {
		panic("No transform function registered for node %+F.", node);
210
	}
Matthias Braun's avatar
Matthias Braun committed
211
	be_transform_func *transform = (be_transform_func *)op->ops.generic;
212
213
214

	new_node = transform(node);
	assert(new_node != NULL);
215
216
217
218
219

	be_set_transformed_node(node, new_node);
	return new_node;
}

220
221
void be_enqueue_preds(ir_node *node)
{
222
	/* put the preds in the worklist */
Matthias Braun's avatar
Matthias Braun committed
223
224
	int arity = get_irn_arity(node);
	for (int i = 0; i < arity; ++i) {
225
226
227
228
229
230
231
232
		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.
 */
233
234
static void fix_loops(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
235
236
	assert(node_is_in_irgs_storage(env.irg, node));

237
	if (irn_visited_else_mark(node))
238
239
		return;

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

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

		fix_loops(block);
	}

Matthias Braun's avatar
Matthias Braun committed
254
255
	int arity = get_irn_arity(node);
	for (int i = 0; i < arity; ++i) {
256
		ir_node *in = get_irn_n(node, i);
257
		ir_node *nw = (ir_node*)get_irn_link(in);
258
259
260
261

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

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

	arity = get_irn_deps(node);
Matthias Braun's avatar
Matthias Braun committed
274
	for (int i = 0; i < arity; ++i) {
275
		ir_node *in = get_irn_dep(node, i);
276
		ir_node *nw = (ir_node*)get_irn_link(in);
277
278
279
280

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

		fix_loops(in);
	}
286

287
	if (changed) {
288
		identify_remember(node);
289
	}
290
291
}

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

	return be_transform_node(place);
}

300
static void pre_transform_anchor(ir_graph *irg, int anchor)
301
302
303
{
	ir_node *old_anchor_node = get_irn_n(env.old_anchor, anchor);
	ir_node *transformed     = be_transform_node(old_anchor_node);
304
	set_irg_anchor(irg, anchor, transformed);
305
306
}

307
308
309
/**
 * Transforms all nodes. Deletes the old obstack and creates a new one.
 */
310
static void transform_nodes(ir_graph *irg, arch_pretrans_nodes *pre_transform)
311
{
312
313
314
315
	hook_dead_node_elim(irg, 1);

	inc_irg_visited(irg);

316
317
318
	env.irg        = irg;
	env.worklist   = new_waitq();
	env.old_anchor = irg->anchor;
319

Matthias Braun's avatar
Matthias Braun committed
320
	ir_node *old_end = get_irg_end(irg);
321
322

	/* put all anchor nodes in the worklist */
Matthias Braun's avatar
Matthias Braun committed
323
	for (int i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
324
		ir_node *anchor = get_irg_anchor(irg, i);
325
326
327
328
329
330

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

Matthias Braun's avatar
Matthias Braun committed
331
	ir_node *new_anchor  = new_r_Anchor(irg);
332
	irg->anchor = new_anchor;
333

334
335
	/* pre transform some anchors (so they are available in the other transform
	 * functions) */
336
337
338
339
340
341
	pre_transform_anchor(irg, anchor_no_mem);
	pre_transform_anchor(irg, anchor_end_block);
	pre_transform_anchor(irg, anchor_end);
	pre_transform_anchor(irg, anchor_start_block);
	pre_transform_anchor(irg, anchor_start);
	pre_transform_anchor(irg, anchor_frame);
342
343

	if (pre_transform)
344
		pre_transform();
345
346
347

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

	/* fix loops and set new anchors*/
	inc_irg_visited(irg);
Matthias Braun's avatar
Matthias Braun committed
354
	for (int i = get_irg_n_anchors(irg) - 1; i >= 0; --i) {
355
		ir_node *anchor = get_irn_n(env.old_anchor, i);
356
357
358
359

		if (anchor == NULL)
			continue;

360
		anchor = (ir_node*)get_irn_link(anchor);
361
		fix_loops(anchor);
362
		set_irn_n(new_anchor, i, anchor);
363
364
365
366
367
368
369
	}

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

370
void be_transform_graph(ir_graph *irg, arch_pretrans_nodes *func)
371
372
373
374
375
{
	ir_graph *old_current_ir_graph = current_ir_graph;
	current_ir_graph = irg;

	/* create a new obstack */
Matthias Braun's avatar
Matthias Braun committed
376
377
	struct obstack *old_obst = irg->obst;
	struct obstack *new_obst = XMALLOC(struct obstack);
378
379
380
381
	obstack_init(new_obst);
	irg->obst = new_obst;
	irg->last_node_idx = 0;

382
	free_vrp_data(irg);
383

384
	/* create new value table for CSE */
385
	new_identities(irg);
386
387

	/* do the main transformation */
388
	transform_nodes(irg, func);
389
390
391
392
393
394
395
396

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

	/* restore state */
	current_ir_graph = old_current_ir_graph;

397
	/* most analysis info is wrong after transformation */
398
	be_invalidate_live_chk(irg);
Matthias Braun's avatar
Matthias Braun committed
399
	confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
400

401
402
403
	/* recalculate edges */
	edges_activate(irg);
}