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
31 #include "irgraph_t.h"
35 #include "iropt_dbg.h"
36 #include "iredges_t.h"
45 typedef struct _env_t {
46 unsigned num_confirms; /**< number of inserted Confirm nodes */
47 unsigned num_consts; /**< number of constants placed */
48 unsigned num_eq; /**< number of equalities placed */
52 * Return the effective use block of a node and it's predecessor on
55 * @param node the node
56 * @param pos the position of the used input
58 * This handles correctly Phi nodes.
60 static ir_node *get_effective_use_block(ir_node *node, int pos) {
62 /* the effective use of a Phi argument is in its predecessor block */
63 node = get_nodes_block(node);
64 return get_Block_cfgpred_block(node, pos);
66 return get_nodes_block(node);
70 * Handle a CASE-branch.
72 * @param block the block which is entered by the branch
73 * @param irn the node expressing the switch value
74 * @param nr the branch label
75 * @param env statistical environment
77 * Branch labels are a simple case. We can replace the value
78 * by a Const with the branch label.
80 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
81 const ir_edge_t *edge, *next;
87 for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
88 ir_node *succ = get_edge_src_irn(edge);
89 int pos = get_edge_src_pos(edge);
90 ir_node *blk = get_effective_use_block(succ, pos);
92 next = get_irn_out_edge_next(irn, edge);
94 if (block_dominates(block, blk)) {
96 * Ok, we found a user of irn that is placed
97 * in a block dominated by the branch block.
98 * We can replace the input with the Constant
103 ir_mode *mode = get_irn_mode(irn);
104 ir_type *tp = get_irn_type(irn);
105 tarval *tv = new_tarval_from_long(nr, mode);
106 c = new_r_Const_type(current_ir_graph, block, mode, tv, tp);
109 set_irn_n(succ, pos, c);
110 DBG_OPT_CONFIRM_C(irn, c);
111 // ir_printf("1 Replacing input %d of node %n with %n\n", pos, succ, c);
113 env->num_consts += 1;
119 * Handle a mode_b input of Cond nodes.
121 * @param block the block which is entered by the branch
122 * @param selector the mode_b node expressing the branch condition
123 * @param pnc the true/false condition branch
124 * @param env statistical environment
126 static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env) {
127 ir_node *cond, *old, *cond_block, *other_blk = NULL, *con = NULL;
128 ir_node *c_b = NULL, *c_o = NULL;
129 const ir_edge_t *edge, *next;
131 for (edge = get_irn_out_edge_first(selector); edge; edge = next) {
132 ir_node *user = get_edge_src_irn(edge);
133 int pos = get_edge_src_pos(edge);
134 ir_node *user_blk = get_effective_use_block(user, pos);
136 next = get_irn_out_edge_next(selector, edge);
137 if (block_dominates(block, user_blk)) {
139 * Ok, we found a usage of selector in a block
140 * dominated by the branch block.
141 * We can replace the input with true/false.
144 con = new_Const(mode_b, pnc == pn_Cond_true ? tarval_b_true : tarval_b_false);
146 old = get_irn_n(user, pos);
147 set_irn_n(user, pos, con);
148 DBG_OPT_CONFIRM_C(old, con);
150 // ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, con);
152 env->num_consts += 1;
156 /* get the other block */
157 if (other_blk == NULL) {
158 /* we have already tested, that block has only ONE Cond predecessor */
159 cond = get_Proj_pred(get_Block_cfgpred(block, 0));
160 cond_block = get_nodes_block(cond);
161 foreach_out_edge(cond, edge) {
162 ir_node *proj = get_edge_src_irn(edge);
163 if (get_Proj_proj(proj) == (long)pnc)
165 edge = get_irn_out_edge_first(proj);
166 other_blk = get_edge_src_irn(edge);
172 * Note the special case here: if block is a then, there might be no else
173 * block. In that case the other_block is the user_blk itself and pred_block
174 * is the cond_block ...
176 * Best would be to introduce a block here, removing this critical edge.
177 * For some reasons I cannot repair dominance here, so I have to remove
178 * ALL critical edges...
179 * FIXME: This should not be needed if we could repair dominance ...
183 n = get_Block_n_cfgpreds(user_blk);
186 * We have found a user in a non-dominated block:
187 * check, if all its block predecessors are dominated.
188 * If yes, place a Phi.
190 for (i = n - 1; i >= 0; --i) {
191 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
193 if (!block_dominates(block, pred_blk) &&
194 !block_dominates(other_blk, pred_blk)) {
195 /* can't do anything */
202 NEW_ARR_A(ir_node *, in, n);
203 /* ok, ALL predecessors are either dominated by block OR other block */
205 ir_node *c_true = new_Const(mode_b, tarval_b_true);
206 ir_node *c_false = new_Const(mode_b, tarval_b_false);
207 c_b = new_r_Confirm(current_ir_graph, cond_block, selector,
208 pnc == pn_Cond_true ? c_true : c_false, pn_Cmp_Eq);
209 c_o = new_r_Confirm(current_ir_graph, cond_block, selector,
210 pnc == pn_Cond_false ? c_true : c_false, pn_Cmp_Eq);
212 for (i = n - 1; i >= 0; --i) {
213 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
215 if (block_dominates(block, pred_blk))
220 phi = new_r_Phi(current_ir_graph, user_blk, n, in, mode_b);
221 set_irn_n(user, pos, phi);
228 * Handle an IF-branch.
230 * @param block the block which is entered by the branch
231 * @param cmp the Cmp node expressing the branch condition
232 * @param pnc the Compare relation for taking this branch
233 * @param env statistical environment
235 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
236 ir_node *left = get_Cmp_left(cmp);
237 ir_node *right = get_Cmp_right(cmp);
240 const ir_edge_t *edge, *next;
243 if (is_Bad(left) || is_Bad(right))
246 op = get_irn_op(left);
248 /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
249 These are removed anyway */
250 if (op == op_Const && is_Const(right))
253 /* try to place the constant on the right side for a Confirm */
254 if (op == op_Const || op == op_SymConst) {
260 pnc = get_inversed_pnc(pnc);
264 * First case: both values are identical.
265 * replace the left one by the right (potentially const) one.
267 if (pnc == pn_Cmp_Eq) {
268 cond_block = get_Block_cfgpred_block(block, 0);
269 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
270 ir_node *user = get_edge_src_irn(edge);
271 int pos = get_edge_src_pos(edge);
272 ir_node *blk = get_effective_use_block(user, pos);
274 next = get_irn_out_edge_next(left, edge);
275 if (block_dominates(block, blk)) {
277 * Ok, we found a usage of left in a block
278 * dominated by the branch block.
279 * We can replace the input with right.
281 set_irn_n(user, pos, right);
282 DBG_OPT_CONFIRM(left, right);
284 // ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, right);
287 } else if (block_dominates(blk, cond_block)) {
288 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
290 * left == Const and we found a movable user of left in a
291 * dominator of the Cond block
293 const ir_edge_t *edge, *next;
294 for (edge = get_irn_out_edge_first(user); edge; edge = next) {
295 ir_node *usr_of_usr = get_edge_src_irn(edge);
296 int npos = get_edge_src_pos(edge);
297 ir_node *blk = get_effective_use_block(usr_of_usr, npos);
299 next = get_irn_out_edge_next(user, edge);
300 if (block_dominates(block, blk)) {
302 * The user of the user is dominated by our true/false
303 * block. So, create a copy of user WITH the constant
304 * replacing it's pos'th input.
306 * This is always good for unop's and might be good
309 * If user has other user in the false/true block, code
310 * placement will move it down.
311 * If there are users in cond block or upper, we create
312 * "redundant ops", because one will have a const op,
313 * the other no const ...
315 ir_node *new_op = exact_copy(user);
316 set_nodes_block(new_op, block);
317 set_irn_n(new_op, pos, right);
318 set_irn_n(usr_of_usr, npos, new_op);
324 } else { /* not pn_Cmp_Eq cases */
327 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
328 ir_node *succ = get_edge_src_irn(edge);
329 int pos = get_edge_src_pos(edge);
330 ir_node *blk = get_effective_use_block(succ, pos);
332 next = get_irn_out_edge_next(left, edge);
333 if (block_dominates(block, blk)) {
335 * Ok, we found a usage of left in a block
336 * dominated by the branch block.
337 * We can replace the input with a Confirm(left, pnc, right).
340 c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
342 pos = get_edge_src_pos(edge);
343 set_irn_n(succ, pos, c);
344 // ir_printf("3 Replacing input %d of node %n with %n\n", pos, user, c);
346 env->num_confirms += 1;
353 * Pre-walker: Called for every block to insert Confirm nodes
355 static void insert_Confirm(ir_node *block, void *env) {
356 ir_node *cond, *proj, *selector;
360 * we can only handle blocks with only ONE control flow
363 if (get_Block_n_cfgpreds(block) != 1)
366 proj = get_Block_cfgpred(block, 0);
370 cond = get_Proj_pred(proj);
374 selector = get_Cond_selector(cond);
375 mode = get_irn_mode(selector);
377 if (mode == mode_b) {
381 handle_modeb(block, selector, get_Proj_proj(proj), env);
383 /* this should be an IF, check this */
384 if (! is_Proj(selector))
387 cmp = get_Proj_pred(selector);
391 pnc = get_Proj_proj(selector);
393 if (get_Proj_proj(proj) != pn_Cond_true) {
394 /* it's the false branch */
395 pnc = get_negated_pnc(pnc, mode);
397 // ir_printf("At %n using %n Confirm %=\n", block, cmp, pnc);
399 handle_if(block, cmp, pnc, env);
400 } else if (mode_is_int(mode)) {
401 long proj_nr = get_Proj_proj(proj);
403 /* this is a CASE, but we cannot handle the default case */
404 if (proj_nr == get_Cond_defaultProj(cond))
407 handle_case(block, get_Cond_selector(cond), proj_nr, env);
409 } /* insert_Confirm */
412 * Construct Confirm nodes
414 void construct_confirms(ir_graph *irg) {
416 int edges_active = edges_activated(irg);
418 remove_critical_cf_edges(irg);
420 /* we need dominance info */
423 assert(get_irg_pinned(irg) == op_pin_state_pinned &&
424 "Nodes must be placed to insert Confirms");
426 if (! edges_active) {
431 env.num_confirms = 0;
435 /* now, visit all blocks and add Confirms where possible */
436 irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
438 if (env.num_confirms | env.num_consts | env.num_eq) {
439 /* we have add nodes or changed DF edges */
440 set_irg_outs_inconsistent(irg);
442 /* the new nodes are not in the loop info */
443 set_irg_loopinfo_inconsistent(irg);
447 printf("# Confirms inserted : %u\n", env.num_confirms);
448 printf("# Const replacements: %u\n", env.num_consts);
449 printf("# node equalities : %u\n", env.num_eq);
452 /* deactivate edges if they where off */
454 edges_deactivate(irg);
455 } /* construct_confirms */
458 * Post-walker: Remove Confirm nodes
460 static void rem_Confirm(ir_node *n, void *env) {
462 if (get_irn_op(n) == op_Confirm) {
463 ir_node *value = get_Confirm_value(n);
468 * Strange: a Confirm is it's own bound. This can happen
469 * in dead blocks when Phi nodes are already removed.
471 exchange(n, new_Bad());
477 * Remove all Confirm nodes from a graph.
479 void remove_confirms(ir_graph *irg) {
480 set_opt_remove_confirm(1);
481 optimize_graph_df(irg);
482 } /* remove_confirms */