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