57b8f3c5cce991f2ca0d3ab79aff275d84411c96
[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 } 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, block, mode, tv, tp);
113                         }
114
115                         set_irn_n(succ, pos, c);
116                         DBG_OPT_CONFIRM_C(irn, c);
117 //                      ir_printf("1 Replacing input %d of node %n with %n\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(mode_b, 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 %n with %n\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(mode_b, tarval_b_true);
212                                         ir_node *c_false = new_Const(mode_b, tarval_b_false);
213                                         c_b = new_r_Confirm(current_ir_graph, cond_block, selector,
214                                                 pnc == pn_Cond_true ? c_true : c_false, pn_Cmp_Eq);
215                                         c_o = new_r_Confirm(current_ir_graph, cond_block, selector,
216                                                 pnc == pn_Cond_false ? c_true : c_false, pn_Cmp_Eq);
217                                 }
218                                 for (i = n - 1; i >= 0; --i) {
219                                         ir_node *pred_blk = get_Block_cfgpred_block(user_blk, i);
220
221                                         if (block_dominates(block, pred_blk))
222                                                 in[i] = c_b;
223                                         else
224                                                 in[i] = c_o;
225                                 }
226                                 phi = new_r_Phi(current_ir_graph, user_blk, n, in, mode_b);
227                                 set_irn_n(user, pos, phi);
228                         }
229                 }
230         }
231 }
232
233 /**
234  * Handle an IF-branch.
235  *
236  * @param block   the block which is entered by the branch
237  * @param cmp     the Cmp node expressing the branch condition
238  * @param pnc     the Compare relation for taking this branch
239  * @param env     statistical environment
240  */
241 static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
242         ir_node *left  = get_Cmp_left(cmp);
243         ir_node *right = get_Cmp_right(cmp);
244         ir_node *cond_block;
245         ir_op *op;
246         const ir_edge_t *edge, *next;
247
248         /* Beware of Bads */
249         if (is_Bad(left) || is_Bad(right))
250                 return;
251
252         op = get_irn_op(left);
253
254         /* Do not create Confirm nodes for Cmp(Const, Const) constructs.
255            These are removed anyway */
256         if (op == op_Const && is_Const(right))
257                 return;
258
259         /* try to place the constant on the right side for a Confirm */
260         if (op == op_Const || op == op_SymConst) {
261                 ir_node *t = left;
262
263                 left  = right;
264                 right = t;
265
266                 pnc = get_inversed_pnc(pnc);
267         }
268
269         /*
270          * First case: both values are identical.
271          * replace the left one by the right (potentially const) one.
272          */
273         if (pnc == pn_Cmp_Eq) {
274                 cond_block = get_Block_cfgpred_block(block, 0);
275                 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
276                         ir_node *user = get_edge_src_irn(edge);
277                         int     pos   = get_edge_src_pos(edge);
278                         ir_node *blk  = get_effective_use_block(user, pos);
279
280                         next = get_irn_out_edge_next(left, edge);
281                         if (block_dominates(block, blk)) {
282                                 /*
283                                  * Ok, we found a usage of left in a block
284                                  * dominated by the branch block.
285                                  * We can replace the input with right.
286                                  */
287                                 set_irn_n(user, pos, right);
288                                 DBG_OPT_CONFIRM(left, right);
289
290                                 DB((dbg, LEVEL_2, "Replacing input %d of node %n with %n\n", pos, user, right));
291
292                                 env->num_eq += 1;
293                         } else if (block_dominates(blk, cond_block)) {
294                                 if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
295                                         /*
296                                          * left == Const and we found a movable user of left in a
297                                          * dominator of the Cond block
298                                          */
299                                         const ir_edge_t *edge, *next;
300                                         for (edge = get_irn_out_edge_first(user); edge; edge = next) {
301                                                 ir_node *usr_of_usr = get_edge_src_irn(edge);
302                                                 int      npos = get_edge_src_pos(edge);
303                                                 ir_node *blk  = get_effective_use_block(usr_of_usr, npos);
304
305                                                 next = get_irn_out_edge_next(user, edge);
306                                                 if (block_dominates(block, blk)) {
307                                                         /*
308                                                          * The user of the user is dominated by our true/false
309                                                          * block. So, create a copy of user WITH the constant
310                                                          * replacing it's pos'th input.
311                                                          *
312                                                          * This is always good for unop's and might be good
313                                                          * for binops.
314                                                          *
315                                                          * If user has other user in the false/true block, code
316                                                          * placement will move it down.
317                                                          * If there are users in cond block or upper, we create
318                                                          * "redundant ops", because one will have a const op,
319                                                          * the other no const ...
320                                                          */
321                                                         ir_node *new_op = exact_copy(user);
322                                                         set_nodes_block(new_op, block);
323                                                         set_irn_n(new_op, pos, right);
324                                                         set_irn_n(usr_of_usr, npos, new_op);
325                                                 }
326                                         }
327                                 }
328                         }
329                 }
330         } else { /* not pn_Cmp_Eq cases */
331                 ir_node *c = NULL;
332
333                 for (edge = get_irn_out_edge_first(left); edge; edge = next) {
334                         ir_node *succ = get_edge_src_irn(edge);
335                         int     pos   = get_edge_src_pos(edge);
336                         ir_node *blk  = get_effective_use_block(succ, pos);
337
338                         next = get_irn_out_edge_next(left, edge);
339                         if (block_dominates(block, blk)) {
340                                 /*
341                                  * Ok, we found a usage of left in a block
342                                  * dominated by the branch block.
343                                  * We can replace the input with a Confirm(left, pnc, right).
344                                  */
345                                 if (! c)
346                                         c = new_r_Confirm(current_ir_graph, block, left, right, pnc);
347
348                                 pos = get_edge_src_pos(edge);
349                                 set_irn_n(succ, pos, c);
350                                 DB((dbg, LEVEL_2, "Replacing input %d of node %n with %n\n", pos, succ, c));
351
352                                 env->num_confirms += 1;
353                         }
354                 }
355         }
356 }  /* handle_if */
357
358 /**
359  * Pre-block-walker: Called for every block to insert Confirm nodes
360  */
361 static void insert_Confirm_in_block(ir_node *block, void *env) {
362         ir_node *cond, *proj, *selector;
363         ir_mode *mode;
364
365         /*
366          * we can only handle blocks with only ONE control flow
367          * predecessor yet.
368          */
369         if (get_Block_n_cfgpreds(block) != 1)
370                 return;
371
372         proj = get_Block_cfgpred(block, 0);
373         if (! is_Proj(proj))
374                 return;
375
376         cond = get_Proj_pred(proj);
377         if (! is_Cond(cond))
378                 return;
379
380         selector = get_Cond_selector(cond);
381         mode = get_irn_mode(selector);
382
383         if (mode == mode_b) {
384                 ir_node *cmp;
385                 pn_Cmp pnc;
386
387                 handle_modeb(block, selector, get_Proj_proj(proj), env);
388
389                 /* this should be an IF, check this */
390                 if (! is_Proj(selector))
391                         return;
392
393                 cmp = get_Proj_pred(selector);
394                 if (! is_Cmp(cmp))
395                         return;
396
397                 pnc = get_Proj_proj(selector);
398
399                 if (get_Proj_proj(proj) != pn_Cond_true) {
400                         /* it's the false branch */
401                         pnc = get_negated_pnc(pnc, mode);
402                 }
403                 DB((dbg, LEVEL_2, "At %+F using %+F Confirm %=\n", block, cmp, pnc));
404
405                 handle_if(block, cmp, pnc, env);
406         } else if (mode_is_int(mode)) {
407                 long proj_nr = get_Proj_proj(proj);
408
409                 /* this is a CASE, but we cannot handle the default case */
410                 if (proj_nr == get_Cond_defaultProj(cond))
411                         return;
412
413                 handle_case(block, get_Cond_selector(cond), proj_nr, env);
414         }
415 }  /* insert_Confirm_in_block */
416
417 /**
418  * Checks if a node is a non-null Confirm.
419  */
420 static int is_non_null_Confirm(const ir_node *ptr) {
421         /*
422          * While a SymConst is not a Confirm, it is non-null
423          * anyway. This helps to reduce the number of
424          * constructed Confirms.
425          */
426         if (is_SymConst_addr_ent(ptr))
427                 return 1;
428         for (;;) {
429                 if (! is_Confirm(ptr))
430                         return 0;
431                 if (get_Confirm_cmp(ptr) == pn_Cmp_Lg) {
432                         ir_node *bound = get_Confirm_bound(ptr);
433
434                         if (is_Const(bound) && is_Const_null(bound))
435                                 return 1;
436                 }
437                 ptr = get_Confirm_value(ptr);
438         }
439 }  /* is_non_null_Confirm */
440
441 /**
442  * The given pointer will be dereferenced, add non-null Confirms.
443  *
444  * @param ptr    a node representing an address
445  * @param block  the block of the dereferencing instruction
446  * @param env    environment
447  */
448 static void insert_non_null(ir_node *ptr, ir_node *block, env_t *env) {
449         const ir_edge_t *edge, *next;
450         ir_node         *c = NULL;
451
452         foreach_out_edge_safe(ptr, edge, next) {
453                 ir_node *succ = get_edge_src_irn(edge);
454                 int     pos;
455                 ir_node *blk;
456
457
458                 if (is_Confirm(succ)) {
459                         /* beware of loops */
460                         continue;
461                 }
462
463                 if ((is_Load(succ) || is_Store(succ)) &&
464                         get_nodes_block(succ) == block) {
465                         /* Ignore Loads and Store in the same block for now,
466                            because we are not sure if they are dominated.
467                            This is not a bad restriction: if exception flow is
468                            present, they are in other blocks either. */
469                         continue;
470                 }
471
472                 pos = get_edge_src_pos(edge);
473                 blk = get_effective_use_block(succ, pos);
474
475                 if (block_dominates(block, blk) && !is_non_null_Confirm(get_irn_n(succ, pos))) {
476                         /*
477                          * Ok, we found a usage of ptr in a block
478                          * dominated by the Load/Store block.
479                          * We can replace the input with a Confirm(ptr, !=, NULL).
480                          */
481                         if (c == NULL) {
482                                 ir_mode *mode = get_irn_mode(ptr);
483                                 c = new_Const(mode, get_mode_null(mode));
484
485                                 c = new_r_Confirm(current_ir_graph, block, ptr, c, pn_Cmp_Lg);
486                         }
487
488                         set_irn_n(succ, pos, c);
489                         DB((dbg, LEVEL_2, "Replacing input %d of node %n with %n\n", pos, succ, c));
490
491
492                         env->num_confirms += 1;
493                 }
494         }
495 }  /* insert_non_null */
496
497 /**
498  * Pre-walker: Called for every node to insert Confirm nodes
499  */
500 static void insert_Confirm(ir_node *node, void *env) {
501         ir_node *ptr;
502
503         switch (get_irn_opcode(node)) {
504         case iro_Block:
505                 insert_Confirm_in_block(node, env);
506                 break;
507         case iro_Load:
508                 ptr = get_Load_ptr(node);
509                 if (! is_non_null_Confirm(ptr))
510                         insert_non_null(ptr, get_nodes_block(node), env);
511                 break;
512         case iro_Store:
513                 ptr = get_Store_ptr(node);
514                 if (! is_non_null_Confirm(ptr))
515                         insert_non_null(ptr, get_nodes_block(node), env);
516                 break;
517         default:
518                 break;
519         }
520 }  /* insert_Confirm */
521
522 /*
523  * Construct Confirm nodes
524  */
525 void construct_confirms(ir_graph *irg) {
526         env_t env;
527         int edges_active = edges_activated(irg);
528
529
530         FIRM_DBG_REGISTER(dbg, "firm.ana.confirm");
531
532         remove_critical_cf_edges(irg);
533
534         /* we need dominance info */
535         assure_doms(irg);
536
537         assert(get_irg_pinned(irg) == op_pin_state_pinned &&
538                "Nodes must be placed to insert Confirms");
539
540         if (! edges_active) {
541                 /* We need edges */
542                 edges_activate(irg);
543         }
544
545         env.num_confirms = 0;
546         env.num_consts   = 0;
547         env.num_eq       = 0;
548
549         if (get_opt_global_null_ptr_elimination()) {
550                 /* do global NULL test elimination */
551                 irg_walk_graph(irg, insert_Confirm, NULL, &env);
552         } else {
553                 /* now, visit all blocks and add Confirms where possible */
554                 irg_block_walk_graph(irg, insert_Confirm_in_block, NULL, &env);
555         }
556
557         if (env.num_confirms | env.num_consts | env.num_eq) {
558                 /* we have add nodes or changed DF edges */
559                 set_irg_outs_inconsistent(irg);
560
561                 /* the new nodes are not in the loop info */
562                 set_irg_loopinfo_inconsistent(irg);
563         }
564
565         DB((dbg, LEVEL_1, "# Confirms inserted : %u\n", env.num_confirms));
566         DB((dbg, LEVEL_1, "# Const replacements: %u\n", env.num_consts));
567         DB((dbg, LEVEL_1, "# node equalities   : %u\n", env.num_eq));
568
569         /* deactivate edges if they where off */
570         if (! edges_active)
571                 edges_deactivate(irg);
572 }  /* construct_confirms */
573
574 #if 0
575 /**
576  * Post-walker: Remove Confirm nodes
577  */
578 static void rem_Confirm(ir_node *n, void *env) {
579         (void) env;
580         if (is_Confirm(n)) {
581                 ir_node *value = get_Confirm_value(n);
582                 if (value != n)
583                         exchange(n, value);
584                 else {
585                         /*
586                          * Strange: a Confirm is its own bound. This can happen
587                          * in dead blocks when Phi nodes are already removed.
588                          */
589                         exchange(n, new_Bad());
590                 }
591         }
592 }  /* rem_Confirm */
593 #endif
594
595 /*
596  * Remove all Confirm nodes from a graph.
597  */
598 void remove_confirms(ir_graph *irg) {
599         int rem = get_opt_remove_confirm();
600
601         set_opt_remove_confirm(1);
602         optimize_graph_df(irg);
603         set_opt_remove_confirm(rem);
604 }  /* remove_confirms */