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 Construction of Confirm nodes
23 * @author Michael Beck
29 #include "irconsconfirm.h"
31 #include "irgraph_t.h"
35 #include "iropt_dbg.h"
36 #include "iredges_t.h"
49 typedef struct _env_t {
50 unsigned num_confirms; /**< Number of inserted Confirm nodes. */
51 unsigned num_consts; /**< Number of constants placed. */
52 unsigned num_eq; /**< Number of equalities placed. */
53 unsigned num_non_null; /**< Number of non-null Confirms. */
56 /** The debug handle. */
57 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
60 * Return the effective use block of a node and it's predecessor on
63 * @param node the node
64 * @param pos the position of the used input
66 * This handles correctly Phi nodes.
68 static ir_node *get_effective_use_block(ir_node *node, int pos)
71 /* the effective use of a Phi argument is in its predecessor block */
72 node = get_nodes_block(node);
73 return get_Block_cfgpred_block(node, pos);
75 return get_nodes_block(node);
79 * Handle a CASE-branch.
81 * @param block the block which is entered by the branch
82 * @param irn the node expressing the switch value
83 * @param nr the branch label
84 * @param env statistical environment
86 * Branch labels are a simple case. We can replace the value
87 * by a Const with the branch label.
89 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env)
91 const ir_edge_t *edge, *next;
97 for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
98 ir_node *succ = get_edge_src_irn(edge);
99 int pos = get_edge_src_pos(edge);
100 ir_node *blk = get_effective_use_block(succ, pos);
102 next = get_irn_out_edge_next(irn, edge);
104 if (block_dominates(block, blk)) {
106 * Ok, we found a user of irn that is placed
107 * in a block dominated by the branch block.
108 * We can replace the input with the Constant
113 ir_mode *mode = get_irn_mode(irn);
114 ir_type *tp = get_irn_type(irn);
115 tarval *tv = new_tarval_from_long(nr, mode);
116 c = new_r_Const_type(current_ir_graph, tv, tp);
119 set_irn_n(succ, pos, c);
120 DBG_OPT_CONFIRM_C(irn, c);
121 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
123 env->num_consts += 1;
129 * Handle a mode_b input of Cond nodes.
131 * @param block the block which is entered by the branch
132 * @param selector the mode_b node expressing the branch condition
133 * @param pnc the true/false condition branch
134 * @param env statistical environment
136 static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env)
138 ir_node *cond, *old, *cond_block = NULL, *other_blk = NULL, *con = NULL;
139 ir_node *c_b = NULL, *c_o = NULL;
140 const ir_edge_t *edge, *next;
142 for (edge = get_irn_out_edge_first(selector); edge; edge = next) {
143 ir_node *user = get_edge_src_irn(edge);
144 int pos = get_edge_src_pos(edge);
145 ir_node *user_blk = get_effective_use_block(user, pos);
147 next = get_irn_out_edge_next(selector, edge);
148 if (block_dominates(block, user_blk)) {
150 * Ok, we found a usage of selector in a block
151 * dominated by the branch block.
152 * We can replace the input with true/false.
155 con = new_Const(pnc == pn_Cond_true ? tarval_b_true : tarval_b_false);
157 old = get_irn_n(user, pos);
158 set_irn_n(user, pos, con);
159 DBG_OPT_CONFIRM_C(old, con);
161 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, user, con));
163 env->num_consts += 1;
167 /* get the other block */
168 if (other_blk == NULL) {
169 /* we have already tested, that block has only ONE Cond predecessor */
170 cond = get_Proj_pred(get_Block_cfgpred(block, 0));
171 cond_block = get_nodes_block(cond);
172 foreach_out_edge(cond, edge) {
173 ir_node *proj = get_edge_src_irn(edge);
174 if (get_Proj_proj(proj) == (long)pnc)
176 edge = get_irn_out_edge_first(proj);
177 other_blk = get_edge_src_irn(edge);
183 * Note the special case here: if block is a then, there might be no else
184 * block. In that case the other_block is the user_blk itself and pred_block
185 * is the cond_block ...
187 * Best would be to introduce a block here, removing this critical edge.
188 * For some reasons I cannot repair dominance here, so I have to remove
189 * ALL critical edges...
190 * FIXME: This should not be needed if we could repair dominance ...
194 n = get_Block_n_cfgpreds(user_blk);
197 * We have found a user in a non-dominated block:
198 * check, if all its block predecessors are dominated.
199 * If yes, place a Phi.
201 for (i = n - 1; i >= 0; --i) {
202 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
204 if (!block_dominates(block, pred_blk) &&
205 !block_dominates(other_blk, pred_blk)) {
206 /* can't do anything */
213 NEW_ARR_A(ir_node *, in, n);
214 /* ok, ALL predecessors are either dominated by block OR other block */
216 ir_node *c_true = new_Const(tarval_b_true);
217 ir_node *c_false = new_Const(tarval_b_false);
218 if (pnc == pn_Cond_true) {
226 for (i = n - 1; i >= 0; --i) {
227 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
229 if (block_dominates(block, pred_blk))
234 phi = new_r_Phi(user_blk, n, in, mode_b);
235 set_irn_n(user, pos, phi);
242 * Handle an IF-branch.
244 * @param block the block which is entered by the branch
245 * @param cmp the Cmp node expressing the branch condition
246 * @param pnc the Compare relation for taking this branch
247 * @param env statistical environment
249 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env)
251 ir_node *left = get_Cmp_left(cmp);
252 ir_node *right = get_Cmp_right(cmp);
255 const ir_edge_t *edge, *next;
258 if (is_Bad(left) || is_Bad(right))
261 op = get_irn_op(left);
263 /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
264 These are removed anyway */
265 if (op == op_Const && is_Const(right))
268 /* try to place the constant on the right side for a Confirm */
269 if (op == op_Const || op == op_SymConst) {
275 pnc = get_inversed_pnc(pnc);
279 * First case: both values are identical.
280 * replace the left one by the right (potentially const) one.
282 if (pnc == pn_Cmp_Eq) {
283 cond_block = get_Block_cfgpred_block(block, 0);
284 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
285 ir_node *user = get_edge_src_irn(edge);
286 int pos = get_edge_src_pos(edge);
287 ir_node *blk = get_effective_use_block(user, pos);
289 next = get_irn_out_edge_next(left, edge);
290 if (block_dominates(block, blk)) {
292 * Ok, we found a usage of left in a block
293 * dominated by the branch block.
294 * We can replace the input with right.
296 set_irn_n(user, pos, right);
297 DBG_OPT_CONFIRM(left, right);
299 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, user, right));
302 } else if (block_dominates(blk, cond_block)) {
303 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
305 * left == Const and we found a movable user of left in a
306 * dominator of the Cond block
308 const ir_edge_t *edge, *next;
309 for (edge = get_irn_out_edge_first(user); edge; edge = next) {
310 ir_node *usr_of_usr = get_edge_src_irn(edge);
311 int npos = get_edge_src_pos(edge);
312 ir_node *blk = get_effective_use_block(usr_of_usr, npos);
314 next = get_irn_out_edge_next(user, edge);
315 if (block_dominates(block, blk)) {
317 * The user of the user is dominated by our true/false
318 * block. So, create a copy of user WITH the constant
319 * replacing it's pos'th input.
321 * This is always good for unop's and might be good
324 * If user has other user in the false/true block, code
325 * placement will move it down.
326 * If there are users in cond block or upper, we create
327 * "redundant ops", because one will have a const op,
328 * the other no const ...
330 ir_node *new_op = exact_copy(user);
331 set_nodes_block(new_op, block);
332 set_irn_n(new_op, pos, right);
333 set_irn_n(usr_of_usr, npos, new_op);
339 } else { /* not pn_Cmp_Eq cases */
342 foreach_out_edge_safe(left, edge, next) {
343 ir_node *succ = get_edge_src_irn(edge);
344 int pos = get_edge_src_pos(edge);
345 ir_node *blk = get_effective_use_block(succ, pos);
347 if (block_dominates(block, blk)) {
349 * Ok, we found a usage of left in a block
350 * dominated by the branch block.
351 * We can replace the input with a Confirm(left, pnc, right).
354 c = new_r_Confirm(block, left, right, pnc);
356 pos = get_edge_src_pos(edge);
357 set_irn_n(succ, pos, c);
358 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
360 env->num_confirms += 1;
364 if (! is_Const(right)) {
365 /* also construct inverse Confirms */
368 pnc = get_inversed_pnc(pnc);
369 foreach_out_edge_safe(right, edge, next) {
370 ir_node *succ = get_edge_src_irn(edge);
377 pos = get_edge_src_pos(edge);
378 blk = get_effective_use_block(succ, pos);
380 if (block_dominates(block, blk)) {
382 * Ok, we found a usage of right in a block
383 * dominated by the branch block.
384 * We can replace the input with a Confirm(right, pnc^-1, left).
387 rc = new_r_Confirm(block, right, left, pnc);
389 pos = get_edge_src_pos(edge);
390 set_irn_n(succ, pos, rc);
391 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, rc));
393 env->num_confirms += 1;
401 * Pre-block-walker: Called for every block to insert Confirm nodes
403 static void insert_Confirm_in_block(ir_node *block, void *env)
405 ir_node *cond, *proj, *selector;
409 * we can only handle blocks with only ONE control flow
412 if (get_Block_n_cfgpreds(block) != 1)
415 proj = get_Block_cfgpred(block, 0);
419 cond = get_Proj_pred(proj);
423 selector = get_Cond_selector(cond);
424 mode = get_irn_mode(selector);
426 if (mode == mode_b) {
430 handle_modeb(block, selector, get_Proj_proj(proj), env);
432 /* this should be an IF, check this */
433 if (! is_Proj(selector))
436 cmp = get_Proj_pred(selector);
440 pnc = get_Proj_proj(selector);
442 if (get_Proj_proj(proj) != pn_Cond_true) {
443 /* it's the false branch */
444 mode = get_irn_mode(get_Cmp_left(cmp));
445 pnc = get_negated_pnc(pnc, mode);
447 DB((dbg, LEVEL_2, "At %+F using %+F Confirm %=\n", block, cmp, pnc));
449 handle_if(block, cmp, pnc, env);
450 } else if (mode_is_int(mode)) {
451 long proj_nr = get_Proj_proj(proj);
453 /* this is a CASE, but we cannot handle the default case */
454 if (proj_nr == get_Cond_default_proj(cond))
457 handle_case(block, get_Cond_selector(cond), proj_nr, env);
459 } /* insert_Confirm_in_block */
462 * Checks if a node is a non-null Confirm.
464 static int is_non_null_Confirm(const ir_node *ptr)
467 if (! is_Confirm(ptr))
469 if (get_Confirm_cmp(ptr) == pn_Cmp_Lg) {
470 ir_node *bound = get_Confirm_bound(ptr);
472 if (is_Const(bound) && is_Const_null(bound))
475 ptr = get_Confirm_value(ptr);
478 * While a SymConst is not a Confirm, it is non-null
479 * anyway. This helps to reduce the number of
480 * constructed Confirms.
482 if (is_SymConst_addr_ent(ptr))
485 } /* is_non_null_Confirm */
488 * The given pointer will be dereferenced, add non-null Confirms.
490 * @param ptr a node representing an address
491 * @param block the block of the dereferencing instruction
492 * @param env environment
494 static void insert_non_null(ir_node *ptr, ir_node *block, env_t *env)
496 const ir_edge_t *edge, *next;
499 foreach_out_edge_safe(ptr, edge, next) {
500 ir_node *succ = get_edge_src_irn(edge);
505 /* for now, we place a Confirm only in front of a Cmp */
509 pos = get_edge_src_pos(edge);
510 blk = get_effective_use_block(succ, pos);
512 if (block_dominates(block, blk)) {
514 * Ok, we found a usage of ptr in a block
515 * dominated by the Load/Store block.
516 * We can replace the input with a Confirm(ptr, !=, NULL).
519 ir_mode *mode = get_irn_mode(ptr);
520 c = new_Const(get_mode_null(mode));
522 c = new_r_Confirm(block, ptr, c, pn_Cmp_Lg);
525 set_irn_n(succ, pos, c);
526 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
528 env->num_non_null += 1;
529 env->num_confirms += 1;
532 } /* insert_non_null */
535 * Pre-walker: Called for every node to insert Confirm nodes
537 static void insert_Confirm(ir_node *node, void *env)
541 switch (get_irn_opcode(node)) {
543 insert_Confirm_in_block(node, env);
546 ptr = get_Load_ptr(node);
547 if (! is_non_null_Confirm(ptr))
548 insert_non_null(ptr, get_nodes_block(node), env);
551 ptr = get_Store_ptr(node);
552 if (! is_non_null_Confirm(ptr))
553 insert_non_null(ptr, get_nodes_block(node), env);
558 } /* insert_Confirm */
561 * Construct Confirm nodes
563 void construct_confirms(ir_graph *irg)
566 int edges_active = edges_activated(irg);
569 FIRM_DBG_REGISTER(dbg, "firm.ana.confirm");
571 remove_critical_cf_edges(irg);
573 /* we need dominance info */
576 assert(get_irg_pinned(irg) == op_pin_state_pinned &&
577 "Nodes must be placed to insert Confirms");
579 if (! edges_active) {
584 env.num_confirms = 0;
587 env.num_non_null = 0;
589 if (get_opt_global_null_ptr_elimination()) {
590 /* do global NULL test elimination */
591 irg_walk_graph(irg, insert_Confirm, NULL, &env);
593 /* now, visit all blocks and add Confirms where possible */
594 irg_block_walk_graph(irg, insert_Confirm_in_block, NULL, &env);
597 if (env.num_confirms | env.num_consts | env.num_eq) {
598 /* we have add nodes or changed DF edges */
599 set_irg_outs_inconsistent(irg);
601 /* the new nodes are not in the loop info */
602 set_irg_loopinfo_inconsistent(irg);
605 DB((dbg, LEVEL_1, "# Confirms inserted : %u\n", env.num_confirms));
606 DB((dbg, LEVEL_1, "# Const replacements: %u\n", env.num_consts));
607 DB((dbg, LEVEL_1, "# node equalities : %u\n", env.num_eq));
608 DB((dbg, LEVEL_1, "# non-null Confirms : %u\n", env.num_non_null));
610 /* deactivate edges if they where off */
612 edges_deactivate(irg);
613 } /* construct_confirms */
615 /* Construct a pass. */
616 ir_graph_pass_t *construct_confirms_pass(const char *name)
618 return def_graph_pass(name ? name : "confirm", construct_confirms);
619 } /* construct_confirms_pass */
623 * Post-walker: Remove Confirm nodes
625 static void rem_Confirm(ir_node *n, void *env)
629 ir_node *value = get_Confirm_value(n);
634 * Strange: a Confirm is its own bound. This can happen
635 * in dead blocks when Phi nodes are already removed.
637 exchange(n, new_Bad());
644 * Remove all Confirm nodes from a graph.
646 void remove_confirms(ir_graph *irg)
648 int rem = get_opt_remove_confirm();
650 set_opt_remove_confirm(1);
651 optimize_graph_df(irg);
652 set_opt_remove_confirm(rem);
653 } /* remove_confirms */
655 /* Construct a pass. */
656 ir_graph_pass_t *remove_confirms_pass(const char *name)
658 return def_graph_pass(name ? name : "rem_confirm", remove_confirms);
659 } /* remove_confirms_pass */