4a0abc38b7d01189dd7b63c53246b1a24d8b810e
[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 #include "config.h"
28
29 #include "irgraph_t.h"
30 #include "irnode_t.h"
31 #include "ircons_t.h"
32 #include "irgmod.h"
33 #include "iropt_dbg.h"
34 #include "iredges_t.h"
35 #include "irgwalk.h"
36 #include "irprintf.h"
37 #include "irgopt.h"
38 #include "irtools.h"
39 #include "array_t.h"
40 #include "debug.h"
41 #include "irflag.h"
42
43 /**
44  * Walker environment.
45  */
46 typedef struct _env_t {
47         unsigned num_confirms;  /**< Number of inserted Confirm nodes. */
48         unsigned num_consts;    /**< Number of constants placed. */
49         unsigned num_eq;        /**< Number of equalities placed. */
50         unsigned num_non_null;  /**< Number of non-null Confirms. */
51 } env_t;
52
53 /** The debug handle. */
54 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
55
56 /**
57  * Return the effective use block of a node and it's predecessor on
58  * position pos.
59  *
60  * @param node  the node
61  * @param pos   the position of the used input
62  *
63  * This handles correctly Phi nodes.
64  */
65 static ir_node *get_effective_use_block(ir_node *node, int pos) {
66         if (is_Phi(node)) {
67                 /* the effective use of a Phi argument is in its predecessor block */
68                 node = get_nodes_block(node);
69                 return get_Block_cfgpred_block(node, pos);
70         }
71         return get_nodes_block(node);
72 }
73
74 /**
75  * Handle a CASE-branch.
76  *
77  * @param block   the block which is entered by the branch
78  * @param irn     the node expressing the switch value
79  * @param nr      the branch label
80  * @param env     statistical environment
81  *
82  * Branch labels are a simple case. We can replace the value
83  * by a Const with the branch label.
84  */
85 static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
86         const ir_edge_t *edge, *next;
87         ir_node *c = NULL;
88
89         if (is_Bad(irn))
90                 return;
91
92         for (edge = get_irn_out_edge_first(irn); edge; edge = next) {
93                 ir_node *succ = get_edge_src_irn(edge);
94                 int     pos   = get_edge_src_pos(edge);
95                 ir_node *blk  = get_effective_use_block(succ, pos);
96
97                 next = get_irn_out_edge_next(irn, edge);
98
99                 if (block_dominates(block, blk)) {
100                         /*
101                          * Ok, we found a user of irn that is placed
102                          * in a block dominated by the branch block.
103                          * We can replace the input with the Constant
104                          * branch label.
105                          */
106
107                         if (! c) {
108                                 ir_mode *mode = get_irn_mode(irn);
109                                 ir_type *tp   = get_irn_type(irn);
110                                 tarval *tv    = new_tarval_from_long(nr, mode);
111                                 c = new_r_Const_type(current_ir_graph, tv, tp);
112                         }
113
114                         set_irn_n(succ, pos, c);
115                         DBG_OPT_CONFIRM_C(irn, c);
116                         DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
117
118                         env->num_consts += 1;
119                 }
120         }
121 }  /* handle_case */
122
123 /**
124  * Handle a mode_b input of Cond nodes.
125  *
126  * @param block     the block which is entered by the branch
127  * @param selector  the mode_b node expressing the branch condition
128  * @param pnc       the true/false condition branch
129  * @param env       statistical environment
130  */
131 static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env) {
132         ir_node *cond, *old, *cond_block = NULL, *other_blk = NULL, *con = NULL;
133         ir_node *c_b = NULL, *c_o = NULL;
134         const ir_edge_t *edge, *next;
135
136         for (edge = get_irn_out_edge_first(selector); edge; edge = next) {
137                 ir_node *user     = get_edge_src_irn(edge);
138                 int     pos       = get_edge_src_pos(edge);
139                 ir_node *user_blk = get_effective_use_block(user, pos);
140
141                 next = get_irn_out_edge_next(selector, edge);
142                 if (block_dominates(block, user_blk)) {
143                         /*
144                          * Ok, we found a usage of selector in a block
145                          * dominated by the branch block.
146                          * We can replace the input with true/false.
147                          */
148                         if (con == NULL) {
149                                 con = new_Const(pnc == pn_Cond_true ? tarval_b_true : tarval_b_false);
150                         }
151                         old = get_irn_n(user, pos);
152                         set_irn_n(user, pos, con);
153                         DBG_OPT_CONFIRM_C(old, con);
154
155                         DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, user, con));
156
157                         env->num_consts += 1;
158                 } else {
159                         int i, n;
160
161                         /* get the other block */
162                         if (other_blk == NULL) {
163                                 /* we have already tested, that block has only ONE Cond predecessor */
164                                 cond = get_Proj_pred(get_Block_cfgpred(block, 0));
165                                 cond_block = get_nodes_block(cond);
166                                 foreach_out_edge(cond, edge) {
167                                         ir_node *proj = get_edge_src_irn(edge);
168                                         if (get_Proj_proj(proj) == (long)pnc)
169                                                 continue;
170                                         edge = get_irn_out_edge_first(proj);
171                                         other_blk = get_edge_src_irn(edge);
172                                         break;
173                                 }
174                                 assert(other_blk);
175
176                                 /*
177                                  * Note the special case here: if block is a then, there might be no else
178                                  * block. In that case the other_block is the user_blk itself and pred_block
179                                  * is the cond_block ...
180                                  *
181                                  * Best would be to introduce a block here, removing this critical edge.
182                                  * For some reasons I cannot repair dominance here, so I have to remove
183                                  * ALL critical edges...
184                                  * FIXME: This should not be needed if we could repair dominance ...
185                                  */
186                         }
187
188                         n = get_Block_n_cfgpreds(user_blk);
189
190                         /*
191                          * We have found a user in a non-dominated block:
192                          * check, if all its block predecessors are dominated.
193                          * If yes, place a Phi.
194                          */
195                         for (i = n - 1; i >= 0; --i) {
196                                 ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
197
198                                 if (!block_dominates(block, pred_blk) &&
199                                     !block_dominates(other_blk, pred_blk)) {
200                                         /* can't do anything */
201                                         break;
202                                 }
203                         }
204                         if (i < 0) {
205                                 ir_node *phi, **in;
206
207                                 NEW_ARR_A(ir_node *, in, n);
208                                 /* ok, ALL predecessors are either dominated by block OR other block */
209                                 if (c_b == NULL) {
210                                         ir_node *c_true  = new_Const(tarval_b_true);
211                                         ir_node *c_false = new_Const(tarval_b_false);
212                                         if (pnc == pn_Cond_true) {
213                                                 c_b = c_true;
214                                                 c_o = c_false;
215                                         } else {
216                                                 c_b = c_false;
217                                                 c_o = c_true;
218                                         }
219                                 }
220                                 for (i = n - 1; i >= 0; --i) {
221                                         ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
222
223                                         if (block_dominates(block, pred_blk))
224                                                 in[i] = c_b;
225                                         else
226                                                 in[i] = c_o;
227                                 }
228                                 phi = new_r_Phi(user_blk, n, in, mode_b);
229                                 set_irn_n(user, pos, phi);
230                         }
231                 }
232         }
233 }
234
235 /**
236  * Handle an IF-branch.
237  *
238  * @param block   the block which is entered by the branch
239  * @param cmp     the Cmp node expressing the branch condition
240  * @param pnc     the Compare relation for taking this branch
241  * @param env     statistical environment
242  */
243 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
244         ir_node *left  = get_Cmp_left(cmp);
245         ir_node *right = get_Cmp_right(cmp);
246         ir_node *cond_block;
247         ir_op *op;
248         const ir_edge_t *edge, *next;
249
250         /* Beware of Bads */
251         if (is_Bad(left) || is_Bad(right))
252                 return;
253
254         op = get_irn_op(left);
255
256         /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
257            These are removed anyway */
258         if (op == op_Const && is_Const(right))
259                 return;
260
261         /* try to place the constant on the right side for a Confirm */
262         if (op == op_Const || op == op_SymConst) {
263                 ir_node *t = left;
264
265                 left  = right;
266                 right = t;
267
268                 pnc = get_inversed_pnc(pnc);
269         }
270
271         /*
272          * First case: both values are identical.
273          * replace the left one by the right (potentially const) one.
274          */
275         if (pnc == pn_Cmp_Eq) {
276                 cond_block = get_Block_cfgpred_block(block, 0);
277                 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
278                         ir_node *user = get_edge_src_irn(edge);
279                         int     pos   = get_edge_src_pos(edge);
280                         ir_node *blk  = get_effective_use_block(user, pos);
281
282                         next = get_irn_out_edge_next(left, edge);
283                         if (block_dominates(block, blk)) {
284                                 /*
285                                  * Ok, we found a usage of left in a block
286                                  * dominated by the branch block.
287                                  * We can replace the input with right.
288                                  */
289                                 set_irn_n(user, pos, right);
290                                 DBG_OPT_CONFIRM(left, right);
291
292                                 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, user, right));
293
294                                 env->num_eq += 1;
295                         } else if (block_dominates(blk, cond_block)) {
296                                 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
297                                         /*
298                                          * left == Const and we found a movable user of left in a
299                                          * dominator of the Cond block
300                                          */
301                                         const ir_edge_t *edge, *next;
302                                         for (edge = get_irn_out_edge_first(user); edge; edge = next) {
303                                                 ir_node *usr_of_usr = get_edge_src_irn(edge);
304                                                 int      npos = get_edge_src_pos(edge);
305                                                 ir_node *blk  = get_effective_use_block(usr_of_usr, npos);
306
307                                                 next = get_irn_out_edge_next(user, edge);
308                                                 if (block_dominates(block, blk)) {
309                                                         /*
310                                                          * The user of the user is dominated by our true/false
311                                                          * block. So, create a copy of user WITH the constant
312                                                          * replacing it's pos'th input.
313                                                          *
314                                                          * This is always good for unop's and might be good
315                                                          * for binops.
316                                                          *
317                                                          * If user has other user in the false/true block, code
318                                                          * placement will move it down.
319                                                          * If there are users in cond block or upper, we create
320                                                          * "redundant ops", because one will have a const op,
321                                                          * the other no const ...
322                                                          */
323                                                         ir_node *new_op = exact_copy(user);
324                                                         set_nodes_block(new_op, block);
325                                                         set_irn_n(new_op, pos, right);
326                                                         set_irn_n(usr_of_usr, npos, new_op);
327                                                 }
328                                         }
329                                 }
330                         }
331                 }
332         } else { /* not pn_Cmp_Eq cases */
333                 ir_node *c = NULL;
334
335                 foreach_out_edge_safe(left, edge, next) {
336                         ir_node *succ = get_edge_src_irn(edge);
337                         int     pos   = get_edge_src_pos(edge);
338                         ir_node *blk  = get_effective_use_block(succ, pos);
339
340                         if (block_dominates(block, blk)) {
341                                 /*
342                                  * Ok, we found a usage of left in a block
343                                  * dominated by the branch block.
344                                  * We can replace the input with a Confirm(left, pnc, right).
345                                  */
346                                 if (! c)
347                                         c = new_r_Confirm(block, left, right, pnc);
348
349                                 pos = get_edge_src_pos(edge);
350                                 set_irn_n(succ, pos, c);
351                                 DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
352
353                                 env->num_confirms += 1;
354                         }
355                 }
356
357                 if (! is_Const(right)) {
358                         /* also construct inverse Confirms */
359                         ir_node *rc = NULL;
360
361                         pnc = get_inversed_pnc(pnc);
362                         foreach_out_edge_safe(right, edge, next) {
363                                 ir_node *succ = get_edge_src_irn(edge);
364                                 int     pos;
365                                 ir_node *blk;
366
367                                 if (succ == c)
368                                         continue;
369
370                                 pos  = get_edge_src_pos(edge);
371                                 blk  = get_effective_use_block(succ, pos);
372
373                                 if (block_dominates(block, blk)) {
374                                         /*
375                                          * Ok, we found a usage of right in a block
376                                          * dominated by the branch block.
377                                          * We can replace the input with a Confirm(right, pnc^-1, left).
378                                          */
379                                         if (! rc)
380                                                 rc = new_r_Confirm(block, right, left, pnc);
381
382                                         pos = get_edge_src_pos(edge);
383                                         set_irn_n(succ, pos, rc);
384                                         DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, rc));
385
386                                         env->num_confirms += 1;
387                                 }
388                         }
389                 }
390         }
391 }  /* handle_if */
392
393 /**
394  * Pre-block-walker: Called for every block to insert Confirm nodes
395  */
396 static void insert_Confirm_in_block(ir_node *block, void *env) {
397         ir_node *cond, *proj, *selector;
398         ir_mode *mode;
399
400         /*
401          * we can only handle blocks with only ONE control flow
402          * predecessor yet.
403          */
404         if (get_Block_n_cfgpreds(block) != 1)
405                 return;
406
407         proj = get_Block_cfgpred(block, 0);
408         if (! is_Proj(proj))
409                 return;
410
411         cond = get_Proj_pred(proj);
412         if (! is_Cond(cond))
413                 return;
414
415         selector = get_Cond_selector(cond);
416         mode = get_irn_mode(selector);
417
418         if (mode == mode_b) {
419                 ir_node *cmp;
420                 pn_Cmp pnc;
421
422                 handle_modeb(block, selector, get_Proj_proj(proj), env);
423
424                 /* this should be an IF, check this */
425                 if (! is_Proj(selector))
426                         return;
427
428                 cmp = get_Proj_pred(selector);
429                 if (! is_Cmp(cmp))
430                         return;
431
432                 pnc = get_Proj_proj(selector);
433
434                 if (get_Proj_proj(proj) != pn_Cond_true) {
435                         /* it's the false branch */
436                         mode = get_irn_mode(get_Cmp_left(cmp));
437                         pnc = get_negated_pnc(pnc, mode);
438                 }
439                 DB((dbg, LEVEL_2, "At %+F using %+F Confirm %=\n", block, cmp, pnc));
440
441                 handle_if(block, cmp, pnc, env);
442         } else if (mode_is_int(mode)) {
443                 long proj_nr = get_Proj_proj(proj);
444
445                 /* this is a CASE, but we cannot handle the default case */
446                 if (proj_nr == get_Cond_default_proj(cond))
447                         return;
448
449                 handle_case(block, get_Cond_selector(cond), proj_nr, env);
450         }
451 }  /* insert_Confirm_in_block */
452
453 /**
454  * Checks if a node is a non-null Confirm.
455  */
456 static int is_non_null_Confirm(const ir_node *ptr) {
457         for (;;) {
458                 if (! is_Confirm(ptr))
459                         break;
460                 if (get_Confirm_cmp(ptr) == pn_Cmp_Lg) {
461                         ir_node *bound = get_Confirm_bound(ptr);
462
463                         if (is_Const(bound) && is_Const_null(bound))
464                                 return 1;
465                 }
466                 ptr = get_Confirm_value(ptr);
467         }
468         /*
469          * While a SymConst is not a Confirm, it is non-null
470          * anyway. This helps to reduce the number of
471          * constructed Confirms.
472          */
473         if (is_SymConst_addr_ent(ptr))
474                 return 1;
475         return 0;
476 }  /* is_non_null_Confirm */
477
478 /**
479  * The given pointer will be dereferenced, add non-null Confirms.
480  *
481  * @param ptr    a node representing an address
482  * @param block  the block of the dereferencing instruction
483  * @param env    environment
484  */
485 static void insert_non_null(ir_node *ptr, ir_node *block, env_t *env) {
486         const ir_edge_t *edge, *next;
487         ir_node         *c = NULL;
488
489         foreach_out_edge_safe(ptr, edge, next) {
490                 ir_node *succ = get_edge_src_irn(edge);
491                 int     pos;
492                 ir_node *blk;
493
494
495                 /* for now, we place a Confirm only in front of a Cmp */
496                 if (! is_Cmp(succ))
497                         continue;
498
499                 pos = get_edge_src_pos(edge);
500                 blk = get_effective_use_block(succ, pos);
501
502                 if (block_dominates(block, blk)) {
503                         /*
504                          * Ok, we found a usage of ptr in a block
505                          * dominated by the Load/Store block.
506                          * We can replace the input with a Confirm(ptr, !=, NULL).
507                          */
508                         if (c == NULL) {
509                                 ir_mode *mode = get_irn_mode(ptr);
510                                 c = new_Const(get_mode_null(mode));
511
512                                 c = new_r_Confirm(block, ptr, c, pn_Cmp_Lg);
513                         }
514
515                         set_irn_n(succ, pos, c);
516                         DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, succ, c));
517
518                         env->num_non_null += 1;
519                         env->num_confirms += 1;
520                 }
521         }
522 }  /* insert_non_null */
523
524 /**
525  * Pre-walker: Called for every node to insert Confirm nodes
526  */
527 static void insert_Confirm(ir_node *node, void *env) {
528         ir_node *ptr;
529
530         switch (get_irn_opcode(node)) {
531         case iro_Block:
532                 insert_Confirm_in_block(node, env);
533                 break;
534         case iro_Load:
535                 ptr = get_Load_ptr(node);
536                 if (! is_non_null_Confirm(ptr))
537                         insert_non_null(ptr, get_nodes_block(node), env);
538                 break;
539         case iro_Store:
540                 ptr = get_Store_ptr(node);
541                 if (! is_non_null_Confirm(ptr))
542                         insert_non_null(ptr, get_nodes_block(node), env);
543                 break;
544         default:
545                 break;
546         }
547 }  /* insert_Confirm */
548
549 /*
550  * Construct Confirm nodes
551  */
552 void construct_confirms(ir_graph *irg) {
553         env_t env;
554         int edges_active = edges_activated(irg);
555
556
557         FIRM_DBG_REGISTER(dbg, "firm.ana.confirm");
558
559         remove_critical_cf_edges(irg);
560
561         /* we need dominance info */
562         assure_doms(irg);
563
564         assert(get_irg_pinned(irg) == op_pin_state_pinned &&
565                "Nodes must be placed to insert Confirms");
566
567         if (! edges_active) {
568                 /* We need edges */
569                 edges_activate(irg);
570         }
571
572         env.num_confirms = 0;
573         env.num_consts   = 0;
574         env.num_eq       = 0;
575         env.num_non_null = 0;
576
577         if (get_opt_global_null_ptr_elimination()) {
578                 /* do global NULL test elimination */
579                 irg_walk_graph(irg, insert_Confirm, NULL, &env);
580         } else {
581                 /* now, visit all blocks and add Confirms where possible */
582                 irg_block_walk_graph(irg, insert_Confirm_in_block, NULL, &env);
583         }
584
585         if (env.num_confirms | env.num_consts | env.num_eq) {
586                 /* we have add nodes or changed DF edges */
587                 set_irg_outs_inconsistent(irg);
588
589                 /* the new nodes are not in the loop info */
590                 set_irg_loopinfo_inconsistent(irg);
591         }
592
593         DB((dbg, LEVEL_1, "# Confirms inserted : %u\n", env.num_confirms));
594         DB((dbg, LEVEL_1, "# Const replacements: %u\n", env.num_consts));
595         DB((dbg, LEVEL_1, "# node equalities   : %u\n", env.num_eq));
596         DB((dbg, LEVEL_1, "# non-null Confirms : %u\n", env.num_non_null));
597
598         /* deactivate edges if they where off */
599         if (! edges_active)
600                 edges_deactivate(irg);
601 }  /* construct_confirms */
602
603 /* Construct a pass. */
604 ir_graph_pass_t *construct_confirms_pass(const char *name, int verify, int dump) {
605         return def_graph_pass(name ? name : "confirm", verify, dump, construct_confirms);
606 }  /* construct_confirms_pass */
607
608 #if 0
609 /**
610  * Post-walker: Remove Confirm nodes
611  */
612 static void rem_Confirm(ir_node *n, void *env) {
613         (void) env;
614         if (is_Confirm(n)) {
615                 ir_node *value = get_Confirm_value(n);
616                 if (value != n)
617                         exchange(n, value);
618                 else {
619                         /*
620                          * Strange: a Confirm is its own bound. This can happen
621                          * in dead blocks when Phi nodes are already removed.
622                          */
623                         exchange(n, new_Bad());
624                 }
625         }
626 }  /* rem_Confirm */
627 #endif
628
629 /*
630  * Remove all Confirm nodes from a graph.
631  */
632 void remove_confirms(ir_graph *irg) {
633         int rem = get_opt_remove_confirm();
634
635         set_opt_remove_confirm(1);
636         optimize_graph_df(irg);
637         set_opt_remove_confirm(rem);
638 }  /* remove_confirms */
639
640 /* Construct a pass. */
641 ir_graph_pass_t *remove_confirms_pass(const char *name, int verify, int dump) {
642         return def_graph_pass(name ? name : "rem_confirm", verify, dump, remove_confirms);
643 }  /* remove_confirms_pass */