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