irgmod.c 7.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
#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"
38
#include "error.h"
Christian Schäfer's avatar
Christian Schäfer committed
39

Christian Würdig's avatar
Christian Würdig committed
40
41
42
43
44
45
/**
 * 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.
 */
46
47
void turn_into_tuple(ir_node *node, int arity)
{
48
49
	ir_graph *irg = get_irn_irg(node);
	ir_node **in  = ALLOCAN(ir_node*, arity);
Matthias Braun's avatar
Matthias Braun committed
50
	ir_node  *bad = new_r_Bad(irg, mode_ANY);
51
52
53
54
	int       i;

	/* construct a new in array, with every input being bad */
	for (i = 0; i < arity; ++i) {
Matthias Braun's avatar
Matthias Braun committed
55
		in[i] = bad;
Christian Würdig's avatar
Christian Würdig committed
56
	}
57
58
	set_irn_in(node, arity, in);
	set_irn_op(node, op_Tuple);
Christian Schäfer's avatar
Christian Schäfer committed
59
60
}

Christian Würdig's avatar
Christian Würdig committed
61
62
63
64
65
/**
 * 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.
 */
66
67
void exchange(ir_node *old, ir_node *nw)
{
Christian Würdig's avatar
Christian Würdig committed
68
69
70
71
72
73
74
75
76
77
78
	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);

	/*
79
80
81
	 * 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
82
83
84
85
	if (edges_activated(irg)) {
		/* copy all dependencies from old to new */
		add_irn_deps(nw, old);

Matthias Braun's avatar
Matthias Braun committed
86
87
88
		edges_reroute(old, nw);
		edges_reroute_kind(old, nw, EDGE_KIND_DEP);
		edges_node_deleted(old);
89
90
		/* noone is allowed to reference this node anymore */
		set_irn_op(old, op_Deleted);
91
	} else {
Christian Würdig's avatar
Christian Würdig committed
92
93
94
95
96
97
98
99
100
101
		/* 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) {
102
				panic("cannot find legal block for id");
Christian Würdig's avatar
Christian Würdig committed
103
104
105
			}
		}

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

Christian Würdig's avatar
Christian Würdig committed
110
111
112
113
114
		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
115
}
116

117
118
119
/*--------------------------------------------------------------------*/
/*  Functionality for collect_phis                                    */
/*--------------------------------------------------------------------*/
120

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

	if (is_Phi(n)) {
131
		ir_node *block = get_nodes_block(n);
Michael Beck's avatar
Michael Beck committed
132
		add_Block_phi(block, n);
133
	} else if (is_Proj(n)) {
Christian Würdig's avatar
Christian Würdig committed
134
135
136
137
138
139
140
141
		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);
	}
142
143
}

144
145
void collect_phiprojs(ir_graph *irg)
{
146
	assert((ir_resources_reserved(irg) & (IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST)) ==
147
		(IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST));
148
	irg_walk_graph(irg, firm_clear_node_and_phi_links, collect_phiprojs_walker, NULL);
149
150
}

151
152
153
/*--------------------------------------------------------------------*/
/*  Functionality for part_block                                      */
/*--------------------------------------------------------------------*/
154

Michael Beck's avatar
Michael Beck committed
155
156
157
158
/**
 * Moves node and all predecessors of node from from_bl to to_bl.
 * Does not move predecessors of Phi nodes (or block nodes).
 */
159
160
static void move(ir_node *node, ir_node *from_bl, ir_node *to_bl)
{
Christian Würdig's avatar
Christian Würdig committed
161
	int i, arity;
162
163
164
165

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

Michael Beck's avatar
Michael Beck committed
166
	/* move its Projs */
167
	if (get_irn_mode(node) == mode_T) {
168
		ir_node *proj = (ir_node*)get_irn_link(node);
169
170
171
		while (proj) {
			if (get_nodes_block(proj) == from_bl)
				set_nodes_block(proj, to_bl);
172
			proj = (ir_node*)get_irn_link(proj);
173
174
		}
	}
Christian Würdig's avatar
Christian Würdig committed
175

176
177
178
	if (is_Phi(node))
		return;

Christian Würdig's avatar
Christian Würdig committed
179
	/* recursion ... */
180
181
182
183
184
185
186
187
188
189
190
191
	arity = get_irn_arity(node);
	for (i = 0; i < arity; i++) {
		ir_node *pred = get_irn_n(node, i);
		if (get_nodes_block(pred) == from_bl)
			move(pred, from_bl, to_bl);
	}
}

/**
 * Moves node and all predecessors of node from from_bl to to_bl.
 * Does not move predecessors of Phi nodes (or block nodes).
 */
192
static void move_edges(ir_node *node, ir_node *from_bl, ir_node *to_bl)
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
	int i, arity;

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

	/* move its Projs */
	if (get_irn_mode(node) == mode_T) {
		const ir_edge_t *edge;
		foreach_out_edge(node, edge) {
			ir_node *proj = get_edge_src_irn(edge);
			set_nodes_block(proj, to_bl);
		}
	}

	/* We must not move predecessors of Phi nodes, even if they are in
	 * from_bl. (because these are values from an earlier loop iteration
	 * which are not predecessors of node here)
	 */
Michael Beck's avatar
Michael Beck committed
212
213
	if (is_Phi(node))
		return;
Christian Würdig's avatar
Christian Würdig committed
214

215
	/* recursion ... */
Christian Würdig's avatar
Christian Würdig committed
216
217
	arity = get_irn_arity(node);
	for (i = 0; i < arity; i++) {
Michael Beck's avatar
Michael Beck committed
218
		ir_node *pred = get_irn_n(node, i);
Christian Würdig's avatar
Christian Würdig committed
219
		if (get_nodes_block(pred) == from_bl)
220
			move_edges(pred, from_bl, to_bl);
Christian Würdig's avatar
Christian Würdig committed
221
	}
222
223
}

224
225
void part_block(ir_node *node)
{
226
227
228
	ir_graph *irg = get_irn_irg(node);
	ir_node  *new_block, *old_block;
	ir_node  *phi, *jmp;
229

Christian Würdig's avatar
Christian Würdig committed
230
231
232
233
234
235
	/* 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);
236
237
	new_block = new_r_Block(irg, get_Block_n_cfgpreds(old_block),
	                        get_Block_cfgpred_arr(old_block));
238

239
240
241
	/* create a jump from new_block to old_block, which is now the lower one */
	jmp = new_r_Jmp(new_block);
	set_irn_in(old_block, 1, &jmp);
Christian Würdig's avatar
Christian Würdig committed
242
243
244
245
246

	/* 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
247
248
249
	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
250
251
	while (phi) {
		set_nodes_block(phi, new_block);
Michael Beck's avatar
Michael Beck committed
252
		phi = get_Phi_next(phi);
Christian Würdig's avatar
Christian Würdig committed
253
254
255
	}

	set_optimize(rem_opt);
256
}
257

258
259
ir_node *part_block_edges(ir_node *node)
{
260
	ir_graph        *irg       = get_irn_irg(node);
261
	ir_node         *old_block = get_nodes_block(node);
262
263
264
	ir_node         *new_block = new_r_Block(irg,
	                                         get_Block_n_cfgpreds(old_block),
	                                         get_Block_cfgpred_arr(old_block));
265
266
267
268
269
270
271
	const ir_edge_t *edge;
	const ir_edge_t *next;

	/* old_block has no predecessors anymore for now */
	set_irn_in(old_block, 0, NULL);

	/* move node and its predecessors to new_block */
272
	move_edges(node, old_block, new_block);
273
274
275
276
277
278
279
280
281
282
283
284

	/* move Phi nodes to new_block */
	foreach_out_edge_safe(old_block, edge, next) {
		ir_node *phi = get_edge_src_irn(edge);
		if (!is_Phi(phi))
			continue;
		set_nodes_block(phi, new_block);
	}

	return old_block;
}

285
286
void kill_node(ir_node *node)
{
287
288
	ir_graph *irg = get_irn_irg(node);

Matthias Braun's avatar
Matthias Braun committed
289
290
	if (edges_activated(irg)) {
		edges_node_deleted(node);
291
	}
Matthias Braun's avatar
Matthias Braun committed
292
293
	/* noone is allowed to reference this node anymore */
	set_irn_op(node, op_Deleted);
294
}