added missing include
[libfirm] / ir / opt / ifconv.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/opt/ifconv.c
4  * Purpose:     If conversion
5  * Author:      Sebastian Hack.
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2005 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 /**
13  * @file ifconv.c
14  * If conversion.
15  * Make Mux nodes from Conds where it its possible.
16  * @author Sebastian Hack
17  * @date 4.2.2005
18  */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #ifdef HAVE_ALLOCA_H
30 #include <alloca.h>
31 #endif
32 #ifdef HAVE_MALLOC_H
33 #include <malloc.h>
34 #endif
35
36 #include "irgraph_t.h"
37 #include "irnode_t.h"
38 #include "iropt_t.h"
39 #include "irgmod.h"
40 #include "irmode_t.h"
41 #include "ircons_t.h"
42 #include "irdom_t.h"
43 #include "irgwalk.h"
44
45 #include "ifconv.h"
46 #include "irflag_t.h"
47
48 #include "irprintf.h"
49 #include "debug.h"
50 #include "obst.h"
51 #include "set.h"
52 #include "bitset.h"
53 #include "bitfiddle.h"
54 #include "irhooks.h"
55 #include "return.h"
56
57 #define MAX_DEPTH   4
58
59 /**
60  * check, if a node is const and return its tarval or
61  * return a default tarval.
62  * @param cnst The node whose tarval to get.
63  * @param or The alternative tarval, if the node is no Const.
64  * @return The tarval of @p cnst, if the node is Const, @p otherwise.
65  */
66 static tarval *get_value_or(ir_node *cnst, tarval *or)
67 {
68   return get_irn_op(cnst) == op_Const ? get_Const_tarval(cnst) : or;
69 }
70
71
72 /**
73  * Try to optimize nested muxes into a dis- or conjunction
74  * of two muxes.
75  * @param mux The parent mux, which has muxes as operands.
76  * @return The replacement node for this mux. If the optimization is
77  * successful, this might be an And or Or node, if not, its the mux
78  * himself.
79  */
80 static ir_node *optimize_mux_chain(ir_node *mux)
81 {
82   int i;
83   ir_node *res;
84   ir_node *ops[2];
85   ir_mode *mode = get_irn_mode(mux);
86   tarval *null;
87   tarval *minus_one;
88
89   /*
90    * If we have no mux, or its mode is not integer, we
91    * can return.
92    */
93   if(get_irn_op(mux) != op_Mux || !mode_is_int(mode))
94     return mux;
95
96   res = mux;
97   null = get_mode_null(mode);
98   minus_one = tarval_sub(null, get_tarval_one(mode));
99
100   ops[0] = get_Mux_false(mux);
101   ops[1] = get_Mux_true(mux);
102
103   for(i = 0; i < 2; ++i) {
104     ir_node *a, *b, *d;
105     tarval *tva, *tvb, *tvd;
106     ir_node *child_mux;
107
108     /*
109      * A mux operand at the first position can be factored
110      * out, if the operands fulfill several conditions:
111      *
112      * mux(c1, mux(c2, a, b), d)
113      *
114      * This can be made into:
115      * 1) mux(c1, 0, d) | mux(c2, a, b)
116      *    if a | d == d and b | d == d
117      *
118      * 2) mux(c1, -1, d) & mux(c2, a, b)
119      *    if a & d == d and a & b == b
120      */
121     if(get_irn_op(ops[i]) == op_Mux) {
122
123       child_mux = ops[i];
124       a = get_Mux_false(child_mux);
125       b = get_Mux_true(child_mux);
126       d = ops[1 - i];
127
128       /* Try the or stuff */
129       tva = get_value_or(a, minus_one);
130       tvb = get_value_or(b, minus_one);
131       tvd = get_value_or(d, null);
132
133       if(tarval_cmp(tarval_or(tva, tvd), tvd) == pn_Cmp_Eq
134           && tarval_cmp(tarval_or(tvb, tvd), tvd) == pn_Cmp_Eq) {
135
136         ops[i] = new_Const(mode, null);
137         res = new_r_Or(current_ir_graph, get_nodes_block(mux),
138             mux, child_mux, mode);
139         break;
140       }
141
142       /* If the or didn't go, try the and stuff */
143       tva = get_value_or(a, null);
144       tvb = get_value_or(b, null);
145       tvd = get_value_or(d, minus_one);
146
147       if(tarval_cmp(tarval_and(tva, tvd), tvd) == pn_Cmp_Eq
148           && tarval_cmp(tarval_and(tvb, tvd), tvd) == pn_Cmp_Eq) {
149
150         ops[i] = new_Const(mode, minus_one);
151         res = new_r_And(current_ir_graph, get_nodes_block(mux),
152             mux, child_mux, mode);
153         break;
154       }
155     }
156   }
157
158   /* recursively optimize nested muxes. */
159   set_irn_n(mux, 1, optimize_mux_chain(ops[0]));
160   set_irn_n(mux, 2, optimize_mux_chain(ops[1]));
161
162   return res;
163 }
164
165
166 /***********************************************************
167  * The If conversion itself.
168  ***********************************************************/
169
170 /** allow every Mux to be created. */
171 static int default_allow_mux(ir_node *sel, ir_node *false_res, ir_node *true_res) {
172   return 1;
173 }
174
175 /**
176  * Default options.
177  */
178 static const opt_if_conv_info_t default_info = {
179   MAX_DEPTH,
180   default_allow_mux
181 };
182
183 /** The debugging module. */
184 static firm_dbg_module_t *dbg;
185
186 /**
187  * A simple check for side effects upto an opcode of a ir node.
188  * @param irn The ir node to check,
189  * @return 1 if the opcode itself may produce side effects, 0 if not.
190  */
191 static INLINE int has_side_effects(const ir_node *irn)
192 {
193   ir_op *op = get_irn_op(irn);
194
195   if (op == op_Cmp)
196     return 0;
197
198   return !mode_is_datab(get_irn_mode(irn));
199 }
200
201 /**
202  * Possible failure reasons
203  */
204 enum failure_reason_t {
205   SUCCESS      = IF_RESULT_SUCCESS,
206   TO_DEEP      = IF_RESULT_TOO_DEEP,
207   SIDE_EFFECTS = IF_RESULT_SIDE_EFFECT,
208   PHI_FOUND    = IF_RESULT_SIDE_EFFECT_PHI,
209   DENIED       = IF_RESULT_DENIED
210 };
211
212 /**
213  * Decides, if a given expression and its subexpressions
214  * (to certain, also given extent) can be moved to a block.
215  *
216  * @param expr      The expression to examine.
217  * @param block     The block where the expression should go.
218  * @param depth     The current depth, passed recursively. Use 0 for
219  *                  non-recursive calls.
220  * @param info      The options for createing Mux nodes.
221  * examined.
222  *
223  * @return a failure reason
224  */
225 static int _can_move_to(ir_node *expr, ir_node *dest_block, int depth, const opt_if_conv_info_t *info)
226 {
227   int i, n;
228   int res = SUCCESS;
229   ir_node *expr_block = get_nodes_block(expr);
230
231   /*
232    * If we are forced to look too deep into the expression,
233    * treat it like it could not be moved.
234    */
235   if(depth >= info->max_depth) {
236     res = TO_DEEP;
237     goto end;
238   }
239
240   /*
241    * If the block of the expression dominates the specified
242    * destination block, it does not matter if the expression
243    * has side effects or anything else. It is executed on each
244    * path the destination block is reached.
245    */
246   if (block_dominates(expr_block, dest_block))
247     goto end;
248
249   /*
250    * We cannot move phis!
251    */
252   if (is_Phi(expr)) {
253     res = PHI_FOUND;
254     goto end;
255   }
256
257   /*
258    * This should be superfluous and could be converted into a assertion.
259    * The destination block _must_ dominate the block of the expression,
260    * else the expression could be used without its definition.
261    */
262   if (! block_dominates(dest_block, expr_block)) {
263     res = IF_RESULT_SIDE_EFFECT;
264     goto end;
265   }
266
267   /*
268    * Surely, if the expression does not have a data mode, it is not
269    * movable. Perhaps one should also test the floating property of
270    * the opcode/node.
271    */
272   if (has_side_effects(expr)) {
273     res = IF_RESULT_SIDE_EFFECT;
274     goto end;
275   }
276
277   /*
278    * If the node looks alright so far, look at its operands and
279    * check them out. If one of them cannot be moved, this one
280    * cannot be moved either.
281    */
282   for (i = 0, n = get_irn_arity(expr); i < n; ++i) {
283     ir_node *op = get_irn_n(expr, i);
284     int new_depth = is_Proj(op) ? depth : depth + 1;
285
286     res = _can_move_to(op, dest_block, new_depth, info);
287
288     if (res != SUCCESS)
289       goto end;
290   }
291
292 end:
293   DBG((dbg, LEVEL_3, "\t\t\tcan move to %n: %d\n", expr, res));
294
295   return res;
296 }
297
298 /**
299  * Convenience function for _can_move_to.
300  * Checks, if an expression can be moved to another block. The check can
301  * be limited to a expression depth meaning if we need to crawl in
302  * deeper into an expression than a given threshold to examine if
303  * it can be moved, the expression is rejected and the test returns
304  * false.
305  *
306  * @param expr       The expression to check for.
307  * @param dest_block The destination block you want @p expr to be.
308  * @param info       The options for createing Mux nodes.
309  *
310  * @return return a failure reason
311  */
312 static INLINE int can_move_to(ir_node *expr, ir_node *dest_block, const opt_if_conv_info_t *info)
313 {
314   return _can_move_to(expr, dest_block, 0, info);
315 }
316
317 /**
318  * move a DAG given by a root node expr into a new block
319  *
320  * @param expr       the root of a dag
321  * @param dest_block the destination block
322  */
323 static void move_to(ir_node *expr, ir_node *dest_block)
324 {
325   int i, n;
326   ir_node *expr_block = get_nodes_block(expr);
327
328   /*
329    * If we reached the dominator, we are done.
330    * We will never put code through the dominator
331    */
332   if (block_dominates(expr_block, dest_block))
333     return;
334
335   for (i = 0, n = get_irn_arity(expr); i < n; ++i)
336     move_to(get_irn_n(expr, i), dest_block);
337
338   set_nodes_block(expr, dest_block);
339 }
340
341 /**
342  * return the common dominator of two blocks
343  */
344 static INLINE ir_node *common_idom(ir_node *b1, ir_node *b2)
345 {
346   if(block_dominates(b1, b2))
347     return b1;
348   else if(block_dominates(b2, b1))
349     return b2;
350   else {
351     ir_node *p;
352
353     for (p = get_Block_idom(b1); !block_dominates(p, b2); p = get_Block_idom(p));
354     return p;
355   }
356 }
357
358 /**
359  * Information about a cond node.
360  */
361 typedef struct _cond_t {
362   ir_node *cond;          /**< The cond node. */
363   struct list_head list;  /**< List head which is used for queuing this cond
364                                into the cond bunch it belongs to. */
365   unsigned is_new : 1;
366   unsigned totally_covers : 1;
367   struct _cond_t *link;
368   long visited_nr;
369
370   /**
371    * Information about the both 'branches'
372    * (true and false), the cond creates.
373    */
374   struct {
375     int pos;         /**< Number of the predecessor of the
376                           phi block by which this branch is
377                           reached. It is -1, if this branch is
378                           only reached through another cond. */
379
380     struct _cond_t *masked_by; /**< If this cond's branch is only reached
381                                     through another cond, we store this
382                                     cond ir_node here. */
383   } cases[2];
384 } cond_t;
385
386 /**
387  * retrieve the conditional information from a Cond node
388  */
389 static INLINE cond_t *get_cond(ir_node *irn, set *cond_set)
390 {
391   cond_t templ;
392
393   templ.cond = irn;
394   return set_find(cond_set, &templ, sizeof(templ), HASH_PTR(templ.cond));
395 }
396
397
398 typedef void (cond_walker_t)(cond_t *cond, void *env);
399
400 static void _walk_conds(cond_t *cond, cond_walker_t *pre, cond_walker_t *post,
401       long visited_nr, void *env)
402 {
403   int i;
404
405   if(cond->visited_nr >= visited_nr)
406     return;
407
408   cond->visited_nr = visited_nr;
409
410   if(pre)
411     pre(cond, env);
412
413   for(i = 0; i < 2; ++i) {
414     cond_t *c = cond->cases[i].masked_by;
415
416     if(c)
417       _walk_conds(c, pre, post, visited_nr, env);
418   }
419
420   if(post)
421     post(cond, env);
422 }
423
424 static long cond_visited_nr = 0;
425
426 static void walk_conds(cond_t *cond, cond_walker_t *pre, cond_walker_t *post, void *env)
427 {
428   _walk_conds(cond, pre, post, ++cond_visited_nr, env);
429 }
430
431 static void link_conds(cond_t *cond, void *env)
432 {
433   cond_t **ptr = (cond_t **) env;
434
435   cond->link = *ptr;
436   *ptr = cond;
437 }
438
439 /**
440  * Compare two conds for use in a firm set.
441  * Two cond_t's are equal, if they designate the same cond node.
442  * @param a A cond_t.
443  * @param b Another one.
444  * @param size Not used.
445  * @return 0 (!) if they are equal, != 0 otherwise.
446  */
447 static int cond_cmp(const void *a, const void *b, size_t size)
448 {
449   const cond_t *x = a;
450   const cond_t *y = b;
451   return x->cond != y->cond;
452 }
453
454 /**
455  * Information about conds which can be made to muxes.
456  * Instances of this struct are attached to the link field of
457  * blocks in which phis are located.
458  */
459 typedef struct _cond_info_t {
460   struct list_head list;      /**< Used to list all of these structs per class. */
461
462   struct list_head roots;     /**< A list of non-depending Conds. Two Conds are
463                                 independent, if it's not possible not reach one from the
464                                 other (all Conds in this list have to dominate the
465                                 block this struct is attached to). */
466
467   ir_node *first_phi;         /**< The first phi node this cond info was made for. */
468   set *cond_set;              /**< A set of all dominating reachable Conds. */
469 } cond_info_t;
470
471 /**
472  * @see find_conds.
473  */
474 static void _find_conds(ir_node *irn, unsigned long visited_nr,
475     ir_node *dominator, cond_t *masked_by, int pos, int depth, cond_info_t *ci)
476 {
477   ir_node *block;
478   int saw_select_cond = 0;
479
480   block = get_nodes_block(irn);
481
482   /*
483    * Only check this block if it is dominated by the specified
484    * dominator or it has not been visited yet.
485    */
486   if (block_dominates(dominator, block) && get_Block_block_visited(block) < visited_nr) {
487     cond_t *res = masked_by;
488     int i, n;
489
490     /* check, if we're on a ProjX
491      *
492      * Further, the ProjX/Cond block must dominate the base block
493      * (the block with the phi in it), otherwise, the Cond
494      * is not affecting the phi so that a mux can be inserted.
495      */
496     if(is_Proj(irn) && get_irn_mode(irn) == mode_X) {
497
498       int proj = get_Proj_proj(irn);
499       ir_node *cond = get_Proj_pred(irn);
500
501       /* true, if the mode is a mode_b cond _NO_ switch cond */
502       int is_modeb_cond = get_irn_opcode(cond) == iro_Cond
503         && get_irn_mode(get_Cond_selector(cond)) == mode_b;
504
505       saw_select_cond = !is_modeb_cond;
506
507       /* Check, if the pred of the proj is a Cond
508        * with a Projb as selector.
509        */
510       if(is_modeb_cond) {
511         cond_t c;
512
513         memset(&c, 0, sizeof(c));
514         c.cond = cond;
515         c.is_new = 1;
516         c.cases[0].pos = -1;
517         c.cases[1].pos = -1;
518
519         /* get or insert the cond info into the set. */
520         res = set_insert(ci->cond_set, &c, sizeof(c), HASH_PTR(cond));
521
522         /*
523          * If this cond is already masked by the masked_by cond
524          * return immediately, since we don't have anything to add.
525          */
526         if(masked_by && res->cases[proj].masked_by == masked_by)
527           return;
528
529         if(res->is_new) {
530           res->is_new = 0;
531           list_add(&res->list, &ci->roots);
532         }
533
534         /*
535          * Set masked by (either NULL or another cond node.
536          * If this cond is truly masked by another one, set
537          * the position of the actually investigated branch
538          * to -1. Since the cond is masked by another one,
539          * there could be more ways from the start block
540          * to this branch, so we choose -1.
541          */
542         res->cases[proj].masked_by = masked_by;
543
544         if(!masked_by)
545           res->cases[proj].pos = pos;
546
547         /*
548          * Since the masked_by nodes masks a cond, remove it from the
549          * root list of the conf trees.
550          */
551         else {
552           assert(res->cases[proj].pos < 0);
553           list_del_init(&masked_by->list);
554         }
555
556         DBG((dbg, LEVEL_2, "%n (%s branch) "
557               "for pos %d in block %n reached by %n\n",
558               cond, proj ? "true" : "false", pos,
559               block, masked_by ? masked_by->cond : NULL));
560       }
561     }
562
563     if(get_Block_block_visited(block) < visited_nr && !saw_select_cond) {
564
565       set_Block_block_visited(block, visited_nr);
566
567       /* Search recursively from this cond. */
568       for(i = 0, n = get_irn_arity(block); i < n; ++i) {
569         ir_node *pred = get_irn_n(block, i);
570
571         /*
572          * If the depth is 0 (the first recursion), we set the pos to
573          * the current viewed predecessor, else we adopt the position
574          * as given by the caller. We also increase the depth for the
575          * recursively called functions.
576          */
577         _find_conds(pred, visited_nr, dominator, res, pos, depth + (res != masked_by), ci);
578       }
579     }
580   }
581 }
582
583
584 /**
585  * A convenience function for _find_conds.
586  * It sets some parameters needed for recursion to appropriate start
587  * values. Always use this function.
588  *
589  * @param irn   The node to start looking for Conds from. This might
590  *              be the phi node we are investigating.
591  * @param conds The set to record the found Conds in.
592  */
593 static INLINE void find_conds(ir_node *irn, cond_info_t *ci)
594 {
595   int i, n;
596   unsigned long visited_nr;
597   ir_node *block = get_nodes_block(irn);
598   ir_node *dom = get_Block_idom(block);
599
600   for(i = 0, n = get_irn_arity(block); i < n; ++i) {
601     ir_node *pred = get_irn_n(block, i);
602
603     inc_irg_block_visited(current_ir_graph);
604     visited_nr = get_irg_block_visited(current_ir_graph);
605     set_Block_block_visited(block, visited_nr);
606
607     DBG((dbg, LEVEL_2, "find conds at pred %d (%n) and idom %n\n", i, pred, dom));
608     _find_conds(pred, visited_nr, dom, NULL, i, 0, ci);
609   }
610 }
611
612 /**
613  * Make the mux for a given cond.
614  *
615  * @param phi       The phi node which shall be replaced by a mux.
616  * @param dom       The block where the muxes shall be placed.
617  * @param cond      The cond information.
618  * @param info      The options for createing Mux nodes.
619  * @return The mux node made for this cond.
620  */
621 static ir_node *make_mux_on_demand(ir_node *phi, ir_node *dom, cond_t *cond,
622     const opt_if_conv_info_t *info, ir_node **mux, bitset_t *positions,
623     int *muxes_made, long visited_nr)
624 {
625   int i, can_move[2];
626   ir_node *projb = get_Cond_selector(cond->cond);
627   ir_node *bl = get_nodes_block(cond->cond);
628   ir_node *operands[2];
629   int set[2];
630
631   cond->visited_nr = visited_nr;
632   DBG((dbg, LEVEL_2, "%n\n", cond->cond));
633   for(i = 0; i < 2; ++i) {
634     cond_t *masked_by = cond->cases[i].masked_by;
635     int pos = cond->cases[i].pos;
636
637     operands[i] = NULL;
638     set[i] = -1;
639
640     /*
641      * If this Cond branch is masked by another cond, make the mux
642      * for that Cond first, since the Mux for this cond takes
643      * it as an operand.
644      */
645     if(masked_by) {
646       assert(pos < 0);
647       DBG((dbg, LEVEL_2, "\tmasked by: %n\n", masked_by->cond));
648       if(masked_by->visited_nr < visited_nr)
649         operands[i] = make_mux_on_demand(phi, dom, masked_by, info, mux, positions, muxes_made, visited_nr);
650     }
651
652     /*
653      * If this cond branch is not masked by another cond, take
654      * the corresponding phi operand as an operand to the mux.
655      */
656     else if(pos >= 0) {
657       operands[i] = get_irn_n(phi, pos);
658       set[i] = pos;
659     }
660   }
661
662   /*
663    * Move the operands to the dominator block if the cond
664    * made sense. Some Conds found are not suitable for making a mux
665    * out of them, since one of their branches cannot be reached from
666    * the phi block. In that case we do not make a mux and return NULL.
667    */
668   if(operands[0] && operands[1]) {
669     if (operands[0] == operands[1]) {
670       /* there is no gain in using mux in this case, as
671          it will be optimized away. We will NOT move the
672          content of the blocks either
673         */
674       for (i = 0; i < 2; ++i)
675         if(set[i] >= 0)
676           bitset_set(positions, set[i]);
677
678       *mux = operands[0];
679       return *mux;
680     }
681
682     can_move[0] = can_move_to(operands[0], bl, info);
683     can_move[1] = can_move_to(operands[1], bl, info);
684
685     if (can_move[0] == SUCCESS && can_move[1] == SUCCESS) {
686       if (info->allow_mux(projb, operands[0], operands[1])) {
687         move_to(operands[0], bl);
688         move_to(operands[1], bl);
689
690         /* Make the mux. */
691         *mux = new_r_Mux(current_ir_graph, bl, projb,
692             operands[0], operands[1], get_irn_mode(operands[0]));
693
694         *muxes_made += 1;
695
696         DBG((dbg, LEVEL_2, "\t%n(%n, %n, %n)[%d, %d]\n",
697               *mux, projb, operands[0], operands[1], set[0], set[1]));
698
699         for(i = 0; i < 2; ++i)
700           if(set[i] >= 0) {
701             bitset_set(positions, set[i]);
702
703             /* we have done one */
704             hook_if_conversion(current_ir_graph, phi, set[i], *mux, IF_RESULT_SUCCESS);
705           }
706       }
707       else {
708         hook_if_conversion(current_ir_graph, phi, set[i], *mux, IF_RESULT_DENIED);
709       }
710     }
711     else {
712       if(can_move[0] != SUCCESS)
713         hook_if_conversion(current_ir_graph, phi, set[0], NULL, can_move[0]);
714       if(can_move[1] != SUCCESS)
715         hook_if_conversion(current_ir_graph, phi, set[1], NULL, can_move[1]);
716     }
717   }
718   else {
719     if(operands[0])
720       hook_if_conversion(current_ir_graph, phi, set[0], NULL, IF_RESULT_BAD_CF);
721     if(operands[1])
722       hook_if_conversion(current_ir_graph, phi, set[1], NULL, IF_RESULT_BAD_CF);
723   }
724
725   return *mux;
726 }
727
728 typedef struct _phi_info_t {
729   struct list_head list;
730   cond_info_t *cond_info;
731   ir_node *irn;
732 } phi_info_t;
733
734
735 /**
736  * Examine a phi node if it can be replaced by some muxes.
737  * @param irn A phi node.
738  * @param info Parameters for the if conversion algorithm.
739  */
740 static int check_out_phi(phi_info_t *phi_info, const opt_if_conv_info_t *info)
741 {
742   ir_node *irn = phi_info->irn;
743   ir_node *block, *nw;
744   cond_info_t *cond_info = phi_info->cond_info;
745   cond_t *cond;
746   int i, arity;
747   int muxes_made = 0;
748   bitset_t *positions;
749
750   block = get_nodes_block(irn);
751   arity = get_irn_arity(irn);
752   positions = bitset_alloca(arity);
753
754   assert(is_Phi(irn));
755   assert(get_irn_arity(irn) == get_irn_arity(block));
756   assert(arity > 0);
757
758   DBG((dbg, LEVEL_2, "phi candidate: %n\n", irn));
759
760   list_for_each_entry(cond_t, cond, &cond_info->roots, list) {
761     ir_node *cidom = block;
762     ir_node *mux = NULL;
763     cond_t *p, *head = NULL;
764     long pos;
765
766     bitset_clear_all(positions);
767
768     DBG((dbg, LEVEL_2, "\tcond root: %n\n", cond->cond));
769     /*
770      * Link all conds which are in the subtree of
771      * the current cond in the list together.
772      */
773     walk_conds(cond, link_conds, NULL, &head);
774
775     cidom = block;
776     for(p = head; p; p = p->link) {
777       for(i = 0; i < 2; ++i) {
778         int pos = p->cases[i].pos;
779         if(pos != -1)
780           cidom = common_idom(cidom, get_nodes_block(get_irn_n(block, pos)));
781       }
782     }
783
784     DBG((dbg, LEVEL_2, "\tcommon idom: %n\n", cidom));
785     make_mux_on_demand(irn, cidom, cond, info, &mux, positions, &muxes_made, ++cond_visited_nr);
786
787     if(mux) {
788       bitset_foreach(positions, pos)
789         set_irn_n(irn, (int) pos, mux);
790     }
791   }
792
793   /*
794    * optimize the phi away. This can anable further runs of this
795    * function. Look at _can_move. phis cannot be moved there.
796    */
797   nw = optimize_in_place_2(irn);
798   if(nw != irn)
799     exchange(irn, nw);
800
801   return muxes_made;
802 }
803
804 typedef struct _cond_walk_info_t {
805   struct obstack *obst;
806   struct list_head cond_info_head;
807   struct list_head phi_head;
808 } cond_walk_info_t;
809
810
811 static void annotate_cond_info_pre(ir_node *irn, void *data)
812 {
813   set_irn_link(irn, NULL);
814 }
815
816 static void annotate_cond_info_post(ir_node *irn, void *data)
817 {
818   cond_walk_info_t *cwi = data;
819
820   /*
821    * Check, if the node is a phi
822    * we then compute a set of conds which are reachable from this
823    * phi's block up to its dominator.
824    * The set is attached to the blocks link field.
825    */
826   if(is_Phi(irn) && mode_is_datab(get_irn_mode(irn))) {
827     ir_node *block = get_nodes_block(irn);
828
829     cond_info_t *ci = get_irn_link(block);
830
831     /* If the set is not yet computed, do it now. */
832     if(!ci) {
833       ci = obstack_alloc(cwi->obst, sizeof(*ci));
834       ci->cond_set = new_set(cond_cmp, log2_ceil(get_irn_arity(block)));
835       ci->first_phi = irn;
836
837       INIT_LIST_HEAD(&ci->roots);
838       INIT_LIST_HEAD(&ci->list);
839
840       /*
841        * Add this cond info to the list of all cond infos
842        * in this graph. This is just done to free the
843        * set easier afterwards (we save an irg_walk_graph).
844        */
845       list_add(&cwi->cond_info_head, &ci->list);
846
847       DBG((dbg, LEVEL_2, "searching conds at %n\n", irn));
848
849       /*
850        * Fill the set with conds we find on the way from
851        * the block to its dominator.
852        */
853       find_conds(irn, ci);
854
855       /*
856        * If there where no suitable conds, delete the set
857        * immediately and reset the set pointer to NULL
858        */
859       if(set_count(ci->cond_set) == 0) {
860         del_set(ci->cond_set);
861         list_del(&ci->list);
862         obstack_free(cwi->obst, ci);
863         ci = NULL;
864       }
865     }
866
867     else
868       DBG((dbg, LEVEL_2, "conds already computed for %n (look at %n)\n", irn, ci->first_phi));
869
870     set_irn_link(block, ci);
871
872     if(ci) {
873       phi_info_t *pi = obstack_alloc(cwi->obst, sizeof(*pi));
874       pi->irn = irn;
875       pi->cond_info = ci;
876       INIT_LIST_HEAD(&pi->list);
877       list_add(&pi->list, &cwi->phi_head);
878     }
879
880   }
881 }
882
883 static void dump_conds(cond_t *cond, void *env)
884 {
885   int i;
886   FILE *f = env;
887
888   ir_fprintf(f, "node:{title:\"n%p\" label:\"%n(%d, %d)\n%n\"}\n",
889       cond, cond->cond, cond->cases[0].pos, cond->cases[1].pos,
890       get_nodes_block(cond->cond));
891
892   for(i = 0; i < 2; ++i)
893     if(cond->cases[i].masked_by)
894       ir_fprintf(f, "edge:{sourcename:\"n%p\" targetname:\"n%p\" label:\"%d\"}\n",
895           cond, cond->cases[i].masked_by, i);
896 }
897
898 static void vcg_dump_conds(ir_graph *irg, cond_walk_info_t *cwi)
899 {
900   char buf[512];
901   FILE *f;
902
903   snprintf(buf, sizeof(buf), "%s-conds.vcg", get_entity_name(get_irg_entity(irg)));
904
905   if((f = fopen(buf, "wt")) != NULL) {
906     cond_info_t *ci;
907     phi_info_t *phi;
908     cond_t *cond;
909
910     ir_fprintf(f, "graph:{\ndisplay_edge_labels:yes\n");
911     list_for_each_entry(cond_info_t, ci, &cwi->cond_info_head, list) {
912       ir_fprintf(f, "node:{title:\"n%p\" label:\"cond info\"}\n", ci);
913       list_for_each_entry(cond_t, cond, &ci->roots, list) {
914         walk_conds(cond, NULL, dump_conds, f);
915         ir_fprintf(f, "edge:{sourcename:\"n%p\" targetname:\"n%p\"}\n", ci, cond);
916       }
917     }
918
919     list_for_each_entry(phi_info_t, phi, &cwi->phi_head, list) {
920       ir_fprintf(f, "node:{title:\"n%p\" label:\"%n\n%n\"}\n",
921           phi->irn, phi->irn, get_nodes_block(phi->irn));
922       ir_fprintf(f, "edge:{sourcename:\"n%p\" targetname:\"n%p\"}\n", phi->irn, phi->cond_info);
923     }
924     fprintf(f, "}\n");
925   }
926 }
927
928 void opt_if_conv(ir_graph *irg, const opt_if_conv_info_t *params)
929 {
930   int muxes_made = 0;
931   struct obstack obst;
932   phi_info_t *phi_info;
933   cond_info_t *cond_info;
934   cond_walk_info_t cwi;
935
936   opt_if_conv_info_t p;
937
938   if(!get_opt_if_conversion())
939     return;
940
941   /* get the parameters */
942   if (params)
943     memcpy(&p, params, sizeof(p));
944   else
945     memcpy(&p, &default_info, sizeof(p));
946
947   if (! p.allow_mux)
948     p.allow_mux = default_info.allow_mux;
949
950   obstack_init(&obst);
951
952   cwi.obst = &obst;
953   INIT_LIST_HEAD(&cwi.cond_info_head);
954   INIT_LIST_HEAD(&cwi.phi_head);
955
956   /* Init the debug stuff. */
957   dbg = firm_dbg_register("firm.opt.ifconv");
958 #if 0
959   firm_dbg_set_mask(dbg, LEVEL_1|LEVEL_2|LEVEL_3);
960 #endif
961
962   /* if-conversion works better with normalized returns */
963   normalize_one_return(irg);
964
965   /* Ensure, that the dominators are computed. */
966   compute_doms(irg);
967
968   DBG((dbg, LEVEL_1, "if conversion for irg %s(%p)\n",
969         get_entity_name(get_irg_entity(irg)), irg));
970
971   /*
972    * Collect information about the conds pu the phis on an obstack.
973    * It is important that phi nodes which are 'higher' (with a
974    * lower dfs pre order) are in front of the obstack. Since they are
975    * possibly turned in to muxes this can enable the optimization
976    * of 'lower' ones.
977    */
978   irg_walk_graph(irg, annotate_cond_info_pre, annotate_cond_info_post, &cwi);
979
980 #if 0
981   vcg_dump_conds(irg, &cwi);
982 #endif
983
984   /* Process each suitable phi found. */
985   list_for_each_entry(phi_info_t, phi_info, &cwi.phi_head, list) {
986     DBG((dbg, LEVEL_2, "phi node %n\n", phi_info->irn));
987     muxes_made += check_out_phi(phi_info, &p);
988   }
989
990   list_for_each_entry(cond_info_t, cond_info, &cwi.cond_info_head, list) {
991     del_set(cond_info->cond_set);
992   }
993
994   DBG((dbg, LEVEL_1, "muxes made: %d\n", muxes_made));
995
996   obstack_free(&obst, NULL);
997 }