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