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