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