ce3d34492e990f755a73ba64d49bb36751d29703
[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 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 "irflag.h"
38 #include "ircons.h"
39 #include "list.h"
40 #include "array.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 typedef struct node_t            node_t;
63 typedef struct partition_t       partition_t;
64 typedef struct opcode_key_t      opcode_key_t;
65 typedef struct listmap_entry_t   listmap_entry_t;
66
67 /** The type of the compute function. */
68 typedef void (*compute_func)(node_t *node);
69
70 /**
71  * An opcode map key.
72  */
73 struct opcode_key_t {
74         ir_opcode   code;   /**< The Firm opcode. */
75         ir_mode     *mode;  /**< The mode of all nodes in the partition. */
76         union {
77                 long      proj;   /**< For Proj nodes, its proj number */
78                 ir_entity *ent;   /**< For Sel Nodes, its entity */
79         } u;
80 };
81
82 /**
83  * An entry in the list_map.
84  */
85 struct listmap_entry_t {
86         void            *id;    /**< The id. */
87         node_t          *list;  /**< The associated list for this id. */
88         listmap_entry_t *next;  /**< Link to the next entry in the map. */
89 };
90
91 /** We must map id's to lists. */
92 typedef struct listmap_t {
93         set             *map;    /**< Map id's to listmap_entry_t's */
94         listmap_entry_t *values; /**< List of all values in the map. */
95 } listmap_t;
96
97 /**
98  * A lattice element. Because we handle constants and symbolic constants different, we
99  * have to use this union.
100  */
101 typedef union {
102         tarval          *tv;
103         symconst_symbol sym;
104 } lattice_elem_t;
105
106 /**
107  * A node.
108  */
109 struct node_t {
110         ir_node         *node;          /**< The IR-node itself. */
111         list_head       node_list;      /**< Double-linked list of entries. */
112         list_head       cprop_list;     /**< Double-linked partition.cprop list. */
113         partition_t     *part;          /**< points to the partition this node belongs to */
114         node_t          *next;          /**< Next node on local list (partition.touched, fallen). */
115         lattice_elem_t  type;           /**< The associated lattice element "type". */
116         int             max_user_input; /**< Maximum input number of Def-Use edges. */
117         int             next_edge;      /**< Index of the next Def-Use edge to use. */
118         unsigned        on_touched:1;   /**< Set, if this node is on the partition.touched set. */
119         unsigned        on_cprop:1;     /**< Set, if this node is on the partition.cprop list. */
120         unsigned        on_fallen:1;    /**< Set, if this node is on the fallen list. */
121 };
122
123 /**
124  * A partition containing congruent nodes.
125  */
126 struct partition_t {
127         list_head         entries;         /**< The head of partition node list. */
128         list_head         cprop;           /**< The head of partition.cprop list. */
129         partition_t       *wl_next;        /**< Next entry in the work list if any. */
130         partition_t       *touched_next;   /**< Points to the next partition in the touched set. */
131         partition_t       *cprop_next;     /**< Points to the next partition in the cprop list. */
132         node_t            *touched;        /**< The partition.touched set of this partition. */
133         unsigned          n_nodes;         /**< Number of entries in this partition. */
134         unsigned          n_touched;       /**< Number of entries in the partition.touched. */
135         int               max_arity;       /**< Maximum arity of all entries. */
136         int               max_user_inputs; /**< Maximum number of user inputs of all entries. */
137         unsigned          on_worklist:1;   /**< Set, if this partition is in the work list. */
138         unsigned          on_touched:1;    /**< Set, if this partition is on the touched set. */
139         unsigned          on_cprop:1;      /**< Set, if this partition is on the cprop list. */
140 #ifdef DEBUG_libfirm
141         partition_t       *dbg_next;       /**< Link all partitions for debugging */
142         unsigned          nr;              /**< A unique number for (what-)mapping, >0. */
143 #endif
144 };
145
146 typedef struct environment_t {
147         struct obstack  obst;           /**< obstack to allocate data structures. */
148         partition_t     *worklist;      /**< The work list. */
149         partition_t     *cprop;         /**< The constant propagation list. */
150         partition_t     *touched;       /**< the touched set. */
151         partition_t     *initial;       /**< The initial partition. */
152         set             *opcode2id_map; /**< The opcodeMode->id map. */
153         pmap            *type2id_map;   /**< The type->id map. */
154         int             end_idx;        /**< -1 for local and 0 for global congruences. */
155         int             lambda_input;   /**< Captured argument for lambda_partition(). */
156 #ifdef DEBUG_libfirm
157         partition_t     *dbg_list;      /**< List of all partitions. */
158 #endif
159 } environment_t;
160
161 /** Type of the what function. */
162 typedef void *(*what_func)(const node_t *node, environment_t *env);
163
164 #define get_irn_node(irn)         ((node_t *)get_irn_link(irn))
165 #define set_irn_node(irn, node)   set_irn_link(irn, node)
166
167 /** The debug module handle. */
168 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
169
170 /** Next partition number. */
171 DEBUG_ONLY(static unsigned part_nr = 0);
172
173 #ifdef DEBUG_libfirm
174 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
175
176 /**
177  * Dump partition to output.
178  */
179 static void dump_partition(const char *msg, const partition_t *part) {
180         const node_t   *node;
181         int            first = 1;
182         lattice_elem_t type = get_partition_type(part);
183
184         DB((dbg, LEVEL_2, "%s part%u (%u, %+F) {\n  ", msg, part->nr, part->n_nodes, type));
185         list_for_each_entry(node_t, node, &part->entries, node_list) {
186                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
187                 first = 0;
188         }
189         DB((dbg, LEVEL_2, "\n}\n"));
190 }
191
192 /**
193  * Dump all partitions.
194  */
195 static void dump_all_partitions(const environment_t *env) {
196         const partition_t *P;
197
198         DB((dbg, LEVEL_2, "All partitions\n===============\n"));
199         for (P = env->dbg_list; P != NULL; P = P->dbg_next)
200                 dump_partition("", P);
201 }
202
203 #else
204 #define dump_partition(msg, part)
205 #define dump_all_partitions(env)
206 #endif
207
208 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
209 /**
210  * Verify that a type transition is monotone
211  */
212 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
213         if (old_type.tv == new_type.tv) {
214                 /* no change */
215                 return;
216         }
217         if (old_type.tv == tarval_top) {
218                 /* from Top down-to is always allowed */
219                 return;
220         }
221         if (old_type.tv == tarval_unreachable) {
222                 if (new_type.tv == tarval_reachable) {
223                         /* U -> R */
224                         return;
225                 }
226                 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
227         }
228         if (new_type.tv == tarval_bottom) {
229                 /* bottom reached */
230                 return;
231         }
232         panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
233 }
234 #else
235 #define verify_type(old_type, new_type)
236 #endif
237
238 /**
239  * Return the "top" value depending on the mode
240  */
241 static tarval *get_top_value(const ir_mode *mode) {
242         return (mode == mode_X || mode == mode_BB) ? tarval_unreachable : tarval_top;
243 }
244
245 /**
246  * Compare two pointer values of a listmap.
247  */
248 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
249         const listmap_entry_t *e1 = elt;
250         const listmap_entry_t *e2 = key;
251
252         (void) size;
253         return e1->id != e2->id;
254 }  /* listmap_cmp_ptr */
255
256 /**
257  * Initializes a listmap.
258  *
259  * @param map  the listmap
260  */
261 static void listmap_init(listmap_t *map) {
262         map->map    = new_set(listmap_cmp_ptr, 16);
263         map->values = NULL;
264 }  /* listmap_init */
265
266 /**
267  * Terminates a listmap.
268  *
269  * @param map  the listmap
270  */
271 static void listmap_term(listmap_t *map) {
272         del_set(map->map);
273 }  /* listmap_term */
274
275 /**
276  * Return the associated listmap entry for a given id.
277  *
278  * @param map  the listmap
279  * @param id   the id to search for
280  *
281  * @return the asociated listmap entry for the given id
282  */
283 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
284         listmap_entry_t key, *entry;
285
286         key.id   = id;
287         key.list = NULL;
288         key.next = NULL;
289         entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
290
291         if (entry->list == NULL) {
292                 /* a new entry, put into the list */
293                 entry->next = map->values;
294                 map->values = entry;
295         }
296         return entry;
297 }  /* listmap_find */
298
299 /**
300  * Calculate the hash value for an opcode map entry.
301  *
302  * @param entry  an opcode map entry
303  *
304  * @return a hash value for the given opcode map entry
305  */
306 static unsigned opcode_hash(const opcode_key_t *entry) {
307         return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
308 }  /* opcode_hash */
309
310 /**
311  * Compare two entries in the opcode map.
312  */
313 static int cmp_opcode(const void *elt, const void *key, size_t size) {
314         const opcode_key_t *o1 = elt;
315         const opcode_key_t *o2 = key;
316
317         (void) size;
318         return o1->code != o2->code || o1->mode != o2->mode ||
319                o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
320 }  /* cmp_opcode */
321
322 /**
323  * Compare two Def-Use edges for input position.
324  */
325 static int cmp_def_use_edge(const void *a, const void *b) {
326         const ir_def_use_edge *ea = a;
327         const ir_def_use_edge *eb = b;
328
329         /* no overrun, because range is [-1, MAXINT] */
330         return ea->pos - eb->pos;
331 }  /* cmp_def_use_edge */
332
333 /**
334  * We need the Def-Use edges sorted.
335  */
336 static void sort_irn_outs(node_t *node) {
337         ir_node *irn = node->node;
338         int n_outs = get_irn_n_outs(irn);
339
340         if (n_outs > 1) {
341                 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
342         }
343         node->max_user_input = irn->out[n_outs + 1].pos;
344 }  /* sort_irn_outs */
345
346 /**
347  * Return the type of a node.
348  *
349  * @param irn  an IR-node
350  *
351  * @return the associated type of this node
352  */
353 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
354         return get_irn_node(irn)->type;
355 }  /* get_node_type */
356
357 /**
358  * Return the tarval of a node.
359  *
360  * @param irn  an IR-node
361  *
362  * @return the associated type of this node
363  */
364 static INLINE tarval *get_node_tarval(const ir_node *irn) {
365         lattice_elem_t type = get_node_type(irn);
366
367         if (is_tarval(type.tv))
368                 return type.tv;
369         return tarval_bottom;
370 }  /* get_node_type */
371
372 /**
373  * Add a partition to the worklist.
374  */
375 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
376         assert(X->on_worklist == 0);
377         X->wl_next     = env->worklist;
378         X->on_worklist = 1;
379         env->worklist  = X;
380 }
381
382 /**
383  * Create a new empty partition.
384  *
385  * @param env   the environment
386  *
387  * @return a newly allocated partition
388  */
389 static INLINE partition_t *new_partition(environment_t *env) {
390         partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
391
392         INIT_LIST_HEAD(&part->entries);
393         INIT_LIST_HEAD(&part->cprop);
394         part->wl_next         = NULL;
395         part->touched_next    = NULL;
396         part->cprop_next      = NULL;
397         part->touched         = NULL;
398         part->n_nodes         = 0;
399         part->n_touched       = 0;
400         part->max_arity       = 0;
401         part->max_user_inputs = 0;
402         part->on_worklist     = 0;
403         part->on_touched      = 0;
404         part->on_cprop        = 0;
405 #ifdef DEBUG_libfirm
406         part->dbg_next        = env->dbg_list;
407         env->dbg_list         = part;
408         part->nr              = part_nr++;
409 #endif
410
411         return part;
412 }  /* new_partition */
413
414 /**
415  * Get the first node from a partition.
416  */
417 static INLINE node_t *get_first_node(const partition_t *X) {
418         return list_entry(X->entries.next, node_t, node_list);
419 }
420
421 /**
422  * Return the type of a partition (assuming partition is non-empty and
423  * all elements have the same type).
424  *
425  * @param X  a partition
426  *
427  * @return the type of the first element of the partition
428  */
429 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
430         const node_t *first = get_first_node(X);
431         return first->type;
432 }  /* get_partition_type */
433
434 /**
435  * Creates a partition node for the given IR-node and place it
436  * into the given partition.
437  *
438  * @param irn   an IR-node
439  * @param part  a partition to place the node in
440  * @param env   the environment
441  *
442  * @return the created node
443  */
444 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
445         /* create a partition node and place it in the partition */
446         node_t *node = obstack_alloc(&env->obst, sizeof(*node));
447         ir_mode *mode = get_irn_mode(irn);
448
449         INIT_LIST_HEAD(&node->node_list);
450         INIT_LIST_HEAD(&node->cprop_list);
451         node->node           = irn;
452         node->part           = part;
453         node->next           = NULL;
454         node->type.tv        = get_top_value(mode);
455         node->max_user_input = 0;
456         node->next_edge      = 0;
457         node->on_touched     = 0;
458         node->on_cprop       = 0;
459         node->on_fallen      = 0;
460         set_irn_node(irn, node);
461
462         list_add_tail(&node->node_list, &part->entries);
463         ++part->n_nodes;
464
465         return node;
466 }  /* create_partition_node */
467
468 /**
469  * Pre-Walker, init all Block-Phi lists.
470  */
471 static void init_block_phis(ir_node *irn, void *env) {
472         (void) env;
473
474         if (is_Block(irn)) {
475                 set_Block_phis(irn, NULL);
476         }
477 }
478
479 /**
480  * Post-Walker, initialize all Nodes' type to U or top and place
481  * all nodes into the TOP partition.
482  */
483 static void create_initial_partitions(ir_node *irn, void *ctx) {
484         environment_t *env  = ctx;
485         partition_t   *part = env->initial;
486         node_t        *node;
487         int           arity;
488
489         node = create_partition_node(irn, part, env);
490         sort_irn_outs(node);
491         arity = get_irn_arity(irn);
492         if (arity > part->max_arity)
493                 part->max_arity = arity;
494         if (node->max_user_input > part->max_user_inputs)
495                 part->max_user_inputs = node->max_user_input;
496
497         if (is_Phi(irn)) {
498                 add_Block_phi(get_nodes_block(irn), irn);
499         }
500 }  /* create_initial_partitions */
501
502 /**
503  * Add a partition to the touched set if not already there.
504  *
505  * @param part  the partition
506  * @param env   the environment
507  */
508 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
509         if (part->on_touched == 0) {
510                 part->touched_next = env->touched;
511                 env->touched       = part;
512                 part->on_touched   = 1;
513         }
514 }  /* add_to_touched */
515
516 /**
517  * Add a node to the entry.partition.touched set if not already there.
518  *
519  * @param y  a node
520  */
521 static INLINE void add_to_partition_touched(node_t *y) {
522         if (y->on_touched == 0) {
523                 partition_t *part = y->part;
524
525                 y->next       = part->touched;
526                 part->touched = y;
527                 y->on_touched = 1;
528                 ++part->n_touched;
529         }
530 }  /* add_to_partition_touched */
531
532 /**
533  * Update the worklist: If Z is on worklist then add Z' to worklist.
534  * Else add the smaller of Z and Z' to worklist.
535  *
536  * @param Z        the Z partition
537  * @param Z_prime  the Z' partition, a previous part of Z
538  * @param env      the environment
539  */
540 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
541         if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
542                 add_to_worklist(Z_prime, env);
543         } else {
544                 add_to_worklist(Z, env);
545         }
546 }  /* update_worklist */
547
548 /**
549  * Split a partition by a local list.
550  *
551  * @param Z    the Z partition to split
552  * @param g    a (non-empty) node list
553  * @param env  the environment
554  *
555  * @return  a new partition containing the nodes of g
556  */
557 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
558         partition_t *Z_prime;
559         node_t      *node;
560         unsigned    n = 0;
561         int         max_input, max_arity, arity;
562
563         dump_partition("Splitting ", Z);
564
565         assert(g != NULL);
566
567         /* Remove g from Z. */
568         for (node = g; node != NULL; node = node->next) {
569                 list_del(&node->node_list);
570                 ++n;
571         }
572         assert(n < Z->n_nodes);
573         Z->n_nodes -= n;
574
575         /* Move g to a new partition, Z\92. */
576         Z_prime = new_partition(env);
577         max_arity = max_input = 0;
578         for (node = g; node != NULL; node = node->next) {
579                 list_add(&node->node_list, &Z_prime->entries);
580                 node->part = Z_prime;
581                 arity = get_irn_arity(node->node);
582                 if (arity > max_arity)
583                         max_arity = arity;
584                 if (node->max_user_input > max_input)
585                         max_input = node->max_user_input;
586         }
587         Z_prime->max_arity       = max_arity;
588         Z_prime->max_user_inputs = max_input;
589         Z_prime->n_nodes         = n;
590
591         update_worklist(Z, Z_prime, env);
592
593         dump_partition("Now ", Z);
594         dump_partition("Created new ", Z_prime);
595         return Z_prime;
596 }  /* split */
597
598 /**
599  * Returns non-zero if the i'th input of a Phi node is live.
600  *
601  * @param phi  a Phi-node
602  * @param i    an input number
603  *
604  * @return non-zero if the i'th input of the given Phi node is live
605  */
606 static int is_live_input(ir_node *phi, int i) {
607         if (i >= 0) {
608                 ir_node        *block = get_nodes_block(phi);
609                 ir_node        *pred  = get_Block_cfgpred(block, i);
610                 lattice_elem_t type   = get_node_type(pred);
611
612                 return type.tv != tarval_unreachable;
613         }
614         /* else it's the control input, always live */
615         return 1;
616 }  /* is_live_input */
617
618 /**
619  * Return non-zero if a type is a constant.
620  */
621 static int is_constant_type(lattice_elem_t type) {
622         if (type.tv != tarval_bottom && type.tv != tarval_top)
623                 return 1;
624         return 0;
625 }  /* is_constant_type */
626
627 /**
628  * Place a node on the cprop list.
629  *
630  * @param y    the node
631  * @param env  the environment
632  */
633 static void add_node_to_cprop(node_t *y, environment_t *env) {
634         /* Add y to y.partition.cprop. */
635         if (y->on_cprop == 0) {
636                 partition_t *Y = y->part;
637
638                 list_add_tail(&y->cprop_list, &Y->cprop);
639                 y->on_cprop   = 1;
640
641                 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
642
643                 /* place its partition on the cprop list */
644                 if (Y->on_cprop == 0) {
645                         Y->cprop_next = env->cprop;
646                         env->cprop    = Y;
647                         Y->on_cprop   = 1;
648                 }
649         }
650         if (get_irn_mode(y->node) == mode_T) {
651                 /* mode_T nodes always produce tarval_bottom, so we must explicitly
652                    add it's Proj's to get constant evaluation to work */
653                 int i;
654
655                 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
656                         node_t *proj = get_irn_node(get_irn_out(y->node, i));
657
658                         add_node_to_cprop(proj, env);
659                 }
660         }
661
662         if (is_Block(y->node)) {
663                 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
664                  * if someone placed the block. The Block is only placed if the reachability
665                  * changes, and this must be re-evaluated in compute_Phi(). */
666                 ir_node *phi;
667                 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
668                         node_t *p = get_irn_node(phi);
669                         add_node_to_cprop(p, env);
670                 }
671         }
672 }  /* add_node_to_cprop */
673
674 /**
675  * Check whether a type is neither Top or a constant.
676  * Note: U, R are NOT constants!
677  *
678  * @param type  the type to check
679  */
680 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
681         if (is_tarval(type.tv)) {
682                 if (type.tv == tarval_top)
683                         return 0;
684                 if (tarval_is_constant(type.tv))
685                         return 0;
686         } else {
687                 /* is a symconst */
688                 return 0;
689         }
690         return 1;
691 }
692
693 /**
694  * Split the partitions if caused by the first entry on the worklist.
695  *
696  * @param env  the environment
697  */
698 static void cause_splits(environment_t *env) {
699         partition_t *X, *Y, *Z;
700         node_t      *x, *y, *e;
701         int         i, end_idx;
702         ir_opcode   code;
703         ir_node     *succ;
704
705         /* remove the first partition from the worklist */
706         X = env->worklist;
707         env->worklist  = X->wl_next;
708         X->on_worklist = 0;
709
710         dump_partition("Cause_split: ", X);
711         end_idx = env->end_idx;
712         for (i = -1; i <= X->max_user_inputs; ++i) {
713                 /* empty the touched set: already done, just clear the list */
714                 env->touched = NULL;
715
716                 list_for_each_entry(node_t, x, &X->entries, node_list) {
717                         int num_edges;
718
719                         if (i == -1) {
720                                 x->next_edge = 1;
721                         }
722                         num_edges = get_irn_n_outs(x->node);
723
724                         while (x->next_edge <= num_edges) {
725                                 ir_def_use_edge *edge = &x->node->out[x->next_edge];
726
727                                 /* check if we have necessary edges */
728                                 if (edge->pos > i)
729                                         break;
730
731                                 ++x->next_edge;
732
733                                 succ = edge->use;
734
735                                 /* ignore the "control input" for non-pinned nodes
736                                    if we are running in GCSE mode */
737                                 if (i < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
738                                         continue;
739
740                                 y = get_irn_node(succ);
741                                 if (is_constant_type(y->type)) {
742                                         code = get_irn_opcode(succ);
743                                         if (code == iro_Sub || (code == iro_Proj && is_Cmp(get_Proj_pred(succ))))
744                                                 add_node_to_cprop(y, env);
745                                 }
746
747                                 /* Partitions of constants should not be split simply because their Nodes have unequal
748                                    functions or incongruent inputs. */
749                                 if (type_is_neither_top_nor_const(y->type) &&
750                                         (! is_Phi(y->node) || is_live_input(y->node, i))) {
751                                         Y = y->part;
752                                         add_to_touched(Y, env);
753                                         add_to_partition_touched(y);
754                                 }
755                         }
756                 }
757
758                 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
759                         /* remove it from the touched set */
760                         Z->on_touched = 0;
761
762                         if (Z->n_nodes != Z->n_touched) {
763                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
764                                 split(Z, Z->touched, env);
765                         }
766                         /* Empty local Z.touched. */
767                         for (e = Z->touched; e != NULL; e = e->next) {
768                                 e->on_touched = 0;
769                         }
770                         Z->touched   = NULL;
771                         Z->n_touched = 0;
772                 }
773         }
774 }  /* cause_splits */
775
776 /**
777  * Implements split_by_what(): Split a partition by characteristics given
778  * by the what function.
779  *
780  * @param X     the partition to split
781  * @param What  a function returning an Id for every node of the partition X
782  * @param P     an flexible array to store the result partitions or NULL
783  * @param env   the environment
784  *
785  * @return if P != NULL P will be filled with the resulting partitions and returned
786  */
787 static partition_t **split_by_what(partition_t *X, what_func What,
788                                    partition_t **P, environment_t *env) {
789         node_t          *x, *S;
790         listmap_t       map;
791         listmap_entry_t *iter;
792         partition_t     *R;
793
794         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
795         listmap_init(&map);
796         list_for_each_entry(node_t, x, &X->entries, node_list) {
797                 void            *id = What(x, env);
798                 listmap_entry_t *entry;
799
800                 if (id == NULL) {
801                         /* input not allowed, ignore */
802                         continue;
803                 }
804                 /* Add x to map[What(x)]. */
805                 entry = listmap_find(&map, id);
806                 x->next     = entry->list;
807                 entry->list = x;
808         }
809         /* Let P be a set of Partitions. */
810
811         /* for all sets S except one in the range of map do */
812         for (iter = map.values; iter != NULL; iter = iter->next) {
813                 if (iter->next == NULL) {
814                         /* this is the last entry, ignore */
815                         break;
816                 }
817                 S = iter->list;
818
819                 /* Add SPLIT( X, S ) to P. */
820                 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
821                 R = split(X, S, env);
822                 if (P != NULL) {
823                         ARR_APP1(partition_t *, P, R);
824                 }
825         }
826         /* Add X to P. */
827         if (P != NULL) {
828                 ARR_APP1(partition_t *, P, X);
829         }
830
831         listmap_term(&map);
832         return P;
833 }  /* split_by_what */
834
835 /** lambda n.(n.type) */
836 static void *lambda_type(const node_t *node, environment_t *env) {
837         (void)env;
838         return node->type.tv;
839 }  /* lambda_type */
840
841 /** lambda n.(n.opcode) */
842 static void *lambda_opcode(const node_t *node, environment_t *env) {
843         opcode_key_t key, *entry;
844         ir_node      *irn = node->node;
845
846         key.code   = get_irn_opcode(irn);
847         key.mode   = get_irn_mode(irn);
848         key.u.proj = 0;
849         key.u.ent  = NULL;
850
851         switch (get_irn_opcode(irn)) {
852         case iro_Proj:
853                 key.u.proj = get_Proj_proj(irn);
854                 break;
855         case iro_Sel:
856                 key.u.ent = get_Sel_entity(irn);
857                 break;
858         default:
859                 break;
860         }
861
862         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
863         return entry;
864 }  /* lambda_opcode */
865
866 /** lambda n.(n[i].partition) */
867 static void *lambda_partition(const node_t *node, environment_t *env) {
868         ir_node *skipped = skip_Proj(node->node);
869         ir_node *pred;
870         node_t  *p;
871         int     i = env->lambda_input;
872
873         if (i >= get_irn_arity(node->node)) {
874                 /* we are outside the allowed range */
875                 return NULL;
876         }
877
878         /* ignore the "control input" for non-pinned nodes
879            if we are running in GCSE mode */
880         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
881                 return NULL;
882
883         pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
884         p    = get_irn_node(pred);
885
886         return p->part;
887 }  /* lambda_partition */
888
889 /**
890  * Checks whether a type is a constant.
891  */
892 static int is_type_constant(lattice_elem_t type) {
893         if (is_tarval(type.tv))
894                 return tarval_is_constant(type.tv);
895         /* else it is a symconst */
896         return 1;
897 }
898
899 /**
900  * Implements split_by().
901  *
902  * @param X    the partition to split
903  * @param env  the environment
904  */
905 static void split_by(partition_t *X, environment_t *env) {
906         partition_t **P = NEW_ARR_F(partition_t *, 0);
907         int         i, j, k;
908
909         DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
910         P = split_by_what(X, lambda_type, P, env);
911         for (i = ARR_LEN(P) - 1; i >= 0; --i) {
912                 partition_t *Y = P[i];
913
914                 if (Y->n_nodes > 1) {
915                         lattice_elem_t type = get_partition_type(Y);
916
917                         /* we do not want split the TOP, unreachable or constant partitions */
918                         if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
919                                 partition_t **Q = NEW_ARR_F(partition_t *, 0);
920
921                                 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
922                                 Q = split_by_what(Y, lambda_opcode, Q, env);
923
924                                 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
925                                         partition_t *Z = Q[j];
926
927                                         for (k = Z->max_arity - 1; k >= -1; --k) {
928                                                 if (Z->n_nodes > 1) {
929                                                         env->lambda_input = k;
930                                                         DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", k, Z->nr));
931                                                         split_by_what(Z, lambda_partition, NULL, env);
932                                                 }
933                                         }
934                                 }
935                                 DEL_ARR_F(Q);
936                         }
937                 }
938         }
939         DEL_ARR_F(P);
940 }  /* split_by */
941
942 /**
943  * (Re-)compute the type for a given node.
944  *
945  * @param node  the node
946  */
947 static void default_compute(node_t *node) {
948         int     i;
949         ir_node *irn = node->node;
950         tarval  *top = tarval_top;
951
952         if (get_irn_mode(node->node) == mode_X)
953                 top = tarval_unreachable;
954
955         if (get_irn_pinned(irn) == op_pin_state_pinned) {
956                 node_t *block = get_irn_node(get_nodes_block(irn));
957
958                 if (block->type.tv == tarval_unreachable) {
959                         node->type.tv = top;
960                         return;
961                 }
962         }
963
964         /* if any of the data inputs have type top, the result is type top */
965         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
966                 ir_node *pred = get_irn_n(irn, i);
967                 node_t  *p    = get_irn_node(pred);
968
969                 if (p->type.tv == tarval_top) {
970                         node->type.tv = top;
971                         return;
972                 }
973         }
974
975         if (get_irn_mode(node->node) == mode_X)
976                 node->type.tv = tarval_reachable;
977         else
978                 node->type.tv = computed_value(irn);
979 }  /* default_compute */
980
981 /**
982  * (Re-)compute the type for a Block node.
983  *
984  * @param node  the node
985  */
986 static void compute_Block(node_t *node) {
987         int     i;
988         ir_node *block = node->node;
989
990         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
991                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
992
993                 if (pred->type.tv == tarval_reachable) {
994                         /* A block is reachable, if at least of predecessor is reachable. */
995                         node->type.tv = tarval_reachable;
996                         return;
997                 }
998         }
999         node->type.tv = tarval_unreachable;
1000 }  /* compute_Block */
1001
1002 /**
1003  * (Re-)compute the type for a Jmp node.
1004  *
1005  * @param node  the node
1006  */
1007 static void compute_Jmp(node_t *node) {
1008         node_t *block = get_irn_node(get_nodes_block(node->node));
1009
1010         node->type = block->type;
1011 }  /* compute_Jmp */
1012
1013 /**
1014  * (Re-)compute the type for the End node.
1015  *
1016  * @param node  the node
1017  */
1018 static void compute_End(node_t *node) {
1019         /* the End node is NOT dead of course */
1020         node->type.tv = tarval_reachable;
1021 }
1022
1023 /**
1024  * (Re-)compute the type for a SymConst node.
1025  *
1026  * @param node  the node
1027  */
1028 static void compute_SymConst(node_t *node) {
1029         ir_node *irn = node->node;
1030         node_t  *block = get_irn_node(get_nodes_block(irn));
1031
1032         if (block->type.tv == tarval_unreachable) {
1033                 node->type.tv = tarval_top;
1034                 return;
1035         }
1036         switch (get_SymConst_kind(irn)) {
1037         case symconst_addr_ent:
1038         /* case symconst_addr_name: cannot handle this yet */
1039                 node->type.sym = get_SymConst_symbol(irn);
1040                 break;
1041         default:
1042                 node->type.tv = computed_value(irn);
1043         }
1044 }  /* compute_SymConst */
1045
1046 /**
1047  * (Re-)compute the type for a Phi node.
1048  *
1049  * @param node  the node
1050  */
1051 static void compute_Phi(node_t *node) {
1052         int            i;
1053         ir_node        *phi = node->node;
1054         lattice_elem_t type;
1055
1056         /* if a Phi is in a unreachable block, its type is TOP */
1057         node_t *block = get_irn_node(get_nodes_block(phi));
1058
1059         if (block->type.tv == tarval_unreachable) {
1060                 node->type.tv = tarval_top;
1061                 return;
1062         }
1063
1064         /* Phi implements the Meet operation */
1065         type.tv = tarval_top;
1066         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1067                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
1068                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1069
1070                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1071                         /* ignore TOP inputs: We must check here for unreachable blocks,
1072                            because Firm constants live in the Start Block are NEVER Top.
1073                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1074                            comes from a unreachable input. */
1075                         continue;
1076                 }
1077                 if (pred->type.tv == tarval_bottom) {
1078                         node->type.tv = tarval_bottom;
1079                         return;
1080                 } else if (type.tv == tarval_top) {
1081                         /* first constant found */
1082                         type = pred->type;
1083                 } else if (type.tv != pred->type.tv) {
1084                         /* different constants or tarval_bottom */
1085                         node->type.tv = tarval_bottom;
1086                         return;
1087                 }
1088                 /* else nothing, constants are the same */
1089         }
1090         node->type = type;
1091 }  /* compute_Phi */
1092
1093 /**
1094  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1095  *
1096  * @param node  the node
1097  */
1098 static void compute_Add(node_t *node) {
1099         ir_node        *sub = node->node;
1100         node_t         *l   = get_irn_node(get_Add_left(sub));
1101         node_t         *r   = get_irn_node(get_Add_right(sub));
1102         lattice_elem_t a    = l->type;
1103         lattice_elem_t b    = r->type;
1104         node_t         *block = get_irn_node(get_nodes_block(sub));
1105         ir_mode        *mode;
1106
1107         if (block->type.tv == tarval_unreachable) {
1108                 node->type.tv = tarval_top;
1109                 return;
1110         }
1111
1112         if (a.tv == tarval_top || b.tv == tarval_top) {
1113                 node->type.tv = tarval_top;
1114         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1115                 node->type.tv = tarval_bottom;
1116         } else {
1117                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1118                    must call tarval_add() first to handle this case! */
1119                 if (is_tarval(a.tv)) {
1120                         if (is_tarval(b.tv)) {
1121                                 node->type.tv = tarval_add(a.tv, b.tv);
1122                                 return;
1123                         }
1124                         mode = get_tarval_mode(a.tv);
1125                         if (a.tv == get_mode_null(mode)) {
1126                                 node->type = b;
1127                                 return;
1128                         }
1129                 } else if (is_tarval(b.tv)) {
1130                         mode = get_tarval_mode(b.tv);
1131                         if (b.tv == get_mode_null(mode)) {
1132                                 node->type = a;
1133                                 return;
1134                         }
1135                 }
1136                 node->type.tv = tarval_bottom;
1137         }
1138 }  /* compute_Add */
1139
1140 /**
1141  * Returns true if a type is a constant.
1142  */
1143 static int is_con(const lattice_elem_t type) {
1144         return is_entity(type.sym.entity_p) || tarval_is_constant(type.tv);
1145 }
1146
1147 /**
1148  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1149  *
1150  * @param node  the node
1151  */
1152 static void compute_Sub(node_t *node) {
1153         ir_node        *sub = node->node;
1154         node_t         *l   = get_irn_node(get_Sub_left(sub));
1155         node_t         *r   = get_irn_node(get_Sub_right(sub));
1156         lattice_elem_t a    = l->type;
1157         lattice_elem_t b    = r->type;
1158         node_t         *block = get_irn_node(get_nodes_block(sub));
1159
1160         if (block->type.tv == tarval_unreachable) {
1161                 node->type.tv = tarval_top;
1162                 return;
1163         }
1164         if (a.tv == tarval_top || b.tv == tarval_top) {
1165                 node->type.tv = tarval_top;
1166         } else if (is_con(a) && is_con(b)) {
1167                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1168                         node->type.tv = tarval_sub(a.tv, b.tv);
1169                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1170                         node->type = b;
1171                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1172                         node->type = a;
1173                 } else {
1174                         node->type.tv = tarval_bottom;
1175                 }
1176         } else if (r->part == l->part &&
1177                    (!mode_is_float(get_irn_mode(l->node)))) {
1178                 /*
1179                  * BEWARE: a - a is NOT always 0 for floating Point values, as
1180                  * NaN op NaN = NaN, so we must check this here.
1181                  */
1182                 ir_mode *mode = get_irn_mode(sub);
1183                 node->type.tv = get_mode_null(mode);
1184         } else {
1185                 node->type.tv = tarval_bottom;
1186         }
1187 }  /* compute_Sub */
1188
1189 /**
1190  * (Re-)compute the type for a Proj(Cmp).
1191  *
1192  * @param node  the node
1193  * @param cond  the predecessor Cmp node
1194  */
1195 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1196         ir_node        *proj = node->node;
1197         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1198         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1199         lattice_elem_t a     = l->type;
1200         lattice_elem_t b     = r->type;
1201         pn_Cmp         pnc   = get_Proj_proj(proj);
1202
1203         if (a.tv == tarval_top || b.tv == tarval_top) {
1204                 node->type.tv = tarval_top;
1205         } else if (is_con(a) && is_con(b)) {
1206                 default_compute(node);
1207         } else if (r->part == l->part &&
1208                    (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1209                 /*
1210                  * BEWARE: a == a is NOT always True for floating Point values, as
1211                  * NaN != NaN is defined, so we must check this here.
1212                  */
1213                 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1214         } else {
1215                 node->type.tv = tarval_bottom;
1216         }
1217 }  /* compute_Proj_Cmp */
1218
1219 /**
1220  * (Re-)compute the type for a Proj(Cond).
1221  *
1222  * @param node  the node
1223  * @param cond  the predecessor Cond node
1224  */
1225 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1226         ir_node *proj     = node->node;
1227         long    pnc       = get_Proj_proj(proj);
1228         ir_node *sel      = get_Cond_selector(cond);
1229         node_t  *selector = get_irn_node(sel);
1230
1231         if (get_irn_mode(sel) == mode_b) {
1232                 /* an IF */
1233                 if (pnc == pn_Cond_true) {
1234                         if (selector->type.tv == tarval_b_false) {
1235                                 node->type.tv = tarval_unreachable;
1236                         } else if (selector->type.tv == tarval_b_true) {
1237                                 node->type.tv = tarval_reachable;
1238                         } else if (selector->type.tv == tarval_bottom) {
1239                                 node->type.tv = tarval_reachable;
1240                         } else {
1241                                 assert(selector->type.tv == tarval_top);
1242                                 node->type.tv = tarval_unreachable;
1243                         }
1244                 } else {
1245                         assert(pnc == pn_Cond_false);
1246
1247                         if (selector->type.tv == tarval_b_false) {
1248                                 node->type.tv = tarval_reachable;
1249                         } else if (selector->type.tv == tarval_b_true) {
1250                                 node->type.tv = tarval_unreachable;
1251                         } else if (selector->type.tv == tarval_bottom) {
1252                                 node->type.tv = tarval_reachable;
1253                         } else {
1254                                 assert(selector->type.tv == tarval_top);
1255                                 node->type.tv = tarval_unreachable;
1256                         }
1257                 }
1258         } else {
1259                 /* an SWITCH */
1260                 if (selector->type.tv == tarval_bottom) {
1261                         node->type.tv = tarval_reachable;
1262                 } else if (selector->type.tv == tarval_top) {
1263                         node->type.tv = tarval_unreachable;
1264                 } else {
1265                         long value = get_tarval_long(selector->type.tv);
1266                         if (pnc == get_Cond_defaultProj(cond)) {
1267                                 /* default switch, have to check ALL other cases */
1268                                 int i;
1269
1270                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1271                                         ir_node *succ = get_irn_out(cond, i);
1272
1273                                         if (succ == proj)
1274                                                 continue;
1275                                         if (value == get_Proj_proj(succ)) {
1276                                                 /* we found a match, will NOT take the default case */
1277                                                 node->type.tv = tarval_unreachable;
1278                                                 return;
1279                                         }
1280                                 }
1281                                 /* all cases checked, no match, will take default case */
1282                                 node->type.tv = tarval_reachable;
1283                         } else {
1284                                 /* normal case */
1285                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1286                         }
1287                 }
1288         }
1289 }  /* compute_Proj_Cond */
1290
1291 /**
1292  * (Re-)compute the type for a Proj-Nodes.
1293  *
1294  * @param node  the node
1295  */
1296 static void compute_Proj(node_t *node) {
1297         ir_node *proj = node->node;
1298         ir_mode *mode = get_irn_mode(proj);
1299         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1300         ir_node *pred  = get_Proj_pred(proj);
1301
1302         if (block->type.tv == tarval_unreachable) {
1303                 /* a Proj node in an unreachable block computes Top
1304                    except if it's the initial_exec node. */
1305                 if (get_Proj_proj(proj) != pn_Start_X_initial_exec ||
1306                         ! is_Start(pred)) {
1307                         node->type.tv = get_top_value(mode);
1308                         return;
1309                 }
1310         }
1311
1312         if (mode == mode_M) {
1313                 /* mode M is always bottom */
1314                 node->type.tv = tarval_bottom;
1315                 return;
1316         }
1317         if (mode != mode_X) {
1318                 if (is_Cmp(pred))
1319                         compute_Proj_Cmp(node, pred);
1320                 else
1321                         default_compute(node);
1322                 return;
1323         }
1324         /* handle mode_X nodes */
1325
1326         switch (get_irn_opcode(pred)) {
1327         case iro_Start:
1328                 /* the Proj_X from the Start is always reachable */
1329                 node->type.tv = tarval_reachable;
1330                 break;
1331         case iro_Cond:
1332                 compute_Proj_Cond(node, pred);
1333                 break;
1334         default:
1335                 default_compute(node);
1336         }
1337 }  /* compute_Proj */
1338
1339 /**
1340  * (Re-)compute the type for a given node.
1341  *
1342  * @param node  the node
1343  */
1344 static void compute(node_t *node) {
1345         compute_func func = (compute_func)node->node->op->ops.generic;
1346
1347         if (func != NULL)
1348                 func(node);
1349 }  /* compute */
1350
1351 /**
1352  * Propagate constant evaluation.
1353  *
1354  * @param env  the environment
1355  */
1356 static void propagate(environment_t *env) {
1357         partition_t    *X, *Y;
1358         node_t         *x;
1359         lattice_elem_t old_type;
1360         node_t         *fallen;
1361         unsigned       n_fallen;
1362         int            i;
1363
1364         while (env->cprop != NULL) {
1365                 /* remove the first partition X from cprop */
1366                 X          = env->cprop;
1367                 X->on_cprop = 0;
1368                 env->cprop = X->cprop_next;
1369
1370                 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
1371                 fallen   = NULL;
1372                 n_fallen = 0;
1373                 while (! list_empty(&X->cprop)) {
1374                         /* remove the first Node x from X.cprop */
1375                         x = list_entry(X->cprop.next, node_t, cprop_list);
1376                         list_del(&x->cprop_list);
1377                         x->on_cprop = 0;
1378
1379                         /* compute a new type for x */
1380                         old_type = x->type;
1381                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1382                         compute(x);
1383                         if (x->type.tv != old_type.tv) {
1384                                 verify_type(old_type, x->type);
1385                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1386
1387                                 if (x->on_fallen == 0) {
1388                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
1389                                            not already on the list. */
1390                                         x->next      = fallen;
1391                                         x->on_fallen = 1;
1392                                         fallen       = x;
1393                                         ++n_fallen;
1394                                         DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
1395                                 }
1396                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1397                                         ir_node *succ = get_irn_out(x->node, i);
1398                                         node_t  *y    = get_irn_node(succ);
1399
1400                                         /* Add y to y.partition.cprop. */
1401                                         add_node_to_cprop(y, env);
1402                                 }
1403                         }
1404                 }
1405
1406                 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1407                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
1408                         Y = split(X, fallen, env);
1409                 } else {
1410                         Y = X;
1411                 }
1412                 /* remove the nodes from the fallen list */
1413                 for (x = fallen; x != NULL; x = x->next)
1414                         x->on_fallen = 0;
1415
1416                 if (Y->n_nodes > 1)
1417                         split_by(Y, env);
1418         }
1419 }  /* propagate */
1420
1421 /**
1422  * Get the leader for a given node from its congruence class.
1423  *
1424  * @param irn  the node
1425  */
1426 static ir_node *get_leader(node_t *node) {
1427         partition_t *part = node->part;
1428
1429         if (part->n_nodes > 1) {
1430                 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1431
1432                 return get_first_node(part)->node;
1433         }
1434         return node->node;
1435 }
1436
1437 /**
1438  * Post-Walker, apply the analysis results;
1439  */
1440 static void apply_result(ir_node *irn, void *ctx) {
1441         node_t *node = get_irn_node(irn);
1442
1443         (void) ctx;
1444         if (is_Block(irn)) {
1445                 if (irn == get_irg_end_block(current_ir_graph)) {
1446                         /* the EndBlock is always reachable even if the analysis
1447                            finds out the opposite :-) */
1448                         return;
1449                 }
1450
1451                 if (node->type.tv == tarval_unreachable) {
1452                         /* mark dead blocks */
1453                         set_Block_dead(irn);
1454                 }
1455         } else if (is_End(irn)) {
1456                 /* do not touch the End node */
1457         } else {
1458                 node_t *block = get_irn_node(get_nodes_block(irn));
1459
1460                 if (block->type.tv == tarval_unreachable) {
1461                         if (! is_Bad(irn)) {
1462                                 ir_node *bad = get_irg_bad(current_ir_graph);
1463
1464                                 /* here, bad might already have a node, but this can be safely ignored
1465                                    as long as bad has at least ONE valid node */
1466                                 set_irn_node(bad, node);
1467                                 node->node = bad;
1468                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1469                                 exchange(irn, bad);
1470                         }
1471                 }
1472                 else if (get_irn_mode(irn) == mode_X) {
1473                         if (node->type.tv == tarval_unreachable) {
1474                                 ir_node *bad = get_irg_bad(current_ir_graph);
1475
1476                                 /* see comment above */
1477                                 set_irn_node(bad, node);
1478                                 node->node = bad;
1479                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1480                                 exchange(irn, bad);
1481                         }
1482                         else if (is_Proj(irn)) {
1483                                 /* leave or Jmp */
1484                                 ir_node *cond = get_Proj_pred(irn);
1485
1486                                 if (is_Cond(cond)) {
1487                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
1488
1489                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1490                                                 /* Cond selector is a constant, make a Jmp */
1491                                                 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1492                                                 set_irn_node(jmp, node);
1493                                                 node->node = jmp;
1494                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1495                                                 exchange(irn, jmp);
1496                                         }
1497                                 }
1498                         }
1499                 } else {
1500                         /* normal data node */
1501                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1502                                 tarval *tv = node->type.tv;
1503
1504                                 if (! is_Const(irn)) {
1505                                         /* can be replaced by a constant */
1506                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1507                                         set_irn_node(c, node);
1508                                         node->node = c;
1509                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1510                                         exchange(irn, c);
1511                                 }
1512                         } else if (is_entity(node->type.sym.entity_p)) {
1513                                 if (! is_SymConst(irn)) {
1514                                         /* can be replaced by a Symconst */
1515                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1516                                         set_irn_node(symc, node);
1517                                         node->node = symc;
1518
1519                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1520                                         exchange(irn, symc);
1521                                 }
1522                         } else {
1523                                 ir_node *leader = get_leader(node);
1524
1525                                 if (leader != irn) {
1526                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
1527                                         exchange(irn, leader);
1528                                 }
1529                         }
1530                 }
1531         }
1532 }  /* static void apply_result(ir_node *irn, void *ctx) {
1533  */
1534
1535 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1536
1537 /**
1538  * sets the generic functions to compute.
1539  */
1540 static void set_compute_functions(void) {
1541         int i;
1542
1543         /* set the default compute function */
1544         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1545                 ir_op *op = get_irp_opcode(i);
1546                 op->ops.generic = (op_func)default_compute;
1547         }
1548
1549         /* set specific functions */
1550         SET(Block);
1551         SET(Jmp);
1552         SET(Phi);
1553         SET(Add);
1554         SET(Sub);
1555         SET(SymConst);
1556         SET(Proj);
1557         SET(End);
1558 }  /* set_compute_functions */
1559
1560 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1561         ir_node *irn = local != NULL ? local : n;
1562         node_t *node = get_irn_node(irn);
1563
1564         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1565         return 1;
1566 }
1567
1568 void combo(ir_graph *irg) {
1569         environment_t env;
1570         ir_node       *initial_X;
1571         node_t        *start;
1572         ir_graph      *rem = current_ir_graph;
1573
1574         current_ir_graph = irg;
1575
1576         /* register a debug mask */
1577         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1578         //firm_dbg_set_mask(dbg, SET_LEVEL_3);
1579
1580         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1581
1582         obstack_init(&env.obst);
1583         env.worklist       = NULL;
1584         env.cprop          = NULL;
1585         env.touched        = NULL;
1586         env.initial        = NULL;
1587 #ifdef DEBUG_libfirm
1588         env.dbg_list       = NULL;
1589 #endif
1590         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
1591         env.type2id_map    = pmap_create();
1592         env.end_idx        = get_opt_global_cse() ? 0 : -1;
1593         env.lambda_input   = 0;
1594
1595         assure_irg_outs(irg);
1596
1597         /* we have our own value_of function */
1598         set_value_of_func(get_node_tarval);
1599
1600         set_compute_functions();
1601         DEBUG_ONLY(part_nr = 0);
1602
1603         /* create the initial partition and place it on the work list */
1604         env.initial = new_partition(&env);
1605         add_to_worklist(env.initial, &env);
1606         irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
1607
1608         /* Place the START Node's partition on cprop.
1609            Place the START Node on its local worklist. */
1610         initial_X = get_irg_initial_exec(irg);
1611         start     = get_irn_node(initial_X);
1612         add_node_to_cprop(start, &env);
1613
1614         do {
1615                 propagate(&env);
1616                 if (env.worklist != NULL)
1617                         cause_splits(&env);
1618         } while (env.cprop != NULL || env.worklist != NULL);
1619
1620         dump_all_partitions(&env);
1621
1622         set_dump_node_vcgattr_hook(dump_partition_hook);
1623         dump_ir_block_graph(irg, "-partition");
1624         set_dump_node_vcgattr_hook(NULL);
1625
1626
1627         /* apply the result */
1628         irg_walk_graph(irg, NULL, apply_result, &env);
1629
1630         pmap_destroy(env.type2id_map);
1631         del_set(env.opcode2id_map);
1632         obstack_free(&env.obst, NULL);
1633
1634         /* restore value_of() default behavior */
1635         set_value_of_func(NULL);
1636         current_ir_graph = rem;
1637 }  /* combo */