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