3956c17f1b7bdf140a26dfba910a09a447900472
[libfirm] / ir / ana / irconsconfirm.c
1 /*
2  * Copyright (C) 1995-2008 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         /* the effective use of a Phi is in its predecessor block */
60         if (is_Phi(node))
61                 return get_nodes_block(get_irn_n(node, pos));
62         else
63                 return get_nodes_block(node);
64 }
65
66 /**
67  * Handle a CASE-branch.
68  *
69  * @param block   the block which is entered by the branch
70  * @param irn     the node expressing the switch value
71  * @param nr      the branch label
72  * @param env     statistical environment
73  *
74  * Branch labels are a simple case. We can replace the value
75  * by a Const with the branch label.
76  */
77 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
78         const ir_edge_t *edge, *next;
79         ir_node *c = NULL;
80
81         if (is_Bad(irn))
82                 return;
83
84         for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
85                 ir_node *succ = get_edge_src_irn(edge);
86                 int     pos   = get_edge_src_pos(edge);
87                 ir_node *blk  = get_effective_use_block(succ, pos);
88
89                 next = get_irn_out_edge_next(irn, edge);
90
91                 if (block_dominates(block, blk)) {
92                         /*
93                          * Ok, we found a user of irn that is placed
94                          * in a block dominated by the branch block.
95                          * We can replace the input with the Constant
96                          * branch label.
97                          */
98
99                         if (! c) {
100                                 ir_mode *mode = get_irn_mode(irn);
101                                 ir_type *tp   = get_irn_type(irn);
102                                 tarval *tv    = new_tarval_from_long(nr, mode);
103                                 c = new_r_Const_type(current_ir_graph, block, mode, tv, tp);
104                         }
105
106                         set_irn_n(succ, pos, c);
107                         DBG_OPT_CONFIRM_C(irn, c);
108 //                      ir_printf("1 Replacing input %d of node %n with %n\n", pos, succ, c);
109
110                         env->num_consts += 1;
111                 }
112         }
113 }  /* handle_case */
114
115 /**
116  * Handle an IF-branch.
117  *
118  * @param block   the block which is entered by the branch
119  * @param cmp     the Cmp node expressing the branch condition
120  * @param pnc     the Compare relation for taking this branch
121  * @param env     statistical environment
122  */
123 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
124         ir_node *left  = get_Cmp_left(cmp);
125         ir_node *right = get_Cmp_right(cmp);
126         ir_node *cond_block;
127         ir_op *op;
128         const ir_edge_t *edge, *next;
129
130         /* Beware of Bads */
131         if (is_Bad(left) ||is_Bad(right))
132                 return;
133
134         op = get_irn_op(left);
135
136         /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
137            These are removed anyway */
138         if (op == op_Const && is_Const(right))
139                 return;
140
141         /* try to place the constant on the right side for a Confirm */
142         if (op == op_Const || op == op_SymConst) {
143                 ir_node *t = left;
144
145                 left  = right;
146                 right = t;
147
148                 pnc = get_inversed_pnc(pnc);
149         }
150
151         /*
152          * First case: both values are identical.
153          * replace the left one by the right (potentially const) one.
154          */
155         if (pnc == pn_Cmp_Eq) {
156                 cond_block = get_Block_cfgpred_block(block, 0);
157                 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
158                         ir_node *user = get_edge_src_irn(edge);
159                         int     pos   = get_edge_src_pos(edge);
160                         ir_node *blk  = get_effective_use_block(user, pos);
161
162                         next = get_irn_out_edge_next(left, edge);
163                         if (block_dominates(block, blk)) {
164                                 /*
165                                  * Ok, we found a usage of left in a block
166                                  * dominated by the branch block.
167                                  * We can replace the input with right.
168                                  */
169                                 set_irn_n(user, pos, right);
170                                 DBG_OPT_CONFIRM(left, right);
171
172 //                              ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, right);
173
174                                 env->num_eq += 1;
175                         } else if (block_dominates(blk, cond_block)) {
176                                 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
177                                         /*
178                                          * left == Const and we found a movable user of left in a
179                                          * dominator of the Cond block
180                                          */
181                                         const ir_edge_t *edge, *next;
182                                         for (edge = get_irn_out_edge_first(user); edge; edge = next) {
183                                                 ir_node *usr_of_usr = get_edge_src_irn(edge);
184                                                 int      npos = get_edge_src_pos(edge);
185                                                 ir_node *blk  = get_effective_use_block(usr_of_usr, npos);
186
187                                                 next = get_irn_out_edge_next(user, edge);
188                                                 if (block_dominates(block, blk)) {
189                                                         /*
190                                                          * The user of the user is dominated by our true/false
191                                                          * block. So, create a copy of user WITH the constant
192                                                          * replacing it's pos'th input.
193                                                          *
194                                                          * This is always good for unop's and might be good
195                                                          * for binops.
196                                                          *
197                                                          * If user has other user in the false/true block, code
198                                                          * placement will move it down.
199                                                          * If there are users in cond block or upper, we create
200                                                          * "redundant ops", because one will have a const op,
201                                                          * the other no const ...
202                                                          */
203                                                         ir_node *new_op = exact_copy(user);
204                                                         set_nodes_block(new_op, block);
205                                                         set_irn_n(new_op, pos, right);
206                                                         set_irn_n(usr_of_usr, npos, new_op);
207                                                 }
208                                         }
209                                 }
210                         }
211                 }
212         } else { /* not pn_Cmp_Eq cases */
213                 ir_node *c = NULL;
214
215                 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
216                         ir_node *succ = get_edge_src_irn(edge);
217                         int     pos   = get_edge_src_pos(edge);
218                         ir_node *blk  = get_effective_use_block(succ, pos);
219
220                         next = get_irn_out_edge_next(left, edge);
221                         if (block_dominates(block, blk)) {
222                                 /*
223                                  * Ok, we found a usage of left in a block
224                                  * dominated by the branch block.
225                                  * We can replace the input with a Confirm(left, pnc, right).
226                                  */
227                                 if (! c)
228                                         c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
229
230                                 pos = get_edge_src_pos(edge);
231                                 set_irn_n(succ, pos, c);
232 //                              ir_printf("3 Replacing input %d of node %n with %n\n", pos, user, c);
233
234                                 env->num_confirms += 1;
235                         }
236                 }
237         }
238 }  /* handle_if */
239
240 /**
241  * Pre-walker: Called for every block to insert Confirm nodes
242  */
243 static void insert_Confirm(ir_node *block, void *env) {
244         ir_node *cond, *proj, *selector;
245         ir_mode *mode;
246
247         /*
248          * we can only handle blocks with only ONE control flow
249          * predecessor yet.
250          */
251         if (get_Block_n_cfgpreds(block) != 1)
252                 return;
253
254         proj = get_Block_cfgpred(block, 0);
255         if (get_irn_op(proj) != op_Proj)
256                 return;
257
258         cond = get_Proj_pred(proj);
259         if (get_irn_op(cond) != op_Cond)
260                 return;
261
262         selector = get_Cond_selector(cond);
263         mode = get_irn_mode(selector);
264
265         if (mode == mode_b) {
266                 ir_node *cmp;
267                 pn_Cmp pnc;
268
269                 /* this should be an IF, check this */
270                 if (get_irn_op(selector) != op_Proj)
271                         return;
272
273                 cmp = get_Proj_pred(selector);
274                 if (get_irn_op(cmp) != op_Cmp)
275                         return;
276
277                 pnc = get_Proj_proj(selector);
278
279                 if (get_Proj_proj(proj) != pn_Cond_true) {
280                         /* it's the false branch */
281                         pnc = get_negated_pnc(pnc, mode);
282                 }
283 //              ir_printf("At %n using %n Confirm %=\n", block, cmp, pnc);
284
285                 handle_if(block, cmp, pnc, env);
286         } else if (mode_is_int(mode)) {
287                 long proj_nr = get_Proj_proj(proj);
288
289                 /* this is a CASE, but we cannot handle the default case */
290                 if (proj_nr == get_Cond_defaultProj(cond))
291                         return;
292
293                 handle_case(block, get_Cond_selector(cond), proj_nr, env);
294         }
295 }  /* insert_Confirm */
296
297 /*
298  * Construct Confirm nodes
299  */
300 void construct_confirms(ir_graph *irg) {
301         env_t env;
302         int edges_active = edges_activated(irg);
303
304         /* we need dominance info */
305         assure_doms(irg);
306
307         assert(get_irg_pinned(irg) == op_pin_state_pinned &&
308                "Nodes must be placed to insert Confirms");
309
310         if (! edges_active) {
311                 /* We need edges */
312                 edges_activate(irg);
313         }
314
315         env.num_confirms = 0;
316         env.num_consts   = 0;
317         env.num_eq       = 0;
318
319         /* now, visit all blocks and add Confirms where possible */
320         irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
321
322         if (env.num_confirms | env.num_consts | env.num_eq) {
323                 /* we have add nodes or changed DF edges */
324                 set_irg_outs_inconsistent(irg);
325
326                 /* the new nodes are not in the loop info */
327                 set_irg_loopinfo_inconsistent(irg);
328         }
329
330 #if 0
331         printf("# Confirms inserted : %u\n", env.num_confirms);
332         printf("# Const replacements: %u\n", env.num_consts);
333         printf("# node equalities   : %u\n", env.num_eq);
334 #endif
335
336         /* deactivate edges if they where off */
337         if (! edges_active)
338                 edges_deactivate(irg);
339 }  /* construct_confirms */
340
341 /**
342  * Post-walker: Remove Confirm nodes
343  */
344 static void rem_Confirm(ir_node *n, void *env) {
345         (void) env;
346         if (get_irn_op(n) == op_Confirm) {
347                 ir_node *value = get_Confirm_value(n);
348                 if (value != n)
349                         exchange(n, value);
350                 else {
351                         /*
352                          * Strange: a Confirm is it's own bound. This can happen
353                          * in dead blocks when Phi nodes are already removed.
354                          */
355                         exchange(n, new_Bad());
356                 }
357         }
358 }  /* rem_Confirm */
359
360 /*
361  * Remove all Confirm nodes from a graph.
362  */
363 void remove_confirms(ir_graph *irg) {
364         irg_walk_graph(irg, NULL, rem_Confirm, NULL);
365 }  /* remove_confirms */