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 "irgraph_t.h"
33 #include "iropt_dbg.h"
34 #include "iredges_t.h"
46 typedef struct _env_t {
47 unsigned num_confirms; /**< Number of inserted Confirm nodes. */
48 unsigned num_consts; /**< Number of constants placed. */
49 unsigned num_eq; /**< Number of equalities placed. */
50 unsigned num_non_null; /**< Number of non-null Confirms. */
53 /** The debug handle. */
54 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
57 * Return the effective use block of a node and it's predecessor on
60 * @param node the node
61 * @param pos the position of the used input
63 * This handles correctly Phi nodes.
65 static ir_node *get_effective_use_block(ir_node *node, int pos) {
67 /* the effective use of a Phi argument is in its predecessor block */
68 node = get_nodes_block(node);
69 return get_Block_cfgpred_block(node, pos);
71 return get_nodes_block(node);
75 * Handle a CASE-branch.
77 * @param block the block which is entered by the branch
78 * @param irn the node expressing the switch value
79 * @param nr the branch label
80 * @param env statistical environment
82 * Branch labels are a simple case. We can replace the value
83 * by a Const with the branch label.
85 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
86 const ir_edge_t *edge, *next;
92 for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
93 ir_node *succ = get_edge_src_irn(edge);
94 int pos = get_edge_src_pos(edge);
95 ir_node *blk = get_effective_use_block(succ, pos);
97 next = get_irn_out_edge_next(irn, edge);
99 if (block_dominates(block, blk)) {
101 * Ok, we found a user of irn that is placed
102 * in a block dominated by the branch block.
103 * We can replace the input with the Constant
108 ir_mode *mode = get_irn_mode(irn);
109 ir_type *tp = get_irn_type(irn);
110 tarval *tv = new_tarval_from_long(nr, mode);
111 c = new_r_Const_type(current_ir_graph, tv, tp);
114 set_irn_n(succ, pos, c);
115 DBG_OPT_CONFIRM_C(irn, c);
116 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
118 env->num_consts += 1;
124 * Handle a mode_b input of Cond nodes.
126 * @param block the block which is entered by the branch
127 * @param selector the mode_b node expressing the branch condition
128 * @param pnc the true/false condition branch
129 * @param env statistical environment
131 static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env) {
132 ir_node *cond, *old, *cond_block = NULL, *other_blk = NULL, *con = NULL;
133 ir_node *c_b = NULL, *c_o = NULL;
134 const ir_edge_t *edge, *next;
136 for (edge = get_irn_out_edge_first(selector); edge; edge = next) {
137 ir_node *user = get_edge_src_irn(edge);
138 int pos = get_edge_src_pos(edge);
139 ir_node *user_blk = get_effective_use_block(user, pos);
141 next = get_irn_out_edge_next(selector, edge);
142 if (block_dominates(block, user_blk)) {
144 * Ok, we found a usage of selector in a block
145 * dominated by the branch block.
146 * We can replace the input with true/false.
149 con = new_Const(pnc == pn_Cond_true ? tarval_b_true : tarval_b_false);
151 old = get_irn_n(user, pos);
152 set_irn_n(user, pos, con);
153 DBG_OPT_CONFIRM_C(old, con);
155 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, user, con));
157 env->num_consts += 1;
161 /* get the other block */
162 if (other_blk == NULL) {
163 /* we have already tested, that block has only ONE Cond predecessor */
164 cond = get_Proj_pred(get_Block_cfgpred(block, 0));
165 cond_block = get_nodes_block(cond);
166 foreach_out_edge(cond, edge) {
167 ir_node *proj = get_edge_src_irn(edge);
168 if (get_Proj_proj(proj) == (long)pnc)
170 edge = get_irn_out_edge_first(proj);
171 other_blk = get_edge_src_irn(edge);
177 * Note the special case here: if block is a then, there might be no else
178 * block. In that case the other_block is the user_blk itself and pred_block
179 * is the cond_block ...
181 * Best would be to introduce a block here, removing this critical edge.
182 * For some reasons I cannot repair dominance here, so I have to remove
183 * ALL critical edges...
184 * FIXME: This should not be needed if we could repair dominance ...
188 n = get_Block_n_cfgpreds(user_blk);
191 * We have found a user in a non-dominated block:
192 * check, if all its block predecessors are dominated.
193 * If yes, place a Phi.
195 for (i = n - 1; i >= 0; --i) {
196 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
198 if (!block_dominates(block, pred_blk) &&
199 !block_dominates(other_blk, pred_blk)) {
200 /* can't do anything */
207 NEW_ARR_A(ir_node *, in, n);
208 /* ok, ALL predecessors are either dominated by block OR other block */
210 ir_node *c_true = new_Const(tarval_b_true);
211 ir_node *c_false = new_Const(tarval_b_false);
212 c_b = new_r_Confirm(current_ir_graph, cond_block, selector,
213 pnc == pn_Cond_true ? c_true : c_false, pn_Cmp_Eq);
214 c_o = new_r_Confirm(current_ir_graph, cond_block, selector,
215 pnc == pn_Cond_false ? c_true : c_false, pn_Cmp_Eq);
217 for (i = n - 1; i >= 0; --i) {
218 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
220 if (block_dominates(block, pred_blk))
225 phi = new_r_Phi(current_ir_graph, user_blk, n, in, mode_b);
226 set_irn_n(user, pos, phi);
233 * Handle an IF-branch.
235 * @param block the block which is entered by the branch
236 * @param cmp the Cmp node expressing the branch condition
237 * @param pnc the Compare relation for taking this branch
238 * @param env statistical environment
240 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
241 ir_node *left = get_Cmp_left(cmp);
242 ir_node *right = get_Cmp_right(cmp);
245 const ir_edge_t *edge, *next;
248 if (is_Bad(left) || is_Bad(right))
251 op = get_irn_op(left);
253 /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
254 These are removed anyway */
255 if (op == op_Const && is_Const(right))
258 /* try to place the constant on the right side for a Confirm */
259 if (op == op_Const || op == op_SymConst) {
265 pnc = get_inversed_pnc(pnc);
269 * First case: both values are identical.
270 * replace the left one by the right (potentially const) one.
272 if (pnc == pn_Cmp_Eq) {
273 cond_block = get_Block_cfgpred_block(block, 0);
274 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
275 ir_node *user = get_edge_src_irn(edge);
276 int pos = get_edge_src_pos(edge);
277 ir_node *blk = get_effective_use_block(user, pos);
279 next = get_irn_out_edge_next(left, edge);
280 if (block_dominates(block, blk)) {
282 * Ok, we found a usage of left in a block
283 * dominated by the branch block.
284 * We can replace the input with right.
286 set_irn_n(user, pos, right);
287 DBG_OPT_CONFIRM(left, right);
289 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, user, right));
292 } else if (block_dominates(blk, cond_block)) {
293 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
295 * left == Const and we found a movable user of left in a
296 * dominator of the Cond block
298 const ir_edge_t *edge, *next;
299 for (edge = get_irn_out_edge_first(user); edge; edge = next) {
300 ir_node *usr_of_usr = get_edge_src_irn(edge);
301 int npos = get_edge_src_pos(edge);
302 ir_node *blk = get_effective_use_block(usr_of_usr, npos);
304 next = get_irn_out_edge_next(user, edge);
305 if (block_dominates(block, blk)) {
307 * The user of the user is dominated by our true/false
308 * block. So, create a copy of user WITH the constant
309 * replacing it's pos'th input.
311 * This is always good for unop's and might be good
314 * If user has other user in the false/true block, code
315 * placement will move it down.
316 * If there are users in cond block or upper, we create
317 * "redundant ops", because one will have a const op,
318 * the other no const ...
320 ir_node *new_op = exact_copy(user);
321 set_nodes_block(new_op, block);
322 set_irn_n(new_op, pos, right);
323 set_irn_n(usr_of_usr, npos, new_op);
329 } else { /* not pn_Cmp_Eq cases */
332 foreach_out_edge_safe(left, edge, next) {
333 ir_node *succ = get_edge_src_irn(edge);
334 int pos = get_edge_src_pos(edge);
335 ir_node *blk = get_effective_use_block(succ, pos);
337 if (block_dominates(block, blk)) {
339 * Ok, we found a usage of left in a block
340 * dominated by the branch block.
341 * We can replace the input with a Confirm(left, pnc, right).
344 c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
346 pos = get_edge_src_pos(edge);
347 set_irn_n(succ, pos, c);
348 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
350 env->num_confirms += 1;
354 if (! is_Const(right)) {
355 /* also construct inverse Confirms */
358 pnc = get_inversed_pnc(pnc);
359 foreach_out_edge_safe(right, edge, next) {
360 ir_node *succ = get_edge_src_irn(edge);
367 pos = get_edge_src_pos(edge);
368 blk = get_effective_use_block(succ, pos);
370 if (block_dominates(block, blk)) {
372 * Ok, we found a usage of right in a block
373 * dominated by the branch block.
374 * We can replace the input with a Confirm(right, pnc^-1, left).
377 rc = new_r_Confirm(current_ir_graph, block, right, left, pnc);
379 pos = get_edge_src_pos(edge);
380 set_irn_n(succ, pos, rc);
381 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, rc));
383 env->num_confirms += 1;
391 * Pre-block-walker: Called for every block to insert Confirm nodes
393 static void insert_Confirm_in_block(ir_node *block, void *env) {
394 ir_node *cond, *proj, *selector;
398 * we can only handle blocks with only ONE control flow
401 if (get_Block_n_cfgpreds(block) != 1)
404 proj = get_Block_cfgpred(block, 0);
408 cond = get_Proj_pred(proj);
412 selector = get_Cond_selector(cond);
413 mode = get_irn_mode(selector);
415 if (mode == mode_b) {
419 handle_modeb(block, selector, get_Proj_proj(proj), env);
421 /* this should be an IF, check this */
422 if (! is_Proj(selector))
425 cmp = get_Proj_pred(selector);
429 pnc = get_Proj_proj(selector);
431 if (get_Proj_proj(proj) != pn_Cond_true) {
432 /* it's the false branch */
433 mode = get_irn_mode(get_Cmp_left(cmp));
434 pnc = get_negated_pnc(pnc, mode);
436 DB((dbg, LEVEL_2, "At %+F using %+F Confirm %=\n", block, cmp, pnc));
438 handle_if(block, cmp, pnc, env);
439 } else if (mode_is_int(mode)) {
440 long proj_nr = get_Proj_proj(proj);
442 /* this is a CASE, but we cannot handle the default case */
443 if (proj_nr == get_Cond_defaultProj(cond))
446 handle_case(block, get_Cond_selector(cond), proj_nr, env);
448 } /* insert_Confirm_in_block */
451 * Checks if a node is a non-null Confirm.
453 static int is_non_null_Confirm(const ir_node *ptr) {
455 if (! is_Confirm(ptr))
457 if (get_Confirm_cmp(ptr) == pn_Cmp_Lg) {
458 ir_node *bound = get_Confirm_bound(ptr);
460 if (is_Const(bound) && is_Const_null(bound))
463 ptr = get_Confirm_value(ptr);
466 * While a SymConst is not a Confirm, it is non-null
467 * anyway. This helps to reduce the number of
468 * constructed Confirms.
470 if (is_SymConst_addr_ent(ptr))
473 } /* is_non_null_Confirm */
476 * The given pointer will be dereferenced, add non-null Confirms.
478 * @param ptr a node representing an address
479 * @param block the block of the dereferencing instruction
480 * @param env environment
482 static void insert_non_null(ir_node *ptr, ir_node *block, env_t *env) {
483 const ir_edge_t *edge, *next;
486 foreach_out_edge_safe(ptr, edge, next) {
487 ir_node *succ = get_edge_src_irn(edge);
492 /* for now, we place a Confirm only in front of a Cmp */
496 pos = get_edge_src_pos(edge);
497 blk = get_effective_use_block(succ, pos);
499 if (block_dominates(block, blk)) {
501 * Ok, we found a usage of ptr in a block
502 * dominated by the Load/Store block.
503 * We can replace the input with a Confirm(ptr, !=, NULL).
506 ir_mode *mode = get_irn_mode(ptr);
507 c = new_Const(get_mode_null(mode));
509 c = new_r_Confirm(current_ir_graph, block, ptr, c, pn_Cmp_Lg);
512 set_irn_n(succ, pos, c);
513 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
515 env->num_non_null += 1;
516 env->num_confirms += 1;
519 } /* insert_non_null */
522 * Pre-walker: Called for every node to insert Confirm nodes
524 static void insert_Confirm(ir_node *node, void *env) {
527 switch (get_irn_opcode(node)) {
529 insert_Confirm_in_block(node, env);
532 ptr = get_Load_ptr(node);
533 if (! is_non_null_Confirm(ptr))
534 insert_non_null(ptr, get_nodes_block(node), env);
537 ptr = get_Store_ptr(node);
538 if (! is_non_null_Confirm(ptr))
539 insert_non_null(ptr, get_nodes_block(node), env);
544 } /* insert_Confirm */
547 * Construct Confirm nodes
549 void construct_confirms(ir_graph *irg) {
551 int edges_active = edges_activated(irg);
554 FIRM_DBG_REGISTER(dbg, "firm.ana.confirm");
556 remove_critical_cf_edges(irg);
558 /* we need dominance info */
561 assert(get_irg_pinned(irg) == op_pin_state_pinned &&
562 "Nodes must be placed to insert Confirms");
564 if (! edges_active) {
569 env.num_confirms = 0;
572 env.num_non_null = 0;
574 if (get_opt_global_null_ptr_elimination()) {
575 /* do global NULL test elimination */
576 irg_walk_graph(irg, insert_Confirm, NULL, &env);
578 /* now, visit all blocks and add Confirms where possible */
579 irg_block_walk_graph(irg, insert_Confirm_in_block, NULL, &env);
582 if (env.num_confirms | env.num_consts | env.num_eq) {
583 /* we have add nodes or changed DF edges */
584 set_irg_outs_inconsistent(irg);
586 /* the new nodes are not in the loop info */
587 set_irg_loopinfo_inconsistent(irg);
590 DB((dbg, LEVEL_1, "# Confirms inserted : %u\n", env.num_confirms));
591 DB((dbg, LEVEL_1, "# Const replacements: %u\n", env.num_consts));
592 DB((dbg, LEVEL_1, "# node equalities : %u\n", env.num_eq));
593 DB((dbg, LEVEL_1, "# non-null Confirms : %u\n", env.num_non_null));
595 /* deactivate edges if they where off */
597 edges_deactivate(irg);
598 } /* construct_confirms */
602 * Post-walker: Remove Confirm nodes
604 static void rem_Confirm(ir_node *n, void *env) {
607 ir_node *value = get_Confirm_value(n);
612 * Strange: a Confirm is its own bound. This can happen
613 * in dead blocks when Phi nodes are already removed.
615 exchange(n, new_Bad());
622 * Remove all Confirm nodes from a graph.
624 void remove_confirms(ir_graph *irg) {
625 int rem = get_opt_remove_confirm();
627 set_opt_remove_confirm(1);
628 optimize_graph_df(irg);
629 set_opt_remove_confirm(rem);
630 } /* remove_confirms */