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