convert opts to use the opt_manage framework
[libfirm] / ir / opt / combo.c
1 /*
2  * Copyright (C) 1995-2011 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   Cliff Click's Combined Analysis/Optimization
23  * @author  Michael Beck
24  * @version $Id$
25  *
26  * This is a slightly enhanced version of Cliff Clicks combo algorithm
27  * - support for commutative nodes is added, Add(a,b) and Add(b,a) ARE congruent
28  * - supports all Firm direct (by a data edge) identities except Mux
29  *   (Mux can be a 2-input or 1-input identity, only 2-input is implemented yet)
30  * - supports Confirm nodes (handle them like Copies but do NOT remove them)
31  * - let Cmp nodes calculate Top like all othe data nodes: this would let
32  *   Mux nodes to calculate Unknown instead of taking the true result
33  * - let Cond(Top) always select FALSE/default: This is tricky. Nodes are only reavaluated
34  *   IFF the predecessor changed its type. Because nodes are initialized with Top
35  *   this never happens, let all Proj(Cond) be unreachable.
36  *   We avoid this condition by the same way we work around Phi: whenever a Block
37  *   node is placed on the list, place its Cond nodes (and because they are Tuple
38  *   all its Proj-nodes either on the cprop list)
39  *   Especially, this changes the meaning of Click's example:
40  *
41  *   int main() {
42  *     int x;
43  *
44  *     if (x == 2)
45  *       printf("x == 2\n");
46  *     if (x == 3)
47  *       printf("x == 3\n");
48  *   }
49  *
50  *   Would print:
51  *   x == 2
52  *   x == 3
53  *
54  *   using Click's version while is silent with our.
55  * - support for global congruences is implemented but not tested yet
56  *
57  * Note further that we use the terminology from Click's work here, which is different
58  * in some cases from Firm terminology.  Especially, Click's type is a
59  * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
60  */
61 #include "config.h"
62
63 #include <assert.h>
64
65 #include "iroptimize.h"
66 #include "irflag.h"
67 #include "ircons.h"
68 #include "list.h"
69 #include "set.h"
70 #include "pmap.h"
71 #include "obstack.h"
72 #include "irgraph_t.h"
73 #include "irnode_t.h"
74 #include "iropt_t.h"
75 #include "irgwalk.h"
76 #include "irop.h"
77 #include "irouts.h"
78 #include "irgmod.h"
79 #include "iropt_dbg.h"
80 #include "debug.h"
81 #include "array_t.h"
82 #include "error.h"
83 #include "irnodeset.h"
84 #include "irpass.h"
85 #include "tv_t.h"
86 #include "irtools.h"
87 #include "opt_manage.h"
88
89 #include "irprintf.h"
90 #include "irdump.h"
91
92 /* define this to check that all type translations are monotone */
93 #define VERIFY_MONOTONE
94
95 /* define this to check the consistency of partitions */
96 #define CHECK_PARTITIONS
97
98 typedef struct node_t            node_t;
99 typedef struct partition_t       partition_t;
100 typedef struct opcode_key_t      opcode_key_t;
101 typedef struct listmap_entry_t   listmap_entry_t;
102
103 /** The type of the compute function. */
104 typedef void (*compute_func)(node_t *node);
105
106 /**
107  * An opcode map key.
108  */
109 struct opcode_key_t {
110         ir_node *irn;    /**< An IR node representing this opcode. */
111 };
112
113 /**
114  * An entry in the list_map.
115  */
116 struct listmap_entry_t {
117         void            *id;    /**< The id. */
118         node_t          *list;  /**< The associated list for this id. */
119         listmap_entry_t *next;  /**< Link to the next entry in the map. */
120 };
121
122 /** We must map id's to lists. */
123 typedef struct listmap_t {
124         set             *map;    /**< Map id's to listmap_entry_t's */
125         listmap_entry_t *values; /**< List of all values in the map. */
126 } listmap_t;
127
128 /**
129  * A lattice element. Because we handle constants and symbolic constants different, we
130  * have to use this union.
131  */
132 typedef union {
133         ir_tarval      *tv;
134         symconst_symbol sym;
135 } lattice_elem_t;
136
137 /**
138  * A node.
139  */
140 struct node_t {
141         ir_node         *node;          /**< The IR-node itself. */
142         list_head       node_list;      /**< Double-linked list of leader/follower entries. */
143         list_head       cprop_list;     /**< Double-linked partition.cprop list. */
144         partition_t     *part;          /**< points to the partition this node belongs to */
145         node_t          *next;          /**< Next node on local list (partition.touched, fallen). */
146         node_t          *race_next;     /**< Next node on race list. */
147         lattice_elem_t  type;           /**< The associated lattice element "type". */
148         int             max_user_input; /**< Maximum input number of Def-Use edges. */
149         int             next_edge;      /**< Index of the next Def-Use edge to use. */
150         int             n_followers;    /**< Number of Follower in the outs set. */
151         unsigned        on_touched:1;   /**< Set, if this node is on the partition.touched set. */
152         unsigned        on_cprop:1;     /**< Set, if this node is on the partition.cprop list. */
153         unsigned        on_fallen:1;    /**< Set, if this node is on the fallen list. */
154         unsigned        is_follower:1;  /**< Set, if this node is a follower. */
155         unsigned        flagged:2;      /**< 2 Bits, set if this node was visited by race 1 or 2. */
156 };
157
158 /**
159  * A partition containing congruent nodes.
160  */
161 struct partition_t {
162         list_head         Leader;          /**< The head of partition Leader node list. */
163         list_head         Follower;        /**< The head of partition Follower node list. */
164         list_head         cprop;           /**< The head of partition.cprop list. */
165         list_head         cprop_X;         /**< The head of partition.cprop (Cond nodes and its Projs) list. */
166         partition_t       *wl_next;        /**< Next entry in the work list if any. */
167         partition_t       *touched_next;   /**< Points to the next partition in the touched set. */
168         partition_t       *cprop_next;     /**< Points to the next partition in the cprop list. */
169         partition_t       *split_next;     /**< Points to the next partition in the list that must be split by split_by(). */
170         node_t            *touched;        /**< The partition.touched set of this partition. */
171         unsigned          n_leader;        /**< Number of entries in this partition.Leader. */
172         unsigned          n_touched;       /**< Number of entries in the partition.touched. */
173         int               max_user_inputs; /**< Maximum number of user inputs of all entries. */
174         unsigned          on_worklist:1;   /**< Set, if this partition is in the work list. */
175         unsigned          on_touched:1;    /**< Set, if this partition is on the touched set. */
176         unsigned          on_cprop:1;      /**< Set, if this partition is on the cprop list. */
177         unsigned          type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
178 #ifdef DEBUG_libfirm
179         partition_t       *dbg_next;       /**< Link all partitions for debugging */
180         unsigned          nr;              /**< A unique number for (what-)mapping, >0. */
181 #endif
182 };
183
184 typedef struct environment_t {
185         struct obstack  obst;           /**< obstack to allocate data structures. */
186         partition_t     *worklist;      /**< The work list. */
187         partition_t     *cprop;         /**< The constant propagation list. */
188         partition_t     *touched;       /**< the touched set. */
189         partition_t     *initial;       /**< The initial partition. */
190         set             *opcode2id_map; /**< The opcodeMode->id map. */
191         ir_node         **kept_memory;  /**< Array of memory nodes that must be kept. */
192         int             end_idx;        /**< -1 for local and 0 for global congruences. */
193         int             lambda_input;   /**< Captured argument for lambda_partition(). */
194         unsigned        modified:1;     /**< Set, if the graph was modified. */
195         unsigned        unopt_cf:1;     /**< If set, control flow is not optimized due to Unknown. */
196         /* options driving the optimization */
197         unsigned        commutative:1;  /**< Set, if commutation nodes should be handled specially. */
198         unsigned        opt_unknown:1;  /**< Set, if non-strict programs should be optimized. */
199 #ifdef DEBUG_libfirm
200         partition_t     *dbg_list;      /**< List of all partitions. */
201 #endif
202 } environment_t;
203
204 /** Type of the what function. */
205 typedef void *(*what_func)(const node_t *node, environment_t *env);
206
207 #define get_irn_node(irn)         ((node_t *)get_irn_link(irn))
208 #define set_irn_node(irn, node)   set_irn_link(irn, node)
209
210 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
211 #undef tarval_unreachable
212 #define tarval_unreachable tarval_top
213
214
215 /** The debug module handle. */
216 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
217
218 /** The what reason. */
219 DEBUG_ONLY(static const char *what_reason;)
220
221 /** Next partition number. */
222 DEBUG_ONLY(static unsigned part_nr = 0);
223
224 /** The tarval returned by Unknown nodes: set to either tarval_bad OR tarval_top. */
225 static ir_tarval *tarval_UNKNOWN;
226
227 /* forward */
228 static node_t *identity(node_t *node);
229
230 /**
231  * Compare two opcode representatives.
232  */
233 static int cmp_irn_opcode(const ir_node *a, const ir_node *b)
234 {
235         int arity;
236
237         if ((get_irn_op(a) != get_irn_op(b)) ||
238             (get_irn_mode(a) != get_irn_mode(b)))
239                 return 1;
240
241         /* compare if a's in and b's in are of equal length */
242         arity = get_irn_arity(a);
243         if (arity != get_irn_arity(b))
244                 return 1;
245
246         if (is_Block(a)) {
247                 /*
248                  * Some ugliness here: Two Blocks having the same
249                  * IJmp predecessor would be congruent, which of course is wrong.
250                  * We fix it by never letting blocks be congruent
251                  * which cannot be detected by combo either.
252                  */
253                 return 1;
254         }
255
256         /*
257          * here, we already know that the nodes are identical except their
258          * attributes
259          */
260         if (a->op->ops.node_cmp_attr)
261                 return a->op->ops.node_cmp_attr(a, b);
262
263         return 0;
264 }  /* cmp_irn_opcode */
265
266 #ifdef CHECK_PARTITIONS
267 /**
268  * Check a partition.
269  */
270 static void check_partition(const partition_t *T)
271 {
272         node_t   *node;
273         unsigned n = 0;
274
275         list_for_each_entry(node_t, node, &T->Leader, node_list) {
276                 assert(node->is_follower == 0);
277                 assert(node->flagged == 0);
278                 assert(node->part == T);
279                 ++n;
280         }
281         assert(n == T->n_leader);
282
283         list_for_each_entry(node_t, node, &T->Follower, node_list) {
284                 assert(node->is_follower == 1);
285                 assert(node->flagged == 0);
286                 assert(node->part == T);
287         }
288 }  /* check_partition */
289
290 /**
291  * check that all leader nodes in the partition have the same opcode.
292  */
293 static void check_opcode(const partition_t *Z)
294 {
295         node_t        *node;
296         const ir_node *repr = NULL;
297
298         list_for_each_entry(node_t, node, &Z->Leader, node_list) {
299                 ir_node *irn = node->node;
300
301                 if (repr == NULL) {
302                         repr = irn;
303                 } else {
304                         assert(cmp_irn_opcode(repr, irn) == 0);
305                 }
306         }
307 }  /* check_opcode */
308
309 static void check_all_partitions(environment_t *env)
310 {
311 #ifdef DEBUG_libfirm
312         partition_t *P;
313         node_t      *node;
314
315         for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
316                 check_partition(P);
317                 if (! P->type_is_T_or_C)
318                         check_opcode(P);
319                 list_for_each_entry(node_t, node, &P->Follower, node_list) {
320                         node_t *leader = identity(node);
321
322                         assert(leader != node && leader->part == node->part);
323                 }
324         }
325 #else
326         (void) env;
327 #endif
328 }
329
330 /**
331  * Check list.
332  */
333 static void do_check_list(const node_t *list, int ofs, const partition_t *Z)
334 {
335
336 #ifndef NDEBUG
337         const node_t *e;
338 #define NEXT(e)  *((const node_t **)((char *)(e) + (ofs)))
339         for (e = list; e != NULL; e = NEXT(e)) {
340                 assert(e->part == Z);
341         }
342 #undef NEXT
343 #else
344         (void) list;
345         (void) ofs;
346         (void) Z;
347 #endif
348 }  /* ido_check_list */
349
350 /**
351  * Check a local list.
352  */
353 static void check_list(const node_t *list, const partition_t *Z)
354 {
355         do_check_list(list, offsetof(node_t, next), Z);
356 }  /* check_list */
357
358 #else
359 #define check_partition(T)
360 #define check_list(list, Z)
361 #define check_all_partitions(env)
362 #endif /* CHECK_PARTITIONS */
363
364 #ifdef DEBUG_libfirm
365 static inline lattice_elem_t get_partition_type(const partition_t *X);
366
367 /**
368  * Dump partition to output.
369  */
370 static void dump_partition(const char *msg, const partition_t *part)
371 {
372         const node_t   *node;
373         int            first = 1;
374         lattice_elem_t type = get_partition_type(part);
375
376         DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n  ",
377                 msg, part->nr, part->type_is_T_or_C ? "*" : "",
378                 part->n_leader, type));
379         list_for_each_entry(node_t, node, &part->Leader, node_list) {
380                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
381                 first = 0;
382         }
383         if (! list_empty(&part->Follower)) {
384                 DB((dbg, LEVEL_2, "\n---\n  "));
385                 first = 1;
386                 list_for_each_entry(node_t, node, &part->Follower, node_list) {
387                         DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
388                         first = 0;
389                 }
390         }
391         DB((dbg, LEVEL_2, "\n}\n"));
392 }  /* dump_partition */
393
394 /**
395  * Dumps a list.
396  */
397 static void do_dump_list(const char *msg, const node_t *node, int ofs)
398 {
399         const node_t *p;
400         int          first = 1;
401
402 #define GET_LINK(p, ofs)  *((const node_t **)((char *)(p) + (ofs)))
403
404         DB((dbg, LEVEL_3, "%s = {\n  ", msg));
405         for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
406                 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
407                 first = 0;
408         }
409         DB((dbg, LEVEL_3, "\n}\n"));
410
411 #undef GET_LINK
412 }  /* do_dump_list */
413
414 /**
415  * Dumps a race list.
416  */
417 static void dump_race_list(const char *msg, const node_t *list)
418 {
419         do_dump_list(msg, list, offsetof(node_t, race_next));
420 }  /* dump_race_list */
421
422 /**
423  * Dumps a local list.
424  */
425 static void dump_list(const char *msg, const node_t *list)
426 {
427         do_dump_list(msg, list, offsetof(node_t, next));
428 }  /* dump_list */
429
430 /**
431  * Dump all partitions.
432  */
433 static void dump_all_partitions(const environment_t *env)
434 {
435         const partition_t *P;
436
437         DB((dbg, LEVEL_2, "All partitions\n===============\n"));
438         for (P = env->dbg_list; P != NULL; P = P->dbg_next)
439                 dump_partition("", P);
440 }  /* dump_all_partitions */
441
442 /**
443  * Sump a split list.
444  */
445 static void dump_split_list(const partition_t *list)
446 {
447         const partition_t *p;
448
449         DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
450         for (p = list; p != NULL; p = p->split_next)
451                 DB((dbg, LEVEL_2, "part%u, ", p->nr));
452         DB((dbg, LEVEL_2, "\n}\n"));
453 }  /* dump_split_list */
454
455 /**
456  * Dump partition and type for a node.
457  */
458 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local)
459 {
460         ir_node *irn = local != NULL ? local : n;
461         node_t *node = get_irn_node(irn);
462
463         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
464         return 1;
465 }  /* dump_partition_hook */
466
467 #else
468 #define dump_partition(msg, part)
469 #define dump_race_list(msg, list)
470 #define dump_list(msg, list)
471 #define dump_all_partitions(env)
472 #define dump_split_list(list)
473 #endif
474
475 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
476 /**
477  * Verify that a type transition is monotone
478  */
479 static void verify_type(const lattice_elem_t old_type, node_t *node)
480 {
481         if (old_type.tv == node->type.tv) {
482                 /* no change */
483                 return;
484         }
485         if (old_type.tv == tarval_top) {
486                 /* from Top down-to is always allowed */
487                 return;
488         }
489         if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
490                 /* bottom reached */
491                 return;
492         }
493         panic("combo: wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
494 }  /* verify_type */
495
496 #else
497 #define verify_type(old_type, node)
498 #endif
499
500 /**
501  * Compare two pointer values of a listmap.
502  */
503 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
504 {
505         const listmap_entry_t *e1 = (listmap_entry_t*)elt;
506         const listmap_entry_t *e2 = (listmap_entry_t*)key;
507
508         (void) size;
509         return e1->id != e2->id;
510 }  /* listmap_cmp_ptr */
511
512 /**
513  * Initializes a listmap.
514  *
515  * @param map  the listmap
516  */
517 static void listmap_init(listmap_t *map)
518 {
519         map->map    = new_set(listmap_cmp_ptr, 16);
520         map->values = NULL;
521 }  /* listmap_init */
522
523 /**
524  * Terminates a listmap.
525  *
526  * @param map  the listmap
527  */
528 static void listmap_term(listmap_t *map)
529 {
530         del_set(map->map);
531 }  /* listmap_term */
532
533 /**
534  * Return the associated listmap entry for a given id.
535  *
536  * @param map  the listmap
537  * @param id   the id to search for
538  *
539  * @return the associated listmap entry for the given id
540  */
541 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
542 {
543         listmap_entry_t key, *entry;
544
545         key.id   = id;
546         key.list = NULL;
547         key.next = NULL;
548         entry = (listmap_entry_t*)set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
549
550         if (entry->list == NULL) {
551                 /* a new entry, put into the list */
552                 entry->next = map->values;
553                 map->values = entry;
554         }
555         return entry;
556 }  /* listmap_find */
557
558 /**
559  * Calculate the hash value for an opcode map entry.
560  *
561  * @param entry  an opcode map entry
562  *
563  * @return a hash value for the given opcode map entry
564  */
565 static unsigned opcode_hash(const opcode_key_t *entry)
566 {
567         /* we cannot use the ir ops hash function here, because it hashes the
568          * predecessors. */
569         const ir_node *n = entry->irn;
570         ir_opcode code  = get_irn_opcode(n);
571         ir_mode   *mode = get_irn_mode(n);
572         unsigned hash = (unsigned)(PTR_TO_INT(mode) * 9 + code) + get_irn_arity(n);
573
574         if (code == iro_Const)
575                 hash ^= (unsigned)HASH_PTR(get_Const_tarval(n));
576         else if (code == iro_Proj)
577                 hash += (unsigned)get_Proj_proj(n);
578         return hash;
579 }  /* opcode_hash */
580
581 /**
582  * Compare two entries in the opcode map.
583  */
584 static int cmp_opcode(const void *elt, const void *key, size_t size)
585 {
586         const opcode_key_t *o1 = (opcode_key_t*)elt;
587         const opcode_key_t *o2 = (opcode_key_t*)key;
588
589         (void) size;
590
591         return cmp_irn_opcode(o1->irn, o2->irn);
592 }  /* cmp_opcode */
593
594 /**
595  * Compare two Def-Use edges for input position.
596  */
597 static int cmp_def_use_edge(const void *a, const void *b)
598 {
599         const ir_def_use_edge *ea = (const ir_def_use_edge*)a;
600         const ir_def_use_edge *eb = (const ir_def_use_edge*)b;
601
602         /* no overrun, because range is [-1, MAXINT] */
603         return ea->pos - eb->pos;
604 }  /* cmp_def_use_edge */
605
606 /**
607  * We need the Def-Use edges sorted.
608  */
609 static void sort_irn_outs(node_t *node)
610 {
611         ir_node *irn = node->node;
612         int n_outs = get_irn_n_outs(irn);
613
614         if (n_outs > 1) {
615                 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
616         }
617         node->max_user_input = irn->out[n_outs].pos;
618 }  /* sort_irn_outs */
619
620 /**
621  * Return the type of a node.
622  *
623  * @param irn  an IR-node
624  *
625  * @return the associated type of this node
626  */
627 static inline lattice_elem_t get_node_type(const ir_node *irn)
628 {
629         return get_irn_node(irn)->type;
630 }  /* get_node_type */
631
632 /**
633  * Return the tarval of a node.
634  *
635  * @param irn  an IR-node
636  *
637  * @return the associated type of this node
638  */
639 static inline ir_tarval *get_node_tarval(const ir_node *irn)
640 {
641         lattice_elem_t type = get_node_type(irn);
642
643         if (is_tarval(type.tv))
644                 return type.tv;
645         return tarval_bottom;
646 }  /* get_node_type */
647
648 /**
649  * Add a partition to the worklist.
650  */
651 static inline void add_to_worklist(partition_t *X, environment_t *env)
652 {
653         assert(X->on_worklist == 0);
654         DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
655         X->wl_next     = env->worklist;
656         X->on_worklist = 1;
657         env->worklist  = X;
658 }  /* add_to_worklist */
659
660 /**
661  * Create a new empty partition.
662  *
663  * @param env   the environment
664  *
665  * @return a newly allocated partition
666  */
667 static inline partition_t *new_partition(environment_t *env)
668 {
669         partition_t *part = OALLOC(&env->obst, partition_t);
670
671         INIT_LIST_HEAD(&part->Leader);
672         INIT_LIST_HEAD(&part->Follower);
673         INIT_LIST_HEAD(&part->cprop);
674         INIT_LIST_HEAD(&part->cprop_X);
675         part->wl_next         = NULL;
676         part->touched_next    = NULL;
677         part->cprop_next      = NULL;
678         part->split_next      = NULL;
679         part->touched         = NULL;
680         part->n_leader        = 0;
681         part->n_touched       = 0;
682         part->max_user_inputs = 0;
683         part->on_worklist     = 0;
684         part->on_touched      = 0;
685         part->on_cprop        = 0;
686         part->type_is_T_or_C  = 0;
687 #ifdef DEBUG_libfirm
688         part->dbg_next        = env->dbg_list;
689         env->dbg_list         = part;
690         part->nr              = part_nr++;
691 #endif
692
693         return part;
694 }  /* new_partition */
695
696 /**
697  * Get the first node from a partition.
698  */
699 static inline node_t *get_first_node(const partition_t *X)
700 {
701         return list_entry(X->Leader.next, node_t, node_list);
702 }  /* get_first_node */
703
704 /**
705  * Return the type of a partition (assuming partition is non-empty and
706  * all elements have the same type).
707  *
708  * @param X  a partition
709  *
710  * @return the type of the first element of the partition
711  */
712 static inline lattice_elem_t get_partition_type(const partition_t *X)
713 {
714         const node_t *first = get_first_node(X);
715         return first->type;
716 }  /* get_partition_type */
717
718 /**
719  * Creates a partition node for the given IR-node and place it
720  * into the given partition.
721  *
722  * @param irn   an IR-node
723  * @param part  a partition to place the node in
724  * @param env   the environment
725  *
726  * @return the created node
727  */
728 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env)
729 {
730         /* create a partition node and place it in the partition */
731         node_t *node = OALLOC(&env->obst, node_t);
732
733         INIT_LIST_HEAD(&node->node_list);
734         INIT_LIST_HEAD(&node->cprop_list);
735         node->node           = irn;
736         node->part           = part;
737         node->next           = NULL;
738         node->race_next      = NULL;
739         node->type.tv        = tarval_top;
740         node->max_user_input = 0;
741         node->next_edge      = 0;
742         node->n_followers    = 0;
743         node->on_touched     = 0;
744         node->on_cprop       = 0;
745         node->on_fallen      = 0;
746         node->is_follower    = 0;
747         node->flagged        = 0;
748         set_irn_node(irn, node);
749
750         list_add_tail(&node->node_list, &part->Leader);
751         ++part->n_leader;
752
753         return node;
754 }  /* create_partition_node */
755
756 /**
757  * Pre-Walker, initialize all Nodes' type to U or top and place
758  * all nodes into the TOP partition.
759  */
760 static void create_initial_partitions(ir_node *irn, void *ctx)
761 {
762         environment_t *env  = (environment_t*)ctx;
763         partition_t   *part = env->initial;
764         node_t        *node;
765
766         node = create_partition_node(irn, part, env);
767         sort_irn_outs(node);
768         if (node->max_user_input > part->max_user_inputs)
769                 part->max_user_inputs = node->max_user_input;
770
771         if (is_Block(irn)) {
772                 set_Block_phis(irn, NULL);
773         }
774 }  /* create_initial_partitions */
775
776 /**
777  * Post-Walker, collect  all Block-Phi lists, set Cond.
778  */
779 static void init_block_phis(ir_node *irn, void *ctx)
780 {
781         (void) ctx;
782
783         if (is_Phi(irn)) {
784                 ir_node *block = get_nodes_block(irn);
785                 add_Block_phi(block, irn);
786         }
787 }  /* init_block_phis */
788
789 /**
790  * Add a node to the entry.partition.touched set and
791  * node->partition to the touched set if not already there.
792  *
793  * @param y    a node
794  * @param env  the environment
795  */
796 static inline void add_to_touched(node_t *y, environment_t *env)
797 {
798         if (y->on_touched == 0) {
799                 partition_t *part = y->part;
800
801                 y->next       = part->touched;
802                 part->touched = y;
803                 y->on_touched = 1;
804                 ++part->n_touched;
805
806                 if (part->on_touched == 0) {
807                         part->touched_next = env->touched;
808                         env->touched       = part;
809                         part->on_touched   = 1;
810                 }
811
812                 check_list(part->touched, part);
813         }
814 }  /* add_to_touched */
815
816 /**
817  * Place a node on the cprop list.
818  *
819  * @param y    the node
820  * @param env  the environment
821  */
822 static void add_to_cprop(node_t *y, environment_t *env)
823 {
824         ir_node *irn;
825
826         /* Add y to y.partition.cprop. */
827         if (y->on_cprop == 0) {
828                 partition_t *Y = y->part;
829                 ir_node *irn   = y->node;
830
831                 /* place Conds and all its Projs on the cprop_X list */
832                 if (is_Cond(skip_Proj(irn)))
833                         list_add_tail(&y->cprop_list, &Y->cprop_X);
834                 else
835                         list_add_tail(&y->cprop_list, &Y->cprop);
836                 y->on_cprop   = 1;
837
838                 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
839
840                 /* place its partition on the cprop list */
841                 if (Y->on_cprop == 0) {
842                         Y->cprop_next = env->cprop;
843                         env->cprop    = Y;
844                         Y->on_cprop   = 1;
845                 }
846         }
847         irn = y->node;
848         if (get_irn_mode(irn) == mode_T) {
849                 /* mode_T nodes always produce tarval_bottom, so we must explicitly
850                  * add its Projs to get constant evaluation to work */
851                 int i;
852
853                 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
854                         node_t *proj = get_irn_node(get_irn_out(irn, i));
855
856                         add_to_cprop(proj, env);
857                 }
858         } else if (is_Block(irn)) {
859                 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
860                  * if someone placed the block. The Block is only placed if the reachability
861                  * changes, and this must be re-evaluated in compute_Phi(). */
862                 ir_node *phi;
863                 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
864                         node_t *p = get_irn_node(phi);
865                         add_to_cprop(p, env);
866                 }
867         }
868 }  /* add_to_cprop */
869
870 /**
871  * Update the worklist: If Z is on worklist then add Z' to worklist.
872  * Else add the smaller of Z and Z' to worklist.
873  *
874  * @param Z        the Z partition
875  * @param Z_prime  the Z' partition, a previous part of Z
876  * @param env      the environment
877  */
878 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env)
879 {
880         if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
881                 add_to_worklist(Z_prime, env);
882         } else {
883                 add_to_worklist(Z, env);
884         }
885 }  /* update_worklist */
886
887 /**
888  * Make all inputs to x no longer be F.def_use edges.
889  *
890  * @param x  the node
891  */
892 static void move_edges_to_leader(node_t *x)
893 {
894         ir_node     *irn = x->node;
895         int         i, j, k;
896
897         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
898                 node_t  *pred = get_irn_node(get_irn_n(irn, i));
899                 ir_node *p;
900                 int     n;
901
902                 p = pred->node;
903                 n = get_irn_n_outs(p);
904                 for (j = 1; j <= pred->n_followers; ++j) {
905                         if (p->out[j].pos == i && p->out[j].use == irn) {
906                                 /* found a follower edge to x, move it to the Leader */
907                                 ir_def_use_edge edge = p->out[j];
908
909                                 /* remove this edge from the Follower set */
910                                 p->out[j] = p->out[pred->n_followers];
911                                 --pred->n_followers;
912
913                                 /* sort it into the leader set */
914                                 for (k = pred->n_followers + 2; k <= n; ++k) {
915                                         if (p->out[k].pos >= edge.pos)
916                                                 break;
917                                         p->out[k - 1] = p->out[k];
918                                 }
919                                 /* place the new edge here */
920                                 p->out[k - 1] = edge;
921
922                                 /* edge found and moved */
923                                 break;
924                         }
925                 }
926         }
927 }  /* move_edges_to_leader */
928
929 /**
930  * Split a partition that has NO followers by a local list.
931  *
932  * @param Z    partition to split
933  * @param g    a (non-empty) node list
934  * @param env  the environment
935  *
936  * @return  a new partition containing the nodes of g
937  */
938 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env)
939 {
940         partition_t *Z_prime;
941         node_t      *node;
942         unsigned    n = 0;
943         int         max_input;
944
945         dump_partition("Splitting ", Z);
946         dump_list("by list ", g);
947
948         assert(g != NULL);
949
950         /* Remove g from Z. */
951         for (node = g; node != NULL; node = node->next) {
952                 assert(node->part == Z);
953                 list_del(&node->node_list);
954                 ++n;
955         }
956         assert(n < Z->n_leader);
957         Z->n_leader -= n;
958
959         /* Move g to a new partition, Z'. */
960         Z_prime = new_partition(env);
961         max_input = 0;
962         for (node = g; node != NULL; node = node->next) {
963                 list_add_tail(&node->node_list, &Z_prime->Leader);
964                 node->part = Z_prime;
965                 if (node->max_user_input > max_input)
966                         max_input = node->max_user_input;
967         }
968         Z_prime->max_user_inputs = max_input;
969         Z_prime->n_leader        = n;
970
971         check_partition(Z);
972         check_partition(Z_prime);
973
974         /* for now, copy the type info tag, it will be adjusted in split_by(). */
975         Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
976
977         update_worklist(Z, Z_prime, env);
978
979         dump_partition("Now ", Z);
980         dump_partition("Created new ", Z_prime);
981         return Z_prime;
982 }  /* split_no_followers */
983
984 /**
985  * Make the Follower -> Leader transition for a node.
986  *
987  * @param n  the node
988  */
989 static void follower_to_leader(node_t *n)
990 {
991         assert(n->is_follower == 1);
992
993         DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
994         n->is_follower = 0;
995         move_edges_to_leader(n);
996         list_del(&n->node_list);
997         list_add_tail(&n->node_list, &n->part->Leader);
998         ++n->part->n_leader;
999 }  /* follower_to_leader */
1000
1001 /**
1002  * The environment for one race step.
1003  */
1004 typedef struct step_env {
1005         node_t   *initial;    /**< The initial node list. */
1006         node_t   *unwalked;   /**< The unwalked node list. */
1007         node_t   *walked;     /**< The walked node list. */
1008         int      index;       /**< Next index of Follower use_def edge. */
1009         unsigned side;        /**< side number. */
1010 } step_env;
1011
1012 /**
1013  * Return non-zero, if a input is a real follower
1014  *
1015  * @param irn    the node to check
1016  * @param input  number of the input
1017  */
1018 static int is_real_follower(const ir_node *irn, int input)
1019 {
1020         node_t *pred;
1021
1022         switch (get_irn_opcode(irn)) {
1023         case iro_Confirm:
1024                 if (input == 1) {
1025                         /* ignore the Confirm bound input */
1026                         return 0;
1027                 }
1028                 break;
1029         case iro_Mux:
1030                 if (input == 0) {
1031                         /* ignore the Mux sel input */
1032                         return 0;
1033                 }
1034                 break;
1035         case iro_Phi: {
1036                 /* dead inputs are not follower edges */
1037                 ir_node *block = get_nodes_block(irn);
1038                 node_t  *pred  = get_irn_node(get_Block_cfgpred(block, input));
1039
1040                 if (pred->type.tv == tarval_unreachable)
1041                         return 0;
1042                 break;
1043         }
1044         case iro_Sub:
1045         case iro_Shr:
1046         case iro_Shl:
1047         case iro_Shrs:
1048         case iro_Rotl:
1049                 if (input == 1) {
1050                         /* only a Sub x,0 / Shift x,0 might be a follower */
1051                         return 0;
1052                 }
1053                 break;
1054         case iro_Add:
1055         case iro_Or:
1056         case iro_Eor:
1057                 pred = get_irn_node(get_irn_n(irn, input));
1058                 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1059                         return 0;
1060                 break;
1061         case iro_Mul:
1062                 pred = get_irn_node(get_irn_n(irn, input));
1063                 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1064                         return 0;
1065                 break;
1066         case iro_And:
1067                 pred = get_irn_node(get_irn_n(irn, input));
1068                 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1069                         return 0;
1070                 break;
1071         default:
1072                 assert(!"opcode not implemented yet");
1073                 break;
1074         }
1075         return 1;
1076 }  /* is_real_follower */
1077
1078 /**
1079  * Do one step in the race.
1080  */
1081 static int step(step_env *env)
1082 {
1083         node_t *n;
1084
1085         if (env->initial != NULL) {
1086                 /* Move node from initial to unwalked */
1087                 n             = env->initial;
1088                 env->initial  = n->race_next;
1089
1090                 n->race_next  = env->unwalked;
1091                 env->unwalked = n;
1092
1093                 return 0;
1094         }
1095
1096         while (env->unwalked != NULL) {
1097                 /* let n be the first node in unwalked */
1098                 n = env->unwalked;
1099                 while (env->index < n->n_followers) {
1100                         const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1101
1102                         /* let m be n.F.def_use[index] */
1103                         node_t *m = get_irn_node(edge->use);
1104
1105                         assert(m->is_follower);
1106                         /*
1107                          * Some inputs, like the get_Confirm_bound are NOT
1108                          * real followers, sort them out.
1109                          */
1110                         if (! is_real_follower(m->node, edge->pos)) {
1111                                 ++env->index;
1112                                 continue;
1113                         }
1114                         ++env->index;
1115
1116                         /* only followers from our partition */
1117                         if (m->part != n->part)
1118                                 continue;
1119
1120                         if ((m->flagged & env->side) == 0) {
1121                                 m->flagged |= env->side;
1122
1123                                 if (m->flagged != 3) {
1124                                         /* visited the first time */
1125                                         /* add m to unwalked not as first node (we might still need to
1126                                            check for more follower node */
1127                                         m->race_next = n->race_next;
1128                                         n->race_next = m;
1129                                         return 0;
1130                                 }
1131                                 /* else already visited by the other side and on the other list */
1132                         }
1133                 }
1134                 /* move n to walked */
1135                 env->unwalked = n->race_next;
1136                 n->race_next  = env->walked;
1137                 env->walked   = n;
1138                 env->index    = 0;
1139         }
1140         return 1;
1141 }  /* step */
1142
1143 /**
1144  * Clear the flags from a list and check for
1145  * nodes that where touched from both sides.
1146  *
1147  * @param list  the list
1148  */
1149 static int clear_flags(node_t *list)
1150 {
1151         int    res = 0;
1152         node_t *n;
1153
1154         for (n = list; n != NULL; n = n->race_next) {
1155                 if (n->flagged == 3) {
1156                         /* we reach a follower from both sides, this will split congruent
1157                          * inputs and make it a leader. */
1158                         follower_to_leader(n);
1159                         res = 1;
1160                 }
1161                 n->flagged = 0;
1162         }
1163         return res;
1164 }  /* clear_flags */
1165
1166 /**
1167  * Split a partition by a local list using the race.
1168  *
1169  * @param pX   pointer to the partition to split, might be changed!
1170  * @param gg   a (non-empty) node list
1171  * @param env  the environment
1172  *
1173  * @return  a new partition containing the nodes of gg
1174  */
1175 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env)
1176 {
1177         partition_t *X = *pX;
1178         partition_t *X_prime;
1179         list_head   tmp;
1180         step_env    senv[2];
1181         node_t      *g, *h, *node, *t;
1182         int         max_input, transitions, winner, shf;
1183         unsigned    n;
1184         DEBUG_ONLY(static int run = 0;)
1185
1186         DB((dbg, LEVEL_2, "Run %d ", run++));
1187         if (list_empty(&X->Follower)) {
1188                 /* if the partition has NO follower, we can use the fast
1189                    splitting algorithm. */
1190                 return split_no_followers(X, gg, env);
1191         }
1192         /* else do the race */
1193
1194         dump_partition("Splitting ", X);
1195         dump_list("by list ", gg);
1196
1197         INIT_LIST_HEAD(&tmp);
1198
1199         /* Remove gg from X.Leader and put into g */
1200         g = NULL;
1201         for (node = gg; node != NULL; node = node->next) {
1202                 assert(node->part == X);
1203                 assert(node->is_follower == 0);
1204
1205                 list_del(&node->node_list);
1206                 list_add_tail(&node->node_list, &tmp);
1207                 node->race_next = g;
1208                 g               = node;
1209         }
1210         /* produce h */
1211         h = NULL;
1212         list_for_each_entry(node_t, node, &X->Leader, node_list) {
1213                 node->race_next = h;
1214                 h               = node;
1215         }
1216         /* restore X.Leader */
1217         list_splice(&tmp, &X->Leader);
1218
1219         senv[0].initial   = g;
1220         senv[0].unwalked  = NULL;
1221         senv[0].walked    = NULL;
1222         senv[0].index     = 0;
1223         senv[0].side      = 1;
1224
1225         senv[1].initial   = h;
1226         senv[1].unwalked  = NULL;
1227         senv[1].walked    = NULL;
1228         senv[1].index     = 0;
1229         senv[1].side      = 2;
1230
1231         /*
1232          * Some informations on the race that are not stated clearly in Click's
1233          * thesis.
1234          * 1) A follower stays on the side that reach him first.
1235          * 2) If the other side reches a follower, if will be converted to
1236          *    a leader. /This must be done after the race is over, else the
1237          *    edges we are iterating on are renumbered./
1238          * 3) /New leader might end up on both sides./
1239          * 4) /If one side ends up with new Leaders, we must ensure that
1240          *    they can split out by opcode, hence we have to put _every_
1241          *    partition with new Leader nodes on the cprop list, as
1242          *    opcode splitting is done by split_by() at the end of
1243          *    constant propagation./
1244          */
1245         for (;;) {
1246                 if (step(&senv[0])) {
1247                         winner = 0;
1248                         break;
1249                 }
1250                 if (step(&senv[1])) {
1251                         winner = 1;
1252                         break;
1253                 }
1254         }
1255         assert(senv[winner].initial == NULL);
1256         assert(senv[winner].unwalked == NULL);
1257
1258         /* clear flags from walked/unwalked */
1259         shf = winner;
1260         transitions  = clear_flags(senv[0].unwalked) << shf;
1261         transitions |= clear_flags(senv[0].walked)   << shf;
1262         shf ^= 1;
1263         transitions |= clear_flags(senv[1].unwalked) << shf;
1264         transitions |= clear_flags(senv[1].walked)   << shf;
1265
1266         dump_race_list("winner ", senv[winner].walked);
1267
1268         /* Move walked_{winner} to a new partition, X'. */
1269         X_prime   = new_partition(env);
1270         max_input = 0;
1271         n         = 0;
1272         for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1273                 list_del(&node->node_list);
1274                 node->part = X_prime;
1275                 if (node->is_follower) {
1276                         list_add_tail(&node->node_list, &X_prime->Follower);
1277                 } else {
1278                         list_add_tail(&node->node_list, &X_prime->Leader);
1279                         ++n;
1280                 }
1281                 if (node->max_user_input > max_input)
1282                         max_input = node->max_user_input;
1283         }
1284         X_prime->n_leader        = n;
1285         X_prime->max_user_inputs = max_input;
1286         X->n_leader             -= X_prime->n_leader;
1287
1288         /* for now, copy the type info tag, it will be adjusted in split_by(). */
1289         X_prime->type_is_T_or_C = X->type_is_T_or_C;
1290
1291         /*
1292          * Even if a follower was not checked by both sides, it might have
1293          * loose its congruence, so we need to check this case for all follower.
1294          */
1295         list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1296                 if (identity(node) == node) {
1297                         follower_to_leader(node);
1298                         transitions |= 1;
1299                 }
1300         }
1301
1302         check_partition(X);
1303         check_partition(X_prime);
1304
1305         /* X' is the smaller part */
1306         add_to_worklist(X_prime, env);
1307
1308         /*
1309          * If there where follower to leader transitions, ensure that the nodes
1310          * can be split out if necessary.
1311          */
1312         if (transitions & 1) {
1313                 /* place winner partition on the cprop list */
1314                 if (X_prime->on_cprop == 0) {
1315                         X_prime->cprop_next = env->cprop;
1316                         env->cprop          = X_prime;
1317                         X_prime->on_cprop   = 1;
1318                 }
1319         }
1320         if (transitions & 2) {
1321                 /* place other partition on the cprop list */
1322                 if (X->on_cprop == 0) {
1323                         X->cprop_next = env->cprop;
1324                         env->cprop    = X;
1325                         X->on_cprop   = 1;
1326                 }
1327         }
1328
1329         dump_partition("Now ", X);
1330         dump_partition("Created new ", X_prime);
1331
1332         /* we have to ensure that the partition containing g is returned */
1333         if (winner != 0) {
1334                 *pX = X_prime;
1335                 return X;
1336         }
1337
1338         return X_prime;
1339 }  /* split */
1340
1341 /**
1342  * Returns non-zero if the i'th input of a Phi node is live.
1343  *
1344  * @param phi  a Phi-node
1345  * @param i    an input number
1346  *
1347  * @return non-zero if the i'th input of the given Phi node is live
1348  */
1349 static int is_live_input(ir_node *phi, int i)
1350 {
1351         if (i >= 0) {
1352                 ir_node        *block = get_nodes_block(phi);
1353                 ir_node        *pred  = get_Block_cfgpred(block, i);
1354                 lattice_elem_t type   = get_node_type(pred);
1355
1356                 return type.tv != tarval_unreachable;
1357         }
1358         /* else it's the control input, always live */
1359         return 1;
1360 }  /* is_live_input */
1361
1362 /**
1363  * Return non-zero if a type is a constant.
1364  */
1365 static int is_constant_type(lattice_elem_t type)
1366 {
1367         if (type.tv != tarval_bottom && type.tv != tarval_top)
1368                 return 1;
1369         return 0;
1370 }  /* is_constant_type */
1371
1372 /**
1373  * Check whether a type is neither Top or a constant.
1374  * Note: U is handled like Top here, R is a constant.
1375  *
1376  * @param type  the type to check
1377  */
1378 static int type_is_neither_top_nor_const(const lattice_elem_t type)
1379 {
1380         if (is_tarval(type.tv)) {
1381                 if (type.tv == tarval_top)
1382                         return 0;
1383                 if (tarval_is_constant(type.tv))
1384                         return 0;
1385         } else {
1386                 /* is a symconst */
1387                 return 0;
1388         }
1389         return 1;
1390 }  /* type_is_neither_top_nor_const */
1391
1392 /**
1393  * Collect nodes to the touched list.
1394  *
1395  * @param list  the list which contains the nodes that must be evaluated
1396  * @param idx   the index of the def_use edge to evaluate
1397  * @param env   the environment
1398  */
1399 static void collect_touched(list_head *list, int idx, environment_t *env)
1400 {
1401         node_t  *x, *y;
1402         int     end_idx = env->end_idx;
1403
1404         list_for_each_entry(node_t, x, list, node_list) {
1405                 int num_edges;
1406
1407                 if (idx == -1) {
1408                         /* leader edges start AFTER follower edges */
1409                         x->next_edge = x->n_followers + 1;
1410                 }
1411                 num_edges = get_irn_n_outs(x->node);
1412
1413                 /* for all edges in x.L.def_use_{idx} */
1414                 while (x->next_edge <= num_edges) {
1415                         const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1416                         ir_node               *succ;
1417
1418                         /* check if we have necessary edges */
1419                         if (edge->pos > idx)
1420                                 break;
1421
1422                         ++x->next_edge;
1423
1424                         succ = edge->use;
1425
1426                         /* only non-commutative nodes */
1427                         if (env->commutative &&
1428                             (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1429                                 continue;
1430
1431                         /* ignore the "control input" for non-pinned nodes
1432                         if we are running in GCSE mode */
1433                         if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1434                                 continue;
1435
1436                         y = get_irn_node(succ);
1437                         assert(get_irn_n(succ, idx) == x->node);
1438
1439                         /* ignore block edges touching followers */
1440                         if (idx == -1 && y->is_follower)
1441                                 continue;
1442
1443                         if (is_constant_type(y->type)) {
1444                                 unsigned  code = get_irn_opcode(succ);
1445                                 if (code == iro_Sub || code == iro_Cmp)
1446                                         add_to_cprop(y, env);
1447                         }
1448
1449                         /* Partitions of constants should not be split simply because their Nodes have unequal
1450                            functions or incongruent inputs. */
1451                         if (type_is_neither_top_nor_const(y->type) &&
1452                                 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1453                                         add_to_touched(y, env);
1454                         }
1455                 }
1456         }
1457 }  /* collect_touched */
1458
1459 /**
1460  * Collect commutative nodes to the touched list.
1461  *
1462  * @param list  the list which contains the nodes that must be evaluated
1463  * @param env   the environment
1464  */
1465 static void collect_commutative_touched(list_head *list, environment_t *env)
1466 {
1467         node_t  *x, *y;
1468
1469         list_for_each_entry(node_t, x, list, node_list) {
1470                 int num_edges;
1471
1472                 num_edges = get_irn_n_outs(x->node);
1473
1474                 x->next_edge = x->n_followers + 1;
1475
1476                 /* for all edges in x.L.def_use_{idx} */
1477                 while (x->next_edge <= num_edges) {
1478                         const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1479                         ir_node               *succ;
1480
1481                         /* check if we have necessary edges */
1482                         if (edge->pos > 1)
1483                                 break;
1484
1485                         ++x->next_edge;
1486                         if (edge->pos < 0)
1487                                 continue;
1488
1489                         succ = edge->use;
1490
1491                         /* only commutative nodes */
1492                         if (!is_op_commutative(get_irn_op(succ)))
1493                                 continue;
1494
1495                         y = get_irn_node(succ);
1496                         if (is_constant_type(y->type)) {
1497                                 unsigned code = get_irn_opcode(succ);
1498                                 if (code == iro_Eor)
1499                                         add_to_cprop(y, env);
1500                         }
1501
1502                         /* Partitions of constants should not be split simply because their Nodes have unequal
1503                            functions or incongruent inputs. */
1504                         if (type_is_neither_top_nor_const(y->type)) {
1505                                 add_to_touched(y, env);
1506                         }
1507                 }
1508         }
1509 }  /* collect_commutative_touched */
1510
1511 /**
1512  * Split the partitions if caused by the first entry on the worklist.
1513  *
1514  * @param env  the environment
1515  */
1516 static void cause_splits(environment_t *env)
1517 {
1518         partition_t *X, *Z, *N;
1519         int         idx;
1520
1521         /* remove the first partition from the worklist */
1522         X = env->worklist;
1523         env->worklist  = X->wl_next;
1524         X->on_worklist = 0;
1525
1526         dump_partition("Cause_split: ", X);
1527
1528         if (env->commutative) {
1529                 /* handle commutative nodes first */
1530
1531                 /* empty the touched set: already done, just clear the list */
1532                 env->touched = NULL;
1533
1534                 collect_commutative_touched(&X->Leader, env);
1535                 collect_commutative_touched(&X->Follower, env);
1536
1537                 for (Z = env->touched; Z != NULL; Z = N) {
1538                         node_t   *e, *n;
1539                         node_t   *touched     = Z->touched;
1540                         node_t   *touched_aa  = NULL;
1541                         node_t   *touched_ab  = NULL;
1542                         unsigned n_touched_aa = 0;
1543                         unsigned n_touched_ab = 0;
1544
1545                         assert(Z->touched != NULL);
1546
1547                         /* beware, split might change Z */
1548                         N = Z->touched_next;
1549
1550                         /* remove it from the touched set */
1551                         Z->on_touched = 0;
1552
1553                         /* Empty local Z.touched. */
1554                         for (e = touched; e != NULL; e = n) {
1555                                 node_t *left  = get_irn_node(get_irn_n(e->node, 0));
1556                                 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1557
1558                                 assert(e->is_follower == 0);
1559                                 e->on_touched = 0;
1560                                 n = e->next;
1561
1562                                 /*
1563                                  * Note: op(a, a) is NOT congruent to op(a, b).
1564                                  * So, we must split the touched list.
1565                                  */
1566                                 if (left->part == right->part) {
1567                                         e->next = touched_aa;
1568                                         touched_aa = e;
1569                                         ++n_touched_aa;
1570                                 } else {
1571                                         e->next = touched_ab;
1572                                         touched_ab = e;
1573                                         ++n_touched_ab;
1574                                 }
1575                         }
1576                         assert(n_touched_aa + n_touched_ab == Z->n_touched);
1577                         Z->touched   = NULL;
1578                         Z->n_touched = 0;
1579
1580                         if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1581                                 partition_t *Z_prime = Z;
1582                                 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1583                                 split(&Z_prime, touched_aa, env);
1584                         } else
1585                                 assert(n_touched_aa <= Z->n_leader);
1586
1587                         if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1588                                 partition_t *Z_prime = Z;
1589                                 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1590                                 split(&Z_prime, touched_ab, env);
1591                         } else
1592                                 assert(n_touched_ab <= Z->n_leader);
1593                 }
1594         }
1595
1596         /* combine temporary leader and follower list */
1597         for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1598                 /* empty the touched set: already done, just clear the list */
1599                 env->touched = NULL;
1600
1601                 collect_touched(&X->Leader, idx, env);
1602                 collect_touched(&X->Follower, idx, env);
1603
1604                 for (Z = env->touched; Z != NULL; Z = N) {
1605                         node_t   *e;
1606                         node_t   *touched  = Z->touched;
1607                         unsigned n_touched = Z->n_touched;
1608
1609                         assert(Z->touched != NULL);
1610
1611                         /* beware, split might change Z */
1612                         N = Z->touched_next;
1613
1614                         /* remove it from the touched set */
1615                         Z->on_touched = 0;
1616
1617                         /* Empty local Z.touched. */
1618                         for (e = touched; e != NULL; e = e->next) {
1619                                 assert(e->is_follower == 0);
1620                                 e->on_touched = 0;
1621                         }
1622                         Z->touched   = NULL;
1623                         Z->n_touched = 0;
1624
1625                         if (0 < n_touched && n_touched < Z->n_leader) {
1626                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1627                                 split(&Z, touched, env);
1628                         } else
1629                                 assert(n_touched <= Z->n_leader);
1630                 }
1631         }
1632 }  /* cause_splits */
1633
1634 /**
1635  * Implements split_by_what(): Split a partition by characteristics given
1636  * by the what function.
1637  *
1638  * @param X     the partition to split
1639  * @param What  a function returning an Id for every node of the partition X
1640  * @param P     a list to store the result partitions
1641  * @param env   the environment
1642  *
1643  * @return *P
1644  */
1645 static partition_t *split_by_what(partition_t *X, what_func What,
1646                                   partition_t **P, environment_t *env)
1647 {
1648         node_t          *x, *S;
1649         listmap_t       map;
1650         listmap_entry_t *iter;
1651         partition_t     *R;
1652
1653         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1654         listmap_init(&map);
1655         list_for_each_entry(node_t, x, &X->Leader, node_list) {
1656                 void            *id = What(x, env);
1657                 listmap_entry_t *entry;
1658
1659                 if (id == NULL) {
1660                         /* input not allowed, ignore */
1661                         continue;
1662                 }
1663                 /* Add x to map[What(x)]. */
1664                 entry = listmap_find(&map, id);
1665                 x->next     = entry->list;
1666                 entry->list = x;
1667         }
1668         /* Let P be a set of Partitions. */
1669
1670         /* for all sets S except one in the range of map do */
1671         for (iter = map.values; iter != NULL; iter = iter->next) {
1672                 if (iter->next == NULL) {
1673                         /* this is the last entry, ignore */
1674                         break;
1675                 }
1676                 S = iter->list;
1677
1678                 /* Add SPLIT( X, S ) to P. */
1679                 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1680                 R = split(&X, S, env);
1681                 R->split_next = *P;
1682                 *P            = R;
1683         }
1684         /* Add X to P. */
1685         X->split_next = *P;
1686         *P            = X;
1687
1688         listmap_term(&map);
1689         return *P;
1690 }  /* split_by_what */
1691
1692 /** lambda n.(n.type) */
1693 static void *lambda_type(const node_t *node, environment_t *env)
1694 {
1695         (void)env;
1696         return node->type.tv;
1697 }  /* lambda_type */
1698
1699 /** lambda n.(n.opcode) */
1700 static void *lambda_opcode(const node_t *node, environment_t *env)
1701 {
1702         opcode_key_t key, *entry;
1703
1704         key.irn = node->node;
1705
1706         entry = (opcode_key_t*)set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1707         return entry;
1708 }  /* lambda_opcode */
1709
1710 /** lambda n.(n[i].partition) */
1711 static void *lambda_partition(const node_t *node, environment_t *env)
1712 {
1713         ir_node *skipped = skip_Proj(node->node);
1714         ir_node *pred;
1715         node_t  *p;
1716         int     i = env->lambda_input;
1717
1718         if (i >= get_irn_arity(node->node)) {
1719                 /*
1720                  * We are outside the allowed range: This can happen even
1721                  * if we have split by opcode first: doing so might move Followers
1722                  * to Leaders and those will have a different opcode!
1723                  * Note that in this case the partition is on the cprop list and will be
1724                  * split again.
1725                  */
1726                 return NULL;
1727         }
1728
1729         /* ignore the "control input" for non-pinned nodes
1730            if we are running in GCSE mode */
1731         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1732                 return NULL;
1733
1734         pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1735         p    = get_irn_node(pred);
1736         return p->part;
1737 }  /* lambda_partition */
1738
1739 /** lambda n.(n[i].partition) for commutative nodes */
1740 static void *lambda_commutative_partition(const node_t *node, environment_t *env)
1741 {
1742         ir_node     *irn     = node->node;
1743         ir_node     *skipped = skip_Proj(irn);
1744         ir_node     *pred, *left, *right;
1745         node_t      *p;
1746         partition_t *pl, *pr;
1747         int         i = env->lambda_input;
1748
1749         if (i >= get_irn_arity(node->node)) {
1750                 /*
1751                  * We are outside the allowed range: This can happen even
1752                  * if we have split by opcode first: doing so might move Followers
1753                  * to Leaders and those will have a different opcode!
1754                  * Note that in this case the partition is on the cprop list and will be
1755                  * split again.
1756                  */
1757                 return NULL;
1758         }
1759
1760         /* ignore the "control input" for non-pinned nodes
1761            if we are running in GCSE mode */
1762         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1763                 return NULL;
1764
1765         if (i == -1) {
1766                 pred = get_irn_n(skipped, i);
1767                 p    = get_irn_node(pred);
1768                 return p->part;
1769         }
1770
1771         if (is_op_commutative(get_irn_op(irn))) {
1772                 /* normalize partition order by returning the "smaller" on input 0,
1773                    the "bigger" on input 1. */
1774                 left  = get_binop_left(irn);
1775                 pl    = get_irn_node(left)->part;
1776                 right = get_binop_right(irn);
1777                 pr    = get_irn_node(right)->part;
1778
1779                 if (i == 0)
1780                         return pl < pr ? pl : pr;
1781                 else
1782                 return pl > pr ? pl : pr;
1783         } else {
1784                 /* a not split out Follower */
1785                 pred = get_irn_n(irn, i);
1786                 p    = get_irn_node(pred);
1787
1788                 return p->part;
1789         }
1790 }  /* lambda_commutative_partition */
1791
1792 /**
1793  * Returns true if a type is a constant (and NOT Top
1794  * or Bottom).
1795  */
1796 static int is_con(const lattice_elem_t type)
1797 {
1798         /* be conservative */
1799         if (is_tarval(type.tv))
1800                 return tarval_is_constant(type.tv);
1801         return is_entity(type.sym.entity_p);
1802 }  /* is_con */
1803
1804 /**
1805  * Implements split_by().
1806  *
1807  * @param X    the partition to split
1808  * @param env  the environment
1809  */
1810 static void split_by(partition_t *X, environment_t *env)
1811 {
1812         partition_t *I, *P = NULL;
1813         int         input;
1814
1815         dump_partition("split_by", X);
1816
1817         if (X->n_leader == 1) {
1818                 /* we have only one leader, no need to split, just check its type */
1819                 node_t *x = get_first_node(X);
1820                 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1821                 return;
1822         }
1823
1824         DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1825         P = split_by_what(X, lambda_type, &P, env);
1826         dump_split_list(P);
1827
1828         /* adjust the type tags, we have split partitions by type */
1829         for (I = P; I != NULL; I = I->split_next) {
1830                 node_t *x = get_first_node(I);
1831                 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1832         }
1833
1834         do {
1835                 partition_t *Y = P;
1836
1837                 P = P->split_next;
1838                 if (Y->n_leader > 1) {
1839                         /* we do not want split the TOP or constant partitions */
1840                         if (! Y->type_is_T_or_C) {
1841                                 partition_t *Q = NULL;
1842
1843                                 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1844                                 Q = split_by_what(Y, lambda_opcode, &Q, env);
1845                                 dump_split_list(Q);
1846
1847                                 do {
1848                                         partition_t *Z = Q;
1849
1850                                         Q = Q->split_next;
1851                                         if (Z->n_leader > 1) {
1852                                                 const node_t *first = get_first_node(Z);
1853                                                 int          arity  = get_irn_arity(first->node);
1854                                                 partition_t  *R, *S;
1855                                                 what_func    what = lambda_partition;
1856                                                 DEBUG_ONLY(char buf[64];)
1857
1858                                                 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1859                                                         what = lambda_commutative_partition;
1860
1861                                                 /*
1862                                                  * BEWARE: during splitting by input 2 for instance we might
1863                                                  * create new partitions which are different by input 1, so collect
1864                                                  * them and split further.
1865                                                  */
1866                                                 Z->split_next = NULL;
1867                                                 R             = Z;
1868                                                 S             = NULL;
1869                                                 for (input = arity - 1; input >= -1; --input) {
1870                                                         do {
1871                                                                 partition_t *Z_prime = R;
1872
1873                                                                 R = R->split_next;
1874                                                                 if (Z_prime->n_leader > 1) {
1875                                                                         env->lambda_input = input;
1876                                                                         DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1877                                                                         DEBUG_ONLY(what_reason = buf;)
1878                                                                         S = split_by_what(Z_prime, what, &S, env);
1879                                                                         dump_split_list(S);
1880                                                                 } else {
1881                                                                         Z_prime->split_next = S;
1882                                                                         S                   = Z_prime;
1883                                                                 }
1884                                                         } while (R != NULL);
1885                                                         R = S;
1886                                                         S = NULL;
1887                                                 }
1888                                         }
1889                                 } while (Q != NULL);
1890                         }
1891                 }
1892         } while (P != NULL);
1893 }  /* split_by */
1894
1895 /**
1896  * (Re-)compute the type for a given node.
1897  *
1898  * @param node  the node
1899  */
1900 static void default_compute(node_t *node)
1901 {
1902         int     i;
1903         ir_node *irn = node->node;
1904
1905         /* if any of the data inputs have type top, the result is type top */
1906         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1907                 ir_node *pred = get_irn_n(irn, i);
1908                 node_t  *p    = get_irn_node(pred);
1909
1910                 if (p->type.tv == tarval_top) {
1911                         node->type.tv = tarval_top;
1912                         return;
1913                 }
1914         }
1915
1916         if (get_irn_mode(node->node) == mode_X)
1917                 node->type.tv = tarval_reachable;
1918         else
1919                 node->type.tv = computed_value(irn);
1920 }  /* default_compute */
1921
1922 /**
1923  * (Re-)compute the type for a Block node.
1924  *
1925  * @param node  the node
1926  */
1927 static void compute_Block(node_t *node)
1928 {
1929         int     i;
1930         ir_node *block = node->node;
1931
1932         if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
1933                 /* start block and labelled blocks are always reachable */
1934                 node->type.tv = tarval_reachable;
1935                 return;
1936         }
1937
1938         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1939                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1940
1941                 if (pred->type.tv == tarval_reachable) {
1942                         /* A block is reachable, if at least of predecessor is reachable. */
1943                         node->type.tv = tarval_reachable;
1944                         return;
1945                 }
1946         }
1947         node->type.tv = tarval_top;
1948 }  /* compute_Block */
1949
1950 /**
1951  * (Re-)compute the type for a Bad node.
1952  *
1953  * @param node  the node
1954  */
1955 static void compute_Bad(node_t *node)
1956 {
1957         /* Bad nodes ALWAYS compute Top */
1958         node->type.tv = tarval_top;
1959 }  /* compute_Bad */
1960
1961 /**
1962  * (Re-)compute the type for an Unknown node.
1963  *
1964  * @param node  the node
1965  */
1966 static void compute_Unknown(node_t *node)
1967 {
1968         /* While Unknown nodes should compute Top this is dangerous:
1969          * a Top input to a Cond would lead to BOTH control flows unreachable.
1970          * While this is correct in the given semantics, it would destroy the Firm
1971          * graph.
1972          *
1973          * It would be safe to compute Top IF it can be assured, that only Cmp
1974          * nodes are inputs to Conds. We check that first.
1975          * This is the way Frontends typically build Firm, but some optimizations
1976          * (jump threading for instance) might replace them by Phib's...
1977          */
1978         node->type.tv = tarval_UNKNOWN;
1979 }  /* compute_Unknown */
1980
1981 /**
1982  * (Re-)compute the type for a Jmp node.
1983  *
1984  * @param node  the node
1985  */
1986 static void compute_Jmp(node_t *node)
1987 {
1988         node_t *block = get_irn_node(get_nodes_block(node->node));
1989
1990         node->type = block->type;
1991 }  /* compute_Jmp */
1992
1993 /**
1994  * (Re-)compute the type for the Return node.
1995  *
1996  * @param node  the node
1997  */
1998 static void compute_Return(node_t *node)
1999 {
2000         /* The Return node is NOT dead if it is in a reachable block.
2001          * This is already checked in compute(). so we can return
2002          * Reachable here. */
2003         node->type.tv = tarval_reachable;
2004 }  /* compute_Return */
2005
2006 /**
2007  * (Re-)compute the type for the End node.
2008  *
2009  * @param node  the node
2010  */
2011 static void compute_End(node_t *node)
2012 {
2013         /* the End node is NOT dead of course */
2014         node->type.tv = tarval_reachable;
2015 }  /* compute_End */
2016
2017 /**
2018  * (Re-)compute the type for a Call.
2019  *
2020  * @param node  the node
2021  */
2022 static void compute_Call(node_t *node)
2023 {
2024         /*
2025          * A Call computes always bottom, even if it has Unknown
2026          * predecessors.
2027          */
2028         node->type.tv = tarval_bottom;
2029 }  /* compute_Call */
2030
2031 /**
2032  * (Re-)compute the type for a SymConst node.
2033  *
2034  * @param node  the node
2035  */
2036 static void compute_SymConst(node_t *node)
2037 {
2038         ir_node *irn = node->node;
2039         node_t  *block = get_irn_node(get_nodes_block(irn));
2040
2041         if (block->type.tv == tarval_unreachable) {
2042                 node->type.tv = tarval_top;
2043                 return;
2044         }
2045         switch (get_SymConst_kind(irn)) {
2046         case symconst_addr_ent:
2047                 node->type.sym = get_SymConst_symbol(irn);
2048                 break;
2049         default:
2050                 node->type.tv = computed_value(irn);
2051         }
2052 }  /* compute_SymConst */
2053
2054 /**
2055  * (Re-)compute the type for a Phi node.
2056  *
2057  * @param node  the node
2058  */
2059 static void compute_Phi(node_t *node)
2060 {
2061         int            i;
2062         ir_node        *phi = node->node;
2063         lattice_elem_t type;
2064
2065         /* if a Phi is in a unreachable block, its type is TOP */
2066         node_t *block = get_irn_node(get_nodes_block(phi));
2067
2068         if (block->type.tv == tarval_unreachable) {
2069                 node->type.tv = tarval_top;
2070                 return;
2071         }
2072
2073         /* Phi implements the Meet operation */
2074         type.tv = tarval_top;
2075         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2076                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
2077                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2078
2079                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2080                         /* ignore TOP inputs: We must check here for unreachable blocks,
2081                            because Firm constants live in the Start Block are NEVER Top.
2082                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2083                            comes from a unreachable input. */
2084                         continue;
2085                 }
2086                 if (pred->type.tv == tarval_bottom) {
2087                         node->type.tv = tarval_bottom;
2088                         return;
2089                 } else if (type.tv == tarval_top) {
2090                         /* first constant found */
2091                         type = pred->type;
2092                 } else if (type.tv != pred->type.tv) {
2093                         /* different constants or tarval_bottom */
2094                         node->type.tv = tarval_bottom;
2095                         return;
2096                 }
2097                 /* else nothing, constants are the same */
2098         }
2099         node->type = type;
2100 }  /* compute_Phi */
2101
2102 /**
2103  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2104  *
2105  * @param node  the node
2106  */
2107 static void compute_Add(node_t *node)
2108 {
2109         ir_node        *sub = node->node;
2110         node_t         *l   = get_irn_node(get_Add_left(sub));
2111         node_t         *r   = get_irn_node(get_Add_right(sub));
2112         lattice_elem_t a    = l->type;
2113         lattice_elem_t b    = r->type;
2114         ir_mode        *mode;
2115
2116         if (a.tv == tarval_top || b.tv == tarval_top) {
2117                 node->type.tv = tarval_top;
2118         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2119                 node->type.tv = tarval_bottom;
2120         } else {
2121                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2122                    must call tarval_add() first to handle this case! */
2123                 if (is_tarval(a.tv)) {
2124                         if (is_tarval(b.tv)) {
2125                                 node->type.tv = tarval_add(a.tv, b.tv);
2126                                 return;
2127                         }
2128                         mode = get_tarval_mode(a.tv);
2129                         if (a.tv == get_mode_null(mode)) {
2130                                 node->type = b;
2131                                 return;
2132                         }
2133                 } else if (is_tarval(b.tv)) {
2134                         mode = get_tarval_mode(b.tv);
2135                         if (b.tv == get_mode_null(mode)) {
2136                                 node->type = a;
2137                                 return;
2138                         }
2139                 }
2140                 node->type.tv = tarval_bottom;
2141         }
2142 }  /* compute_Add */
2143
2144 /**
2145  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2146  *
2147  * @param node  the node
2148  */
2149 static void compute_Sub(node_t *node)
2150 {
2151         ir_node        *sub = node->node;
2152         node_t         *l   = get_irn_node(get_Sub_left(sub));
2153         node_t         *r   = get_irn_node(get_Sub_right(sub));
2154         lattice_elem_t a    = l->type;
2155         lattice_elem_t b    = r->type;
2156         ir_tarval      *tv;
2157
2158         if (a.tv == tarval_top || b.tv == tarval_top) {
2159                 node->type.tv = tarval_top;
2160         } else if (is_con(a) && is_con(b)) {
2161                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2162                         node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2163                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2164                         node->type = b;
2165                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2166                         node->type = a;
2167                 } else {
2168                         node->type.tv = tarval_bottom;
2169                 }
2170         } else if (r->part == l->part &&
2171                    (!mode_is_float(get_irn_mode(l->node)))) {
2172                 /*
2173                  * BEWARE: a - a is NOT always 0 for floating Point values, as
2174                  * NaN op NaN = NaN, so we must check this here.
2175                  */
2176                 ir_mode *mode = get_irn_mode(sub);
2177                 tv = get_mode_null(mode);
2178
2179                 /* if the node was ONCE evaluated by all constants, but now
2180                    this breaks AND we get from the argument partitions a different
2181                    result, switch to bottom.
2182                    This happens because initially all nodes are in the same partition ... */
2183                 if (node->type.tv != tv)
2184                         tv = tarval_bottom;
2185                 node->type.tv = tv;
2186         } else {
2187                 node->type.tv = tarval_bottom;
2188         }
2189 }  /* compute_Sub */
2190
2191 /**
2192  * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2193  *
2194  * @param node  the node
2195  */
2196 static void compute_Eor(node_t *node)
2197 {
2198         ir_node        *eor = node->node;
2199         node_t         *l   = get_irn_node(get_Eor_left(eor));
2200         node_t         *r   = get_irn_node(get_Eor_right(eor));
2201         lattice_elem_t a    = l->type;
2202         lattice_elem_t b    = r->type;
2203         ir_tarval      *tv;
2204
2205         if (a.tv == tarval_top || b.tv == tarval_top) {
2206                 node->type.tv = tarval_top;
2207         } else if (is_con(a) && is_con(b)) {
2208                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2209                         node->type.tv = tarval_eor(a.tv, b.tv);
2210                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2211                         node->type = b;
2212                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2213                         node->type = a;
2214                 } else {
2215                         node->type.tv = tarval_bottom;
2216                 }
2217         } else if (r->part == l->part) {
2218                 ir_mode *mode = get_irn_mode(eor);
2219                 tv = get_mode_null(mode);
2220
2221                 /* if the node was ONCE evaluated by all constants, but now
2222                    this breaks AND we get from the argument partitions a different
2223                    result, switch to bottom.
2224                    This happens because initially all nodes are in the same partition ... */
2225                 if (node->type.tv != tv)
2226                         tv = tarval_bottom;
2227                 node->type.tv = tv;
2228         } else {
2229                 node->type.tv = tarval_bottom;
2230         }
2231 }  /* compute_Eor */
2232
2233 /**
2234  * (Re-)compute the type for Cmp.
2235  *
2236  * @param node  the node
2237  */
2238 static void compute_Cmp(node_t *node)
2239 {
2240         ir_node        *cmp     = node->node;
2241         node_t         *l       = get_irn_node(get_Cmp_left(cmp));
2242         node_t         *r       = get_irn_node(get_Cmp_right(cmp));
2243         lattice_elem_t a        = l->type;
2244         lattice_elem_t b        = r->type;
2245         ir_relation    relation = get_Cmp_relation(cmp);
2246         ir_tarval      *tv;
2247
2248         if (a.tv == tarval_top || b.tv == tarval_top) {
2249                 node->type.tv = tarval_undefined;
2250         } else if (is_con(a) && is_con(b)) {
2251                 default_compute(node);
2252
2253         /*
2254          * BEWARE: a == a is NOT always True for floating Point values, as
2255          * NaN != NaN is defined, so we must check this here.
2256          * (while for some pnc we could still optimize we have to stay
2257          *  consistent with compute_Cmp, so don't do anything for floats)
2258          */
2259         } else if (r->part == l->part && !mode_is_float(get_irn_mode(l->node))) {
2260                 tv = relation & ir_relation_equal ? tarval_b_true : tarval_b_false;
2261
2262                 /* if the node was ONCE evaluated to a constant, but now
2263                    this breaks AND we get from the argument partitions a different
2264                    result, ensure monotony by fall to bottom.
2265                    This happens because initially all nodes are in the same partition ... */
2266                 if (node->type.tv == tarval_bottom)
2267                         tv = tarval_bottom;
2268                 else if (node->type.tv != tv && is_constant_type(node->type))
2269                         tv = tarval_bottom;
2270                 node->type.tv = tv;
2271         } else {
2272                 node->type.tv = tarval_bottom;
2273         }
2274 }
2275
2276 /**
2277  * (Re-)compute the type for a Proj(Cond).
2278  *
2279  * @param node  the node
2280  * @param cond  the predecessor Cond node
2281  */
2282 static void compute_Proj_Cond(node_t *node, ir_node *cond)
2283 {
2284         ir_node *proj     = node->node;
2285         long    pnc       = get_Proj_proj(proj);
2286         ir_node *sel      = get_Cond_selector(cond);
2287         node_t  *selector = get_irn_node(sel);
2288
2289         /*
2290          * Note: it is crucial for the monotony that the Proj(Cond)
2291          * are evaluates after all predecessors of the Cond selector are
2292          * processed.
2293          * Example
2294          *
2295          * if (x != 0)
2296          *
2297          * Due to the fact that 0 is a const, the Cmp gets immediately
2298          * on the cprop list. It will be evaluated before x is evaluated,
2299          * might leaving x as Top. When later x is evaluated, the Cmp
2300          * might change its value.
2301          * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2302          * gets R, and later changed to F if Cmp is evaluated to True!
2303          *
2304          * We prevent this by putting Conds in an extra cprop_X queue, which
2305          * gets evaluated after the cprop queue is empty.
2306          *
2307          * Note that this even happens with Click's original algorithm, if
2308          * Cmp(x, 0) is evaluated to True first and later changed to False
2309          * if x was Top first and later changed to a Const ...
2310          * It is unclear how Click solved that problem ...
2311          *
2312          * However, in rare cases even this does not help, if a Top reaches
2313          * a compare  through a Phi, than Proj(Cond) is evaluated changing
2314          * the type of the Phi to something other.
2315          * So, we take the last resort and bind the type to R once
2316          * it is calculated.
2317          *
2318          * (This might be even the way Click works around the whole problem).
2319          *
2320          * Finally, we may miss some optimization possibilities due to this:
2321          *
2322          * x = phi(Top, y)
2323          * if (x == 0)
2324          *
2325          * If Top reaches the if first, than we decide for != here.
2326          * If y later is evaluated to 0, we cannot revert this decision
2327          * and must live with both outputs enabled. If this happens,
2328          * we get an unresolved if (true) in the code ...
2329          *
2330          * In Click's version where this decision is done at the Cmp,
2331          * the Cmp is NOT optimized away than (if y evaluated to 1
2332          * for instance) and we get a if (1 == 0) here ...
2333          *
2334          * Both solutions are suboptimal.
2335          * At least, we could easily detect this problem and run
2336          * cf_opt() (or even combo) again :-(
2337          */
2338         if (node->type.tv == tarval_reachable)
2339                 return;
2340
2341         if (get_irn_mode(sel) == mode_b) {
2342                 /* an IF */
2343                 if (pnc == pn_Cond_true) {
2344                         if (selector->type.tv == tarval_b_false) {
2345                                 node->type.tv = tarval_unreachable;
2346                         } else if (selector->type.tv == tarval_b_true) {
2347                                 node->type.tv = tarval_reachable;
2348                         } else if (selector->type.tv == tarval_bottom) {
2349                                 node->type.tv = tarval_reachable;
2350                         } else {
2351                                 assert(selector->type.tv == tarval_top);
2352                                 if (tarval_UNKNOWN == tarval_top) {
2353                                         /* any condition based on Top is "!=" */
2354                                         node->type.tv = tarval_unreachable;
2355                                 } else {
2356                                         node->type.tv = tarval_unreachable;
2357                                 }
2358                         }
2359                 } else {
2360                         assert(pnc == pn_Cond_false);
2361
2362                         if (selector->type.tv == tarval_b_false) {
2363                                 node->type.tv = tarval_reachable;
2364                         } else if (selector->type.tv == tarval_b_true) {
2365                                 node->type.tv = tarval_unreachable;
2366                         } else if (selector->type.tv == tarval_bottom) {
2367                                 node->type.tv = tarval_reachable;
2368                         } else {
2369                                 assert(selector->type.tv == tarval_top);
2370                                 if (tarval_UNKNOWN == tarval_top) {
2371                                         /* any condition based on Top is "!=" */
2372                                         node->type.tv = tarval_reachable;
2373                                 } else {
2374                                         node->type.tv = tarval_unreachable;
2375                                 }
2376                         }
2377                 }
2378         } else {
2379                 /* an SWITCH */
2380                 if (selector->type.tv == tarval_bottom) {
2381                         node->type.tv = tarval_reachable;
2382                 } else if (selector->type.tv == tarval_top) {
2383                         if (tarval_UNKNOWN == tarval_top &&
2384                             pnc == get_Cond_default_proj(cond)) {
2385                                 /* a switch based of Top is always "default" */
2386                                 node->type.tv = tarval_reachable;
2387                         } else {
2388                                 node->type.tv = tarval_unreachable;
2389                         }
2390                 } else {
2391                         long value = get_tarval_long(selector->type.tv);
2392                         if (pnc == get_Cond_default_proj(cond)) {
2393                                 /* default switch, have to check ALL other cases */
2394                                 int i;
2395
2396                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2397                                         ir_node *succ = get_irn_out(cond, i);
2398
2399                                         if (succ == proj)
2400                                                 continue;
2401                                         if (value == get_Proj_proj(succ)) {
2402                                                 /* we found a match, will NOT take the default case */
2403                                                 node->type.tv = tarval_unreachable;
2404                                                 return;
2405                                         }
2406                                 }
2407                                 /* all cases checked, no match, will take default case */
2408                                 node->type.tv = tarval_reachable;
2409                         } else {
2410                                 /* normal case */
2411                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2412                         }
2413                 }
2414         }
2415 }  /* compute_Proj_Cond */
2416
2417 /**
2418  * (Re-)compute the type for a Proj-Node.
2419  *
2420  * @param node  the node
2421  */
2422 static void compute_Proj(node_t *node)
2423 {
2424         ir_node *proj = node->node;
2425         ir_mode *mode = get_irn_mode(proj);
2426         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2427         ir_node *pred  = get_Proj_pred(proj);
2428
2429         if (block->type.tv == tarval_unreachable) {
2430                 /* a Proj in a unreachable Block stay Top */
2431                 node->type.tv = tarval_top;
2432                 return;
2433         }
2434         if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred)) {
2435                 /* if the predecessor is Top, its Proj follow */
2436                 node->type.tv = tarval_top;
2437                 return;
2438         }
2439
2440         if (mode == mode_M) {
2441                 /* mode M is always bottom */
2442                 node->type.tv = tarval_bottom;
2443                 return;
2444         } else if (mode == mode_X) {
2445                 /* handle mode_X nodes */
2446                 switch (get_irn_opcode(pred)) {
2447                 case iro_Start:
2448                         /* the Proj_X from the Start is always reachable.
2449                            However this is already handled at the top. */
2450                         node->type.tv = tarval_reachable;
2451                         return;
2452                 case iro_Cond:
2453                         compute_Proj_Cond(node, pred);
2454                         return;
2455                 default:
2456                         break;
2457                 }
2458         }
2459
2460         default_compute(node);
2461 }  /* compute_Proj */
2462
2463 /**
2464  * (Re-)compute the type for a Confirm.
2465  *
2466  * @param node  the node
2467  */
2468 static void compute_Confirm(node_t *node)
2469 {
2470         ir_node *confirm = node->node;
2471         node_t  *pred = get_irn_node(get_Confirm_value(confirm));
2472
2473         if (get_Confirm_relation(confirm) == ir_relation_equal) {
2474                 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2475
2476                 if (is_con(bound->type)) {
2477                         /* is equal to a constant */
2478                         node->type = bound->type;
2479                         return;
2480                 }
2481         }
2482         /* a Confirm is a copy OR a Const */
2483         node->type = pred->type;
2484 }  /* compute_Confirm */
2485
2486 /**
2487  * (Re-)compute the type for a given node.
2488  *
2489  * @param node  the node
2490  */
2491 static void compute(node_t *node)
2492 {
2493         ir_node *irn = node->node;
2494         compute_func func;
2495
2496 #ifndef VERIFY_MONOTONE
2497         /*
2498          * Once a node reaches bottom, the type cannot fall further
2499          * in the lattice and we can stop computation.
2500          * Do not take this exit if the monotony verifier is
2501          * enabled to catch errors.
2502          */
2503         if (node->type.tv == tarval_bottom)
2504                 return;
2505 #endif
2506
2507         if (!is_Block(irn)) {
2508                 /* for pinned nodes, check its control input */
2509                 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2510                         node_t *block = get_irn_node(get_nodes_block(irn));
2511
2512                         if (block->type.tv == tarval_unreachable) {
2513                                 node->type.tv = tarval_top;
2514                                 return;
2515                         }
2516                 }
2517         }
2518
2519         func = (compute_func)node->node->op->ops.generic;
2520         if (func != NULL)
2521                 func(node);
2522 }  /* compute */
2523
2524 /*
2525  * Identity functions: Note that one might think that identity() is just a
2526  * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2527  * here, because it expects that the identity node is one of the inputs, which is NOT
2528  * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2529  * So, we have our own implementation, which copies some parts of equivalent_node()
2530  */
2531
2532 /**
2533  * Calculates the Identity for Phi nodes
2534  */
2535 static node_t *identity_Phi(node_t *node)
2536 {
2537         ir_node *phi    = node->node;
2538         ir_node *block  = get_nodes_block(phi);
2539         node_t  *n_part = NULL;
2540         int     i;
2541
2542         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2543                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2544
2545                 if (pred_X->type.tv == tarval_reachable) {
2546                         node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2547
2548                         if (n_part == NULL)
2549                                 n_part = pred;
2550                         else if (n_part->part != pred->part) {
2551                                 /* incongruent inputs, not a follower */
2552                                 return node;
2553                         }
2554                 }
2555         }
2556         /* if n_part is NULL here, all inputs path are dead, the Phi computes
2557          * tarval_top, is in the TOP partition and should NOT being split! */
2558         assert(n_part != NULL);
2559         return n_part;
2560 }  /* identity_Phi */
2561
2562 /**
2563  * Calculates the Identity for commutative 0 neutral nodes.
2564  */
2565 static node_t *identity_comm_zero_binop(node_t *node)
2566 {
2567         ir_node   *op   = node->node;
2568         node_t    *a    = get_irn_node(get_binop_left(op));
2569         node_t    *b    = get_irn_node(get_binop_right(op));
2570         ir_mode   *mode = get_irn_mode(op);
2571         ir_tarval *zero;
2572
2573         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2574         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2575                 return node;
2576
2577         /* node: no input should be tarval_top, else the binop would be also
2578          * Top and not being split. */
2579         zero = get_mode_null(mode);
2580         if (a->type.tv == zero)
2581                 return b;
2582         if (b->type.tv == zero)
2583                 return a;
2584         return node;
2585 }  /* identity_comm_zero_binop */
2586
2587 /**
2588  * Calculates the Identity for Shift nodes.
2589  */
2590 static node_t *identity_shift(node_t *node)
2591 {
2592         ir_node   *op   = node->node;
2593         node_t    *b    = get_irn_node(get_binop_right(op));
2594         ir_mode   *mode = get_irn_mode(b->node);
2595         ir_tarval *zero;
2596
2597         /* node: no input should be tarval_top, else the binop would be also
2598          * Top and not being split. */
2599         zero = get_mode_null(mode);
2600         if (b->type.tv == zero)
2601                 return get_irn_node(get_binop_left(op));
2602         return node;
2603 }  /* identity_shift */
2604
2605 /**
2606  * Calculates the Identity for Mul nodes.
2607  */
2608 static node_t *identity_Mul(node_t *node)
2609 {
2610         ir_node   *op   = node->node;
2611         node_t    *a    = get_irn_node(get_Mul_left(op));
2612         node_t    *b    = get_irn_node(get_Mul_right(op));
2613         ir_mode   *mode = get_irn_mode(op);
2614         ir_tarval *one;
2615
2616         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2617         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2618                 return node;
2619
2620         /* node: no input should be tarval_top, else the binop would be also
2621          * Top and not being split. */
2622         one = get_mode_one(mode);
2623         if (a->type.tv == one)
2624                 return b;
2625         if (b->type.tv == one)
2626                 return a;
2627         return node;
2628 }  /* identity_Mul */
2629
2630 /**
2631  * Calculates the Identity for Sub nodes.
2632  */
2633 static node_t *identity_Sub(node_t *node)
2634 {
2635         ir_node *sub  = node->node;
2636         node_t  *b    = get_irn_node(get_Sub_right(sub));
2637         ir_mode *mode = get_irn_mode(sub);
2638
2639         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2640         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2641                 return node;
2642
2643         /* node: no input should be tarval_top, else the binop would be also
2644          * Top and not being split. */
2645         if (b->type.tv == get_mode_null(mode))
2646                 return get_irn_node(get_Sub_left(sub));
2647         return node;
2648 }  /* identity_Sub */
2649
2650 /**
2651  * Calculates the Identity for And nodes.
2652  */
2653 static node_t *identity_And(node_t *node)
2654 {
2655         ir_node   *andnode = node->node;
2656         node_t    *a       = get_irn_node(get_And_left(andnode));
2657         node_t    *b       = get_irn_node(get_And_right(andnode));
2658         ir_tarval *neutral = get_mode_all_one(get_irn_mode(andnode));
2659
2660         /* node: no input should be tarval_top, else the And would be also
2661          * Top and not being split. */
2662         if (a->type.tv == neutral)
2663                 return b;
2664         if (b->type.tv == neutral)
2665                 return a;
2666         return node;
2667 }  /* identity_And */
2668
2669 /**
2670  * Calculates the Identity for Confirm nodes.
2671  */
2672 static node_t *identity_Confirm(node_t *node)
2673 {
2674         ir_node *confirm = node->node;
2675
2676         /* a Confirm is always a Copy */
2677         return get_irn_node(get_Confirm_value(confirm));
2678 }  /* identity_Confirm */
2679
2680 /**
2681  * Calculates the Identity for Mux nodes.
2682  */
2683 static node_t *identity_Mux(node_t *node)
2684 {
2685         ir_node *mux = node->node;
2686         node_t  *t   = get_irn_node(get_Mux_true(mux));
2687         node_t  *f   = get_irn_node(get_Mux_false(mux));
2688         /*node_t  *sel; */
2689
2690         if (t->part == f->part)
2691                 return t;
2692
2693         /* for now, the 1-input identity is not supported */
2694 #if 0
2695         sel = get_irn_node(get_Mux_sel(mux));
2696
2697         /* Mux sel input is mode_b, so it is always a tarval */
2698         if (sel->type.tv == tarval_b_true)
2699                 return t;
2700         if (sel->type.tv == tarval_b_false)
2701                 return f;
2702 #endif
2703         return node;
2704 }  /* identity_Mux */
2705
2706 /**
2707  * Calculates the Identity for nodes.
2708  */
2709 static node_t *identity(node_t *node)
2710 {
2711         ir_node *irn = node->node;
2712
2713         switch (get_irn_opcode(irn)) {
2714         case iro_Phi:
2715                 return identity_Phi(node);
2716         case iro_Mul:
2717                 return identity_Mul(node);
2718         case iro_Add:
2719         case iro_Or:
2720         case iro_Eor:
2721                 return identity_comm_zero_binop(node);
2722         case iro_Shr:
2723         case iro_Shl:
2724         case iro_Shrs:
2725         case iro_Rotl:
2726                 return identity_shift(node);
2727         case iro_And:
2728                 return identity_And(node);
2729         case iro_Sub:
2730                 return identity_Sub(node);
2731         case iro_Confirm:
2732                 return identity_Confirm(node);
2733         case iro_Mux:
2734                 return identity_Mux(node);
2735         default:
2736                 return node;
2737         }
2738 }  /* identity */
2739
2740 /**
2741  * Node follower is a (new) follower of leader, segregate Leader
2742  * out edges.
2743  */
2744 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader)
2745 {
2746         ir_node *l   = leader->node;
2747         int     j, i, n = get_irn_n_outs(l);
2748
2749         DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2750         /* The leader edges must remain sorted, but follower edges can
2751            be unsorted. */
2752         for (i = leader->n_followers + 1; i <= n; ++i) {
2753                 if (l->out[i].use == follower) {
2754                         ir_def_use_edge t = l->out[i];
2755
2756                         for (j = i - 1; j >= leader->n_followers + 1; --j)
2757                                 l->out[j + 1] = l->out[j];
2758                         ++leader->n_followers;
2759                         l->out[leader->n_followers] = t;
2760                         break;
2761                 }
2762         }
2763 }  /* segregate_def_use_chain_1 */
2764
2765 /**
2766  * Node follower is a (new) follower segregate its Leader
2767  * out edges.
2768  *
2769  * @param follower  the follower IR node
2770  */
2771 static void segregate_def_use_chain(const ir_node *follower)
2772 {
2773         int i;
2774
2775         for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2776                 node_t *pred = get_irn_node(get_irn_n(follower, i));
2777
2778                 segregate_def_use_chain_1(follower, pred);
2779         }
2780 }  /* segregate_def_use_chain */
2781
2782 /**
2783  * Propagate constant evaluation.
2784  *
2785  * @param env  the environment
2786  */
2787 static void propagate(environment_t *env)
2788 {
2789         partition_t    *X, *Y;
2790         node_t         *x;
2791         lattice_elem_t old_type;
2792         node_t         *fallen;
2793         unsigned       n_fallen, old_type_was_T_or_C;
2794         int            i;
2795
2796         while (env->cprop != NULL) {
2797                 void *oldopcode = NULL;
2798
2799                 /* remove the first partition X from cprop */
2800                 X           = env->cprop;
2801                 X->on_cprop = 0;
2802                 env->cprop  = X->cprop_next;
2803
2804                 old_type_was_T_or_C = X->type_is_T_or_C;
2805
2806                 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2807                 fallen   = NULL;
2808                 n_fallen = 0;
2809                 for (;;) {
2810                         int cprop_empty   = list_empty(&X->cprop);
2811                         int cprop_X_empty = list_empty(&X->cprop_X);
2812
2813                         if (cprop_empty && cprop_X_empty) {
2814                                 /* both cprop lists are empty */
2815                                 break;
2816                         }
2817
2818                         /* remove the first Node x from X.cprop */
2819                         if (cprop_empty) {
2820                                 /* Get a node from the cprop_X list only if
2821                                  * all data nodes are processed.
2822                                  * This ensures, that all inputs of the Cond
2823                                  * predecessor are processed if its type is still Top.
2824                                  */
2825                                 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2826                         } else {
2827                                 x = list_entry(X->cprop.next, node_t, cprop_list);
2828                         }
2829
2830                         //assert(x->part == X);
2831                         list_del(&x->cprop_list);
2832                         x->on_cprop = 0;
2833
2834                         if (x->is_follower && identity(x) == x) {
2835                                 /* check the opcode first */
2836                                 if (oldopcode == NULL) {
2837                                         oldopcode = lambda_opcode(get_first_node(X), env);
2838                                 }
2839                                 if (oldopcode != lambda_opcode(x, env)) {
2840                                         if (x->on_fallen == 0) {
2841                                                 /* different opcode -> x falls out of this partition */
2842                                                 x->next      = fallen;
2843                                                 x->on_fallen = 1;
2844                                                 fallen       = x;
2845                                                 ++n_fallen;
2846                                                 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2847                                         }
2848                                 }
2849
2850                                 /* x will make the follower -> leader transition */
2851                                 follower_to_leader(x);
2852                         }
2853
2854                         /* compute a new type for x */
2855                         old_type = x->type;
2856                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2857                         compute(x);
2858                         if (x->type.tv != old_type.tv) {
2859                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2860                                 verify_type(old_type, x);
2861
2862                                 if (x->on_fallen == 0) {
2863                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2864                                            not already on the list. */
2865                                         x->next      = fallen;
2866                                         x->on_fallen = 1;
2867                                         fallen       = x;
2868                                         ++n_fallen;
2869                                         DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2870                                 }
2871                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2872                                         ir_node *succ = get_irn_out(x->node, i);
2873                                         node_t  *y    = get_irn_node(succ);
2874
2875                                         /* Add y to y.partition.cprop. */
2876                                         add_to_cprop(y, env);
2877                                 }
2878                         }
2879                 }
2880
2881                 if (n_fallen > 0 && n_fallen != X->n_leader) {
2882                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2883                         Y = split(&X, fallen, env);
2884                         /*
2885                          * We have split out fallen node. The type of the result
2886                          * partition is NOT set yet.
2887                          */
2888                         Y->type_is_T_or_C = 0;
2889                 } else {
2890                         Y = X;
2891                 }
2892                 /* remove the flags from the fallen list */
2893                 for (x = fallen; x != NULL; x = x->next)
2894                         x->on_fallen = 0;
2895
2896                 if (old_type_was_T_or_C) {
2897                         node_t *y, *tmp;
2898
2899                         /* check if some nodes will make the leader -> follower transition */
2900                         list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2901                                 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2902                                         node_t *eq_node = identity(y);
2903
2904                                         if (eq_node != y && eq_node->part == y->part) {
2905                                                 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2906                                                 /* move to Follower */
2907                                                 y->is_follower = 1;
2908                                                 list_del(&y->node_list);
2909                                                 list_add_tail(&y->node_list, &Y->Follower);
2910                                                 --Y->n_leader;
2911
2912                                                 segregate_def_use_chain(y->node);
2913                                         }
2914                                 }
2915                         }
2916                 }
2917                 split_by(Y, env);
2918         }
2919 }  /* propagate */
2920
2921 /**
2922  * Get the leader for a given node from its congruence class.
2923  *
2924  * @param irn  the node
2925  */
2926 static ir_node *get_leader(node_t *node)
2927 {
2928         partition_t *part = node->part;
2929
2930         if (part->n_leader > 1 || node->is_follower) {
2931                 if (node->is_follower) {
2932                         DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2933                 }
2934                 else
2935                         DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2936
2937                 return get_first_node(part)->node;
2938         }
2939         return node->node;
2940 }  /* get_leader */
2941
2942 /**
2943  * Returns non-zero if a mode_T node has only one reachable output.
2944  */
2945 static int only_one_reachable_proj(ir_node *n)
2946 {
2947         int i, k = 0;
2948
2949         for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2950                 ir_node *proj = get_irn_out(n, i);
2951                 node_t  *node;
2952
2953                 /* skip non-control flow Proj's */
2954                 if (get_irn_mode(proj) != mode_X)
2955                         continue;
2956
2957                 node = get_irn_node(proj);
2958                 if (node->type.tv == tarval_reachable) {
2959                         if (++k > 1)
2960                                 return 0;
2961                 }
2962         }
2963         return 1;
2964 }  /* only_one_reachable_proj */
2965
2966 /**
2967  * Return non-zero if the control flow predecessor node pred
2968  * is the only reachable control flow exit of its block.
2969  *
2970  * @param pred   the control flow exit
2971  * @param block  the destination block
2972  */
2973 static int can_exchange(ir_node *pred, ir_node *block)
2974 {
2975         if (is_Start(pred) || has_Block_entity(block))
2976                 return 0;
2977         else if (is_Jmp(pred))
2978                 return 1;
2979         else if (is_Raise(pred)) {
2980                 /* Raise is a tuple and usually has only one reachable ProjX,
2981                  * but it must not be eliminated like a Jmp */
2982                 return 0;
2983         }
2984         else if (get_irn_mode(pred) == mode_T) {
2985                 /* if the predecessor block has more than one
2986                    reachable outputs we cannot remove the block */
2987                 return only_one_reachable_proj(pred);
2988         }
2989         return 0;
2990 }  /* can_exchange */
2991
2992 /**
2993  * Block Post-Walker, apply the analysis results on control flow by
2994  * shortening Phi's and Block inputs.
2995  */
2996 static void apply_cf(ir_node *block, void *ctx)
2997 {
2998         environment_t *env = (environment_t*)ctx;
2999         node_t        *node = get_irn_node(block);
3000         int           i, j, k, n;
3001         ir_node       **ins, **in_X;
3002         ir_node       *phi, *next;
3003
3004         n = get_Block_n_cfgpreds(block);
3005
3006         if (node->type.tv == tarval_unreachable) {
3007                 env->modified = 1;
3008
3009                 for (i = n - 1; i >= 0; --i) {
3010                         ir_node *pred = get_Block_cfgpred(block, i);
3011
3012                         if (! is_Bad(pred)) {
3013                                 ir_node *pred_block = get_nodes_block(skip_Proj(pred));
3014                                 if (!is_Bad(pred_block)) {
3015                                         node_t *pred_bl = get_irn_node(pred_block);
3016
3017                                         if (pred_bl->flagged == 0) {
3018                                                 pred_bl->flagged = 3;
3019
3020                                                 if (pred_bl->type.tv == tarval_reachable) {
3021                                                         /*
3022                                                          * We will remove an edge from block to its pred.
3023                                                          * This might leave the pred block as an endless loop
3024                                                          */
3025                                                         if (! is_backedge(block, i))
3026                                                                 keep_alive(pred_bl->node);
3027                                                 }
3028                                         }
3029                                 }
3030                         }
3031                 }
3032
3033                 if (block == get_irg_end_block(current_ir_graph)) {
3034                         /* Analysis found out that the end block is unreachable,
3035                          * hence we remove all its control flow predecessors. */
3036                         set_irn_in(block, 0, NULL);
3037                 }
3038                 return;
3039         }
3040
3041         if (n == 1) {
3042                 /* only one predecessor combine */
3043                 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3044
3045                 if (can_exchange(pred, block)) {
3046                         ir_node *new_block = get_nodes_block(pred);
3047                         DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3048                         DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3049                         exchange(block, new_block);
3050                         node->node = new_block;
3051                         env->modified = 1;
3052                 }
3053                 return;
3054         }
3055
3056         NEW_ARR_A(ir_node *, in_X, n);
3057         k = 0;
3058         for (i = 0; i < n; ++i) {
3059                 ir_node *pred = get_Block_cfgpred(block, i);
3060                 node_t  *node = get_irn_node(pred);
3061
3062                 if (node->type.tv == tarval_reachable) {
3063                         in_X[k++] = pred;
3064                 } else {
3065                         DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3066                         if (! is_Bad(pred)) {
3067                                 ir_node *pred_block = get_nodes_block(skip_Proj(pred));
3068                                 if (!is_Bad(pred_block)) {
3069                                         node_t *pred_bl = get_irn_node(pred_block);
3070
3071                                         if (!is_Bad(pred_bl->node) && pred_bl->flagged == 0) {
3072                                                 pred_bl->flagged = 3;
3073
3074                                                 if (pred_bl->type.tv == tarval_reachable) {
3075                                                         /*
3076                                                          * We will remove an edge from block to its pred.
3077                                                          * This might leave the pred block as an endless loop
3078                                                          */
3079                                                         if (! is_backedge(block, i))
3080                                                                 keep_alive(pred_bl->node);
3081                                                 }
3082                                         }
3083                                 }
3084                         }
3085                 }
3086         }
3087         if (k >= n)
3088                 return;
3089
3090         /* fix Phi's */
3091         NEW_ARR_A(ir_node *, ins, n);
3092         for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3093                 node_t *node = get_irn_node(phi);
3094
3095                 next = get_Phi_next(phi);
3096                 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3097                         /* this Phi is replaced by a constant */
3098                         ir_tarval *tv = node->type.tv;
3099                         ir_node   *c  = new_r_Const(current_ir_graph, tv);
3100
3101                         set_irn_node(c, node);
3102                         node->node = c;
3103                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3104                         DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3105                         exchange(phi, c);
3106                         env->modified = 1;
3107                 } else {
3108                         j = 0;
3109                         for (i = 0; i < n; ++i) {
3110                                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3111
3112                                 if (pred->type.tv == tarval_reachable) {
3113                                         ins[j++] = get_Phi_pred(phi, i);
3114                                 }
3115                         }
3116                         if (j == 1) {
3117                                 /* this Phi is replaced by a single predecessor */
3118                                 ir_node *s = ins[0];
3119                                 node_t *phi_node = get_irn_node(phi);
3120
3121                                 node->node = s;
3122                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3123                                 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3124                                 exchange(phi, s);
3125                                 phi_node->node = s;
3126                                 env->modified = 1;
3127                         } else {
3128                                 set_irn_in(phi, j, ins);
3129                                 env->modified = 1;
3130                         }
3131                 }
3132         }
3133
3134         /* fix block */
3135         if (k == 1) {
3136                 /* this Block has only one live predecessor */
3137                 ir_node *pred = skip_Proj(in_X[0]);
3138
3139                 if (can_exchange(pred, block)) {
3140                         ir_node *new_block = get_nodes_block(pred);
3141                         DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3142                         exchange(block, new_block);
3143                         node->node = new_block;
3144                         env->modified = 1;
3145                         return;
3146                 }
3147         }
3148         set_irn_in(block, k, in_X);
3149         env->modified = 1;
3150 }  /* apply_cf */
3151
3152 /**
3153  * Exchange a node by its leader.
3154  * Beware: in rare cases the mode might be wrong here, for instance
3155  * AddP(x, NULL) is a follower of x, but with different mode.
3156  * Fix it here.
3157  */
3158 static void exchange_leader(ir_node *irn, ir_node *leader)
3159 {
3160         ir_mode *mode = get_irn_mode(irn);
3161         if (mode != get_irn_mode(leader)) {
3162                 /* The conv is a no-op, so we are free to place it
3163                  * either in the block of the leader OR in irn's block.
3164                  * Probably placing it into leaders block might reduce
3165                  * the number of Conv due to CSE. */
3166                 ir_node  *block = get_nodes_block(leader);
3167                 dbg_info *dbg   = get_irn_dbg_info(irn);
3168                 ir_node  *nlead = new_rd_Conv(dbg, block, leader, mode);
3169
3170                 if (nlead != leader) {
3171                         /* Note: this newly create irn has no node info because
3172                          * it is created after the analysis. However, this node
3173                          * replaces the node irn and should not be visited again,
3174                          * so set its visited count to the count of irn.
3175                          * Otherwise we might visited this node more than once if
3176                          * irn had more than one user.
3177                          */
3178                         set_irn_node(nlead, NULL);
3179                         set_irn_visited(nlead, get_irn_visited(irn));
3180                         leader = nlead;
3181                 }
3182         }
3183         exchange(irn, leader);
3184 }  /* exchange_leader */
3185
3186 /**
3187  * Check, if all users of a mode_M node are dead. Use
3188  * the Def-Use edges for this purpose, as they still
3189  * reflect the situation.
3190  */
3191 static int all_users_are_dead(const ir_node *irn)
3192 {
3193         int i, n = get_irn_n_outs(irn);
3194
3195         for (i = 1; i <= n; ++i) {
3196                 const ir_node *succ  = irn->out[i].use;
3197                 const node_t  *block = get_irn_node(get_nodes_block(succ));
3198                 const node_t  *node;
3199
3200                 if (block->type.tv == tarval_unreachable) {
3201                         /* block is unreachable */
3202                         continue;
3203                 }
3204                 node = get_irn_node(succ);
3205                 if (node->type.tv != tarval_top) {
3206                         /* found a reachable user */
3207                         return 0;
3208                 }
3209         }
3210         /* all users are unreachable */
3211         return 1;
3212 }  /* all_user_are_dead */
3213
3214 /**
3215  * Walker: Find reachable mode_M nodes that have only
3216  * unreachable users. These nodes must be kept later.
3217  */
3218 static void find_kept_memory(ir_node *irn, void *ctx)
3219 {
3220         environment_t *env = (environment_t*)ctx;
3221         node_t        *node, *block;
3222
3223         if (get_irn_mode(irn) != mode_M)
3224                 return;
3225
3226         block = get_irn_node(get_nodes_block(irn));
3227         if (block->type.tv == tarval_unreachable)
3228                 return;
3229
3230         node = get_irn_node(irn);
3231         if (node->type.tv == tarval_top)
3232                 return;
3233
3234         /* ok, we found a live memory node. */
3235         if (all_users_are_dead(irn)) {
3236                 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3237                 ARR_APP1(ir_node *, env->kept_memory, irn);
3238         }
3239 }  /* find_kept_memory */
3240
3241 /**
3242  * Post-Walker, apply the analysis results;
3243  */
3244 static void apply_result(ir_node *irn, void *ctx)
3245 {
3246         environment_t *env = (environment_t*)ctx;
3247         node_t        *node = get_irn_node(irn);
3248
3249         if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3250                 /* blocks already handled, do not touch the End node */
3251         } else {
3252                 node_t *block = get_irn_node(get_nodes_block(irn));
3253
3254                 if (block->type.tv == tarval_unreachable) {
3255                         ir_graph *irg  = get_irn_irg(irn);
3256                         ir_mode  *mode = get_irn_mode(node->node);
3257                         ir_node  *bad  = new_r_Bad(irg, mode);
3258
3259                         /* here, bad might already have a node, but this can be safely ignored
3260                            as long as bad has at least ONE valid node */
3261                         set_irn_node(bad, node);
3262                         node->node = bad;
3263                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3264                         exchange(irn, bad);
3265                         env->modified = 1;
3266                 } else if (node->type.tv == tarval_top) {
3267                         ir_mode *mode = get_irn_mode(irn);
3268
3269                         if (mode == mode_M) {
3270                                 /* never kill a mode_M node */
3271                                 if (is_Proj(irn)) {
3272                                         ir_node *pred  = get_Proj_pred(irn);
3273                                         node_t  *pnode = get_irn_node(pred);
3274
3275                                         if (pnode->type.tv == tarval_top) {
3276                                                 /* skip the predecessor */
3277                                                 ir_node *mem = get_memop_mem(pred);
3278                                                 node->node = mem;
3279                                                 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3280                                                 exchange(irn, mem);
3281                                                 env->modified = 1;
3282                                         }
3283                                 }
3284                                 /* leave other nodes, especially PhiM */
3285                         } else if (mode == mode_T) {
3286                                 /* Do not kill mode_T nodes, kill their Projs */
3287                         } else if (! is_Unknown(irn)) {
3288                                 /* don't kick away Unknown's, they might be still needed */
3289                                 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3290
3291                                 /* control flow should already be handled at apply_cf() */
3292                                 assert(mode != mode_X);
3293
3294                                 /* see comment above */
3295                                 set_irn_node(unk, node);
3296                                 node->node = unk;
3297                                 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3298                                 exchange(irn, unk);
3299                                 env->modified = 1;
3300                         }
3301                 }
3302                 else if (get_irn_mode(irn) == mode_X) {
3303                         if (is_Proj(irn)) {
3304                                 /* leave or Jmp */
3305                                 ir_node *cond = get_Proj_pred(irn);
3306
3307                                 if (is_Cond(cond)) {
3308                                         if (only_one_reachable_proj(cond)) {
3309                                                 ir_node *jmp = new_r_Jmp(block->node);
3310                                                 set_irn_node(jmp, node);
3311                                                 node->node = jmp;
3312                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3313                                                 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3314                                                 exchange(irn, jmp);
3315                                                 env->modified = 1;
3316                                         } else {
3317                                                 node_t    *sel = get_irn_node(get_Cond_selector(cond));
3318                                                 ir_tarval *tv  = sel->type.tv;
3319
3320                                                 if (is_tarval(tv) && tarval_is_constant(tv)) {
3321                                                         /* The selector is a constant, but more
3322                                                          * than one output is active: An unoptimized
3323                                                          * case found. */
3324                                                         env->unopt_cf = 1;
3325                                                 }
3326                                         }
3327                                 }
3328                         }
3329                 } else {
3330                         /* normal data node */
3331                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3332                                 ir_tarval *tv = node->type.tv;
3333
3334                                 /*
3335                                  * Beware: never replace mode_T nodes by constants. Currently we must mark
3336                                  * mode_T nodes with constants, but do NOT replace them.
3337                                  */
3338                                 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3339                                         /* can be replaced by a constant */
3340                                         ir_node *c = new_r_Const(current_ir_graph, tv);
3341                                         set_irn_node(c, node);
3342                                         node->node = c;
3343                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3344                                         DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3345                                         exchange_leader(irn, c);
3346                                         env->modified = 1;
3347                                 }
3348                         } else if (is_entity(node->type.sym.entity_p)) {
3349                                 if (! is_SymConst(irn)) {
3350                                         /* can be replaced by a SymConst */
3351                                         ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3352                                         set_irn_node(symc, node);
3353                                         node->node = symc;
3354
3355                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3356                                         DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3357                                         exchange_leader(irn, symc);
3358                                         env->modified = 1;
3359                                 }
3360                         } else if (is_Confirm(irn)) {
3361                                 /* Confirms are always follower, but do not kill them here */
3362                         } else {
3363                                 ir_node *leader = get_leader(node);
3364
3365                                 if (leader != irn) {
3366                                         int non_strict_phi = 0;
3367
3368                                         /*
3369                                          * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3370                                          * as this might create non-strict programs.
3371                                          */
3372                                         if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3373                                                 int i;
3374
3375                                                 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3376                                                         ir_node *pred = get_Phi_pred(irn, i);
3377
3378                                                         if (is_Unknown(pred)) {
3379                                                                 non_strict_phi = 1;
3380                                                                 break;
3381                                                         }
3382                                                 }
3383                                         }
3384                                         if (! non_strict_phi) {
3385                                                 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3386                                                 if (node->is_follower)
3387                                                         DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3388                                                 else
3389                                                         DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3390                                                 exchange_leader(irn, leader);
3391                                                 env->modified = 1;
3392                                         }
3393                                 }
3394                         }
3395                 }
3396         }
3397 }  /* apply_result */
3398
3399 /**
3400  * Fix the keep-alives by deleting unreachable ones.
3401  */
3402 static void apply_end(ir_node *end, environment_t *env)
3403 {
3404         int i, j,  n = get_End_n_keepalives(end);
3405         ir_node **in = NULL;
3406
3407         if (n > 0)
3408                 NEW_ARR_A(ir_node *, in, n);
3409
3410         /* fix the keep alive */
3411         for (i = j = 0; i < n; i++) {
3412                 ir_node *ka = get_End_keepalive(end, i);
3413                 ir_node *block;
3414                 node_t  *node;
3415
3416                 if (is_Bad(ka))
3417                         continue;
3418                 if (!is_Block(ka)) {
3419                         block = get_nodes_block(ka);
3420                         if (is_Bad(block))
3421                                 continue;
3422                 } else {
3423                         block = ka;
3424                 }
3425
3426                 node = get_irn_node(block);
3427                 if (node->type.tv != tarval_unreachable)
3428                         in[j++] = ka;
3429         }
3430         if (j != n) {
3431                 set_End_keepalives(end, j, in);
3432                 env->modified = 1;
3433         }
3434 }  /* apply_end */
3435
3436 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3437
3438 /**
3439  * sets the generic functions to compute.
3440  */
3441 static void set_compute_functions(void)
3442 {
3443         size_t i, n;
3444
3445         /* set the default compute function */
3446         for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
3447                 ir_op *op = get_irp_opcode(i);
3448                 op->ops.generic = (op_func)default_compute;
3449         }
3450
3451         /* set specific functions */
3452         SET(Block);
3453         SET(Unknown);
3454         SET(Bad);
3455         SET(Jmp);
3456         SET(Phi);
3457         SET(Add);
3458         SET(Sub);
3459         SET(Eor);
3460         SET(SymConst);
3461         SET(Cmp);
3462         SET(Proj);
3463         SET(Confirm);
3464         SET(Return);
3465         SET(End);
3466         SET(Call);
3467 }  /* set_compute_functions */
3468
3469 /**
3470  * Add memory keeps.
3471  */
3472 static void add_memory_keeps(ir_node **kept_memory, size_t len)
3473 {
3474         ir_node      *end = get_irg_end(current_ir_graph);
3475         int          i;
3476         size_t       idx;
3477         ir_nodeset_t set;
3478
3479         ir_nodeset_init(&set);
3480
3481         /* check, if those nodes are already kept */
3482         for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3483                 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3484
3485         for (idx = 0; idx < len; ++idx) {
3486                 ir_node *ka = kept_memory[idx];
3487
3488                 if (! ir_nodeset_contains(&set, ka)) {
3489                         add_End_keepalive(end, ka);
3490                 }
3491         }
3492         ir_nodeset_destroy(&set);
3493 }  /* add_memory_keeps */
3494
3495 static ir_graph_state_t do_combo(ir_graph *irg)
3496 {
3497         environment_t env;
3498         ir_node       *initial_bl;
3499         node_t        *start;
3500         ir_graph      *rem = current_ir_graph;
3501         size_t        len;
3502
3503         current_ir_graph = irg;
3504
3505         /* register a debug mask */
3506         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3507
3508         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3509
3510         obstack_init(&env.obst);
3511         env.worklist       = NULL;
3512         env.cprop          = NULL;
3513         env.touched        = NULL;
3514         env.initial        = NULL;
3515 #ifdef DEBUG_libfirm
3516         env.dbg_list       = NULL;
3517 #endif
3518         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
3519         env.kept_memory    = NEW_ARR_F(ir_node *, 0);
3520         env.end_idx        = get_opt_global_cse() ? 0 : -1;
3521         env.lambda_input   = 0;
3522         env.modified       = 0;
3523         env.unopt_cf       = 0;
3524         /* options driving the optimization */
3525         env.commutative    = 1;
3526         env.opt_unknown    = 1;
3527
3528         /* we have our own value_of function */
3529         set_value_of_func(get_node_tarval);
3530
3531         set_compute_functions();
3532         DEBUG_ONLY(part_nr = 0);
3533
3534         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3535
3536         if (env.opt_unknown)
3537                 tarval_UNKNOWN = tarval_top;
3538         else
3539                 tarval_UNKNOWN = tarval_bad;
3540
3541         /* create the initial partition and place it on the work list */
3542         env.initial = new_partition(&env);
3543         add_to_worklist(env.initial, &env);
3544         irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3545
3546         /* set the hook: from now, every node has a partition and a type */
3547         DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook));
3548
3549         /* all nodes on the initial partition have type Top */
3550         env.initial->type_is_T_or_C = 1;
3551
3552         /* Place the START Node's partition on cprop.
3553            Place the START Node on its local worklist. */
3554         initial_bl = get_irg_start_block(irg);
3555         start      = get_irn_node(initial_bl);
3556         add_to_cprop(start, &env);
3557
3558         do {
3559                 propagate(&env);
3560                 if (env.worklist != NULL)
3561                         cause_splits(&env);
3562         } while (env.cprop != NULL || env.worklist != NULL);
3563
3564         dump_all_partitions(&env);
3565         check_all_partitions(&env);
3566
3567 #if 0
3568         dump_ir_block_graph(irg, "-partition");
3569 #endif
3570
3571         /* apply the result */
3572
3573         /* check, which nodes must be kept */
3574         irg_walk_graph(irg, NULL, find_kept_memory, &env);
3575
3576         /* kill unreachable control flow */
3577         irg_block_walk_graph(irg, NULL, apply_cf, &env);
3578         /* Kill keep-alives of dead blocks: this speeds up apply_result()
3579          * and fixes assertion because dead cf to dead blocks is NOT removed by
3580          * apply_cf(). */
3581         apply_end(get_irg_end(irg), &env);
3582         irg_walk_graph(irg, NULL, apply_result, &env);
3583
3584         len = ARR_LEN(env.kept_memory);
3585         if (len > 0)
3586                 add_memory_keeps(env.kept_memory, len);
3587
3588         if (env.unopt_cf) {
3589                 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3590         }
3591
3592         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3593
3594         /* remove the partition hook */
3595         DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
3596
3597         DEL_ARR_F(env.kept_memory);
3598         del_set(env.opcode2id_map);
3599         obstack_free(&env.obst, NULL);
3600
3601         /* restore value_of() default behavior */
3602         set_value_of_func(NULL);
3603         current_ir_graph = rem;
3604
3605         return 0; // cannot guarantee anything
3606 }  /* combo */
3607
3608 optdesc_t opt_combo = {
3609         "combo",
3610         IR_GRAPH_STATE_NO_BAD_BLOCKS | IR_GRAPH_STATE_CONSISTENT_OUTS | IR_GRAPH_STATE_CONSISTENT_LOOPINFO,
3611         do_combo,
3612 };
3613
3614 void combo(ir_graph *irg)
3615 {
3616         perform_irg_optimization(irg, &opt_combo);
3617 }
3618
3619 /* Creates an ir_graph pass for combo. */
3620 ir_graph_pass_t *combo_pass(const char *name)
3621 {
3622         return def_graph_pass(name ? name : "combo", combo);
3623 }  /* combo_pass */