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