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