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