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