condeval.c 9.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * Project:     libFIRM
 * File name:   ir/opt/cfopt.c
 * Purpose:     Partial condition evaluation
 * Author:      Christoph Mallon, Matthias Braun
 * Created:     10. Sep. 2006
 * CVS-ID:      $Id$
 * Copyright:   (c) 1998-2006 Universität Karlsruhe
 * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 */
11
12
13
14
15
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
16
#include "array.h"
Christoph Mallon's avatar
Christoph Mallon committed
17
#include "condeval.h"
18
19
#include "debug.h"
#include "ircons.h"
Christoph Mallon's avatar
Christoph Mallon committed
20
21
#include "irgmod.h"
#include "irgopt.h"
22
23
#include "irgwalk.h"
#include "irnode.h"
24
#include "irnode_t.h"
25
#include "iredges.h"
26
#include "iredges_t.h"
27
#include "irtools.h"
28
29
30
31
#include "tv.h"

DEBUG_ONLY(static firm_dbg_module_t *dbg);

Christoph Mallon's avatar
Christoph Mallon committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

/**
 * Add the new predecessor x to node node, which is either a Block or a Phi
 */
static void add_pred(ir_node* node, ir_node* x)
{
	ir_node** ins;
	int n;
	int i;

	assert(is_Block(node) || is_Phi(node));

	n = get_irn_arity(node);
	NEW_ARR_A(ir_node*, ins, n + 1);
	for (i = 0; i < n; i++) ins[i] = get_irn_n(node, i);
	ins[n] = x;
	set_irn_in(node, n + 1, ins);
}

/**
 * Remove predecessor j from node, which is either a Block or a Phi
 * returns true if only one predecessor is left
 */
static int remove_pred(ir_node* node, int j)
{
	int n;

	assert(is_Block(node) || is_Phi(node));

	n = get_irn_arity(node);
	if (n == 2) {
		ir_node* pred = get_irn_n(node, 1 - j);

65
66
		if (is_Block(node)) {
			pred = get_nodes_block(pred);
67
68
69
70
			ir_fprintf(stderr, "Exchaning %+F with %+F\n", node, pred);
			//set_irn_in(node, get_irn_arity(pred), get_irn_in(pred) + 1);
			//exchange(node, pred);
			edges_reroute(node, pred, current_ir_graph);
71
72
73
		} else {
			exchange(node, pred);
		}
Christoph Mallon's avatar
Christoph Mallon committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
		return 1;
	} else {
		ir_node** ins;
		int i;

		NEW_ARR_A(ir_node*, ins, n - 1);
		for (i = 0; i < j; i++) ins[i]     = get_irn_n(node, i);
		for (i++;   i < n; i++) ins[i - 1] = get_irn_n(node, i);
		set_irn_in(node, n - 1, ins);
		return 0;
	}
}

static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
{
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	int i;
	int n_cfgpreds;
	ir_graph *irg;
	ir_node *phi;
	ir_node **in;

	assert(!is_Bad(block));

	// already processed this block?
	if(irn_visited(block)) {
		ir_node *value = (ir_node*) get_irn_link(block);
		return value;
	}

	// blocks with only 1 pred need no phi
	n_cfgpreds = get_Block_n_cfgpreds(block);
	if(n_cfgpreds == 1) {
		ir_node *pred_block = get_Block_cfgpred_block(block, 0);
		ir_node *value = search_def_and_create_phis(pred_block, mode);

		set_irn_link(block, value);
		mark_irn_visited(block);
		return value;
	}

	// create a new phi
	in = alloca(sizeof(in[0]) * n_cfgpreds);
	for(i = 0; i < n_cfgpreds; ++i)
		in[i] = new_Unknown(mode);

	irg = get_irn_irg(block);
	phi = new_r_Phi(irg, block, n_cfgpreds, in, mode);
	set_irn_link(block, phi);
	mark_irn_visited(block);

	// set phi preds
	for(i = 0; i < n_cfgpreds; ++i) {
		ir_node *pred_block = get_Block_cfgpred_block(block, i);
		ir_node *pred_val;

		pred_val = search_def_and_create_phis(pred_block, mode);
		set_irn_n(phi, i, pred_val);
	}

	return phi;
}

/**
 * Given a set of values this function constructs SSA-form for all users of the
 * values (the user are determined through the out-edges of the values).
 */
140
static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_vals)
Christoph Mallon's avatar
Christoph Mallon committed
141
{
142
143
144
145
146
147
148
149
150
151
152
153
	int i;
	ir_graph *irg;
	ir_mode *mode;

	assert(n_vals > 0);

	irg = get_irn_irg(vals[0]);
	inc_irg_visited(irg);

	mode = get_irn_mode(vals[0]);
	for(i = 0; i < n_vals; ++i) {
		ir_node *value = vals[i];
154
		ir_node *value_block = blocks[i];
155
156
157
158
159
160
161
162

		assert(get_irn_mode(value) == mode);

		set_irn_link(value_block, value);
		mark_irn_visited(value_block);
	}

	for(i = 0; i < n_vals; ++i) {
163
		const ir_edge_t *edge, *next;
164
165
		ir_node *value = vals[i];

166
167
168
169
170
171
		// this can happen when fixing phi preds, we mustn't fix the users
		if(get_nodes_block(value) != blocks[i]) {
			continue;
		}

		foreach_out_edge_safe(value, edge, next) {
172
			ir_node *user = get_edge_src_irn(edge);
173
			int j = get_edge_src_pos(edge);
174
175
176
			ir_node *user_block = get_nodes_block(user);
			ir_node *newval;

177
178
179
180
181
182
183
184
185
186
187
188
189
190
			// ignore keeps
			if(get_irn_op(user) == op_End)
				continue;

			if(is_Phi(user)) {
				ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
				newval = search_def_and_create_phis(pred_block, mode);
			} else {
				newval = search_def_and_create_phis(user_block, mode);
			}

			// don't fix newly created phis from the SSA construction
			if(newval != user)
				set_irn_n(user, j, newval);
191
192
193
194
		}
	}
}

Christoph Mallon's avatar
Christoph Mallon committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

#if 0
/**
 * Compare node compares two Const nodes
 * Returns true if this direction is taken
 */
static int handle_const_const(ir_node* cnst_left, cnst_right, pn_Cmp pnc)
{
	// TODO
}
#endif


/**
 *
 */
211
static int handle_phi_const(ir_node* block, ir_node* cond_block, ir_node* phi, ir_node* cnst, pn_Cmp pnc)
Christoph Mallon's avatar
Christoph Mallon committed
212
{
213
214
	int changed = 0;
	ir_graph *irg;
Christoph Mallon's avatar
Christoph Mallon committed
215
216
217
218
219
220
221
	tarval* tv_cnst;
	int n_phi;
	int j;

	tv_cnst = get_Const_tarval(cnst);
	n_phi = get_Phi_n_preds(phi);
	for (j = 0; j < n_phi; j++) {
222
223
224
225
		const ir_edge_t *edge, *next;
		ir_node *pred;
		ir_node *pred_block;
		tarval *tv_phi;
Christoph Mallon's avatar
Christoph Mallon committed
226
227
228
229
		pn_Cmp cmp_val;

		pred = get_Phi_pred(phi, j);
		// TODO handle Phi cascades
230
231
		if (!is_Const(pred))
			continue;
Christoph Mallon's avatar
Christoph Mallon committed
232

233
		tv_phi = get_Const_tarval(pred);
Christoph Mallon's avatar
Christoph Mallon committed
234
235
236
237
238
239
240
241
242
243
244
245

		cmp_val = tarval_cmp(tv_phi, tv_cnst);
		if (cmp_val == pn_Cmp_False) continue;
		if ((cmp_val & pnc) != cmp_val) continue;

		DB((
			dbg, LEVEL_1,
			"> Found condition evaluation candidate %+F->%+F predecessor %d\n",
			block, cond_block, j
		));

		add_pred(block, get_Block_cfgpred(cond_block, j));
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
		// split critical edges
		/*if(get_irn_arity(block) == 2)*/ {
			ir_node *in[1];
			ir_node *new_block;
			ir_node *new_jmp;

			in[0] = get_Block_cfgpred(block, 0);
			new_block = new_r_Block(current_ir_graph, 1, in);
			new_jmp = new_r_Jmp(current_ir_graph, new_block);
			set_Block_cfgpred(block, 0, new_jmp);
		}

		pred_block = get_Block_cfgpred_block(cond_block, j);

		/* Look at all nodes in the cond_block and copy them into pred */
		foreach_out_edge(cond_block, edge) {
			ir_node *node = get_edge_src_irn(edge);
			ir_node *copy;

			// ignore these as SSA construction would fail on ProjX
			if (is_Proj(node) && get_irn_mode(node) == mode_X)
				continue;

			// we can evaluate Phis right now, all other nodes get copied
			if (is_Phi(node)) {
				copy = get_Phi_pred(node, j);
			} else {
				copy = exact_copy(node);
				set_nodes_block(copy, pred_block);
			}
Christoph Mallon's avatar
Christoph Mallon committed
276

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
			set_irn_link(node, copy);
		}

		/* fix data-flow (and reconstruct SSA if needed) */
		foreach_out_edge(cond_block, edge) {
			ir_node *vals[2];
			ir_node *blocks[2];

			ir_node *node = get_edge_src_irn(edge);
			assert(get_nodes_block(node) == cond_block);
			if (is_Proj(node) && get_irn_mode(node) == mode_X)
				continue;

			blocks[0] = cond_block;
			vals[0] = node;
			blocks[1] = pred_block;
			vals[1] = get_irn_link(node);
			construct_ssa(blocks, vals, 2);
		}

		/* shorten phis */
		foreach_out_edge_safe(cond_block, edge, next) {
			ir_node *node = get_edge_src_irn(edge);

			if(is_Phi(node))
				remove_pred(node, j);
		}

		changed = 1;
		remove_pred(cond_block, j);
#if 0
		if (remove_pred(cond_block, j))
			break;
		// the phi got shortened
		n_phi--;
		j--;
Christoph Mallon's avatar
Christoph Mallon committed
313
#endif
314
		break;
Christoph Mallon's avatar
Christoph Mallon committed
315
	}
316
317
318
319
320
321
322
323

	if (changed) {
		irg = get_irn_irg(block);
		set_irg_doms_inconsistent(irg);
		set_irg_extblk_inconsistent(irg);
		set_irg_loopinfo_inconsistent(irg);
	}
	return changed;
Christoph Mallon's avatar
Christoph Mallon committed
324
325
326
}


327
328
329
/**
 * Block-walker:
 */
330
331
static void cond_eval(ir_node* block, void* env)
{
332
	int n_block;
333
334
	int i;

335
	n_block = get_Block_n_cfgpreds(block);
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
	for (i = 0; i < n_block; i++) {
		ir_node* pred;
		ir_node* projx;
		ir_node* cond;
		ir_node* cmp;
		ir_node* left;
		ir_node* right;
		ir_node* cond_block;
		pn_Cmp pnc;

		pred = get_Block_cfgpred(block, i);
		if (!is_Proj(pred)) continue;
		projx = pred;

		pred = get_Proj_pred(projx);
		if (!is_Cond(pred)) continue;
		cond = pred;

		pred = get_Cond_selector(cond);
		// TODO handle switches
		if (get_irn_mode(pred) != mode_b) continue;
Matthias Braun's avatar
Matthias Braun committed
357
		assert(is_Proj(pred));
358
359
360
361
362
363
364
		pnc = get_Proj_proj(pred);

		cmp = get_Proj_pred(pred);
		assert(is_Cmp(cmp));

		left  = get_Cmp_left(cmp);
		right = get_Cmp_right(cmp);
Christoph Mallon's avatar
Christoph Mallon committed
365
		assert(get_irn_mode(left) == get_irn_mode(right));
366

367
		if (get_Proj_proj(projx) == 0) {
Christoph Mallon's avatar
Christoph Mallon committed
368
			pnc = get_negated_pnc(pnc, get_irn_mode(left));
369
		}
370

Christoph Mallon's avatar
Christoph Mallon committed
371
372
373
374
375
376
377
378
#if 0 // TODO implement
		if (is_Const(left) && is_Const(right)) {
			if (!handle_const_const()) {
				n_block--;
				i--;
			}
			continue;
		}
379
#endif
Christoph Mallon's avatar
Christoph Mallon committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
		cond_block = get_nodes_block(cond);
		if (is_Phi(left) && is_Const(right)) {
			if (get_nodes_block(left) != cond_block) continue;
			handle_phi_const(block, cond_block, left, right, pnc);
			continue;
		}
		if (is_Const(left) && is_Phi(right)) {
			if (get_nodes_block(right) != cond_block) continue;
			handle_phi_const(block, cond_block, right, left, get_inversed_pnc(pnc));
			continue;
		}
#if 0
		if (is_Phi(left) && is_Phi(right)) {
			// TODO implement
394
		}
Christoph Mallon's avatar
Christoph Mallon committed
395
#endif
396
397
398
399
400
401
402
	}
}


void opt_cond_eval(ir_graph* irg)
{
	FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
Christoph Mallon's avatar
Christoph Mallon committed
403
	firm_dbg_set_mask(dbg, SET_LEVEL_5);
404
405
406

	DB((dbg, LEVEL_1, "===> Performing condition evaluation on %+F\n", irg));

407
408
409
	if(edges_activated(irg))
		edges_deactivate(irg);
	edges_assure(irg);
Christoph Mallon's avatar
Christoph Mallon committed
410
411
	remove_critical_cf_edges(irg);

412
413
414
415
	normalize_proj_nodes(irg);

	irg_block_walk_graph(irg, cond_eval, NULL, NULL);
#if 0
416
	irg_block_walk_graph(irg, NULL, cond_eval, NULL);
417
418
419
	irg_block_walk_graph(irg, NULL, cond_eval, NULL);
	irg_block_walk_graph(irg, NULL, cond_eval, NULL);
#endif
420
}