65a7de8b4543ca25d3d5e0f1d56acd5fea0a080a
[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.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;
260
261                 if (is_Block(node)) {
262                         /* Block->Block edge, should be the MacroBlock edge */
263                         assert(get_Block_MacroBlock(node) == block && "Block->Block edge found");
264                         continue;
265                 }
266
267                 /* ignore control flow */
268                 mode = get_irn_mode(node);
269                 if (mode == mode_X || is_Cond(node))
270                         continue;
271 #ifdef AVOID_PHIB
272                 /* we may not copy mode_b nodes, because this could produce phi with
273                  * mode_bs which can't be handled in all backends. Instead we duplicate
274                  * the node and move it to its users */
275                 if (mode == mode_b) {
276                         const ir_edge_t *edge, *next;
277                         ir_node *pred;
278                         int      pn;
279
280                         assert(is_Proj(node));
281
282                         pred = get_Proj_pred(node);
283                         pn   = get_Proj_proj(node);
284
285                         foreach_out_edge_safe(node, edge, next) {
286                                 ir_node *cmp_copy;
287                                 ir_node *user       = get_edge_src_irn(edge);
288                                 int pos             = get_edge_src_pos(edge);
289                                 ir_node *user_block = get_nodes_block(user);
290
291                                 if(user_block == block)
292                                         continue;
293
294                                 cmp_copy = exact_copy(pred);
295                                 set_nodes_block(cmp_copy, user_block);
296                                 copy = new_r_Proj(current_ir_graph, user_block, cmp_copy, mode_b, pn);
297                                 set_irn_n(user, pos, copy);
298                         }
299                         continue;
300                 }
301 #endif
302
303                 copy = copy_and_fix_node(env, block, copy_block, j, node);
304
305                 /* we might hit values in blocks that have already been processed by a
306                  * recursive find_phi_with_const call */
307                 assert(get_irn_visited(copy) <= env->visited_nr);
308                 if(get_irn_visited(copy) >= env->visited_nr) {
309                         ir_node *prev_copy = get_irn_link(copy);
310                         if(prev_copy != NULL)
311                                 set_irn_link(node, prev_copy);
312                 }
313         }
314
315         /* fix data-flow (and reconstruct SSA if needed) */
316         foreach_out_edge(block, edge) {
317                 ir_node *vals[2];
318                 ir_node *blocks[2];
319                 ir_node *node = get_edge_src_irn(edge);
320                 ir_mode *mode;
321
322                 if (is_Block(node)) {
323                         /* Block->Block edge, should be the MacroBlock edge */
324                         assert(get_Block_MacroBlock(node) == block && "Block->Block edge found");
325                         continue;
326                 }
327
328                 mode = get_irn_mode(node);
329                 if (mode == mode_X || is_Cond(node))
330                         continue;
331 #ifdef AVOID_PHIB
332                 if (mode == mode_b)
333                         continue;
334 #endif
335
336                 DB((dbg, LEVEL_2, ">> Fixing users of %+F\n", node));
337
338                 blocks[0] = block;
339                 vals[0] = node;
340                 blocks[1] = copy_block;
341                 vals[1] = get_irn_link(node);
342                 construct_ssa(blocks, vals, 2);
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 static int eval_cmp(pn_Cmp pnc, tarval *tv1, tarval *tv2) {
351         pn_Cmp cmp_result = tarval_cmp(tv1, tv2);
352
353         // does the compare evaluate to true?
354         if(cmp_result == pn_Cmp_False)
355                 return -1;
356         if((cmp_result & pnc) != cmp_result)
357                 return 0;
358
359         return 1;
360 }
361
362 /**
363  * Check for Const or constlike Confirm.
364  */
365 static int is_Const_or_Confirm(const ir_node *node) {
366         if (is_Confirm(node)) {
367                 if (get_Confirm_cmp(node) == pn_Cmp_Eq)
368                         node = get_Confirm_bound(node);
369         }
370         return is_Const(node);
371 }
372
373 /**
374  * get the tarval of a COnst or constlike Confirm
375  */
376 static tarval *get_Const_or_Confirm_tarval(const ir_node *node) {
377         if (is_Confirm(node)) {
378                 if (get_Confirm_cmp(node) == pn_Cmp_Eq)
379                         node = get_Confirm_bound(node);
380         }
381         return get_Const_tarval(node);
382 }
383
384 static ir_node *find_const(condeval_env_t *env, ir_node *jump, ir_node *value)
385 {
386         ir_node *block = get_nodes_block(jump);
387
388         if (irn_visited(value))
389                 return NULL;
390         mark_irn_visited(value);
391
392         if(is_Const_or_Confirm(value)) {
393                 tarval *tv_const = get_Const_tarval(env->cnst);
394                 tarval *tv       = get_Const_or_Confirm_tarval(value);
395
396                 if(eval_cmp(env->pnc, tv, tv_const) <= 0) {
397                         return NULL;
398                 }
399
400                 DB((
401                         dbg, LEVEL_1,
402                         "> Found condition evaluation candidate %+F->%+F\n",
403                         env->true_block, block
404                 ));
405
406                 // adjust true_block to point directly towards our jump
407                 add_pred(env->true_block, jump);
408
409                 split_critical_edge(env->true_block, 0);
410
411                 // we need a bigger visited nr when going back
412                 env->visited_nr++;
413
414                 return block;
415         }
416
417         if(is_Phi(value)) {
418                 int i, arity;
419
420                 /* the phi has to be in the same block as the jump */
421                 if(get_nodes_block(value) != block) {
422                         return NULL;
423                 }
424
425                 arity = get_irn_arity(value);
426                 for(i = 0; i < arity; ++i) {
427                         ir_node *copy_block;
428                         ir_node *phi_pred = get_Phi_pred(value, i);
429                         ir_node *cfgpred  = get_Block_cfgpred(block, i);
430
431                         copy_block = find_const(env, cfgpred, phi_pred);
432                         if(copy_block == NULL)
433                                 continue;
434
435                         /* copy duplicated nodes in copy_block and fix SSA */
436                         copy_and_fix(env, block, copy_block, i);
437
438                         if(copy_block == get_nodes_block(cfgpred)) {
439                                 env->cnst_pred = block;
440                                 env->cnst_pos  = i;
441                         }
442
443                         /* return now as we can't process more possibilities in 1 run */
444                         return copy_block;
445                 }
446         }
447
448         return NULL;
449 }
450
451 static ir_node *find_candidate(condeval_env_t *env, ir_node *jump,
452                                ir_node *value)
453 {
454         ir_node *block = get_nodes_block(jump);
455
456         if(irn_visited(value)) {
457                 return NULL;
458         }
459         mark_irn_visited(value);
460
461         if(is_Const_or_Confirm(value)) {
462                 tarval *tv = get_Const_or_Confirm_tarval(value);
463
464                 if(tv != env->tv)
465                         return NULL;
466
467                 DB((
468                         dbg, LEVEL_1,
469                         "> Found condition evaluation candidate %+F->%+F\n",
470                         env->true_block, block
471                 ));
472
473                 // adjust true_block to point directly towards our jump
474                 add_pred(env->true_block, jump);
475
476                 split_critical_edge(env->true_block, 0);
477
478                 // we need a bigger visited nr when going back
479                 env->visited_nr++;
480
481                 return block;
482         }
483         if(is_Phi(value)) {
484                 int i, arity;
485
486                 // the phi has to be in the same block as the jump
487                 if(get_nodes_block(value) != block)
488                         return NULL;
489
490                 arity = get_irn_arity(value);
491                 for(i = 0; i < arity; ++i) {
492                         ir_node *copy_block;
493                         ir_node *phi_pred = get_Phi_pred(value, i);
494                         ir_node *cfgpred  = get_Block_cfgpred(block, i);
495
496                         copy_block = find_candidate(env, cfgpred, phi_pred);
497                         if(copy_block == NULL)
498                                 continue;
499
500                         /* copy duplicated nodes in copy_block and fix SSA */
501                         copy_and_fix(env, block, copy_block, i);
502
503                         if(copy_block == get_nodes_block(cfgpred)) {
504                                 env->cnst_pred = block;
505                                 env->cnst_pos  = i;
506                         }
507
508                         // return now as we can't process more possibilities in 1 run
509                         return copy_block;
510                 }
511         }
512         if(is_Proj(value)) {
513                 ir_node *left;
514                 ir_node *right;
515                 int      pnc;
516                 ir_node *cmp = get_Proj_pred(value);
517                 if(!is_Cmp(cmp))
518                         return NULL;
519
520                 left  = get_Cmp_left(cmp);
521                 right = get_Cmp_right(cmp);
522                 pnc   = get_Proj_proj(value);
523
524                 /* we assume that the constant is on the right side, swap left/right
525                  * if needed */
526                 if(is_Const(left)) {
527                         ir_node *t = left;
528                         left       = right;
529                         right      = t;
530
531                         pnc        = get_inversed_pnc(pnc);
532                 }
533
534                 if(!is_Const(right))
535                         return 0;
536
537                 if(get_nodes_block(left) != block) {
538                         return 0;
539                 }
540
541                 /* negate condition when we're looking for the false block */
542                 if(env->tv == get_tarval_b_false()) {
543                         pnc = get_negated_pnc(pnc, get_irn_mode(right));
544                 }
545
546                 // (recursively) look if a pred of a phi is a constant
547                 env->pnc  = pnc;
548                 env->cnst = right;
549
550                 return find_const(env, jump, left);
551         }
552
553         return NULL;
554 }
555
556 /**
557  * Block-walker: searches for the following construct
558  *
559  *  Const or Phi with constants
560  *           |
561  *          Cmp
562  *           |
563  *         Cond
564  *          /
565  *       ProjX
566  *        /
567  *     Block
568  */
569 static void cond_eval(ir_node* block, void* data)
570 {
571         condeval_env_t env;
572         int *changed = data;
573         ir_node *selector;
574         ir_node *projx;
575         ir_node *cond;
576         ir_node *copy_block;
577         int      selector_evaluated;
578         const ir_edge_t *edge, *next;
579         ir_node* bad;
580         size_t   cnst_pos;
581
582         if(get_Block_n_cfgpreds(block) != 1)
583                 return;
584
585         projx = get_Block_cfgpred(block, 0);
586         if (!is_Proj(projx))
587                 return;
588         assert(get_irn_mode(projx) == mode_X);
589
590         cond = get_Proj_pred(projx);
591         if (!is_Cond(cond))
592                 return;
593
594         selector = get_Cond_selector(cond);
595         // TODO handle switch Conds
596         if (get_irn_mode(selector) != mode_b)
597                 return;
598
599         /* handle cases that can be immediately evaluated */
600         selector_evaluated = -1;
601         if(is_Proj(selector)) {
602                 ir_node *cmp = get_Proj_pred(selector);
603                 if(is_Cmp(cmp)) {
604                         ir_node *left  = get_Cmp_left(cmp);
605                         ir_node *right = get_Cmp_right(cmp);
606                         if(is_Const(left) && is_Const(right)) {
607                                 int     pnc      = get_Proj_proj(selector);
608                                 tarval *tv_left  = get_Const_tarval(left);
609                                 tarval *tv_right = get_Const_tarval(right);
610
611                                 selector_evaluated = eval_cmp(pnc, tv_left, tv_right);
612                                 if(selector_evaluated < 0)
613                                         return;
614                         }
615                 }
616         } else if(is_Const_or_Confirm(selector)) {
617                 tarval *tv = get_Const_or_Confirm_tarval(selector);
618                 if(tv == get_tarval_b_true()) {
619                         selector_evaluated = 1;
620                 } else {
621                         assert(tv == get_tarval_b_false());
622                         selector_evaluated = 0;
623                 }
624         }
625
626         env.cnst_pred = NULL;
627         if (get_Proj_proj(projx) == pn_Cond_false) {
628                 env.tv = get_tarval_b_false();
629                 if(selector_evaluated >= 0)
630                         selector_evaluated = !selector_evaluated;
631         } else {
632                 env.tv = get_tarval_b_true();
633         }
634
635         if(selector_evaluated == 0) {
636                 bad = new_Bad();
637                 exchange(projx, bad);
638                 *changed = 1;
639                 return;
640         } else if(selector_evaluated == 1) {
641                 dbg_info *dbgi = get_irn_dbg_info(selector);
642                 ir_node  *jmp  = new_rd_Jmp(dbgi, current_ir_graph, get_nodes_block(projx));
643                 exchange(projx, jmp);
644                 *changed = 1;
645                 return;
646         }
647
648         // (recursively) look if a pred of a phi is a constant
649         env.true_block = block;
650         inc_irg_visited(current_ir_graph);
651         env.visited_nr = get_irg_visited(current_ir_graph);
652
653         copy_block = find_candidate(&env, projx, selector);
654         if (copy_block == NULL)
655                 return;
656
657         /* we have to remove the edge towards the pred as the pred now
658          * jumps into the true_block. We also have to shorten phis
659          * in our block because of this */
660         bad      = new_Bad();
661         cnst_pos = env.cnst_pos;
662
663         /* shorten phis */
664         foreach_out_edge_safe(env.cnst_pred, edge, next) {
665                 ir_node *node = get_edge_src_irn(edge);
666
667                 if(is_Phi(node))
668                         set_Phi_pred(node, cnst_pos, bad);
669         }
670
671         set_Block_cfgpred(env.cnst_pred, cnst_pos, bad);
672
673         /* the graph is changed now */
674         *changed = 1;
675 }
676
677 void opt_cond_eval(ir_graph* irg)
678 {
679         int changed, rerun;
680
681         FIRM_DBG_REGISTER(dbg, "firm.opt.condeval");
682
683         DB((dbg, LEVEL_1, "===> Performing condition evaluation on %+F\n", irg));
684
685         remove_critical_cf_edges(irg);
686         normalize_proj_nodes(irg);
687
688         edges_assure(irg);
689         set_using_irn_link(irg);
690         set_using_irn_visited(irg);
691
692         changed = 0;
693         do {
694                 rerun = 0;
695                 irg_block_walk_graph(irg, cond_eval, NULL, &rerun);
696                 changed |= rerun;
697         } while (rerun);
698
699         clear_using_irn_visited(irg);
700         clear_using_irn_link(irg);
701
702         if (changed) {
703                 /* control flow changed, some blocks may become dead */
704                 set_irg_outs_inconsistent(irg);
705                 set_irg_doms_inconsistent(irg);
706                 set_irg_extblk_inconsistent(irg);
707                 set_irg_loopinfo_inconsistent(irg);
708
709                 /* Dead code might be created. Optimize it away as it is dangerous
710                  * to call optimize_df() an dead code. */
711                 optimize_cf(irg);
712         }
713
714 }