3 * File name: ir/ana/irconsconfirm.c
4 * Purpose: Construction of Confirm nodes
9 * Copyright: (C) 2002-2005 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 * @file irconsconfirm.c
16 * Construction of Confirm nodes
18 * @author Michael Beck
20 #include "irgraph_t.h"
24 #include "iropt_dbg.h"
25 #include "iredges_t.h"
32 typedef struct _env_t {
33 unsigned num_confirms; /**< number of inserted Confirm nodes */
34 unsigned num_consts; /**< number of constants placed */
35 unsigned num_eq; /**< number of equalities placed */
39 * Handle a CASE-branch.
41 * @param block the block which is entered by the branch
42 * @param irn the node expressing the switch value
43 * @param nr the branch label
44 * @param env statistical environment
46 * Branch labels are a simple case. We can replace the value
47 * by a Const with the branch label.
49 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env)
52 const ir_edge_t *edge, *next;
58 for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
59 ir_node *succ = get_edge_src_irn(edge);
60 ir_node *blk = get_nodes_block(succ);
62 next = get_irn_out_edge_next(irn, edge);
64 if (block_dominates(block, blk)) {
66 * Ok, we found a user of irn that is placed
67 * in a block dominated by the branch block.
68 * We can replace the input with the Constant
73 ir_mode *mode = get_irn_mode(irn);
74 ir_type *tp = get_irn_type(irn);
75 tarval *tv = new_tarval_from_long(nr, mode);
76 c = new_r_Const_type(current_ir_graph, block, mode, tv, tp);
79 pos = get_edge_src_pos(edge);
80 set_irn_n(succ, pos, c);
81 DBG_OPT_CONFIRM_C(irn, c);
82 // ir_printf("1 Replacing input %d of node %n with %n\n", pos, succ, c);
90 * Handle an IF-branch.
92 * @param block the block which is entered by the branch
93 * @param cmp the Cmp node expressing the branch condition
94 * @param pnc the Compare relation for taking this branch
95 * @param env statistical environment
97 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env)
99 ir_node *left = get_Cmp_left(cmp);
100 ir_node *right = get_Cmp_right(cmp);
102 const ir_edge_t *edge, *next;
105 if (is_Bad(left) ||is_Bad(right))
108 op = get_irn_op(left);
110 /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
111 These are removed anyway */
112 if (op == op_Const && is_Const(right))
115 /* try to place the constant on the right side for a Confirm */
116 if (op == op_Const || op == op_SymConst) {
122 pnc = get_inversed_pnc(pnc);
126 * First case: both values are identical.
127 * replace the right one by the left one.
129 if (pnc == pn_Cmp_Eq) {
132 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
133 ir_node *succ = get_edge_src_irn(edge);
134 ir_node *blk = get_nodes_block(succ);
136 next = get_irn_out_edge_next(left, edge);
137 if (block_dominates(block, blk)) {
139 * Ok, we found a user of right that is placed
140 * in a block dominated by the branch block.
141 * We can replace the input with right.
143 pos = get_edge_src_pos(edge);
144 set_irn_n(succ, pos, right);
145 DBG_OPT_CONFIRM(left, right);
147 // ir_printf("2 Replacing input %d of node %n with %n\n", pos, succ, right);
153 else { /* not == cases */
157 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
158 ir_node *succ = get_edge_src_irn(edge);
159 ir_node *blk = get_nodes_block(succ);
161 next = get_irn_out_edge_next(left, edge);
162 if (block_dominates(block, blk)) {
164 * Ok, we found a user of right that is placed
165 * in a block dominated by the branch block.
166 * We can replace the input with a Confirm(left, pnc, right).
169 c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
171 pos = get_edge_src_pos(edge);
172 set_irn_n(succ, pos, c);
173 // ir_printf("3 Replacing input %d of node %n with %n\n", pos, succ, c);
175 env->num_confirms += 1;
182 * Pre-walker: Called for every block to insert Confirm nodes
184 static void insert_Confirm(ir_node *block, void *env)
186 ir_node *cond, *proj, *selector;
190 * we can only handle blocks with only ONE control flow
193 if (get_Block_n_cfgpreds(block) != 1)
196 proj = get_Block_cfgpred(block, 0);
197 if (get_irn_op(proj) != op_Proj)
200 cond = get_Proj_pred(proj);
201 if (get_irn_op(cond) != op_Cond)
204 selector = get_Cond_selector(cond);
205 mode = get_irn_mode(selector);
207 if (mode == mode_b) {
211 /* this should be an IF, check this */
212 if (get_irn_op(selector) != op_Proj)
215 cmp = get_Proj_pred(selector);
216 if (get_irn_op(cmp) != op_Cmp)
219 pnc = get_Proj_proj(selector);
221 if (get_Proj_proj(proj) != pn_Cond_true) {
222 /* it's the false branch */
223 pnc = get_negated_pnc(pnc, mode);
225 // ir_printf("At %n using %n Confirm %=\n", block, cmp, pnc);
227 handle_if(block, cmp, pnc, env);
229 else if (mode_is_int(mode)) {
230 long proj_nr = get_Proj_proj(proj);
232 /* this is a CASE, but we cannot handle the default case */
233 if (proj_nr == get_Cond_defaultProj(cond))
236 handle_case(block, get_Cond_selector(cond), proj_nr, env);
241 * Construct Confirm nodes
243 void construct_confirms(ir_graph *irg)
246 int edges_active = edges_activated(irg);
248 /* we need dominance info */
251 assert(get_irg_pinned(irg) == op_pin_state_pinned &&
252 "Nodes must be placed to insert Confirms");
254 if (! edges_active) {
259 env.num_confirms = 0;
263 /* now, visit all blocks and add Confirms where possible */
264 irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
266 if (env.num_confirms | env.num_consts | env.num_eq) {
267 /* we have add nodes or changed DF edges */
268 set_irg_outs_inconsistent(irg);
270 /* the new nodes are not in the loop info */
271 set_irg_loopinfo_inconsistent(irg);
275 printf("# Confirms inserted : %u\n", env.num_confirms);
276 printf("# Const replacements: %u\n", env.num_consts);
277 printf("# node equalities : %u\n", env.num_eq);
280 /* deactivate edges if they where off */
282 edges_deactivate(irg);
286 * Post-walker: Remove Confirm nodes
288 static void rem_Confirm(ir_node *n, void *env) {
289 if (get_irn_op(n) == op_Confirm) {
290 ir_node *value = get_Confirm_value(n);
295 * Strange: a Confirm is it's own bound. This can happen
296 * in dead blocks when Phi nodes are already removed.
298 exchange(n, new_Bad());
304 * Remove all Confirm nodes from a graph.
306 void remove_confirms(ir_graph *irg) {
307 irg_walk_graph(irg, NULL, rem_Confirm, NULL);