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