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