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"
30 typedef struct _env_t {
31 unsigned num_confirms; /**< number of inserted Confirm nodes */
32 unsigned num_consts; /**< number of constants placed */
33 unsigned num_eq; /**< number of equalities placed */
37 * Handle a CASE-branch.
39 * @param block the block which is entered by the branch
40 * @param irn the node expressing the switch value
41 * @param pnc the branch label
42 * @param env statistical environment
44 * Branch labels are a simple case. We can replace the value
45 * by a Const with the branch label.
47 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env)
50 const ir_edge_t *edge, *next;
53 for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
54 ir_node *succ = get_edge_src_irn(edge);
55 ir_node *blk = get_nodes_block(succ);
57 next = get_irn_out_edge_next(irn, edge);
59 if (block_dominates(block, blk)) {
61 * Ok, we found a user of irn that is placed
62 * in a block dominated by the branch block.
63 * We can replace the input with the Constant
68 ir_mode *mode = get_irn_mode(irn);
69 type *tp = get_irn_type(irn);
70 tarval *tv = new_tarval_from_long(nr, mode);
71 c = new_r_Const_type(current_ir_graph, block, mode, tv, tp);
74 pos = get_edge_src_pos(edge);
75 set_irn_n(succ, pos, c);
76 DBG_OPT_CONFIRM_C(irn, c);
84 * Handle an IF-branch.
86 * @param block the block which is entered by the branch
87 * @param cmp the Cmp node expressing the branch condition
88 * @param pnc the Compare relation for taking this branch
89 * @param env statistical environment
91 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env)
93 ir_node *left = get_Cmp_left(cmp);
94 ir_node *right = get_Cmp_right(cmp);
95 const ir_edge_t *edge, *next;
97 /* remove unordered if it's an integer compare */
98 if (mode_is_int(get_irn_mode(left)))
101 /* try to place the constant on the right side */
102 if (get_irn_op(left) == op_Const) {
108 pnc = get_inversed_pnc(pnc);
112 * First case: both values are identical.
113 * replace the left one by the right one.
115 if (pnc == pn_Cmp_Eq) {
118 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
119 ir_node *succ = get_edge_src_irn(edge);
120 ir_node *blk = get_nodes_block(succ);
122 next = get_irn_out_edge_next(left, edge);
123 if (block_dominates(block, blk)) {
125 * Ok, we found a user of left that is placed
126 * in a block dominated by the branch block.
127 * We can replace the input with right.
129 pos = get_edge_src_pos(edge);
130 set_irn_n(succ, pos, right);
131 DBG_OPT_CONFIRM(left, right);
137 else { /* not == cases */
141 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
142 ir_node *succ = get_edge_src_irn(edge);
143 ir_node *blk = get_nodes_block(succ);
145 next = get_irn_out_edge_next(left, edge);
146 if (block_dominates(block, blk)) {
148 * Ok, we found a user of left that is placed
149 * in a block dominated by the branch block.
150 * We can replace the input with a Confirm(left, pnc, right).
154 c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
156 pos = get_edge_src_pos(edge);
157 set_irn_n(succ, pos, c);
159 env->num_confirms += 1;
166 * Pre-walker: Called for every block to insert Confirm nodes
168 static void insert_Confirm(ir_node *block, void *env)
170 ir_node *cond, *proj, *selector;
174 * we can only handle blocks with only ONE control flow
177 if (get_Block_n_cfgpreds(block) != 1)
180 proj = get_Block_cfgpred(block, 0);
181 if (get_irn_op(proj) != op_Proj)
184 cond = get_Proj_pred(proj);
185 if (get_irn_op(cond) != op_Cond)
188 selector = get_Cond_selector(cond);
189 mode = get_irn_mode(selector);
191 if (mode == mode_b) {
195 /* this should be an IF, check this */
196 if (get_irn_op(selector) != op_Proj)
199 cmp = get_Proj_pred(selector);
200 if (get_irn_op(cmp) != op_Cmp)
203 pnc = get_Proj_proj(selector);
205 if (get_Proj_proj(proj) != pn_Cond_true) {
206 /* it's the false branch */
207 pnc = get_negated_pnc(pnc);
209 handle_if(block, cmp, pnc, env);
211 else if (mode_is_int(mode)) {
212 long proj_nr = get_Proj_proj(proj);
214 /* this is a CASE, but we cannot handle the default case */
215 if (proj_nr == get_Cond_defaultProj(cond))
218 handle_case(block, get_Cond_selector(cond), proj_nr, env);
223 * Construct Confirm nodes
225 void construct_confirms(ir_graph *irg)
228 int edges_active = edges_activated(irg);
230 if (get_irg_dom_state(irg) != dom_consistent) {
231 /* we need dominance info */
235 if (! edges_active) {
240 env.num_confirms = 0;
244 /* now, visit all blocks and add Confirms where possible */
245 irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
247 if (env.num_confirms | env.num_consts | env.num_eq) {
248 /* we have add nodes or changed DF edges */
249 set_irg_outs_inconsistent(irg);
251 /* the new nodes are not in the loop info */
252 set_irg_loopinfo_inconsistent(irg);
255 printf("No Confirms inserted : %u\n", env.num_confirms);
256 printf("No Const replacements: %u\n", env.num_consts);
257 printf("No node equalities : %u\n", env.num_eq);
259 /* deactivate edges if they where off */
261 edges_deactivate(irg);
265 * Post-walker: Remove COnfirm nodes
267 static void rem_Confirm(ir_node *n, void *env) {
268 if (get_irn_op(n) == op_Confirm) {
269 exchange(n, get_Confirm_value(n));
274 * Remove all Confirm nodes from a graph.
276 void remove_confirms(ir_graph *irg) {
277 irg_walk_graph(irg, NULL, rem_Confirm, NULL);