2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Partial condition evaluation
24 * @author Christoph Mallon, Matthias Braun
31 #include "iroptimize.h"
43 #include "iredges_t.h"
50 DEBUG_ONLY(static firm_dbg_module_t *dbg);
53 * Add the new predecessor x to node node, which is either a Block or a Phi
55 static void add_pred(ir_node* node, ir_node* x)
61 assert(is_Block(node) || is_Phi(node));
63 n = get_irn_arity(node);
64 NEW_ARR_A(ir_node*, ins, n + 1);
65 for (i = 0; i < n; i++)
66 ins[i] = get_irn_n(node, i);
68 set_irn_in(node, n + 1, ins);
71 static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode)
79 // This is needed because we create bads sometimes
83 // already processed this block?
84 if(irn_visited(block)) {
85 ir_node *value = (ir_node*) get_irn_link(block);
89 irg = get_irn_irg(block);
90 assert(block != get_irg_start_block(irg));
92 // blocks with only 1 pred need no phi
93 n_cfgpreds = get_Block_n_cfgpreds(block);
95 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
96 ir_node *value = search_def_and_create_phis(pred_block, mode);
98 set_irn_link(block, value);
99 mark_irn_visited(block);
104 NEW_ARR_A(ir_node*, in, n_cfgpreds);
105 for(i = 0; i < n_cfgpreds; ++i)
106 in[i] = new_Unknown(mode);
108 phi = new_r_Phi(irg, block, n_cfgpreds, in, mode);
109 set_irn_link(block, phi);
110 mark_irn_visited(block);
113 for(i = 0; i < n_cfgpreds; ++i) {
114 ir_node *pred_block = get_Block_cfgpred_block(block, i);
115 ir_node *pred_val = search_def_and_create_phis(pred_block, mode);
117 set_irn_n(phi, i, pred_val);
124 * Given a set of values this function constructs SSA-form for the users of the
125 * first value (the users are determined through the out-edges of the value).
126 * Uses the irn_visited flags. Works without using the dominance tree.
128 static void construct_ssa(ir_node * const *blocks, ir_node * const *vals, int n_vals)
133 const ir_edge_t *edge;
134 const ir_edge_t *next;
139 irg = get_irn_irg(vals[0]);
140 inc_irg_visited(irg);
142 mode = get_irn_mode(vals[0]);
143 for(i = 0; i < n_vals; ++i) {
144 ir_node *value = vals[i];
145 ir_node *value_block = blocks[i];
147 assert(get_irn_mode(value) == mode);
149 set_irn_link(value_block, value);
150 mark_irn_visited(value_block);
153 // Only fix the users of the first, i.e. the original node
156 foreach_out_edge_safe(value, edge, next) {
157 ir_node *user = get_edge_src_irn(edge);
158 int j = get_edge_src_pos(edge);
159 ir_node *user_block = get_nodes_block(user);
163 if(get_irn_op(user) == op_End)
166 if (user_block == blocks[1])
169 DB((dbg, LEVEL_3, ">>> Fixing user %+F (pred %d == %+F)\n", user, j, get_irn_n(user, j)));
172 ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
173 newval = search_def_and_create_phis(pred_block, mode);
175 newval = search_def_and_create_phis(user_block, mode);
178 // don't fix newly created phis from the SSA construction
179 if (newval != user) {
180 DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user, newval));
181 set_irn_n(user, j, newval);
186 static void split_critical_edge(ir_node *block, int pos) {
187 ir_graph *irg = get_irn_irg(block);
192 in[0] = get_Block_cfgpred(block, pos);
193 new_block = new_r_Block(irg, 1, in);
194 new_jmp = new_r_Jmp(irg, new_block);
195 set_Block_cfgpred(block, pos, new_jmp);
198 typedef struct condeval_env_t {
203 unsigned long visited_nr;
205 ir_node *cnst_pred; /**< the block before the constant */
206 int cnst_pos; /**< the pos to the constant block (needed to
207 kill that edge later) */
210 static ir_node *copy_and_fix_node(const condeval_env_t *env, ir_node *block,
211 ir_node *copy_block, int j, ir_node *node) {
215 /* we can evaluate Phis right now, all other nodes get copied */
217 copy = get_Phi_pred(node, j);
218 /* we might have to evaluate a phi-cascades */
219 if(get_irn_visited(copy) >= env->visited_nr) {
220 copy = get_irn_link(copy);
223 copy = exact_copy(node);
224 set_nodes_block(copy, copy_block);
226 assert(get_irn_mode(copy) != mode_X);
228 arity = get_irn_arity(copy);
229 for(i = 0; i < arity; ++i) {
230 ir_node *pred = get_irn_n(copy, i);
233 if(get_nodes_block(pred) != block)
236 if(get_irn_visited(pred) >= env->visited_nr) {
237 new_pred = get_irn_link(pred);
239 new_pred = copy_and_fix_node(env, block, copy_block, j, pred);
241 set_irn_n(copy, i, new_pred);
245 set_irn_link(node, copy);
246 set_irn_visited(node, env->visited_nr);
251 static void copy_and_fix(const condeval_env_t *env, ir_node *block,
252 ir_node *copy_block, int j) {
253 const ir_edge_t *edge;
255 /* Look at all nodes in the cond_block and copy them into pred */
256 foreach_out_edge(block, edge) {
257 ir_node *node = get_edge_src_irn(edge);
261 if (is_Block(node)) {
262 /* Block->Block edge, should be the MacroBlock edge */
263 assert(get_Block_MacroBlock(node) == block && "Block->Block edge found");
267 /* ignore control flow */
268 mode = get_irn_mode(node);
269 if (mode == mode_X || is_Cond(node))
272 /* we may not copy mode_b nodes, because this could produce phi with
273 * mode_bs which can't be handled in all backends. Instead we duplicate
274 * the node and move it to its users */
275 if (mode == mode_b) {
276 const ir_edge_t *edge, *next;
280 assert(is_Proj(node));
282 pred = get_Proj_pred(node);
283 pn = get_Proj_proj(node);
285 foreach_out_edge_safe(node, edge, next) {
287 ir_node *user = get_edge_src_irn(edge);
288 int pos = get_edge_src_pos(edge);
289 ir_node *user_block = get_nodes_block(user);
291 if(user_block == block)
294 cmp_copy = exact_copy(pred);
295 set_nodes_block(cmp_copy, user_block);
296 copy = new_r_Proj(current_ir_graph, user_block, cmp_copy, mode_b, pn);
297 set_irn_n(user, pos, copy);
303 copy = copy_and_fix_node(env, block, copy_block, j, node);
305 /* we might hit values in blocks that have already been processed by a
306 * recursive find_phi_with_const call */
307 assert(get_irn_visited(copy) <= env->visited_nr);
308 if(get_irn_visited(copy) >= env->visited_nr) {
309 ir_node *prev_copy = get_irn_link(copy);
310 if(prev_copy != NULL)
311 set_irn_link(node, prev_copy);
315 /* fix data-flow (and reconstruct SSA if needed) */
316 foreach_out_edge(block, edge) {
319 ir_node *node = get_edge_src_irn(edge);
322 if (is_Block(node)) {
323 /* Block->Block edge, should be the MacroBlock edge */
324 assert(get_Block_MacroBlock(node) == block && "Block->Block edge found");
328 mode = get_irn_mode(node);
329 if (mode == mode_X || is_Cond(node))
336 DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));
340 blocks[1] = copy_block;
341 vals[1] = get_irn_link(node);
342 construct_ssa(blocks, vals, 2);
347 * returns whether the cmp evaluates to true or false, or can't be evaluated!
348 * 1: true, 0: false, -1: can't evaluate
350 static int eval_cmp(pn_Cmp pnc, tarval *tv1, tarval *tv2) {
351 pn_Cmp cmp_result = tarval_cmp(tv1, tv2);
353 // does the compare evaluate to true?
354 if(cmp_result == pn_Cmp_False)
356 if((cmp_result & pnc) != cmp_result)
363 * Check for Const or constlike Confirm.
365 static int is_Const_or_Confirm(const ir_node *node) {
366 if (is_Confirm(node)) {
367 if (get_Confirm_cmp(node) == pn_Cmp_Eq)
368 node = get_Confirm_bound(node);
370 return is_Const(node);
374 * get the tarval of a COnst or constlike Confirm
376 static tarval *get_Const_or_Confirm_tarval(const ir_node *node) {
377 if (is_Confirm(node)) {
378 if (get_Confirm_cmp(node) == pn_Cmp_Eq)
379 node = get_Confirm_bound(node);
381 return get_Const_tarval(node);
384 static ir_node *find_const(condeval_env_t *env, ir_node *jump, ir_node *value)
386 ir_node *block = get_nodes_block(jump);
388 if (irn_visited(value))
390 mark_irn_visited(value);
392 if(is_Const_or_Confirm(value)) {
393 tarval *tv_const = get_Const_tarval(env->cnst);
394 tarval *tv = get_Const_or_Confirm_tarval(value);
396 if(eval_cmp(env->pnc, tv, tv_const) <= 0) {
402 "> Found condition evaluation candidate %+F->%+F\n",
403 env->true_block, block
406 // adjust true_block to point directly towards our jump
407 add_pred(env->true_block, jump);
409 split_critical_edge(env->true_block, 0);
411 // we need a bigger visited nr when going back
420 /* the phi has to be in the same block as the jump */
421 if(get_nodes_block(value) != block) {
425 arity = get_irn_arity(value);
426 for(i = 0; i < arity; ++i) {
428 ir_node *phi_pred = get_Phi_pred(value, i);
429 ir_node *cfgpred = get_Block_cfgpred(block, i);
431 copy_block = find_const(env, cfgpred, phi_pred);
432 if(copy_block == NULL)
435 /* copy duplicated nodes in copy_block and fix SSA */
436 copy_and_fix(env, block, copy_block, i);
438 if(copy_block == get_nodes_block(cfgpred)) {
439 env->cnst_pred = block;
443 /* return now as we can't process more possibilities in 1 run */
451 static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
454 ir_node *block = get_nodes_block(jump);
456 if(irn_visited(value)) {
459 mark_irn_visited(value);
461 if(is_Const_or_Confirm(value)) {
462 tarval *tv = get_Const_or_Confirm_tarval(value);
469 "> Found condition evaluation candidate %+F->%+F\n",
470 env->true_block, block
473 // adjust true_block to point directly towards our jump
474 add_pred(env->true_block, jump);
476 split_critical_edge(env->true_block, 0);
478 // we need a bigger visited nr when going back
486 // the phi has to be in the same block as the jump
487 if(get_nodes_block(value) != block)
490 arity = get_irn_arity(value);
491 for(i = 0; i < arity; ++i) {
493 ir_node *phi_pred = get_Phi_pred(value, i);
494 ir_node *cfgpred = get_Block_cfgpred(block, i);
496 copy_block = find_candidate(env, cfgpred, phi_pred);
497 if(copy_block == NULL)
500 /* copy duplicated nodes in copy_block and fix SSA */
501 copy_and_fix(env, block, copy_block, i);
503 if(copy_block == get_nodes_block(cfgpred)) {
504 env->cnst_pred = block;
508 // return now as we can't process more possibilities in 1 run
516 ir_node *cmp = get_Proj_pred(value);
520 left = get_Cmp_left(cmp);
521 right = get_Cmp_right(cmp);
522 pnc = get_Proj_proj(value);
524 /* we assume that the constant is on the right side, swap left/right
531 pnc = get_inversed_pnc(pnc);
537 if(get_nodes_block(left) != block) {
541 /* negate condition when we're looking for the false block */
542 if(env->tv == get_tarval_b_false()) {
543 pnc = get_negated_pnc(pnc, get_irn_mode(right));
546 // (recursively) look if a pred of a phi is a constant
550 return find_const(env, jump, left);
557 * Block-walker: searches for the following construct
559 * Const or Phi with constants
569 static void cond_eval(ir_node* block, void* data)
577 int selector_evaluated;
578 const ir_edge_t *edge, *next;
582 if(get_Block_n_cfgpreds(block) != 1)
585 projx = get_Block_cfgpred(block, 0);
588 assert(get_irn_mode(projx) == mode_X);
590 cond = get_Proj_pred(projx);
594 selector = get_Cond_selector(cond);
595 // TODO handle switch Conds
596 if (get_irn_mode(selector) != mode_b)
599 /* handle cases that can be immediately evaluated */
600 selector_evaluated = -1;
601 if(is_Proj(selector)) {
602 ir_node *cmp = get_Proj_pred(selector);
604 ir_node *left = get_Cmp_left(cmp);
605 ir_node *right = get_Cmp_right(cmp);
606 if(is_Const(left) && is_Const(right)) {
607 int pnc = get_Proj_proj(selector);
608 tarval *tv_left = get_Const_tarval(left);
609 tarval *tv_right = get_Const_tarval(right);
611 selector_evaluated = eval_cmp(pnc, tv_left, tv_right);
612 if(selector_evaluated < 0)
616 } else if(is_Const_or_Confirm(selector)) {
617 tarval *tv = get_Const_or_Confirm_tarval(selector);
618 if(tv == get_tarval_b_true()) {
619 selector_evaluated = 1;
621 assert(tv == get_tarval_b_false());
622 selector_evaluated = 0;
626 env.cnst_pred = NULL;
627 if (get_Proj_proj(projx) == pn_Cond_false) {
628 env.tv = get_tarval_b_false();
629 if(selector_evaluated >= 0)
630 selector_evaluated = !selector_evaluated;
632 env.tv = get_tarval_b_true();
635 if(selector_evaluated == 0) {
637 exchange(projx, bad);
640 } else if(selector_evaluated == 1) {
641 dbg_info *dbgi = get_irn_dbg_info(selector);
642 ir_node *jmp = new_rd_Jmp(dbgi, current_ir_graph, get_nodes_block(projx));
643 exchange(projx, jmp);
648 // (recursively) look if a pred of a phi is a constant
649 env.true_block = block;
650 inc_irg_visited(current_ir_graph);
651 env.visited_nr = get_irg_visited(current_ir_graph);
653 copy_block = find_candidate(&env, projx, selector);
654 if (copy_block == NULL)
657 /* we have to remove the edge towards the pred as the pred now
658 * jumps into the true_block. We also have to shorten phis
659 * in our block because of this */
661 cnst_pos = env.cnst_pos;
664 foreach_out_edge_safe(env.cnst_pred, edge, next) {
665 ir_node *node = get_edge_src_irn(edge);
668 set_Phi_pred(node, cnst_pos, bad);
671 set_Block_cfgpred(env.cnst_pred, cnst_pos, bad);
673 /* the graph is changed now */
677 void opt_cond_eval(ir_graph* irg)
681 FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
683 DB((dbg, LEVEL_1, "===> Performing condition evaluation on %+F\n", irg));
685 remove_critical_cf_edges(irg);
686 normalize_proj_nodes(irg);
689 set_using_irn_link(irg);
690 set_using_irn_visited(irg);
695 irg_block_walk_graph(irg, cond_eval, NULL, &rerun);
699 clear_using_irn_visited(irg);
700 clear_using_irn_link(irg);
703 /* control flow changed, some blocks may become dead */
704 set_irg_outs_inconsistent(irg);
705 set_irg_doms_inconsistent(irg);
706 set_irg_extblk_inconsistent(irg);
707 set_irg_loopinfo_inconsistent(irg);
709 /* Dead code might be created. Optimize it away as it is dangerous
710 * to call optimize_df() an dead code. */