ca03e36912f871b2ea0ce8f7c700fdf2e14e6bf6
[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         partition_t     *part;          /**< points to the partition this node belongs to */
109         node_t          *cprop_next;    /**< Next node on partition.cprop list. */
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         node_t            *cprop;          /**< The 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         part->cprop           = NULL;
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         node->node           = irn;
410         node->part           = part;
411         node->cprop_next     = NULL;
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  * Walker, initialize all Nodes' type to U or top and place
429  * all nodes into the TOP partition.
430  */
431 static void create_initial_partitions(ir_node *irn, void *ctx) {
432         environment_t *env  = ctx;
433         partition_t   *part = env->initial;
434         node_t        *node;
435         int           arity;
436
437         node = create_partition_node(irn, part, env);
438         sort_irn_outs(node);
439         arity = get_irn_arity(irn);
440         if (arity > part->max_arity)
441                 part->max_arity = arity;
442         if (node->max_user_input > part->max_user_inputs)
443                 part->max_user_inputs = node->max_user_input;
444 }  /* create_initial_partitions */
445
446 /**
447  * Add a partition to the touched set if not already there.
448  *
449  * @param part  the partition
450  * @param env   the environment
451  */
452 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
453         if (part->on_touched == 0) {
454                 part->touched_next = env->touched;
455                 env->touched       = part;
456                 part->on_touched   = 1;
457         }
458 }  /* add_to_touched */
459
460 /**
461  * Add a node to the entry.partition.touched set if not already there.
462  *
463  * @param y  a node
464  */
465 static INLINE void add_to_partition_touched(node_t *y) {
466         if (y->on_touched == 0) {
467                 partition_t *part = y->part;
468
469                 y->next       = part->touched;
470                 part->touched = y;
471                 y->on_touched = 1;
472                 ++part->n_touched;
473         }
474 }  /* add_to_partition_touched */
475
476 /**
477  * Update the worklist: If Z is on worklist then add Z' to worklist.
478  * Else add the smaller of Z and Z' to worklist.
479  *
480  * @param Z        the Z partition
481  * @param Z_prime  the Z' partition, a previous part of Z
482  * @param env      the environment
483  */
484 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
485         if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
486                 add_to_worklist(Z_prime, env);
487         } else {
488                 add_to_worklist(Z, env);
489         }
490 }  /* update_worklist */
491
492 /**
493  * Split a partition by a local list.
494  *
495  * @param Z    the Z partition to split
496  * @param g    a (non-empty) node list
497  * @param env  the environment
498  *
499  * @return  a new partition containing the nodes of g
500  */
501 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
502         partition_t *Z_prime;
503         node_t      *node;
504         unsigned    n = 0;
505         int         max_input, max_arity, arity;
506
507         dump_partition("Splitting ", Z);
508
509         assert(g != NULL);
510
511         /* Remove g from Z. */
512         for (node = g; node != NULL; node = node->next) {
513                 list_del(&node->node_list);
514                 ++n;
515         }
516         assert(n < Z->n_nodes);
517         Z->n_nodes -= n;
518
519         /* Move g to a new partition, Z\92. */
520         Z_prime = new_partition(env);
521         max_arity = max_input = 0;
522         for (node = g; node != NULL; node = node->next) {
523                 list_add(&node->node_list, &Z_prime->entries);
524                 node->part = Z_prime;
525                 arity = get_irn_arity(node->node);
526                 if (arity > max_arity)
527                         max_arity = arity;
528                 if (node->max_user_input > max_input)
529                         max_input = node->max_user_input;
530         }
531         Z_prime->max_arity       = max_arity;
532         Z_prime->max_user_inputs = max_input;
533         Z_prime->n_nodes         = n;
534
535         update_worklist(Z, Z_prime, env);
536
537         dump_partition("Now ", Z);
538         dump_partition("Created new ", Z_prime);
539         return Z_prime;
540 }  /* split */
541
542 /**
543  * Returns non-zero if the i'th input of a Phi node is live.
544  *
545  * @param phi  a Phi-node
546  * @param i    an input number
547  *
548  * @return non-zero if the i'th input of the given Phi node is live
549  */
550 static int is_live_input(ir_node *phi, int i) {
551         if (i >= 0) {
552                 ir_node        *block = get_nodes_block(phi);
553                 ir_node        *pred  = get_Block_cfgpred(block, i);
554                 lattice_elem_t type   = get_node_type(pred);
555
556                 return type.tv != tarval_unreachable;
557         }
558         /* else it's the control input, always live */
559         return 1;
560 }  /* is_live_input */
561
562 /**
563  * Return non-zero if a type is a constant.
564  */
565 static int is_constant_type(lattice_elem_t type) {
566         if (type.tv != tarval_bottom && type.tv != tarval_top)
567                 return 1;
568         return 0;
569 }  /* is_constant_type */
570
571 /**
572  * Place a node on the cprop list.
573  *
574  * @param y    the node
575  * @param env  the environment
576  */
577 static void add_node_to_cprop(node_t *y, environment_t *env) {
578         /* Add y to y.partition.cprop. */
579         if (y->on_cprop == 0) {
580                 partition_t *Y = y->part;
581
582                 y->cprop_next = Y->cprop;
583                 Y->cprop      = y;
584                 y->on_cprop   = 1;
585
586                 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
587
588                 /* place its partition on the cprop list */
589                 if (Y->on_cprop == 0) {
590                         Y->cprop_next = env->cprop;
591                         env->cprop    = Y;
592                         Y->on_cprop   = 1;
593                 }
594         }
595         if (get_irn_mode(y->node) == mode_T) {
596                 /* mode_T nodes always produce tarval_bottom, so we must explicitly
597                    add it's Proj's to get constant evaluation to work */
598                 int i;
599
600                 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
601                         node_t *proj = get_irn_node(get_irn_out(y->node, i));
602
603                         add_node_to_cprop(proj, env);
604                 }
605         }
606 }  /* add_node_to_cprop */
607
608 /**
609  * Split the partitions if caused by the first entry on the worklist.
610  *
611  * @param env  the environment
612  */
613 static void cause_splits(environment_t *env) {
614         partition_t *X, *Y, *Z;
615         node_t      *x, *y, *e;
616         int         i, end_idx;
617         ir_opcode   code;
618         ir_node     *succ;
619
620         /* remove the first partition from the worklist */
621         X = env->worklist;
622         env->worklist  = X->wl_next;
623         X->on_worklist = 0;
624
625         dump_partition("Cause_split: ", X);
626         end_idx = env->end_idx;
627         for (i = -1; i <= X->max_user_inputs; ++i) {
628                 /* empty the touched set: already done, just clear the list */
629                 env->touched = NULL;
630
631                 list_for_each_entry(node_t, x, &X->entries, node_list) {
632                         int num_edges;
633
634                         if (i == -1) {
635                                 x->next_edge = 1;
636                         }
637                         num_edges = get_irn_n_outs(x->node);
638
639                         while (x->next_edge <= num_edges) {
640                                 ir_def_use_edge *edge = &x->node->out[x->next_edge];
641
642                                 /* check if we have necessary edges */
643                                 if (edge->pos > i)
644                                         break;
645
646                                 ++x->next_edge;
647
648                                 succ = edge->use;
649
650                                 /* ignore the "control input" for non-pinned nodes
651                                    if we are running in GCSE mode */
652                                 if (i < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
653                                         continue;
654
655                                 y = get_irn_node(succ);
656                                 if (is_constant_type(y->type)) {
657                                         code = get_irn_opcode(succ);
658                                         if (code == iro_Sub || (code == iro_Proj && is_Cmp(get_Proj_pred(succ))))
659                                                 add_node_to_cprop(y, env);
660                                 }
661
662                                 /* Partitions of constants should not be split simply because their Nodes have unequal
663                                    functions or incongruent inputs. */
664                                 if (y->type.tv == tarval_bottom &&
665                                         (! is_Phi(y->node) || is_live_input(y->node, i))) {
666                                         Y = y->part;
667                                         add_to_touched(Y, env);
668                                         add_to_partition_touched(y);
669                                 }
670                         }
671                 }
672
673                 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
674                         /* remove it from the touched set */
675                         Z->on_touched = 0;
676
677                         if (Z->n_nodes != Z->n_touched) {
678                                 split(Z, Z->touched, env);
679                         }
680                         /* Empty local Z.touched. */
681                         for (e = Z->touched; e != NULL; e = e->next) {
682                                 e->on_touched = 0;
683                         }
684                         Z->touched   = NULL;
685                         Z->n_touched = 0;
686                 }
687         }
688 }  /* cause_splits */
689
690 /**
691  * Implements split_by_what(): Split a partition by characteristics given
692  * by the what function.
693  *
694  * @param X     the partition to split
695  * @param What  a function returning an Id for every node of the partition X
696  * @param P     an flexible array to store the result partitions or NULL
697  * @param env   the environment
698  *
699  * @return if P != NULL P will be filled with the resulting partitions and returned
700  */
701 static partition_t **split_by_what(partition_t *X, what_func What,
702                                                                   partition_t **P, environment_t *env) {
703         node_t          *x, *S;
704         listmap_t       map;
705         listmap_entry_t *iter;
706         partition_t     *R;
707
708         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
709         listmap_init(&map);
710         list_for_each_entry(node_t, x, &X->entries, node_list) {
711                 void            *id = What(x, env);
712                 listmap_entry_t *entry;
713
714                 if (id == NULL) {
715                         /* input not allowed, ignore */
716                         continue;
717                 }
718                 /* Add x to map[What(x)]. */
719                 entry = listmap_find(&map, id);
720                 x->next     = entry->list;
721                 entry->list = x;
722         }
723         /* Let P be a set of Partitions. */
724
725         /* for all sets S except one in the range of map do */
726         for (iter = map.values; iter != NULL; iter = iter->next) {
727                 if (iter->next == NULL) {
728                         /* this is the last entry, ignore */
729                         break;
730                 }
731                 S = iter->list;
732
733                 /* Add SPLIT( X, S ) to P. */
734                 R = split(X, S, env);
735                 if (P != NULL) {
736                         ARR_APP1(partition_t *, P, R);
737                 }
738         }
739         /* Add X to P. */
740         if (P != NULL) {
741                 ARR_APP1(partition_t *, P, X);
742         }
743
744         listmap_term(&map);
745         return P;
746 }  /* split_by_what */
747
748 /** lambda n.(n.type) */
749 static void *lambda_type(const node_t *node, environment_t *env) {
750         (void)env;
751         return node->type.tv;
752 }  /* lambda_type */
753
754 /** lambda n.(n.opcode) */
755 static void *lambda_opcode(const node_t *node, environment_t *env) {
756         opcode_key_t key, *entry;
757         ir_node      *irn = node->node;
758
759         key.code   = get_irn_opcode(irn);
760         key.mode   = get_irn_mode(irn);
761         key.u.proj = 0;
762         key.u.ent  = NULL;
763
764         switch (get_irn_opcode(irn)) {
765         case iro_Proj:
766                 key.u.proj = get_Proj_proj(irn);
767                 break;
768         case iro_Sel:
769                 key.u.ent = get_Sel_entity(irn);
770                 break;
771         default:
772                 break;
773         }
774
775         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
776         return entry;
777 }  /* lambda_opcode */
778
779 /** lambda n.(n[i].partition) */
780 static void *lambda_partition(const node_t *node, environment_t *env) {
781         ir_node *pred;
782         node_t  *p;
783         int     i = env->lambda_input;
784
785         if (i >= get_irn_arity(node->node)) {
786                 /* we are outside the allowed range */
787                 return NULL;
788         }
789
790         /* ignore the "control input" for non-pinned nodes
791            if we are running in GCSE mode */
792         if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
793                 return NULL;
794
795         pred = get_irn_n(node->node, i);
796         p    = get_irn_node(pred);
797
798         return p->part;
799 }  /* lambda_partition */
800
801 /**
802  * Checks whether a type is a constant.
803  */
804 static int is_type_constant(lattice_elem_t type) {
805         if (is_tarval(type.tv))
806                 return tarval_is_constant(type.tv);
807         /* else it is a symconst */
808         return 1;
809 }
810
811 /**
812  * Implements split_by().
813  *
814  * @param X    the partition to split
815  * @param env  the environment
816  */
817 static void split_by(partition_t *X, environment_t *env) {
818         partition_t **P = NEW_ARR_F(partition_t *, 0);
819         int         i, j, k;
820
821         P = split_by_what(X, lambda_type, P, env);
822         for (i = ARR_LEN(P) - 1; i >= 0; --i) {
823                 partition_t    *Y = P[i];
824
825                 if (Y->n_nodes > 1) {
826                         lattice_elem_t type = get_partition_type(Y);
827
828                         /* we do not want split the TOP, unreachable or constant partitions */
829                         if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
830                                 partition_t **Q = NEW_ARR_F(partition_t *, 0);
831
832                                 Q = split_by_what(Y, lambda_opcode, Q, env);
833
834                                 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
835                                         partition_t *Z = Q[j];
836
837                                         for (k = Z->max_arity - 1; k >= -1; --k) {
838                                                 if (Z->n_nodes > 1) {
839                                                         env->lambda_input = k;
840                                                         split_by_what(Z, lambda_partition, NULL, env);
841                                                 }
842                                         }
843                                 }
844                                 DEL_ARR_F(Q);
845                         }
846                 }
847         }
848         DEL_ARR_F(P);
849 }  /* split_by */
850
851 /**
852  * (Re-)compute the type for a given node.
853  *
854  * @param node  the node
855  */
856 static void default_compute(node_t *node) {
857         int     i;
858         ir_node *irn = node->node;
859         tarval  *top = tarval_top;
860
861         if (get_irn_mode(node->node) == mode_X)
862                 top = tarval_unreachable;
863
864         if (get_irn_pinned(irn) == op_pin_state_pinned) {
865                 node_t *block = get_irn_node(get_nodes_block(irn));
866
867                 if (block->type.tv == tarval_unreachable) {
868                         node->type.tv = top;
869                         return;
870                 }
871         }
872
873         /* if any of the data inputs have type top, the result is type top */
874         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
875                 ir_node *pred = get_irn_n(irn, i);
876                 node_t  *p    = get_irn_node(pred);
877
878                 if (p->type.tv == tarval_top) {
879                         node->type.tv = top;
880                         return;
881                 }
882         }
883
884         if (get_irn_mode(node->node) == mode_X)
885                 node->type.tv = tarval_reachable;
886         else
887                 node->type.tv = computed_value(irn);
888 }  /* default_compute */
889
890 /**
891  * (Re-)compute the type for a Block node.
892  *
893  * @param node  the node
894  */
895 static void compute_Block(node_t *node) {
896         int     i;
897         ir_node *block = node->node;
898
899         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
900                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
901
902                 if (pred->type.tv == tarval_reachable) {
903                         /* A block is reachable, if at least of predecessor is reachable. */
904                         node->type.tv = tarval_reachable;
905                         return;
906                 }
907         }
908         node->type.tv = tarval_unreachable;
909 }  /* compute_Block */
910
911 /**
912  * (Re-)compute the type for a Jmp node.
913  *
914  * @param node  the node
915  */
916 static void compute_Jmp(node_t *node) {
917         node_t *block = get_irn_node(get_nodes_block(node->node));
918
919         node->type = block->type;
920 }  /* compute_Jmp */
921
922 /**
923  * (Re-)compute the type for the End node.
924  *
925  * @param node  the node
926  */
927 static void compute_End(node_t *node) {
928         /* the End node is NOT dead of course */
929         node->type.tv = tarval_reachable;
930 }
931
932 /**
933  * (Re-)compute the type for a SymConst node.
934  *
935  * @param node  the node
936  */
937 static void compute_SymConst(node_t *node) {
938         ir_node *irn = node->node;
939         node_t  *block = get_irn_node(get_nodes_block(irn));
940
941         if (block->type.tv == tarval_unreachable) {
942                 node->type.tv = tarval_top;
943                 return;
944         }
945         switch (get_SymConst_kind(irn)) {
946         case symconst_addr_ent:
947         /* case symconst_addr_name: cannot handle this yet */
948                 node->type.sym = get_SymConst_symbol(irn);
949                 break;
950         default:
951                 node->type.tv = computed_value(irn);
952         }
953 }  /* compute_SymConst */
954
955 /**
956  * (Re-)compute the type for a Phi node.
957  *
958  * @param node  the node
959  */
960 static void compute_Phi(node_t *node) {
961         int            i;
962         ir_node        *phi = node->node;
963         lattice_elem_t type;
964
965         /* if a Phi is in a unreachable block, its type is TOP */
966         node_t *block = get_irn_node(get_nodes_block(phi));
967
968         if (block->type.tv == tarval_unreachable) {
969                 node->type.tv = tarval_top;
970                 return;
971         }
972
973         /* Phi implements the Meet operation */
974         type.tv = tarval_top;
975         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
976                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
977                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
978
979                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
980                         /* ignore TOP inputs: We must check here for unreachable blocks,
981                            because Firm constants live in the Start Block are NEVER Top.
982                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
983                            comes from a unreachable input. */
984                         continue;
985                 }
986                 if (pred->type.tv == tarval_bottom) {
987                         node->type.tv = tarval_bottom;
988                         return;
989                 } else if (type.tv == tarval_top) {
990                         /* first constant found */
991                         type = pred->type;
992                 } else if (type.tv != pred->type.tv) {
993                         /* different constants or tarval_bottom */
994                         node->type.tv = tarval_bottom;
995                         return;
996                 }
997                 /* else nothing, constants are the same */
998         }
999         node->type = type;
1000 }  /* compute_Phi */
1001
1002 /**
1003  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1004  *
1005  * @param node  the node
1006  */
1007 static void compute_Add(node_t *node) {
1008         ir_node        *sub = node->node;
1009         node_t         *l   = get_irn_node(get_Add_left(sub));
1010         node_t         *r   = get_irn_node(get_Add_right(sub));
1011         lattice_elem_t a    = l->type;
1012         lattice_elem_t b    = r->type;
1013         node_t         *block = get_irn_node(get_nodes_block(sub));
1014         ir_mode        *mode;
1015
1016         if (block->type.tv == tarval_unreachable) {
1017                 node->type.tv = tarval_top;
1018                 return;
1019         }
1020
1021         if (a.tv == tarval_top || b.tv == tarval_top) {
1022                 node->type.tv = tarval_top;
1023         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1024                 node->type.tv = tarval_bottom;
1025         } else {
1026                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1027                    must call tarval_add() first to handle this case! */
1028                 if (is_tarval(a.tv)) {
1029                         if (is_tarval(b.tv)) {
1030                                 node->type.tv = tarval_add(a.tv, b.tv);
1031                                 return;
1032                         }
1033                         mode = get_tarval_mode(a.tv);
1034                         if (a.tv == get_mode_null(mode)) {
1035                                 node->type = b;
1036                                 return;
1037                         }
1038                 } else if (is_tarval(b.tv)) {
1039                         mode = get_tarval_mode(b.tv);
1040                         if (b.tv == get_mode_null(mode)) {
1041                                 node->type = a;
1042                                 return;
1043                         }
1044                 }
1045                 node->type.tv = tarval_bottom;
1046         }
1047 }  /* compute_Add */
1048
1049 /**
1050  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1051  *
1052  * @param node  the node
1053  */
1054 static void compute_Sub(node_t *node) {
1055         ir_node        *sub = node->node;
1056         node_t         *l   = get_irn_node(get_Sub_left(sub));
1057         node_t         *r   = get_irn_node(get_Sub_right(sub));
1058         lattice_elem_t a    = l->type;
1059         lattice_elem_t b    = r->type;
1060         node_t         *block = get_irn_node(get_nodes_block(sub));
1061
1062         if (block->type.tv == tarval_unreachable) {
1063                 node->type.tv = tarval_top;
1064                 return;
1065         }
1066
1067         if (a.tv == tarval_top || b.tv == tarval_top) {
1068                 node->type.tv = tarval_top;
1069         } else if (r->part == l->part) {
1070                 ir_mode *mode = get_irn_mode(sub);
1071                 node->type.tv = get_mode_null(mode);
1072         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1073                 node->type.tv = tarval_bottom;
1074         } else {
1075                 if (is_tarval(a.tv) && is_tarval(b.tv))
1076                         node->type.tv = tarval_sub(a.tv, b.tv);
1077                 else
1078                         node->type.tv = tarval_bottom;
1079         }
1080 }  /* compute_Sub */
1081
1082 /**
1083  * (Re-)compute the type for a Proj(Cmp).
1084  *
1085  * @param node  the node
1086  * @param cond  the predecessor Cmp node
1087  */
1088 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1089         ir_node        *proj = node->node;
1090         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1091         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1092         lattice_elem_t a     = l->type;
1093         lattice_elem_t b     = r->type;
1094         pn_Cmp         pnc   = get_Proj_proj(proj);
1095
1096         /*
1097          * BEWARE: a == a is NOT always True for floating Point values, as
1098          * NaN != NaN is defined, so we must check this here.
1099          */
1100         if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt ||  pnc == pn_Cmp_Gt) {
1101                 if (a.tv == tarval_top || b.tv == tarval_top) {
1102                         node->type.tv = tarval_top;
1103                 } else if (r->part == l->part) {
1104                         node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1105                 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1106                         node->type.tv = tarval_bottom;
1107                 } else {
1108                         default_compute(node);
1109                 }
1110         } else {
1111                 default_compute(node);
1112         }
1113 }  /* compute_Proj_Cmp */
1114
1115 /**
1116  * (Re-)compute the type for a Proj(Cond).
1117  *
1118  * @param node  the node
1119  * @param cond  the predecessor Cond node
1120  */
1121 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1122         ir_node *proj     = node->node;
1123         long    pnc       = get_Proj_proj(proj);
1124         ir_node *sel      = get_Cond_selector(cond);
1125         node_t  *selector = get_irn_node(sel);
1126
1127         if (get_irn_mode(sel) == mode_b) {
1128                 /* an IF */
1129                 if (pnc == pn_Cond_true) {
1130                         if (selector->type.tv == tarval_b_false) {
1131                                 node->type.tv = tarval_unreachable;
1132                         } else if (selector->type.tv == tarval_b_true) {
1133                                 node->type.tv = tarval_reachable;
1134                         } else if (selector->type.tv == tarval_bottom) {
1135                                 node->type.tv = tarval_reachable;
1136                         } else {
1137                                 assert(selector->type.tv == tarval_top);
1138                                 node->type.tv = tarval_unreachable;
1139                         }
1140                 } else {
1141                         assert(pnc == pn_Cond_false);
1142
1143                         if (selector->type.tv == tarval_b_false) {
1144                                 node->type.tv = tarval_reachable;
1145                         } else if (selector->type.tv == tarval_b_true) {
1146                                 node->type.tv = tarval_unreachable;
1147                         } else if (selector->type.tv == tarval_bottom) {
1148                                 node->type.tv = tarval_reachable;
1149                         } else {
1150                                 assert(selector->type.tv == tarval_top);
1151                                 node->type.tv = tarval_unreachable;
1152                         }
1153                 }
1154         } else {
1155                 /* an SWITCH */
1156                 if (selector->type.tv == tarval_bottom) {
1157                         node->type.tv = tarval_reachable;
1158                 } else if (selector->type.tv == tarval_top) {
1159                         node->type.tv = tarval_unreachable;
1160                 } else {
1161                         long value = get_tarval_long(selector->type.tv);
1162                         if (pnc == get_Cond_defaultProj(cond)) {
1163                                 /* default switch, have to check ALL other cases */
1164                                 int i;
1165
1166                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1167                                         ir_node *succ = get_irn_out(cond, i);
1168
1169                                         if (succ == proj)
1170                                                 continue;
1171                                         if (value == get_Proj_proj(succ)) {
1172                                                 /* we found a match, will NOT take the default case */
1173                                                 node->type.tv = tarval_unreachable;
1174                                                 return;
1175                                         }
1176                                 }
1177                                 /* all cases checked, no match, will take default case */
1178                                 node->type.tv = tarval_reachable;
1179                         } else {
1180                                 /* normal case */
1181                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1182                         }
1183                 }
1184         }
1185 }  /* compute_Proj_Cond */
1186
1187 /**
1188  * (Re-)compute the type for a Proj-Nodes.
1189  *
1190  * @param node  the node
1191  */
1192 static void compute_Proj(node_t *node) {
1193         ir_node *proj = node->node;
1194         ir_mode *mode = get_irn_mode(proj);
1195         ir_node *pred;
1196
1197         if (mode == mode_M) {
1198                 /* mode M is always bottom */
1199                 node->type.tv = tarval_bottom;
1200                 return;
1201         }
1202         if (mode != mode_X) {
1203                 ir_node *cmp = get_Proj_pred(proj);
1204                 if (is_Cmp(cmp))
1205                         compute_Proj_Cmp(node, cmp);
1206                 else
1207                         default_compute(node);
1208                 return;
1209         }
1210         /* handle mode_X nodes */
1211         pred = get_Proj_pred(proj);
1212
1213         switch (get_irn_opcode(pred)) {
1214         case iro_Start:
1215                 /* the Proj_X from the Start is always reachable */
1216                 node->type.tv = tarval_reachable;
1217                 break;
1218         case iro_Cond:
1219                 compute_Proj_Cond(node, pred);
1220                 break;
1221         default:
1222                 default_compute(node);
1223         }
1224 }  /* compute_Proj */
1225
1226 /**
1227  * (Re-)compute the type for a given node.
1228  *
1229  * @param node  the node
1230  */
1231 static void compute(node_t *node) {
1232         compute_func func = (compute_func)node->node->op->ops.generic;
1233
1234         if (func != NULL)
1235                 func(node);
1236 }  /* compute */
1237
1238 /**
1239  * Propagate constant evaluation.
1240  *
1241  * @param env  the environment
1242  */
1243 static void propagate(environment_t *env) {
1244         partition_t    *X, *Y;
1245         node_t         *x;
1246         lattice_elem_t old_type;
1247         node_t         *fallen;
1248         unsigned       n_fallen;
1249         int            i;
1250
1251         while (env->cprop != NULL) {
1252                 /* remove the first partition X from cprop */
1253                 X          = env->cprop;
1254                 X->on_cprop = 0;
1255                 env->cprop = X->cprop_next;
1256
1257                 fallen   = NULL;
1258                 n_fallen = 0;
1259                 while (X->cprop != NULL) {
1260                         /* remove the first Node x from X.cprop */
1261                         x           = X->cprop;
1262                         x->on_cprop = 0;
1263                         X->cprop    = x->cprop_next;
1264
1265                         /* compute a new type for x */
1266                         old_type = x->type;
1267                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1268                         compute(x);
1269                         if (x->type.tv != old_type.tv) {
1270                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1271
1272                                 if (x->on_fallen == 0) {
1273                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
1274                                            not already on the list. */
1275                                         x->next      = fallen;
1276                                         x->on_fallen = 1;
1277                                         fallen       = x;
1278                                         ++n_fallen;
1279                                 }
1280                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1281                                         ir_node *succ = get_irn_out(x->node, i);
1282                                         node_t  *y    = get_irn_node(succ);
1283
1284                                         /* Add y to y.partition.cprop. */
1285                                         add_node_to_cprop(y, env);
1286                                 }
1287                         }
1288                 }
1289
1290                 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1291                         Y = split(X, fallen, env);
1292                 } else {
1293                         Y = X;
1294                 }
1295                 /* remove the nodes from the fallen list */
1296                 for (x = fallen; x != NULL; x = x->next)
1297                         x->on_fallen = 0;
1298
1299                 if (Y->n_nodes > 1)
1300                         split_by(Y, env);
1301         }
1302 }  /* propagate */
1303
1304 /**
1305  * Get the leader for a given node from its congruence class.
1306  *
1307  * @param irn  the node
1308  */
1309 static ir_node *get_leader(node_t *node) {
1310         partition_t *part = node->part;
1311
1312         if (part->n_nodes > 1) {
1313                 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1314
1315                 return get_first_node(part)->node;
1316         }
1317         return node->node;
1318 }
1319
1320 /**
1321  * Post-Walker, apply the analysis results;
1322  */
1323 static void apply_result(ir_node *irn, void *ctx) {
1324         node_t *node = get_irn_node(irn);
1325
1326         (void) ctx;
1327         if (is_Block(irn)) {
1328                 if (irn == get_irg_end_block(current_ir_graph)) {
1329                         /* the EndBlock is always reachable even if the analysis
1330                            finds out the opposite :-) */
1331                         return;
1332                 }
1333
1334                 if (node->type.tv == tarval_unreachable) {
1335                         /* mark dead blocks */
1336                         set_Block_dead(irn);
1337                 }
1338         } else if (is_End(irn)) {
1339                 /* do not touch the End node */
1340         } else {
1341                 node_t *block = get_irn_node(get_nodes_block(irn));
1342
1343                 if (block->type.tv == tarval_unreachable) {
1344                         if (! is_Bad(irn)) {
1345                                 ir_node *bad = get_irg_bad(current_ir_graph);
1346
1347                                 /* here, bad might already have a node, but this can be safely ignored
1348                                    as long as bad has at least ONE valid node */
1349                                 set_irn_node(bad, node);
1350                                 node->node = bad;
1351                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1352                                 exchange(irn, bad);
1353                         }
1354                 }
1355                 else if (get_irn_mode(irn) == mode_X) {
1356                         if (node->type.tv == tarval_unreachable) {
1357                                 ir_node *bad = get_irg_bad(current_ir_graph);
1358
1359                                 /* see comment above */
1360                                 set_irn_node(bad, node);
1361                                 node->node = bad;
1362                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1363                                 exchange(irn, bad);
1364                         }
1365                         else if (is_Proj(irn)) {
1366                                 /* leave or Jmp */
1367                                 ir_node *cond = get_Proj_pred(irn);
1368
1369                                 if (is_Cond(cond)) {
1370                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
1371
1372                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1373                                                 /* Cond selector is a constant, make a Jmp */
1374                                                 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1375                                                 set_irn_node(jmp, node);
1376                                                 node->node = jmp;
1377                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1378                                                 exchange(irn, jmp);
1379                                         }
1380                                 }
1381                         }
1382                 } else {
1383                         /* normal data node */
1384                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1385                                 tarval *tv = node->type.tv;
1386
1387                                 if (! is_Const(irn)) {
1388                                         /* can be replaced by a constant */
1389                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1390                                         set_irn_node(c, node);
1391                                         node->node = c;
1392                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1393                                         exchange(irn, c);
1394                                 }
1395                         } else if (is_entity(node->type.sym.entity_p)) {
1396                                 if (! is_SymConst(irn)) {
1397                                         /* can be replaced by a Symconst */
1398                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1399                                         set_irn_node(symc, node);
1400                                         node->node = symc;
1401
1402                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1403                                         exchange(irn, symc);
1404                                 }
1405                         } else {
1406                                 ir_node *leader = get_leader(node);
1407
1408                                 if (leader != irn) {
1409                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
1410                                         exchange(irn, leader);
1411                                 }
1412                         }
1413                 }
1414         }
1415 }  /* static void apply_result(ir_node *irn, void *ctx) {
1416  */
1417
1418 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1419
1420 /**
1421  * sets the generic functions to compute.
1422  */
1423 static void set_compute_functions(void) {
1424         int i;
1425
1426         /* set the default compute function */
1427         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1428                 ir_op *op = get_irp_opcode(i);
1429                 op->ops.generic = (op_func)default_compute;
1430         }
1431
1432         /* set specific functions */
1433         SET(Block);
1434         SET(Jmp);
1435         SET(Phi);
1436         SET(Add);
1437         SET(Sub);
1438         SET(SymConst);
1439         SET(Proj);
1440         SET(End);
1441 }  /* set_compute_functions */
1442
1443 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1444         ir_node *irn = local != NULL ? local : n;
1445         node_t *node = get_irn_node(irn);
1446
1447         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1448         return 1;
1449 }
1450
1451 void combo(ir_graph *irg) {
1452         environment_t env;
1453         ir_node       *initial_X;
1454         node_t        *start;
1455         ir_graph      *rem = current_ir_graph;
1456
1457         current_ir_graph = irg;
1458
1459         /* register a debug mask */
1460         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1461
1462         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1463
1464         obstack_init(&env.obst);
1465         env.worklist       = NULL;
1466         env.cprop          = NULL;
1467         env.touched        = NULL;
1468         env.initial        = NULL;
1469 #ifdef DEBUG_libfirm
1470         env.dbg_list       = NULL;
1471 #endif
1472         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
1473         env.type2id_map    = pmap_create();
1474         env.end_idx        = get_opt_global_cse() ? 0 : -1;
1475         env.lambda_input   = 0;
1476
1477         assure_irg_outs(irg);
1478
1479         /* we have our own value_of function */
1480         set_value_of_func(get_node_tarval);
1481
1482         set_compute_functions();
1483         DEBUG_ONLY(part_nr = 0);
1484
1485         /* create the initial partition and place it on the work list */
1486         env.initial = new_partition(&env);
1487         add_to_worklist(env.initial, &env);
1488         irg_walk_graph(irg, NULL, create_initial_partitions, &env);
1489
1490         /* Place the START Node's partition on cprop.
1491            Place the START Node on its local worklist. */
1492         initial_X = get_irg_initial_exec(irg);
1493         start     = get_irn_node(initial_X);
1494         add_node_to_cprop(start, &env);
1495
1496         do {
1497                 propagate(&env);
1498                 if (env.worklist != NULL)
1499                         cause_splits(&env);
1500         } while (env.cprop != NULL || env.worklist != NULL);
1501
1502         dump_all_partitions(&env);
1503
1504         set_dump_node_vcgattr_hook(dump_partition_hook);
1505         dump_ir_block_graph(irg, "-partition");
1506         set_dump_node_vcgattr_hook(NULL);
1507
1508
1509         /* apply the result */
1510         irg_walk_graph(irg, NULL, apply_result, &env);
1511
1512         pmap_destroy(env.type2id_map);
1513         del_set(env.opcode2id_map);
1514         obstack_free(&env.obst, NULL);
1515
1516         /* restore value_of() default behavior */
1517         set_value_of_func(NULL);
1518         current_ir_graph = rem;
1519 }  /* combo */