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