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