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