- BugFix: constants must be evaluated BEFORE congruence checks
[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  * Returns true if a type is a constant.
1141  */
1142 static int is_con(const lattice_elem_t type) {
1143         return is_entity(type.sym.entity_p) || tarval_is_constant(type.tv);
1144 }
1145
1146 /**
1147  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1148  *
1149  * @param node  the node
1150  */
1151 static void compute_Sub(node_t *node) {
1152         ir_node        *sub = node->node;
1153         node_t         *l   = get_irn_node(get_Sub_left(sub));
1154         node_t         *r   = get_irn_node(get_Sub_right(sub));
1155         lattice_elem_t a    = l->type;
1156         lattice_elem_t b    = r->type;
1157         node_t         *block = get_irn_node(get_nodes_block(sub));
1158
1159         if (block->type.tv == tarval_unreachable) {
1160                 node->type.tv = tarval_top;
1161                 return;
1162         }
1163         if (a.tv == tarval_top || b.tv == tarval_top) {
1164                 node->type.tv = tarval_top;
1165         } else if (is_con(a) && is_con(b)) {
1166                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1167                         node->type.tv = tarval_sub(a.tv, b.tv);
1168                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1169                         node->type = b;
1170                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1171                         node->type = a;
1172                 } else {
1173                         node->type.tv = tarval_bottom;
1174                 }
1175         } else if (r->part == l->part) {
1176                 ir_mode *mode = get_irn_mode(sub);
1177                 node->type.tv = get_mode_null(mode);
1178         } else {
1179                 node->type.tv = tarval_bottom;
1180         }
1181 }  /* compute_Sub */
1182
1183 /**
1184  * (Re-)compute the type for a Proj(Cmp).
1185  *
1186  * @param node  the node
1187  * @param cond  the predecessor Cmp node
1188  */
1189 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1190         ir_node        *proj = node->node;
1191         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1192         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1193         lattice_elem_t a     = l->type;
1194         lattice_elem_t b     = r->type;
1195         pn_Cmp         pnc   = get_Proj_proj(proj);
1196
1197         /*
1198          * BEWARE: a == a is NOT always True for floating Point values, as
1199          * NaN != NaN is defined, so we must check this here.
1200          */
1201         if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt ||  pnc == pn_Cmp_Gt) {
1202                 if (a.tv == tarval_top || b.tv == tarval_top) {
1203                         node->type.tv = tarval_top;
1204                 } else if (is_con(a) && is_con(b)) {
1205                         default_compute(node);
1206                 } else if (r->part == l->part) {
1207                         node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1208                 } else {
1209                         node->type.tv = tarval_bottom;
1210                 }
1211         } else {
1212                 default_compute(node);
1213         }
1214 }  /* compute_Proj_Cmp */
1215
1216 /**
1217  * (Re-)compute the type for a Proj(Cond).
1218  *
1219  * @param node  the node
1220  * @param cond  the predecessor Cond node
1221  */
1222 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1223         ir_node *proj     = node->node;
1224         long    pnc       = get_Proj_proj(proj);
1225         ir_node *sel      = get_Cond_selector(cond);
1226         node_t  *selector = get_irn_node(sel);
1227
1228         if (get_irn_mode(sel) == mode_b) {
1229                 /* an IF */
1230                 if (pnc == pn_Cond_true) {
1231                         if (selector->type.tv == tarval_b_false) {
1232                                 node->type.tv = tarval_unreachable;
1233                         } else if (selector->type.tv == tarval_b_true) {
1234                                 node->type.tv = tarval_reachable;
1235                         } else if (selector->type.tv == tarval_bottom) {
1236                                 node->type.tv = tarval_reachable;
1237                         } else {
1238                                 assert(selector->type.tv == tarval_top);
1239                                 node->type.tv = tarval_unreachable;
1240                         }
1241                 } else {
1242                         assert(pnc == pn_Cond_false);
1243
1244                         if (selector->type.tv == tarval_b_false) {
1245                                 node->type.tv = tarval_reachable;
1246                         } else if (selector->type.tv == tarval_b_true) {
1247                                 node->type.tv = tarval_unreachable;
1248                         } else if (selector->type.tv == tarval_bottom) {
1249                                 node->type.tv = tarval_reachable;
1250                         } else {
1251                                 assert(selector->type.tv == tarval_top);
1252                                 node->type.tv = tarval_unreachable;
1253                         }
1254                 }
1255         } else {
1256                 /* an SWITCH */
1257                 if (selector->type.tv == tarval_bottom) {
1258                         node->type.tv = tarval_reachable;
1259                 } else if (selector->type.tv == tarval_top) {
1260                         node->type.tv = tarval_unreachable;
1261                 } else {
1262                         long value = get_tarval_long(selector->type.tv);
1263                         if (pnc == get_Cond_defaultProj(cond)) {
1264                                 /* default switch, have to check ALL other cases */
1265                                 int i;
1266
1267                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1268                                         ir_node *succ = get_irn_out(cond, i);
1269
1270                                         if (succ == proj)
1271                                                 continue;
1272                                         if (value == get_Proj_proj(succ)) {
1273                                                 /* we found a match, will NOT take the default case */
1274                                                 node->type.tv = tarval_unreachable;
1275                                                 return;
1276                                         }
1277                                 }
1278                                 /* all cases checked, no match, will take default case */
1279                                 node->type.tv = tarval_reachable;
1280                         } else {
1281                                 /* normal case */
1282                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1283                         }
1284                 }
1285         }
1286 }  /* compute_Proj_Cond */
1287
1288 /**
1289  * (Re-)compute the type for a Proj-Nodes.
1290  *
1291  * @param node  the node
1292  */
1293 static void compute_Proj(node_t *node) {
1294         ir_node *proj = node->node;
1295         ir_mode *mode = get_irn_mode(proj);
1296         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1297         ir_node *pred  = get_Proj_pred(proj);
1298
1299         if (block->type.tv == tarval_unreachable) {
1300                 /* a Proj node in an unreachable block computes Top
1301                    except if it's the initial_exec node. */
1302                 if (get_Proj_proj(proj) != pn_Start_X_initial_exec ||
1303                         ! is_Start(pred)) {
1304                         node->type.tv = get_top_value(mode);
1305                         return;
1306                 }
1307         }
1308
1309         if (mode == mode_M) {
1310                 /* mode M is always bottom */
1311                 node->type.tv = tarval_bottom;
1312                 return;
1313         }
1314         if (mode != mode_X) {
1315                 if (is_Cmp(pred))
1316                         compute_Proj_Cmp(node, pred);
1317                 else
1318                         default_compute(node);
1319                 return;
1320         }
1321         /* handle mode_X nodes */
1322
1323         switch (get_irn_opcode(pred)) {
1324         case iro_Start:
1325                 /* the Proj_X from the Start is always reachable */
1326                 node->type.tv = tarval_reachable;
1327                 break;
1328         case iro_Cond:
1329                 compute_Proj_Cond(node, pred);
1330                 break;
1331         default:
1332                 default_compute(node);
1333         }
1334 }  /* compute_Proj */
1335
1336 /**
1337  * (Re-)compute the type for a given node.
1338  *
1339  * @param node  the node
1340  */
1341 static void compute(node_t *node) {
1342         compute_func func = (compute_func)node->node->op->ops.generic;
1343
1344         if (func != NULL)
1345                 func(node);
1346 }  /* compute */
1347
1348 /**
1349  * Propagate constant evaluation.
1350  *
1351  * @param env  the environment
1352  */
1353 static void propagate(environment_t *env) {
1354         partition_t    *X, *Y;
1355         node_t         *x;
1356         lattice_elem_t old_type;
1357         node_t         *fallen;
1358         unsigned       n_fallen;
1359         int            i;
1360
1361         while (env->cprop != NULL) {
1362                 /* remove the first partition X from cprop */
1363                 X          = env->cprop;
1364                 X->on_cprop = 0;
1365                 env->cprop = X->cprop_next;
1366
1367                 fallen   = NULL;
1368                 n_fallen = 0;
1369                 while (! list_empty(&X->cprop)) {
1370                         /* remove the first Node x from X.cprop */
1371                         x = list_entry(X->cprop.next, node_t, cprop_list);
1372                         list_del(&x->cprop_list);
1373                         x->on_cprop = 0;
1374
1375                         /* compute a new type for x */
1376                         old_type = x->type;
1377                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1378                         compute(x);
1379                         if (x->type.tv != old_type.tv) {
1380                                 verify_type(old_type, x->type);
1381                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1382
1383                                 if (x->on_fallen == 0) {
1384                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
1385                                            not already on the list. */
1386                                         x->next      = fallen;
1387                                         x->on_fallen = 1;
1388                                         fallen       = x;
1389                                         ++n_fallen;
1390                                 }
1391                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1392                                         ir_node *succ = get_irn_out(x->node, i);
1393                                         node_t  *y    = get_irn_node(succ);
1394
1395                                         /* Add y to y.partition.cprop. */
1396                                         add_node_to_cprop(y, env);
1397                                 }
1398                         }
1399                 }
1400
1401                 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1402                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
1403                         Y = split(X, fallen, env);
1404                 } else {
1405                         Y = X;
1406                 }
1407                 /* remove the nodes from the fallen list */
1408                 for (x = fallen; x != NULL; x = x->next)
1409                         x->on_fallen = 0;
1410
1411                 if (Y->n_nodes > 1)
1412                         split_by(Y, env);
1413         }
1414 }  /* propagate */
1415
1416 /**
1417  * Get the leader for a given node from its congruence class.
1418  *
1419  * @param irn  the node
1420  */
1421 static ir_node *get_leader(node_t *node) {
1422         partition_t *part = node->part;
1423
1424         if (part->n_nodes > 1) {
1425                 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1426
1427                 return get_first_node(part)->node;
1428         }
1429         return node->node;
1430 }
1431
1432 /**
1433  * Post-Walker, apply the analysis results;
1434  */
1435 static void apply_result(ir_node *irn, void *ctx) {
1436         node_t *node = get_irn_node(irn);
1437
1438         (void) ctx;
1439         if (is_Block(irn)) {
1440                 if (irn == get_irg_end_block(current_ir_graph)) {
1441                         /* the EndBlock is always reachable even if the analysis
1442                            finds out the opposite :-) */
1443                         return;
1444                 }
1445
1446                 if (node->type.tv == tarval_unreachable) {
1447                         /* mark dead blocks */
1448                         set_Block_dead(irn);
1449                 }
1450         } else if (is_End(irn)) {
1451                 /* do not touch the End node */
1452         } else {
1453                 node_t *block = get_irn_node(get_nodes_block(irn));
1454
1455                 if (block->type.tv == tarval_unreachable) {
1456                         if (! is_Bad(irn)) {
1457                                 ir_node *bad = get_irg_bad(current_ir_graph);
1458
1459                                 /* here, bad might already have a node, but this can be safely ignored
1460                                    as long as bad has at least ONE valid node */
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                 }
1467                 else if (get_irn_mode(irn) == mode_X) {
1468                         if (node->type.tv == tarval_unreachable) {
1469                                 ir_node *bad = get_irg_bad(current_ir_graph);
1470
1471                                 /* see comment above */
1472                                 set_irn_node(bad, node);
1473                                 node->node = bad;
1474                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1475                                 exchange(irn, bad);
1476                         }
1477                         else if (is_Proj(irn)) {
1478                                 /* leave or Jmp */
1479                                 ir_node *cond = get_Proj_pred(irn);
1480
1481                                 if (is_Cond(cond)) {
1482                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
1483
1484                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1485                                                 /* Cond selector is a constant, make a Jmp */
1486                                                 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1487                                                 set_irn_node(jmp, node);
1488                                                 node->node = jmp;
1489                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1490                                                 exchange(irn, jmp);
1491                                         }
1492                                 }
1493                         }
1494                 } else {
1495                         /* normal data node */
1496                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1497                                 tarval *tv = node->type.tv;
1498
1499                                 if (! is_Const(irn)) {
1500                                         /* can be replaced by a constant */
1501                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1502                                         set_irn_node(c, node);
1503                                         node->node = c;
1504                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1505                                         exchange(irn, c);
1506                                 }
1507                         } else if (is_entity(node->type.sym.entity_p)) {
1508                                 if (! is_SymConst(irn)) {
1509                                         /* can be replaced by a Symconst */
1510                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1511                                         set_irn_node(symc, node);
1512                                         node->node = symc;
1513
1514                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1515                                         exchange(irn, symc);
1516                                 }
1517                         } else {
1518                                 ir_node *leader = get_leader(node);
1519
1520                                 if (leader != irn) {
1521                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
1522                                         exchange(irn, leader);
1523                                 }
1524                         }
1525                 }
1526         }
1527 }  /* static void apply_result(ir_node *irn, void *ctx) {
1528  */
1529
1530 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1531
1532 /**
1533  * sets the generic functions to compute.
1534  */
1535 static void set_compute_functions(void) {
1536         int i;
1537
1538         /* set the default compute function */
1539         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1540                 ir_op *op = get_irp_opcode(i);
1541                 op->ops.generic = (op_func)default_compute;
1542         }
1543
1544         /* set specific functions */
1545         SET(Block);
1546         SET(Jmp);
1547         SET(Phi);
1548         SET(Add);
1549         SET(Sub);
1550         SET(SymConst);
1551         SET(Proj);
1552         SET(End);
1553 }  /* set_compute_functions */
1554
1555 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1556         ir_node *irn = local != NULL ? local : n;
1557         node_t *node = get_irn_node(irn);
1558
1559         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1560         return 1;
1561 }
1562
1563 void combo(ir_graph *irg) {
1564         environment_t env;
1565         ir_node       *initial_X;
1566         node_t        *start;
1567         ir_graph      *rem = current_ir_graph;
1568
1569         current_ir_graph = irg;
1570
1571         /* register a debug mask */
1572         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1573         //firm_dbg_set_mask(dbg, SET_LEVEL_3);
1574
1575         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1576
1577         obstack_init(&env.obst);
1578         env.worklist       = NULL;
1579         env.cprop          = NULL;
1580         env.touched        = NULL;
1581         env.initial        = NULL;
1582 #ifdef DEBUG_libfirm
1583         env.dbg_list       = NULL;
1584 #endif
1585         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
1586         env.type2id_map    = pmap_create();
1587         env.end_idx        = get_opt_global_cse() ? 0 : -1;
1588         env.lambda_input   = 0;
1589
1590         assure_irg_outs(irg);
1591
1592         /* we have our own value_of function */
1593         set_value_of_func(get_node_tarval);
1594
1595         set_compute_functions();
1596         DEBUG_ONLY(part_nr = 0);
1597
1598         /* create the initial partition and place it on the work list */
1599         env.initial = new_partition(&env);
1600         add_to_worklist(env.initial, &env);
1601         irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
1602
1603         /* Place the START Node's partition on cprop.
1604            Place the START Node on its local worklist. */
1605         initial_X = get_irg_initial_exec(irg);
1606         start     = get_irn_node(initial_X);
1607         add_node_to_cprop(start, &env);
1608
1609         do {
1610                 propagate(&env);
1611                 if (env.worklist != NULL)
1612                         cause_splits(&env);
1613         } while (env.cprop != NULL || env.worklist != NULL);
1614
1615         dump_all_partitions(&env);
1616
1617         set_dump_node_vcgattr_hook(dump_partition_hook);
1618         dump_ir_block_graph(irg, "-partition");
1619         set_dump_node_vcgattr_hook(NULL);
1620
1621
1622         /* apply the result */
1623         irg_walk_graph(irg, NULL, apply_result, &env);
1624
1625         pmap_destroy(env.type2id_map);
1626         del_set(env.opcode2id_map);
1627         obstack_free(&env.obst, NULL);
1628
1629         /* restore value_of() default behavior */
1630         set_value_of_func(NULL);
1631         current_ir_graph = rem;
1632 }  /* combo */