- fixed a stupid Bug: the marks are not cleared at the beginning,
[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 #include "irgopt.h"
40 #include "irtools.h"
41
42 /**
43  * Walker environment.
44  */
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 */
49 } env_t;
50
51 /**
52  * Return the effective use block of a node and it's predecessor on
53  * position pos.
54  *
55  * @param node  the node
56  * @param pos   the position of the used input
57  *
58  * This handles correctly Phi nodes.
59  */
60 static ir_node *get_effective_use_block(ir_node *node, int pos) {
61         if (is_Phi(node)) {
62                 /* the effective use of a Phi is in its predecessor block */
63                 node = get_irn_n(node, pos);
64         }
65         return get_nodes_block(node);
66 }
67
68 /**
69  * Handle a CASE-branch.
70  *
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
75  *
76  * Branch labels are a simple case. We can replace the value
77  * by a Const with the branch label.
78  */
79 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
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 a mode_b input of Cond nodes.
119  *
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
124  */
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;
129
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);
134
135                 next = get_irn_out_edge_next(selector, edge);
136                 if (block_dominates(block, user_blk)) {
137                         /*
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.
141                          */
142                         if (con == NULL) {
143                                 con = new_Const(mode_b, pnc == pn_Cond_true ? tarval_b_true : tarval_b_false);
144                         }
145                         old = get_irn_n(user, pos);
146                         set_irn_n(user, pos, con);
147                         DBG_OPT_CONFIRM_C(old, con);
148
149                         // ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, con);
150
151                         env->num_consts += 1;
152                 } else {
153                         int i, n;
154
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)
163                                                 continue;
164                                         edge = get_irn_out_edge_first(proj);
165                                         other_blk = get_edge_src_irn(edge);
166                                         break;
167                                 }
168                                 assert(other_blk);
169
170                                 /*
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 ...
174                                  *
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 ...
179                                  */
180                         }
181
182                         n = get_Block_n_cfgpreds(user_blk);
183
184                         /*
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.
188                          */
189                         for (i = n - 1; i >= 0; --i) {
190                                 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
191
192                                 if (!block_dominates(block, pred_blk) &&
193                                     !block_dominates(other_blk, pred_blk)) {
194                                         /* can't do anything */
195                                         break;
196                                 }
197                         }
198                         if (i < 0) {
199                                 ir_node *phi, **in;
200
201                                 NEW_ARR_A(ir_node *, in, n);
202                                 /* ok, ALL predecessors are either dominated by block OR other block */
203                                 if (c_b == NULL) {
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);
210                                 }
211                                 for (i = n - 1; i >= 0; --i) {
212                                         ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
213
214                                         if (block_dominates(block, pred_blk))
215                                                 in[i] = c_b;
216                                         else
217                                                 in[i] = c_o;
218                                 }
219                                 phi = new_r_Phi(current_ir_graph, user_blk, n, in, mode_b);
220                                 set_irn_n(user, pos, phi);
221                         }
222                 }
223         }
224 }
225
226 /**
227  * Handle an IF-branch.
228  *
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
233  */
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);
237         ir_node *cond_block;
238         ir_op *op;
239         const ir_edge_t *edge, *next;
240
241         /* Beware of Bads */
242         if (is_Bad(left) || is_Bad(right))
243                 return;
244
245         op = get_irn_op(left);
246
247         /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
248            These are removed anyway */
249         if (op == op_Const && is_Const(right))
250                 return;
251
252         /* try to place the constant on the right side for a Confirm */
253         if (op == op_Const || op == op_SymConst) {
254                 ir_node *t = left;
255
256                 left  = right;
257                 right = t;
258
259                 pnc = get_inversed_pnc(pnc);
260         }
261
262         /*
263          * First case: both values are identical.
264          * replace the left one by the right (potentially const) one.
265          */
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);
272
273                         next = get_irn_out_edge_next(left, edge);
274                         if (block_dominates(block, blk)) {
275                                 /*
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.
279                                  */
280                                 set_irn_n(user, pos, right);
281                                 DBG_OPT_CONFIRM(left, right);
282
283 //                              ir_printf("2 Replacing input %d of node %n with %n\n", pos, user, right);
284
285                                 env->num_eq += 1;
286                         } else if (block_dominates(blk, cond_block)) {
287                                 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
288                                         /*
289                                          * left == Const and we found a movable user of left in a
290                                          * dominator of the Cond block
291                                          */
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);
297
298                                                 next = get_irn_out_edge_next(user, edge);
299                                                 if (block_dominates(block, blk)) {
300                                                         /*
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.
304                                                          *
305                                                          * This is always good for unop's and might be good
306                                                          * for binops.
307                                                          *
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 ...
313                                                          */
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);
318                                                 }
319                                         }
320                                 }
321                         }
322                 }
323         } else { /* not pn_Cmp_Eq cases */
324                 ir_node *c = NULL;
325
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);
330
331                         next = get_irn_out_edge_next(left, edge);
332                         if (block_dominates(block, blk)) {
333                                 /*
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).
337                                  */
338                                 if (! c)
339                                         c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
340
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);
344
345                                 env->num_confirms += 1;
346                         }
347                 }
348         }
349 }  /* handle_if */
350
351 /**
352  * Pre-walker: Called for every block to insert Confirm nodes
353  */
354 static void insert_Confirm(ir_node *block, void *env) {
355         ir_node *cond, *proj, *selector;
356         ir_mode *mode;
357
358         /*
359          * we can only handle blocks with only ONE control flow
360          * predecessor yet.
361          */
362         if (get_Block_n_cfgpreds(block) != 1)
363                 return;
364
365         proj = get_Block_cfgpred(block, 0);
366         if (! is_Proj(proj))
367                 return;
368
369         cond = get_Proj_pred(proj);
370         if (! is_Cond(cond))
371                 return;
372
373         selector = get_Cond_selector(cond);
374         mode = get_irn_mode(selector);
375
376         if (mode == mode_b) {
377                 ir_node *cmp;
378                 pn_Cmp pnc;
379
380                 handle_modeb(block, selector, get_Proj_proj(proj), env);
381
382                 /* this should be an IF, check this */
383                 if (get_irn_op(selector) != op_Proj)
384                         return;
385
386                 cmp = get_Proj_pred(selector);
387                 if (get_irn_op(cmp) != op_Cmp)
388                         return;
389
390                 pnc = get_Proj_proj(selector);
391
392                 if (get_Proj_proj(proj) != pn_Cond_true) {
393                         /* it's the false branch */
394                         pnc = get_negated_pnc(pnc, mode);
395                 }
396 //              ir_printf("At %n using %n Confirm %=\n", block, cmp, pnc);
397
398                 handle_if(block, cmp, pnc, env);
399         } else if (mode_is_int(mode)) {
400                 long proj_nr = get_Proj_proj(proj);
401
402                 /* this is a CASE, but we cannot handle the default case */
403                 if (proj_nr == get_Cond_defaultProj(cond))
404                         return;
405
406                 handle_case(block, get_Cond_selector(cond), proj_nr, env);
407         }
408 }  /* insert_Confirm */
409
410 /*
411  * Construct Confirm nodes
412  */
413 void construct_confirms(ir_graph *irg) {
414         env_t env;
415         int edges_active = edges_activated(irg);
416
417         remove_critical_cf_edges(irg);
418
419         /* we need dominance info */
420         assure_doms(irg);
421
422         assert(get_irg_pinned(irg) == op_pin_state_pinned &&
423                "Nodes must be placed to insert Confirms");
424
425         if (! edges_active) {
426                 /* We need edges */
427                 edges_activate(irg);
428         }
429
430         env.num_confirms = 0;
431         env.num_consts   = 0;
432         env.num_eq       = 0;
433
434         /* now, visit all blocks and add Confirms where possible */
435         irg_block_walk_graph(irg, insert_Confirm, NULL, &env);
436
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);
440
441                 /* the new nodes are not in the loop info */
442                 set_irg_loopinfo_inconsistent(irg);
443         }
444
445 #if 0
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);
449 #endif
450
451         /* deactivate edges if they where off */
452         if (! edges_active)
453                 edges_deactivate(irg);
454 }  /* construct_confirms */
455
456 /**
457  * Post-walker: Remove Confirm nodes
458  */
459 static void rem_Confirm(ir_node *n, void *env) {
460         (void) env;
461         if (get_irn_op(n) == op_Confirm) {
462                 ir_node *value = get_Confirm_value(n);
463                 if (value != n)
464                         exchange(n, value);
465                 else {
466                         /*
467                          * Strange: a Confirm is it's own bound. This can happen
468                          * in dead blocks when Phi nodes are already removed.
469                          */
470                         exchange(n, new_Bad());
471                 }
472         }
473 }  /* rem_Confirm */
474
475 /*
476  * Remove all Confirm nodes from a graph.
477  */
478 void remove_confirms(ir_graph *irg) {
479         set_opt_remove_confirm(1);
480         optimize_graph_df(irg);
481 }  /* remove_confirms */