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