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