remove #ifdef HAVE_CONFIG_Hs
[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    env1, env2, *winner;
1085         node_t      *g, *h, *node, *t;
1086         int         max_input, transitions;
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         env1.initial       = g;
1124         env1.unwalked      = NULL;
1125         env1.walked        = NULL;
1126         env1.index         = 0;
1127         env1.side          = 1;
1128
1129         env2.initial       = h;
1130         env2.unwalked      = NULL;
1131         env2.walked        = NULL;
1132         env2.index         = 0;
1133         env2.side          = 2;
1134
1135         for (;;) {
1136                 if (step(&env1)) {
1137                         winner = &env1;
1138                         break;
1139                 }
1140                 if (step(&env2)) {
1141                         winner = &env2;
1142                         break;
1143                 }
1144         }
1145         assert(winner->initial == NULL);
1146         assert(winner->unwalked == NULL);
1147
1148         /* clear flags from walked/unwalked */
1149         transitions  = clear_flags(env1.unwalked);
1150         transitions |= clear_flags(env1.walked);
1151         transitions |= clear_flags(env2.unwalked);
1152         transitions |= clear_flags(env2.walked);
1153
1154         dump_race_list("winner ", winner->walked);
1155
1156         /* Move walked_{winner} to a new partition, X'. */
1157         X_prime   = new_partition(env);
1158         max_input = 0;
1159         n         = 0;
1160         for (node = winner->walked; node != NULL; node = node->race_next) {
1161                 list_del(&node->node_list);
1162                 node->part = X_prime;
1163                 if (node->is_follower) {
1164                         list_add_tail(&node->node_list, &X_prime->Follower);
1165                 } else {
1166                         list_add_tail(&node->node_list, &X_prime->Leader);
1167                         ++n;
1168                 }
1169                 if (node->max_user_input > max_input)
1170                         max_input = node->max_user_input;
1171         }
1172         X_prime->n_leader        = n;
1173         X_prime->max_user_inputs = max_input;
1174         X->n_leader             -= X_prime->n_leader;
1175
1176         /* for now, copy the type info tag, it will be adjusted in split_by(). */
1177         X_prime->type_is_T_or_C = X->type_is_T_or_C;
1178
1179         /*
1180          * Even if a follower was not checked by both sides, it might have
1181          * loose its congruence, so we need to check this case for all follower.
1182          */
1183         list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1184                 if (identity(node) == node) {
1185                         follower_to_leader(node);
1186                         transitions = 1;
1187                 }
1188         }
1189
1190         check_partition(X);
1191         check_partition(X_prime);
1192
1193         /* X' is the smaller part */
1194         add_to_worklist(X_prime, env);
1195
1196         /*
1197          * If there where follower to leader transitions, ensure that the nodes
1198          * can be split out if necessary.
1199          */
1200         if (transitions) {
1201                 /* place partitions on the cprop list */
1202                 if (X_prime->on_cprop == 0) {
1203                         X_prime->cprop_next = env->cprop;
1204                         env->cprop          = X_prime;
1205                         X_prime->on_cprop   = 1;
1206                 }
1207         }
1208
1209         dump_partition("Now ", X);
1210         dump_partition("Created new ", X_prime);
1211
1212         /* we have to ensure that the partition containing g is returned */
1213         if (winner == &env2) {
1214                 *pX = X_prime;
1215                 return X;
1216         }
1217
1218         return X_prime;
1219 }  /* split */
1220
1221 /**
1222  * Returns non-zero if the i'th input of a Phi node is live.
1223  *
1224  * @param phi  a Phi-node
1225  * @param i    an input number
1226  *
1227  * @return non-zero if the i'th input of the given Phi node is live
1228  */
1229 static int is_live_input(ir_node *phi, int i) {
1230         if (i >= 0) {
1231                 ir_node        *block = get_nodes_block(phi);
1232                 ir_node        *pred  = get_Block_cfgpred(block, i);
1233                 lattice_elem_t type   = get_node_type(pred);
1234
1235                 return type.tv != tarval_unreachable;
1236         }
1237         /* else it's the control input, always live */
1238         return 1;
1239 }  /* is_live_input */
1240
1241 /**
1242  * Return non-zero if a type is a constant.
1243  */
1244 static int is_constant_type(lattice_elem_t type) {
1245         if (type.tv != tarval_bottom && type.tv != tarval_top)
1246                 return 1;
1247         return 0;
1248 }  /* is_constant_type */
1249
1250 /**
1251  * Check whether a type is neither Top or a constant.
1252  * Note: U is handled like Top here, R is a constant.
1253  *
1254  * @param type  the type to check
1255  */
1256 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1257         if (is_tarval(type.tv)) {
1258                 if (type.tv == tarval_top)
1259                         return 0;
1260                 if (tarval_is_constant(type.tv))
1261                         return 0;
1262         } else {
1263                 /* is a symconst */
1264                 return 0;
1265         }
1266         return 1;
1267 }  /* type_is_neither_top_nor_const */
1268
1269 /**
1270  * Collect nodes to the touched list.
1271  *
1272  * @param list  the list which contains the nodes that must be evaluated
1273  * @param idx   the index of the def_use edge to evaluate
1274  * @param env   the environment
1275  */
1276 static void collect_touched(list_head *list, int idx, environment_t *env) {
1277         node_t  *x, *y;
1278         int     end_idx = env->end_idx;
1279
1280         list_for_each_entry(node_t, x, list, node_list) {
1281                 int num_edges;
1282
1283                 if (idx == -1) {
1284                         /* leader edges start AFTER follower edges */
1285                         x->next_edge = x->n_followers + 1;
1286                 }
1287                 num_edges = get_irn_n_outs(x->node);
1288
1289                 /* for all edges in x.L.def_use_{idx} */
1290                 while (x->next_edge <= num_edges) {
1291                         const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1292                         ir_node               *succ;
1293
1294                         /* check if we have necessary edges */
1295                         if (edge->pos > idx)
1296                                 break;
1297
1298                         ++x->next_edge;
1299
1300                         succ = edge->use;
1301
1302                         /* only non-commutative nodes */
1303                         if (env->commutative &&
1304                             (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1305                                 continue;
1306
1307                         /* ignore the "control input" for non-pinned nodes
1308                         if we are running in GCSE mode */
1309                         if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1310                                 continue;
1311
1312                         y = get_irn_node(succ);
1313                         assert(get_irn_n(succ, idx) == x->node);
1314
1315                         /* ignore block edges touching followers */
1316                         if (idx == -1 && y->is_follower)
1317                                 continue;
1318
1319                         if (is_constant_type(y->type)) {
1320                                 ir_opcode code = get_irn_opcode(succ);
1321                                 if (code == iro_Sub || code == iro_Cmp)
1322                                         add_to_cprop(y, env);
1323                         }
1324
1325                         /* Partitions of constants should not be split simply because their Nodes have unequal
1326                            functions or incongruent inputs. */
1327                         if (type_is_neither_top_nor_const(y->type) &&
1328                                 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1329                                         add_to_touched(y, env);
1330                         }
1331                 }
1332         }
1333 }  /* collect_touched */
1334
1335 /**
1336  * Collect commutative nodes to the touched list.
1337  *
1338  * @param list  the list which contains the nodes that must be evaluated
1339  * @param env   the environment
1340  */
1341 static void collect_commutative_touched(list_head *list, environment_t *env) {
1342         node_t  *x, *y;
1343
1344         list_for_each_entry(node_t, x, list, node_list) {
1345                 int num_edges;
1346
1347                 num_edges = get_irn_n_outs(x->node);
1348
1349                 x->next_edge = x->n_followers + 1;
1350
1351                 /* for all edges in x.L.def_use_{idx} */
1352                 while (x->next_edge <= num_edges) {
1353                         const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1354                         ir_node               *succ;
1355
1356                         /* check if we have necessary edges */
1357                         if (edge->pos > 1)
1358                                 break;
1359
1360                         ++x->next_edge;
1361                         if (edge->pos < 0)
1362                                 continue;
1363
1364                         succ = edge->use;
1365
1366                         /* only commutative nodes */
1367                         if (!is_op_commutative(get_irn_op(succ)))
1368                                 continue;
1369
1370                         y = get_irn_node(succ);
1371                         if (is_constant_type(y->type)) {
1372                                 ir_opcode code = get_irn_opcode(succ);
1373                                 if (code == iro_Eor)
1374                                         add_to_cprop(y, env);
1375                         }
1376
1377                         /* Partitions of constants should not be split simply because their Nodes have unequal
1378                            functions or incongruent inputs. */
1379                         if (type_is_neither_top_nor_const(y->type)) {
1380                                 add_to_touched(y, env);
1381                         }
1382                 }
1383         }
1384 }  /* collect_commutative_touched */
1385
1386 /**
1387  * Split the partitions if caused by the first entry on the worklist.
1388  *
1389  * @param env  the environment
1390  */
1391 static void cause_splits(environment_t *env) {
1392         partition_t *X, *Z, *N;
1393         int         idx;
1394
1395         /* remove the first partition from the worklist */
1396         X = env->worklist;
1397         env->worklist  = X->wl_next;
1398         X->on_worklist = 0;
1399
1400         dump_partition("Cause_split: ", X);
1401
1402         if (env->commutative) {
1403                 /* handle commutative nodes first */
1404
1405                 /* empty the touched set: already done, just clear the list */
1406                 env->touched = NULL;
1407
1408                 collect_commutative_touched(&X->Leader, env);
1409                 collect_commutative_touched(&X->Follower, env);
1410
1411                 for (Z = env->touched; Z != NULL; Z = N) {
1412                         node_t   *e;
1413                         node_t   *touched  = Z->touched;
1414                         unsigned n_touched = Z->n_touched;
1415
1416                         assert(Z->touched != NULL);
1417
1418                         /* beware, split might change Z */
1419                         N = Z->touched_next;
1420
1421                         /* remove it from the touched set */
1422                         Z->on_touched = 0;
1423
1424                         /* Empty local Z.touched. */
1425                         for (e = touched; e != NULL; e = e->next) {
1426                                 assert(e->is_follower == 0);
1427                                 e->on_touched = 0;
1428                         }
1429                         Z->touched   = NULL;
1430                         Z->n_touched = 0;
1431
1432                         if (0 < n_touched && n_touched < Z->n_leader) {
1433                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1434                                 split(&Z, touched, env);
1435                         } else
1436                                 assert(n_touched <= Z->n_leader);
1437                 }
1438         }
1439
1440         /* combine temporary leader and follower list */
1441         for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1442                 /* empty the touched set: already done, just clear the list */
1443                 env->touched = NULL;
1444
1445                 collect_touched(&X->Leader, idx, env);
1446                 collect_touched(&X->Follower, idx, env);
1447
1448                 for (Z = env->touched; Z != NULL; Z = N) {
1449                         node_t   *e;
1450                         node_t   *touched  = Z->touched;
1451                         unsigned n_touched = Z->n_touched;
1452
1453                         assert(Z->touched != NULL);
1454
1455                         /* beware, split might change Z */
1456                         N = Z->touched_next;
1457
1458                         /* remove it from the touched set */
1459                         Z->on_touched = 0;
1460
1461                         /* Empty local Z.touched. */
1462                         for (e = touched; e != NULL; e = e->next) {
1463                                 assert(e->is_follower == 0);
1464                                 e->on_touched = 0;
1465                         }
1466                         Z->touched   = NULL;
1467                         Z->n_touched = 0;
1468
1469                         if (0 < n_touched && n_touched < Z->n_leader) {
1470                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1471                                 split(&Z, touched, env);
1472                         } else
1473                                 assert(n_touched <= Z->n_leader);
1474                 }
1475         }
1476 }  /* cause_splits */
1477
1478 /**
1479  * Implements split_by_what(): Split a partition by characteristics given
1480  * by the what function.
1481  *
1482  * @param X     the partition to split
1483  * @param What  a function returning an Id for every node of the partition X
1484  * @param P     a list to store the result partitions
1485  * @param env   the environment
1486  *
1487  * @return *P
1488  */
1489 static partition_t *split_by_what(partition_t *X, what_func What,
1490                                   partition_t **P, environment_t *env) {
1491         node_t          *x, *S;
1492         listmap_t       map;
1493         listmap_entry_t *iter;
1494         partition_t     *R;
1495
1496         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1497         listmap_init(&map);
1498         list_for_each_entry(node_t, x, &X->Leader, node_list) {
1499                 void            *id = What(x, env);
1500                 listmap_entry_t *entry;
1501
1502                 if (id == NULL) {
1503                         /* input not allowed, ignore */
1504                         continue;
1505                 }
1506                 /* Add x to map[What(x)]. */
1507                 entry = listmap_find(&map, id);
1508                 x->next     = entry->list;
1509                 entry->list = x;
1510         }
1511         /* Let P be a set of Partitions. */
1512
1513         /* for all sets S except one in the range of map do */
1514         for (iter = map.values; iter != NULL; iter = iter->next) {
1515                 if (iter->next == NULL) {
1516                         /* this is the last entry, ignore */
1517                         break;
1518                 }
1519                 S = iter->list;
1520
1521                 /* Add SPLIT( X, S ) to P. */
1522                 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1523                 R = split(&X, S, env);
1524                 R->split_next = *P;
1525                 *P            = R;
1526         }
1527         /* Add X to P. */
1528         X->split_next = *P;
1529         *P            = X;
1530
1531         listmap_term(&map);
1532         return *P;
1533 }  /* split_by_what */
1534
1535 /** lambda n.(n.type) */
1536 static void *lambda_type(const node_t *node, environment_t *env) {
1537         (void)env;
1538         return node->type.tv;
1539 }  /* lambda_type */
1540
1541 /** lambda n.(n.opcode) */
1542 static void *lambda_opcode(const node_t *node, environment_t *env) {
1543         opcode_key_t key, *entry;
1544         ir_node      *irn = node->node;
1545
1546         key.code   = get_irn_opcode(irn);
1547         key.mode   = get_irn_mode(irn);
1548         key.arity  = get_irn_arity(irn);
1549         key.u.proj = 0;
1550         key.u.ent  = NULL;
1551
1552         switch (get_irn_opcode(irn)) {
1553         case iro_Proj:
1554                 key.u.proj = get_Proj_proj(irn);
1555                 break;
1556         case iro_Sel:
1557                 key.u.ent = get_Sel_entity(irn);
1558                 break;
1559         default:
1560                 break;
1561         }
1562
1563         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1564         return entry;
1565 }  /* lambda_opcode */
1566
1567 /** lambda n.(n[i].partition) */
1568 static void *lambda_partition(const node_t *node, environment_t *env) {
1569         ir_node *skipped = skip_Proj(node->node);
1570         ir_node *pred;
1571         node_t  *p;
1572         int     i = env->lambda_input;
1573
1574         if (i >= get_irn_arity(node->node)) {
1575                 /*
1576                  * We are outside the allowed range: This can happen even
1577                  * if we have split by opcode first: doing so might move Followers
1578                  * to Leaders and those will have a different opcode!
1579                  * Note that in this case the partition is on the cprop list and will be
1580                  * split again.
1581                  */
1582                 return NULL;
1583         }
1584
1585         /* ignore the "control input" for non-pinned nodes
1586            if we are running in GCSE mode */
1587         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1588                 return NULL;
1589
1590         pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1591         p    = get_irn_node(pred);
1592
1593         return p->part;
1594 }  /* lambda_partition */
1595
1596 /** lambda n.(n[i].partition) for commutative nodes */
1597 static void *lambda_commutative_partition(const node_t *node, environment_t *env) {
1598         ir_node     *irn     = node->node;
1599         ir_node     *skipped = skip_Proj(irn);
1600         ir_node     *pred, *left, *right;
1601         node_t      *p;
1602         partition_t *pl, *pr;
1603         int         i = env->lambda_input;
1604
1605         if (i >= get_irn_arity(node->node)) {
1606                 /*
1607                  * We are outside the allowed range: This can happen even
1608                  * if we have split by opcode first: doing so might move Followers
1609                  * to Leaders and those will have a different opcode!
1610                  * Note that in this case the partition is on the cprop list and will be
1611                  * split again.
1612                  */
1613                 return NULL;
1614         }
1615
1616         /* ignore the "control input" for non-pinned nodes
1617            if we are running in GCSE mode */
1618         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1619                 return NULL;
1620
1621         if (i == -1) {
1622                 pred = get_irn_n(skipped, i);
1623                 p    = get_irn_node(pred);
1624                 return p->part;
1625         }
1626
1627         if (is_op_commutative(get_irn_op(irn))) {
1628                 /* normalize partition order by returning the "smaller" on input 0,
1629                    the "bigger" on input 1. */
1630                 left  = get_binop_left(irn);
1631                 pl    = get_irn_node(left)->part;
1632                 right = get_binop_right(irn);
1633                 pr    = get_irn_node(right)->part;
1634
1635                 if (i == 0)
1636                         return pl < pr ? pl : pr;
1637                 else
1638                 return pl > pr ? pl : pr;
1639         } else {
1640                 /* a not split out Follower */
1641                 pred = get_irn_n(irn, i);
1642                 p    = get_irn_node(pred);
1643
1644                 return p->part;
1645         }
1646 }  /* lambda_commutative_partition */
1647
1648 /**
1649  * Returns true if a type is a constant.
1650  */
1651 static int is_con(const lattice_elem_t type) {
1652         /* be conservative */
1653         if (is_tarval(type.tv))
1654                 return tarval_is_constant(type.tv);
1655         return is_entity(type.sym.entity_p);
1656 }  /* is_con */
1657
1658 /**
1659  * Implements split_by().
1660  *
1661  * @param X    the partition to split
1662  * @param env  the environment
1663  */
1664 static void split_by(partition_t *X, environment_t *env) {
1665         partition_t *I, *P = NULL;
1666         int         input;
1667
1668         dump_partition("split_by", X);
1669
1670         if (X->n_leader == 1) {
1671                 /* we have only one leader, no need to split, just check it's type */
1672                 node_t *x = get_first_node(X);
1673                 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1674                 return;
1675         }
1676
1677         DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1678         P = split_by_what(X, lambda_type, &P, env);
1679         dump_split_list(P);
1680
1681         /* adjust the type tags, we have split partitions by type */
1682         for (I = P; I != NULL; I = I->split_next) {
1683                 node_t *x = get_first_node(I);
1684                 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1685         }
1686
1687         do {
1688                 partition_t *Y = P;
1689
1690                 P = P->split_next;
1691                 if (Y->n_leader > 1) {
1692                         /* we do not want split the TOP or constant partitions */
1693                         if (! Y->type_is_T_or_C) {
1694                                 partition_t *Q = NULL;
1695
1696                                 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1697                                 Q = split_by_what(Y, lambda_opcode, &Q, env);
1698                                 dump_split_list(Q);
1699
1700                                 do {
1701                                         partition_t *Z = Q;
1702
1703                                         Q = Q->split_next;
1704                                         if (Z->n_leader > 1) {
1705                                                 const node_t *first = get_first_node(Z);
1706                                                 int          arity  = get_irn_arity(first->node);
1707                                                 partition_t  *R, *S;
1708                                                 what_func    what = lambda_partition;
1709                                                 DEBUG_ONLY(char buf[64];)
1710
1711                                                 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1712                                                         what = lambda_commutative_partition;
1713
1714                                                 /*
1715                                                  * BEWARE: during splitting by input 2 for instance we might
1716                                                  * create new partitions which are different by input 1, so collect
1717                                                  * them and split further.
1718                                                  */
1719                                                 Z->split_next = NULL;
1720                                                 R             = Z;
1721                                                 S             = NULL;
1722                                                 for (input = arity - 1; input >= -1; --input) {
1723                                                         do {
1724                                                                 partition_t *Z_prime = R;
1725
1726                                                                 R = R->split_next;
1727                                                                 if (Z_prime->n_leader > 1) {
1728                                                                         env->lambda_input = input;
1729                                                                         DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1730                                                                         DEBUG_ONLY(what_reason = buf;)
1731                                                                         S = split_by_what(Z_prime, what, &S, env);
1732                                                                         dump_split_list(S);
1733                                                                 } else {
1734                                                                         Z_prime->split_next = S;
1735                                                                         S                   = Z_prime;
1736                                                                 }
1737                                                         } while (R != NULL);
1738                                                         R = S;
1739                                                         S = NULL;
1740                                                 }
1741                                         }
1742                                 } while (Q != NULL);
1743                         }
1744                 }
1745         } while (P != NULL);
1746 }  /* split_by */
1747
1748 /**
1749  * (Re-)compute the type for a given node.
1750  *
1751  * @param node  the node
1752  */
1753 static void default_compute(node_t *node) {
1754         int     i;
1755         ir_node *irn = node->node;
1756         node_t  *block = get_irn_node(get_nodes_block(irn));
1757
1758         if (block->type.tv == tarval_unreachable) {
1759                 node->type.tv = tarval_top;
1760                 return;
1761         }
1762
1763         /* if any of the data inputs have type top, the result is type top */
1764         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1765                 ir_node *pred = get_irn_n(irn, i);
1766                 node_t  *p    = get_irn_node(pred);
1767
1768                 if (p->type.tv == tarval_top) {
1769                         node->type.tv = tarval_top;
1770                         return;
1771                 }
1772         }
1773
1774         if (get_irn_mode(node->node) == mode_X)
1775                 node->type.tv = tarval_reachable;
1776         else
1777                 node->type.tv = computed_value(irn);
1778 }  /* default_compute */
1779
1780 /**
1781  * (Re-)compute the type for a Block node.
1782  *
1783  * @param node  the node
1784  */
1785 static void compute_Block(node_t *node) {
1786         int     i;
1787         ir_node *block = node->node;
1788
1789         if (block == get_irg_start_block(current_ir_graph)) {
1790                 /* start block is always reachable */
1791                 node->type.tv = tarval_reachable;
1792                 return;
1793         }
1794
1795         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1796                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1797
1798                 if (pred->type.tv == tarval_reachable) {
1799                         /* A block is reachable, if at least of predecessor is reachable. */
1800                         node->type.tv = tarval_reachable;
1801                         return;
1802                 }
1803         }
1804         node->type.tv = tarval_top;
1805 }  /* compute_Block */
1806
1807 /**
1808  * (Re-)compute the type for a Bad node.
1809  *
1810  * @param node  the node
1811  */
1812 static void compute_Bad(node_t *node) {
1813         /* Bad nodes ALWAYS compute Top */
1814         node->type.tv = tarval_top;
1815 }  /* compute_Bad */
1816
1817 /**
1818  * (Re-)compute the type for an Unknown node.
1819  *
1820  * @param node  the node
1821  */
1822 static void compute_Unknown(node_t *node) {
1823         /* While Unknown nodes should compute Top this is dangerous:
1824          * a Top input to a Cond would lead to BOTH control flows unreachable.
1825          * While this is correct in the given semantics, it would destroy the Firm
1826          * graph.
1827          *
1828          * It would be safe to compute Top IF it can be assured, that only Cmp
1829          * nodes are inputs to Conds. We check that first.
1830          * This is the way Frontends typically build Firm, but some optimizations
1831          * (cond_eval for instance) might replace them by Phib's...
1832          */
1833         node->type.tv = tarval_UNKNOWN;
1834 }  /* compute_Unknown */
1835
1836 /**
1837  * (Re-)compute the type for a Jmp node.
1838  *
1839  * @param node  the node
1840  */
1841 static void compute_Jmp(node_t *node) {
1842         node_t *block = get_irn_node(get_nodes_block(node->node));
1843
1844         node->type = block->type;
1845 }  /* compute_Jmp */
1846
1847 /**
1848  * (Re-)compute the type for the End node.
1849  *
1850  * @param node  the node
1851  */
1852 static void compute_End(node_t *node) {
1853         /* the End node is NOT dead of course */
1854         node->type.tv = tarval_reachable;
1855 }
1856
1857 /**
1858  * (Re-)compute the type for a SymConst node.
1859  *
1860  * @param node  the node
1861  */
1862 static void compute_SymConst(node_t *node) {
1863         ir_node *irn = node->node;
1864         node_t  *block = get_irn_node(get_nodes_block(irn));
1865
1866         if (block->type.tv == tarval_unreachable) {
1867                 node->type.tv = tarval_top;
1868                 return;
1869         }
1870         switch (get_SymConst_kind(irn)) {
1871         case symconst_addr_ent:
1872         /* case symconst_addr_name: cannot handle this yet */
1873                 node->type.sym = get_SymConst_symbol(irn);
1874                 break;
1875         default:
1876                 node->type.tv = computed_value(irn);
1877         }
1878 }  /* compute_SymConst */
1879
1880 /**
1881  * (Re-)compute the type for a Phi node.
1882  *
1883  * @param node  the node
1884  */
1885 static void compute_Phi(node_t *node) {
1886         int            i;
1887         ir_node        *phi = node->node;
1888         lattice_elem_t type;
1889
1890         /* if a Phi is in a unreachable block, its type is TOP */
1891         node_t *block = get_irn_node(get_nodes_block(phi));
1892
1893         if (block->type.tv == tarval_unreachable) {
1894                 node->type.tv = tarval_top;
1895                 return;
1896         }
1897
1898         /* Phi implements the Meet operation */
1899         type.tv = tarval_top;
1900         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1901                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
1902                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1903
1904                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1905                         /* ignore TOP inputs: We must check here for unreachable blocks,
1906                            because Firm constants live in the Start Block are NEVER Top.
1907                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1908                            comes from a unreachable input. */
1909                         continue;
1910                 }
1911                 if (pred->type.tv == tarval_bottom) {
1912                         node->type.tv = tarval_bottom;
1913                         return;
1914                 } else if (type.tv == tarval_top) {
1915                         /* first constant found */
1916                         type = pred->type;
1917                 } else if (type.tv != pred->type.tv) {
1918                         /* different constants or tarval_bottom */
1919                         node->type.tv = tarval_bottom;
1920                         return;
1921                 }
1922                 /* else nothing, constants are the same */
1923         }
1924         node->type = type;
1925 }  /* compute_Phi */
1926
1927 /**
1928  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1929  *
1930  * @param node  the node
1931  */
1932 static void compute_Add(node_t *node) {
1933         ir_node        *sub = node->node;
1934         node_t         *l   = get_irn_node(get_Add_left(sub));
1935         node_t         *r   = get_irn_node(get_Add_right(sub));
1936         lattice_elem_t a    = l->type;
1937         lattice_elem_t b    = r->type;
1938         ir_mode        *mode;
1939
1940         if (a.tv == tarval_top || b.tv == tarval_top) {
1941                 node->type.tv = tarval_top;
1942         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1943                 node->type.tv = tarval_bottom;
1944         } else {
1945                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1946                    must call tarval_add() first to handle this case! */
1947                 if (is_tarval(a.tv)) {
1948                         if (is_tarval(b.tv)) {
1949                                 node->type.tv = tarval_add(a.tv, b.tv);
1950                                 return;
1951                         }
1952                         mode = get_tarval_mode(a.tv);
1953                         if (a.tv == get_mode_null(mode)) {
1954                                 node->type = b;
1955                                 return;
1956                         }
1957                 } else if (is_tarval(b.tv)) {
1958                         mode = get_tarval_mode(b.tv);
1959                         if (b.tv == get_mode_null(mode)) {
1960                                 node->type = a;
1961                                 return;
1962                         }
1963                 }
1964                 node->type.tv = tarval_bottom;
1965         }
1966 }  /* compute_Add */
1967
1968 /**
1969  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1970  *
1971  * @param node  the node
1972  */
1973 static void compute_Sub(node_t *node) {
1974         ir_node        *sub = node->node;
1975         node_t         *l   = get_irn_node(get_Sub_left(sub));
1976         node_t         *r   = get_irn_node(get_Sub_right(sub));
1977         lattice_elem_t a    = l->type;
1978         lattice_elem_t b    = r->type;
1979         tarval         *tv;
1980
1981         if (a.tv == tarval_top || b.tv == tarval_top) {
1982                 node->type.tv = tarval_top;
1983         } else if (is_con(a) && is_con(b)) {
1984                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1985                         node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1986                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1987                         node->type = b;
1988                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1989                         node->type = a;
1990                 } else {
1991                         node->type.tv = tarval_bottom;
1992                 }
1993                 node->by_all_const = 1;
1994         } else if (r->part == l->part &&
1995                    (!mode_is_float(get_irn_mode(l->node)))) {
1996                 /*
1997                  * BEWARE: a - a is NOT always 0 for floating Point values, as
1998                  * NaN op NaN = NaN, so we must check this here.
1999                  */
2000                 ir_mode *mode = get_irn_mode(sub);
2001                 tv = get_mode_null(mode);
2002
2003                 /* if the node was ONCE evaluated by all constants, but now
2004                    this breaks AND we get from the argument partitions a different
2005                    result, switch to bottom.
2006                    This happens because initially all nodes are in the same partition ... */
2007                 if (node->by_all_const && node->type.tv != tv)
2008                         tv = tarval_bottom;
2009                 node->type.tv = tv;
2010         } else {
2011                 node->type.tv = tarval_bottom;
2012         }
2013 }  /* compute_Sub */
2014
2015 /**
2016  * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2017  *
2018  * @param node  the node
2019  */
2020 static void compute_Eor(node_t *node) {
2021         ir_node        *eor = node->node;
2022         node_t         *l   = get_irn_node(get_Eor_left(eor));
2023         node_t         *r   = get_irn_node(get_Eor_right(eor));
2024         lattice_elem_t a    = l->type;
2025         lattice_elem_t b    = r->type;
2026         tarval         *tv;
2027
2028         if (a.tv == tarval_top || b.tv == tarval_top) {
2029                 node->type.tv = tarval_top;
2030         } else if (is_con(a) && is_con(b)) {
2031                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2032                         node->type.tv = tarval_eor(a.tv, b.tv);
2033                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2034                         node->type = b;
2035                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2036                         node->type = a;
2037                 } else {
2038                         node->type.tv = tarval_bottom;
2039                 }
2040                 node->by_all_const = 1;
2041         } else if (r->part == l->part) {
2042                 ir_mode *mode = get_irn_mode(eor);
2043                 tv = get_mode_null(mode);
2044
2045                 /* if the node was ONCE evaluated by all constants, but now
2046                    this breaks AND we get from the argument partitions a different
2047                    result, switch to bottom.
2048                    This happens because initially all nodes are in the same partition ... */
2049                 if (node->by_all_const && node->type.tv != tv)
2050                         tv = tarval_bottom;
2051                 node->type.tv = tv;
2052         } else {
2053                 node->type.tv = tarval_bottom;
2054         }
2055 }  /* compute_Eor */
2056
2057 /**
2058  * (Re-)compute the type for Cmp.
2059  *
2060  * @param node  the node
2061  */
2062 static void compute_Cmp(node_t *node) {
2063         ir_node        *cmp  = node->node;
2064         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
2065         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
2066         lattice_elem_t a     = l->type;
2067         lattice_elem_t b     = r->type;
2068
2069         if (a.tv == tarval_top || b.tv == tarval_top) {
2070 #ifdef WITH_UNKNOWN
2071                 /*
2072                  * Top is congruent to any other value, we can
2073                  * calculate the compare result.
2074                  */
2075                 node->type.tv = tarval_b_true;
2076 #else
2077                 node->type.tv = tarval_top;
2078 #endif
2079         } else if (is_con(a) && is_con(b)) {
2080                 /* both nodes are constants, we can probably do something */
2081                 node->type.tv = tarval_b_true;
2082         } else if (r->part == l->part) {
2083                 /* both nodes congruent, we can probably do something */
2084                 node->type.tv = tarval_b_true;
2085         } else {
2086                 node->type.tv = tarval_bottom;
2087         }
2088 }  /* compute_Proj_Cmp */
2089
2090 /**
2091  * (Re-)compute the type for a Proj(Cmp).
2092  *
2093  * @param node  the node
2094  * @param cond  the predecessor Cmp node
2095  */
2096 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
2097         ir_node        *proj = node->node;
2098         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
2099         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
2100         lattice_elem_t a     = l->type;
2101         lattice_elem_t b     = r->type;
2102         pn_Cmp         pnc   = get_Proj_proj(proj);
2103         tarval         *tv;
2104
2105         if (a.tv == tarval_top || b.tv == tarval_top) {
2106 #ifdef WITH_UNKNOWN
2107                 /* see above */
2108                 tv = new_tarval_from_long((pnc & pn_Cmp_Eq) ^ pn_Cmp_Eq, mode_b);
2109                 goto not_equal;
2110 #else
2111                 node->type.tv = tarval_top;
2112 #endif
2113         } else if (is_con(a) && is_con(b)) {
2114                 default_compute(node);
2115                 node->by_all_const = 1;
2116         } else if (r->part == l->part &&
2117                    (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2118                 /*
2119                  * BEWARE: a == a is NOT always True for floating Point values, as
2120                  * NaN != NaN is defined, so we must check this here.
2121                  */
2122                 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
2123 #ifdef WITH_UNKNOWN
2124 not_equal:
2125 #endif
2126
2127                 /* if the node was ONCE evaluated by all constants, but now
2128                    this breaks AND we get from the argument partitions a different
2129                    result, switch to bottom.
2130                    This happens because initially all nodes are in the same partition ... */
2131                 if (node->by_all_const && node->type.tv != tv)
2132                         tv = tarval_bottom;
2133                 node->type.tv = tv;
2134         } else {
2135                 node->type.tv = tarval_bottom;
2136         }
2137 }  /* compute_Proj_Cmp */
2138
2139 /**
2140  * (Re-)compute the type for a Proj(Cond).
2141  *
2142  * @param node  the node
2143  * @param cond  the predecessor Cond node
2144  */
2145 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
2146         ir_node *proj     = node->node;
2147         long    pnc       = get_Proj_proj(proj);
2148         ir_node *sel      = get_Cond_selector(cond);
2149         node_t  *selector = get_irn_node(sel);
2150
2151         if (get_irn_mode(sel) == mode_b) {
2152                 /* an IF */
2153                 if (pnc == pn_Cond_true) {
2154                         if (selector->type.tv == tarval_b_false) {
2155                                 node->type.tv = tarval_unreachable;
2156                         } else if (selector->type.tv == tarval_b_true) {
2157                                 node->type.tv = tarval_reachable;
2158                         } else if (selector->type.tv == tarval_bottom) {
2159                                 node->type.tv = tarval_reachable;
2160                         } else {
2161                                 assert(selector->type.tv == tarval_top);
2162                                 node->type.tv = tarval_unreachable;
2163                         }
2164                 } else {
2165                         assert(pnc == pn_Cond_false);
2166
2167                         if (selector->type.tv == tarval_b_false) {
2168                                 node->type.tv = tarval_reachable;
2169                         } else if (selector->type.tv == tarval_b_true) {
2170                                 node->type.tv = tarval_unreachable;
2171                         } else if (selector->type.tv == tarval_bottom) {
2172                                 node->type.tv = tarval_reachable;
2173                         } else {
2174                                 assert(selector->type.tv == tarval_top);
2175                                 node->type.tv = tarval_unreachable;
2176                         }
2177                 }
2178         } else {
2179                 /* an SWITCH */
2180                 if (selector->type.tv == tarval_bottom) {
2181                         node->type.tv = tarval_reachable;
2182                 } else if (selector->type.tv == tarval_top) {
2183                         node->type.tv = tarval_unreachable;
2184                 } else {
2185                         long value = get_tarval_long(selector->type.tv);
2186                         if (pnc == get_Cond_defaultProj(cond)) {
2187                                 /* default switch, have to check ALL other cases */
2188                                 int i;
2189
2190                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2191                                         ir_node *succ = get_irn_out(cond, i);
2192
2193                                         if (succ == proj)
2194                                                 continue;
2195                                         if (value == get_Proj_proj(succ)) {
2196                                                 /* we found a match, will NOT take the default case */
2197                                                 node->type.tv = tarval_unreachable;
2198                                                 return;
2199                                         }
2200                                 }
2201                                 /* all cases checked, no match, will take default case */
2202                                 node->type.tv = tarval_reachable;
2203                         } else {
2204                                 /* normal case */
2205                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2206                         }
2207                 }
2208         }
2209 }  /* compute_Proj_Cond */
2210
2211 /**
2212  * (Re-)compute the type for a Proj-Node.
2213  *
2214  * @param node  the node
2215  */
2216 static void compute_Proj(node_t *node) {
2217         ir_node *proj = node->node;
2218         ir_mode *mode = get_irn_mode(proj);
2219         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2220         ir_node *pred  = get_Proj_pred(proj);
2221
2222         if (block->type.tv == tarval_unreachable) {
2223                 /* a Proj in a unreachable Block stay Top */
2224                 node->type.tv = tarval_top;
2225                 return;
2226         }
2227         if (get_irn_node(pred)->type.tv == tarval_top) {
2228                 /* if the predecessor is Top, its Proj follow */
2229                 node->type.tv = tarval_top;
2230                 return;
2231         }
2232
2233         if (mode == mode_M) {
2234                 /* mode M is always bottom */
2235                 node->type.tv = tarval_bottom;
2236                 return;
2237         }
2238         if (mode != mode_X) {
2239                 if (is_Cmp(pred))
2240                         compute_Proj_Cmp(node, pred);
2241                 else
2242                         default_compute(node);
2243                 return;
2244         }
2245         /* handle mode_X nodes */
2246
2247         switch (get_irn_opcode(pred)) {
2248         case iro_Start:
2249                 /* the Proj_X from the Start is always reachable.
2250                    However this is already handled at the top. */
2251                 node->type.tv = tarval_reachable;
2252                 break;
2253         case iro_Cond:
2254                 compute_Proj_Cond(node, pred);
2255                 break;
2256         default:
2257                 default_compute(node);
2258         }
2259 }  /* compute_Proj */
2260
2261 /**
2262  * (Re-)compute the type for a Confirm.
2263  *
2264  * @param node  the node
2265  */
2266 static void compute_Confirm(node_t *node) {
2267         ir_node *confirm = node->node;
2268         node_t  *pred = get_irn_node(get_Confirm_value(confirm));
2269
2270         if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2271                 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2272
2273                 if (is_con(bound->type)) {
2274                         /* is equal to a constant */
2275                         node->type = bound->type;
2276                         return;
2277                 }
2278         }
2279         /* a Confirm is a copy OR a Const */
2280         node->type = pred->type;
2281 }  /* compute_Confirm */
2282
2283 /**
2284  * (Re-)compute the type for a Max.
2285  *
2286  * @param node  the node
2287  */
2288 static void compute_Max(node_t *node) {
2289         ir_node        *op = node->node;
2290         node_t         *l  = get_irn_node(get_binop_left(op));
2291         node_t         *r  = get_irn_node(get_binop_right(op));
2292         lattice_elem_t a   = l->type;
2293         lattice_elem_t b   = r->type;
2294
2295         if (a.tv == tarval_top || b.tv == tarval_top) {
2296                 node->type.tv = tarval_top;
2297         } else if (is_con(a) && is_con(b)) {
2298                 /* both nodes are constants, we can probably do something */
2299                 if (a.tv == b.tv) {
2300                         /* this case handles SymConsts as well */
2301                         node->type = a;
2302                 } else {
2303                         ir_mode *mode   = get_irn_mode(op);
2304                         tarval  *tv_min = get_mode_min(mode);
2305
2306                         if (a.tv == tv_min)
2307                                 node->type = b;
2308                         else if (b.tv == tv_min)
2309                                 node->type = a;
2310                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2311                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2312                                         node->type.tv = a.tv;
2313                                 else
2314                                         node->type.tv = b.tv;
2315                         } else {
2316                                 node->type.tv = tarval_bad;
2317                         }
2318                 }
2319         } else if (r->part == l->part) {
2320                 /* both nodes congruent, we can probably do something */
2321                 node->type = a;
2322         } else {
2323                 node->type.tv = tarval_bottom;
2324         }
2325 }  /* compute_Max */
2326
2327 /**
2328  * (Re-)compute the type for a Min.
2329  *
2330  * @param node  the node
2331  */
2332 static void compute_Min(node_t *node) {
2333         ir_node        *op = node->node;
2334         node_t         *l  = get_irn_node(get_binop_left(op));
2335         node_t         *r  = get_irn_node(get_binop_right(op));
2336         lattice_elem_t a   = l->type;
2337         lattice_elem_t b   = r->type;
2338
2339         if (a.tv == tarval_top || b.tv == tarval_top) {
2340                 node->type.tv = tarval_top;
2341         } else if (is_con(a) && is_con(b)) {
2342                 /* both nodes are constants, we can probably do something */
2343                 if (a.tv == b.tv) {
2344                         /* this case handles SymConsts as well */
2345                         node->type = a;
2346                 } else {
2347                         ir_mode *mode   = get_irn_mode(op);
2348                         tarval  *tv_max = get_mode_max(mode);
2349
2350                         if (a.tv == tv_max)
2351                                 node->type = b;
2352                         else if (b.tv == tv_max)
2353                                 node->type = a;
2354                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2355                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2356                                         node->type.tv = a.tv;
2357                                 else
2358                                         node->type.tv = b.tv;
2359                         } else {
2360                                 node->type.tv = tarval_bad;
2361                         }
2362                 }
2363         } else if (r->part == l->part) {
2364                 /* both nodes congruent, we can probably do something */
2365                 node->type = a;
2366         } else {
2367                 node->type.tv = tarval_bottom;
2368         }
2369 }  /* compute_Min */
2370
2371 /**
2372  * (Re-)compute the type for a given node.
2373  *
2374  * @param node  the node
2375  */
2376 static void compute(node_t *node) {
2377         compute_func func;
2378
2379         if (is_no_Block(node->node)) {
2380                 node_t *block = get_irn_node(get_nodes_block(node->node));
2381
2382                 if (block->type.tv == tarval_unreachable) {
2383                         node->type.tv = tarval_top;
2384                         return;
2385                 }
2386         }
2387
2388         func = (compute_func)node->node->op->ops.generic;
2389         if (func != NULL)
2390                 func(node);
2391 }  /* compute */
2392
2393 /*
2394  * Identity functions: Note that one might thing that identity() is just a
2395  * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2396  * here, because it expects that the identity node is one of the inputs, which is NOT
2397  * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2398  * So, we have our own implementation, which copies some parts of equivalent_node()
2399  */
2400
2401 /**
2402  * Calculates the Identity for Phi nodes
2403  */
2404 static node_t *identity_Phi(node_t *node) {
2405         ir_node *phi    = node->node;
2406         ir_node *block  = get_nodes_block(phi);
2407         node_t  *n_part = NULL;
2408         int     i;
2409
2410         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2411                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2412
2413                 if (pred_X->type.tv == tarval_reachable) {
2414                         node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2415
2416                         if (n_part == NULL)
2417                                 n_part = pred;
2418                         else if (n_part->part != pred->part) {
2419                                 /* incongruent inputs, not a follower */
2420                                 return node;
2421                         }
2422                 }
2423         }
2424         /* if n_part is NULL here, all inputs path are dead, the Phi computes
2425          * tarval_top, is in the TOP partition and should NOT being split! */
2426         assert(n_part != NULL);
2427         return n_part;
2428 }  /* identity_Phi */
2429
2430 /**
2431  * Calculates the Identity for commutative 0 neutral nodes.
2432  */
2433 static node_t *identity_comm_zero_binop(node_t *node) {
2434         ir_node *op   = node->node;
2435         node_t  *a    = get_irn_node(get_binop_left(op));
2436         node_t  *b    = get_irn_node(get_binop_right(op));
2437         ir_mode *mode = get_irn_mode(op);
2438         tarval  *zero;
2439
2440         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2441         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2442                 return node;
2443
2444         /* node: no input should be tarval_top, else the binop would be also
2445          * Top and not being split. */
2446         zero = get_mode_null(mode);
2447         if (a->type.tv == zero)
2448                 return b;
2449         if (b->type.tv == zero)
2450                 return a;
2451         return node;
2452 }  /* identity_comm_zero_binop */
2453
2454 /**
2455  * Calculates the Identity for Shift nodes.
2456  */
2457 static node_t *identity_shift(node_t *node) {
2458         ir_node *op   = node->node;
2459         node_t  *b    = get_irn_node(get_binop_right(op));
2460         ir_mode *mode = get_irn_mode(b->node);
2461         tarval  *zero;
2462
2463         /* node: no input should be tarval_top, else the binop would be also
2464          * Top and not being split. */
2465         zero = get_mode_null(mode);
2466         if (b->type.tv == zero)
2467                 return get_irn_node(get_binop_left(op));
2468         return node;
2469 }  /* identity_shift */
2470
2471 /**
2472  * Calculates the Identity for Mul nodes.
2473  */
2474 static node_t *identity_Mul(node_t *node) {
2475         ir_node *op   = node->node;
2476         node_t  *a    = get_irn_node(get_Mul_left(op));
2477         node_t  *b    = get_irn_node(get_Mul_right(op));
2478         ir_mode *mode = get_irn_mode(op);
2479         tarval  *one;
2480
2481         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2482         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2483                 return node;
2484
2485         /* node: no input should be tarval_top, else the binop would be also
2486          * Top and not being split. */
2487         one = get_mode_one(mode);
2488         if (a->type.tv == one)
2489                 return b;
2490         if (b->type.tv == one)
2491                 return a;
2492         return node;
2493 }  /* identity_Mul */
2494
2495 /**
2496  * Calculates the Identity for Sub nodes.
2497  */
2498 static node_t *identity_Sub(node_t *node) {
2499         ir_node *sub  = node->node;
2500         node_t  *b    = get_irn_node(get_Sub_right(sub));
2501         ir_mode *mode = get_irn_mode(sub);
2502
2503         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2504         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2505                 return node;
2506
2507         /* node: no input should be tarval_top, else the binop would be also
2508          * Top and not being split. */
2509         if (b->type.tv == get_mode_null(mode))
2510                 return get_irn_node(get_Sub_left(sub));
2511         return node;
2512 }  /* identity_Sub */
2513
2514 /**
2515  * Calculates the Identity for And nodes.
2516  */
2517 static node_t *identity_And(node_t *node) {
2518         ir_node *and = node->node;
2519         node_t  *a   = get_irn_node(get_And_left(and));
2520         node_t  *b   = get_irn_node(get_And_right(and));
2521         tarval  *neutral = get_mode_all_one(get_irn_mode(and));
2522
2523         /* node: no input should be tarval_top, else the And would be also
2524          * Top and not being split. */
2525         if (a->type.tv == neutral)
2526                 return b;
2527         if (b->type.tv == neutral)
2528                 return a;
2529         return node;
2530 }  /* identity_And */
2531
2532 /**
2533  * Calculates the Identity for Confirm nodes.
2534  */
2535 static node_t *identity_Confirm(node_t *node) {
2536         ir_node *confirm = node->node;
2537
2538         /* a Confirm is always a Copy */
2539         return get_irn_node(get_Confirm_value(confirm));
2540 }  /* identity_Confirm */
2541
2542 /**
2543  * Calculates the Identity for Mux nodes.
2544  */
2545 static node_t *identity_Mux(node_t *node) {
2546         ir_node *mux = node->node;
2547         node_t  *t   = get_irn_node(get_Mux_true(mux));
2548         node_t  *f   = get_irn_node(get_Mux_false(mux));
2549         /*node_t  *sel; */
2550
2551         if (t->part == f->part)
2552                 return t;
2553
2554         /* for now, the 1-input identity is not supported */
2555 #if 0
2556         sel = get_irn_node(get_Mux_sel(mux));
2557
2558         /* Mux sel input is mode_b, so it is always a tarval */
2559         if (sel->type.tv == tarval_b_true)
2560                 return t;
2561         if (sel->type.tv == tarval_b_false)
2562                 return f;
2563 #endif
2564         return node;
2565 }  /* identity_Mux */
2566
2567 /**
2568  * Calculates the Identity for Min nodes.
2569  */
2570 static node_t *identity_Min(node_t *node) {
2571         ir_node *op   = node->node;
2572         node_t  *a    = get_irn_node(get_binop_left(op));
2573         node_t  *b    = get_irn_node(get_binop_right(op));
2574         ir_mode *mode = get_irn_mode(op);
2575         tarval  *tv_max;
2576
2577         if (a->part == b->part) {
2578                 /* leader of multiple predecessors */
2579                 return a;
2580         }
2581
2582         /* works even with NaN */
2583         tv_max = get_mode_max(mode);
2584         if (a->type.tv == tv_max)
2585                 return b;
2586         if (b->type.tv == tv_max)
2587                 return a;
2588         return node;
2589 }  /* identity_Min */
2590
2591 /**
2592  * Calculates the Identity for Max nodes.
2593  */
2594 static node_t *identity_Max(node_t *node) {
2595         ir_node *op   = node->node;
2596         node_t  *a    = get_irn_node(get_binop_left(op));
2597         node_t  *b    = get_irn_node(get_binop_right(op));
2598         ir_mode *mode = get_irn_mode(op);
2599         tarval  *tv_min;
2600
2601         if (a->part == b->part) {
2602                 /* leader of multiple predecessors */
2603                 return a;
2604         }
2605
2606         /* works even with NaN */
2607         tv_min = get_mode_min(mode);
2608         if (a->type.tv == tv_min)
2609                 return b;
2610         if (b->type.tv == tv_min)
2611                 return a;
2612         return node;
2613 }  /* identity_Max */
2614
2615 /**
2616  * Calculates the Identity for nodes.
2617  */
2618 static node_t *identity(node_t *node) {
2619         ir_node *irn = node->node;
2620
2621         switch (get_irn_opcode(irn)) {
2622         case iro_Phi:
2623                 return identity_Phi(node);
2624         case iro_Mul:
2625                 return identity_Mul(node);
2626         case iro_Add:
2627         case iro_Or:
2628         case iro_Eor:
2629                 return identity_comm_zero_binop(node);
2630         case iro_Shr:
2631         case iro_Shl:
2632         case iro_Shrs:
2633         case iro_Rotl:
2634                 return identity_shift(node);
2635         case iro_And:
2636                 return identity_And(node);
2637         case iro_Sub:
2638                 return identity_Sub(node);
2639         case iro_Confirm:
2640                 return identity_Confirm(node);
2641         case iro_Mux:
2642                 return identity_Mux(node);
2643         case iro_Min:
2644                 return identity_Min(node);
2645         case iro_Max:
2646                 return identity_Max(node);
2647         default:
2648                 return node;
2649         }
2650 }  /* identity */
2651
2652 /**
2653  * Node follower is a (new) follower of leader, segregate Leader
2654  * out edges.
2655  */
2656 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2657         ir_node *l   = leader->node;
2658         int     j, i, n = get_irn_n_outs(l);
2659
2660         DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2661         /* The leader edges must remain sorted, but follower edges can
2662            be unsorted. */
2663         for (i = leader->n_followers + 1; i <= n; ++i) {
2664                 if (l->out[i].use == follower) {
2665                         ir_def_use_edge t = l->out[i];
2666
2667                         for (j = i - 1; j >= leader->n_followers + 1; --j)
2668                                 l->out[j + 1] = l->out[j];
2669                         ++leader->n_followers;
2670                         l->out[leader->n_followers] = t;
2671                         break;
2672                 }
2673         }
2674 }  /* segregate_def_use_chain_1 */
2675
2676 /**
2677  * Node follower is a (new) follower segregate its Leader
2678  * out edges.
2679  *
2680  * @param follower  the follower IR node
2681  */
2682 static void segregate_def_use_chain(const ir_node *follower) {
2683         int i;
2684
2685         for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2686                 node_t *pred = get_irn_node(get_irn_n(follower, i));
2687
2688                 segregate_def_use_chain_1(follower, pred);
2689         }
2690 }  /* segregate_def_use_chain */
2691
2692 /**
2693  * Propagate constant evaluation.
2694  *
2695  * @param env  the environment
2696  */
2697 static void propagate(environment_t *env) {
2698         partition_t    *X, *Y;
2699         node_t         *x;
2700         lattice_elem_t old_type;
2701         node_t         *fallen;
2702         unsigned       n_fallen, old_type_was_T_or_C;
2703         int            i;
2704
2705         while (env->cprop != NULL) {
2706                 void *oldopcode = NULL;
2707
2708                 /* remove the first partition X from cprop */
2709                 X           = env->cprop;
2710                 X->on_cprop = 0;
2711                 env->cprop  = X->cprop_next;
2712
2713                 old_type_was_T_or_C = X->type_is_T_or_C;
2714
2715                 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2716                 fallen   = NULL;
2717                 n_fallen = 0;
2718                 while (! list_empty(&X->cprop)) {
2719                         /* remove the first Node x from X.cprop */
2720                         x = list_entry(X->cprop.next, node_t, cprop_list);
2721                         //assert(x->part == X);
2722                         list_del(&x->cprop_list);
2723                         x->on_cprop = 0;
2724
2725                         if (x->is_follower && identity(x) == x) {
2726                                 /* check the opcode first */
2727                                 if (oldopcode == NULL) {
2728                                         oldopcode = lambda_opcode(get_first_node(X), env);
2729                                 }
2730                                 if (oldopcode != lambda_opcode(x, env)) {
2731                                         if (x->on_fallen == 0) {
2732                                                 /* different opcode -> x falls out of this partition */
2733                                                 x->next      = fallen;
2734                                                 x->on_fallen = 1;
2735                                                 fallen       = x;
2736                                                 ++n_fallen;
2737                                                 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2738                                         }
2739                                 }
2740
2741                                 /* x will make the follower -> leader transition */
2742                                 follower_to_leader(x);
2743                         }
2744
2745                         /* compute a new type for x */
2746                         old_type = x->type;
2747                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2748                         compute(x);
2749                         if (x->type.tv != old_type.tv) {
2750                                 verify_type(old_type, x->type);
2751                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2752
2753                                 if (x->on_fallen == 0) {
2754                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2755                                            not already on the list. */
2756                                         x->next      = fallen;
2757                                         x->on_fallen = 1;
2758                                         fallen       = x;
2759                                         ++n_fallen;
2760                                         DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2761                                 }
2762                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2763                                         ir_node *succ = get_irn_out(x->node, i);
2764                                         node_t  *y    = get_irn_node(succ);
2765
2766                                         /* Add y to y.partition.cprop. */
2767                                         add_to_cprop(y, env);
2768                                 }
2769                         }
2770                 }
2771
2772                 if (n_fallen > 0 && n_fallen != X->n_leader) {
2773                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2774                         Y = split(&X, fallen, env);
2775                         /*
2776                          * We have split out fallen node. The type of the result
2777                          * partition is NOT set yet.
2778                          */
2779                         Y->type_is_T_or_C = 0;
2780                 } else {
2781                         Y = X;
2782                 }
2783                 /* remove the flags from the fallen list */
2784                 for (x = fallen; x != NULL; x = x->next)
2785                         x->on_fallen = 0;
2786
2787                 if (old_type_was_T_or_C) {
2788                         node_t *y, *tmp;
2789
2790                         /* check if some nodes will make the leader -> follower transition */
2791                         list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2792                                 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2793                                         node_t *eq_node = identity(y);
2794
2795                                         if (eq_node != y && eq_node->part == y->part) {
2796                                                 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2797                                                 /* move to Follower */
2798                                                 y->is_follower = 1;
2799                                                 list_del(&y->node_list);
2800                                                 list_add_tail(&y->node_list, &Y->Follower);
2801                                                 --Y->n_leader;
2802
2803                                                 segregate_def_use_chain(y->node);
2804                                         }
2805                                 }
2806                         }
2807                 }
2808                 split_by(Y, env);
2809         }
2810 }  /* propagate */
2811
2812 /**
2813  * Get the leader for a given node from its congruence class.
2814  *
2815  * @param irn  the node
2816  */
2817 static ir_node *get_leader(node_t *node) {
2818         partition_t *part = node->part;
2819
2820         if (part->n_leader > 1 || node->is_follower) {
2821                 if (node->is_follower) {
2822                         DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2823                 }
2824                 else
2825                         DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2826
2827                 return get_first_node(part)->node;
2828         }
2829         return node->node;
2830 }  /* get_leader */
2831
2832 /**
2833  * Return non-zero if the control flow predecessor node pred
2834  * is the only reachable control flow exit of its block.
2835  *
2836  * @param pred  the control flow exit
2837  */
2838 static int can_exchange(ir_node *pred) {
2839         if (is_Start(pred))
2840                 return 0;
2841         else if (is_Jmp(pred))
2842                 return 1;
2843         else if (get_irn_mode(pred) == mode_T) {
2844                 int i, k;
2845
2846                 /* if the predecessor block has more than one
2847                    reachable outputs we cannot remove the block */
2848                 k = 0;
2849                 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2850                         ir_node *proj = get_irn_out(pred, i);
2851                         node_t  *node;
2852
2853                         /* skip non-control flow Proj's */
2854                         if (get_irn_mode(proj) != mode_X)
2855                                 continue;
2856
2857                         node = get_irn_node(proj);
2858                         if (node->type.tv == tarval_reachable) {
2859                                 if (++k > 1)
2860                                         return 0;
2861                         }
2862                 }
2863                 return 1;
2864         }
2865         return 0;
2866 }  /* can_exchange */
2867
2868 /**
2869  * Block Post-Walker, apply the analysis results on control flow by
2870  * shortening Phi's and Block inputs.
2871  */
2872 static void apply_cf(ir_node *block, void *ctx) {
2873         environment_t *env = ctx;
2874         node_t        *node = get_irn_node(block);
2875         int           i, j, k, n;
2876         ir_node       **ins, **in_X;
2877         ir_node       *phi, *next;
2878
2879         n = get_Block_n_cfgpreds(block);
2880
2881         if (node->type.tv == tarval_unreachable) {
2882                 env->modified = 1;
2883
2884                 for (i = n - 1; i >= 0; --i) {
2885                         ir_node *pred = get_Block_cfgpred(block, i);
2886
2887                         if (! is_Bad(pred)) {
2888                                 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2889
2890                                 if (pred_bl->flagged == 0) {
2891                                         pred_bl->flagged = 3;
2892
2893                                         if (pred_bl->type.tv == tarval_reachable) {
2894                                                 /*
2895                                                  * We will remove an edge from block to its pred.
2896                                                  * This might leave the pred block as an endless loop
2897                                                  */
2898                                                 if (! is_backedge(block, i))
2899                                                         keep_alive(pred_bl->node);
2900                                         }
2901                                 }
2902                         }
2903                 }
2904
2905                 /* the EndBlock is always reachable even if the analysis
2906                    finds out the opposite :-) */
2907                 if (block != get_irg_end_block(current_ir_graph)) {
2908                         /* mark dead blocks */
2909                         set_Block_dead(block);
2910                         DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
2911                 } else {
2912                         /* the endblock is unreachable */
2913                         set_irn_in(block, 0, NULL);
2914                 }
2915                 return;
2916         }
2917
2918         if (n == 1) {
2919                 /* only one predecessor combine */
2920                 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2921
2922                 if (can_exchange(pred)) {
2923                         ir_node *new_block = get_nodes_block(pred);
2924                         DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
2925                         DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
2926                         exchange(block, new_block);
2927                         node->node = new_block;
2928                         env->modified = 1;
2929                 }
2930                 return;
2931         }
2932
2933         NEW_ARR_A(ir_node *, in_X, n);
2934         k = 0;
2935         for (i = 0; i < n; ++i) {
2936                 ir_node *pred = get_Block_cfgpred(block, i);
2937                 node_t  *node = get_irn_node(pred);
2938
2939                 if (node->type.tv == tarval_reachable) {
2940                         in_X[k++] = pred;
2941                 } else {
2942                         DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
2943                         if (! is_Bad(pred)) {
2944                                 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2945
2946                                 if (pred_bl->flagged == 0) {
2947                                         pred_bl->flagged = 3;
2948
2949                                         if (pred_bl->type.tv == tarval_reachable) {
2950                                                 /*
2951                                                  * We will remove an edge from block to its pred.
2952                                                  * This might leave the pred block as an endless loop
2953                                                  */
2954                                                 if (! is_backedge(block, i))
2955                                                         keep_alive(pred_bl->node);
2956                                         }
2957                                 }
2958                         }
2959                 }
2960         }
2961         if (k >= n)
2962                 return;
2963
2964         NEW_ARR_A(ir_node *, ins, n);
2965         for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2966                 node_t *node = get_irn_node(phi);
2967
2968                 next = get_Phi_next(phi);
2969                 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2970                         /* this Phi is replaced by a constant */
2971                         tarval  *tv = node->type.tv;
2972                         ir_node *c  = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2973
2974                         set_irn_node(c, node);
2975                         node->node = c;
2976                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2977                         DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
2978                         exchange(phi, c);
2979                         env->modified = 1;
2980                 } else {
2981                         j = 0;
2982                         for (i = 0; i < n; ++i) {
2983                                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2984
2985                                 if (pred->type.tv == tarval_reachable) {
2986                                         ins[j++] = get_Phi_pred(phi, i);
2987                                 }
2988                         }
2989                         if (j == 1) {
2990                                 /* this Phi is replaced by a single predecessor */
2991                                 ir_node *s = ins[0];
2992                                 node_t *phi_node = get_irn_node(phi);
2993
2994                                 node->node = s;
2995                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2996                                 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
2997                                 exchange(phi, s);
2998                                 phi_node->node = s;
2999                                 env->modified = 1;
3000                         } else {
3001                                 set_irn_in(phi, j, ins);
3002                                 env->modified = 1;
3003                         }
3004                 }
3005         }
3006
3007         if (k == 1) {
3008                 /* this Block has only one live predecessor */
3009                 ir_node *pred = skip_Proj(in_X[0]);
3010
3011                 if (can_exchange(pred)) {
3012                         ir_node *new_block = get_nodes_block(pred);
3013                         DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3014                         exchange(block, new_block);
3015                         node->node = new_block;
3016                         env->modified = 1;
3017                 }
3018         } else {
3019                 set_irn_in(block, k, in_X);
3020                 env->modified = 1;
3021         }
3022 }  /* apply_cf */
3023
3024 /**
3025  * Exchange a node by its leader.
3026  * Beware: in rare cases the mode might be wrong here, for instance
3027  * AddP(x, NULL) is a follower of x, but with different mode.
3028  * Fix it here.
3029  */
3030 static void exchange_leader(ir_node *irn, ir_node *leader) {
3031         ir_mode *mode = get_irn_mode(irn);
3032         if (mode != get_irn_mode(leader)) {
3033                 /* The conv is a no-op, so we are fre to place in
3034                  * either in the block of the leader OR in irn's block.
3035                  * Propably placing it into leaders block might reduce
3036                  * the number of Conv due to CSE. */
3037                 ir_node  *block = get_nodes_block(leader);
3038                 dbg_info *dbg   = get_irn_dbg_info(irn);
3039
3040                 leader = new_rd_Conv(dbg, current_ir_graph, block, leader, mode);
3041         }
3042         exchange(irn, leader);
3043 }
3044
3045 /**
3046  * Post-Walker, apply the analysis results;
3047  */
3048 static void apply_result(ir_node *irn, void *ctx) {
3049         environment_t *env = ctx;
3050         node_t        *node = get_irn_node(irn);
3051
3052         if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3053                 /* blocks already handled, do not touch the End node */
3054         } else {
3055                 node_t *block = get_irn_node(get_nodes_block(irn));
3056
3057                 if (block->type.tv == tarval_unreachable) {
3058                         ir_node *bad = get_irg_bad(current_ir_graph);
3059
3060                         /* here, bad might already have a node, but this can be safely ignored
3061                            as long as bad has at least ONE valid node */
3062                         set_irn_node(bad, node);
3063                         node->node = bad;
3064                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3065                         exchange(irn, bad);
3066                         env->modified = 1;
3067                 }
3068                 else if (node->type.tv == tarval_unreachable) {
3069                         /* don't kick away Unknown */
3070                         if (! is_Unknown(irn)) {
3071                                 ir_node *bad = get_irg_bad(current_ir_graph);
3072
3073                                 /* see comment above */
3074                                 set_irn_node(bad, node);
3075                                 node->node = bad;
3076                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3077                                 exchange(irn, bad);
3078                                 env->modified = 1;
3079                         }
3080                 }
3081                 else if (get_irn_mode(irn) == mode_X) {
3082                         if (is_Proj(irn)) {
3083                                 /* leave or Jmp */
3084                                 ir_node *cond = get_Proj_pred(irn);
3085
3086                                 if (is_Cond(cond)) {
3087                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
3088
3089                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
3090                                                 /* Cond selector is a constant and the Proj is reachable, make a Jmp */
3091                                                 ir_node *jmp  = new_r_Jmp(current_ir_graph, block->node);
3092                                                 set_irn_node(jmp, node);
3093                                                 node->node = jmp;
3094                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3095                                                 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3096                                                 exchange(irn, jmp);
3097                                                 env->modified = 1;
3098                                         }
3099                                 }
3100                         }
3101                 } else {
3102                         /* normal data node */
3103                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3104                                 tarval *tv = node->type.tv;
3105
3106                                 /*
3107                                  * Beware: never replace mode_T nodes by constants. Currently we must mark
3108                                  * mode_T nodes with constants, but do NOT replace them.
3109                                  */
3110                                 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3111                                         /* can be replaced by a constant */
3112                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
3113                                         set_irn_node(c, node);
3114                                         node->node = c;
3115                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3116                                         DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3117                                         exchange_leader(irn, c);
3118                                         env->modified = 1;
3119                                 }
3120                         } else if (is_entity(node->type.sym.entity_p)) {
3121                                 if (! is_SymConst(irn)) {
3122                                         /* can be replaced by a SymConst */
3123                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3124                                         set_irn_node(symc, node);
3125                                         node->node = symc;
3126
3127                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3128                                         DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3129                                         exchange_leader(irn, symc);
3130                                         env->modified = 1;
3131                                 }
3132                         } else if (is_Confirm(irn)) {
3133                                 /* Confirms are always follower, but do not kill them here */
3134                         } else {
3135                                 ir_node *leader = get_leader(node);
3136
3137                                 if (leader != irn) {
3138                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3139                                         if (node->is_follower)
3140                                                 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3141                                         else
3142                                                 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3143                                         exchange_leader(irn, leader);
3144                                         env->modified = 1;
3145                                 }
3146                         }
3147                 }
3148         }
3149 }  /* apply_result */
3150
3151 /**
3152  * Fix the keep-alives by deleting unreachable ones.
3153  */
3154 static void apply_end(ir_node *end, environment_t *env) {
3155         int i, j,  n = get_End_n_keepalives(end);
3156         ir_node **in;
3157
3158         if (n > 0)
3159                 NEW_ARR_A(ir_node *, in, n);
3160
3161         /* fix the keep alive */
3162         for (i = j = 0; i < n; i++) {
3163                 ir_node *ka   = get_End_keepalive(end, i);
3164                 node_t  *node = get_irn_node(ka);
3165
3166                 if (! is_Block(ka))
3167                         node = get_irn_node(get_nodes_block(ka));
3168
3169                 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3170                         in[j++] = ka;
3171         }
3172         if (j != n) {
3173                 set_End_keepalives(end, j, in);
3174                 env->modified = 1;
3175         }
3176 }  /* apply_end */
3177
3178 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3179
3180 /**
3181  * sets the generic functions to compute.
3182  */
3183 static void set_compute_functions(void) {
3184         int i;
3185
3186         /* set the default compute function */
3187         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3188                 ir_op *op = get_irp_opcode(i);
3189                 op->ops.generic = (op_func)default_compute;
3190         }
3191
3192         /* set specific functions */
3193         SET(Block);
3194         SET(Unknown);
3195         SET(Bad);
3196         SET(Jmp);
3197         SET(Phi);
3198         SET(Add);
3199         SET(Sub);
3200         SET(Eor);
3201         SET(SymConst);
3202         SET(Cmp);
3203         SET(Proj);
3204         SET(Confirm);
3205         SET(End);
3206
3207         if (op_Max != NULL)
3208                 SET(Max);
3209         if (op_Min != NULL)
3210                 SET(Min);
3211
3212 }  /* set_compute_functions */
3213
3214 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
3215 #ifdef DEBUG_libfirm
3216         ir_node *irn = local != NULL ? local : n;
3217         node_t *node = get_irn_node(irn);
3218
3219         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
3220         return 1;
3221 #endif
3222 }
3223
3224 void combo(ir_graph *irg) {
3225         environment_t env;
3226         ir_node       *initial_bl;
3227         node_t        *start;
3228         ir_graph      *rem = current_ir_graph;
3229
3230         current_ir_graph = irg;
3231
3232         /* register a debug mask */
3233         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3234
3235         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3236
3237         obstack_init(&env.obst);
3238         env.worklist       = NULL;
3239         env.cprop          = NULL;
3240         env.touched        = NULL;
3241         env.initial        = NULL;
3242 #ifdef DEBUG_libfirm
3243         env.dbg_list       = NULL;
3244 #endif
3245         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
3246         env.type2id_map    = pmap_create();
3247         env.end_idx        = get_opt_global_cse() ? 0 : -1;
3248         env.lambda_input   = 0;
3249         env.nonstd_cond    = 0;
3250         env.commutative    = 1;
3251         env.modified       = 0;
3252
3253         assure_irg_outs(irg);
3254         assure_cf_loop(irg);
3255
3256         /* we have our own value_of function */
3257         set_value_of_func(get_node_tarval);
3258
3259         set_compute_functions();
3260         DEBUG_ONLY(part_nr = 0);
3261
3262         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
3263
3264         /* create the initial partition and place it on the work list */
3265         env.initial = new_partition(&env);
3266         add_to_worklist(env.initial, &env);
3267         irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
3268
3269 #ifdef WITH_UNKNOWN
3270         tarval_UNKNOWN = env.nonstd_cond ? tarval_bad : tarval_top;
3271 #else
3272         tarval_UNKNOWN = tarval_bad;
3273 #endif
3274
3275         /* all nodes on the initial partition have type Top */
3276         env.initial->type_is_T_or_C = 1;
3277
3278         /* Place the START Node's partition on cprop.
3279            Place the START Node on its local worklist. */
3280         initial_bl = get_irg_start_block(irg);
3281         start      = get_irn_node(initial_bl);
3282         add_to_cprop(start, &env);
3283
3284         do {
3285                 propagate(&env);
3286                 if (env.worklist != NULL)
3287                         cause_splits(&env);
3288         } while (env.cprop != NULL || env.worklist != NULL);
3289
3290         dump_all_partitions(&env);
3291         check_all_partitions(&env);
3292
3293 #if 0
3294         set_dump_node_vcgattr_hook(dump_partition_hook);
3295         dump_ir_block_graph(irg, "-partition");
3296         set_dump_node_vcgattr_hook(NULL);
3297 #else
3298         (void)dump_partition_hook;
3299 #endif
3300
3301         /* apply the result */
3302         irg_block_walk_graph(irg, NULL, apply_cf, &env);
3303         irg_walk_graph(irg, NULL, apply_result, &env);
3304         apply_end(get_irg_end(irg), &env);
3305
3306         if (env.modified) {
3307                 /* control flow might changed */
3308                 set_irg_outs_inconsistent(irg);
3309                 set_irg_extblk_inconsistent(irg);
3310                 set_irg_doms_inconsistent(irg);
3311                 set_irg_loopinfo_inconsistent(irg);
3312         }
3313
3314         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
3315
3316         pmap_destroy(env.type2id_map);
3317         del_set(env.opcode2id_map);
3318         obstack_free(&env.obst, NULL);
3319
3320         /* restore value_of() default behavior */
3321         set_value_of_func(NULL);
3322         current_ir_graph = rem;
3323 }  /* combo */