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