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