2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Path-Sensitive Jump Threading
10 * @author Christoph Mallon, Matthias Braun
14 #include "iroptimize.h"
27 #include "iredges_t.h"
31 #include "iroptimize.h"
32 #include "iropt_dbg.h"
35 #include "firmstat_t.h"
39 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
42 * Add the new predecessor x to node node, which is either a Block or a Phi
44 static void add_pred(ir_node* node, ir_node* x)
48 int const n = get_Block_n_cfgpreds(node);
49 NEW_ARR_A(ir_node*, ins, n + 1);
50 for (int i = 0; i < n; i++)
51 ins[i] = get_irn_n(node, i);
53 set_irn_in(node, n + 1, ins);
56 static ir_node *ssa_second_def;
57 static ir_node *ssa_second_def_block;
59 static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode,
69 /* In case of a bad input to a block we need to return the bad value */
71 ir_graph *irg = get_irn_irg(block);
72 return new_r_Bad(irg, mode);
75 /* the other defs can't be marked for cases where a user of the original
76 * value is in the same block as the alternative definition.
77 * In this case we mustn't use the alternative definition.
78 * So we keep a flag that indicated whether we walked at least 1 block
79 * away and may use the alternative definition */
80 if (block == ssa_second_def_block && !first) {
81 return ssa_second_def;
84 /* already processed this block? */
85 if (irn_visited(block)) {
86 ir_node *value = (ir_node*) get_irn_link(block);
90 irg = get_irn_irg(block);
91 assert(block != get_irg_start_block(irg));
93 /* a Block with only 1 predecessor needs no Phi */
94 n_cfgpreds = get_Block_n_cfgpreds(block);
95 if (n_cfgpreds == 1) {
96 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
97 ir_node *value = search_def_and_create_phis(pred_block, mode, 0);
99 set_irn_link(block, value);
100 mark_irn_visited(block);
104 /* create a new Phi */
105 NEW_ARR_A(ir_node*, in, n_cfgpreds);
106 dummy = new_r_Dummy(irg, mode);
107 for (i = 0; i < n_cfgpreds; ++i)
110 phi = new_r_Phi(block, n_cfgpreds, in, mode);
111 set_irn_link(block, phi);
112 mark_irn_visited(block);
114 /* set Phi predecessors */
115 for (i = 0; i < n_cfgpreds; ++i) {
116 ir_node *pred_block = get_Block_cfgpred_block(block, i);
117 ir_node *pred_val = search_def_and_create_phis(pred_block, mode, 0);
119 set_irn_n(phi, i, pred_val);
126 * Given a set of values this function constructs SSA-form for the users of the
127 * first value (the users are determined through the out-edges of the value).
128 * Uses the irn_visited flags. Works without using the dominance tree.
130 static void construct_ssa(ir_node *orig_block, ir_node *orig_val,
131 ir_node *second_block, ir_node *second_val)
136 /* no need to do anything */
137 if (orig_val == second_val)
140 irg = get_irn_irg(orig_val);
141 inc_irg_visited(irg);
143 mode = get_irn_mode(orig_val);
144 set_irn_link(orig_block, orig_val);
145 mark_irn_visited(orig_block);
147 ssa_second_def_block = second_block;
148 ssa_second_def = second_val;
150 /* Only fix the users of the first, i.e. the original node */
151 foreach_out_edge_safe(orig_val, edge) {
152 ir_node *user = get_edge_src_irn(edge);
153 int j = get_edge_src_pos(edge);
154 ir_node *user_block = get_nodes_block(user);
161 DB((dbg, LEVEL_3, ">>> Fixing user %+F (pred %d == %+F)\n", user, j, get_irn_n(user, j)));
164 ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
165 newval = search_def_and_create_phis(pred_block, mode, 1);
167 newval = search_def_and_create_phis(user_block, mode, 1);
170 /* don't fix newly created Phis from the SSA construction */
171 if (newval != user) {
172 DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user, newval));
173 set_irn_n(user, j, newval);
179 * jumpthreading produces critical edges, e.g. B-C:
186 * By splitting this critical edge more threadings might be possible.
188 static void split_critical_edge(ir_node *block, int pos)
190 ir_graph *irg = get_irn_irg(block);
195 in[0] = get_Block_cfgpred(block, pos);
196 new_block = new_r_Block(irg, 1, in);
197 new_jmp = new_r_Jmp(new_block);
198 set_Block_cfgpred(block, pos, new_jmp);
201 typedef struct jumpthreading_env_t {
203 ir_node *cmp; /**< The Compare node that might be partial evaluated */
204 ir_relation relation; /**< The Compare mode of the Compare node. */
207 ir_visited_t visited_nr;
209 ir_node *cnst_pred; /**< the block before the constant */
210 int cnst_pos; /**< the pos to the constant block (needed to
211 kill that edge later) */
212 } jumpthreading_env_t;
214 static ir_node *copy_and_fix_node(const jumpthreading_env_t *env,
215 ir_node *block, ir_node *copy_block, int j,
221 /* we can evaluate Phis right now, all other nodes get copied */
223 copy = get_Phi_pred(node, j);
224 /* we might have to evaluate a Phi-cascade */
225 if (get_irn_visited(copy) >= env->visited_nr) {
226 copy = (ir_node*)get_irn_link(copy);
229 copy = exact_copy(node);
230 set_nodes_block(copy, copy_block);
232 assert(get_irn_mode(copy) != mode_X);
234 arity = get_irn_arity(copy);
235 for (i = 0; i < arity; ++i) {
236 ir_node *pred = get_irn_n(copy, i);
239 if (get_nodes_block(pred) != block)
242 if (get_irn_visited(pred) >= env->visited_nr) {
243 new_pred = (ir_node*)get_irn_link(pred);
245 new_pred = copy_and_fix_node(env, block, copy_block, j, pred);
247 DB((dbg, LEVEL_2, ">> Set Pred of %+F to %+F\n", copy, new_pred));
248 set_irn_n(copy, i, new_pred);
252 set_irn_link(node, copy);
253 set_irn_visited(node, env->visited_nr);
258 static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
259 ir_node *copy_block, int j)
261 /* Look at all nodes in the cond_block and copy them into pred */
262 foreach_out_edge(block, edge) {
263 ir_node *node = get_edge_src_irn(edge);
268 /* edge is a Keep edge. If the end block is unreachable via normal
269 * control flow, we must maintain end's reachability with Keeps.
271 keep_alive(copy_block);
274 /* ignore control flow */
275 mode = get_irn_mode(node);
276 if (mode == mode_X || is_Cond(node) || is_Switch(node))
279 /* we may not copy mode_b nodes, because this could produce Phi with
280 * mode_bs which can't be handled in all backends. Instead we duplicate
281 * the node and move it to its users */
282 if (mode == mode_b) {
283 ir_node *const pred = get_Proj_pred(node);
284 long const pn = get_Proj_proj(node);
286 foreach_out_edge_safe(node, edge) {
288 ir_node *user = get_edge_src_irn(edge);
289 int pos = get_edge_src_pos(edge);
290 ir_node *user_block = get_nodes_block(user);
292 if (user_block == block)
295 cmp_copy = exact_copy(pred);
296 set_nodes_block(cmp_copy, user_block);
297 copy = new_r_Proj(cmp_copy, mode_b, pn);
298 set_irn_n(user, pos, copy);
304 copy = copy_and_fix_node(env, block, copy_block, j, node);
306 /* we might hit values in blocks that have already been processed by a
307 * recursive find_phi_with_const() call */
308 assert(get_irn_visited(copy) <= env->visited_nr);
309 if (get_irn_visited(copy) >= env->visited_nr) {
310 ir_node *prev_copy = (ir_node*)get_irn_link(copy);
311 if (prev_copy != NULL)
312 set_irn_link(node, prev_copy);
316 /* fix data-flow (and reconstruct SSA if needed) */
317 foreach_out_edge(block, edge) {
318 ir_node *node = get_edge_src_irn(edge);
322 mode = get_irn_mode(node);
323 if (mode == mode_X || is_Cond(node) || is_Switch(node))
330 DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));
332 copy_node = (ir_node*)get_irn_link(node);
333 construct_ssa(block, node, copy_block, copy_node);
336 /* make sure new nodes are kept alive if old nodes were */
337 ir_graph *irg = get_irn_irg(block);
338 ir_node *end = get_irg_end(irg);
339 for (int i = 0, arity = get_End_n_keepalives(end); i < arity; ++i) {
340 ir_node *keep = get_End_keepalive(end, i);
341 if (get_irn_visited(keep) < env->visited_nr || is_Block(keep))
343 ir_node *copy = get_irn_link(keep);
344 add_End_keepalive(end, copy);
349 * returns whether the cmp evaluates to true or false, or can't be evaluated!
350 * 1: true, 0: false, -1: can't evaluate
352 * @param relation the compare mode of the Compare
353 * @param tv_left the left tarval
354 * @param tv_right the right tarval
356 static int eval_cmp_tv(ir_relation relation, ir_tarval *tv_left,
359 ir_relation cmp_result = tarval_cmp(tv_left, tv_right);
361 /* does the compare evaluate to true? */
362 if (cmp_result == ir_relation_false)
364 if ((cmp_result & relation) != 0)
371 * returns whether the cmp evaluates to true or false, or can't be evaluated!
372 * 1: true, 0: false, -1: can't evaluate
374 * @param env the environment
375 * @param cand the candidate node, either a Const or a Confirm
377 static int eval_cmp(jumpthreading_env_t *env, ir_node *cand)
379 if (is_Const(cand)) {
380 ir_tarval *tv_cand = get_Const_tarval(cand);
381 ir_tarval *tv_cmp = get_Const_tarval(env->cnst);
383 return eval_cmp_tv(env->relation, tv_cand, tv_cmp);
384 } else { /* a Confirm */
385 ir_tarval *res = computed_value_Cmp_Confirm(env->cmp, cand, env->cnst, env->relation);
387 if (res == tarval_bad)
389 return res == tarval_b_true;
394 * Check for Const or Confirm with Const.
396 static int is_Const_or_Confirm(const ir_node *node)
398 if (is_Confirm(node))
399 node = get_Confirm_bound(node);
400 return is_Const(node);
404 * get the tarval of a Const or Confirm with
406 static ir_tarval *get_Const_or_Confirm_tarval(const ir_node *node)
408 if (is_Confirm(node)) {
409 if (get_Confirm_bound(node))
410 node = get_Confirm_bound(node);
412 return get_Const_tarval(node);
415 static ir_node *find_const_or_confirm(jumpthreading_env_t *env, ir_node *jump,
418 ir_node *block = get_nodes_block(jump);
420 if (irn_visited_else_mark(value))
423 if (is_Const_or_Confirm(value)) {
424 if (eval_cmp(env, value) <= 0)
429 "> Found jump threading candidate %+F->%+F\n",
430 block, env->true_block
433 /* adjust true_block to point directly towards our jump */
434 add_pred(env->true_block, jump);
436 split_critical_edge(env->true_block, 0);
438 /* we need a bigger visited nr when going back */
447 /* the Phi has to be in the same Block as the Jmp */
448 if (get_nodes_block(value) != block)
451 arity = get_irn_arity(value);
452 for (i = 0; i < arity; ++i) {
454 ir_node *phi_pred = get_Phi_pred(value, i);
455 ir_node *cfgpred = get_Block_cfgpred(block, i);
457 copy_block = find_const_or_confirm(env, cfgpred, phi_pred);
458 if (copy_block == NULL)
461 /* copy duplicated nodes in copy_block and fix SSA */
462 copy_and_fix(env, block, copy_block, i);
464 if (copy_block == get_nodes_block(cfgpred)) {
465 env->cnst_pred = block;
469 /* return now as we can't process more possibilities in 1 run */
477 static ir_node *find_candidate(jumpthreading_env_t *env, ir_node *jump,
480 ir_node *block = get_nodes_block(jump);
482 if (irn_visited_else_mark(value)) {
486 if (is_Const_or_Confirm(value)) {
487 ir_tarval *tv = get_Const_or_Confirm_tarval(value);
494 "> Found jump threading candidate %+F->%+F\n",
495 block, env->true_block
498 /* adjust true_block to point directly towards our jump */
499 add_pred(env->true_block, jump);
501 split_critical_edge(env->true_block, 0);
503 /* we need a bigger visited nr when going back */
511 /* the Phi has to be in the same Block as the Jmp */
512 if (get_nodes_block(value) != block)
515 arity = get_irn_arity(value);
516 for (i = 0; i < arity; ++i) {
518 ir_node *phi_pred = get_Phi_pred(value, i);
519 ir_node *cfgpred = get_Block_cfgpred(block, i);
521 copy_block = find_candidate(env, cfgpred, phi_pred);
522 if (copy_block == NULL)
525 /* copy duplicated nodes in copy_block and fix SSA */
526 copy_and_fix(env, block, copy_block, i);
528 if (copy_block == get_nodes_block(cfgpred)) {
529 env->cnst_pred = block;
533 /* return now as we can't process more possibilities in 1 run */
538 ir_node *cmp = value;
539 ir_node *left = get_Cmp_left(cmp);
540 ir_node *right = get_Cmp_right(cmp);
541 ir_relation relation = get_Cmp_relation(cmp);
543 /* we assume that the constant is on the right side, swap left/right
545 if (is_Const(left)) {
550 relation = get_inversed_relation(relation);
553 if (!is_Const(right))
556 if (get_nodes_block(left) != block)
559 /* negate condition when we're looking for the false block */
560 if (env->tv == tarval_b_false) {
561 relation = get_negated_relation(relation);
564 /* (recursively) look if a pred of a Phi is a constant or a Confirm */
566 env->relation = relation;
569 return find_const_or_confirm(env, jump, left);
576 * Block-walker: searches for the following construct
578 * Const or Phi with constants
588 static void thread_jumps(ir_node* block, void* data)
590 jumpthreading_env_t env;
591 bool *changed = (bool*)data;
596 int selector_evaluated;
601 /* we do not deal with Phis, so restrict this to exactly one cfgpred */
602 if (get_Block_n_cfgpreds(block) != 1)
605 projx = get_Block_cfgpred(block, 0);
608 assert(get_irn_mode(projx) == mode_X);
610 cond = get_Proj_pred(projx);
611 /* TODO handle switch Conds */
615 /* handle cases that can be immediately evaluated */
616 selector = get_Cond_selector(cond);
617 selector_evaluated = -1;
618 if (is_Cmp(selector)) {
619 ir_node *left = get_Cmp_left(selector);
620 ir_node *right = get_Cmp_right(selector);
621 if (is_Const(left) && is_Const(right)) {
622 ir_relation relation = get_Cmp_relation(selector);
623 ir_tarval *tv_left = get_Const_tarval(left);
624 ir_tarval *tv_right = get_Const_tarval(right);
626 selector_evaluated = eval_cmp_tv(relation, tv_left, tv_right);
628 } else if (is_Const_or_Confirm(selector)) {
629 ir_tarval *tv = get_Const_or_Confirm_tarval(selector);
630 if (tv == tarval_b_true) {
631 selector_evaluated = 1;
633 assert(tv == tarval_b_false);
634 selector_evaluated = 0;
638 env.cnst_pred = NULL;
639 if (get_Proj_proj(projx) == pn_Cond_false) {
640 env.tv = tarval_b_false;
641 if (selector_evaluated >= 0)
642 selector_evaluated = !selector_evaluated;
644 env.tv = tarval_b_true;
647 if (selector_evaluated == 0) {
648 ir_graph *irg = get_irn_irg(block);
649 ir_node *bad = new_r_Bad(irg, mode_X);
650 exchange(projx, bad);
653 } else if (selector_evaluated == 1) {
654 dbg_info *dbgi = get_irn_dbg_info(selector);
655 ir_node *jmp = new_rd_Jmp(dbgi, get_nodes_block(projx));
656 DBG_OPT_JUMPTHREADING(projx, jmp);
657 exchange(projx, jmp);
662 /* (recursively) look if a pred of a Phi is a constant or a Confirm */
663 env.true_block = block;
664 irg = get_irn_irg(block);
665 inc_irg_visited(irg);
666 env.visited_nr = get_irg_visited(irg);
668 copy_block = find_candidate(&env, projx, selector);
669 if (copy_block == NULL)
672 /* We might thread the condition block of an infinite loop,
673 * such that there is no path to End anymore. */
676 /* we have to remove the edge towards the pred as the pred now
677 * jumps into the true_block. We also have to shorten Phis
678 * in our block because of this */
679 badX = new_r_Bad(irg, mode_X);
680 cnst_pos = env.cnst_pos;
683 foreach_out_edge_safe(env.cnst_pred, edge) {
684 ir_node *node = get_edge_src_irn(edge);
687 ir_node *bad = new_r_Bad(irg, get_irn_mode(node));
688 set_Phi_pred(node, cnst_pos, bad);
692 set_Block_cfgpred(env.cnst_pred, cnst_pos, badX);
694 /* the graph is changed now */
698 void opt_jumpthreading(ir_graph* irg)
703 assure_irg_properties(irg,
704 IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
705 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
706 | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES);
708 FIRM_DBG_REGISTER(dbg, "firm.opt.jumpthreading");
710 DB((dbg, LEVEL_1, "===> Performing jumpthreading on %+F\n", irg));
712 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
717 irg_block_walk_graph(irg, thread_jumps, NULL, &rerun);
721 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
723 confirm_irg_properties(irg,
724 changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
727 /* Creates an ir_graph pass for opt_jumpthreading. */
728 ir_graph_pass_t *opt_jumpthreading_pass(const char *name)
730 return def_graph_pass(name ? name : "jumpthreading", opt_jumpthreading);