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 is in its predecessor block */
63 node = get_irn_n(node, pos);
65 return get_nodes_block(node);
69 * Handle a CASE-branch.
71 * @param block the block which is entered by the branch
72 * @param irn the node expressing the switch value
73 * @param nr the branch label
74 * @param env statistical environment
76 * Branch labels are a simple case. We can replace the value
77 * by a Const with the branch label.
79 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
80 const ir_edge_t *edge, *next;
86 for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
87 ir_node *succ = get_edge_src_irn(edge);
88 int pos = get_edge_src_pos(edge);
89 ir_node *blk = get_effective_use_block(succ, pos);
91 next = get_irn_out_edge_next(irn, edge);
93 if (block_dominates(block, blk)) {
95 * Ok, we found a user of irn that is placed
96 * in a block dominated by the branch block.
97 * We can replace the input with the Constant
102 ir_mode *mode = get_irn_mode(irn);
103 ir_type *tp = get_irn_type(irn);
104 tarval *tv = new_tarval_from_long(nr, mode);
105 c = new_r_Const_type(current_ir_graph, block, mode, tv, tp);
108 set_irn_n(succ, pos, c);
109 DBG_OPT_CONFIRM_C(irn, c);
110 // ir_printf("1 Replacing input %d of node %n with %n\n", pos, succ, c);
112 env->num_consts += 1;
118 * Handle a mode_b input of Cond nodes.
120 * @param block the block which is entered by the branch
121 * @param selector the mode_b node expressing the branch condition
122 * @param pnc the true/false condition branch
123 * @param env statistical environment
125 static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env) {
126 ir_node *cond, *old, *cond_block, *other_blk = NULL, *con = NULL;
127 ir_node *c_b = NULL, *c_o = NULL;
128 const ir_edge_t *edge, *next;
130 for (edge = get_irn_out_edge_first(selector); edge; edge = next) {
131 ir_node *user = get_edge_src_irn(edge);
132 int pos = get_edge_src_pos(edge);
133 ir_node *user_blk = get_effective_use_block(user, pos);
135 next = get_irn_out_edge_next(selector, edge);
136 if (block_dominates(block, user_blk)) {
138 * Ok, we found a usage of selector in a block
139 * dominated by the branch block.
140 * We can replace the input with true/false.
143 con = new_Const(mode_b, pnc == pn_Cond_true ? tarval_b_true : tarval_b_false);
145 old = get_irn_n(user, pos);
146 set_irn_n(user, pos, con);
147 DBG_OPT_CONFIRM_C(old, con);
149 // ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, con);
151 env->num_consts += 1;
155 /* get the other block */
156 if (other_blk == NULL) {
157 /* we have already tested, that block has only ONE Cond predecessor */
158 cond = get_Proj_pred(get_Block_cfgpred(block, 0));
159 cond_block = get_nodes_block(cond);
160 foreach_out_edge(cond, edge) {
161 ir_node *proj = get_edge_src_irn(edge);
162 if (get_Proj_proj(proj) == (long)pnc)
164 edge = get_irn_out_edge_first(proj);
165 other_blk = get_edge_src_irn(edge);
171 * Note the special case here: if block is a then, there might be no else
172 * block. In that case the other_block is the user_blk itself and pred_block
173 * is the cond_block ...
175 * Best would be to indroduce a block here, removing this critical edge.
176 * For some reasons I cannot repair dominance here, so I have to remove
177 * ALL critical edges...
178 * FIXME: This should not be needed if we could repair dominance ...
182 n = get_Block_n_cfgpreds(user_blk);
185 * We have found a user in a non-dominated block:
186 * check, if all its block predecessors are dominated.
187 * If yes, place a Phi.
189 for (i = n - 1; i >= 0; --i) {
190 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
192 if (!block_dominates(block, pred_blk) &&
193 !block_dominates(other_blk, pred_blk)) {
194 /* can't do anything */
201 NEW_ARR_A(ir_node *, in, n);
202 /* ok, ALL predecessors are either dominated by block OR other block */
204 ir_node *c_true = new_Const(mode_b, tarval_b_true);
205 ir_node *c_false = new_Const(mode_b, tarval_b_false);
206 c_b = new_r_Confirm(current_ir_graph, cond_block, selector,
207 pnc == pn_Cond_true ? c_true : c_false, pn_Cmp_Eq);
208 c_o = new_r_Confirm(current_ir_graph, cond_block, selector,
209 pnc == pn_Cond_false ? c_true : c_false, pn_Cmp_Eq);
211 for (i = n - 1; i >= 0; --i) {
212 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
214 if (block_dominates(block, pred_blk))
219 phi = new_r_Phi(current_ir_graph, user_blk, n, in, mode_b);
220 set_irn_n(user, pos, phi);
227 * Handle an IF-branch.
229 * @param block the block which is entered by the branch
230 * @param cmp the Cmp node expressing the branch condition
231 * @param pnc the Compare relation for taking this branch
232 * @param env statistical environment
234 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
235 ir_node *left = get_Cmp_left(cmp);
236 ir_node *right = get_Cmp_right(cmp);
239 const ir_edge_t *edge, *next;
242 if (is_Bad(left) || is_Bad(right))
245 op = get_irn_op(left);
247 /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
248 These are removed anyway */
249 if (op == op_Const && is_Const(right))
252 /* try to place the constant on the right side for a Confirm */
253 if (op == op_Const || op == op_SymConst) {
259 pnc = get_inversed_pnc(pnc);
263 * First case: both values are identical.
264 * replace the left one by the right (potentially const) one.
266 if (pnc == pn_Cmp_Eq) {
267 cond_block = get_Block_cfgpred_block(block, 0);
268 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
269 ir_node *user = get_edge_src_irn(edge);
270 int pos = get_edge_src_pos(edge);
271 ir_node *blk = get_effective_use_block(user, pos);
273 next = get_irn_out_edge_next(left, edge);
274 if (block_dominates(block, blk)) {
276 * Ok, we found a usage of left in a block
277 * dominated by the branch block.
278 * We can replace the input with right.
280 set_irn_n(user, pos, right);
281 DBG_OPT_CONFIRM(left, right);
283 // ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, right);
286 } else if (block_dominates(blk, cond_block)) {
287 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
289 * left == Const and we found a movable user of left in a
290 * dominator of the Cond block
292 const ir_edge_t *edge, *next;
293 for (edge = get_irn_out_edge_first(user); edge; edge = next) {
294 ir_node *usr_of_usr = get_edge_src_irn(edge);
295 int npos = get_edge_src_pos(edge);
296 ir_node *blk = get_effective_use_block(usr_of_usr, npos);
298 next = get_irn_out_edge_next(user, edge);
299 if (block_dominates(block, blk)) {
301 * The user of the user is dominated by our true/false
302 * block. So, create a copy of user WITH the constant
303 * replacing it's pos'th input.
305 * This is always good for unop's and might be good
308 * If user has other user in the false/true block, code
309 * placement will move it down.
310 * If there are users in cond block or upper, we create
311 * "redundant ops", because one will have a const op,
312 * the other no const ...
314 ir_node *new_op = exact_copy(user);
315 set_nodes_block(new_op, block);
316 set_irn_n(new_op, pos, right);
317 set_irn_n(usr_of_usr, npos, new_op);
323 } else { /* not pn_Cmp_Eq cases */
326 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
327 ir_node *succ = get_edge_src_irn(edge);
328 int pos = get_edge_src_pos(edge);
329 ir_node *blk = get_effective_use_block(succ, pos);
331 next = get_irn_out_edge_next(left, edge);
332 if (block_dominates(block, blk)) {
334 * Ok, we found a usage of left in a block
335 * dominated by the branch block.
336 * We can replace the input with a Confirm(left, pnc, right).
339 c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
341 pos = get_edge_src_pos(edge);
342 set_irn_n(succ, pos, c);
343 // ir_printf("3 Replacing input %d of node %n with %n\n", pos, user, c);
345 env->num_confirms += 1;
352 * Pre-walker: Called for every block to insert Confirm nodes
354 static void insert_Confirm(ir_node *block, void *env) {
355 ir_node *cond, *proj, *selector;
359 * we can only handle blocks with only ONE control flow
362 if (get_Block_n_cfgpreds(block) != 1)
365 proj = get_Block_cfgpred(block, 0);
369 cond = get_Proj_pred(proj);
373 selector = get_Cond_selector(cond);
374 mode = get_irn_mode(selector);
376 if (mode == mode_b) {
380 handle_modeb(block, selector, get_Proj_proj(proj), env);
382 /* this should be an IF, check this */
383 if (get_irn_op(selector) != op_Proj)
386 cmp = get_Proj_pred(selector);
387 if (get_irn_op(cmp) != op_Cmp)
390 pnc = get_Proj_proj(selector);
392 if (get_Proj_proj(proj) != pn_Cond_true) {
393 /* it's the false branch */
394 pnc = get_negated_pnc(pnc, mode);
396 // ir_printf("At %n using %n Confirm %=\n", block, cmp, pnc);
398 handle_if(block, cmp, pnc, env);
399 } else if (mode_is_int(mode)) {
400 long proj_nr = get_Proj_proj(proj);
402 /* this is a CASE, but we cannot handle the default case */
403 if (proj_nr == get_Cond_defaultProj(cond))
406 handle_case(block, get_Cond_selector(cond), proj_nr, env);
408 } /* insert_Confirm */
411 * Construct Confirm nodes
413 void construct_confirms(ir_graph *irg) {
415 int edges_active = edges_activated(irg);
417 remove_critical_cf_edges(irg);
419 /* we need dominance info */
422 assert(get_irg_pinned(irg) == op_pin_state_pinned &&
423 "Nodes must be placed to insert Confirms");
425 if (! edges_active) {
430 env.num_confirms = 0;
434 /* now, visit all blocks and add Confirms where possible */
435 irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
437 if (env.num_confirms | env.num_consts | env.num_eq) {
438 /* we have add nodes or changed DF edges */
439 set_irg_outs_inconsistent(irg);
441 /* the new nodes are not in the loop info */
442 set_irg_loopinfo_inconsistent(irg);
446 printf("# Confirms inserted : %u\n", env.num_confirms);
447 printf("# Const replacements: %u\n", env.num_consts);
448 printf("# node equalities : %u\n", env.num_eq);
451 /* deactivate edges if they where off */
453 edges_deactivate(irg);
454 } /* construct_confirms */
457 * Post-walker: Remove Confirm nodes
459 static void rem_Confirm(ir_node *n, void *env) {
461 if (get_irn_op(n) == op_Confirm) {
462 ir_node *value = get_Confirm_value(n);
467 * Strange: a Confirm is it's own bound. This can happen
468 * in dead blocks when Phi nodes are already removed.
470 exchange(n, new_Bad());
476 * Remove all Confirm nodes from a graph.
478 void remove_confirms(ir_graph *irg) {
479 set_opt_remove_confirm(1);
480 optimize_graph_df(irg);
481 } /* remove_confirms */