jumpthreading.c 18.9 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
/**
 * @file
8
 * @brief   Path-Sensitive Jump Threading
Michael Beck's avatar
Michael Beck committed
9
10
 * @date    10. Sep. 2006
 * @author  Christoph Mallon, Matthias Braun
11
 */
12
13
#include "iroptimize.h"

14
#include <assert.h>
15
#include <stdbool.h>
16
#include "array.h"
17
18
#include "debug.h"
#include "ircons.h"
Christoph Mallon's avatar
Christoph Mallon committed
19
20
#include "irgmod.h"
#include "irgopt.h"
21
#include "irgwalk.h"
22
#include "irnode_t.h"
23
#include "iredges_t.h"
24
#include "irtools.h"
25
#include "tv.h"
26
#include "iroptimize.h"
27
#include "iropt_dbg.h"
28
#include "vrp.h"
29
#include "firmstat_t.h"
30

31
#undef AVOID_PHIB
32

33
DEBUG_ONLY(static firm_dbg_module_t *dbg;)
34

Christoph Mallon's avatar
Christoph Mallon committed
35
36
37
/**
 * Add the new predecessor x to node node, which is either a Block or a Phi
 */
Matthias Braun's avatar
Matthias Braun committed
38
static void add_pred(ir_node *node, ir_node *x)
Christoph Mallon's avatar
Christoph Mallon committed
39
{
Matthias Braun's avatar
Matthias Braun committed
40
41
42
	int        const n   = get_Block_n_cfgpreds(node);
	ir_node  **const ins = ALLOCAN(ir_node*, n+1);
	foreach_irn_in(node, i, pred) {
43
		ins[i] = pred;
Matthias Braun's avatar
Matthias Braun committed
44
	}
Christoph Mallon's avatar
Christoph Mallon committed
45
46
47
48
	ins[n] = x;
	set_irn_in(node, n + 1, ins);
}

49
50
51
52
static ir_node *ssa_second_def;
static ir_node *ssa_second_def_block;

static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode,
Matthias Braun's avatar
Matthias Braun committed
53
                                           bool first)
Christoph Mallon's avatar
Christoph Mallon committed
54
{
55
	assert(is_Block(block));
56

57
58
59
	/* the other defs can't be marked for cases where a user of the original
	 * value is in the same block as the alternative definition.
	 * In this case we mustn't use the alternative definition.
Michael Beck's avatar
Michael Beck committed
60
	 * So we keep a flag that indicated whether we walked at least 1 block
61
	 * away and may use the alternative definition */
Matthias Braun's avatar
Matthias Braun committed
62
	if (block == ssa_second_def_block && !first)
63
64
		return ssa_second_def;

65
	/* already processed this block? */
66
	if (irn_visited(block)) {
67
68
69
70
		ir_node *value = (ir_node*) get_irn_link(block);
		return value;
	}

Matthias Braun's avatar
Matthias Braun committed
71
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
72
73
	assert(block != get_irg_start_block(irg));

Christoph Mallon's avatar
Christoph Mallon committed
74
	/* a Block with only 1 predecessor needs no Phi */
Matthias Braun's avatar
Matthias Braun committed
75
	int n_cfgpreds = get_Block_n_cfgpreds(block);
76
	if (n_cfgpreds == 1) {
77
		ir_node *pred_block = get_Block_cfgpred_block(block, 0);
78
79
80
81
82
83
84
		ir_node *value;
		if (pred_block == NULL) {
			ir_graph *irg = get_irn_irg(block);
			value = new_r_Bad(irg, mode);
		} else {
			value = search_def_and_create_phis(pred_block, mode, false);
		}
85
86
87
88
89
		set_irn_link(block, value);
		mark_irn_visited(block);
		return value;
	}

90
	/* create a new Phi */
Matthias Braun's avatar
Matthias Braun committed
91
92
	ir_node **in    = ALLOCAN(ir_node*, n_cfgpreds);
	ir_node  *dummy = new_r_Dummy(irg, mode);
Matthias Braun's avatar
Matthias Braun committed
93
	for (int i = 0; i < n_cfgpreds; ++i) {
94
		in[i] = dummy;
Matthias Braun's avatar
Matthias Braun committed
95
	}
96

97
98
99
	/* we might have created a potential endless loop, and need a PhiLoop */
	ir_node *phi = mode == mode_M ? new_r_Phi_loop(block, n_cfgpreds, in)
	                              : new_r_Phi(block, n_cfgpreds, in, mode);
100
101
102
	set_irn_link(block, phi);
	mark_irn_visited(block);

103
	/* set Phi predecessors */
Matthias Braun's avatar
Matthias Braun committed
104
	for (int i = 0; i < n_cfgpreds; ++i) {
105
		ir_node *pred_block = get_Block_cfgpred_block(block, i);
106
107
108
109
110
111
112
		ir_node *pred_val;
		if (pred_block == NULL) {
			ir_graph *irg = get_irn_irg(block);
			pred_val = new_r_Bad(irg, mode);
		} else {
			pred_val = search_def_and_create_phis(pred_block, mode, false);
		}
113
114
115
116
117
118
119
		set_irn_n(phi, i, pred_val);
	}

	return phi;
}

/**
120
121
122
 * Given a set of values this function constructs SSA-form for the users of the
 * first value (the users are determined through the out-edges of the value).
 * Uses the irn_visited flags. Works without using the dominance tree.
123
 */
124
125
static void construct_ssa(ir_node *orig_block, ir_node *orig_val,
                          ir_node *second_block, ir_node *second_val)
Christoph Mallon's avatar
Christoph Mallon committed
126
{
127
	/* no need to do anything */
128
	if (orig_val == second_val && !(is_Phi(orig_val) && get_Phi_loop(orig_val)))
129
130
		return;

Matthias Braun's avatar
Matthias Braun committed
131
	ir_graph *irg = get_irn_irg(orig_val);
132
133
	inc_irg_visited(irg);

Matthias Braun's avatar
Matthias Braun committed
134
	ir_mode *mode = get_irn_mode(orig_val);
135
136
	set_irn_link(orig_block, orig_val);
	mark_irn_visited(orig_block);
137

138
139
140
141
142
143
144
	if (orig_val == second_val) {
		/* In the loop-phi case setting a 2nd def is wrong */
		ssa_second_def_block = NULL;
	} else {
		ssa_second_def_block = second_block;
		ssa_second_def       = second_val;
	}
145

146
	/* Only fix the users of the first, i.e. the original node */
147
	foreach_out_edge_safe(orig_val, edge) {
148
		ir_node *user = get_edge_src_irn(edge);
149
150
		/* ignore keeps */
		if (is_End(user))
151
			continue;
152

Matthias Braun's avatar
Matthias Braun committed
153
154
155
		int j = get_edge_src_pos(edge);
		DB((dbg, LEVEL_3, ">>> Fixing user %+F (pred %d == %+F)\n", user, j,
		    get_irn_n(user, j)));
156

Matthias Braun's avatar
Matthias Braun committed
157
158
		ir_node *user_block = get_nodes_block(user);
		ir_node *newval;
159
		if (is_Phi(user)) {
160
			ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
161
162
163
164
165
166
			if (pred_block == NULL) {
				ir_graph *irg = get_irn_irg(user_block);
				newval = new_r_Bad(irg, mode);
			} else {
				newval = search_def_and_create_phis(pred_block, mode, true);
			}
167
		} else {
Matthias Braun's avatar
Matthias Braun committed
168
			newval = search_def_and_create_phis(user_block, mode, true);
169
170
		}

171
		/* don't fix newly created Phis from the SSA construction */
172
		if (newval != user) {
Matthias Braun's avatar
Matthias Braun committed
173
174
			DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user,
			    newval));
175
			set_irn_n(user, j, newval);
176
177
178
179
180
			if (is_Phi(user) && get_irn_mode(user) == mode_M && !get_Phi_loop(user)) {
				set_Phi_loop(user, true);
				keep_alive(user);
				keep_alive(user_block);
			}
181
182
183
184
		}
	}
}

185
186
187
188
189
190
191
192
193
194
/**
 * jumpthreading produces critical edges, e.g. B-C:
 *     A         A
 *  \ /       \  |
 *   B    =>   B |
 *  / \       / \|
 *     C         C
 *
 * By splitting this critical edge more threadings might be possible.
 */
195
196
static void split_critical_edge(ir_node *block, int pos)
{
Matthias Braun's avatar
Matthias Braun committed
197
198
199
200
	ir_graph *irg       = get_irn_irg(block);
	ir_node  *in[]      = { get_Block_cfgpred(block, pos) };
	ir_node  *new_block = new_r_Block(irg, ARRAY_SIZE(in), in);
	ir_node  *new_jmp   = new_r_Jmp(new_block);
201
202
203
	set_Block_cfgpred(block, pos, new_jmp);
}

204
typedef struct jumpthreading_env_t {
205
206
207
208
	ir_node      *true_block;  /**< Block we try to thread into */
	ir_node      *cmp;         /**< The Compare node that might be partial
	                                evaluated */
	ir_relation   relation;    /**< The Compare mode of the Compare node. */
Matthias Braun's avatar
Matthias Braun committed
209
210
211
212
213
214
	ir_node      *cnst;
	ir_tarval    *tv;
	ir_visited_t  visited_nr;
	ir_node      *cnst_pred;   /**< the block before the constant */
	int           cnst_pos;    /**< the pos to the constant block (needed to
	                                kill that edge later) */
215
} jumpthreading_env_t;
Christoph Mallon's avatar
Christoph Mallon committed
216

217
218
219
220
static ir_node *copy_and_fix_node(const jumpthreading_env_t *env,
                                  ir_node *block, ir_node *copy_block, int j,
                                  ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
221
	/* we can evaluate Phis right now, all other nodes get copied */
Matthias Braun's avatar
Matthias Braun committed
222
	ir_node *copy;
Matthias Braun's avatar
Matthias Braun committed
223
224
	if (is_Phi(node)) {
		copy = get_Phi_pred(node, j);
225
		/* we might have to evaluate a Phi-cascade */
Matthias Braun's avatar
Matthias Braun committed
226
		if (get_irn_visited(copy) >= env->visited_nr)
227
			copy = (ir_node*)get_irn_link(copy);
Matthias Braun's avatar
Matthias Braun committed
228
229
230
231
232
	} else {
		copy = exact_copy(node);
		set_nodes_block(copy, copy_block);
		assert(get_irn_mode(copy) != mode_X);

233
		foreach_irn_in(copy, i, pred) {
234
			if (get_nodes_block(pred) != block)
Matthias Braun's avatar
Matthias Braun committed
235
236
				continue;

Matthias Braun's avatar
Matthias Braun committed
237
			ir_node *new_pred;
238
			if (get_irn_visited(pred) >= env->visited_nr) {
239
				new_pred = (ir_node*)get_irn_link(pred);
Matthias Braun's avatar
Matthias Braun committed
240
241
242
			} else {
				new_pred = copy_and_fix_node(env, block, copy_block, j, pred);
			}
243
			DB((dbg, LEVEL_2, ">> Set Pred of %+F to %+F\n", copy, new_pred));
Matthias Braun's avatar
Matthias Braun committed
244
245
246
247
248
249
250
251
252
			set_irn_n(copy, i, new_pred);
		}
	}

	set_irn_link(node, copy);
	set_irn_visited(node, env->visited_nr);
	return copy;
}

253
254
255
static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                         ir_node *copy_block, int j)
{
Matthias Braun's avatar
Matthias Braun committed
256
	/* Look at all nodes in the cond_block and copy them into copy_block */
257
258
	foreach_out_edge(block, edge) {
		ir_node *node = get_edge_src_irn(edge);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
259
		if (is_End(node)) {
260
			/* edge is a Keep edge. If the end block is unreachable via normal
Matthias Braun's avatar
Matthias Braun committed
261
			 * control flow, we must maintain end's reachability with Keeps. */
Andreas Zwinkau's avatar
Andreas Zwinkau committed
262
263
264
			keep_alive(copy_block);
			continue;
		}
265
		/* ignore control flow */
Matthias Braun's avatar
Matthias Braun committed
266
		ir_mode *mode = get_irn_mode(node);
Matthias Braun's avatar
Matthias Braun committed
267
		if (mode == mode_X || is_Cond(node) || is_Switch(node))
268
			continue;
269
#ifdef AVOID_PHIB
270
		/* we may not copy mode_b nodes, because this could produce Phi with
271
		 * mode_bs which can't be handled in all backends. Instead we duplicate
Christoph Mallon's avatar
Christoph Mallon committed
272
		 * the node and move it to its users */
273
		if (mode == mode_b) {
274
			ir_node *const pred = get_Proj_pred(node);
275
			unsigned const pn   = get_Proj_num(node);
276

277
			foreach_out_edge_safe(node, edge) {
278
279
				ir_node *user       = get_edge_src_irn(edge);
				ir_node *user_block = get_nodes_block(user);
280
				if (user_block == block)
281
282
					continue;

Matthias Braun's avatar
Matthias Braun committed
283
284
				int      pos      = get_edge_src_pos(edge);
				ir_node *cmp_copy = exact_copy(pred);
285
				set_nodes_block(cmp_copy, user_block);
Matthias Braun's avatar
Matthias Braun committed
286
				ir_node *copy = new_r_Proj(cmp_copy, mode_b, pn);
287
288
289
290
				set_irn_n(user, pos, copy);
			}
			continue;
		}
291
#endif
Matthias Braun's avatar
Matthias Braun committed
292
		ir_node *copy = copy_and_fix_node(env, block, copy_block, j, node);
293
294

		/* we might hit values in blocks that have already been processed by a
yb9976's avatar
yb9976 committed
295
		 * recursive find_const_or_confirm() call */
296
		assert(get_irn_visited(copy) <= env->visited_nr);
297
		if (get_irn_visited(copy) >= env->visited_nr) {
298
			ir_node *prev_copy = (ir_node*)get_irn_link(copy);
299
			if (prev_copy != NULL)
300
301
302
				set_irn_link(node, prev_copy);
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
303

304
305
306
	/* fix data-flow (and reconstruct SSA if needed) */
	foreach_out_edge(block, edge) {
		ir_node *node = get_edge_src_irn(edge);
Matthias Braun's avatar
Matthias Braun committed
307
		ir_mode *mode = get_irn_mode(node);
Matthias Braun's avatar
Matthias Braun committed
308
		if (mode == mode_X || is_Cond(node) || is_Switch(node))
309
			continue;
310
#ifdef AVOID_PHIB
311
		if (mode == mode_b)
312
			continue;
313
#endif
314

Matthias Braun's avatar
Matthias Braun committed
315
		ir_node *copy_node = (ir_node*)get_irn_link(node);
316
		DB((dbg, LEVEL_2, ">> Fixing users of %+F (copy %+F)\n", node, copy_node));
317
		construct_ssa(block, node, copy_block, copy_node);
318
	}
319

320
	/* make sure copied PhiM nodes are kept alive if old nodes were */
321
322
323
324
325
326
327
	ir_graph *irg = get_irn_irg(block);
	ir_node  *end = get_irg_end(irg);
	for (int i = 0, arity = get_End_n_keepalives(end); i < arity; ++i) {
		ir_node *keep = get_End_keepalive(end, i);
		if (get_irn_visited(keep) < env->visited_nr || is_Block(keep))
			continue;
		ir_node *copy = get_irn_link(keep);
328
329
		/* exact copy does not reproduce the keep alive edges */
		if (is_Phi(copy) && get_Phi_loop(copy))
330
			add_End_keepalive(end, copy);
331
	}
332
333
}

334
/**
Matthias Braun's avatar
Matthias Braun committed
335
 * Returns whether the cmp evaluates to true or false, or can't be evaluated!
336
 *
337
 * @param relation  the compare mode of the Compare
338
339
 * @param tv_left   the left tarval
 * @param tv_right  the right tarval
Matthias Braun's avatar
Matthias Braun committed
340
 * @returns 1: true, 0: false, -1: can't evaluate
341
 */
342
343
static int eval_cmp_tv(ir_relation relation, ir_tarval *tv_left,
                       ir_tarval *tv_right)
344
{
345
	/* does the compare evaluate to true? */
Matthias Braun's avatar
Matthias Braun committed
346
	ir_relation cmp_result = tarval_cmp(tv_left, tv_right);
347
	if (cmp_result == ir_relation_false)
348
		return -1;
349
350
	if ((cmp_result & relation) != 0)
		return 1;
351

352
	return 0;
353
354
}

355
/**
Matthias Braun's avatar
Matthias Braun committed
356
 * Returns whether the cmp evaluates to true or false, or can't be evaluated!
357
358
359
 *
 * @param env      the environment
 * @param cand     the candidate node, either a Const or a Confirm
Matthias Braun's avatar
Matthias Braun committed
360
 * @returns 1: true, 0: false, -1: can't evaluate
361
 */
362
363
static int eval_cmp(jumpthreading_env_t *env, ir_node *cand)
{
364
	if (is_Const(cand)) {
365
366
367
		ir_tarval *tv_cand = get_Const_tarval(cand);
		ir_tarval *tv_cmp  = get_Const_tarval(env->cnst);
		return eval_cmp_tv(env->relation, tv_cand, tv_cmp);
Matthias Braun's avatar
Matthias Braun committed
368
369
370
371
	} else {
		assert(is_Confirm(cand));
		ir_tarval *res = computed_value_Cmp_Confirm(env->cmp, cand, env->cnst,
		                                            env->relation);
372
		if (!tarval_is_constant(res))
373
374
			return -1;
		return res == tarval_b_true;
375
	}
376
377
378
379
380
}

/**
 * Check for Const or Confirm with Const.
 */
Matthias Braun's avatar
Matthias Braun committed
381
static bool is_Const_or_Confirm(const ir_node *node)
382
{
383
384
	if (is_Confirm(node))
		node = get_Confirm_bound(node);
385
386
387
388
	return is_Const(node);
}

/**
389
 * get the tarval of a Const or Confirm with
390
 */
Matthias Braun's avatar
Matthias Braun committed
391
static ir_tarval *get_Const_or_Confirm_tarval(const ir_node *node)
392
{
Matthias Braun's avatar
Matthias Braun committed
393
394
	if (is_Confirm(node))
		node = get_Confirm_bound(node);
395
396
397
	return get_Const_tarval(node);
}

398
399
static ir_node *find_const_or_confirm(jumpthreading_env_t *env, ir_node *jump,
                                      ir_node *value)
400
{
401
	if (irn_visited_else_mark(value))
402
403
		return NULL;

Matthias Braun's avatar
Matthias Braun committed
404
	ir_node *block = get_nodes_block(jump);
405
	if (is_Const_or_Confirm(value)) {
406
407
408
409
410
		int evaluated = eval_cmp(env, value);
		if (evaluated < 0)
			return NULL;
		/* maybe we could evaluate the condition completely without any
		 * partial tracking along paths. */
411
412
		assert(get_Block_n_cfgpreds(env->true_block) == 1);
		if (block == get_Block_cfgpred_block(env->true_block, 0)) {
413
414
415
416
417
418
419
420
421
			if (evaluated == 0) {
				ir_graph *irg = get_irn_irg(block);
				ir_node  *bad = new_r_Bad(irg, mode_X);
				exchange(jump, bad);
			} else if (evaluated == 1) {
				dbg_info *dbgi = get_irn_dbg_info(skip_Proj(jump));
				ir_node  *jmp  = new_rd_Jmp(dbgi, get_nodes_block(jump));
				exchange(jump, jmp);
			}
Matthias Braun's avatar
Matthias Braun committed
422
423
			/* we need a bigger visited nr when going back */
			env->visited_nr++;
424
425
426
			return block;
		}
		if (evaluated <= 0)
427
			return NULL;
Christoph Mallon's avatar
Christoph Mallon committed
428

Matthias Braun's avatar
Matthias Braun committed
429
430
		DB((dbg, LEVEL_1, "> Found jump threading candidate %+F->%+F\n",
			block, env->true_block));
Christoph Mallon's avatar
Christoph Mallon committed
431

432
		/* adjust true_block to point directly towards our jump */
433
		add_pred(env->true_block, jump);
434

435
436
		split_critical_edge(env->true_block, 0);

437
		/* we need a bigger visited nr when going back */
438
439
440
		env->visited_nr++;
		return block;
	}
441

442
	if (is_Phi(value)) {
443
		/* the Phi has to be in the same Block as the Jmp */
444
		if (get_nodes_block(value) != block)
445
			return NULL;
446

447
		foreach_irn_in(value, i, phi_pred) {
Matthias Braun's avatar
Matthias Braun committed
448
449
			ir_node *cfgpred    = get_Block_cfgpred(block, i);
			ir_node *copy_block = find_const_or_confirm(env, cfgpred, phi_pred);
450
			if (copy_block == NULL)
451
452
				continue;

453
			/* copy duplicated nodes in copy_block and fix SSA */
454
			copy_and_fix(env, block, copy_block, i);
455
			if (copy_block == get_nodes_block(cfgpred)) {
456
				env->cnst_pred = block;
457
				env->cnst_pos  = i;
458
			}
459

460
			/* return now as we can't process more possibilities in 1 run */
461
			return copy_block;
462
		}
Christoph Mallon's avatar
Christoph Mallon committed
463
	}
464

465
	return NULL;
Christoph Mallon's avatar
Christoph Mallon committed
466
467
}

468
static ir_node *find_candidate(jumpthreading_env_t *env, ir_node *jump,
469
                               ir_node *value)
470
{
Matthias Braun's avatar
Matthias Braun committed
471
	if (irn_visited_else_mark(value))
472
473
		return NULL;

Matthias Braun's avatar
Matthias Braun committed
474
	ir_node *block = get_nodes_block(jump);
475
	if (is_Const_or_Confirm(value)) {
Matthias Braun's avatar
Matthias Braun committed
476
		ir_tarval *tv = get_Const_or_Confirm_tarval(value);
477
		if (tv != env->tv)
478
479
			return NULL;

Matthias Braun's avatar
Matthias Braun committed
480
481
		DB((dbg, LEVEL_1, "> Found jump threading candidate %+F->%+F\n",
			block, env->true_block));
482

483
		/* adjust true_block to point directly towards our jump */
484
485
		add_pred(env->true_block, jump);

486
487
		split_critical_edge(env->true_block, 0);

488
		/* we need a bigger visited nr when going back */
489
490
		env->visited_nr++;
		return block;
491
	}
492
	if (is_Phi(value)) {
493
		/* the Phi has to be in the same Block as the Jmp */
494
		if (get_nodes_block(value) != block)
495
			return NULL;
496

497
		foreach_irn_in(value, i, phi_pred) {
Matthias Braun's avatar
Matthias Braun committed
498
499
			ir_node *cfgpred    = get_Block_cfgpred(block, i);
			ir_node *copy_block = find_candidate(env, cfgpred, phi_pred);
500
			if (copy_block == NULL)
501
502
503
504
				continue;

			/* copy duplicated nodes in copy_block and fix SSA */
			copy_and_fix(env, block, copy_block, i);
505
			if (copy_block == get_nodes_block(cfgpred)) {
506
507
508
509
				env->cnst_pred = block;
				env->cnst_pos  = i;
			}

510
			/* return now as we can't process more possibilities in 1 run */
511
			return copy_block;
Christoph Mallon's avatar
Christoph Mallon committed
512
		}
513
	}
514
515
516
517
518
	if (is_Cmp(value)) {
		ir_node    *cmp      = value;
		ir_node    *left     = get_Cmp_left(cmp);
		ir_node    *right    = get_Cmp_right(cmp);
		ir_relation relation = get_Cmp_relation(cmp);
519

520
521
		/* we assume that the constant is on the right side, swap left/right
		 * if needed */
522
		if (is_Const(left)) {
523
524
525
			ir_node *t = left;
			left       = right;
			right      = t;
526

527
			relation   = get_inversed_relation(relation);
528
		}
529

530
		if (!is_Const(right))
531
532
533
			return NULL;
		if (get_nodes_block(left) != block)
			return NULL;
534
		/* negate condition when we're looking for the false block */
Matthias Braun's avatar
Matthias Braun committed
535
		if (env->tv == tarval_b_false)
536
			relation = get_negated_relation(relation);
537

538
		/* (recursively) look if a pred of a Phi is a constant or a Confirm */
539
540
541
		env->cmp      = cmp;
		env->relation = relation;
		env->cnst     = right;
542
		return find_const_or_confirm(env, jump, left);
543
	}
544

545
546
	return NULL;
}
547
548
549
550
551
552
553
554
555
556
557
558
559
560

/**
 * Block-walker: searches for the following construct
 *
 *  Const or Phi with constants
 *           |
 *          Cmp
 *           |
 *         Cond
 *          /
 *       ProjX
 *        /
 *     Block
 */
561
static void thread_jumps(ir_node* block, void* data)
562
{
563
	bool *changed = (bool*)data;
564

565
	/* we do not deal with Phis, so restrict this to exactly one cfgpred */
566
	if (get_Block_n_cfgpreds(block) != 1)
567
568
		return;

Matthias Braun's avatar
Matthias Braun committed
569
	ir_node *projx = get_Block_cfgpred(block, 0);
570
571
572
	if (!is_Proj(projx))
		return;

Matthias Braun's avatar
Matthias Braun committed
573
	ir_node *cond = get_Proj_pred(projx);
574
	/* TODO handle switch Conds */
Matthias Braun's avatar
Matthias Braun committed
575
	if (!is_Cond(cond))
576
		return;
577

578
	/* (recursively) look if a pred of a Phi is a constant or a Confirm */
579
580
	ir_node  *selector = get_Cond_selector(cond);
	ir_graph *irg      = get_irn_irg(block);
581
582
583
584
585
586
587
588
589
590
591
592
	if (is_Const(selector)) {
		const ir_tarval *tv = get_Const_tarval(selector);
		assert(tv == tarval_b_false || tv == tarval_b_true);
		ir_node    *const cond_block = get_nodes_block(cond);
		ir_node    *const jmp        = new_r_Jmp(cond_block);
		ir_node    *const bad        = new_r_Bad(irg, mode_X);
		const bool        is_true    = tv == tarval_b_true;
		ir_node *const in[] = {
			[pn_Cond_false] = is_true ? bad : jmp,
			[pn_Cond_true]  = is_true ? jmp : bad,
		};
		turn_into_tuple(cond, ARRAY_SIZE(in), in);
593
		*changed = true;
594
595
		return;
	}
596
	inc_irg_visited(irg);
597
598
	jumpthreading_env_t env;
	env.cnst_pred  = NULL;
599
	env.tv         = get_Proj_num(projx) == pn_Cond_false
600
601
	                 ? tarval_b_false : tarval_b_true;
	env.true_block = block;
602
	env.visited_nr = get_irg_visited(irg);
603

Matthias Braun's avatar
Matthias Braun committed
604
	ir_node *copy_block = find_candidate(&env, projx, selector);
605
	if (copy_block == NULL)
606
607
		return;

608
609
610
611
	if (copy_block != get_nodes_block(cond)) {
		/* We might thread the condition block of an infinite loop,
		 * such that there is no path to End anymore. */
		keep_alive(block);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
612

613
614
615
616
617
		/* we have to remove the edge towards the pred as the pred now
		 * jumps into the true_block. We also have to shorten Phis
		 * in our block because of this */
		ir_node *badX     = new_r_Bad(irg, mode_X);
		int      cnst_pos = env.cnst_pos;
618

619
620
621
		/* shorten Phis */
		foreach_out_edge_safe(env.cnst_pred, edge) {
			ir_node *node = get_edge_src_irn(edge);
622

623
624
625
626
			if (is_Phi(node)) {
				ir_node *bad = new_r_Bad(irg, get_irn_mode(node));
				set_Phi_pred(node, cnst_pos, bad);
			}
Matthias Braun's avatar
Matthias Braun committed
627
		}
628

629
630
		set_Block_cfgpred(env.cnst_pred, cnst_pos, badX);
	}
631
632

	/* the graph is changed now */
633
	*changed = true;
634
635
}

636
void opt_jumpthreading(ir_graph* irg)
637
{
638
639
640
641
	assure_irg_properties(irg,
		IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
		| IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
		| IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES);
642

643
	FIRM_DBG_REGISTER(dbg, "firm.opt.jumpthreading");
644

645
	DB((dbg, LEVEL_1, "===> Performing jumpthreading on %+F\n", irg));
646

647
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
648

Matthias Braun's avatar
Matthias Braun committed
649
650
	bool changed = false;
	bool rerun;
651
	do {
652
		rerun = false;
653
		irg_block_walk_graph(irg, thread_jumps, NULL, &rerun);
Michael Beck's avatar
Michael Beck committed
654
655
656
		changed |= rerun;
	} while (rerun);

657
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
658

659
	if (changed) {
yb9976's avatar
yb9976 committed
660
		/* we tend to produce a lot of duplicated keep edges, remove them */
661
662
663
664
665
		remove_End_Bads_and_doublets(get_irg_end(irg));
		confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
	} else {
		confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL);
	}
666
}