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