irgmod.c 8.56 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */

Matthias Braun's avatar
Matthias Braun committed
20
21
22
23
24
/**
 * @file
 * @brief    Support for ir graph modification.
 * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier
 * @version  $Id$
Götz Lindenmaier's avatar
Götz Lindenmaier committed
25
 */
Matthias Braun's avatar
Matthias Braun committed
26
#include "config.h"
Boris Boesler's avatar
added    
Boris Boesler committed
27

Michael Beck's avatar
Michael Beck committed
28
29
30
31
32
33
34
35
36
37
38
#include "irvrfy.h"
#include "irflag_t.h"
#include "irgwalk.h"
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irgmod.h"
#include "array.h"
#include "ircons.h"
#include "irhooks.h"
#include "iredges_t.h"
#include "irtools.h"
39
#include "error.h"
Christian Schäfer's avatar
Christian Schäfer committed
40

Christian Würdig's avatar
Christian Würdig committed
41
42
43
44
45
46
/**
 * Turns a node into a "useless" Tuple.  The Tuple just forms a tuple
 * from several inputs.
 * This is useful if a node returning a tuple is removed, but the Projs
 * extracting values from the tuple are not available.
 */
47
void turn_into_tuple(ir_node *node, int arity) {
Christian Würdig's avatar
Christian Würdig committed
48
49
50
51
52
	assert(node);
	set_irn_op(node, op_Tuple);
	if (get_irn_arity(node) == arity) {
		/* keep old array */
	} else {
Michael Beck's avatar
Michael Beck committed
53
54
55
		ir_graph *irg = get_irn_irg(node);
		ir_node *block = get_nodes_block(node);
		edges_node_deleted(node, irg);
Michael Beck's avatar
Michael Beck committed
56
		/* Allocate new array, don't free old in_array, it's on the obstack. */
Michael Beck's avatar
Michael Beck committed
57
		node->in = NEW_ARR_D(ir_node *, irg->obst, arity+1);
Christian Würdig's avatar
Christian Würdig committed
58
59
		/* clear the new in array, else edge_notify tries to delete garbage */
		memset(node->in, 0, (arity+1) * sizeof(node->in[0]));
Michael Beck's avatar
Michael Beck committed
60
		/* set the block back */
Michael Beck's avatar
Michael Beck committed
61
		set_nodes_block(node, block);
Christian Würdig's avatar
Christian Würdig committed
62
	}
Christian Schäfer's avatar
Christian Schäfer committed
63
64
}

Christian Würdig's avatar
Christian Würdig committed
65
66
67
68
69
/**
 * Insert irnode `new' in place of irnode `old'
 * Since `new' may be bigger than `old' replace `old'
 * by an op_Id which is smaller than everything.
 */
70
void exchange(ir_node *old, ir_node *nw) {
Christian Würdig's avatar
Christian Würdig committed
71
72
73
74
75
76
77
78
79
80
81
	ir_graph *irg;

	assert(old && nw);
	assert(old != nw && "Exchanging node with itself is not allowed");

	irg = get_irn_irg(old);
	assert(irg == get_irn_irg(nw) && "New node must be in same irg as old node");

	hook_replace(old, nw);

	/*
82
83
84
	 * If new outs are on, we can skip the id node creation and reroute
	 * the edges from the old node to the new directly.
	 */
Christian Würdig's avatar
Christian Würdig committed
85
86
87
88
89
90
91
92
	if (edges_activated(irg)) {
		/* copy all dependencies from old to new */
		add_irn_deps(nw, old);

		edges_reroute(old, nw, irg);
		edges_reroute_kind(old, nw, EDGE_KIND_DEP, irg);
		edges_node_deleted(old, irg);
		old->op = op_Bad;
93
	} else {
Christian Würdig's avatar
Christian Würdig committed
94
95
96
97
98
99
100
101
102
103
		/* Else, do it the old-fashioned way. */
		ir_node *block;

		hook_turn_into_id(old);

		block = old->in[0];
		if (! block) {
			block = is_Block(nw) ? nw : get_nodes_block(nw);

			if (! block) {
104
				panic("cannot find legal block for id");
Christian Würdig's avatar
Christian Würdig committed
105
106
107
			}
		}

108
109
110
111
		if (get_irn_op(old)->opar == oparity_dynamic) {
			DEL_ARR_F(get_irn_in(old));
		}

Christian Würdig's avatar
Christian Würdig committed
112
113
114
115
116
		old->op    = op_Id;
		old->in    = NEW_ARR_D (ir_node *, irg->obst, 2);
		old->in[0] = block;
		old->in[1] = nw;
	}
Christian Schäfer's avatar
Christian Schäfer committed
117
}
118

119
120
121
/*--------------------------------------------------------------------*/
/*  Functionality for collect_phis                                    */
/*--------------------------------------------------------------------*/
122

Michael Beck's avatar
Michael Beck committed
123
/**
124
125
126
 * Walker: links all Phi nodes to their Blocks lists,
 *         all Proj nodes to there predecessors and all
 *         partBlocks to there MacroBlock header.
Michael Beck's avatar
Michael Beck committed
127
 */
128
static void collect_phiprojs_walker(ir_node *n, void *env) {
Christian Würdig's avatar
Christian Würdig committed
129
	ir_node *pred;
130
	(void) env;
Christian Würdig's avatar
Christian Würdig committed
131
132

	if (is_Phi(n)) {
133
		ir_node *block = get_nodes_block(n);
Michael Beck's avatar
Michael Beck committed
134
		add_Block_phi(block, n);
135
	} else if (is_Proj(n)) {
Christian Würdig's avatar
Christian Würdig committed
136
137
138
139
140
141
142
		pred = n;
		do {
			pred = get_Proj_pred(pred);
		} while (is_Proj(pred));

		set_irn_link(n, get_irn_link(pred));
		set_irn_link(pred, n);
143
144
145
146
147
148
149
	} else if (is_Block(n)) {
		ir_node *mbh = get_Block_MacroBlock(n);

		if (mbh != n) {
			set_irn_link(n, get_irn_link(mbh));
			set_irn_link(mbh, n);
		}
Christian Würdig's avatar
Christian Würdig committed
150
	}
151
152
}

153
154
155
/**
 * clear all links, including the Phi list of blocks and Phi nodes.
 */
156
static void clear_node_and_phis_links(ir_node *n, void *env) {
157
158
159
160
161
162
163
164
165
	(void) env;

	set_irn_link(n, NULL);
	if (is_Block(n))
		set_Block_phis(n, NULL);
	else if (is_Phi(n))
		set_Phi_next(n, NULL);
}

166
void collect_phiprojs(ir_graph *irg) {
167
168
	assert(ir_resources_reserved(irg) & (IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST) ==
		(IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST));
169
	irg_walk_graph(irg, clear_node_and_phis_links, collect_phiprojs_walker, NULL);
170
171
}

172
173
174
/*--------------------------------------------------------------------*/
/*  Functionality for part_block                                      */
/*--------------------------------------------------------------------*/
175

Michael Beck's avatar
Michael Beck committed
176
177
178
179
/**
 * Moves node and all predecessors of node from from_bl to to_bl.
 * Does not move predecessors of Phi nodes (or block nodes).
 */
Christian Würdig's avatar
Christian Würdig committed
180
181
static void move(ir_node *node, ir_node *from_bl, ir_node *to_bl) {
	int i, arity;
182
183
184
185

	/* move this node */
	set_nodes_block(node, to_bl);

Michael Beck's avatar
Michael Beck committed
186
	/* move its Projs */
187
	if (get_irn_mode(node) == mode_T) {
Michael Beck's avatar
Michael Beck committed
188
		ir_node *proj = get_irn_link(node);
189
190
191
192
193
194
		while (proj) {
			if (get_nodes_block(proj) == from_bl)
				set_nodes_block(proj, to_bl);
			proj = get_irn_link(proj);
		}
	}
Christian Würdig's avatar
Christian Würdig committed
195
196

	/* recursion ... */
Michael Beck's avatar
Michael Beck committed
197
198
	if (is_Phi(node))
		return;
Christian Würdig's avatar
Christian Würdig committed
199
200
201

	arity = get_irn_arity(node);
	for (i = 0; i < arity; i++) {
Michael Beck's avatar
Michael Beck committed
202
		ir_node *pred = get_irn_n(node, i);
Christian Würdig's avatar
Christian Würdig committed
203
204
205
		if (get_nodes_block(pred) == from_bl)
			move(pred, from_bl, to_bl);
	}
206
207
208
}

void part_block(ir_node *node) {
Christian Würdig's avatar
Christian Würdig committed
209
210
211
	ir_node *new_block;
	ir_node *old_block;
	ir_node *phi;
212
	ir_node *mbh;
213
	ir_node *next, *block;
Christian Würdig's avatar
Christian Würdig committed
214
215
216
217
218
219
220

	/* Turn off optimizations so that blocks are not merged again. */
	int rem_opt = get_opt_optimize();
	set_optimize(0);

	/* Transform the control flow */
	old_block = get_nodes_block(node);
221
	mbh       = get_Block_MacroBlock(old_block);
Christian Würdig's avatar
Christian Würdig committed
222
	new_block = new_Block(get_Block_n_cfgpreds(old_block),
223
	                      get_Block_cfgpred_arr(old_block));
224
225
226

	if (mbh != old_block) {
		/* we splitting a partBlock */
227
		set_Block_MacroBlock(new_block, mbh);
228
229
	} else {
		/* we are splitting a header: this creates a new header */
230
		set_Block_MacroBlock(new_block, new_block);
231
	}
Christian Würdig's avatar
Christian Würdig committed
232
233
234
235
236
237
238
239
240
241
242
	set_irg_current_block(current_ir_graph, new_block);
	{
		ir_node *jmp = new_Jmp();
		set_irn_in(old_block, 1, &jmp);
		irn_vrfy_irg(old_block, current_ir_graph);
	}

	/* move node and its predecessors to new_block */
	move(node, old_block, new_block);

	/* move Phi nodes to new_block */
Michael Beck's avatar
Michael Beck committed
243
244
245
	phi = get_Block_phis(old_block);
	set_Block_phis(new_block, phi);
	set_Block_phis(old_block, NULL);
Christian Würdig's avatar
Christian Würdig committed
246
247
	while (phi) {
		set_nodes_block(phi, new_block);
Michael Beck's avatar
Michael Beck committed
248
		phi = get_Phi_next(phi);
Christian Würdig's avatar
Christian Würdig committed
249
250
	}

251
252
	/* rewire partBlocks */
	if (mbh != old_block) {
253
254
255
256
		ir_node *list = NULL;

		/* move blocks from mbh to old_block if old_block dominates them */
		block = get_irn_link(mbh);
Michael Beck's avatar
Michael Beck committed
257
258

		set_irn_link(mbh, NULL);
259
		set_Block_MacroBlock(old_block, old_block);
260

Michael Beck's avatar
Michael Beck committed
261
262
		/* note that we must splice the list of partBlock here */
		for (; block != NULL; block = next) {
263
			ir_node *curr = block;
Michael Beck's avatar
Michael Beck committed
264
265
266
			assert(is_Block(curr));

			next = get_irn_link(block);
267
268
269
270

			if (block == old_block)
				continue;

271
			assert(get_Block_MacroBlock(curr) == mbh);
Michael Beck's avatar
Michael Beck committed
272

273
274
275
276
			for (;;) {
				if (curr == old_block) {
					/* old_block dominates the block, so old_block will be
					   the new macro block header */
277
					set_Block_MacroBlock(block, old_block);
278
279
					set_irn_link(block, list);
					list = block;
280
281
					break;
				}
Michael Beck's avatar
Michael Beck committed
282
283
284
285
				if (curr == mbh) {
					/* leave it in the mbh */
					set_irn_link(block, get_irn_link(mbh));
					set_irn_link(mbh, block);
286
					break;
Michael Beck's avatar
Michael Beck committed
287
				}
288
289
290
291
292

				assert(get_Block_n_cfgpreds(curr) == 1);
				curr = get_Block_cfgpred_block(curr, 0);
			}
		}
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
		/* beware: do NOT directly manipulate old_block's list, as old_block is
		   in mbh's list and this would destroy the list! */
		set_irn_link(old_block, list);

		/* finally add new_block to mbh's list */
		set_irn_link(new_block, get_irn_link(mbh));
		set_irn_link(mbh, new_block);
	} else {
		/* move blocks from mbh to new_block */
		block = get_irn_link(mbh);

		set_irn_link(mbh, NULL);
		set_irn_link(new_block, NULL);

		for (; block != NULL; block = next) {
			next = get_irn_link(block);

			set_Block_MacroBlock(block, new_block);
			set_irn_link(block, get_irn_link(new_block));
			set_irn_link(new_block, block);
		}
314
315
	}

Christian Würdig's avatar
Christian Würdig committed
316
	set_optimize(rem_opt);
317
}
318
319
320
321
322
323
324
325
326
327
328
329

/* kill a node by setting its predecessors to Bad and finally exchange the node by Bad itself. */
void kill_node(ir_node *node) {
	ir_graph *irg = get_irn_irg(node);
	ir_node *bad = get_irg_bad(irg);
	int i;

	for (i = get_irn_arity(node) - 1; i >= -1; --i) {
		set_irn_n(node, i, bad);
	}
	exchange(node, bad);
}