icc doesn't like -fomit-frame-pointer without -O
[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        is_flagged:1;   /**< Set, if this node was visited by the race. */
132         unsigned        by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
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->is_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->is_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->is_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 } step_env;
823
824 /**
825  * Return non-zero, if a input is a real follower
826  *
827  * @param irn    the node to check
828  * @param input  number of the input
829  */
830 static int is_real_follower(const ir_node *irn, int input) {
831         if (input == 1 && is_Confirm(irn)) {
832                 /* return the Confirm bound input */
833                 return 0;
834         }
835         if (input == 0 && is_Mux(irn)) {
836                 /* ignore the Mux sel input */
837         }
838         return 1;
839 }
840
841 /**
842  * Do one step in the race.
843  */
844 static int step(step_env *env) {
845         node_t *n;
846
847         if (env->initial != NULL) {
848                 /* Move node from initial to unwalked */
849                 n             = env->initial;
850                 env->initial  = n->race_next;
851
852                 n->race_next  = env->unwalked;
853                 env->unwalked = n;
854
855                 return 0;
856         }
857
858         while (env->unwalked != NULL) {
859                 /* let n be the first node in unwalked */
860                 n = env->unwalked;
861                 while (env->index < n->n_followers) {
862                         const ir_def_use_edge *edge = &n->node->out[1 + env->index];
863
864                         /* let m be n.F.def_use[index] */
865                         node_t *m = get_irn_node(edge->use);
866
867                         assert(m->is_follower);
868                         /*
869                          * Some inputs, like the get_Confirm_bound are NOT
870                          * real followers, sort them out.
871                          */
872                         if (! is_real_follower(m->node, edge->pos)) {
873                                 ++env->index;
874                                 continue;
875                         }
876                         ++env->index;
877
878                         /* only followers from our partition */
879                         if (m->part != n->part)
880                                 continue;
881
882                         if (m->is_flagged == 0) {
883                                 m->is_flagged = 1;
884
885                                 /* visited the first time */
886                                 /* add m to unwalked not as first node (we might still need to
887                                    check for more follower node */
888                                 m->race_next = n->race_next;
889                                 n->race_next = m;
890                                 return 0;
891                         }
892                 }
893                 /* move n to walked */
894                 env->unwalked = n->race_next;
895                 n->race_next  = env->walked;
896                 env->walked   = n;
897                 env->index    = 0;
898         }
899         return 1;
900 }  /* step */
901
902 /**
903  * Clear the flags from a list and check for
904  * nodes that where touched from both sides.
905  *
906  * @param list  the list
907  */
908 static void clear_flags(node_t *list) {
909         node_t *n;
910
911         for (n = list; n != NULL; n = n->race_next)
912                 n->is_flagged = 0;
913 }  /* clear_flags */
914
915 /**
916  * Split a partition by a local list using the race.
917  *
918  * @param pX   pointer to the partition to split, might be changed!
919  * @param gg   a (non-empty) node list
920  * @param env  the environment
921  *
922  * @return  a new partition containing the nodes of gg
923  */
924 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
925         partition_t *X = *pX;
926         partition_t *X_prime;
927         list_head   tmp;
928         step_env    env1, env2, *winner;
929         node_t      *g, *h, *node, *t;
930         int         max_input;
931         unsigned    n;
932         DEBUG_ONLY(static int run = 0;)
933
934         DB((dbg, LEVEL_2, "Run %d ", run++));
935         if (list_empty(&X->Follower)) {
936                 /* if the partition has NO follower, we can use the fast
937                    splitting algorithm. */
938                 return split_no_followers(X, gg, env);
939         }
940         /* else do the race */
941
942         dump_partition("Splitting ", X);
943         dump_list("by list ", gg);
944
945         INIT_LIST_HEAD(&tmp);
946
947         /* Remove gg from X.Leader and put into g */
948         g = NULL;
949         for (node = gg; node != NULL; node = node->next) {
950                 assert(node->part == X);
951                 assert(node->is_follower == 0);
952
953                 list_del(&node->node_list);
954                 list_add_tail(&node->node_list, &tmp);
955                 node->race_next = g;
956                 g               = node;
957         }
958         /* produce h */
959         h = NULL;
960         list_for_each_entry(node_t, node, &X->Leader, node_list) {
961                 node->race_next = h;
962                 h               = node;
963         }
964         /* restore X.Leader */
965         list_splice(&tmp, &X->Leader);
966
967         env1.initial       = g;
968         env1.unwalked      = NULL;
969         env1.walked        = NULL;
970         env1.index         = 0;
971
972         env2.initial       = h;
973         env2.unwalked      = NULL;
974         env2.walked        = NULL;
975         env2.index         = 0;
976
977         for (;;) {
978                 if (step(&env1)) {
979                         winner = &env1;
980                         break;
981                 }
982                 if (step(&env2)) {
983                         winner = &env2;
984                         break;
985                 }
986         }
987         assert(winner->initial == NULL);
988         assert(winner->unwalked == NULL);
989
990         /* clear flags from walked/unwalked */
991         clear_flags(env1.unwalked);
992         clear_flags(env1.walked);
993         clear_flags(env2.unwalked);
994         clear_flags(env2.walked);
995
996         dump_race_list("winner ", winner->walked);
997
998         /* Move walked_{winner} to a new partition, X'. */
999         X_prime   = new_partition(env);
1000         max_input = 0;
1001         n         = 0;
1002         for (node = winner->walked; node != NULL; node = node->race_next) {
1003                 list_del(&node->node_list);
1004                 node->part = X_prime;
1005                 if (node->is_follower) {
1006                         list_add_tail(&node->node_list, &X_prime->Follower);
1007                 } else {
1008                         list_add_tail(&node->node_list, &X_prime->Leader);
1009                         ++n;
1010                 }
1011                 if (node->max_user_input > max_input)
1012                         max_input = node->max_user_input;
1013         }
1014         X_prime->n_leader        = n;
1015         X_prime->max_user_inputs = max_input;
1016         X->n_leader             -= X_prime->n_leader;
1017
1018         /* for now, copy the type info tag, it will be adjusted in split_by(). */
1019         X_prime->type_is_T_or_C = X->type_is_T_or_C;
1020
1021         /*
1022          * Even if a follower was not checked by both sides, it might have
1023          * loose its congruence, so we need to check this case for all follower.
1024          */
1025         list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1026                 if (identity(node) == node)
1027                         follower_to_leader(node);
1028         }
1029
1030         check_partition(X);
1031         check_partition(X_prime);
1032
1033         /* X' is the smaller part */
1034         add_to_worklist(X_prime, env);
1035
1036         dump_partition("Now ", X);
1037         dump_partition("Created new ", X_prime);
1038
1039         /* we have to ensure that the partition containing g is returned */
1040         if (winner == &env2) {
1041                 *pX = X_prime;
1042                 return X;
1043         }
1044
1045         return X_prime;
1046 }  /* split */
1047 #endif /* NO_FOLLOWER */
1048
1049 /**
1050  * Returns non-zero if the i'th input of a Phi node is live.
1051  *
1052  * @param phi  a Phi-node
1053  * @param i    an input number
1054  *
1055  * @return non-zero if the i'th input of the given Phi node is live
1056  */
1057 static int is_live_input(ir_node *phi, int i) {
1058         if (i >= 0) {
1059                 ir_node        *block = get_nodes_block(phi);
1060                 ir_node        *pred  = get_Block_cfgpred(block, i);
1061                 lattice_elem_t type   = get_node_type(pred);
1062
1063                 return type.tv != tarval_unreachable;
1064         }
1065         /* else it's the control input, always live */
1066         return 1;
1067 }  /* is_live_input */
1068
1069 /**
1070  * Return non-zero if a type is a constant.
1071  */
1072 static int is_constant_type(lattice_elem_t type) {
1073         if (type.tv != tarval_bottom && type.tv != tarval_top)
1074                 return 1;
1075         return 0;
1076 }  /* is_constant_type */
1077
1078 /**
1079  * Check whether a type is neither Top or a constant.
1080  * Note: U is handled like Top here, R is a constant.
1081  *
1082  * @param type  the type to check
1083  */
1084 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1085         if (is_tarval(type.tv)) {
1086                 if (type.tv == tarval_top)
1087                         return 0;
1088                 if (tarval_is_constant(type.tv))
1089                         return 0;
1090         } else {
1091                 /* is a symconst */
1092                 return 0;
1093         }
1094         return 1;
1095 }
1096
1097 /**
1098  * Collect nodes to the touched list.
1099  *
1100  * @param list  the list which contains the nodes that must be evaluated
1101  * @param idx   the index of the def_use edge to evaluate
1102  * @param env   the environment
1103  */
1104 static void collect_touched(list_head *list, int idx, environment_t *env) {
1105         node_t  *x, *y;
1106         int     end_idx = env->end_idx;
1107
1108         list_for_each_entry(node_t, x, list, node_list) {
1109                 int num_edges;
1110
1111                 if (idx == -1) {
1112                         /* leader edges start AFTER follower edges */
1113                         x->next_edge = x->n_followers + 1;
1114                 }
1115                 num_edges = get_irn_n_outs(x->node);
1116
1117                 /* for all edges in x.L.def_use_{idx} */
1118                 while (x->next_edge <= num_edges) {
1119                         const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1120                         ir_node               *succ;
1121
1122                         /* check if we have necessary edges */
1123                         if (edge->pos > idx)
1124                                 break;
1125
1126                         ++x->next_edge;
1127
1128                         succ = edge->use;
1129
1130                         /* ignore the "control input" for non-pinned nodes
1131                         if we are running in GCSE mode */
1132                         if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1133                                 continue;
1134
1135                         y = get_irn_node(succ);
1136                         assert(get_irn_n(succ, idx) == x->node);
1137
1138                         /* ignore block edges touching followers */
1139                         if (idx == -1 && y->is_follower)
1140                                 continue;
1141
1142                         if (is_constant_type(y->type)) {
1143                                 ir_opcode code = get_irn_opcode(succ);
1144                                 if (code == iro_Sub || code == iro_Cmp)
1145                                         add_to_cprop(y, env);
1146                         }
1147
1148                         /* Partitions of constants should not be split simply because their Nodes have unequal
1149                            functions or incongruent inputs. */
1150                         if (type_is_neither_top_nor_const(y->type) &&
1151                                 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1152                                         add_to_touched(y, env);
1153                         }
1154                 }
1155         }
1156 }  /* collect_touched */
1157
1158 /**
1159  * Split the partitions if caused by the first entry on the worklist.
1160  *
1161  * @param env  the environment
1162  */
1163 static void cause_splits(environment_t *env) {
1164         partition_t *X, *Z, *N;
1165         int         idx;
1166
1167         /* remove the first partition from the worklist */
1168         X = env->worklist;
1169         env->worklist  = X->wl_next;
1170         X->on_worklist = 0;
1171
1172         dump_partition("Cause_split: ", X);
1173
1174         /* combine temporary leader and follower list */
1175         for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1176                 /* empty the touched set: already done, just clear the list */
1177                 env->touched = NULL;
1178
1179                 collect_touched(&X->Leader, idx, env);
1180                 collect_touched(&X->Follower, idx, env);
1181
1182                 for (Z = env->touched; Z != NULL; Z = N) {
1183                         node_t   *e;
1184                         node_t   *touched  = Z->touched;
1185                         unsigned n_touched = Z->n_touched;
1186
1187                         assert(Z->touched != NULL);
1188
1189                         /* beware, split might change Z */
1190                         N = Z->touched_next;
1191
1192                         /* remove it from the touched set */
1193                         Z->on_touched = 0;
1194
1195                         /* Empty local Z.touched. */
1196                         for (e = touched; e != NULL; e = e->next) {
1197                                 assert(e->is_follower == 0);
1198                                 e->on_touched = 0;
1199                         }
1200                         Z->touched   = NULL;
1201                         Z->n_touched = 0;
1202
1203                         if (0 < n_touched && n_touched < Z->n_leader) {
1204                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1205                                 split(&Z, touched, env);
1206                         } else
1207                                 assert(n_touched <= Z->n_leader);
1208                 }
1209         }
1210 }  /* cause_splits */
1211
1212 /**
1213  * Implements split_by_what(): Split a partition by characteristics given
1214  * by the what function.
1215  *
1216  * @param X     the partition to split
1217  * @param What  a function returning an Id for every node of the partition X
1218  * @param P     a list to store the result partitions
1219  * @param env   the environment
1220  *
1221  * @return *P
1222  */
1223 static partition_t *split_by_what(partition_t *X, what_func What,
1224                                   partition_t **P, environment_t *env) {
1225         node_t          *x, *S;
1226         listmap_t       map;
1227         listmap_entry_t *iter;
1228         partition_t     *R;
1229
1230         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1231         listmap_init(&map);
1232         list_for_each_entry(node_t, x, &X->Leader, node_list) {
1233                 void            *id = What(x, env);
1234                 listmap_entry_t *entry;
1235
1236                 if (id == NULL) {
1237                         /* input not allowed, ignore */
1238                         continue;
1239                 }
1240                 /* Add x to map[What(x)]. */
1241                 entry = listmap_find(&map, id);
1242                 x->next     = entry->list;
1243                 entry->list = x;
1244         }
1245         /* Let P be a set of Partitions. */
1246
1247         /* for all sets S except one in the range of map do */
1248         for (iter = map.values; iter != NULL; iter = iter->next) {
1249                 if (iter->next == NULL) {
1250                         /* this is the last entry, ignore */
1251                         break;
1252                 }
1253                 S = iter->list;
1254
1255                 /* Add SPLIT( X, S ) to P. */
1256                 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1257                 R = split(&X, S, env);
1258                 R->split_next = *P;
1259                 *P            = R;
1260         }
1261         /* Add X to P. */
1262         X->split_next = *P;
1263         *P            = X;
1264
1265         listmap_term(&map);
1266         return *P;
1267 }  /* split_by_what */
1268
1269 /** lambda n.(n.type) */
1270 static void *lambda_type(const node_t *node, environment_t *env) {
1271         (void)env;
1272         return node->type.tv;
1273 }  /* lambda_type */
1274
1275 /** lambda n.(n.opcode) */
1276 static void *lambda_opcode(const node_t *node, environment_t *env) {
1277         opcode_key_t key, *entry;
1278         ir_node      *irn = node->node;
1279
1280         key.code   = get_irn_opcode(irn);
1281         key.mode   = get_irn_mode(irn);
1282         key.arity  = get_irn_arity(irn);
1283         key.u.proj = 0;
1284         key.u.ent  = NULL;
1285
1286         switch (get_irn_opcode(irn)) {
1287         case iro_Proj:
1288                 key.u.proj = get_Proj_proj(irn);
1289                 break;
1290         case iro_Sel:
1291                 key.u.ent = get_Sel_entity(irn);
1292                 break;
1293         default:
1294                 break;
1295         }
1296
1297         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1298         return entry;
1299 }  /* lambda_opcode */
1300
1301 /** lambda n.(n[i].partition) */
1302 static void *lambda_partition(const node_t *node, environment_t *env) {
1303         ir_node *skipped = skip_Proj(node->node);
1304         ir_node *pred;
1305         node_t  *p;
1306         int     i = env->lambda_input;
1307
1308         if (i >= get_irn_arity(node->node)) {
1309                 /* we are outside the allowed range */
1310                 return NULL;
1311         }
1312
1313         /* ignore the "control input" for non-pinned nodes
1314            if we are running in GCSE mode */
1315         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1316                 return NULL;
1317
1318         pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1319         p    = get_irn_node(pred);
1320
1321         return p->part;
1322 }  /* lambda_partition */
1323
1324 /**
1325  * Returns true if a type is a constant.
1326  */
1327 static int is_con(const lattice_elem_t type) {
1328         /* be conservative */
1329         if (is_tarval(type.tv))
1330                 return tarval_is_constant(type.tv);
1331         return is_entity(type.sym.entity_p);
1332 }  /* is_con */
1333
1334 /**
1335  * Implements split_by().
1336  *
1337  * @param X    the partition to split
1338  * @param env  the environment
1339  */
1340 static void split_by(partition_t *X, environment_t *env) {
1341         partition_t *I, *P = NULL;
1342         int         input;
1343
1344         dump_partition("split_by", X);
1345
1346         if (X->n_leader == 1) {
1347                 /* we have only one leader, no need to split, just check it's type */
1348                 node_t *x = get_first_node(X);
1349                 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1350                 return;
1351         }
1352
1353         DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1354         P = split_by_what(X, lambda_type, &P, env);
1355
1356         /* adjust the type tags, we have split partitions by type */
1357         for (I = P; I != NULL; I = I->split_next) {
1358                 node_t *x = get_first_node(I);
1359                 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1360         }
1361
1362         do {
1363                 partition_t *Y = P;
1364
1365                 P = P->split_next;
1366                 if (Y->n_leader > 1) {
1367                         /* we do not want split the TOP or constant partitions */
1368                         if (! Y->type_is_T_or_C) {
1369                                 partition_t *Q = NULL;
1370
1371                                 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1372                                 Q = split_by_what(Y, lambda_opcode, &Q, env);
1373
1374                                 do {
1375                                         partition_t *Z = Q;
1376
1377                                         Q = Q->split_next;
1378                                         if (Z->n_leader > 1) {
1379                                                 const node_t *first = get_first_node(Z);
1380                                                 int          arity  = get_irn_arity(first->node);
1381                                                 partition_t  *R, *S;
1382
1383                                                 /*
1384                                                  * BEWARE: during splitting by input 2 for instance we might
1385                                                  * create new partitions which are different by input 1, so collect
1386                                                  * them and split further.
1387                                                  */
1388                                                 Z->split_next = NULL;
1389                                                 R             = Z;
1390                                                 S             = NULL;
1391                                                 for (input = arity - 1; input >= -1; --input) {
1392                                                         do {
1393                                                                 partition_t *Z_prime = R;
1394
1395                                                                 R = R->split_next;
1396                                                                 if (Z_prime->n_leader > 1) {
1397                                                                         env->lambda_input = input;
1398                                                                         DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1399                                                                         S = split_by_what(Z_prime, lambda_partition, &S, env);
1400                                                                 } else {
1401                                                                         Z_prime->split_next = S;
1402                                                                         S                   = Z_prime;
1403                                                                 }
1404                                                         } while (R != NULL);
1405                                                         R = S;
1406                                                         S = NULL;
1407                                                 }
1408                                         }
1409                                 } while (Q != NULL);
1410                         }
1411                 }
1412         } while (P != NULL);
1413 }  /* split_by */
1414
1415 /**
1416  * (Re-)compute the type for a given node.
1417  *
1418  * @param node  the node
1419  */
1420 static void default_compute(node_t *node) {
1421         int     i;
1422         ir_node *irn = node->node;
1423         node_t  *block = get_irn_node(get_nodes_block(irn));
1424
1425         if (block->type.tv == tarval_unreachable) {
1426                 node->type.tv = tarval_top;
1427                 return;
1428         }
1429
1430         /* if any of the data inputs have type top, the result is type top */
1431         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1432                 ir_node *pred = get_irn_n(irn, i);
1433                 node_t  *p    = get_irn_node(pred);
1434
1435                 if (p->type.tv == tarval_top) {
1436                         node->type.tv = tarval_top;
1437                         return;
1438                 }
1439         }
1440
1441         if (get_irn_mode(node->node) == mode_X)
1442                 node->type.tv = tarval_reachable;
1443         else
1444                 node->type.tv = computed_value(irn);
1445 }  /* default_compute */
1446
1447 /**
1448  * (Re-)compute the type for a Block node.
1449  *
1450  * @param node  the node
1451  */
1452 static void compute_Block(node_t *node) {
1453         int     i;
1454         ir_node *block = node->node;
1455
1456         if (block == get_irg_start_block(current_ir_graph)) {
1457                 /* start block is always reachable */
1458                 node->type.tv = tarval_reachable;
1459                 return;
1460         }
1461
1462         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1463                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1464
1465                 if (pred->type.tv == tarval_reachable) {
1466                         /* A block is reachable, if at least of predecessor is reachable. */
1467                         node->type.tv = tarval_reachable;
1468                         return;
1469                 }
1470         }
1471         node->type.tv = tarval_top;
1472 }  /* compute_Block */
1473
1474 /**
1475  * (Re-)compute the type for a Bad node.
1476  *
1477  * @param node  the node
1478  */
1479 static void compute_Bad(node_t *node) {
1480         /* Bad nodes ALWAYS compute Top */
1481         node->type.tv = tarval_top;
1482 }  /* compute_Bad */
1483
1484 /**
1485  * (Re-)compute the type for an Unknown node.
1486  *
1487  * @param node  the node
1488  */
1489 static void compute_Unknown(node_t *node) {
1490         /* While Unknown nodes should compute Top this is dangerous:
1491          * a Top input to a Cond would lead to BOTH control flows unreachable.
1492          * While this is correct in the given semantics, it would destroy the Firm
1493          * graph.
1494          *
1495          * It would be safe to compute Top IF it can be assured, that only Cmp
1496          * nodes are inputs to Conds. We check that first.
1497          * This is the way Frontends typically build Firm, but some optimizations
1498          * (cond_eval for instance) might replace them by Phib's...
1499          *
1500          * For now, we compute bottom here.
1501          */
1502         node->type.tv = tarval_bottom;
1503 }  /* compute_Unknown */
1504
1505 /**
1506  * (Re-)compute the type for a Jmp node.
1507  *
1508  * @param node  the node
1509  */
1510 static void compute_Jmp(node_t *node) {
1511         node_t *block = get_irn_node(get_nodes_block(node->node));
1512
1513         node->type = block->type;
1514 }  /* compute_Jmp */
1515
1516 /**
1517  * (Re-)compute the type for the End node.
1518  *
1519  * @param node  the node
1520  */
1521 static void compute_End(node_t *node) {
1522         /* the End node is NOT dead of course */
1523         node->type.tv = tarval_reachable;
1524 }
1525
1526 /**
1527  * (Re-)compute the type for a SymConst node.
1528  *
1529  * @param node  the node
1530  */
1531 static void compute_SymConst(node_t *node) {
1532         ir_node *irn = node->node;
1533         node_t  *block = get_irn_node(get_nodes_block(irn));
1534
1535         if (block->type.tv == tarval_unreachable) {
1536                 node->type.tv = tarval_top;
1537                 return;
1538         }
1539         switch (get_SymConst_kind(irn)) {
1540         case symconst_addr_ent:
1541         /* case symconst_addr_name: cannot handle this yet */
1542                 node->type.sym = get_SymConst_symbol(irn);
1543                 break;
1544         default:
1545                 node->type.tv = computed_value(irn);
1546         }
1547 }  /* compute_SymConst */
1548
1549 /**
1550  * (Re-)compute the type for a Phi node.
1551  *
1552  * @param node  the node
1553  */
1554 static void compute_Phi(node_t *node) {
1555         int            i;
1556         ir_node        *phi = node->node;
1557         lattice_elem_t type;
1558
1559         /* if a Phi is in a unreachable block, its type is TOP */
1560         node_t *block = get_irn_node(get_nodes_block(phi));
1561
1562         if (block->type.tv == tarval_unreachable) {
1563                 node->type.tv = tarval_top;
1564                 return;
1565         }
1566
1567         /* Phi implements the Meet operation */
1568         type.tv = tarval_top;
1569         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1570                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
1571                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1572
1573                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1574                         /* ignore TOP inputs: We must check here for unreachable blocks,
1575                            because Firm constants live in the Start Block are NEVER Top.
1576                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1577                            comes from a unreachable input. */
1578                         continue;
1579                 }
1580                 if (pred->type.tv == tarval_bottom) {
1581                         node->type.tv = tarval_bottom;
1582                         return;
1583                 } else if (type.tv == tarval_top) {
1584                         /* first constant found */
1585                         type = pred->type;
1586                 } else if (type.tv != pred->type.tv) {
1587                         /* different constants or tarval_bottom */
1588                         node->type.tv = tarval_bottom;
1589                         return;
1590                 }
1591                 /* else nothing, constants are the same */
1592         }
1593         node->type = type;
1594 }  /* compute_Phi */
1595
1596 /**
1597  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1598  *
1599  * @param node  the node
1600  */
1601 static void compute_Add(node_t *node) {
1602         ir_node        *sub = node->node;
1603         node_t         *l   = get_irn_node(get_Add_left(sub));
1604         node_t         *r   = get_irn_node(get_Add_right(sub));
1605         lattice_elem_t a    = l->type;
1606         lattice_elem_t b    = r->type;
1607         ir_mode        *mode;
1608
1609         if (a.tv == tarval_top || b.tv == tarval_top) {
1610                 node->type.tv = tarval_top;
1611         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1612                 node->type.tv = tarval_bottom;
1613         } else {
1614                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1615                    must call tarval_add() first to handle this case! */
1616                 if (is_tarval(a.tv)) {
1617                         if (is_tarval(b.tv)) {
1618                                 node->type.tv = tarval_add(a.tv, b.tv);
1619                                 return;
1620                         }
1621                         mode = get_tarval_mode(a.tv);
1622                         if (a.tv == get_mode_null(mode)) {
1623                                 node->type = b;
1624                                 return;
1625                         }
1626                 } else if (is_tarval(b.tv)) {
1627                         mode = get_tarval_mode(b.tv);
1628                         if (b.tv == get_mode_null(mode)) {
1629                                 node->type = a;
1630                                 return;
1631                         }
1632                 }
1633                 node->type.tv = tarval_bottom;
1634         }
1635 }  /* compute_Add */
1636
1637 /**
1638  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1639  *
1640  * @param node  the node
1641  */
1642 static void compute_Sub(node_t *node) {
1643         ir_node        *sub = node->node;
1644         node_t         *l   = get_irn_node(get_Sub_left(sub));
1645         node_t         *r   = get_irn_node(get_Sub_right(sub));
1646         lattice_elem_t a    = l->type;
1647         lattice_elem_t b    = r->type;
1648         tarval         *tv;
1649
1650         if (a.tv == tarval_top || b.tv == tarval_top) {
1651                 node->type.tv = tarval_top;
1652         } else if (is_con(a) && is_con(b)) {
1653                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1654                         node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1655                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1656                         node->type = b;
1657                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1658                         node->type = a;
1659                 } else {
1660                         node->type.tv = tarval_bottom;
1661                 }
1662                 node->by_all_const = 1;
1663         } else if (r->part == l->part &&
1664                    (!mode_is_float(get_irn_mode(l->node)))) {
1665                 /*
1666                  * BEWARE: a - a is NOT always 0 for floating Point values, as
1667                  * NaN op NaN = NaN, so we must check this here.
1668                  */
1669                 ir_mode *mode = get_irn_mode(sub);
1670                 tv = get_mode_null(mode);
1671
1672                 /* if the node was ONCE evaluated by all constants, but now
1673                    this breakes AND we cat by partition a different result, switch to bottom.
1674                    This happens because initially all nodes are in the same partition ... */
1675                 if (node->by_all_const && node->type.tv != tv)
1676                         tv = tarval_bottom;
1677                 node->type.tv = tv;
1678         } else {
1679                 node->type.tv = tarval_bottom;
1680         }
1681 }  /* compute_Sub */
1682
1683 /**
1684  * (Re-)compute the type for Cmp.
1685  *
1686  * @param node  the node
1687  */
1688 static void compute_Cmp(node_t *node) {
1689         ir_node        *cmp  = node->node;
1690         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1691         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1692         lattice_elem_t a     = l->type;
1693         lattice_elem_t b     = r->type;
1694
1695         if (a.tv == tarval_top || b.tv == tarval_top) {
1696                 node->type.tv = tarval_top;
1697         } else if (is_con(a) && is_con(b)) {
1698                 /* both nodes are constants, we can probably do something */
1699                 node->type.tv = tarval_b_true;
1700         } else if (r->part == l->part) {
1701                 /* both nodes congruent, we can probably do something */
1702                 node->type.tv = tarval_b_true;
1703         } else {
1704                 node->type.tv = tarval_bottom;
1705         }
1706 }  /* compute_Proj_Cmp */
1707
1708 /**
1709  * (Re-)compute the type for a Proj(Cmp).
1710  *
1711  * @param node  the node
1712  * @param cond  the predecessor Cmp node
1713  */
1714 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1715         ir_node        *proj = node->node;
1716         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1717         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1718         lattice_elem_t a     = l->type;
1719         lattice_elem_t b     = r->type;
1720         pn_Cmp         pnc   = get_Proj_proj(proj);
1721         tarval         *tv;
1722
1723         if (a.tv == tarval_top || b.tv == tarval_top) {
1724                 node->type.tv = tarval_top;
1725         } else if (is_con(a) && is_con(b)) {
1726                 default_compute(node);
1727                 node->by_all_const = 1;
1728         } else if (r->part == l->part &&
1729                    (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1730                 /*
1731                  * BEWARE: a == a is NOT always True for floating Point values, as
1732                  * NaN != NaN is defined, so we must check this here.
1733                  */
1734                 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1735
1736                 /* if the node was ONCE evaluated by all constants, but now
1737                    this breakes AND we cat by partition a different result, switch to bottom.
1738                    This happens because initially all nodes are in the same partition ... */
1739                 if (node->by_all_const && node->type.tv != tv)
1740                         tv = tarval_bottom;
1741                 node->type.tv = tv;
1742         } else {
1743                 node->type.tv = tarval_bottom;
1744         }
1745 }  /* compute_Proj_Cmp */
1746
1747 /**
1748  * (Re-)compute the type for a Proj(Cond).
1749  *
1750  * @param node  the node
1751  * @param cond  the predecessor Cond node
1752  */
1753 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1754         ir_node *proj     = node->node;
1755         long    pnc       = get_Proj_proj(proj);
1756         ir_node *sel      = get_Cond_selector(cond);
1757         node_t  *selector = get_irn_node(sel);
1758
1759         if (get_irn_mode(sel) == mode_b) {
1760                 /* an IF */
1761                 if (pnc == pn_Cond_true) {
1762                         if (selector->type.tv == tarval_b_false) {
1763                                 node->type.tv = tarval_unreachable;
1764                         } else if (selector->type.tv == tarval_b_true) {
1765                                 node->type.tv = tarval_reachable;
1766                         } else if (selector->type.tv == tarval_bottom) {
1767                                 node->type.tv = tarval_reachable;
1768                         } else {
1769                                 assert(selector->type.tv == tarval_top);
1770                                 node->type.tv = tarval_unreachable;
1771                         }
1772                 } else {
1773                         assert(pnc == pn_Cond_false);
1774
1775                         if (selector->type.tv == tarval_b_false) {
1776                                 node->type.tv = tarval_reachable;
1777                         } else if (selector->type.tv == tarval_b_true) {
1778                                 node->type.tv = tarval_unreachable;
1779                         } else if (selector->type.tv == tarval_bottom) {
1780                                 node->type.tv = tarval_reachable;
1781                         } else {
1782                                 assert(selector->type.tv == tarval_top);
1783                                 node->type.tv = tarval_unreachable;
1784                         }
1785                 }
1786         } else {
1787                 /* an SWITCH */
1788                 if (selector->type.tv == tarval_bottom) {
1789                         node->type.tv = tarval_reachable;
1790                 } else if (selector->type.tv == tarval_top) {
1791                         node->type.tv = tarval_unreachable;
1792                 } else {
1793                         long value = get_tarval_long(selector->type.tv);
1794                         if (pnc == get_Cond_defaultProj(cond)) {
1795                                 /* default switch, have to check ALL other cases */
1796                                 int i;
1797
1798                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1799                                         ir_node *succ = get_irn_out(cond, i);
1800
1801                                         if (succ == proj)
1802                                                 continue;
1803                                         if (value == get_Proj_proj(succ)) {
1804                                                 /* we found a match, will NOT take the default case */
1805                                                 node->type.tv = tarval_unreachable;
1806                                                 return;
1807                                         }
1808                                 }
1809                                 /* all cases checked, no match, will take default case */
1810                                 node->type.tv = tarval_reachable;
1811                         } else {
1812                                 /* normal case */
1813                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1814                         }
1815                 }
1816         }
1817 }  /* compute_Proj_Cond */
1818
1819 /**
1820  * (Re-)compute the type for a Proj-Node.
1821  *
1822  * @param node  the node
1823  */
1824 static void compute_Proj(node_t *node) {
1825         ir_node *proj = node->node;
1826         ir_mode *mode = get_irn_mode(proj);
1827         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1828         ir_node *pred  = get_Proj_pred(proj);
1829
1830         if (block->type.tv == tarval_unreachable) {
1831                 /* a Proj in a unreachable Block stay Top */
1832                 node->type.tv = tarval_top;
1833                 return;
1834         }
1835         if (get_irn_node(pred)->type.tv == tarval_top) {
1836                 /* if the predecessor is Top, its Proj follow */
1837                 node->type.tv = tarval_top;
1838                 return;
1839         }
1840
1841         if (mode == mode_M) {
1842                 /* mode M is always bottom */
1843                 node->type.tv = tarval_bottom;
1844                 return;
1845         }
1846         if (mode != mode_X) {
1847                 if (is_Cmp(pred))
1848                         compute_Proj_Cmp(node, pred);
1849                 else
1850                         default_compute(node);
1851                 return;
1852         }
1853         /* handle mode_X nodes */
1854
1855         switch (get_irn_opcode(pred)) {
1856         case iro_Start:
1857                 /* the Proj_X from the Start is always reachable.
1858                    However this is already handled at the top. */
1859                 node->type.tv = tarval_reachable;
1860                 break;
1861         case iro_Cond:
1862                 compute_Proj_Cond(node, pred);
1863                 break;
1864         default:
1865                 default_compute(node);
1866         }
1867 }  /* compute_Proj */
1868
1869 /**
1870  * (Re-)compute the type for a Confirm.
1871  *
1872  * @param node  the node
1873  */
1874 static void compute_Confirm(node_t *node) {
1875         ir_node *confirm = node->node;
1876         node_t  *pred = get_irn_node(get_Confirm_value(confirm));
1877
1878         if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1879                 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1880
1881                 if (is_con(bound->type)) {
1882                         /* is equal to a constant */
1883                         node->type = bound->type;
1884                         return;
1885                 }
1886         }
1887         /* a Confirm is a copy OR a Const */
1888         node->type = pred->type;
1889 }  /* compute_Confirm */
1890
1891 /**
1892  * (Re-)compute the type for a Max.
1893  *
1894  * @param node  the node
1895  */
1896 static void compute_Max(node_t *node) {
1897         ir_node        *op   = node->node;
1898         node_t         *l    = get_irn_node(get_binop_left(op));
1899         node_t         *r    = get_irn_node(get_binop_right(op));
1900         lattice_elem_t a     = l->type;
1901         lattice_elem_t b     = r->type;
1902
1903         if (a.tv == tarval_top || b.tv == tarval_top) {
1904                 node->type.tv = tarval_top;
1905         } else if (is_con(a) && is_con(b)) {
1906                 /* both nodes are constants, we can probably do something */
1907                 if (a.tv == b.tv) {
1908                         /* this case handles symconsts as well */
1909                         node->type = a;
1910                 } else {
1911                         ir_mode *mode   = get_irn_mode(op);
1912                         tarval  *tv_min = get_mode_min(mode);
1913
1914                         if (a.tv == tv_min)
1915                                 node->type = b;
1916                         else if (b.tv == tv_min)
1917                                 node->type = a;
1918                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1919                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1920                                         node->type.tv = a.tv;
1921                                 else
1922                                         node->type.tv = b.tv;
1923                         } else {
1924                                 node->type.tv = tarval_bad;
1925                         }
1926                 }
1927         } else if (r->part == l->part) {
1928                 /* both nodes congruent, we can probably do something */
1929                 node->type = a;
1930         } else {
1931                 node->type.tv = tarval_bottom;
1932         }
1933 }  /* compute_Max */
1934
1935 /**
1936  * (Re-)compute the type for a Min.
1937  *
1938  * @param node  the node
1939  */
1940 static void compute_Min(node_t *node) {
1941         ir_node        *op   = node->node;
1942         node_t         *l    = get_irn_node(get_binop_left(op));
1943         node_t         *r    = get_irn_node(get_binop_right(op));
1944         lattice_elem_t a     = l->type;
1945         lattice_elem_t b     = r->type;
1946
1947         if (a.tv == tarval_top || b.tv == tarval_top) {
1948                 node->type.tv = tarval_top;
1949         } else if (is_con(a) && is_con(b)) {
1950                 /* both nodes are constants, we can probably do something */
1951                 if (a.tv == b.tv) {
1952                         /* this case handles symconsts as well */
1953                         node->type = a;
1954                 } else {
1955                         ir_mode *mode   = get_irn_mode(op);
1956                         tarval  *tv_max = get_mode_max(mode);
1957
1958                         if (a.tv == tv_max)
1959                                 node->type = b;
1960                         else if (b.tv == tv_max)
1961                                 node->type = a;
1962                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1963                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1964                                         node->type.tv = a.tv;
1965                                 else
1966                                         node->type.tv = b.tv;
1967                         } else {
1968                                 node->type.tv = tarval_bad;
1969                         }
1970                 }
1971         } else if (r->part == l->part) {
1972                 /* both nodes congruent, we can probably do something */
1973                 node->type = a;
1974         } else {
1975                 node->type.tv = tarval_bottom;
1976         }
1977 }  /* compute_Min */
1978
1979 /**
1980  * (Re-)compute the type for a given node.
1981  *
1982  * @param node  the node
1983  */
1984 static void compute(node_t *node) {
1985         compute_func func;
1986
1987         if (is_no_Block(node->node)) {
1988                 node_t *block = get_irn_node(get_nodes_block(node->node));
1989
1990                 if (block->type.tv == tarval_unreachable) {
1991                         node->type.tv = tarval_top;
1992                         return;
1993                 }
1994         }
1995
1996         func = (compute_func)node->node->op->ops.generic;
1997         if (func != NULL)
1998                 func(node);
1999 }  /* compute */
2000
2001 /*
2002  * Identity functions: Note that one might thing that identity() is just a
2003  * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2004  * here, because it expects that the identity node is one of the inputs, which is NOT
2005  * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2006  * So, we have our own implementation, which copies some parts of equivalent_node()
2007  */
2008
2009 /**
2010  * Calculates the Identity for Phi nodes
2011  */
2012 static node_t *identity_Phi(node_t *node) {
2013         ir_node *phi    = node->node;
2014         ir_node *block  = get_nodes_block(phi);
2015         node_t  *n_part = NULL;
2016         int     i;
2017
2018         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2019                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2020
2021                 if (pred_X->type.tv == tarval_reachable) {
2022                         node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2023
2024                         if (n_part == NULL)
2025                                 n_part = pred;
2026                         else if (n_part->part != pred->part) {
2027                                 /* incongruent inputs, not a follower */
2028                                 return node;
2029                         }
2030                 }
2031         }
2032         /* if n_part is NULL here, all inputs path are dead, the Phi computes
2033          * tarval_top, is in the TOP partition and should NOT being split! */
2034         assert(n_part != NULL);
2035         return n_part;
2036 }  /* identity_Phi */
2037
2038 /**
2039  * Calculates the Identity for commutative 0 neutral nodes.
2040  */
2041 static node_t *identity_comm_zero_binop(node_t *node) {
2042         ir_node *op   = node->node;
2043         node_t  *a    = get_irn_node(get_binop_left(op));
2044         node_t  *b    = get_irn_node(get_binop_right(op));
2045         ir_mode *mode = get_irn_mode(op);
2046         tarval  *zero;
2047
2048         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2049         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2050                 return node;
2051
2052         /* node: no input should be tarval_top, else the binop would be also
2053          * Top and not being split. */
2054         zero = get_mode_null(mode);
2055         if (a->type.tv == zero)
2056                 return b;
2057         if (b->type.tv == zero)
2058                 return a;
2059         return node;
2060 }  /* identity_comm_zero_binop */
2061
2062 #define identity_Add  identity_comm_zero_binop
2063 #define identity_Or   identity_comm_zero_binop
2064
2065 /**
2066  * Calculates the Identity for Mul nodes.
2067  */
2068 static node_t *identity_Mul(node_t *node) {
2069         ir_node *op   = node->node;
2070         node_t  *a    = get_irn_node(get_Mul_left(op));
2071         node_t  *b    = get_irn_node(get_Mul_right(op));
2072         ir_mode *mode = get_irn_mode(op);
2073         tarval  *one;
2074
2075         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2076         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2077                 return node;
2078
2079         /* node: no input should be tarval_top, else the binop would be also
2080          * Top and not being split. */
2081         one = get_mode_one(mode);
2082         if (a->type.tv == one)
2083                 return b;
2084         if (b->type.tv == one)
2085                 return a;
2086         return node;
2087 }  /* identity_Mul */
2088
2089 /**
2090  * Calculates the Identity for Sub nodes.
2091  */
2092 static node_t *identity_Sub(node_t *node) {
2093         ir_node *sub  = node->node;
2094         node_t  *b    = get_irn_node(get_Sub_right(sub));
2095         ir_mode *mode = get_irn_mode(sub);
2096
2097         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2098         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2099                 return node;
2100
2101         /* node: no input should be tarval_top, else the binop would be also
2102          * Top and not being split. */
2103         if (b->type.tv == get_mode_null(mode))
2104                 return get_irn_node(get_Sub_left(sub));
2105         return node;
2106 }  /* identity_Mul */
2107
2108 /**
2109  * Calculates the Identity for And nodes.
2110  */
2111 static node_t *identity_And(node_t *node) {
2112         ir_node *and = node->node;
2113         node_t  *a   = get_irn_node(get_And_left(and));
2114         node_t  *b   = get_irn_node(get_And_right(and));
2115         tarval  *neutral = get_mode_all_one(get_irn_mode(and));
2116
2117         /* node: no input should be tarval_top, else the And would be also
2118          * Top and not being split. */
2119         if (a->type.tv == neutral)
2120                 return b;
2121         if (b->type.tv == neutral)
2122                 return a;
2123         return node;
2124 }  /* identity_And */
2125
2126 /**
2127  * Calculates the Identity for Confirm nodes.
2128  */
2129 static node_t *identity_Confirm(node_t *node) {
2130         ir_node *confirm = node->node;
2131
2132         /* a Confirm is always a Copy */
2133         return get_irn_node(get_Confirm_value(confirm));
2134 }  /* identity_Confirm */
2135
2136 /**
2137  * Calculates the Identity for Mux nodes.
2138  */
2139 static node_t *identity_Mux(node_t *node) {
2140         ir_node *mux = node->node;
2141         node_t  *t   = get_irn_node(get_Mux_true(mux));
2142         node_t  *f   = get_irn_node(get_Mux_false(mux));
2143         node_t  *sel;
2144
2145         if (t->part == f->part)
2146                 return t;
2147
2148         /* for now, the 1-input identity is not supported */
2149 #if 0
2150         sel = get_irn_node(get_Mux_sel(mux));
2151
2152         /* Mux sel input is mode_b, so it is always a tarval */
2153         if (sel->type.tv == tarval_b_true)
2154                 return t;
2155         if (sel->type.tv == tarval_b_false)
2156                 return f;
2157 #endif
2158         return node;
2159 }  /* identity_Mux */
2160
2161 /**
2162  * Calculates the Identity for Min nodes.
2163  */
2164 static node_t *identity_Min(node_t *node) {
2165         ir_node *op   = node->node;
2166         node_t  *a    = get_irn_node(get_binop_left(op));
2167         node_t  *b    = get_irn_node(get_binop_right(op));
2168         ir_mode *mode = get_irn_mode(op);
2169         tarval  *tv_max;
2170
2171         if (a->part == b->part) {
2172                 /* leader of multiple predecessors */
2173                 return a;
2174         }
2175
2176         /* works even with NaN */
2177         tv_max = get_mode_max(mode);
2178         if (a->type.tv == tv_max)
2179                 return b;
2180         if (b->type.tv == tv_max)
2181                 return a;
2182         return node;
2183 }  /* identity_Min */
2184
2185 /**
2186  * Calculates the Identity for Max nodes.
2187  */
2188 static node_t *identity_Max(node_t *node) {
2189         ir_node *op   = node->node;
2190         node_t  *a    = get_irn_node(get_binop_left(op));
2191         node_t  *b    = get_irn_node(get_binop_right(op));
2192         ir_mode *mode = get_irn_mode(op);
2193         tarval  *tv_min;
2194
2195         if (a->part == b->part) {
2196                 /* leader of multiple predecessors */
2197                 return a;
2198         }
2199
2200         /* works even with NaN */
2201         tv_min = get_mode_min(mode);
2202         if (a->type.tv == tv_min)
2203                 return b;
2204         if (b->type.tv == tv_min)
2205                 return a;
2206         return node;
2207 }  /* identity_Max */
2208
2209 /**
2210  * Calculates the Identity for nodes.
2211  */
2212 static node_t *identity(node_t *node) {
2213         ir_node *irn = node->node;
2214
2215         switch (get_irn_opcode(irn)) {
2216         case iro_Phi:
2217                 return identity_Phi(node);
2218         case iro_Add:
2219                 return identity_Add(node);
2220         case iro_Mul:
2221                 return identity_Mul(node);
2222         case iro_Or:
2223                 return identity_Or(node);
2224         case iro_And:
2225                 return identity_And(node);
2226         case iro_Sub:
2227                 return identity_Sub(node);
2228         case iro_Confirm:
2229                 return identity_Confirm(node);
2230         case iro_Mux:
2231                 return identity_Mux(node);
2232         case iro_Min:
2233                 return identity_Min(node);
2234         case iro_Max:
2235                 return identity_Max(node);
2236         default:
2237                 return node;
2238         }
2239 }  /* identity */
2240
2241 /**
2242  * Node follower is a (new) follower of leader, segregate Leader
2243  * out edges.
2244  */
2245 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2246         ir_node *l   = leader->node;
2247         int     j, i, n = get_irn_n_outs(l);
2248
2249         DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2250         /* The leader edges must remain sorted, but follower edges can
2251            be unsorted. */
2252         for (i = leader->n_followers + 1; i <= n; ++i) {
2253                 if (l->out[i].use == follower) {
2254                         ir_def_use_edge t = l->out[i];
2255
2256                         for (j = i - 1; j >= leader->n_followers + 1; --j)
2257                                 l->out[j + 1] = l->out[j];
2258                         ++leader->n_followers;
2259                         l->out[leader->n_followers] = t;
2260                         break;
2261                 }
2262         }
2263 }  /* segregate_def_use_chain_1 */
2264
2265 /**
2266  * Node follower is a (new) follower of leader, segregate Leader
2267  * out edges. If follower is a n-congruent Input identity, all follower
2268  * inputs congruent to follower are also leader.
2269  *
2270  * @param follower  the follower IR node
2271  */
2272 static void segregate_def_use_chain(const ir_node *follower) {
2273         int i;
2274
2275         for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2276                 node_t *pred = get_irn_node(get_irn_n(follower, i));
2277
2278                 segregate_def_use_chain_1(follower, pred);
2279         }
2280 }  /* segregate_def_use_chain */
2281
2282 /**
2283  * Propagate constant evaluation.
2284  *
2285  * @param env  the environment
2286  */
2287 static void propagate(environment_t *env) {
2288         partition_t    *X, *Y;
2289         node_t         *x;
2290         lattice_elem_t old_type;
2291         node_t         *fallen;
2292         unsigned       n_fallen, old_type_was_T_or_C;
2293         int            i;
2294
2295         while (env->cprop != NULL) {
2296                 void *oldopcode = NULL;
2297
2298                 /* remove the first partition X from cprop */
2299                 X           = env->cprop;
2300                 X->on_cprop = 0;
2301                 env->cprop  = X->cprop_next;
2302
2303                 old_type_was_T_or_C = X->type_is_T_or_C;
2304
2305                 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2306                 fallen   = NULL;
2307                 n_fallen = 0;
2308                 while (! list_empty(&X->cprop)) {
2309                         /* remove the first Node x from X.cprop */
2310                         x = list_entry(X->cprop.next, node_t, cprop_list);
2311                         //assert(x->part == X);
2312                         list_del(&x->cprop_list);
2313                         x->on_cprop = 0;
2314
2315                         if (x->is_follower && identity(x) == x) {
2316                                 /* check the opcode first */
2317                                 if (oldopcode == NULL) {
2318                                         oldopcode = lambda_opcode(get_first_node(X), env);
2319                                 }
2320                                 if (oldopcode != lambda_opcode(x, env)) {
2321                                         if (x->on_fallen == 0) {
2322                                                 /* different opcode -> x falls out of this partition */
2323                                                 x->next      = fallen;
2324                                                 x->on_fallen = 1;
2325                                                 fallen       = x;
2326                                                 ++n_fallen;
2327                                                 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2328                                         }
2329                                 }
2330
2331                                 /* x will make the follower -> leader transition */
2332                                 follower_to_leader(x);
2333                         }
2334
2335                         /* compute a new type for x */
2336                         old_type = x->type;
2337                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2338                         compute(x);
2339                         if (x->type.tv != old_type.tv) {
2340                                 verify_type(old_type, x->type);
2341                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2342
2343                                 if (x->on_fallen == 0) {
2344                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2345                                            not already on the list. */
2346                                         x->next      = fallen;
2347                                         x->on_fallen = 1;
2348                                         fallen       = x;
2349                                         ++n_fallen;
2350                                         DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2351                                 }
2352                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2353                                         ir_node *succ = get_irn_out(x->node, i);
2354                                         node_t  *y    = get_irn_node(succ);
2355
2356                                         /* Add y to y.partition.cprop. */
2357                                         add_to_cprop(y, env);
2358                                 }
2359                         }
2360                 }
2361
2362                 if (n_fallen > 0 && n_fallen != X->n_leader) {
2363                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2364                         Y = split(&X, fallen, env);
2365                 } else {
2366                         Y = X;
2367                 }
2368                 /* remove the flags from the fallen list */
2369                 for (x = fallen; x != NULL; x = x->next)
2370                         x->on_fallen = 0;
2371
2372 #ifndef NO_FOLLOWER
2373                 if (old_type_was_T_or_C) {
2374                         node_t *y, *tmp;
2375
2376                         if (Y->on_worklist == 0)
2377                                 add_to_worklist(Y, env);
2378
2379                         /* check if some nodes will make the leader -> follower transition */
2380                         list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2381                                 if (!is_Phi(y->node) &&
2382                                     y->type.tv != tarval_top && ! is_con(y->type)) {
2383                                         node_t *eq_node = identity(y);
2384
2385                                         if (eq_node != y && eq_node->part == y->part) {
2386                                                 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2387                                                 /* move to Follower */
2388                                                 y->is_follower = 1;
2389                                                 list_del(&y->node_list);
2390                                                 list_add_tail(&y->node_list, &Y->Follower);
2391                                                 --Y->n_leader;
2392
2393                                                 segregate_def_use_chain(y->node);
2394                                         }
2395                                 }
2396                         }
2397                 }
2398 #endif
2399                 split_by(Y, env);
2400         }
2401 }  /* propagate */
2402
2403 /**
2404  * Get the leader for a given node from its congruence class.
2405  *
2406  * @param irn  the node
2407  */
2408 static ir_node *get_leader(node_t *node) {
2409         partition_t *part = node->part;
2410
2411         if (part->n_leader > 1 || node->is_follower) {
2412                 if (node->is_follower)
2413                         DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2414                 else
2415                         DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2416
2417                 return get_first_node(part)->node;
2418         }
2419         return node->node;
2420 }  /* get_leader */
2421
2422 /**
2423  * Return non-zero if the control flow predecessor node pred
2424  * is the only reachable control flow exit of its block.
2425  *
2426  * @param pred  the control flow exit
2427  */
2428 static int can_exchange(ir_node *pred) {
2429         if (is_Start(pred))
2430                 return 0;
2431         else if (is_Jmp(pred))
2432                 return 1;
2433         else if (get_irn_mode(pred) == mode_T) {
2434                 int i, k;
2435
2436                 /* if the predecessor block has more than one
2437                 reachable outputs we cannot remove the block */
2438                 k = 0;
2439                 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2440                         ir_node *proj = get_irn_out(pred, i);
2441                         node_t  *node;
2442
2443                         /* skip non-control flow Proj's */
2444                         if (get_irn_mode(proj) != mode_X)
2445                                 continue;
2446
2447                         node = get_irn_node(proj);
2448                         if (node->type.tv == tarval_reachable) {
2449                                 if (++k > 1)
2450                                         return 0;
2451                         }
2452                 }
2453                 return 1;
2454         }
2455         return 0;
2456 }
2457
2458 /**
2459  * Block Post-Walker, apply the analysis results on control flow by
2460  * shortening Phi's and Block inputs.
2461  */
2462 static void apply_cf(ir_node *block, void *ctx) {
2463         environment_t *env = ctx;
2464         node_t        *node = get_irn_node(block);
2465         int           i, j, k, n;
2466         ir_node       **ins, **in_X;
2467         ir_node       *phi, *next;
2468
2469         if (block == get_irg_end_block(current_ir_graph) ||
2470             block == get_irg_start_block(current_ir_graph)) {
2471                 /* the EndBlock is always reachable even if the analysis
2472                    finds out the opposite :-) */
2473                 return;
2474         }
2475         if (node->type.tv == tarval_unreachable) {
2476                 /* mark dead blocks */
2477                 set_Block_dead(block);
2478                 return;
2479         }
2480
2481         n = get_Block_n_cfgpreds(block);
2482
2483         if (n == 1) {
2484                 /* only one predecessor combine */
2485                 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2486
2487                 if (can_exchange(pred)) {
2488                         exchange(block, get_nodes_block(pred));
2489                         env->modified = 1;
2490                 }
2491                 return;
2492         }
2493
2494         NEW_ARR_A(ir_node *, in_X, n);
2495         k = 0;
2496         for (i = 0; i < n; ++i) {
2497                 ir_node *pred = get_Block_cfgpred(block, i);
2498                 node_t  *node = get_irn_node(pred);
2499
2500                 if (node->type.tv == tarval_reachable) {
2501                         in_X[k++] = pred;
2502                 }
2503         }
2504         if (k >= n)
2505                 return;
2506
2507         NEW_ARR_A(ir_node *, ins, n);
2508         for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2509                 node_t *node = get_irn_node(phi);
2510
2511                 next = get_Phi_next(phi);
2512                 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2513                         /* this Phi is replaced by a constant */
2514                         tarval  *tv = node->type.tv;
2515                         ir_node *c  = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2516
2517                         set_irn_node(c, node);
2518                         node->node = c;
2519                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2520                         exchange(phi, c);
2521                         env->modified = 1;
2522                 } else {
2523                         j = 0;
2524                         for (i = 0; i < n; ++i) {
2525                                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2526
2527                                 if (pred->type.tv == tarval_reachable) {
2528                                         ins[j++] = get_Phi_pred(phi, i);
2529                                 }
2530                         }
2531                         if (j <= 1) {
2532                                 /* this Phi is replaced by a single predecessor */
2533                                 ir_node *s = ins[0];
2534
2535                                 node->node = s;
2536                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2537                                 exchange(phi, s);
2538                                 env->modified = 1;
2539                         } else {
2540                                 set_irn_in(phi, j, ins);
2541                                 env->modified = 1;
2542                         }
2543                 }
2544         }
2545
2546         if (k <= 1) {
2547                 /* this Block has only one live predecessor */
2548                 ir_node *pred = skip_Proj(in_X[0]);
2549
2550                 if (can_exchange(pred)) {
2551                         exchange(block, get_nodes_block(pred));
2552                         env->modified = 1;
2553                 }
2554         } else {
2555                 set_irn_in(block, k, in_X);
2556                 env->modified = 1;
2557         }
2558 }
2559
2560 /**
2561  * Post-Walker, apply the analysis results;
2562  */
2563 static void apply_result(ir_node *irn, void *ctx) {
2564         environment_t *env = ctx;
2565         node_t        *node = get_irn_node(irn);
2566
2567         if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2568                 /* blocks already handled, do not touch the End node */
2569         } else {
2570                 node_t *block = get_irn_node(get_nodes_block(irn));
2571
2572                 if (block->type.tv == tarval_unreachable) {
2573                         ir_node *bad = get_irg_bad(current_ir_graph);
2574
2575                         /* here, bad might already have a node, but this can be safely ignored
2576                            as long as bad has at least ONE valid node */
2577                         set_irn_node(bad, node);
2578                         node->node = bad;
2579                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2580                         exchange(irn, bad);
2581                         env->modified = 1;
2582                 }
2583                 else if (node->type.tv == tarval_unreachable) {
2584                         ir_node *bad = get_irg_bad(current_ir_graph);
2585
2586                         /* see comment above */
2587                         set_irn_node(bad, node);
2588                         node->node = bad;
2589                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2590                         exchange(irn, bad);
2591                         env->modified = 1;
2592                 }
2593                 else if (get_irn_mode(irn) == mode_X) {
2594                         if (is_Proj(irn)) {
2595                                 /* leave or Jmp */
2596                                 ir_node *cond = get_Proj_pred(irn);
2597
2598                                 if (is_Cond(cond)) {
2599                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
2600
2601                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2602                                                 /* Cond selector is a constant, make a Jmp */
2603                                                 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2604                                                 set_irn_node(jmp, node);
2605                                                 node->node = jmp;
2606                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2607                                                 exchange(irn, jmp);
2608                                                 env->modified = 1;
2609                                         }
2610                                 }
2611                         }
2612                 } else {
2613                         /* normal data node */
2614                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2615                                 tarval *tv = node->type.tv;
2616
2617                                 /*
2618                                  * Beware: never replace mode_T nodes by constants. Currently we must mark
2619                                  * mode_T nodes with constants, but do NOT replace them.
2620                                  */
2621                                 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2622                                         /* can be replaced by a constant */
2623                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2624                                         set_irn_node(c, node);
2625                                         node->node = c;
2626                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2627                                         exchange(irn, c);
2628                                         env->modified = 1;
2629                                 }
2630                         } else if (is_entity(node->type.sym.entity_p)) {
2631                                 if (! is_SymConst(irn)) {
2632                                         /* can be replaced by a Symconst */
2633                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2634                                         set_irn_node(symc, node);
2635                                         node->node = symc;
2636
2637                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2638                                         exchange(irn, symc);
2639                                         env->modified = 1;
2640                                 }
2641                         } else if (is_Confirm(irn)) {
2642                                 /* Confirms are always follower, but do not kill them here */
2643                         } else {
2644                                 ir_node *leader = get_leader(node);
2645
2646                                 if (leader != irn) {
2647                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2648                                         exchange(irn, leader);
2649                                         env->modified = 1;
2650                                 }
2651                         }
2652                 }
2653         }
2654 }  /* apply_result */
2655
2656 /**
2657  * Fix the keep-alives by deleting unreachable ones.
2658  */
2659 static void apply_end(ir_node *end, environment_t *env) {
2660         int i, j,  n = get_End_n_keepalives(end);
2661         ir_node **in;
2662
2663         if (n > 0)
2664                 NEW_ARR_A(ir_node *, in, n);
2665
2666         /* fix the keep alive */
2667         for (i = j = 0; i < n; i++) {
2668                 ir_node *ka   = get_End_keepalive(end, i);
2669                 node_t  *node = get_irn_node(ka);
2670
2671                 /* Use the is_flagged bit to mark already visited nodes.
2672                  * This should not be ready but better safe than sorry. */
2673                 if (node->is_flagged == 0) {
2674                         node->is_flagged = 1;
2675
2676                         if (! is_Block(ka))
2677                                 node = get_irn_node(get_nodes_block(ka));
2678
2679                         if (node->type.tv != tarval_unreachable)
2680                                 in[j++] = ka;
2681                 }
2682         }
2683         if (j != n) {
2684                 set_End_keepalives(end, j, in);
2685                 env->modified = 1;
2686         }
2687 }  /* apply_end */
2688
2689 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2690
2691 /**
2692  * sets the generic functions to compute.
2693  */
2694 static void set_compute_functions(void) {
2695         int i;
2696
2697         /* set the default compute function */
2698         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2699                 ir_op *op = get_irp_opcode(i);
2700                 op->ops.generic = (op_func)default_compute;
2701         }
2702
2703         /* set specific functions */
2704         SET(Block);
2705         SET(Unknown);
2706         SET(Bad);
2707         SET(Jmp);
2708         SET(Phi);
2709         SET(Add);
2710         SET(Sub);
2711         SET(SymConst);
2712         SET(Cmp);
2713         SET(Proj);
2714         SET(Confirm);
2715         SET(End);
2716
2717         if (op_Max != NULL)
2718                 SET(Max);
2719         if (op_Min != NULL)
2720                 SET(Min);
2721
2722 }  /* set_compute_functions */
2723
2724 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2725         ir_node *irn = local != NULL ? local : n;
2726         node_t *node = get_irn_node(irn);
2727
2728         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2729         return 1;
2730 }
2731
2732 void combo(ir_graph *irg) {
2733         environment_t env;
2734         ir_node       *initial_bl;
2735         node_t        *start;
2736         ir_graph      *rem = current_ir_graph;
2737
2738         current_ir_graph = irg;
2739
2740         /* register a debug mask */
2741         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2742         //firm_dbg_set_mask(dbg, SET_LEVEL_3);
2743
2744         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2745
2746         obstack_init(&env.obst);
2747         env.worklist       = NULL;
2748         env.cprop          = NULL;
2749         env.touched        = NULL;
2750         env.initial        = NULL;
2751 #ifdef DEBUG_libfirm
2752         env.dbg_list       = NULL;
2753 #endif
2754         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
2755         env.type2id_map    = pmap_create();
2756         env.end_idx        = get_opt_global_cse() ? 0 : -1;
2757         env.lambda_input   = 0;
2758         env.modified       = 0;
2759
2760         assure_irg_outs(irg);
2761
2762         /* we have our own value_of function */
2763         set_value_of_func(get_node_tarval);
2764
2765         set_compute_functions();
2766         DEBUG_ONLY(part_nr = 0);
2767
2768         /* create the initial partition and place it on the work list */
2769         env.initial = new_partition(&env);
2770         add_to_worklist(env.initial, &env);
2771         irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2772
2773         /* all nodes on the initial partition have type Top */
2774         env.initial->type_is_T_or_C = 1;
2775
2776         /* Place the START Node's partition on cprop.
2777            Place the START Node on its local worklist. */
2778         initial_bl = get_irg_start_block(irg);
2779         start      = get_irn_node(initial_bl);
2780         add_to_cprop(start, &env);
2781
2782         do {
2783                 propagate(&env);
2784                 if (env.worklist != NULL)
2785                         cause_splits(&env);
2786         } while (env.cprop != NULL || env.worklist != NULL);
2787
2788         dump_all_partitions(&env);
2789
2790 #if 0
2791         set_dump_node_vcgattr_hook(dump_partition_hook);
2792         dump_ir_block_graph(irg, "-partition");
2793         set_dump_node_vcgattr_hook(NULL);
2794 #else
2795         (void)dump_partition_hook;
2796 #endif
2797
2798         /* apply the result */
2799         irg_block_walk_graph(irg, NULL, apply_cf, &env);
2800         irg_walk_graph(irg, NULL, apply_result, &env);
2801         apply_end(get_irg_end(irg), &env);
2802
2803         if (env.modified) {
2804                 /* control flow might changed */
2805                 set_irg_outs_inconsistent(irg);
2806                 set_irg_extblk_inconsistent(irg);
2807                 set_irg_doms_inconsistent(irg);
2808                 set_irg_loopinfo_inconsistent(irg);
2809         }
2810
2811         pmap_destroy(env.type2id_map);
2812         del_set(env.opcode2id_map);
2813         obstack_free(&env.obst, NULL);
2814
2815         /* restore value_of() default behavior */
2816         set_value_of_func(NULL);
2817         current_ir_graph = rem;
2818 }  /* combo */