a396f87dfc4357c5216f8f783a8b2bc7fb549478
[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 opcode_entry_t    opcode_entry_t;
62 typedef struct listmap_entry_t   listmap_entry_t;
63
64 /** The type of the compute function. */
65 typedef void (*compute_func)(node_t *node);
66
67 /**
68  * An opcode map key.
69  */
70 struct opcode_key_t {
71         ir_opcode   code;   /**< The Firm opcode. */
72         ir_mode     *mode;  /**< The mode of all nodes in the partition. */
73 };
74
75 /**
76  * An entry in the opcode map.
77  */
78 struct opcode_entry_t {
79         opcode_key_t key;    /**< The key. */
80         partition_t  *part;  /**< The associated partition. */
81 };
82
83 /**
84  * An entry in the list_map.
85  */
86 struct listmap_entry_t {
87         void            *id;    /**< The id. */
88         node_t          *list;  /**< The associated list for this id. */
89         listmap_entry_t *next;  /**< Link to the next entry in the map. */
90 };
91
92 /** We must map id's to lists. */
93 typedef struct listmap_t {
94         set             *map;    /**< Map id's to listmap_entry_t's */
95         listmap_entry_t *values; /**< List of all values in the map. */
96 } listmap_t;
97
98 /**
99  * A lattice element. Because we handle constants and symbolic constants different, we
100  * have to use this union.
101  */
102 typedef union {
103         tarval          *tv;
104         symconst_symbol sym;
105 } lattice_elem_t;
106
107 /**
108  * A node.
109  */
110 struct node_t {
111         ir_node         *node;          /**< The IR-node itself. */
112         list_head       node_list;      /**< Double-linked list of entries. */
113         partition_t     *part;          /**< points to the partition this node belongs to */
114         node_t          *cprop_next;    /**< Next node on partition.cprop list. */
115         node_t          *next;          /**< Next node on local list (partition.touched, fallen). */
116         lattice_elem_t  type;           /**< The associated lattice element "type". */
117         int             max_user_input; /**< Maximum input number of Def-Use edges. */
118         int             next_edge;      /**< Index of the next Def-Use edge to use. */
119         unsigned        on_touched:1;   /**< Set, if this node is on the partition.touched set. */
120         unsigned        on_cprop:1;     /**< Set, if this node is on the partition.cprop list. */
121 };
122
123 /**
124  * A partition containing congruent nodes.
125  */
126 struct partition_t {
127         list_head         entries;         /**< The head of partition node list. */
128         node_t            *cprop;          /**< The partition.cprop list. */
129         partition_t       *wl_next;        /**< Next entry in the work list if any. */
130         partition_t       *touched_next;   /**< Points to the next partition in the touched set. */
131         partition_t       *cprop_next;     /**< Points to the next partition in the cprop list. */
132         node_t            *touched;        /**< The partition.touched set of this partition. */
133         unsigned          n_nodes;         /**< Number of entries in this partition. */
134         unsigned          n_touched;       /**< Number of entries in the partition.touched. */
135         int               max_arity;       /**< Maximum arity of all entries. */
136         int               max_user_inputs; /**< Maximum number of user inputs of all entries. */
137         unsigned          on_worklist:1;   /**< Set, if this partition is in the work list. */
138         unsigned          on_touched:1;    /**< Set, if this partition is on the touched set. */
139         unsigned          on_cprop:1;      /**< Set, if this partition is on the cprop list. */
140 #ifdef DEBUG_libfirm
141         partition_t       *dbg_next;       /**< Link all partitions for debugging */
142         unsigned          nr;              /**< A unique number for (what-)mapping, >0. */
143 #endif
144 };
145
146 typedef struct environment_t {
147         struct obstack  obst;           /**< obstack to allocate data structures. */
148         partition_t     *worklist;      /**< The work list. */
149         partition_t     *cprop;         /**< The constant propagation list. */
150         partition_t     *touched;       /**< the touched set. */
151         partition_t     *initial;       /**< The initial partition. */
152 #ifdef DEBUG_libfirm
153         partition_t     *dbg_list;      /**< List of all partitions. */
154 #endif
155         set             *opcode2id_map; /**< The opcodeMode->id map. */
156         pmap            *type2id_map;   /**< The type->id map. */
157         int             end_idx;        /**< -1 for local and 0 for global congruences. */
158         int             lambda_input;   /**< Captured argument for lambda_partition(). */
159 } environment_t;
160
161 /** Type of the what function. */
162 typedef void *(*what_func)(const node_t *node, environment_t *env);
163
164 #define get_irn_node(irn)         ((node_t *)get_irn_link(irn))
165 #define set_irn_node(irn, node)   set_irn_link(irn, node)
166
167 /** The debug module handle. */
168 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
169
170 /** Next partition number. */
171 DEBUG_ONLY(static unsigned part_nr = 0);
172
173 #ifdef DEBUG_libfirm
174 static lattice_elem_t get_partition_type(const partition_t *X);
175
176 /**
177  * Dump partition to output.
178  */
179 static void dump_partition(const char *msg, const partition_t *part) {
180         const node_t   *node;
181         int            first = 1;
182         lattice_elem_t type = get_partition_type(part);
183
184         DB((dbg, LEVEL_2, "%s part%u (%u, %+F) {\n  ", msg, part->nr, part->n_nodes, type));
185         list_for_each_entry(node_t, node, &part->entries, node_list) {
186                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
187                 first = 0;
188         }
189         DB((dbg, LEVEL_2, "\n}\n"));
190 }
191
192 /**
193  * Dump all partitions.
194  */
195 static void dump_all_partitions(const environment_t *env) {
196         const partition_t *P;
197
198         DB((dbg, LEVEL_2, "All partitions\n===============\n"));
199         for (P = env->dbg_list; P != NULL; P = P->dbg_next)
200                 dump_partition("", P);
201 }
202
203 #else
204 #define dump_partition(msg, part)
205 #define dump_all_partitions(env)
206 #endif
207
208 /**
209  * Compare two pointer values of a listmap.
210  */
211 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
212         const listmap_entry_t *e1 = elt;
213         const listmap_entry_t *e2 = key;
214
215         return e1->id != e2->id;
216 }  /* listmap_cmp_ptr */
217
218 /**
219  * Initializes a listmap.
220  *
221  * @param map  the listmap
222  */
223 static void listmap_init(listmap_t *map) {
224         map->map    = new_set(listmap_cmp_ptr, 16);
225         map->values = NULL;
226 }  /* listmap_init */
227
228 /**
229  * Terminates a listmap.
230  *
231  * @param map  the listmap
232  */
233 static void listmap_term(listmap_t *map) {
234         del_set(map->map);
235 }  /* listmap_term */
236
237 /**
238  * Return the associated listmap entry for a given id.
239  *
240  * @param map  the listmap
241  * @param id   the id to search for
242  *
243  * @return the asociated listmap entry for the given id
244  */
245 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
246         listmap_entry_t key, *entry;
247
248         key.id   = id;
249         key.list = NULL;
250         key.next = NULL;
251         entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
252
253         if (entry->list == NULL) {
254                 /* a new entry, put into the list */
255                 entry->next = map->values;
256                 map->values = entry;
257         }
258         return entry;
259 }  /* listmap_find */
260
261 /**
262  * Calculate the hash value for an opcode map entry.
263  *
264  * @param entry  an opcode map entry
265  *
266  * @return a hash value for the given opcode map entry
267  */
268 static unsigned opcode_hash(const opcode_key_t *entry) {
269         return (entry->mode - (ir_mode *)0) * 9 + entry->code;
270 }  /* opcode_hash */
271
272 /**
273  * Compare two entries in the opcode map.
274  */
275 static int cmp_opcode(const void *elt, const void *key, size_t size) {
276         const opcode_key_t *o1 = elt;
277         const opcode_key_t *o2 = key;
278
279         return o1->code != o2->code || o1->mode != o2->mode;
280 }  /* cmp_opcode */
281
282 /**
283  * Compare two Def-Use edges for input position.
284  */
285 static int cmp_def_use_edge(const void *a, const void *b) {
286         const ir_def_use_edge *ea = a;
287         const ir_def_use_edge *eb = b;
288
289         /* no overrun, because range is [-1, MAXINT] */
290         return ea->pos - eb->pos;
291 }  /* cmp_def_use_edge */
292
293 /**
294  * We need the Def-Use edges sorted.
295  */
296 static void sort_irn_outs(node_t *node) {
297         ir_node *irn = node->node;
298         int n_outs = get_irn_n_outs(irn);
299
300         if (n_outs > 1) {
301                 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
302         }
303         node->max_user_input = irn->out[n_outs + 1].pos;
304 }  /* sort_irn_outs */
305
306 /**
307  * Return the type of a node.
308  *
309  * @param irn  an IR-node
310  *
311  * @return the associated type of this node
312  */
313 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
314         return get_irn_node(irn)->type;
315 }  /* get_node_type */
316
317 /**
318  * Return the tarval of a node.
319  *
320  * @param irn  an IR-node
321  *
322  * @return the associated type of this node
323  */
324 static INLINE tarval *get_node_tarval(const ir_node *irn) {
325         lattice_elem_t type = get_node_type(irn);
326
327         if (is_tarval(type.tv))
328                 return type.tv;
329         return tarval_bottom;
330 }  /* get_node_type */
331
332 /**
333  * Add a partition to the worklist.
334  */
335 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
336         assert(X->on_worklist == 0);
337         X->wl_next     = env->worklist;
338         X->on_worklist = 1;
339         env->worklist  = X;
340 }
341
342 /**
343  * Create a new empty partition.
344  *
345  * @param env   the environment
346  *
347  * @return a newly allocated partition
348  */
349 static INLINE partition_t *new_partition(environment_t *env) {
350         partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
351
352         INIT_LIST_HEAD(&part->entries);
353         part->cprop           = NULL;
354         part->wl_next         = NULL;
355         part->touched_next    = NULL;
356         part->cprop_next      = NULL;
357         part->touched         = NULL;
358         part->n_nodes         = 0;
359         part->n_touched       = 0;
360         part->max_arity       = 0;
361         part->max_user_inputs = 0;
362         part->on_worklist     = 0;
363         part->on_touched      = 0;
364         part->on_cprop        = 0;
365 #ifdef DEBUG_libfirm
366         part->dbg_next        = env->dbg_list;
367         env->dbg_list         = part;
368         part->nr              = part_nr++;
369 #endif
370
371         return part;
372 }  /* new_partition */
373
374 /**
375  * Get the first node from a partition.
376  */
377 static INLINE node_t *get_first_node(const partition_t *X) {
378         return list_entry(X->entries.next, node_t, node_list);
379 }
380
381 /**
382  * Return the type of a partition (assuming partition is non-empty and
383  * all elements have the same type).
384  *
385  * @param X  a partition
386  *
387  * @return the type of the first element of the partition
388  */
389 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
390         const node_t *first = get_first_node(X);
391         return first->type;
392 }  /* get_partition_type */
393
394 /**
395  * Creates a partition node for the given IR-node and place it
396  * into the given partition.
397  *
398  * @param irn   an IR-node
399  * @param part  a partition to place the node in
400  * @param env   the environment
401  *
402  * @return the created node
403  */
404 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
405         /* create a partition node and place it in the partition */
406         node_t *node = obstack_alloc(&env->obst, sizeof(*node));
407         ir_mode *mode = get_irn_mode(irn);
408
409         INIT_LIST_HEAD(&node->node_list);
410         node->node           = irn;
411         node->part           = part;
412         node->cprop_next     = NULL;
413         node->next           = NULL;
414         node->type.tv        = (mode == mode_X || mode == mode_BB) ? tarval_unreachable : tarval_top;
415         node->max_user_input = 0;
416         node->next_edge      = 0;
417         node->on_touched     = 0;
418         node->on_cprop       = 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(x->node) || is_live_input(x->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
758         key.code = get_irn_opcode(node->node);
759         key.mode = get_irn_mode(node->node);
760         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
761         return entry;
762 }  /* lambda_opcode */
763
764 /** lambda n.(n[i].partition) */
765 static void *lambda_partition(const node_t *node, environment_t *env) {
766         ir_node *pred;
767         node_t  *p;
768         int     i = env->lambda_input;
769
770         if (i >= get_irn_arity(node->node)) {
771                 /* we are outside the allowed range */
772                 return NULL;
773         }
774
775         /* ignore the "control input" for non-pinned nodes
776            if we are running in GCSE mode */
777         if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
778                 return NULL;
779
780         pred = get_irn_n(node->node, i);
781         p    = get_irn_node(pred);
782
783         return p->part;
784 }  /* lambda_partition */
785
786 /**
787  * Checks whether a type is a constant.
788  */
789 static int is_type_constant(lattice_elem_t type) {
790         if (is_tarval(type.tv))
791                 return tarval_is_constant(type.tv);
792         /* else it is a symconst */
793         return 1;
794 }
795
796 /**
797  * Implements split_by().
798  *
799  * @param X    the partition to split
800  * @param env  the environment
801  */
802 static void split_by(partition_t *X, environment_t *env) {
803         partition_t **P = NEW_ARR_F(partition_t *, 0);
804         int         i, j, k;
805
806         P = split_by_what(X, lambda_type, P, env);
807         for (i = ARR_LEN(P) - 1; i >= 0; --i) {
808                 partition_t    *Y = P[i];
809
810                 if (Y->n_nodes > 1) {
811                         lattice_elem_t type = get_partition_type(Y);
812
813                         /* we do not want split the TOP, unreachable or constant partitions */
814                         if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
815                                 partition_t **Q = NEW_ARR_F(partition_t *, 0);
816
817                                 Q = split_by_what(Y, lambda_opcode, Q, env);
818
819                                 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
820                                         partition_t *Z = Q[j];
821
822                                         for (k = Z->max_arity - 1; k >= -1; --k) {
823                                                 if (Z->n_nodes > 1) {
824                                                         env->lambda_input = k;
825                                                         split_by_what(Z, lambda_partition, NULL, env);
826                                                 }
827                                         }
828                                 }
829                                 DEL_ARR_F(Q);
830                         }
831                 }
832         }
833         DEL_ARR_F(P);
834 }  /* split_by */
835
836 /**
837  * (Re-)compute the type for a given node.
838  *
839  * @param node  the node
840  */
841 static void default_compute(node_t *node) {
842         int     i;
843         ir_node *irn = node->node;
844         tarval  *top = tarval_top;
845
846         if (get_irn_mode(node->node) == mode_X)
847                 top = tarval_unreachable;
848
849         if (get_irn_pinned(irn) == op_pin_state_pinned) {
850                 node_t *block = get_irn_node(get_nodes_block(irn));
851
852                 if (block->type.tv == tarval_unreachable) {
853                         node->type.tv = top;
854                         return;
855                 }
856         }
857
858         /* if any of the data inputs have type top, the result is type top */
859         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
860                 ir_node *pred = get_irn_n(irn, i);
861                 node_t  *p    = get_irn_node(pred);
862
863                 if (p->type.tv == tarval_top) {
864                         node->type.tv = top;
865                         return;
866                 }
867         }
868
869         if (get_irn_mode(node->node) == mode_X)
870                 node->type.tv = tarval_reachable;
871         else
872                 node->type.tv = computed_value(irn);
873 }  /* default_compute */
874
875 /**
876  * (Re-)compute the type for a Block node.
877  *
878  * @param node  the node
879  */
880 static void compute_Block(node_t *node) {
881         int     i;
882         ir_node *block = node->node;
883
884         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
885                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
886
887                 if (pred->type.tv == tarval_reachable) {
888                         /* A block is reachable, if at least of predecessor is reachable. */
889                         node->type.tv = tarval_reachable;
890                         return;
891                 }
892         }
893         node->type.tv = tarval_unreachable;
894 }  /* compute_Block */
895
896 /**
897  * (Re-)compute the type for a Jmp node.
898  *
899  * @param node  the node
900  */
901 static void compute_Jmp(node_t *node) {
902         node_t *block = get_irn_node(get_nodes_block(node->node));
903
904         node->type = block->type;
905 }  /* compute_Jmp */
906
907 /**
908  * (Re-)compute the type for the End node.
909  *
910  * @param node  the node
911  */
912 static void compute_End(node_t *node) {
913         /* the End node is NOT dead of course */
914         node->type.tv = tarval_reachable;
915 }
916
917 /**
918  * (Re-)compute the type for a SymConst node.
919  *
920  * @param node  the node
921  */
922 static void compute_SymConst(node_t *node) {
923         ir_node *irn = node->node;
924         node_t  *block = get_irn_node(get_nodes_block(irn));
925
926         if (block->type.tv == tarval_unreachable) {
927                 node->type.tv = tarval_top;
928                 return;
929         }
930         switch (get_SymConst_kind(irn)) {
931         case symconst_addr_ent:
932         /* case symconst_addr_name: cannot handle this yet */
933                 node->type.sym = get_SymConst_symbol(irn);
934                 break;
935         default:
936                 node->type.tv = computed_value(irn);
937         }
938 }  /* compute_SymConst */
939
940 /**
941  * (Re-)compute the type for a Phi node.
942  *
943  * @param node  the node
944  */
945 static void compute_Phi(node_t *node) {
946         int            i;
947         ir_node        *phi = node->node;
948         lattice_elem_t type;
949
950         /* if a Phi is in a unreachable block, its type is TOP */
951         node_t *block = get_irn_node(get_nodes_block(phi));
952
953         if (block->type.tv == tarval_unreachable) {
954                 node->type.tv = tarval_top;
955                 return;
956         }
957
958         /* Phi implements the Meet operation */
959         type.tv = tarval_top;
960         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
961                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
962                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
963
964                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
965                         /* ignore TOP inputs: We must check here for unreachable blocks,
966                            because Firm constants live in the Start Block are NEVER Top.
967                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
968                            comes from a unreachable input. */
969                         continue;
970                 }
971                 if (pred->type.tv == tarval_bottom) {
972                         node->type.tv = tarval_bottom;
973                         return;
974                 } else if (type.tv == tarval_top) {
975                         /* first constant found */
976                         type = pred->type;
977                 } else if (type.tv != pred->type.tv) {
978                         /* different constants or tarval_bottom */
979                         node->type.tv = tarval_bottom;
980                         return;
981                 }
982                 /* else nothing, constants are the same */
983         }
984         node->type = type;
985 }  /* compute_Phi */
986
987 /**
988  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
989  *
990  * @param node  the node
991  */
992 static void compute_Add(node_t *node) {
993         ir_node        *sub = node->node;
994         node_t         *l   = get_irn_node(get_Add_left(sub));
995         node_t         *r   = get_irn_node(get_Add_right(sub));
996         lattice_elem_t a    = l->type;
997         lattice_elem_t b    = r->type;
998         node_t         *block = get_irn_node(get_nodes_block(sub));
999         ir_mode        *mode;
1000
1001         if (block->type.tv == tarval_unreachable) {
1002                 node->type.tv = tarval_top;
1003                 return;
1004         }
1005
1006         if (a.tv == tarval_top || b.tv == tarval_top) {
1007                 node->type.tv = tarval_top;
1008         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1009                 node->type.tv = tarval_bottom;
1010         } else {
1011                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1012                    must call tarval_add() first to handle this case! */
1013                 if (is_tarval(a.tv)) {
1014                         if (is_tarval(b.tv)) {
1015                                 node->type.tv = tarval_add(a.tv, b.tv);
1016                                 return;
1017                         }
1018                         mode = get_tarval_mode(a.tv);
1019                         if (a.tv == get_mode_null(mode)) {
1020                                 node->type = b;
1021                                 return;
1022                         }
1023                 } else if (is_tarval(b.tv)) {
1024                         mode = get_tarval_mode(b.tv);
1025                         if (b.tv == get_mode_null(mode)) {
1026                                 node->type = a;
1027                                 return;
1028                         }
1029                 }
1030                 node->type.tv = tarval_bottom;
1031         }
1032 }  /* compute_Add */
1033
1034 /**
1035  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1036  *
1037  * @param node  the node
1038  */
1039 static void compute_Sub(node_t *node) {
1040         ir_node        *sub = node->node;
1041         node_t         *l   = get_irn_node(get_Sub_left(sub));
1042         node_t         *r   = get_irn_node(get_Sub_right(sub));
1043         lattice_elem_t a    = l->type;
1044         lattice_elem_t b    = r->type;
1045         node_t         *block = get_irn_node(get_nodes_block(sub));
1046
1047         if (block->type.tv == tarval_unreachable) {
1048                 node->type.tv = tarval_top;
1049                 return;
1050         }
1051
1052         if (a.tv == tarval_top || b.tv == tarval_top) {
1053                 node->type.tv = tarval_top;
1054         } else if (r->part == l->part) {
1055                 ir_mode *mode = get_irn_mode(sub);
1056                 node->type.tv = get_mode_null(mode);
1057         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1058                 node->type.tv = tarval_bottom;
1059         } else {
1060                 if (is_tarval(a.tv) && is_tarval(b.tv))
1061                         node->type.tv = tarval_sub(a.tv, b.tv);
1062                 else
1063                         node->type.tv = tarval_bottom;
1064         }
1065 }  /* compute_Sub */
1066
1067 /**
1068  * (Re-)compute the type for a Proj(Cmp).
1069  *
1070  * @param node  the node
1071  * @param cond  the predecessor Cmp node
1072  */
1073 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1074         ir_node        *proj = node->node;
1075         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1076         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1077         lattice_elem_t a     = l->type;
1078         lattice_elem_t b     = r->type;
1079         pn_Cmp         pnc   = get_Proj_proj(proj);
1080
1081         /*
1082          * BEWARE: a == a is NOT always True for floating Point values, as
1083          * NaN != NaN is defined, so we must check this here.
1084          */
1085         if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt ||  pnc == pn_Cmp_Gt) {
1086                 if (a.tv == tarval_top || b.tv == tarval_top) {
1087                         node->type.tv = tarval_top;
1088                 } else if (r->part == l->part) {
1089                         node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1090                 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1091                         node->type.tv = tarval_bottom;
1092                 } else {
1093                         default_compute(node);
1094                 }
1095         } else {
1096                 default_compute(node);
1097         }
1098 }  /* compute_Proj_Cmp */
1099
1100 /**
1101  * (Re-)compute the type for a Proj(Cond).
1102  *
1103  * @param node  the node
1104  * @param cond  the predecessor Cond node
1105  */
1106 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1107         ir_node *proj     = node->node;
1108         long    pnc       = get_Proj_proj(proj);
1109         ir_node *sel      = get_Cond_selector(cond);
1110         node_t  *selector = get_irn_node(sel);
1111
1112         if (get_irn_mode(sel) == mode_b) {
1113                 /* an IF */
1114                 if (pnc == pn_Cond_true) {
1115                         if (selector->type.tv == tarval_b_false) {
1116                                 node->type.tv = tarval_unreachable;
1117                         } else if (selector->type.tv == tarval_b_true) {
1118                                 node->type.tv = tarval_reachable;
1119                         } else if (selector->type.tv == tarval_bottom) {
1120                                 node->type.tv = tarval_reachable;
1121                         } else {
1122                                 assert(selector->type.tv == tarval_top);
1123                                 node->type.tv = tarval_unreachable;
1124                         }
1125                 } else {
1126                         assert(pnc == pn_Cond_false);
1127
1128                         if (selector->type.tv == tarval_b_false) {
1129                                 node->type.tv = tarval_reachable;
1130                         } else if (selector->type.tv == tarval_b_true) {
1131                                 node->type.tv = tarval_unreachable;
1132                         } else if (selector->type.tv == tarval_bottom) {
1133                                 node->type.tv = tarval_reachable;
1134                         } else {
1135                                 assert(selector->type.tv == tarval_top);
1136                                 node->type.tv = tarval_unreachable;
1137                         }
1138                 }
1139         } else {
1140                 /* an SWITCH */
1141                 if (selector->type.tv == tarval_bottom) {
1142                         node->type.tv = tarval_reachable;
1143                 } else if (selector->type.tv == tarval_top) {
1144                         node->type.tv = tarval_unreachable;
1145                 } else {
1146                         long value = get_tarval_long(selector->type.tv);
1147                         if (pnc == get_Cond_defaultProj(cond)) {
1148                                 /* default switch, have to check ALL other cases */
1149                                 int i;
1150
1151                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1152                                         ir_node *succ = get_irn_out(cond, i);
1153
1154                                         if (succ == proj)
1155                                                 continue;
1156                                         if (value == get_Proj_proj(succ)) {
1157                                                 /* we found a match, will NOT take the default case */
1158                                                 node->type.tv = tarval_unreachable;
1159                                                 return;
1160                                         }
1161                                 }
1162                                 /* all cases checked, no match, will take default case */
1163                                 node->type.tv = tarval_reachable;
1164                         } else {
1165                                 /* normal case */
1166                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1167                         }
1168                 }
1169         }
1170 }  /* compute_Proj_Cond */
1171
1172 /**
1173  * (Re-)compute the type for a Proj-Nodes.
1174  *
1175  * @param node  the node
1176  */
1177 static void compute_Proj(node_t *node) {
1178         ir_node *proj = node->node;
1179         ir_mode *mode = get_irn_mode(proj);
1180         ir_node *pred;
1181
1182         if (mode == mode_M) {
1183                 /* mode M is always bottom */
1184                 node->type.tv = tarval_bottom;
1185                 return;
1186         }
1187         if (mode != mode_X) {
1188                 ir_node *cmp = get_Proj_pred(proj);
1189                 if (is_Cmp(cmp))
1190                         compute_Proj_Cmp(node, cmp);
1191                 else
1192                         default_compute(node);
1193                 return;
1194         }
1195         /* handle mode_X nodes */
1196         pred = get_Proj_pred(proj);
1197
1198         switch (get_irn_opcode(pred)) {
1199         case iro_Start:
1200                 /* the Proj_X from the Start is always reachable */
1201                 node->type.tv = tarval_reachable;
1202                 break;
1203         case iro_Cond:
1204                 compute_Proj_Cond(node, pred);
1205                 break;
1206         default:
1207                 default_compute(node);
1208         }
1209 }  /* compute_Proj */
1210
1211 /**
1212  * (Re-)compute the type for a given node.
1213  *
1214  * @param node  the node
1215  */
1216 static void compute(node_t *node) {
1217         compute_func func = (compute_func)node->node->op->ops.generic;
1218
1219         if (func != NULL)
1220                 func(node);
1221 }  /* compute */
1222
1223 /**
1224  * Propagate constant evaluation.
1225  *
1226  * @param env  the environment
1227  */
1228 static void propagate(environment_t *env) {
1229         partition_t    *X, *Y;
1230         node_t         *x;
1231         lattice_elem_t old_type;
1232         node_t         *fallen;
1233         unsigned       n_fallen;
1234         int            i;
1235
1236         while (env->cprop != NULL) {
1237                 /* remove the first partition X from cprop */
1238                 X          = env->cprop;
1239                 X->on_cprop = 0;
1240                 env->cprop = X->cprop_next;
1241
1242                 fallen   = NULL;
1243                 n_fallen = 0;
1244                 while (X->cprop != NULL) {
1245                         /* remove the first Node x from X.cprop */
1246                         x           = X->cprop;
1247                         x->on_cprop = 0;
1248                         X->cprop    = x->cprop_next;
1249
1250                         /* compute a new type for x */
1251                         old_type = x->type;
1252                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1253                         compute(x);
1254                         if (x->type.tv != old_type.tv) {
1255                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1256
1257                                 /* Add x to fallen. */
1258                                 x->next      = fallen;
1259                                 fallen       = x;
1260                                 ++n_fallen;
1261
1262                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1263                                         ir_node *succ = get_irn_out(x->node, i);
1264                                         node_t  *y    = get_irn_node(succ);
1265
1266                                         /* Add y to y.partition.cprop. */
1267                                         add_node_to_cprop(y, env);
1268                                 }
1269                         }
1270                 }
1271
1272                 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1273                         Y = split(X, fallen, env);
1274                 } else {
1275                         Y = X;
1276                 }
1277                 if (Y->n_nodes > 1)
1278                         split_by(Y, env);
1279         }
1280 }  /* propagate */
1281
1282 /**
1283  * Get the leader for a given node from its congruence class.
1284  *
1285  * @param irn  the node
1286  */
1287 static ir_node *get_leader(node_t *node) {
1288         partition_t *part = node->part;
1289
1290         if (part->n_nodes > 1) {
1291                 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1292
1293                 return get_first_node(part)->node;
1294         }
1295         return node->node;
1296 }
1297
1298 /**
1299  * Post-Walker, apply the analysis results;
1300  */
1301 static void apply_result(ir_node *irn, void *ctx) {
1302         environment_t *env = ctx;
1303         node_t        *node = get_irn_node(irn);
1304
1305         if (is_Block(irn)) {
1306                 if (irn == get_irg_end_block(current_ir_graph)) {
1307                         /* the EndBlock is always reachable even if the analysis
1308                            finds out the opposite :-) */
1309                         return;
1310                 }
1311
1312                 if (node->type.tv == tarval_unreachable) {
1313                         /* mark dead blocks */
1314                         set_Block_dead(irn);
1315                 }
1316         } else if (is_End(irn)) {
1317                 /* do not touch the End node */
1318         } else {
1319                 node_t *block = get_irn_node(get_nodes_block(irn));
1320
1321                 if (block->type.tv == tarval_unreachable) {
1322                         ir_node *bad = get_irg_bad(current_ir_graph);
1323
1324                         /* here, bad might already have a node, but this can be safely ignored
1325                            as long as bad has at least ONE valid node */
1326                         set_irn_node(bad, node);
1327                         node->node = bad;
1328                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1329                         exchange(irn, bad);
1330                 }
1331                 else if (get_irn_mode(irn) == mode_X) {
1332                         if (node->type.tv == tarval_unreachable) {
1333                                 ir_node *bad = get_irg_bad(current_ir_graph);
1334
1335                                 /* see comment above */
1336                                 set_irn_node(bad, node);
1337                                 node->node = bad;
1338                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1339                                 exchange(irn, get_irg_bad(current_ir_graph));
1340                         }
1341                         else if (is_Proj(irn)) {
1342                                 /* leave or Jmp */
1343                                 ir_node *cond = get_Proj_pred(irn);
1344
1345                                 if (is_Cond(cond)) {
1346                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
1347
1348                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1349                                                 /* Cond selector is a constant, make a Jmp */
1350                                                 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1351                                                 set_irn_node(jmp, node);
1352                                                 node->node = jmp;
1353                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1354                                                 exchange(irn, jmp);
1355                                         }
1356                                 }
1357                         }
1358                 } else {
1359                         /* normal data node */
1360                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1361                                 tarval *tv = node->type.tv;
1362
1363                                 if (! is_Const(irn)) {
1364                                         /* can be replaced by a constant */
1365                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1366                                         set_irn_node(c, node);
1367                                         node->node = c;
1368                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1369                                         exchange(irn, c);
1370                                 }
1371                         } else if (is_entity(node->type.sym.entity_p)) {
1372                                 if (! is_SymConst(irn)) {
1373                                         /* can be replaced by a Symconst */
1374                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1375                                         set_irn_node(symc, node);
1376                                         node->node = symc;
1377
1378                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1379                                         exchange(irn, symc);
1380                                 }
1381                         } else {
1382                                 ir_node *leader = get_leader(node);
1383
1384                                 if (leader != irn) {
1385                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, leader));
1386                                         exchange(irn, leader);
1387                                 }
1388                         }
1389                 }
1390         }
1391 }  /* static void apply_result(ir_node *irn, void *ctx) {
1392  */
1393
1394 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1395
1396 /**
1397  * sets the generic functions to compute.
1398  */
1399 static void set_compute_functions(void) {
1400         int i;
1401
1402         /* set the default compute function */
1403         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1404                 ir_op *op = get_irp_opcode(i);
1405                 op->ops.generic = (op_func)default_compute;
1406         }
1407
1408         /* set specific functions */
1409         SET(Block);
1410         SET(Jmp);
1411         SET(Phi);
1412         SET(Add);
1413         SET(Sub);
1414         SET(SymConst);
1415         SET(Proj);
1416         SET(End);
1417 }  /* set_compute_functions */
1418
1419 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1420         ir_node *irn = local != NULL ? local : n;
1421         node_t *node = get_irn_node(n);
1422
1423         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1424         return 1;
1425 }
1426
1427 void combo(ir_graph *irg) {
1428         environment_t env;
1429         ir_node       *initial_X;
1430         node_t        *start;
1431         ir_graph      *rem = current_ir_graph;
1432
1433         current_ir_graph = irg;
1434
1435         /* register a debug mask */
1436         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1437         firm_dbg_set_mask(dbg, SET_LEVEL_1);
1438
1439         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1440
1441         obstack_init(&env.obst);
1442         env.worklist       = NULL;
1443         env.cprop          = NULL;
1444         env.touched        = NULL;
1445         env.initial        = NULL;
1446 #ifdef DEBUG_libfirm
1447         env.dbg_list       = NULL;
1448 #endif
1449         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
1450         env.type2id_map    = pmap_create();
1451         env.end_idx        = get_opt_global_cse() ? 0 : -1;
1452         env.lambda_input   = 0;
1453
1454         assure_irg_outs(irg);
1455
1456         /* we have our own value_of function */
1457         set_value_of_func(get_node_tarval);
1458
1459         set_compute_functions();
1460
1461         /* create the initial partition and place it on the work list */
1462         env.initial = new_partition(&env);
1463         add_to_worklist(env.initial, &env);
1464         irg_walk_graph(irg, NULL, create_initial_partitions, &env);
1465
1466         /* Place the START Node's partition on cprop.
1467            Place the START Node on its local worklist. */
1468         initial_X = get_irg_initial_exec(irg);
1469         start     = get_irn_node(initial_X);
1470         add_node_to_cprop(start, &env);
1471
1472         do {
1473                 propagate(&env);
1474                 if (env.worklist != NULL)
1475                         cause_splits(&env);
1476         } while (env.cprop != NULL || env.worklist != NULL);
1477
1478         dump_all_partitions(&env);
1479
1480         set_dump_node_vcgattr_hook(dump_partition_hook);
1481         dump_ir_block_graph(irg, "-partition");
1482         set_dump_node_vcgattr_hook(NULL);
1483
1484
1485         /* apply the result */
1486         irg_walk_graph(irg, NULL, apply_result, &env);
1487
1488         pmap_destroy(env.type2id_map);
1489         del_set(env.opcode2id_map);
1490         obstack_free(&env.obst, NULL);
1491
1492         /* restore value_of() default behavior */
1493         set_value_of_func(NULL);
1494         current_ir_graph = rem;
1495 }  /* combo */