fix condeval ssa recondstruction
[libfirm] / ir / opt / condeval.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   Partial condition evaluation
23  * @date    10. Sep. 2006
24  * @author  Christoph Mallon, Matthias Braun
25  * @version $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "iroptimize.h"
32
33 #include <assert.h>
34 #include "array_t.h"
35 #include "debug.h"
36 #include "ircons.h"
37 #include "irgmod.h"
38 #include "irgopt.h"
39 #include "irgwalk.h"
40 #include "irnode.h"
41 #include "irnode_t.h"
42 #include "iredges.h"
43 #include "iredges_t.h"
44 #include "irtools.h"
45 #include "irgraph.h"
46 #include "tv.h"
47 #include "opt_confirms.h"
48
49 #undef AVOID_PHIB
50
51 DEBUG_ONLY(static firm_dbg_module_t *dbg);
52
53 /**
54  * Add the new predecessor x to node node, which is either a Block or a Phi
55  */
56 static void add_pred(ir_node* node, ir_node* x)
57 {
58         ir_node** ins;
59         int n;
60         int i;
61
62         assert(is_Block(node) || is_Phi(node));
63
64         n = get_irn_arity(node);
65         NEW_ARR_A(ir_node*, ins, n + 1);
66         for (i = 0; i < n; i++)
67                 ins[i] = get_irn_n(node, i);
68         ins[n] = x;
69         set_irn_in(node, n + 1, ins);
70 }
71
72 static ir_node *ssa_second_def;
73 static ir_node *ssa_second_def_block;
74
75 static ir_node *search_def_and_create_phis(ir_node *block, ir_mode *mode,
76                                            int first)
77 {
78         int i;
79         int n_cfgpreds;
80         ir_graph *irg;
81         ir_node *phi;
82         ir_node **in;
83
84         /* This is needed because we create bads sometimes */
85         if(is_Bad(block))
86                 return new_Bad();
87
88         /* already processed this block? */
89         if(irn_visited(block)) {
90                 ir_node *value = (ir_node*) get_irn_link(block);
91                 return value;
92         }
93
94         /* the other defs can't be marked for cases where a user of the original
95          * value is in the same block as the alternative definition.
96          * In this case we mustn't use the alternative definition.
97          * So we keep a flag that indicated wether we walked at least 1 block
98          * away and may use the alternative definition */
99         if (block == ssa_second_def_block && !first) {
100                 return ssa_second_def;
101         }
102
103         irg = get_irn_irg(block);
104         assert(block != get_irg_start_block(irg));
105
106         /* a Block with only 1 predecessor needs no Phi */
107         n_cfgpreds = get_Block_n_cfgpreds(block);
108         if(n_cfgpreds == 1) {
109                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
110                 ir_node *value      = search_def_and_create_phis(pred_block, mode, 0);
111
112                 set_irn_link(block, value);
113                 mark_irn_visited(block);
114                 return value;
115         }
116
117         /* create a new Phi */
118         NEW_ARR_A(ir_node*, in, n_cfgpreds);
119         for(i = 0; i < n_cfgpreds; ++i)
120                 in[i] = new_Unknown(mode);
121
122         phi = new_r_Phi(irg, block, n_cfgpreds, in, mode);
123         set_irn_link(block, phi);
124         mark_irn_visited(block);
125
126         /* set Phi predecessors */
127         for(i = 0; i < n_cfgpreds; ++i) {
128                 ir_node *pred_block = get_Block_cfgpred_block(block, i);
129                 ir_node *pred_val   = search_def_and_create_phis(pred_block, mode, 0);
130
131                 set_irn_n(phi, i, pred_val);
132         }
133
134         return phi;
135 }
136
137 /**
138  * Given a set of values this function constructs SSA-form for the users of the
139  * first value (the users are determined through the out-edges of the value).
140  * Uses the irn_visited flags. Works without using the dominance tree.
141  */
142 static void construct_ssa(ir_node *orig_block, ir_node *orig_val,
143                           ir_node *second_block, ir_node *second_val)
144 {
145         ir_graph *irg;
146         ir_mode *mode;
147         const ir_edge_t *edge;
148         const ir_edge_t *next;
149
150         irg = get_irn_irg(orig_val);
151         inc_irg_visited(irg);
152
153         mode = get_irn_mode(orig_val);
154         set_irn_link(orig_block, orig_val);
155         mark_irn_visited(orig_block);
156
157         ssa_second_def_block = second_block;
158         ssa_second_def       = second_val;
159
160         /* Only fix the users of the first, i.e. the original node */
161         foreach_out_edge_safe(orig_val, edge, next) {
162                 ir_node *user = get_edge_src_irn(edge);
163                 int j = get_edge_src_pos(edge);
164                 ir_node *user_block = get_nodes_block(user);
165                 ir_node *newval;
166
167                 /* ignore keeps */
168                 if (is_End(user))
169                         continue;
170
171                 DB((dbg, LEVEL_3, ">>> Fixing user %+F (pred %d == %+F)\n", user, j, get_irn_n(user, j)));
172
173                 if(is_Phi(user)) {
174                         ir_node *pred_block = get_Block_cfgpred_block(user_block, j);
175                         newval = search_def_and_create_phis(pred_block, mode, 1);
176                 } else {
177                         newval = search_def_and_create_phis(user_block, mode, 1);
178                 }
179
180                 /* don't fix newly created Phis from the SSA construction */
181                 if (newval != user) {
182                         DB((dbg, LEVEL_4, ">>>> Setting input %d of %+F to %+F\n", j, user, newval));
183                         set_irn_n(user, j, newval);
184                 }
185         }
186 }
187
188 static void split_critical_edge(ir_node *block, int pos) {
189         ir_graph *irg = get_irn_irg(block);
190         ir_node *in[1];
191         ir_node *new_block;
192         ir_node *new_jmp;
193
194         in[0] = get_Block_cfgpred(block, pos);
195         new_block = new_r_Block(irg, 1, in);
196         new_jmp = new_r_Jmp(irg, new_block);
197         set_Block_cfgpred(block, pos, new_jmp);
198 }
199
200 typedef struct condeval_env_t {
201         ir_node       *true_block;
202         ir_node       *cmp;        /**< The Compare node that might be partial evaluated */
203         pn_Cmp         pnc;        /**< The Compare mode of the Compare node. */
204         ir_node       *cnst;
205         tarval        *tv;
206         ir_visited_t   visited_nr;
207
208         ir_node       *cnst_pred;   /**< the block before the constant */
209         int            cnst_pos;    /**< the pos to the constant block (needed to
210                                           kill that edge later) */
211 } condeval_env_t;
212
213 static ir_node *copy_and_fix_node(const condeval_env_t *env, ir_node *block,
214                                   ir_node *copy_block, int j, ir_node *node) {
215         int      i, arity;
216         ir_node *copy;
217
218         /* we can evaluate Phis right now, all other nodes get copied */
219         if (is_Phi(node)) {
220                 copy = get_Phi_pred(node, j);
221                 /* we might have to evaluate a Phi-cascade */
222                 if(get_irn_visited(copy) >= env->visited_nr) {
223                         copy = get_irn_link(copy);
224                 }
225         } else {
226                 copy = exact_copy(node);
227                 set_nodes_block(copy, copy_block);
228
229                 assert(get_irn_mode(copy) != mode_X);
230
231                 arity = get_irn_arity(copy);
232                 for(i = 0; i < arity; ++i) {
233                         ir_node *pred     = get_irn_n(copy, i);
234                         ir_node *new_pred;
235
236                         if(get_nodes_block(pred) != block)
237                                 continue;
238
239                         if(get_irn_visited(pred) >= env->visited_nr) {
240                                 new_pred = get_irn_link(pred);
241                         } else {
242                                 new_pred = copy_and_fix_node(env, block, copy_block, j, pred);
243                         }
244                         DB((dbg, LEVEL_2, ">> Set Pred of %+F to %+F\n", copy, new_pred));
245                         set_irn_n(copy, i, new_pred);
246                 }
247         }
248
249         set_irn_link(node, copy);
250         set_irn_visited(node, env->visited_nr);
251
252         return copy;
253 }
254
255 static void copy_and_fix(const condeval_env_t *env, ir_node *block,
256                          ir_node *copy_block, int j) {
257         const ir_edge_t *edge;
258
259         /* Look at all nodes in the cond_block and copy them into pred */
260         foreach_out_edge(block, edge) {
261                 ir_node *node = get_edge_src_irn(edge);
262                 ir_node *copy;
263                 ir_mode *mode;
264
265                 if (is_Block(node)) {
266                         /* Block->Block edge, should be the MacroBlock edge */
267                         assert(get_Block_MacroBlock(node) == block && "Block->Block edge found");
268                         continue;
269                 }
270
271                 /* ignore control flow */
272                 mode = get_irn_mode(node);
273                 if (mode == mode_X || is_Cond(node))
274                         continue;
275 #ifdef AVOID_PHIB
276                 /* we may not copy mode_b nodes, because this could produce Phi with
277                  * mode_bs which can't be handled in all backends. Instead we duplicate
278                  * the node and move it to its users */
279                 if (mode == mode_b) {
280                         const ir_edge_t *edge, *next;
281                         ir_node *pred;
282                         int      pn;
283
284                         assert(is_Proj(node));
285
286                         pred = get_Proj_pred(node);
287                         pn   = get_Proj_proj(node);
288
289                         foreach_out_edge_safe(node, edge, next) {
290                                 ir_node *cmp_copy;
291                                 ir_node *user       = get_edge_src_irn(edge);
292                                 int pos             = get_edge_src_pos(edge);
293                                 ir_node *user_block = get_nodes_block(user);
294
295                                 if(user_block == block)
296                                         continue;
297
298                                 cmp_copy = exact_copy(pred);
299                                 set_nodes_block(cmp_copy, user_block);
300                                 copy = new_r_Proj(current_ir_graph, user_block, cmp_copy, mode_b, pn);
301                                 set_irn_n(user, pos, copy);
302                         }
303                         continue;
304                 }
305 #endif
306
307                 copy = copy_and_fix_node(env, block, copy_block, j, node);
308
309                 /* we might hit values in blocks that have already been processed by a
310                  * recursive find_phi_with_const() call */
311                 assert(get_irn_visited(copy) <= env->visited_nr);
312                 if(get_irn_visited(copy) >= env->visited_nr) {
313                         ir_node *prev_copy = get_irn_link(copy);
314                         if(prev_copy != NULL)
315                                 set_irn_link(node, prev_copy);
316                 }
317         }
318
319         /* fix data-flow (and reconstruct SSA if needed) */
320         foreach_out_edge(block, edge) {
321                 ir_node *node = get_edge_src_irn(edge);
322                 ir_node *copy_node;
323                 ir_mode *mode;
324
325                 if (is_Block(node)) {
326                         /* Block->Block edge, should be the MacroBlock edge */
327                         assert(get_Block_MacroBlock(node) == block && "Block->Block edge found");
328                         continue;
329                 }
330
331                 mode = get_irn_mode(node);
332                 if (mode == mode_X || is_Cond(node))
333                         continue;
334 #ifdef AVOID_PHIB
335                 if (mode == mode_b)
336                         continue;
337 #endif
338
339                 DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));
340
341                 copy_node = get_irn_link(node);
342                 construct_ssa(block, node, copy_block, copy_node);
343         }
344 }
345
346 /**
347  * returns whether the cmp evaluates to true or false, or can't be evaluated!
348  * 1: true, 0: false, -1: can't evaluate
349  *
350  * @param pnc       the compare mode of the Compare
351  * @param tv_left   the left tarval
352  * @param tv_right  the right tarval
353  */
354 static int eval_cmp_tv(pn_Cmp pnc, tarval *tv_left, tarval *tv_right) {
355         pn_Cmp cmp_result = tarval_cmp(tv_left, tv_right);
356
357         /* does the compare evaluate to true? */
358         if (cmp_result == pn_Cmp_False)
359                 return -1;
360         if ((cmp_result & pnc) != cmp_result)
361                 return 0;
362
363         return 1;
364 }
365
366 /**
367  * returns whether the cmp evaluates to true or false, or can't be evaluated!
368  * 1: true, 0: false, -1: can't evaluate
369  *
370  * @param env      the environment
371  * @param cand     the candidate node, either a Const or a Confirm
372  */
373 static int eval_cmp(condeval_env_t *env, ir_node *cand) {
374         if (is_Const(cand)) {
375                 tarval *tv_cand   = get_Const_tarval(cand);
376                 tarval *tv_cmp    = get_Const_tarval(env->cnst);
377
378                 return eval_cmp_tv(env->pnc, tv_cand, tv_cmp);
379         } else { /* a Confirm */
380                 tarval *res = computed_value_Cmp_Confirm(env->cmp, cand, env->cnst, env->pnc);
381
382                 if (res == tarval_bad)
383                         return -1;
384                 return res == tarval_b_true;
385         }
386 }
387
388 /**
389  * Check for Const or Confirm with Const.
390  */
391 static int is_Const_or_Confirm(const ir_node *node) {
392         if (is_Confirm(node))
393                 node = get_Confirm_bound(node);
394         return is_Const(node);
395 }
396
397 /**
398  * get the tarval of a Const or Confirm with
399  */
400 static tarval *get_Const_or_Confirm_tarval(const ir_node *node) {
401         if (is_Confirm(node)) {
402                 if (get_Confirm_bound(node))
403                         node = get_Confirm_bound(node);
404         }
405         return get_Const_tarval(node);
406 }
407
408 static ir_node *find_const_or_confirm(condeval_env_t *env, ir_node *jump, ir_node *value)
409 {
410         ir_node *block = get_nodes_block(jump);
411
412         if (irn_visited(value))
413                 return NULL;
414         mark_irn_visited(value);
415
416         if (is_Const_or_Confirm(value)) {
417                 if (eval_cmp(env, value) <= 0) {
418                         return NULL;
419                 }
420
421                 DB((
422                         dbg, LEVEL_1,
423                         "> Found condition evaluation candidate %+F->%+F\n",
424                         env->true_block, block
425                 ));
426
427                 /* adjust true_block to point directly towards our jump */
428                 add_pred(env->true_block, jump);
429
430                 split_critical_edge(env->true_block, 0);
431
432                 /* we need a bigger visited nr when going back */
433                 env->visited_nr++;
434
435                 return block;
436         }
437
438         if(is_Phi(value)) {
439                 int i, arity;
440
441                 /* the Phi has to be in the same Block as the Jmp */
442                 if(get_nodes_block(value) != block) {
443                         return NULL;
444                 }
445
446                 arity = get_irn_arity(value);
447                 for(i = 0; i < arity; ++i) {
448                         ir_node *copy_block;
449                         ir_node *phi_pred = get_Phi_pred(value, i);
450                         ir_node *cfgpred  = get_Block_cfgpred(block, i);
451
452                         copy_block = find_const_or_confirm(env, cfgpred, phi_pred);
453                         if(copy_block == NULL)
454                                 continue;
455
456                         /* copy duplicated nodes in copy_block and fix SSA */
457                         copy_and_fix(env, block, copy_block, i);
458
459                         if(copy_block == get_nodes_block(cfgpred)) {
460                                 env->cnst_pred = block;
461                                 env->cnst_pos  = i;
462                         }
463
464                         /* return now as we can't process more possibilities in 1 run */
465                         return copy_block;
466                 }
467         }
468
469         return NULL;
470 }
471
472 static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
473                                ir_node *value)
474 {
475         ir_node *block = get_nodes_block(jump);
476
477         if(irn_visited(value)) {
478                 return NULL;
479         }
480         mark_irn_visited(value);
481
482         if (is_Const_or_Confirm(value)) {
483                 tarval *tv = get_Const_or_Confirm_tarval(value);
484
485                 if (tv != env->tv)
486                         return NULL;
487
488                 DB((
489                         dbg, LEVEL_1,
490                         "> Found condition evaluation candidate %+F->%+F\n",
491                         env->true_block, block
492                 ));
493
494                 /* adjust true_block to point directly towards our jump */
495                 add_pred(env->true_block, jump);
496
497                 split_critical_edge(env->true_block, 0);
498
499                 /* we need a bigger visited nr when going back */
500                 env->visited_nr++;
501
502                 return block;
503         }
504         if(is_Phi(value)) {
505                 int i, arity;
506
507                 /* the Phi has to be in the same Block as the Jmp */
508                 if(get_nodes_block(value) != block)
509                         return NULL;
510
511                 arity = get_irn_arity(value);
512                 for(i = 0; i < arity; ++i) {
513                         ir_node *copy_block;
514                         ir_node *phi_pred = get_Phi_pred(value, i);
515                         ir_node *cfgpred  = get_Block_cfgpred(block, i);
516
517                         copy_block = find_candidate(env, cfgpred, phi_pred);
518                         if(copy_block == NULL)
519                                 continue;
520
521                         /* copy duplicated nodes in copy_block and fix SSA */
522                         copy_and_fix(env, block, copy_block, i);
523
524                         if(copy_block == get_nodes_block(cfgpred)) {
525                                 env->cnst_pred = block;
526                                 env->cnst_pos  = i;
527                         }
528
529                         /* return now as we can't process more possibilities in 1 run */
530                         return copy_block;
531                 }
532         }
533         if(is_Proj(value)) {
534                 ir_node *left;
535                 ir_node *right;
536                 int      pnc;
537                 ir_node *cmp = get_Proj_pred(value);
538                 if(!is_Cmp(cmp))
539                         return NULL;
540
541                 left  = get_Cmp_left(cmp);
542                 right = get_Cmp_right(cmp);
543                 pnc   = get_Proj_proj(value);
544
545                 /* we assume that the constant is on the right side, swap left/right
546                  * if needed */
547                 if(is_Const(left)) {
548                         ir_node *t = left;
549                         left       = right;
550                         right      = t;
551
552                         pnc        = get_inversed_pnc(pnc);
553                 }
554
555                 if(!is_Const(right))
556                         return 0;
557
558                 if(get_nodes_block(left) != block) {
559                         return 0;
560                 }
561
562                 /* negate condition when we're looking for the false block */
563                 if(env->tv == tarval_b_false) {
564                         pnc = get_negated_pnc(pnc, get_irn_mode(right));
565                 }
566
567                 /* (recursively) look if a pred of a Phi is a constant or a Confirm */
568                 env->cmp  = cmp;
569                 env->pnc  = pnc;
570                 env->cnst = right;
571
572                 return find_const_or_confirm(env, jump, left);
573         }
574
575         return NULL;
576 }
577
578 /**
579  * Block-walker: searches for the following construct
580  *
581  *  Const or Phi with constants
582  *           |
583  *          Cmp
584  *           |
585  *         Cond
586  *          /
587  *       ProjX
588  *        /
589  *     Block
590  */
591 static void cond_eval(ir_node* block, void* data)
592 {
593         condeval_env_t env;
594         int *changed = data;
595         ir_node *selector;
596         ir_node *projx;
597         ir_node *cond;
598         ir_node *copy_block;
599         int      selector_evaluated;
600         const ir_edge_t *edge, *next;
601         ir_node* bad;
602         size_t   cnst_pos;
603
604         if(get_Block_n_cfgpreds(block) != 1)
605                 return;
606
607         projx = get_Block_cfgpred(block, 0);
608         if (!is_Proj(projx))
609                 return;
610         assert(get_irn_mode(projx) == mode_X);
611
612         cond = get_Proj_pred(projx);
613         if (!is_Cond(cond))
614                 return;
615
616         selector = get_Cond_selector(cond);
617         /* TODO handle switch Conds */
618         if (get_irn_mode(selector) != mode_b)
619                 return;
620
621         /* handle cases that can be immediately evaluated */
622         selector_evaluated = -1;
623         if(is_Proj(selector)) {
624                 ir_node *cmp = get_Proj_pred(selector);
625                 if(is_Cmp(cmp)) {
626                         ir_node *left  = get_Cmp_left(cmp);
627                         ir_node *right = get_Cmp_right(cmp);
628                         if(is_Const(left) && is_Const(right)) {
629                                 int     pnc      = get_Proj_proj(selector);
630                                 tarval *tv_left  = get_Const_tarval(left);
631                                 tarval *tv_right = get_Const_tarval(right);
632
633                                 selector_evaluated = eval_cmp_tv(pnc, tv_left, tv_right);
634                                 if(selector_evaluated < 0)
635                                         return;
636                         }
637                 }
638         } else if (is_Const_or_Confirm(selector)) {
639                 tarval *tv = get_Const_or_Confirm_tarval(selector);
640                 if(tv == tarval_b_true) {
641                         selector_evaluated = 1;
642                 } else {
643                         assert(tv == tarval_b_false);
644                         selector_evaluated = 0;
645                 }
646         }
647
648         env.cnst_pred = NULL;
649         if (get_Proj_proj(projx) == pn_Cond_false) {
650                 env.tv = tarval_b_false;
651                 if(selector_evaluated >= 0)
652                         selector_evaluated = !selector_evaluated;
653         } else {
654                 env.tv = tarval_b_true;
655         }
656
657         if(selector_evaluated == 0) {
658                 bad = new_Bad();
659                 exchange(projx, bad);
660                 *changed = 1;
661                 return;
662         } else if(selector_evaluated == 1) {
663                 dbg_info *dbgi = get_irn_dbg_info(selector);
664                 ir_node  *jmp  = new_rd_Jmp(dbgi, current_ir_graph, get_nodes_block(projx));
665                 exchange(projx, jmp);
666                 *changed = 1;
667                 return;
668         }
669
670         /* (recursively) look if a pred of a Phi is a constant or a Confirm */
671         env.true_block = block;
672         inc_irg_visited(current_ir_graph);
673         env.visited_nr = get_irg_visited(current_ir_graph);
674
675         copy_block = find_candidate(&env, projx, selector);
676         if (copy_block == NULL)
677                 return;
678
679         /* we have to remove the edge towards the pred as the pred now
680          * jumps into the true_block. We also have to shorten Phis
681          * in our block because of this */
682         bad      = new_Bad();
683         cnst_pos = env.cnst_pos;
684
685         /* shorten Phis */
686         foreach_out_edge_safe(env.cnst_pred, edge, next) {
687                 ir_node *node = get_edge_src_irn(edge);
688
689                 if(is_Phi(node))
690                         set_Phi_pred(node, cnst_pos, bad);
691         }
692
693         set_Block_cfgpred(env.cnst_pred, cnst_pos, bad);
694
695         /* the graph is changed now */
696         *changed = 1;
697 }
698
699 void opt_cond_eval(ir_graph* irg)
700 {
701         int changed, rerun;
702
703         FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
704
705         DB((dbg, LEVEL_1, "===> Performing condition evaluation on %+F\n", irg));
706
707         remove_critical_cf_edges(irg);
708         normalize_proj_nodes(irg);
709
710         edges_assure(irg);
711         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
712
713         changed = 0;
714         do {
715                 rerun = 0;
716                 irg_block_walk_graph(irg, cond_eval, NULL, &rerun);
717                 changed |= rerun;
718         } while (rerun);
719
720         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
721
722         if (changed) {
723                 /* control flow changed, some blocks may become dead */
724                 set_irg_outs_inconsistent(irg);
725                 set_irg_doms_inconsistent(irg);
726                 set_irg_extblk_inconsistent(irg);
727                 set_irg_loopinfo_inconsistent(irg);
728
729                 /* Dead code might be created. Optimize it away as it is dangerous
730                  * to call optimize_df() an dead code. */
731                 optimize_cf(irg);
732         }
733 }