condeval.c 8.79 KB
Newer Older
1
2
3
4
5
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <assert.h>
6
#include "array.h"
Christoph Mallon's avatar
Christoph Mallon committed
7
#include "condeval.h"
8
9
#include "debug.h"
#include "ircons.h"
Christoph Mallon's avatar
Christoph Mallon committed
10
11
#include "irgmod.h"
#include "irgopt.h"
12
13
#include "irgwalk.h"
#include "irnode.h"
14
#include "irnode_t.h"
15
#include "iredges.h"
16
#include "irtools.h"
17
18
19
20
#include "tv.h"

DEBUG_ONLY(static firm_dbg_module_t *dbg);

Christoph Mallon's avatar
Christoph Mallon committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

/**
 * 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);

55
56
57
58
59
60
61
		if (is_Block(node)) {
			pred = get_nodes_block(pred);
			set_irn_in(node, get_irn_arity(pred), get_irn_in(pred) + 1);
			exchange(pred, node);
		} else {
			exchange(node, pred);
		}
Christoph Mallon's avatar
Christoph Mallon committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
		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)
{
78
79
80
81
82
83
84
85
86
87
88
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
	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;
}

Christoph Mallon's avatar
Christoph Mallon committed
125

126
127
128
129
/**
 * 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).
 */
130
static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_vals)
Christoph Mallon's avatar
Christoph Mallon committed
131
{
132
133
134
135
136
137
138
139
140
141
142
143
	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];
144
		ir_node *value_block = blocks[i];
145
146
147
148
149
150
151
152

		assert(get_irn_mode(value) == mode);

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

	for(i = 0; i < n_vals; ++i) {
153
		const ir_edge_t *edge, *next;
154
155
		ir_node *value = vals[i];

156
157
158
159
160
161
		// 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) {
162
			ir_node *user = get_edge_src_irn(edge);
163
			int j = get_edge_src_pos(edge);
164
165
166
			ir_node *user_block = get_nodes_block(user);
			ir_node *newval;

167
168
169
170
171
172
173
174
175
176
177
178
179
180
			// 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);
181
182
183
184
		}
	}
}

Christoph Mallon's avatar
Christoph Mallon committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

#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


/**
 *
 */
201
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
202
{
203
204
	int changed = 0;
	ir_graph *irg;
Christoph Mallon's avatar
Christoph Mallon committed
205
206
207
208
209
210
211
	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++) {
212
213
214
215
		const ir_edge_t *edge, *next;
		ir_node *pred;
		ir_node *pred_block;
		tarval *tv_phi;
Christoph Mallon's avatar
Christoph Mallon committed
216
217
218
219
		pn_Cmp cmp_val;

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

223
		tv_phi = get_Const_tarval(pred);
Christoph Mallon's avatar
Christoph Mallon committed
224
225
226
227
228
229
230
231
232
233
234
235

		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));
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
		// 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
266

267
268
269
270
271
272
273
274
275
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
			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
303
#endif
304
		break;
Christoph Mallon's avatar
Christoph Mallon committed
305
	}
306
307
308
309
310
311
312
313

	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
314
315
316
}


317
318
319
/**
 * Block-walker:
 */
320
321
static void cond_eval(ir_node* block, void* env)
{
322
	int n_block;
323
324
	int i;

325
	n_block = get_Block_n_cfgpreds(block);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	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);
		assert(is_Proj(pred));
		// TODO handle switches
		if (get_irn_mode(pred) != mode_b) continue;
		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
355
		assert(get_irn_mode(left) == get_irn_mode(right));
356

357
		if (get_Proj_proj(projx) == 0) {
Christoph Mallon's avatar
Christoph Mallon committed
358
			pnc = get_negated_pnc(pnc, get_irn_mode(left));
359
		}
360

Christoph Mallon's avatar
Christoph Mallon committed
361
362
363
364
365
366
367
368
#if 0 // TODO implement
		if (is_Const(left) && is_Const(right)) {
			if (!handle_const_const()) {
				n_block--;
				i--;
			}
			continue;
		}
369
#endif
Christoph Mallon's avatar
Christoph Mallon committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
		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
384
		}
Christoph Mallon's avatar
Christoph Mallon committed
385
#endif
386
387
388
389
390
391
392
	}
}


void opt_cond_eval(ir_graph* irg)
{
	FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
Christoph Mallon's avatar
Christoph Mallon committed
393
	firm_dbg_set_mask(dbg, SET_LEVEL_5);
394
395
396

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

397
398
399
	if(edges_activated(irg))
		edges_deactivate(irg);
	edges_assure(irg);
Christoph Mallon's avatar
Christoph Mallon committed
400
401
	remove_critical_cf_edges(irg);

402
403
404
405
	normalize_proj_nodes(irg);

	irg_block_walk_graph(irg, cond_eval, NULL, NULL);
#if 0
406
	irg_block_walk_graph(irg, NULL, cond_eval, NULL);
407
408
409
	irg_block_walk_graph(irg, NULL, cond_eval, NULL);
	irg_block_walk_graph(irg, NULL, cond_eval, NULL);
#endif
410
}