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