Restructured a bit
[libfirm] / ir / ana / irconsconfirm.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Construction of Confirm nodes
23  * @author   Michael Beck
24  * @date     6.2005
25  * @version  $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "irgraph_t.h"
32 #include "irnode_t.h"
33 #include "ircons_t.h"
34 #include "irgmod.h"
35 #include "iropt_dbg.h"
36 #include "iredges_t.h"
37 #include "irgwalk.h"
38 #include "irprintf.h"
39
40 /**
41  * Walker environment.
42  */
43 typedef struct _env_t {
44   unsigned num_confirms;  /**< number of inserted Confirm nodes */
45   unsigned num_consts;    /**< number of constants placed */
46   unsigned num_eq;        /**< number of equalities placed */
47 } env_t;
48
49 /**
50  * Return the effective use block of a node and it's predecessor on
51  * position pos.
52  *
53  * @param node  the node
54  * @param pos   the position of the used input
55  *
56  * This handles correctly Phi nodes.
57  */
58 static ir_node *get_effective_use_block(ir_node *node, int pos)
59 {
60   /* the effective use of a Phi is in its predecessor block */
61   if (is_Phi(node))
62     return get_nodes_block(get_irn_n(node, pos));
63   else
64     return get_nodes_block(node);
65 }
66
67 /**
68  * Handle a CASE-branch.
69  *
70  * @param block   the block which is entered by the branch
71  * @param irn     the node expressing the switch value
72  * @param nr      the branch label
73  * @param env     statistical environment
74  *
75  * Branch labels are a simple case. We can replace the value
76  * by a Const with the branch label.
77  */
78 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env)
79 {
80   const ir_edge_t *edge, *next;
81   ir_node *c = NULL;
82
83   if (is_Bad(irn))
84     return;
85
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);
90
91     next = get_irn_out_edge_next(irn, edge);
92
93     if (block_dominates(block, blk)) {
94       /*
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
98        * branch label.
99        */
100
101       if (! c) {
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);
106       }
107
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);
111
112       env->num_consts += 1;
113     }
114   }
115 }  /* handle_case */
116
117 /**
118  * Handle an IF-branch.
119  *
120  * @param block   the block which is entered by the branch
121  * @param cmp     the Cmp node expressing the branch condition
122  * @param pnc     the Compare relation for taking this branch
123  * @param env     statistical environment
124  */
125 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env)
126 {
127   ir_node *left  = get_Cmp_left(cmp);
128   ir_node *right = get_Cmp_right(cmp);
129   ir_op *op;
130   const ir_edge_t *edge, *next;
131
132   /* Beware of Bads */
133   if (is_Bad(left) ||is_Bad(right))
134     return;
135
136   op = get_irn_op(left);
137
138   /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
139      These are removed anyway */
140   if (op == op_Const && is_Const(right))
141     return;
142
143   /* try to place the constant on the right side for a Confirm */
144   if (op == op_Const || op == op_SymConst) {
145     ir_node *t = left;
146
147     left  = right;
148     right = t;
149
150     pnc = get_inversed_pnc(pnc);
151   }
152
153   /*
154    * First case: both values are identical.
155    * replace the left one by the right (potentially const) one.
156    */
157   if (pnc == pn_Cmp_Eq) {
158     for (edge = get_irn_out_edge_first(left); edge; edge = next) {
159       ir_node *succ = get_edge_src_irn(edge);
160       int     pos   = get_edge_src_pos(edge);
161       ir_node *blk  = get_effective_use_block(succ, pos);
162
163       next = get_irn_out_edge_next(left, edge);
164       if (block_dominates(block, blk)) {
165         /*
166          * Ok, we found a usage of left in a block
167          * dominated by the branch block.
168          * We can replace the input with right.
169          */
170         set_irn_n(succ, pos, right);
171         DBG_OPT_CONFIRM(left, right);
172
173 //        ir_printf("2 Replacing input %d of node %n with %n\n", pos, succ, right);
174
175         env->num_eq += 1;
176       }
177     }
178   }
179   else { /* not pn_Cmp_Eq cases */
180     ir_node *c = NULL;
181
182     for (edge = get_irn_out_edge_first(left); edge; edge = next) {
183       ir_node *succ = get_edge_src_irn(edge);
184       int     pos   = get_edge_src_pos(edge);
185       ir_node *blk  = get_effective_use_block(succ, pos);
186
187       next = get_irn_out_edge_next(left, edge);
188       if (block_dominates(block, blk)) {
189         /*
190          * Ok, we found a usage of left in a block
191          * dominated by the branch block.
192          * We can replace the input with a Confirm(left, pnc, right).
193          */
194         if (! c)
195           c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
196
197         pos = get_edge_src_pos(edge);
198         set_irn_n(succ, pos, c);
199 //        ir_printf("3 Replacing input %d of node %n with %n\n", pos, succ, c);
200
201         env->num_confirms += 1;
202       }
203     }
204   }
205 }  /* handle_if */
206
207 /**
208  * Pre-walker: Called for every block to insert Confirm nodes
209  */
210 static void insert_Confirm(ir_node *block, void *env)
211 {
212   ir_node *cond, *proj, *selector;
213   ir_mode *mode;
214
215   /*
216    * we can only handle blocks with only ONE control flow
217    * predecessor yet.
218    */
219   if (get_Block_n_cfgpreds(block) != 1)
220     return;
221
222   proj = get_Block_cfgpred(block, 0);
223   if (get_irn_op(proj) != op_Proj)
224     return;
225
226   cond = get_Proj_pred(proj);
227   if (get_irn_op(cond) != op_Cond)
228     return;
229
230   selector = get_Cond_selector(cond);
231   mode = get_irn_mode(selector);
232
233   if (mode == mode_b) {
234     ir_node *cmp;
235     pn_Cmp pnc;
236
237     /* this should be an IF, check this */
238     if (get_irn_op(selector) != op_Proj)
239       return;
240
241     cmp = get_Proj_pred(selector);
242     if (get_irn_op(cmp) != op_Cmp)
243       return;
244
245     pnc = get_Proj_proj(selector);
246
247     if (get_Proj_proj(proj) != pn_Cond_true) {
248       /* it's the false branch */
249       pnc = get_negated_pnc(pnc, mode);
250     }
251 //    ir_printf("At %n using %n Confirm %=\n", block, cmp, pnc);
252
253     handle_if(block, cmp, pnc, env);
254   }
255   else if (mode_is_int(mode)) {
256     long proj_nr = get_Proj_proj(proj);
257
258     /* this is a CASE, but we cannot handle the default case */
259     if (proj_nr == get_Cond_defaultProj(cond))
260       return;
261
262     handle_case(block, get_Cond_selector(cond), proj_nr, env);
263   }
264 }  /* insert_Confirm */
265
266 /*
267  * Construct Confirm nodes
268  */
269 void construct_confirms(ir_graph *irg)
270 {
271   env_t env;
272   int edges_active = edges_activated(irg);
273
274   /* we need dominance info */
275   assure_doms(irg);
276
277   assert(get_irg_pinned(irg) == op_pin_state_pinned &&
278     "Nodes must be placed to insert Confirms");
279
280   if (! edges_active) {
281     /* We need edges */
282     edges_activate(irg);
283   }
284
285   env.num_confirms = 0;
286   env.num_consts   = 0;
287   env.num_eq       = 0;
288
289   /* now, visit all blocks and add Confirms where possible */
290   irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
291
292   if (env.num_confirms | env.num_consts | env.num_eq) {
293     /* we have add nodes or changed DF edges */
294     set_irg_outs_inconsistent(irg);
295
296     /* the new nodes are not in the loop info */
297     set_irg_loopinfo_inconsistent(irg);
298   }
299
300 #if 0
301   printf("# Confirms inserted : %u\n", env.num_confirms);
302   printf("# Const replacements: %u\n", env.num_consts);
303   printf("# node equalities   : %u\n", env.num_eq);
304 #endif
305
306   /* deactivate edges if they where off */
307   if (! edges_active)
308     edges_deactivate(irg);
309 }  /* construct_confirms */
310
311 /**
312  * Post-walker: Remove Confirm nodes
313  */
314 static void rem_Confirm(ir_node *n, void *env) {
315   if (get_irn_op(n) == op_Confirm) {
316     ir_node *value = get_Confirm_value(n);
317     if (value != n)
318       exchange(n, value);
319     else {
320       /*
321        * Strange: a Confirm is it's own bound. This can happen
322        * in dead blocks when Phi nodes are already removed.
323        */
324       exchange(n, new_Bad());
325     }
326   }
327 }  /* rem_Confirm */
328
329 /*
330  * Remove all Confirm nodes from a graph.
331  */
332 void remove_confirms(ir_graph *irg) {
333   irg_walk_graph(irg, NULL, rem_Confirm, NULL);
334 }  /* remove_confirms */