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