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