jumpthreading.c 18.1 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
22
#include "irgwalk.h"
#include "irnode.h"
23
#include "irnode_t.h"
24
#include "iredges.h"
25
#include "iredges_t.h"
26
#include "irtools.h"
27
#include "tv.h"
28
#include "iroptimize.h"
29
#include "iropt_dbg.h"
30
#include "vrp.h"
31
#include "firmstat_t.h"
32

33
#undef AVOID_PHIB
34

35
DEBUG_ONLY(static firm_dbg_module_t *dbg;)
36

Christoph Mallon's avatar
Christoph Mallon committed
37
38
39
40
41
/**
 * 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)
{
42
43
	int const  n   = get_Block_n_cfgpreds(node);
	ir_node  **ins = ALLOCAN(ir_node*, n+1);
44
	for (int i = 0; i < n; i++)
45
		ins[i] = get_irn_n(node, i);
Christoph Mallon's avatar
Christoph Mallon committed
46
47
48
49
	ins[n] = x;
	set_irn_in(node, n + 1, ins);
}

50
51
52
53
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
54
                                           bool first)
Christoph Mallon's avatar
Christoph Mallon committed
55
{
56
57
	/* In case of a bad input to a block we need to return the bad value */
	if (is_Bad(block)) {
58
		ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
59
		return new_r_Bad(irg, mode);
60
	}
61

62
63
64
	/* 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
65
	 * So we keep a flag that indicated whether we walked at least 1 block
66
67
68
69
70
	 * away and may use the alternative definition */
	if (block == ssa_second_def_block && !first) {
		return ssa_second_def;
	}

71
	/* already processed this block? */
72
	if (irn_visited(block)) {
73
74
75
76
		ir_node *value = (ir_node*) get_irn_link(block);
		return value;
	}

Matthias Braun's avatar
Matthias Braun committed
77
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
78
79
	assert(block != get_irg_start_block(irg));

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

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

Matthias Braun's avatar
Matthias Braun committed
97
	ir_node *phi = new_r_Phi(block, n_cfgpreds, in, mode);
98
99
100
	set_irn_link(block, phi);
	mark_irn_visited(block);

101
	/* set Phi predecessors */
Matthias Braun's avatar
Matthias Braun committed
102
	for (int i = 0; i < n_cfgpreds; ++i) {
103
		ir_node *pred_block = get_Block_cfgpred_block(block, i);
Matthias Braun's avatar
Matthias Braun committed
104
105
		ir_node *pred_val   = search_def_and_create_phis(pred_block, mode,
		                                                 false);
106
107
108
109
110
111
112
		set_irn_n(phi, i, pred_val);
	}

	return phi;
}

/**
113
114
115
 * 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.
116
 */
117
118
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
119
{
120
121
122
123
	/* no need to do anything */
	if (orig_val == second_val)
		return;

Matthias Braun's avatar
Matthias Braun committed
124
	ir_graph *irg = get_irn_irg(orig_val);
125
126
	inc_irg_visited(irg);

Matthias Braun's avatar
Matthias Braun committed
127
	ir_mode *mode = get_irn_mode(orig_val);
128
129
	set_irn_link(orig_block, orig_val);
	mark_irn_visited(orig_block);
130

131
132
	ssa_second_def_block = second_block;
	ssa_second_def       = second_val;
133

134
	/* Only fix the users of the first, i.e. the original node */
135
	foreach_out_edge_safe(orig_val, edge) {
136
		ir_node *user = get_edge_src_irn(edge);
137
138
		/* ignore keeps */
		if (is_End(user))
139
			continue;
140

Matthias Braun's avatar
Matthias Braun committed
141
142
143
		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)));
144

Matthias Braun's avatar
Matthias Braun committed
145
146
		ir_node *user_block = get_nodes_block(user);
		ir_node *newval;
147
		if (is_Phi(user)) {
148
			ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
Matthias Braun's avatar
Matthias Braun committed
149
			newval = search_def_and_create_phis(pred_block, mode, true);
150
		} else {
Matthias Braun's avatar
Matthias Braun committed
151
			newval = search_def_and_create_phis(user_block, mode, true);
152
153
		}

154
		/* don't fix newly created Phis from the SSA construction */
155
		if (newval != user) {
Matthias Braun's avatar
Matthias Braun committed
156
157
			DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user,
			    newval));
158
			set_irn_n(user, j, newval);
159
160
161
162
		}
	}
}

163
164
165
166
167
168
169
170
171
172
/**
 * jumpthreading produces critical edges, e.g. B-C:
 *     A         A
 *  \ /       \  |
 *   B    =>   B |
 *  / \       / \|
 *     C         C
 *
 * By splitting this critical edge more threadings might be possible.
 */
173
174
static void split_critical_edge(ir_node *block, int pos)
{
Matthias Braun's avatar
Matthias Braun committed
175
176
177
178
	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);
179
180
181
	set_Block_cfgpred(block, pos, new_jmp);
}

182
typedef struct jumpthreading_env_t {
183
184
185
186
	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
187
188
189
190
191
192
	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) */
193
} jumpthreading_env_t;
Christoph Mallon's avatar
Christoph Mallon committed
194

195
196
197
198
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
199
	/* we can evaluate Phis right now, all other nodes get copied */
Matthias Braun's avatar
Matthias Braun committed
200
	ir_node *copy;
Matthias Braun's avatar
Matthias Braun committed
201
202
	if (is_Phi(node)) {
		copy = get_Phi_pred(node, j);
203
		/* we might have to evaluate a Phi-cascade */
204
		if (get_irn_visited(copy) >= env->visited_nr) {
205
			copy = (ir_node*)get_irn_link(copy);
Matthias Braun's avatar
Matthias Braun committed
206
207
208
209
210
211
		}
	} else {
		copy = exact_copy(node);
		set_nodes_block(copy, copy_block);
		assert(get_irn_mode(copy) != mode_X);

Matthias Braun's avatar
Matthias Braun committed
212
213
		for (int i = 0, arity = get_irn_arity(copy); i < arity; ++i) {
			ir_node *pred = get_irn_n(copy, i);
214
			if (get_nodes_block(pred) != block)
Matthias Braun's avatar
Matthias Braun committed
215
216
				continue;

Matthias Braun's avatar
Matthias Braun committed
217
			ir_node *new_pred;
218
			if (get_irn_visited(pred) >= env->visited_nr) {
219
				new_pred = (ir_node*)get_irn_link(pred);
Matthias Braun's avatar
Matthias Braun committed
220
221
222
			} else {
				new_pred = copy_and_fix_node(env, block, copy_block, j, pred);
			}
223
			DB((dbg, LEVEL_2, ">> Set Pred of %+F to %+F\n", copy, new_pred));
Matthias Braun's avatar
Matthias Braun committed
224
225
226
227
228
229
230
231
232
233
			set_irn_n(copy, i, new_pred);
		}
	}

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

	return copy;
}

234
235
236
static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                         ir_node *copy_block, int j)
{
237
238
239
	/* Look at all nodes in the cond_block and copy them into pred */
	foreach_out_edge(block, edge) {
		ir_node *node = get_edge_src_irn(edge);
Andreas Zwinkau's avatar
Andreas Zwinkau committed
240
		if (is_End(node)) {
241
			/* edge is a Keep edge. If the end block is unreachable via normal
Matthias Braun's avatar
Matthias Braun committed
242
			 * control flow, we must maintain end's reachability with Keeps. */
Andreas Zwinkau's avatar
Andreas Zwinkau committed
243
244
245
			keep_alive(copy_block);
			continue;
		}
246
		/* ignore control flow */
Matthias Braun's avatar
Matthias Braun committed
247
		ir_mode *mode = get_irn_mode(node);
Matthias Braun's avatar
Matthias Braun committed
248
		if (mode == mode_X || is_Cond(node) || is_Switch(node))
249
			continue;
250
#ifdef AVOID_PHIB
251
		/* we may not copy mode_b nodes, because this could produce Phi with
252
		 * mode_bs which can't be handled in all backends. Instead we duplicate
Christoph Mallon's avatar
Christoph Mallon committed
253
		 * the node and move it to its users */
254
		if (mode == mode_b) {
255
256
			ir_node *const pred = get_Proj_pred(node);
			long     const pn   = get_Proj_proj(node);
257

258
			foreach_out_edge_safe(node, edge) {
259
260
261
262
263
				ir_node *cmp_copy;
				ir_node *user       = get_edge_src_irn(edge);
				int pos             = get_edge_src_pos(edge);
				ir_node *user_block = get_nodes_block(user);

264
				if (user_block == block)
265
266
267
268
					continue;

				cmp_copy = exact_copy(pred);
				set_nodes_block(cmp_copy, user_block);
Matthias Braun's avatar
Matthias Braun committed
269
				ir_node *copy = new_r_Proj(cmp_copy, mode_b, pn);
270
271
272
273
				set_irn_n(user, pos, copy);
			}
			continue;
		}
274
#endif
Matthias Braun's avatar
Matthias Braun committed
275
		ir_node *copy = copy_and_fix_node(env, block, copy_block, j, node);
276
277

		/* we might hit values in blocks that have already been processed by a
278
		 * recursive find_phi_with_const() call */
279
		assert(get_irn_visited(copy) <= env->visited_nr);
280
		if (get_irn_visited(copy) >= env->visited_nr) {
281
			ir_node *prev_copy = (ir_node*)get_irn_link(copy);
282
			if (prev_copy != NULL)
283
284
285
				set_irn_link(node, prev_copy);
		}
	}
Christoph Mallon's avatar
Christoph Mallon committed
286

287
288
289
	/* 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
290
		ir_mode *mode = get_irn_mode(node);
Matthias Braun's avatar
Matthias Braun committed
291
		if (mode == mode_X || is_Cond(node) || is_Switch(node))
292
			continue;
293
#ifdef AVOID_PHIB
294
		if (mode == mode_b)
295
			continue;
296
#endif
297

298
299
		DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));

Matthias Braun's avatar
Matthias Braun committed
300
		ir_node *copy_node = (ir_node*)get_irn_link(node);
301
		construct_ssa(block, node, copy_block, copy_node);
302
	}
303
304
305
306
307
308
309
310
311
312
313

	/* make sure new nodes are kept alive if old nodes were */
	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);
		add_End_keepalive(end, copy);
	}
314
315
}

316
/**
Matthias Braun's avatar
Matthias Braun committed
317
 * Returns whether the cmp evaluates to true or false, or can't be evaluated!
318
 *
319
 * @param relation  the compare mode of the Compare
320
321
 * @param tv_left   the left tarval
 * @param tv_right  the right tarval
Matthias Braun's avatar
Matthias Braun committed
322
 * @returns 1: true, 0: false, -1: can't evaluate
323
 */
324
325
static int eval_cmp_tv(ir_relation relation, ir_tarval *tv_left,
                       ir_tarval *tv_right)
326
{
327
	ir_relation cmp_result = tarval_cmp(tv_left, tv_right);
328

329
	/* does the compare evaluate to true? */
330
	if (cmp_result == ir_relation_false)
331
		return -1;
332
333
	if ((cmp_result & relation) != 0)
		return 1;
334

335
	return 0;
336
337
}

338
/**
Matthias Braun's avatar
Matthias Braun committed
339
 * Returns whether the cmp evaluates to true or false, or can't be evaluated!
340
341
342
 *
 * @param env      the environment
 * @param cand     the candidate node, either a Const or a Confirm
Matthias Braun's avatar
Matthias Braun committed
343
 * @returns 1: true, 0: false, -1: can't evaluate
344
 */
345
346
static int eval_cmp(jumpthreading_env_t *env, ir_node *cand)
{
347
	if (is_Const(cand)) {
348
349
350
		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
351
352
353
354
	} else {
		assert(is_Confirm(cand));
		ir_tarval *res = computed_value_Cmp_Confirm(env->cmp, cand, env->cnst,
		                                            env->relation);
355
356
357
		if (res == tarval_bad)
			return -1;
		return res == tarval_b_true;
358
	}
359
360
361
362
363
}

/**
 * Check for Const or Confirm with Const.
 */
Matthias Braun's avatar
Matthias Braun committed
364
static bool is_Const_or_Confirm(const ir_node *node)
365
{
366
367
	if (is_Confirm(node))
		node = get_Confirm_bound(node);
368
369
370
371
	return is_Const(node);
}

/**
372
 * get the tarval of a Const or Confirm with
373
 */
Matthias Braun's avatar
Matthias Braun committed
374
static ir_tarval *get_Const_or_Confirm_tarval(const ir_node *node)
375
{
Matthias Braun's avatar
Matthias Braun committed
376
377
	if (is_Confirm(node))
		node = get_Confirm_bound(node);
378
379
380
	return get_Const_tarval(node);
}

381
382
static ir_node *find_const_or_confirm(jumpthreading_env_t *env, ir_node *jump,
                                      ir_node *value)
383
{
384
	if (irn_visited_else_mark(value))
385
386
		return NULL;

Matthias Braun's avatar
Matthias Braun committed
387
	ir_node *block = get_nodes_block(jump);
388
	if (is_Const_or_Confirm(value)) {
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
		int evaluated = eval_cmp(env, value);
		if (evaluated < 0)
			return NULL;
		/* maybe we could evaluate the condition completely without any
		 * partial tracking along paths. */
		if (block == get_nodes_block(env->cmp)) {
			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);
			}
			return block;
		}
		if (evaluated <= 0)
407
			return NULL;
Christoph Mallon's avatar
Christoph Mallon committed
408

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

412
		/* adjust true_block to point directly towards our jump */
413
		add_pred(env->true_block, jump);
414

415
416
		split_critical_edge(env->true_block, 0);

417
		/* we need a bigger visited nr when going back */
418
419
420
		env->visited_nr++;
		return block;
	}
421

422
	if (is_Phi(value)) {
423
		/* the Phi has to be in the same Block as the Jmp */
424
		if (get_nodes_block(value) != block)
425
			return NULL;
426

Matthias Braun's avatar
Matthias Braun committed
427
428
429
430
		for (int i = 0, arity = get_irn_arity(value); i < arity; ++i) {
			ir_node *phi_pred   = get_Phi_pred(value, i);
			ir_node *cfgpred    = get_Block_cfgpred(block, i);
			ir_node *copy_block = find_const_or_confirm(env, cfgpred, phi_pred);
431
			if (copy_block == NULL)
432
433
				continue;

434
			/* copy duplicated nodes in copy_block and fix SSA */
435
			copy_and_fix(env, block, copy_block, i);
436
			if (copy_block == get_nodes_block(cfgpred)) {
437
				env->cnst_pred = block;
438
				env->cnst_pos  = i;
439
			}
440

441
			/* return now as we can't process more possibilities in 1 run */
442
			return copy_block;
443
		}
Christoph Mallon's avatar
Christoph Mallon committed
444
	}
445

446
	return NULL;
Christoph Mallon's avatar
Christoph Mallon committed
447
448
}

449
static ir_node *find_candidate(jumpthreading_env_t *env, ir_node *jump,
450
                               ir_node *value)
451
{
Matthias Braun's avatar
Matthias Braun committed
452
	if (irn_visited_else_mark(value))
453
454
		return NULL;

Matthias Braun's avatar
Matthias Braun committed
455
	ir_node *block = get_nodes_block(jump);
456
	if (is_Const_or_Confirm(value)) {
Matthias Braun's avatar
Matthias Braun committed
457
		ir_tarval *tv = get_Const_or_Confirm_tarval(value);
458
		if (tv != env->tv)
459
460
			return NULL;

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

464
		/* adjust true_block to point directly towards our jump */
465
466
		add_pred(env->true_block, jump);

467
468
		split_critical_edge(env->true_block, 0);

469
		/* we need a bigger visited nr when going back */
470
471
		env->visited_nr++;
		return block;
472
	}
473
	if (is_Phi(value)) {
474
		/* the Phi has to be in the same Block as the Jmp */
475
		if (get_nodes_block(value) != block)
476
			return NULL;
477

Matthias Braun's avatar
Matthias Braun committed
478
479
480
481
		for (int i = 0, arity = get_irn_arity(value); i < arity; ++i) {
			ir_node *phi_pred   = get_Phi_pred(value, i);
			ir_node *cfgpred    = get_Block_cfgpred(block, i);
			ir_node *copy_block = find_candidate(env, cfgpred, phi_pred);
482
			if (copy_block == NULL)
483
484
485
486
				continue;

			/* copy duplicated nodes in copy_block and fix SSA */
			copy_and_fix(env, block, copy_block, i);
487
			if (copy_block == get_nodes_block(cfgpred)) {
488
489
490
491
				env->cnst_pred = block;
				env->cnst_pos  = i;
			}

492
			/* return now as we can't process more possibilities in 1 run */
493
			return copy_block;
Christoph Mallon's avatar
Christoph Mallon committed
494
		}
495
	}
496
497
498
499
500
	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);
501

502
503
		/* we assume that the constant is on the right side, swap left/right
		 * if needed */
504
		if (is_Const(left)) {
505
506
507
			ir_node *t = left;
			left       = right;
			right      = t;
508

509
			relation   = get_inversed_relation(relation);
510
		}
511

512
		if (!is_Const(right))
513
			return NULL;
514

515
516
		if (get_nodes_block(left) != block)
			return NULL;
517

518
		/* negate condition when we're looking for the false block */
Matthias Braun's avatar
Matthias Braun committed
519
		if (env->tv == tarval_b_false)
520
			relation = get_negated_relation(relation);
521

522
		/* (recursively) look if a pred of a Phi is a constant or a Confirm */
523
524
525
		env->cmp      = cmp;
		env->relation = relation;
		env->cnst     = right;
526
		return find_const_or_confirm(env, jump, left);
527
	}
528

529
530
	return NULL;
}
531
532
533
534
535
536
537
538
539
540
541
542
543
544

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

549
	/* we do not deal with Phis, so restrict this to exactly one cfgpred */
550
	if (get_Block_n_cfgpreds(block) != 1)
551
552
		return;

Matthias Braun's avatar
Matthias Braun committed
553
	ir_node *projx = get_Block_cfgpred(block, 0);
554
555
556
	if (!is_Proj(projx))
		return;

Matthias Braun's avatar
Matthias Braun committed
557
	ir_node *cond = get_Proj_pred(projx);
558
	/* TODO handle switch Conds */
Matthias Braun's avatar
Matthias Braun committed
559
	if (!is_Cond(cond))
560
		return;
561

562
	/* (recursively) look if a pred of a Phi is a constant or a Confirm */
563
564
	ir_node  *selector = get_Cond_selector(cond);
	ir_graph *irg      = get_irn_irg(block);
565
566
567
568
569
570
571
572
573
574
575
576
577
578
	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);
		return;
	}
579
	inc_irg_visited(irg);
580
581
582
583
584
	jumpthreading_env_t env;
	env.cnst_pred  = NULL;
	env.tv         = get_Proj_proj(projx) == pn_Cond_false
	                 ? tarval_b_false : tarval_b_true;
	env.true_block = block;
585
	env.visited_nr = get_irg_visited(irg);
586

Matthias Braun's avatar
Matthias Braun committed
587
	ir_node *copy_block = find_candidate(&env, projx, selector);
588
	if (copy_block == NULL)
589
590
		return;

591
592
593
594
	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
595

596
597
598
599
600
		/* 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;
601

602
603
604
		/* shorten Phis */
		foreach_out_edge_safe(env.cnst_pred, edge) {
			ir_node *node = get_edge_src_irn(edge);
605

606
607
608
609
			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
610
		}
611

612
613
		set_Block_cfgpred(env.cnst_pred, cnst_pos, badX);
	}
614
615

	/* the graph is changed now */
616
	*changed = true;
617
618
}

619
void opt_jumpthreading(ir_graph* irg)
620
{
621
622
623
624
	assure_irg_properties(irg,
		IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
		| IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
		| IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES);
625

626
	FIRM_DBG_REGISTER(dbg, "firm.opt.jumpthreading");
627

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

630
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
631

Matthias Braun's avatar
Matthias Braun committed
632
633
	bool changed = false;
	bool rerun;
634
	do {
635
		rerun = false;
636
		irg_block_walk_graph(irg, thread_jumps, NULL, &rerun);
Michael Beck's avatar
Michael Beck committed
637
638
639
		changed |= rerun;
	} while (rerun);

640
	ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
641

642
643
	confirm_irg_properties(irg,
		changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
644
}