71b8a8c21752be2a63ec38e64d331fcb682d40e3
[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 "list.h"
39 #include "array.h"
40 #include "set.h"
41 #include "pmap.h"
42 #include "obstack.h"
43 #include "irgraph_t.h"
44 #include "irnode_t.h"
45 #include "iropt_t.h"
46 #include "irgwalk.h"
47 #include "irop.h"
48 #include "irouts.h"
49 #include "irgmod.h"
50 #include "debug.h"
51
52 /* we need the tarval_R and tarval_U */
53 #define tarval_U  tarval_undefined
54 #define tarval_R  tarval_bad
55
56 typedef struct node_t            node_t;
57 typedef struct partition_t       partition_t;
58 typedef struct opcode_key_t      opcode_key_t;
59 typedef struct opcode_entry_t    opcode_entry_t;
60 typedef struct opcode2id_entry_t opcode2id_entry_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 };
73
74 /**
75  * An entry in the opcode map.
76  */
77 struct opcode_entry_t {
78         opcode_key_t key;    /**< The key. */
79         partition_t  *part;  /**< The associated partition. */
80 };
81
82 /**
83  * An entry in the opcode map2id.
84  */
85 struct opcode2id_entry_t {
86         opcode_key_t key;    /**< The key. */
87         unsigned     id;     /**< The associated id. */
88 };
89
90 /**
91  * An entry in the list_map.
92  */
93 struct listmap_entry_t {
94         unsigned        id;     /**< The id. */
95         node_t          *list;  /**< The associated list for this id. */
96         listmap_entry_t *next;  /**< Link to the next entry in the map. */
97 };
98
99 /** We must map id's to lists. */
100 typedef struct listmap_t {
101         set             *map;    /**< Map id's to listmap_entry_t's */
102         listmap_entry_t *values; /**< List of all values in the map. */
103 } listmap_t;
104
105
106 /**
107  * A node.
108  */
109 struct node_t {
110         ir_node     *node;         /**< The IR-node itself. */
111         list_head   node_list;     /**< Double-linked list of entries. */
112         partition_t *part;         /**< points to the partition this node belongs to */
113         node_t      *cprop_next;   /**< Next node on partition.cprop list. */
114         node_t      *next;         /**< Next node on local list (partition.touched, fallen). */
115         tarval      *type;         /**< The associated lattice element "type". */
116         unsigned    on_touched:1;  /**< Set, if this node is on the partition.touched set. */
117         unsigned    on_cprop:1;    /**< Set, if this node is on the partition.cprop list. */
118 };
119
120 /**
121  * A partition containing congruent nodes.
122  */
123 struct partition_t {
124         list_head         entries;       /**< The head of partition node list. */
125         node_t            *cprop;        /**< The partition.cprop list. */
126         partition_t       *wl_next;      /**< Next entry in the work list if any. */
127         partition_t       *touched_next; /**< Points to the next partition in the touched set. */
128         partition_t       *cprop_next;   /**< Points to the next partition in the cprop list. */
129         node_t            *touched;      /**< The partition.touched set of this partition. */
130         unsigned          n_nodes;       /**< Number of entries in this partition. */
131         unsigned          n_touched;     /**< Number of entries in the partition.touched. */
132         int               n_inputs;      /**< Maximum number of inputs of all entries. */
133         unsigned          on_worklist:1; /**< Set, if this partition is in the work list. */
134         unsigned          on_touched:1;  /**< Set, if this partition is on the touched set. */
135         unsigned          nr;            /**< A unique number for (what-)mapping, >0. */
136 };
137
138 typedef struct environment_t {
139         struct obstack  obst;           /**< obstack to allocate data structures. */
140         partition_t     *worklist;      /**< The work list. */
141         partition_t     *cprop;         /**< The constant propagation list. */
142         partition_t     *touched;       /**< the touched set. */
143         partition_t     *TOP;           /**< The TOP partition. */
144         set             *opcode_map;    /**< The initial opcode->partition map. */
145         set             *opcode2id_map; /**< The opcodeMode->id map. */
146         pmap            *type2id_map;   /**< The type->id map. */
147         int             end_idx;        /**< -1 for local and 0 for global congruences. */
148         int             lambda_input;   /**< Captured argument for lambda_partition(). */
149         int             next_opcode_id; /**< Next ID for the opcode2id map. */
150         int             next_type_id;   /**< Next ID for the type2id map. */
151 } environment_t;
152
153 /** Type of the what function. */
154 typedef unsigned (*what_func)(const node_t *node, environment_t *env);
155
156 #define get_irn_node(irn)         ((node_t *)get_irn_link(irn))
157 #define set_irn_node(irn, node)   set_irn_link(irn, node)
158
159 /** The debug module handle. */
160 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
161
162 /** Next partition number, 0 has special meaning. */
163 static unsigned part_nr = 1;
164
165 #ifdef DEBUG_libfirm
166 /**
167  * Dump partition to output.
168  */
169 static void dump_partition(const char *msg, partition_t *part) {
170         node_t *node;
171         int    first = 1;
172
173         DB((dbg, LEVEL_2, "%s part%u (%u) {\n  ", msg, part->nr, part->n_nodes));
174         list_for_each_entry(node_t, node, &part->entries, node_list) {
175                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
176                 first = 0;
177         }
178         DB((dbg, LEVEL_2, "}\n"));
179 }
180 #else
181 #define dump_partition(msg, part)
182 #endif
183
184 /**
185  * compare two pointer values.
186  */
187 static int cmp_ptr(const void *elt, const void *key, size_t size) {
188         const listmap_entry_t *e1 = elt;
189         const listmap_entry_t *e2 = key;
190
191         return e1->id != e2->id;
192 }
193
194 /**
195  * Creates a new listmap.
196  */
197 static void new_listmap(listmap_t *map) {
198         map->map    = new_set(cmp_ptr, 16);
199         map->values = NULL;
200 }
201
202 /**
203  * Deletes a listmap.
204  */
205 static void del_listmap(listmap_t *map) {
206         del_set(map->map);
207 }
208
209 /**
210  * Return the associated listmap entry for a given id.
211  */
212 static listmap_entry_t *listmap_find(listmap_t *map, unsigned id) {
213         listmap_entry_t key, *entry;
214
215         key.id   = id;
216         key.list = NULL;
217         key.next = NULL;
218         entry = set_insert(map->map, &key, sizeof(key), id);
219
220         if (entry->list == NULL) {
221                 /* a new entry, put into the list */
222                 entry->next = map->values;
223                 map->values = entry;
224         }
225         return entry;
226 }
227
228 /**
229  * calculate the hash value for an opcode map entry.
230  */
231 static unsigned opcode_hash(const opcode_key_t *entry) {
232         return (entry->mode - (ir_mode *)0) * 9 + entry->code;
233 }
234
235 /**
236  * Compare two entries in the opcode map.
237  */
238 static int cmp_opcode(const void *elt, const void *key, size_t size) {
239         const opcode_key_t *o1 = elt;
240         const opcode_key_t *o2 = key;
241
242         return o1->code != o2->code || o1->mode != o2->mode;
243 }
244
245 /** Return the type of a node. */
246 static INLINE tarval *get_node_type(const ir_node *irn) {
247         return get_irn_node(irn)->type;
248 }
249
250 /**
251  * Create a new empty partition.
252  */
253 static INLINE partition_t *new_partition(environment_t *env) {
254         partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
255
256         INIT_LIST_HEAD(&part->entries);
257         part->cprop        = NULL;
258         part->wl_next      = env->worklist;
259         part->touched_next = NULL;
260         part->cprop_next   = NULL;
261         part->touched      = NULL;
262         part->n_nodes      = 0;
263         part->n_touched    = 0;
264         part->n_inputs     = 0;
265         part->on_worklist  = 0;
266         part->on_touched   = 0;
267         part->nr           = part_nr++;
268
269         return part;
270 }
271
272 /**
273  * Get the partition for a given opcode.
274  */
275 static INLINE partition_t *get_partition_for_irn(const ir_node *irn, environment_t *env) {
276         opcode_entry_t key, *entry;
277         unsigned       hash;
278
279         key.key.code = get_irn_opcode(irn);
280         key.key.mode = get_irn_mode(irn);
281         hash         = opcode_hash(&key.key);
282
283         entry = set_find(env->opcode_map, &key, sizeof(key), hash);
284         if (entry == NULL) {
285                 /* create a new partition and place it on the wait queue */
286                 partition_t *part = new_partition(env);
287
288                 part->on_worklist = 1;
289                 env->worklist     = part;
290
291                 key.part = part;
292                 set_insert(env->opcode_map, &key, sizeof(key), hash);
293                 entry = &key;
294         }
295         return entry->part;
296 }
297
298 /**
299  * Creates a partition node for the given IR-node and place it
300  * into the given partition.
301  */
302 static void create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
303         /* create a partition node and place it in the partition */
304         node_t *node = obstack_alloc(&env->obst, sizeof(*node));
305
306         INIT_LIST_HEAD(&node->node_list);
307         node->node         = irn;
308         node->part         = part;
309         node->cprop_next   = NULL;
310         node->next         = NULL;
311         node->type         = tarval_top; /* == tarval_U */
312         node->on_touched   = 0;
313         node->on_cprop     = 0;
314         set_irn_node(irn, node);
315
316         list_add_tail(&node->node_list, &part->entries);
317         ++part->n_nodes;
318
319         DB((dbg, LEVEL_2, "Placing %+F in partition %u\n", irn, part->nr));
320 }
321
322 /**
323  * Walker, initialize all Nodes' type to U or top and place
324  * all nodes into the TOP partition.
325  */
326 static void create_initial_partitions(ir_node *irn, void *ctx) {
327         environment_t *env  = ctx;
328         partition_t   *part = env->TOP;
329         int           arity;
330
331         create_partition_node(irn, part, env);
332         arity = get_irn_arity(irn);
333         if (arity > part->n_inputs)
334                 part->n_inputs = arity;
335 }
336
337 /**
338  * Add a partition to the touched set if not already there.
339  */
340 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
341         if (part->on_touched == 0) {
342                 part->touched_next = env->touched;
343                 env->touched       = part;
344                 part->on_touched   = 1;
345         }
346 }
347
348 /**
349  * Add a node to the entry.partition.touched set if not already there..
350  */
351 static INLINE void add_to_partition_touched(node_t *y) {
352         if (y->on_touched == 0) {
353                 partition_t *part = y->part;
354
355                 y->next       = part->touched;
356                 part->touched = y;
357                 y->on_touched = 1;
358                 ++part->n_touched;
359         }
360 }
361
362 /**
363  * update the worklist
364  */
365 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
366         /* If Z is on worklist then add Z' to worklist.
367            Else add the smaller of Z and Z' to worklist. */
368         if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
369                 Z_prime->on_worklist = 1;
370                 Z_prime->wl_next     = env->worklist;
371                 env->worklist        = Z_prime;
372         } else {
373                 Z->on_worklist = 1;
374                 Z->wl_next     = env->worklist;
375                 env->worklist  = Z;
376         }
377 }
378
379 /**
380  * Split a partition by a local list.
381  */
382 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
383         partition_t *Z_prime;
384         node_t      *node;
385         unsigned    n = 0;
386         int         n_inputs;
387
388         dump_partition("Splitting ", Z);
389
390         /* Remove g from Z. */
391         for (node = g; node != NULL; node = node->next) {
392                 list_del(&node->node_list);
393                 ++n;
394         }
395         Z->n_nodes -= n;
396
397         /* Move g to a new partition, Z\92. */
398         Z_prime = new_partition(env);
399         n_inputs = 0;
400         for (node = g; node != NULL; node = node->next) {
401                 int arity = get_irn_arity(node->node);
402                 list_add(&node->node_list, &Z_prime->entries);
403                 node->part = Z_prime;
404                 if (arity > n_inputs)
405                         n_inputs = arity;
406         }
407         Z_prime->n_inputs = n_inputs;
408         Z_prime->n_nodes  = n;
409
410         update_worklist(Z, Z_prime, env);
411
412         dump_partition("Now ", Z);
413         dump_partition("Created new ", Z_prime);
414         return Z_prime;
415 }
416
417 /**
418  * Returns non-zero if the i'th input of a Phi node is live.
419  */
420 static int is_live_input(ir_node *phi, int i) {
421         ir_node *block = get_nodes_block(phi);
422         ir_node *pred  = get_Block_cfgpred(block, i);
423         tarval *type   = get_node_type(pred);
424
425         return type != tarval_U;
426 }
427
428 /**
429  * Split the partitions if caused by the first entry on the worklist.
430  */
431 static void cause_splits(environment_t *env) {
432         partition_t *X, *Y, *Z;
433         node_t      *x, *y, *e;
434         int         i, end_idx;
435
436         /* remove the first partition from the worklist */
437         X = env->worklist;
438         env->worklist  = X->wl_next;
439         X->on_worklist = 0;
440
441         dump_partition("Cause_split: ", X);
442         end_idx = env->end_idx;
443         for (i = X->n_inputs - 1; i >= -1; --i) {
444                 /* empty the touched set: already done, just clear the list */
445                 env->touched = NULL;
446
447                 list_for_each_entry(node_t, x, &X->entries, node_list) {
448                         /* ignore the "control input" for non-pinned nodes
449                            if we are running in GCSE mode */
450                         if (i < end_idx && get_irn_pinned(x->node) != op_pin_state_pinned)
451                                 continue;
452
453                         /* non-existing input */
454                         if (i >= get_irn_arity(x->node))
455                                 continue;
456
457                         y = get_irn_node(get_irn_n(x->node, i));
458                         Y = y->part;
459                         if (Y != env->TOP && (! is_Phi(x->node) || is_live_input(x->node, i))) {
460                                 add_to_touched(Y, env);
461                                 add_to_partition_touched(y);
462                         }
463                 }
464
465                 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
466                         /* remove it from the touched set */
467                         Z->on_touched = 0;
468
469                         if (Z->n_nodes != Z->n_touched) {
470                                 split(Z, Z->touched, env);
471                         }
472                         /* Empty local Z.touched. */
473                         for (e = Z->touched; e != NULL; e = e->next) {
474                                 e->on_touched = 0;
475                         }
476                         Z->touched   = NULL;
477                         Z->n_touched = 0;
478                 }
479         }
480 }
481
482 /**
483  * Implements split_by_what(): Split a partition by characteristics given
484  * by the what function.
485  *
486  * @return list of partitions
487  */
488 static partition_t **split_by_what(partition_t *X, what_func What,
489                                                                   partition_t**P, environment_t *env) {
490         node_t          *x, *S;
491         listmap_t       map;
492         listmap_entry_t *iter;
493         partition_t     *R;
494
495         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
496         new_listmap(&map);
497         list_for_each_entry(node_t, x, &X->entries, node_list) {
498                 unsigned        id = What(x, env);
499                 listmap_entry_t *entry;
500
501                 if (id == 0) {
502                         /* input not allowed, ignore */
503                         continue;
504                 }
505                 /* Add x to map[What(x)]. */
506                 entry = listmap_find(&map, id);
507                 x->next     = entry->list;
508                 entry->list = x;
509         }
510         /* Let P be a set of Partitions. */
511         P = NULL;
512         /* for all sets S except one in the range of map do */
513         for (iter = map.values; iter != NULL; iter = iter->next) {
514                 if (iter->next == NULL) {
515                         /* this is the last entry, ignore */
516                         break;
517                 }
518                 S = iter->list;
519
520                 /* Add SPLIT( X, S ) to P. */
521                 R = split(X, S, env);
522                 if (P != NULL) {
523                         ARR_APP1(partition_t *, P, R);
524                 }
525         }
526         /* Add X to P. */
527         if (P != NULL) {
528                 ARR_APP1(partition_t *, P, X);
529         }
530
531         del_listmap(&map);
532         return P;
533 }
534
535 /** lambda n.(n.type) */
536 static unsigned lambda_type(const node_t *node, environment_t *env) {
537         (void)env;
538         /* ensure that it is NOT null */
539         return (((unsigned)((char *)node->type - (char *)0)) << 1) | 1;
540 }
541
542 /** lambda n.(n.opcode) */
543 static unsigned lambda_opcode(const node_t *node, environment_t *env) {
544         opcode2id_entry_t key, *entry;
545
546         key.key.code = get_irn_opcode(node->node);
547         key.key.mode = get_irn_mode(node->node);
548         key.id       = 0;
549         entry = set_insert(env->opcode2id_map, &key, sizeof(&key), opcode_hash(&key.key));
550         if (entry->id == 0)
551                 entry->id = ++env->next_opcode_id;
552         return entry->id;
553 }
554
555 /** lambda n.(n[i].partition) */
556 static unsigned lambda_partition(const node_t *node, environment_t *env) {
557         ir_node *pred;
558         node_t  *p;
559         int     i = env->lambda_input;
560
561         if (i >= get_irn_arity(node->node)) {
562                 /* we are outside the allowed range */
563                 return 0;
564         }
565
566         /* ignore the "control input" for non-pinned nodes
567            if we are running in GCSE mode */
568         if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
569                 return 0;
570
571         pred = get_irn_n(node->node, i);
572         p    = get_irn_node(pred);
573
574         return p->part->nr;
575 }
576
577 /**
578  * Implements split_by().
579  */
580 static void split_by(partition_t *X, environment_t *env) {
581         partition_t **P = NEW_ARR_F(partition_t *, 0);
582         int         i, j, k;
583
584         P = split_by_what(X, lambda_type, P, env);
585         for (i = ARR_LEN(P) - 1; i >= 0; --i) {
586                 partition_t *Y = P[i];
587
588                 if (Y != env->TOP) {
589                         partition_t **Q = NEW_ARR_F(partition_t *, 0);
590
591                         Q = split_by_what(Y, lambda_opcode, Q, env);
592
593                         for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
594                                 partition_t *Z = Q[i];
595
596                                 for (k = Z->n_inputs - 1; k >= -1; --k) {
597                                         env->lambda_input = k;
598                                         split_by_what(Z, lambda_partition, NULL, env);
599                                 }
600                         }
601                         DEL_ARR_F(Q);
602                 }
603         }
604         DEL_ARR_F(P);
605 }
606
607 /**
608  * (Re-)compute the type for a given node.
609  */
610 static void default_compute(node_t *node) {
611         int     i;
612         ir_node *irn = node->node;
613         ir_mode *mode;
614
615         if (get_irn_pinned(irn) == op_pin_state_pinned) {
616                 node_t *block = get_irn_node(get_nodes_block(irn));
617
618                 if (block->type == tarval_U) {
619                         node->type = tarval_top;
620                         return;
621                 }
622         }
623         mode = get_irn_mode(irn);
624         if (mode == mode_M) {
625                 /* mode M is always bottom for now */
626                 node->type = tarval_bottom;
627                 return;
628         }
629         if (! mode_is_data(mode))
630                 return;
631
632         /* if any of the data inputs have type top, the result is type top */
633         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
634                 ir_node *pred = get_irn_n(irn, i);
635                 node_t  *p    = get_irn_node(pred);
636
637                 if (p->type == tarval_top) {
638                         node->type = tarval_top;
639                         return;
640                 }
641         }
642         node->type = computed_value(irn);
643 }
644
645 /**
646  * (Re-)compute the type for a Block node.
647  */
648 static void compute_Block(node_t *node) {
649         int     i;
650         ir_node *block = node->node;
651
652         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
653                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
654
655                 if (pred->type == tarval_R) {
656                         /* A block is reachable, if at least of predecessor is reachable. */
657                         node->type = tarval_R;
658                         return;
659                 }
660         }
661         node->type = tarval_U;
662 }
663
664 /**
665  * (Re-)compute the type for a Jmp node.
666  */
667 static void compute_Jmp(node_t *node) {
668         node_t *block = get_irn_node(get_nodes_block(node->node));
669
670         node->type = block->type;
671 }
672
673 /**
674  * (Re-)compute the type for a Phi node.
675  */
676 static void compute_Phi(node_t *node) {
677         int     i;
678         ir_node *phi = node->node;
679         tarval  *type = tarval_top;
680
681         /* if a Phi is in a unreachable block, its type is TOP */
682         node_t *block = get_irn_node(get_nodes_block(phi));
683
684         if (block->type == tarval_U) {
685                 node->type = tarval_top;
686                 return;
687         }
688
689         /* if any of the data inputs have type top, the result is type top */
690         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
691                 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
692
693                 if (pred->type == tarval_top) {
694                         /* ignore TOP inputs */
695                         continue;
696                 }
697                 if (pred->type == tarval_bottom) {
698                         node->type = tarval_bottom;
699                         return;
700                 } else if (type == tarval_top) {
701                         /* first constant found */
702                         type = pred->type;
703                 } else if (type == pred->type) {
704                         /* same constant, continue */
705                         continue;
706                 } else {
707                         /* different constants or tarval_bottom */
708                         node->type = tarval_bottom;
709                         return;
710                 }
711         }
712         node->type = type;
713 }
714
715 /**
716  * (Re-)compute the type for a Proj-Nodes.
717  */
718 static void compute_Proj(node_t *node) {
719         ir_node *proj = node->node;
720         ir_mode *mode = get_irn_mode(proj);
721         ir_node *pred;
722
723         if (mode == mode_M) {
724                 /* mode M is always bottom */
725                 node->type = tarval_bottom;
726                 return;
727         }
728         if (mode != mode_X) {
729                 default_compute(node);
730                 return;
731         }
732         /* handle mode_X nodes */
733         pred = get_Proj_pred(proj);
734
735         switch (get_irn_opcode(pred)) {
736         case iro_Start:
737                 /* the Proj_X from the Start is always reachable */
738                 node->type = tarval_R;
739                 break;
740         default:
741                 default_compute(node);
742         }
743 }
744
745 /**
746  * (Re-)compute the type for a given node.
747  */
748 static void compute(node_t *node) {
749         compute_func func = (compute_func)node->node->op->ops.generic;
750
751         if (func != NULL)
752                 func(node);
753 }
754 /**
755  * Propagate constant evaluation.
756  */
757 static void propagate(environment_t *env) {
758         partition_t *X, *Y;
759         node_t      *x;
760         tarval      *old_type;
761         node_t      *fallen = NULL;
762         unsigned    n_fallen = 0;
763         int         i;
764
765         while (env->cprop != NULL) {
766                 /* remove a partition X from cprop */
767                 X = env->cprop;
768                 env->cprop = X->cprop_next;
769
770                 while (X->cprop != NULL) {
771                         /* remove a Node x from X.cprop */
772                         x = X->cprop;
773                         x->on_cprop = 0;
774                         X->cprop = x->cprop_next;
775
776                         /* compute a new type for x */
777                         old_type = x->type;
778                         compute(x);
779                         if (x->type != old_type) {
780                                 DB((dbg, LEVEL_2, "node %+F has changed type from %T to %T\n", x->node, old_type, x->type));
781                                 /* Add x to fallen. */
782                                 x->next = fallen;
783                                 fallen  = x;
784                                 ++n_fallen;
785
786                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
787                                         ir_node *succ = get_irn_out(x->node, i);
788                                         node_t  *y    = get_irn_node(succ);
789
790                                         /* Add y to y.partition.cprop. */
791                                         if (y->on_cprop == 0) {
792                                                 y->cprop_next  = y->part->cprop;
793                                                 y->part->cprop = y;
794                                                 y->on_cprop    = 1;
795                                         }
796                                 }
797                         }
798                 }
799                 if (n_fallen != X->n_nodes) {
800                         Y = split(X, fallen, env);
801                 } else {
802                         Y = X;
803                 }
804                 split_by(Y, env);
805         }
806 }
807
808 /**
809  * Get the leader for a given node from its congruence class.
810  *
811  * @param irn  the node
812  */
813 static ir_node *get_leader(ir_node *irn) {
814         partition_t *part = get_irn_node(irn)->part;
815
816         if (part->n_nodes > 1) {
817                 DB((dbg, LEVEL_2, "Found congruence class for %+F ", irn));
818                 dump_partition("", part);
819         }
820         return irn;
821 }
822
823 /**
824  * Post-Walker, apply the analysis results;
825  */
826 static void apply_result(ir_node *irn, void *ctx) {
827         environment_t *env = ctx;
828
829         if (is_no_Block(irn)) {
830                 ir_node *leader = get_leader(irn);
831
832                 if (leader != irn) {
833                         exchange(irn, leader);
834                 }
835         }
836 }
837
838 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
839
840 /**
841  * sets the generic functions to compute.
842  */
843 static void set_compute_functions(void) {
844         int i;
845
846         /* set the default compute function */
847         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
848                 ir_op *op = get_irp_opcode(i);
849                 op->ops.generic = (op_func)default_compute;
850         }
851
852         /* set specific functions */
853         SET(Block);
854         SET(Jmp);
855         SET(Phi);
856         SET(Proj);
857 }
858
859 void combo(ir_graph *irg) {
860         environment_t env;
861         ir_node       *start_bl, *initial_X;
862         node_t        *start;
863         ir_graph      *rem = current_ir_graph;
864
865         current_ir_graph = irg;
866
867         /* register a debug mask */
868         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
869         firm_dbg_set_mask(dbg, SET_LEVEL_2);
870
871         obstack_init(&env.obst);
872         env.worklist       = NULL;
873         env.cprop          = NULL;
874         env.touched        = NULL;
875         env.TOP            = NULL;
876         env.opcode_map     = new_set(cmp_opcode, iro_Last * 4);
877         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
878         env.type2id_map    = pmap_create();
879         env.end_idx        = get_opt_global_cse() ? 0 : -1;
880         env.lambda_input   = 0;
881         env.next_opcode_id = 0;
882
883         assure_irg_outs(irg);
884
885         /* we have our own value_of function */
886         set_value_of_func(get_node_type);
887
888         set_compute_functions();
889
890         /* create the initial TOP partition and place it on the work list */
891         env.TOP = new_partition(&env);
892         env.TOP->wl_next = env.worklist;
893         env.worklist     = env.TOP;
894
895         irg_walk_graph(irg, NULL, create_initial_partitions, &env);
896
897         /* Place the START Node's partition on cprop.
898            Place the START Node on its local worklist. */
899         start_bl = get_irg_start_block(irg);
900         start    = get_irn_node(start_bl);
901         start->part->cprop_next = env.cprop;
902         env.cprop               = start->part;
903
904         start->cprop_next  = start->part->cprop;
905         start->part->cprop = start;
906
907         /* set the initial exec to R */
908         initial_X = get_irg_initial_exec(irg);
909         get_irn_node(initial_X)->type = tarval_R;
910
911         while (env.cprop != NULL && env.worklist != NULL) {
912                 propagate(&env);
913                 if (env.worklist != NULL)
914                         cause_splits(&env);
915         }
916
917         /* apply the result */
918         irg_walk_graph(irg, NULL, apply_result, &env);
919
920         pmap_destroy(env.type2id_map);
921         del_set(env.opcode_map);
922         del_set(env.opcode2id_map);
923         obstack_free(&env.obst, NULL);
924
925         /* restore value_of() default behavior */
926         set_value_of_func(NULL);
927         current_ir_graph = rem;
928 }