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