Fix typo in comment.
[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 further that we use the terminology from Click's work here, which is different
27  * in some cases from Firm terminology.  Especially, Click's type is a
28  * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
29  */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <assert.h>
35
36 #include "iroptimize.h"
37 #include "archop.h"
38 #include "irflag.h"
39 #include "ircons.h"
40 #include "list.h"
41 #include "set.h"
42 #include "pmap.h"
43 #include "obstack.h"
44 #include "irgraph_t.h"
45 #include "irnode_t.h"
46 #include "iropt_t.h"
47 #include "irgwalk.h"
48 #include "irop.h"
49 #include "irouts.h"
50 #include "irgmod.h"
51 #include "iropt_dbg.h"
52 #include "debug.h"
53 #include "error.h"
54
55 #include "tv_t.h"
56
57 #include "irprintf.h"
58 #include "irdump.h"
59
60 /* define this to check that all type translations are monotone */
61 #undef VERIFY_MONOTONE
62
63 /* define this to check the consistency of partitions */
64 #define CHECK_PARTITIONS
65
66 typedef struct node_t            node_t;
67 typedef struct partition_t       partition_t;
68 typedef struct opcode_key_t      opcode_key_t;
69 typedef struct listmap_entry_t   listmap_entry_t;
70
71 /** The type of the compute function. */
72 typedef void (*compute_func)(node_t *node);
73
74 /**
75  * An opcode map key.
76  */
77 struct opcode_key_t {
78         ir_opcode   code;   /**< The Firm opcode. */
79         ir_mode     *mode;  /**< The mode of all nodes in the partition. */
80         int         arity;  /**< The arity of this opcode (needed for Phi etc. */
81         union {
82                 long      proj;   /**< For Proj nodes, its proj number */
83                 ir_entity *ent;   /**< For Sel Nodes, its entity */
84         } u;
85 };
86
87 /**
88  * An entry in the list_map.
89  */
90 struct listmap_entry_t {
91         void            *id;    /**< The id. */
92         node_t          *list;  /**< The associated list for this id. */
93         listmap_entry_t *next;  /**< Link to the next entry in the map. */
94 };
95
96 /** We must map id's to lists. */
97 typedef struct listmap_t {
98         set             *map;    /**< Map id's to listmap_entry_t's */
99         listmap_entry_t *values; /**< List of all values in the map. */
100 } listmap_t;
101
102 /**
103  * A lattice element. Because we handle constants and symbolic constants different, we
104  * have to use this union.
105  */
106 typedef union {
107         tarval          *tv;
108         symconst_symbol sym;
109 } lattice_elem_t;
110
111 /**
112  * A node.
113  */
114 struct node_t {
115         ir_node         *node;          /**< The IR-node itself. */
116         list_head       node_list;      /**< Double-linked list of leader/follower entries. */
117         list_head       cprop_list;     /**< Double-linked partition.cprop list. */
118         partition_t     *part;          /**< points to the partition this node belongs to */
119         node_t          *next;          /**< Next node on local list (partition.touched, fallen). */
120         node_t          *race_next;     /**< Next node on race list. */
121         lattice_elem_t  type;           /**< The associated lattice element "type". */
122         int             max_user_input; /**< Maximum input number of Def-Use edges. */
123         int             next_edge;      /**< Index of the next Def-Use edge to use. */
124         int             n_followers;    /**< Number of Follower in the outs set. */
125         unsigned        on_touched:1;   /**< Set, if this node is on the partition.touched set. */
126         unsigned        on_cprop:1;     /**< Set, if this node is on the partition.cprop list. */
127         unsigned        on_fallen:1;    /**< Set, if this node is on the fallen list. */
128         unsigned        is_follower:1;  /**< Set, if this node is a follower. */
129         unsigned        by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
130         unsigned        flagged:2;      /**< 2 Bits, set if this node was visited by race 1 or 2. */
131 };
132
133 /**
134  * A partition containing congruent nodes.
135  */
136 struct partition_t {
137         list_head         Leader;          /**< The head of partition Leader node list. */
138         list_head         Follower;        /**< The head of partition Follower node list. */
139         list_head         cprop;           /**< The head of partition.cprop list. */
140         partition_t       *wl_next;        /**< Next entry in the work list if any. */
141         partition_t       *touched_next;   /**< Points to the next partition in the touched set. */
142         partition_t       *cprop_next;     /**< Points to the next partition in the cprop list. */
143         partition_t       *split_next;     /**< Points to the next partition in the list that must be split by split_by(). */
144         node_t            *touched;        /**< The partition.touched set of this partition. */
145         unsigned          n_leader;        /**< Number of entries in this partition.Leader. */
146         unsigned          n_touched;       /**< Number of entries in the partition.touched. */
147         int               max_user_inputs; /**< Maximum number of user inputs of all entries. */
148         unsigned          on_worklist:1;   /**< Set, if this partition is in the work list. */
149         unsigned          on_touched:1;    /**< Set, if this partition is on the touched set. */
150         unsigned          on_cprop:1;      /**< Set, if this partition is on the cprop list. */
151         unsigned          type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
152 #ifdef DEBUG_libfirm
153         partition_t       *dbg_next;       /**< Link all partitions for debugging */
154         unsigned          nr;              /**< A unique number for (what-)mapping, >0. */
155 #endif
156 };
157
158 typedef struct environment_t {
159         struct obstack  obst;           /**< obstack to allocate data structures. */
160         partition_t     *worklist;      /**< The work list. */
161         partition_t     *cprop;         /**< The constant propagation list. */
162         partition_t     *touched;       /**< the touched set. */
163         partition_t     *initial;       /**< The initial partition. */
164         set             *opcode2id_map; /**< The opcodeMode->id map. */
165         pmap            *type2id_map;   /**< The type->id map. */
166         int             end_idx;        /**< -1 for local and 0 for global congruences. */
167         int             lambda_input;   /**< Captured argument for lambda_partition(). */
168         char            nonstd_cond;    /**< Set, if a Condb note has a non-Cmp predecessor. */
169         char            modified;       /**< Set, if the graph was modified. */
170 #ifdef DEBUG_libfirm
171         partition_t     *dbg_list;      /**< List of all partitions. */
172 #endif
173 } environment_t;
174
175 /** Type of the what function. */
176 typedef void *(*what_func)(const node_t *node, environment_t *env);
177
178 #define get_irn_node(follower)         ((node_t *)get_irn_link(follower))
179 #define set_irn_node(follower, node)   set_irn_link(follower, node)
180
181 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
182 #undef tarval_unreachable
183 #define tarval_unreachable tarval_top
184
185
186 /** The debug module handle. */
187 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
188
189 /** Next partition number. */
190 DEBUG_ONLY(static unsigned part_nr = 0);
191
192 /** The tarval returned by Unknown nodes. */
193 static tarval *tarval_UNKNOWN;
194
195 /* forward */
196 static node_t *identity(node_t *node);
197
198 #ifdef CHECK_PARTITIONS
199 /**
200  * Check a partition.
201  */
202 static void check_partition(const partition_t *T) {
203         node_t   *node;
204         unsigned n = 0;
205
206         list_for_each_entry(node_t, node, &T->Leader, node_list) {
207                 assert(node->is_follower == 0);
208                 assert(node->flagged == 0);
209                 assert(node->part == T);
210                 ++n;
211         }
212         assert(n == T->n_leader);
213
214         list_for_each_entry(node_t, node, &T->Follower, node_list) {
215                 assert(node->is_follower == 1);
216                 assert(node->flagged == 0);
217                 assert(node->part == T);
218         }
219 }  /* check_partition */
220
221 static void check_all_partitions(environment_t *env) {
222         partition_t *P;
223         node_t      *node;
224
225         for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
226                 check_partition(P);
227                 list_for_each_entry(node_t, node, &P->Follower, node_list) {
228                         node_t *leader = identity(node);
229
230                         assert(leader != node && leader->part == node->part);
231                 }
232         }
233 }
234
235 /**
236  * Check list.
237  */
238 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
239         const node_t *e;
240
241 #define NEXT(e)  *((const node_t **)((char *)(e) + (ofs)))
242         for (e = list; e != NULL; e = NEXT(e)) {
243                 assert(e->part == Z);
244         }
245 #undef NEXT
246 }  /* ido_check_list */
247
248 /**
249  * Check a local list.
250  */
251 static void check_list(const node_t *list, const partition_t *Z) {
252         do_check_list(list, offsetof(node_t, next), Z);
253 }  /* check_list */
254
255 #else
256 #define check_partition(T)
257 #define check_list(list, Z)
258 #define check_all_partitions(env)
259 #endif /* CHECK_PARTITIONS */
260
261 #ifdef DEBUG_libfirm
262 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
263
264 /**
265  * Dump partition to output.
266  */
267 static void dump_partition(const char *msg, const partition_t *part) {
268         const node_t   *node;
269         int            first = 1;
270         lattice_elem_t type = get_partition_type(part);
271
272         DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n  ",
273                 msg, part->nr, part->type_is_T_or_C ? "*" : "",
274                 part->n_leader, type));
275         list_for_each_entry(node_t, node, &part->Leader, node_list) {
276                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
277                 first = 0;
278         }
279         if (! list_empty(&part->Follower)) {
280                 DB((dbg, LEVEL_2, "\n---\n  "));
281                 first = 1;
282                 list_for_each_entry(node_t, node, &part->Follower, node_list) {
283                         DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
284                         first = 0;
285                 }
286         }
287         DB((dbg, LEVEL_2, "\n}\n"));
288 }  /* dump_partition */
289
290 /**
291  * Dumps a list.
292  */
293 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
294         const node_t *p;
295         int          first = 1;
296
297 #define GET_LINK(p, ofs)  *((const node_t **)((char *)(p) + (ofs)))
298
299         DB((dbg, LEVEL_3, "%s = {\n  ", msg));
300         for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
301                 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
302                 first = 0;
303         }
304         DB((dbg, LEVEL_3, "\n}\n"));
305
306 #undef GET_LINK
307 }
308
309 /**
310  * Dumps a race list.
311  */
312 static void dump_race_list(const char *msg, const node_t *list) {
313         do_dump_list(msg, list, offsetof(node_t, race_next));
314 }
315
316 /**
317  * Dumps a local list.
318  */
319 static void dump_list(const char *msg, const node_t *list) {
320         do_dump_list(msg, list, offsetof(node_t, next));
321 }
322
323 /**
324  * Dump all partitions.
325  */
326 static void dump_all_partitions(const environment_t *env) {
327         const partition_t *P;
328
329         DB((dbg, LEVEL_2, "All partitions\n===============\n"));
330         for (P = env->dbg_list; P != NULL; P = P->dbg_next)
331                 dump_partition("", P);
332 }
333
334 #else
335 #define dump_partition(msg, part)
336 #define dump_race_list(msg, list)
337 #define dump_list(msg, list)
338 #define dump_all_partitions(env)
339 #endif
340
341 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
342 /**
343  * Verify that a type transition is monotone
344  */
345 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
346         if (old_type.tv == new_type.tv) {
347                 /* no change */
348                 return;
349         }
350         if (old_type.tv == tarval_top) {
351                 /* from Top down-to is always allowed */
352                 return;
353         }
354         if (old_type.tv == tarval_reachable) {
355                 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
356         }
357         if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
358                 /* bottom reached */
359                 return;
360         }
361         panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
362 }
363 #else
364 #define verify_type(old_type, new_type)
365 #endif
366
367 /**
368  * Compare two pointer values of a listmap.
369  */
370 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
371         const listmap_entry_t *e1 = elt;
372         const listmap_entry_t *e2 = key;
373
374         (void) size;
375         return e1->id != e2->id;
376 }  /* listmap_cmp_ptr */
377
378 /**
379  * Initializes a listmap.
380  *
381  * @param map  the listmap
382  */
383 static void listmap_init(listmap_t *map) {
384         map->map    = new_set(listmap_cmp_ptr, 16);
385         map->values = NULL;
386 }  /* listmap_init */
387
388 /**
389  * Terminates a listmap.
390  *
391  * @param map  the listmap
392  */
393 static void listmap_term(listmap_t *map) {
394         del_set(map->map);
395 }  /* listmap_term */
396
397 /**
398  * Return the associated listmap entry for a given id.
399  *
400  * @param map  the listmap
401  * @param id   the id to search for
402  *
403  * @return the asociated listmap entry for the given id
404  */
405 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
406         listmap_entry_t key, *entry;
407
408         key.id   = id;
409         key.list = NULL;
410         key.next = NULL;
411         entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
412
413         if (entry->list == NULL) {
414                 /* a new entry, put into the list */
415                 entry->next = map->values;
416                 map->values = entry;
417         }
418         return entry;
419 }  /* listmap_find */
420
421 /**
422  * Calculate the hash value for an opcode map entry.
423  *
424  * @param entry  an opcode map entry
425  *
426  * @return a hash value for the given opcode map entry
427  */
428 static unsigned opcode_hash(const opcode_key_t *entry) {
429         return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
430 }  /* opcode_hash */
431
432 /**
433  * Compare two entries in the opcode map.
434  */
435 static int cmp_opcode(const void *elt, const void *key, size_t size) {
436         const opcode_key_t *o1 = elt;
437         const opcode_key_t *o2 = key;
438
439         (void) size;
440         return o1->code != o2->code || o1->mode != o2->mode ||
441                o1->arity != o2->arity ||
442                o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
443 }  /* cmp_opcode */
444
445 /**
446  * Compare two Def-Use edges for input position.
447  */
448 static int cmp_def_use_edge(const void *a, const void *b) {
449         const ir_def_use_edge *ea = a;
450         const ir_def_use_edge *eb = b;
451
452         /* no overrun, because range is [-1, MAXINT] */
453         return ea->pos - eb->pos;
454 }  /* cmp_def_use_edge */
455
456 /**
457  * We need the Def-Use edges sorted.
458  */
459 static void sort_irn_outs(node_t *node) {
460         ir_node *irn = node->node;
461         int n_outs = get_irn_n_outs(irn);
462
463         if (n_outs > 1) {
464                 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
465         }
466         node->max_user_input = irn->out[n_outs].pos;
467 }  /* sort_irn_outs */
468
469 /**
470  * Return the type of a node.
471  *
472  * @param irn  an IR-node
473  *
474  * @return the associated type of this node
475  */
476 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
477         return get_irn_node(irn)->type;
478 }  /* get_node_type */
479
480 /**
481  * Return the tarval of a node.
482  *
483  * @param irn  an IR-node
484  *
485  * @return the associated type of this node
486  */
487 static INLINE tarval *get_node_tarval(const ir_node *irn) {
488         lattice_elem_t type = get_node_type(irn);
489
490         if (is_tarval(type.tv))
491                 return type.tv;
492         return tarval_bottom;
493 }  /* get_node_type */
494
495 /**
496  * Add a partition to the worklist.
497  */
498 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
499         assert(X->on_worklist == 0);
500         X->wl_next     = env->worklist;
501         X->on_worklist = 1;
502         env->worklist  = X;
503 }  /* add_to_worklist */
504
505 /**
506  * Create a new empty partition.
507  *
508  * @param env   the environment
509  *
510  * @return a newly allocated partition
511  */
512 static INLINE partition_t *new_partition(environment_t *env) {
513         partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
514
515         INIT_LIST_HEAD(&part->Leader);
516         INIT_LIST_HEAD(&part->Follower);
517         INIT_LIST_HEAD(&part->cprop);
518         part->wl_next         = NULL;
519         part->touched_next    = NULL;
520         part->cprop_next      = NULL;
521         part->split_next      = NULL;
522         part->touched         = NULL;
523         part->n_leader        = 0;
524         part->n_touched       = 0;
525         part->max_user_inputs = 0;
526         part->on_worklist     = 0;
527         part->on_touched      = 0;
528         part->on_cprop        = 0;
529         part->type_is_T_or_C  = 0;
530 #ifdef DEBUG_libfirm
531         part->dbg_next        = env->dbg_list;
532         env->dbg_list         = part;
533         part->nr              = part_nr++;
534 #endif
535
536         return part;
537 }  /* new_partition */
538
539 /**
540  * Get the first node from a partition.
541  */
542 static INLINE node_t *get_first_node(const partition_t *X) {
543         return list_entry(X->Leader.next, node_t, node_list);
544 }  /* get_first_node */
545
546 /**
547  * Return the type of a partition (assuming partition is non-empty and
548  * all elements have the same type).
549  *
550  * @param X  a partition
551  *
552  * @return the type of the first element of the partition
553  */
554 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
555         const node_t *first = get_first_node(X);
556         return first->type;
557 }  /* get_partition_type */
558
559 /**
560  * Creates a partition node for the given IR-node and place it
561  * into the given partition.
562  *
563  * @param irn   an IR-node
564  * @param part  a partition to place the node in
565  * @param env   the environment
566  *
567  * @return the created node
568  */
569 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
570         /* create a partition node and place it in the partition */
571         node_t *node = obstack_alloc(&env->obst, sizeof(*node));
572
573         INIT_LIST_HEAD(&node->node_list);
574         INIT_LIST_HEAD(&node->cprop_list);
575         node->node           = irn;
576         node->part           = part;
577         node->next           = NULL;
578         node->race_next      = NULL;
579         node->type.tv        = tarval_top;
580         node->max_user_input = 0;
581         node->next_edge      = 0;
582         node->n_followers    = 0;
583         node->on_touched     = 0;
584         node->on_cprop       = 0;
585         node->on_fallen      = 0;
586         node->is_follower    = 0;
587         node->by_all_const   = 0;
588         node->flagged        = 0;
589         set_irn_node(irn, node);
590
591         list_add_tail(&node->node_list, &part->Leader);
592         ++part->n_leader;
593
594         return node;
595 }  /* create_partition_node */
596
597 /**
598  * Pre-Walker, init all Block-Phi lists.
599  */
600 static void init_block_phis(ir_node *irn, void *env) {
601         (void) env;
602
603         if (is_Block(irn)) {
604                 set_Block_phis(irn, NULL);
605         }
606 }  /* init_block_phis */
607
608 /**
609  * Post-Walker, initialize all Nodes' type to U or top and place
610  * all nodes into the TOP partition.
611  */
612 static void create_initial_partitions(ir_node *irn, void *ctx) {
613         environment_t *env  = ctx;
614         partition_t   *part = env->initial;
615         node_t        *node;
616
617         node = create_partition_node(irn, part, env);
618         sort_irn_outs(node);
619         if (node->max_user_input > part->max_user_inputs)
620                 part->max_user_inputs = node->max_user_input;
621
622         if (is_Phi(irn)) {
623                 add_Block_phi(get_nodes_block(irn), irn);
624         } else if (is_Cond(irn)) {
625                 /* check if all Cond's have a Cmp predecessor. */
626                 if (get_irn_mode(irn) == mode_b && !is_Cmp(skip_Proj(get_Cond_selector(irn))))
627                         env->nonstd_cond = 1;
628
629         }
630 }  /* create_initial_partitions */
631
632 /**
633  * Add a node to the entry.partition.touched set and
634  * node->partition to the touched set if not already there.
635  *
636  * @param y    a node
637  * @param env  the environment
638  */
639 static INLINE void add_to_touched(node_t *y, environment_t *env) {
640         if (y->on_touched == 0) {
641                 partition_t *part = y->part;
642
643                 y->next       = part->touched;
644                 part->touched = y;
645                 y->on_touched = 1;
646                 ++part->n_touched;
647
648                 if (part->on_touched == 0) {
649                         part->touched_next = env->touched;
650                         env->touched       = part;
651                         part->on_touched   = 1;
652                 }
653
654                 check_list(part->touched, part);
655         }
656 }  /* add_to_touched */
657
658 /**
659  * Place a node on the cprop list.
660  *
661  * @param y    the node
662  * @param env  the environment
663  */
664 static void add_to_cprop(node_t *y, environment_t *env) {
665         /* Add y to y.partition.cprop. */
666         if (y->on_cprop == 0) {
667                 partition_t *Y = y->part;
668
669                 list_add_tail(&y->cprop_list, &Y->cprop);
670                 y->on_cprop   = 1;
671
672                 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
673
674                 /* place its partition on the cprop list */
675                 if (Y->on_cprop == 0) {
676                         Y->cprop_next = env->cprop;
677                         env->cprop    = Y;
678                         Y->on_cprop   = 1;
679                 }
680         }
681         if (get_irn_mode(y->node) == mode_T) {
682                 /* mode_T nodes always produce tarval_bottom, so we must explicitly
683                    add it's Proj's to get constant evaluation to work */
684                 int i;
685
686                 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
687                         node_t *proj = get_irn_node(get_irn_out(y->node, i));
688
689                         add_to_cprop(proj, env);
690                 }
691         } else if (is_Block(y->node)) {
692                 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
693                  * if someone placed the block. The Block is only placed if the reachability
694                  * changes, and this must be re-evaluated in compute_Phi(). */
695                 ir_node *phi;
696                 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
697                         node_t *p = get_irn_node(phi);
698                         add_to_cprop(p, env);
699                 }
700         }
701 }  /* add_to_cprop */
702
703 /**
704  * Update the worklist: If Z is on worklist then add Z' to worklist.
705  * Else add the smaller of Z and Z' to worklist.
706  *
707  * @param Z        the Z partition
708  * @param Z_prime  the Z' partition, a previous part of Z
709  * @param env      the environment
710  */
711 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
712         if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
713                 add_to_worklist(Z_prime, env);
714         } else {
715                 add_to_worklist(Z, env);
716         }
717 }  /* update_worklist */
718
719 /**
720  * Make all inputs to x no longer be F.def_use edges.
721  *
722  * @param x  the node
723  */
724 static void move_edges_to_leader(node_t *x) {
725         ir_node     *irn = x->node;
726         int         i, j, k;
727
728         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
729                 node_t  *pred = get_irn_node(get_irn_n(irn, i));
730                 ir_node *p;
731                 int     n;
732
733                 p = pred->node;
734                 n = get_irn_n_outs(p);
735                 for (j = 1; j <= pred->n_followers; ++j) {
736                         if (p->out[j].pos == i && p->out[j].use == irn) {
737                                 /* found a follower edge to x, move it to the Leader */
738                                 ir_def_use_edge edge = p->out[j];
739
740                                 /* remove this edge from the Follower set */
741                                 p->out[j] = p->out[pred->n_followers];
742                                 --pred->n_followers;
743
744                                 /* sort it into the leader set */
745                                 for (k = pred->n_followers + 2; k <= n; ++k) {
746                                         if (p->out[k].pos >= edge.pos)
747                                                 break;
748                                         p->out[k - 1] = p->out[k];
749                                 }
750                                 /* place the new edge here */
751                                 p->out[k - 1] = edge;
752
753                                 /* edge found and moved */
754                                 break;
755                         }
756                 }
757         }
758 }  /* move_edges_to_leader */
759
760 /**
761  * Split a partition that has NO followers by a local list.
762  *
763  * @param Z    partition to split
764  * @param g    a (non-empty) node list
765  * @param env  the environment
766  *
767  * @return  a new partition containing the nodes of g
768  */
769 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
770         partition_t *Z_prime;
771         node_t      *node;
772         unsigned    n = 0;
773         int         max_input;
774
775         dump_partition("Splitting ", Z);
776         dump_list("by list ", g);
777
778         assert(g != NULL);
779
780         /* Remove g from Z. */
781         for (node = g; node != NULL; node = node->next) {
782                 assert(node->part == Z);
783                 list_del(&node->node_list);
784                 ++n;
785         }
786         assert(n < Z->n_leader);
787         Z->n_leader -= n;
788
789         /* Move g to a new partition, Z'. */
790         Z_prime = new_partition(env);
791         max_input = 0;
792         for (node = g; node != NULL; node = node->next) {
793                 list_add_tail(&node->node_list, &Z_prime->Leader);
794                 node->part = Z_prime;
795                 if (node->max_user_input > max_input)
796                         max_input = node->max_user_input;
797         }
798         Z_prime->max_user_inputs = max_input;
799         Z_prime->n_leader        = n;
800
801         check_partition(Z);
802         check_partition(Z_prime);
803
804         /* for now, copy the type info tag, it will be adjusted in split_by(). */
805         Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
806
807         update_worklist(Z, Z_prime, env);
808
809         dump_partition("Now ", Z);
810         dump_partition("Created new ", Z_prime);
811         return Z_prime;
812 }  /* split_no_followers */
813
814 /**
815  * Make the Follower -> Leader transition for a node.
816  *
817  * @param n  the node
818  */
819 static void follower_to_leader(node_t *n) {
820         assert(n->is_follower == 1);
821
822         DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
823         n->is_follower = 0;
824         move_edges_to_leader(n);
825         list_del(&n->node_list);
826         list_add_tail(&n->node_list, &n->part->Leader);
827         ++n->part->n_leader;
828 }  /* follower_to_leader */
829
830 /**
831  * The environment for one race step.
832  */
833 typedef struct step_env {
834         node_t   *initial;    /**< The initial node list. */
835         node_t   *unwalked;   /**< The unwalked node list. */
836         node_t   *walked;     /**< The walked node list. */
837         int      index;       /**< Next index of Follower use_def edge. */
838         unsigned side;        /**< side number. */
839 } step_env;
840
841 /**
842  * Return non-zero, if a input is a real follower
843  *
844  * @param irn    the node to check
845  * @param input  number of the input
846  */
847 static int is_real_follower(const ir_node *irn, int input) {
848         node_t *pred;
849
850         switch (get_irn_opcode(irn)) {
851         case iro_Confirm:
852                 if (input == 1) {
853                         /* ignore the Confirm bound input */
854                         return 0;
855                 }
856                 break;
857         case iro_Mux:
858                 if (input == 0) {
859                         /* ignore the Mux sel input */
860                         return 0;
861                 }
862                 break;
863         case iro_Phi: {
864                 /* dead inputs are not follower edges */
865                 ir_node *block = get_nodes_block(irn);
866                 node_t  *pred  = get_irn_node(get_Block_cfgpred(block, input));
867
868                 if (pred->type.tv == tarval_unreachable)
869                         return 0;
870                 break;
871         }
872         case iro_Sub:
873         case iro_Shr:
874         case iro_Shl:
875         case iro_Shrs:
876         case iro_Rotl:
877                 if (input == 1) {
878                         /* only a Sub x,0 / Shift x,0 might be a follower */
879                         return 0;
880                 }
881                 break;
882         case iro_Add:
883         case iro_Or:
884         case iro_Eor:
885                 pred = get_irn_node(get_irn_n(irn, input));
886                 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
887                         return 0;
888                 break;
889         case iro_Mul:
890                 pred = get_irn_node(get_irn_n(irn, input));
891                 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
892                         return 0;
893                 break;
894         case iro_And:
895                 pred = get_irn_node(get_irn_n(irn, input));
896                 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
897                         return 0;
898                 break;
899         case iro_Min:
900         case iro_Max:
901                 /* all inputs are followers */
902                 return 1;
903         default:
904                 assert(!"opcode not implemented yet");
905                 break;
906         }
907         return 1;
908 }
909
910 /**
911  * Do one step in the race.
912  */
913 static int step(step_env *env) {
914         node_t *n;
915
916         if (env->initial != NULL) {
917                 /* Move node from initial to unwalked */
918                 n             = env->initial;
919                 env->initial  = n->race_next;
920
921                 n->race_next  = env->unwalked;
922                 env->unwalked = n;
923
924                 return 0;
925         }
926
927         while (env->unwalked != NULL) {
928                 /* let n be the first node in unwalked */
929                 n = env->unwalked;
930                 while (env->index < n->n_followers) {
931                         const ir_def_use_edge *edge = &n->node->out[1 + env->index];
932
933                         /* let m be n.F.def_use[index] */
934                         node_t *m = get_irn_node(edge->use);
935
936                         assert(m->is_follower);
937                         /*
938                          * Some inputs, like the get_Confirm_bound are NOT
939                          * real followers, sort them out.
940                          */
941                         if (! is_real_follower(m->node, edge->pos)) {
942                                 ++env->index;
943                                 continue;
944                         }
945                         ++env->index;
946
947                         /* only followers from our partition */
948                         if (m->part != n->part)
949                                 continue;
950
951                         if ((m->flagged & env->side) == 0) {
952                                 m->flagged |= env->side;
953
954                                 if (m->flagged != 3) {
955                                         /* visited the first time */
956                                         /* add m to unwalked not as first node (we might still need to
957                                            check for more follower node */
958                                         m->race_next = n->race_next;
959                                         n->race_next = m;
960                                         return 0;
961                                 }
962                                 /* else already visited by the other side and on the other list */
963                         }
964                 }
965                 /* move n to walked */
966                 env->unwalked = n->race_next;
967                 n->race_next  = env->walked;
968                 env->walked   = n;
969                 env->index    = 0;
970         }
971         return 1;
972 }  /* step */
973
974 /**
975  * Clear the flags from a list and check for
976  * nodes that where touched from both sides.
977  *
978  * @param list  the list
979  */
980 static int clear_flags(node_t *list) {
981         int    res = 0;
982         node_t *n;
983
984         for (n = list; n != NULL; n = n->race_next) {
985                 if (n->flagged == 3) {
986                         /* we reach a follower from both sides, this will split congruent
987                          * inputs and make it a leader. */
988                         follower_to_leader(n);
989                         res = 1;
990                 }
991                 n->flagged = 0;
992         }
993         return res;
994 }  /* clear_flags */
995
996 /**
997  * Split a partition by a local list using the race.
998  *
999  * @param pX   pointer to the partition to split, might be changed!
1000  * @param gg   a (non-empty) node list
1001  * @param env  the environment
1002  *
1003  * @return  a new partition containing the nodes of gg
1004  */
1005 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1006         partition_t *X = *pX;
1007         partition_t *X_prime;
1008         list_head   tmp;
1009         step_env    env1, env2, *winner;
1010         node_t      *g, *h, *node, *t;
1011         int         max_input, transitions;
1012         unsigned    n;
1013         DEBUG_ONLY(static int run = 0;)
1014
1015         DB((dbg, LEVEL_2, "Run %d ", run++));
1016         if (list_empty(&X->Follower)) {
1017                 /* if the partition has NO follower, we can use the fast
1018                    splitting algorithm. */
1019                 return split_no_followers(X, gg, env);
1020         }
1021         /* else do the race */
1022
1023         dump_partition("Splitting ", X);
1024         dump_list("by list ", gg);
1025
1026         INIT_LIST_HEAD(&tmp);
1027
1028         /* Remove gg from X.Leader and put into g */
1029         g = NULL;
1030         for (node = gg; node != NULL; node = node->next) {
1031                 assert(node->part == X);
1032                 assert(node->is_follower == 0);
1033
1034                 list_del(&node->node_list);
1035                 list_add_tail(&node->node_list, &tmp);
1036                 node->race_next = g;
1037                 g               = node;
1038         }
1039         /* produce h */
1040         h = NULL;
1041         list_for_each_entry(node_t, node, &X->Leader, node_list) {
1042                 node->race_next = h;
1043                 h               = node;
1044         }
1045         /* restore X.Leader */
1046         list_splice(&tmp, &X->Leader);
1047
1048         env1.initial       = g;
1049         env1.unwalked      = NULL;
1050         env1.walked        = NULL;
1051         env1.index         = 0;
1052         env1.side          = 1;
1053
1054         env2.initial       = h;
1055         env2.unwalked      = NULL;
1056         env2.walked        = NULL;
1057         env2.index         = 0;
1058         env2.side          = 2;
1059
1060         for (;;) {
1061                 if (step(&env1)) {
1062                         winner = &env1;
1063                         break;
1064                 }
1065                 if (step(&env2)) {
1066                         winner = &env2;
1067                         break;
1068                 }
1069         }
1070         assert(winner->initial == NULL);
1071         assert(winner->unwalked == NULL);
1072
1073         /* clear flags from walked/unwalked */
1074         transitions  = clear_flags(env1.unwalked);
1075         transitions |= clear_flags(env1.walked);
1076         transitions |= clear_flags(env2.unwalked);
1077         transitions |= clear_flags(env2.walked);
1078
1079         dump_race_list("winner ", winner->walked);
1080
1081         /* Move walked_{winner} to a new partition, X'. */
1082         X_prime   = new_partition(env);
1083         max_input = 0;
1084         n         = 0;
1085         for (node = winner->walked; node != NULL; node = node->race_next) {
1086                 list_del(&node->node_list);
1087                 node->part = X_prime;
1088                 if (node->is_follower) {
1089                         list_add_tail(&node->node_list, &X_prime->Follower);
1090                 } else {
1091                         list_add_tail(&node->node_list, &X_prime->Leader);
1092                         ++n;
1093                 }
1094                 if (node->max_user_input > max_input)
1095                         max_input = node->max_user_input;
1096         }
1097         X_prime->n_leader        = n;
1098         X_prime->max_user_inputs = max_input;
1099         X->n_leader             -= X_prime->n_leader;
1100
1101         /* for now, copy the type info tag, it will be adjusted in split_by(). */
1102         X_prime->type_is_T_or_C = X->type_is_T_or_C;
1103
1104         /*
1105          * Even if a follower was not checked by both sides, it might have
1106          * loose its congruence, so we need to check this case for all follower.
1107          */
1108         list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1109                 if (identity(node) == node) {
1110                         follower_to_leader(node);
1111                         transitions = 1;
1112                 }
1113         }
1114
1115         check_partition(X);
1116         check_partition(X_prime);
1117
1118         /* X' is the smaller part */
1119         add_to_worklist(X_prime, env);
1120
1121         /*
1122          * If there where follower to leader transitions, ensure that the nodes
1123          * can be split out if necessary.
1124          */
1125         if (transitions) {
1126                 /* place partitions on the cprop list */
1127                 if (X_prime->on_cprop == 0) {
1128                         X_prime->cprop_next = env->cprop;
1129                         env->cprop          = X_prime;
1130                         X_prime->on_cprop   = 1;
1131                 }
1132         }
1133
1134         dump_partition("Now ", X);
1135         dump_partition("Created new ", X_prime);
1136
1137         /* we have to ensure that the partition containing g is returned */
1138         if (winner == &env2) {
1139                 *pX = X_prime;
1140                 return X;
1141         }
1142
1143         return X_prime;
1144 }  /* split */
1145
1146 /**
1147  * Returns non-zero if the i'th input of a Phi node is live.
1148  *
1149  * @param phi  a Phi-node
1150  * @param i    an input number
1151  *
1152  * @return non-zero if the i'th input of the given Phi node is live
1153  */
1154 static int is_live_input(ir_node *phi, int i) {
1155         if (i >= 0) {
1156                 ir_node        *block = get_nodes_block(phi);
1157                 ir_node        *pred  = get_Block_cfgpred(block, i);
1158                 lattice_elem_t type   = get_node_type(pred);
1159
1160                 return type.tv != tarval_unreachable;
1161         }
1162         /* else it's the control input, always live */
1163         return 1;
1164 }  /* is_live_input */
1165
1166 /**
1167  * Return non-zero if a type is a constant.
1168  */
1169 static int is_constant_type(lattice_elem_t type) {
1170         if (type.tv != tarval_bottom && type.tv != tarval_top)
1171                 return 1;
1172         return 0;
1173 }  /* is_constant_type */
1174
1175 /**
1176  * Check whether a type is neither Top or a constant.
1177  * Note: U is handled like Top here, R is a constant.
1178  *
1179  * @param type  the type to check
1180  */
1181 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1182         if (is_tarval(type.tv)) {
1183                 if (type.tv == tarval_top)
1184                         return 0;
1185                 if (tarval_is_constant(type.tv))
1186                         return 0;
1187         } else {
1188                 /* is a symconst */
1189                 return 0;
1190         }
1191         return 1;
1192 }
1193
1194 /**
1195  * Collect nodes to the touched list.
1196  *
1197  * @param list  the list which contains the nodes that must be evaluated
1198  * @param idx   the index of the def_use edge to evaluate
1199  * @param env   the environment
1200  */
1201 static void collect_touched(list_head *list, int idx, environment_t *env) {
1202         node_t  *x, *y;
1203         int     end_idx = env->end_idx;
1204
1205         list_for_each_entry(node_t, x, list, node_list) {
1206                 int num_edges;
1207
1208                 if (idx == -1) {
1209                         /* leader edges start AFTER follower edges */
1210                         x->next_edge = x->n_followers + 1;
1211                 }
1212                 num_edges = get_irn_n_outs(x->node);
1213
1214                 /* for all edges in x.L.def_use_{idx} */
1215                 while (x->next_edge <= num_edges) {
1216                         const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1217                         ir_node               *succ;
1218
1219                         /* check if we have necessary edges */
1220                         if (edge->pos > idx)
1221                                 break;
1222
1223                         ++x->next_edge;
1224
1225                         succ = edge->use;
1226
1227                         /* ignore the "control input" for non-pinned nodes
1228                         if we are running in GCSE mode */
1229                         if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1230                                 continue;
1231
1232                         y = get_irn_node(succ);
1233                         assert(get_irn_n(succ, idx) == x->node);
1234
1235                         /* ignore block edges touching followers */
1236                         if (idx == -1 && y->is_follower)
1237                                 continue;
1238
1239                         if (is_constant_type(y->type)) {
1240                                 ir_opcode code = get_irn_opcode(succ);
1241                                 if (code == iro_Sub || code == iro_Eor || code == iro_Cmp)
1242                                         add_to_cprop(y, env);
1243                         }
1244
1245                         /* Partitions of constants should not be split simply because their Nodes have unequal
1246                            functions or incongruent inputs. */
1247                         if (type_is_neither_top_nor_const(y->type) &&
1248                                 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1249                                         add_to_touched(y, env);
1250                         }
1251                 }
1252         }
1253 }  /* collect_touched */
1254
1255 /**
1256  * Split the partitions if caused by the first entry on the worklist.
1257  *
1258  * @param env  the environment
1259  */
1260 static void cause_splits(environment_t *env) {
1261         partition_t *X, *Z, *N;
1262         int         idx;
1263
1264         /* remove the first partition from the worklist */
1265         X = env->worklist;
1266         env->worklist  = X->wl_next;
1267         X->on_worklist = 0;
1268
1269         dump_partition("Cause_split: ", X);
1270
1271         /* combine temporary leader and follower list */
1272         for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1273                 /* empty the touched set: already done, just clear the list */
1274                 env->touched = NULL;
1275
1276                 collect_touched(&X->Leader, idx, env);
1277                 collect_touched(&X->Follower, idx, env);
1278
1279                 for (Z = env->touched; Z != NULL; Z = N) {
1280                         node_t   *e;
1281                         node_t   *touched  = Z->touched;
1282                         unsigned n_touched = Z->n_touched;
1283
1284                         assert(Z->touched != NULL);
1285
1286                         /* beware, split might change Z */
1287                         N = Z->touched_next;
1288
1289                         /* remove it from the touched set */
1290                         Z->on_touched = 0;
1291
1292                         /* Empty local Z.touched. */
1293                         for (e = touched; e != NULL; e = e->next) {
1294                                 assert(e->is_follower == 0);
1295                                 e->on_touched = 0;
1296                         }
1297                         Z->touched   = NULL;
1298                         Z->n_touched = 0;
1299
1300                         if (0 < n_touched && n_touched < Z->n_leader) {
1301                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1302                                 split(&Z, touched, env);
1303                         } else
1304                                 assert(n_touched <= Z->n_leader);
1305                 }
1306         }
1307 }  /* cause_splits */
1308
1309 /**
1310  * Implements split_by_what(): Split a partition by characteristics given
1311  * by the what function.
1312  *
1313  * @param X     the partition to split
1314  * @param What  a function returning an Id for every node of the partition X
1315  * @param P     a list to store the result partitions
1316  * @param env   the environment
1317  *
1318  * @return *P
1319  */
1320 static partition_t *split_by_what(partition_t *X, what_func What,
1321                                   partition_t **P, environment_t *env) {
1322         node_t          *x, *S;
1323         listmap_t       map;
1324         listmap_entry_t *iter;
1325         partition_t     *R;
1326
1327         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1328         listmap_init(&map);
1329         list_for_each_entry(node_t, x, &X->Leader, node_list) {
1330                 void            *id = What(x, env);
1331                 listmap_entry_t *entry;
1332
1333                 if (id == NULL) {
1334                         /* input not allowed, ignore */
1335                         continue;
1336                 }
1337                 /* Add x to map[What(x)]. */
1338                 entry = listmap_find(&map, id);
1339                 x->next     = entry->list;
1340                 entry->list = x;
1341         }
1342         /* Let P be a set of Partitions. */
1343
1344         /* for all sets S except one in the range of map do */
1345         for (iter = map.values; iter != NULL; iter = iter->next) {
1346                 if (iter->next == NULL) {
1347                         /* this is the last entry, ignore */
1348                         break;
1349                 }
1350                 S = iter->list;
1351
1352                 /* Add SPLIT( X, S ) to P. */
1353                 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1354                 R = split(&X, S, env);
1355                 R->split_next = *P;
1356                 *P            = R;
1357         }
1358         /* Add X to P. */
1359         X->split_next = *P;
1360         *P            = X;
1361
1362         listmap_term(&map);
1363         return *P;
1364 }  /* split_by_what */
1365
1366 /** lambda n.(n.type) */
1367 static void *lambda_type(const node_t *node, environment_t *env) {
1368         (void)env;
1369         return node->type.tv;
1370 }  /* lambda_type */
1371
1372 /** lambda n.(n.opcode) */
1373 static void *lambda_opcode(const node_t *node, environment_t *env) {
1374         opcode_key_t key, *entry;
1375         ir_node      *irn = node->node;
1376
1377         key.code   = get_irn_opcode(irn);
1378         key.mode   = get_irn_mode(irn);
1379         key.arity  = get_irn_arity(irn);
1380         key.u.proj = 0;
1381         key.u.ent  = NULL;
1382
1383         switch (get_irn_opcode(irn)) {
1384         case iro_Proj:
1385                 key.u.proj = get_Proj_proj(irn);
1386                 break;
1387         case iro_Sel:
1388                 key.u.ent = get_Sel_entity(irn);
1389                 break;
1390         default:
1391                 break;
1392         }
1393
1394         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1395         return entry;
1396 }  /* lambda_opcode */
1397
1398 /** lambda n.(n[i].partition) */
1399 static void *lambda_partition(const node_t *node, environment_t *env) {
1400         ir_node *skipped = skip_Proj(node->node);
1401         ir_node *pred;
1402         node_t  *p;
1403         int     i = env->lambda_input;
1404
1405         if (i >= get_irn_arity(node->node)) {
1406                 /* we are outside the allowed range */
1407                 return NULL;
1408         }
1409
1410         /* ignore the "control input" for non-pinned nodes
1411            if we are running in GCSE mode */
1412         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1413                 return NULL;
1414
1415         pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1416         p    = get_irn_node(pred);
1417
1418         return p->part;
1419 }  /* lambda_partition */
1420
1421 /**
1422  * Returns true if a type is a constant.
1423  */
1424 static int is_con(const lattice_elem_t type) {
1425         /* be conservative */
1426         if (is_tarval(type.tv))
1427                 return tarval_is_constant(type.tv);
1428         return is_entity(type.sym.entity_p);
1429 }  /* is_con */
1430
1431 /**
1432  * Implements split_by().
1433  *
1434  * @param X    the partition to split
1435  * @param env  the environment
1436  */
1437 static void split_by(partition_t *X, environment_t *env) {
1438         partition_t *I, *P = NULL;
1439         int         input;
1440
1441         dump_partition("split_by", X);
1442
1443         if (X->n_leader == 1) {
1444                 /* we have only one leader, no need to split, just check it's type */
1445                 node_t *x = get_first_node(X);
1446                 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1447                 return;
1448         }
1449
1450         DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1451         P = split_by_what(X, lambda_type, &P, env);
1452
1453         /* adjust the type tags, we have split partitions by type */
1454         for (I = P; I != NULL; I = I->split_next) {
1455                 node_t *x = get_first_node(I);
1456                 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1457         }
1458
1459         do {
1460                 partition_t *Y = P;
1461
1462                 P = P->split_next;
1463                 if (Y->n_leader > 1) {
1464                         /* we do not want split the TOP or constant partitions */
1465                         if (! Y->type_is_T_or_C) {
1466                                 partition_t *Q = NULL;
1467
1468                                 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1469                                 Q = split_by_what(Y, lambda_opcode, &Q, env);
1470
1471                                 do {
1472                                         partition_t *Z = Q;
1473
1474                                         Q = Q->split_next;
1475                                         if (Z->n_leader > 1) {
1476                                                 const node_t *first = get_first_node(Z);
1477                                                 int          arity  = get_irn_arity(first->node);
1478                                                 partition_t  *R, *S;
1479
1480                                                 /*
1481                                                  * BEWARE: during splitting by input 2 for instance we might
1482                                                  * create new partitions which are different by input 1, so collect
1483                                                  * them and split further.
1484                                                  */
1485                                                 Z->split_next = NULL;
1486                                                 R             = Z;
1487                                                 S             = NULL;
1488                                                 for (input = arity - 1; input >= -1; --input) {
1489                                                         do {
1490                                                                 partition_t *Z_prime = R;
1491
1492                                                                 R = R->split_next;
1493                                                                 if (Z_prime->n_leader > 1) {
1494                                                                         env->lambda_input = input;
1495                                                                         DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1496                                                                         S = split_by_what(Z_prime, lambda_partition, &S, env);
1497                                                                 } else {
1498                                                                         Z_prime->split_next = S;
1499                                                                         S                   = Z_prime;
1500                                                                 }
1501                                                         } while (R != NULL);
1502                                                         R = S;
1503                                                         S = NULL;
1504                                                 }
1505                                         }
1506                                 } while (Q != NULL);
1507                         }
1508                 }
1509         } while (P != NULL);
1510 }  /* split_by */
1511
1512 /**
1513  * (Re-)compute the type for a given node.
1514  *
1515  * @param node  the node
1516  */
1517 static void default_compute(node_t *node) {
1518         int     i;
1519         ir_node *irn = node->node;
1520         node_t  *block = get_irn_node(get_nodes_block(irn));
1521
1522         if (block->type.tv == tarval_unreachable) {
1523                 node->type.tv = tarval_top;
1524                 return;
1525         }
1526
1527         /* if any of the data inputs have type top, the result is type top */
1528         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1529                 ir_node *pred = get_irn_n(irn, i);
1530                 node_t  *p    = get_irn_node(pred);
1531
1532                 if (p->type.tv == tarval_top) {
1533                         node->type.tv = tarval_top;
1534                         return;
1535                 }
1536         }
1537
1538         if (get_irn_mode(node->node) == mode_X)
1539                 node->type.tv = tarval_reachable;
1540         else
1541                 node->type.tv = computed_value(irn);
1542 }  /* default_compute */
1543
1544 /**
1545  * (Re-)compute the type for a Block node.
1546  *
1547  * @param node  the node
1548  */
1549 static void compute_Block(node_t *node) {
1550         int     i;
1551         ir_node *block = node->node;
1552
1553         if (block == get_irg_start_block(current_ir_graph)) {
1554                 /* start block is always reachable */
1555                 node->type.tv = tarval_reachable;
1556                 return;
1557         }
1558
1559         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1560                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1561
1562                 if (pred->type.tv == tarval_reachable) {
1563                         /* A block is reachable, if at least of predecessor is reachable. */
1564                         node->type.tv = tarval_reachable;
1565                         return;
1566                 }
1567         }
1568         node->type.tv = tarval_top;
1569 }  /* compute_Block */
1570
1571 /**
1572  * (Re-)compute the type for a Bad node.
1573  *
1574  * @param node  the node
1575  */
1576 static void compute_Bad(node_t *node) {
1577         /* Bad nodes ALWAYS compute Top */
1578         node->type.tv = tarval_top;
1579 }  /* compute_Bad */
1580
1581 /**
1582  * (Re-)compute the type for an Unknown node.
1583  *
1584  * @param node  the node
1585  */
1586 static void compute_Unknown(node_t *node) {
1587         /* While Unknown nodes should compute Top this is dangerous:
1588          * a Top input to a Cond would lead to BOTH control flows unreachable.
1589          * While this is correct in the given semantics, it would destroy the Firm
1590          * graph.
1591          *
1592          * It would be safe to compute Top IF it can be assured, that only Cmp
1593          * nodes are inputs to Conds. We check that first.
1594          * This is the way Frontends typically build Firm, but some optimizations
1595          * (cond_eval for instance) might replace them by Phib's...
1596          */
1597         node->type.tv = tarval_UNKNOWN;
1598 }  /* compute_Unknown */
1599
1600 /**
1601  * (Re-)compute the type for a Jmp node.
1602  *
1603  * @param node  the node
1604  */
1605 static void compute_Jmp(node_t *node) {
1606         node_t *block = get_irn_node(get_nodes_block(node->node));
1607
1608         node->type = block->type;
1609 }  /* compute_Jmp */
1610
1611 /**
1612  * (Re-)compute the type for the End node.
1613  *
1614  * @param node  the node
1615  */
1616 static void compute_End(node_t *node) {
1617         /* the End node is NOT dead of course */
1618         node->type.tv = tarval_reachable;
1619 }
1620
1621 /**
1622  * (Re-)compute the type for a SymConst node.
1623  *
1624  * @param node  the node
1625  */
1626 static void compute_SymConst(node_t *node) {
1627         ir_node *irn = node->node;
1628         node_t  *block = get_irn_node(get_nodes_block(irn));
1629
1630         if (block->type.tv == tarval_unreachable) {
1631                 node->type.tv = tarval_top;
1632                 return;
1633         }
1634         switch (get_SymConst_kind(irn)) {
1635         case symconst_addr_ent:
1636         /* case symconst_addr_name: cannot handle this yet */
1637                 node->type.sym = get_SymConst_symbol(irn);
1638                 break;
1639         default:
1640                 node->type.tv = computed_value(irn);
1641         }
1642 }  /* compute_SymConst */
1643
1644 /**
1645  * (Re-)compute the type for a Phi node.
1646  *
1647  * @param node  the node
1648  */
1649 static void compute_Phi(node_t *node) {
1650         int            i;
1651         ir_node        *phi = node->node;
1652         lattice_elem_t type;
1653
1654         /* if a Phi is in a unreachable block, its type is TOP */
1655         node_t *block = get_irn_node(get_nodes_block(phi));
1656
1657         if (block->type.tv == tarval_unreachable) {
1658                 node->type.tv = tarval_top;
1659                 return;
1660         }
1661
1662         /* Phi implements the Meet operation */
1663         type.tv = tarval_top;
1664         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1665                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
1666                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1667
1668                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1669                         /* ignore TOP inputs: We must check here for unreachable blocks,
1670                            because Firm constants live in the Start Block are NEVER Top.
1671                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1672                            comes from a unreachable input. */
1673                         continue;
1674                 }
1675                 if (pred->type.tv == tarval_bottom) {
1676                         node->type.tv = tarval_bottom;
1677                         return;
1678                 } else if (type.tv == tarval_top) {
1679                         /* first constant found */
1680                         type = pred->type;
1681                 } else if (type.tv != pred->type.tv) {
1682                         /* different constants or tarval_bottom */
1683                         node->type.tv = tarval_bottom;
1684                         return;
1685                 }
1686                 /* else nothing, constants are the same */
1687         }
1688         node->type = type;
1689 }  /* compute_Phi */
1690
1691 /**
1692  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1693  *
1694  * @param node  the node
1695  */
1696 static void compute_Add(node_t *node) {
1697         ir_node        *sub = node->node;
1698         node_t         *l   = get_irn_node(get_Add_left(sub));
1699         node_t         *r   = get_irn_node(get_Add_right(sub));
1700         lattice_elem_t a    = l->type;
1701         lattice_elem_t b    = r->type;
1702         ir_mode        *mode;
1703
1704         if (a.tv == tarval_top || b.tv == tarval_top) {
1705                 node->type.tv = tarval_top;
1706         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1707                 node->type.tv = tarval_bottom;
1708         } else {
1709                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1710                    must call tarval_add() first to handle this case! */
1711                 if (is_tarval(a.tv)) {
1712                         if (is_tarval(b.tv)) {
1713                                 node->type.tv = tarval_add(a.tv, b.tv);
1714                                 return;
1715                         }
1716                         mode = get_tarval_mode(a.tv);
1717                         if (a.tv == get_mode_null(mode)) {
1718                                 node->type = b;
1719                                 return;
1720                         }
1721                 } else if (is_tarval(b.tv)) {
1722                         mode = get_tarval_mode(b.tv);
1723                         if (b.tv == get_mode_null(mode)) {
1724                                 node->type = a;
1725                                 return;
1726                         }
1727                 }
1728                 node->type.tv = tarval_bottom;
1729         }
1730 }  /* compute_Add */
1731
1732 /**
1733  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1734  *
1735  * @param node  the node
1736  */
1737 static void compute_Sub(node_t *node) {
1738         ir_node        *sub = node->node;
1739         node_t         *l   = get_irn_node(get_Sub_left(sub));
1740         node_t         *r   = get_irn_node(get_Sub_right(sub));
1741         lattice_elem_t a    = l->type;
1742         lattice_elem_t b    = r->type;
1743         tarval         *tv;
1744
1745         if (a.tv == tarval_top || b.tv == tarval_top) {
1746                 node->type.tv = tarval_top;
1747         } else if (is_con(a) && is_con(b)) {
1748                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1749                         node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1750                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1751                         node->type = b;
1752                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1753                         node->type = a;
1754                 } else {
1755                         node->type.tv = tarval_bottom;
1756                 }
1757                 node->by_all_const = 1;
1758         } else if (r->part == l->part &&
1759                    (!mode_is_float(get_irn_mode(l->node)))) {
1760                 /*
1761                  * BEWARE: a - a is NOT always 0 for floating Point values, as
1762                  * NaN op NaN = NaN, so we must check this here.
1763                  */
1764                 ir_mode *mode = get_irn_mode(sub);
1765                 tv = get_mode_null(mode);
1766
1767                 /* if the node was ONCE evaluated by all constants, but now
1768                    this breakes AND we cat by partition a different result, switch to bottom.
1769                    This happens because initially all nodes are in the same partition ... */
1770                 if (node->by_all_const && node->type.tv != tv)
1771                         tv = tarval_bottom;
1772                 node->type.tv = tv;
1773         } else {
1774                 node->type.tv = tarval_bottom;
1775         }
1776 }  /* compute_Sub */
1777
1778 /**
1779  * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
1780  *
1781  * @param node  the node
1782  */
1783 static void compute_Eor(node_t *node) {
1784         ir_node        *eor = node->node;
1785         node_t         *l   = get_irn_node(get_Eor_left(eor));
1786         node_t         *r   = get_irn_node(get_Eor_right(eor));
1787         lattice_elem_t a    = l->type;
1788         lattice_elem_t b    = r->type;
1789         tarval         *tv;
1790
1791         if (a.tv == tarval_top || b.tv == tarval_top) {
1792                 node->type.tv = tarval_top;
1793         } else if (is_con(a) && is_con(b)) {
1794                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1795                         node->type.tv = tarval_eor(a.tv, b.tv);
1796                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1797                         node->type = b;
1798                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1799                         node->type = a;
1800                 } else {
1801                         node->type.tv = tarval_bottom;
1802                 }
1803                 node->by_all_const = 1;
1804         } else if (r->part == l->part) {
1805                 ir_mode *mode = get_irn_mode(eor);
1806                 tv = get_mode_null(mode);
1807
1808                 /* if the node was ONCE evaluated by all constants, but now
1809                    this breakes AND we cat by partition a different result, switch to bottom.
1810                    This happens because initially all nodes are in the same partition ... */
1811                 if (node->by_all_const && node->type.tv != tv)
1812                         tv = tarval_bottom;
1813                 node->type.tv = tv;
1814         } else {
1815                 node->type.tv = tarval_bottom;
1816         }
1817 }  /* compute_Eor */
1818
1819 /**
1820  * (Re-)compute the type for Cmp.
1821  *
1822  * @param node  the node
1823  */
1824 static void compute_Cmp(node_t *node) {
1825         ir_node        *cmp  = node->node;
1826         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1827         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1828         lattice_elem_t a     = l->type;
1829         lattice_elem_t b     = r->type;
1830
1831         if (a.tv == tarval_top || b.tv == tarval_top) {
1832 #ifdef WITH_UNKNOWN
1833                 /*
1834                  * Top is congruent to any other value, we can
1835                  * calculate the compare result.
1836                  */
1837                 node->type.tv = tarval_b_true;
1838 #else
1839                 node->type.tv = tarval_top;
1840 #endif
1841         } else if (is_con(a) && is_con(b)) {
1842                 /* both nodes are constants, we can probably do something */
1843                 node->type.tv = tarval_b_true;
1844         } else if (r->part == l->part) {
1845                 /* both nodes congruent, we can probably do something */
1846                 node->type.tv = tarval_b_true;
1847         } else {
1848                 node->type.tv = tarval_bottom;
1849         }
1850 }  /* compute_Proj_Cmp */
1851
1852 /**
1853  * (Re-)compute the type for a Proj(Cmp).
1854  *
1855  * @param node  the node
1856  * @param cond  the predecessor Cmp node
1857  */
1858 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1859         ir_node        *proj = node->node;
1860         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1861         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1862         lattice_elem_t a     = l->type;
1863         lattice_elem_t b     = r->type;
1864         pn_Cmp         pnc   = get_Proj_proj(proj);
1865         tarval         *tv;
1866
1867         if (a.tv == tarval_top || b.tv == tarval_top) {
1868 #ifdef WITH_UNKNOWN
1869                 /* see above */
1870                 tv = new_tarval_from_long((pnc & pn_Cmp_Eq) ^ pn_Cmp_Eq, mode_b);
1871                 goto not_equal;
1872 #else
1873                 node->type.tv = tarval_top;
1874 #endif
1875         } else if (is_con(a) && is_con(b)) {
1876                 default_compute(node);
1877                 node->by_all_const = 1;
1878         } else if (r->part == l->part &&
1879                    (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1880                 /*
1881                  * BEWARE: a == a is NOT always True for floating Point values, as
1882                  * NaN != NaN is defined, so we must check this here.
1883                  */
1884                 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1885 #ifdef WITH_UNKNOWN
1886 not_equal:
1887 #endif
1888
1889                 /* if the node was ONCE evaluated by all constants, but now
1890                    this breakes AND we cat by partition a different result, switch to bottom.
1891                    This happens because initially all nodes are in the same partition ... */
1892                 if (node->by_all_const && node->type.tv != tv)
1893                         tv = tarval_bottom;
1894                 node->type.tv = tv;
1895         } else {
1896                 node->type.tv = tarval_bottom;
1897         }
1898 }  /* compute_Proj_Cmp */
1899
1900 /**
1901  * (Re-)compute the type for a Proj(Cond).
1902  *
1903  * @param node  the node
1904  * @param cond  the predecessor Cond node
1905  */
1906 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1907         ir_node *proj     = node->node;
1908         long    pnc       = get_Proj_proj(proj);
1909         ir_node *sel      = get_Cond_selector(cond);
1910         node_t  *selector = get_irn_node(sel);
1911
1912         if (get_irn_mode(sel) == mode_b) {
1913                 /* an IF */
1914                 if (pnc == pn_Cond_true) {
1915                         if (selector->type.tv == tarval_b_false) {
1916                                 node->type.tv = tarval_unreachable;
1917                         } else if (selector->type.tv == tarval_b_true) {
1918                                 node->type.tv = tarval_reachable;
1919                         } else if (selector->type.tv == tarval_bottom) {
1920                                 node->type.tv = tarval_reachable;
1921                         } else {
1922                                 assert(selector->type.tv == tarval_top);
1923                                 node->type.tv = tarval_unreachable;
1924                         }
1925                 } else {
1926                         assert(pnc == pn_Cond_false);
1927
1928                         if (selector->type.tv == tarval_b_false) {
1929                                 node->type.tv = tarval_reachable;
1930                         } else if (selector->type.tv == tarval_b_true) {
1931                                 node->type.tv = tarval_unreachable;
1932                         } else if (selector->type.tv == tarval_bottom) {
1933                                 node->type.tv = tarval_reachable;
1934                         } else {
1935                                 assert(selector->type.tv == tarval_top);
1936                                 node->type.tv = tarval_unreachable;
1937                         }
1938                 }
1939         } else {
1940                 /* an SWITCH */
1941                 if (selector->type.tv == tarval_bottom) {
1942                         node->type.tv = tarval_reachable;
1943                 } else if (selector->type.tv == tarval_top) {
1944                         node->type.tv = tarval_unreachable;
1945                 } else {
1946                         long value = get_tarval_long(selector->type.tv);
1947                         if (pnc == get_Cond_defaultProj(cond)) {
1948                                 /* default switch, have to check ALL other cases */
1949                                 int i;
1950
1951                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1952                                         ir_node *succ = get_irn_out(cond, i);
1953
1954                                         if (succ == proj)
1955                                                 continue;
1956                                         if (value == get_Proj_proj(succ)) {
1957                                                 /* we found a match, will NOT take the default case */
1958                                                 node->type.tv = tarval_unreachable;
1959                                                 return;
1960                                         }
1961                                 }
1962                                 /* all cases checked, no match, will take default case */
1963                                 node->type.tv = tarval_reachable;
1964                         } else {
1965                                 /* normal case */
1966                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1967                         }
1968                 }
1969         }
1970 }  /* compute_Proj_Cond */
1971
1972 /**
1973  * (Re-)compute the type for a Proj-Node.
1974  *
1975  * @param node  the node
1976  */
1977 static void compute_Proj(node_t *node) {
1978         ir_node *proj = node->node;
1979         ir_mode *mode = get_irn_mode(proj);
1980         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1981         ir_node *pred  = get_Proj_pred(proj);
1982
1983         if (block->type.tv == tarval_unreachable) {
1984                 /* a Proj in a unreachable Block stay Top */
1985                 node->type.tv = tarval_top;
1986                 return;
1987         }
1988         if (get_irn_node(pred)->type.tv == tarval_top) {
1989                 /* if the predecessor is Top, its Proj follow */
1990                 node->type.tv = tarval_top;
1991                 return;
1992         }
1993
1994         if (mode == mode_M) {
1995                 /* mode M is always bottom */
1996                 node->type.tv = tarval_bottom;
1997                 return;
1998         }
1999         if (mode != mode_X) {
2000                 if (is_Cmp(pred))
2001                         compute_Proj_Cmp(node, pred);
2002                 else
2003                         default_compute(node);
2004                 return;
2005         }
2006         /* handle mode_X nodes */
2007
2008         switch (get_irn_opcode(pred)) {
2009         case iro_Start:
2010                 /* the Proj_X from the Start is always reachable.
2011                    However this is already handled at the top. */
2012                 node->type.tv = tarval_reachable;
2013                 break;
2014         case iro_Cond:
2015                 compute_Proj_Cond(node, pred);
2016                 break;
2017         default:
2018                 default_compute(node);
2019         }
2020 }  /* compute_Proj */
2021
2022 /**
2023  * (Re-)compute the type for a Confirm.
2024  *
2025  * @param node  the node
2026  */
2027 static void compute_Confirm(node_t *node) {
2028         ir_node *confirm = node->node;
2029         node_t  *pred = get_irn_node(get_Confirm_value(confirm));
2030
2031         if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2032                 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2033
2034                 if (is_con(bound->type)) {
2035                         /* is equal to a constant */
2036                         node->type = bound->type;
2037                         return;
2038                 }
2039         }
2040         /* a Confirm is a copy OR a Const */
2041         node->type = pred->type;
2042 }  /* compute_Confirm */
2043
2044 /**
2045  * (Re-)compute the type for a Max.
2046  *
2047  * @param node  the node
2048  */
2049 static void compute_Max(node_t *node) {
2050         ir_node        *op   = node->node;
2051         node_t         *l    = get_irn_node(get_binop_left(op));
2052         node_t         *r    = get_irn_node(get_binop_right(op));
2053         lattice_elem_t a     = l->type;
2054         lattice_elem_t b     = r->type;
2055
2056         if (a.tv == tarval_top || b.tv == tarval_top) {
2057                 node->type.tv = tarval_top;
2058         } else if (is_con(a) && is_con(b)) {
2059                 /* both nodes are constants, we can probably do something */
2060                 if (a.tv == b.tv) {
2061                         /* this case handles symconsts as well */
2062                         node->type = a;
2063                 } else {
2064                         ir_mode *mode   = get_irn_mode(op);
2065                         tarval  *tv_min = get_mode_min(mode);
2066
2067                         if (a.tv == tv_min)
2068                                 node->type = b;
2069                         else if (b.tv == tv_min)
2070                                 node->type = a;
2071                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2072                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2073                                         node->type.tv = a.tv;
2074                                 else
2075                                         node->type.tv = b.tv;
2076                         } else {
2077                                 node->type.tv = tarval_bad;
2078                         }
2079                 }
2080         } else if (r->part == l->part) {
2081                 /* both nodes congruent, we can probably do something */
2082                 node->type = a;
2083         } else {
2084                 node->type.tv = tarval_bottom;
2085         }
2086 }  /* compute_Max */
2087
2088 /**
2089  * (Re-)compute the type for a Min.
2090  *
2091  * @param node  the node
2092  */
2093 static void compute_Min(node_t *node) {
2094         ir_node        *op   = node->node;
2095         node_t         *l    = get_irn_node(get_binop_left(op));
2096         node_t         *r    = get_irn_node(get_binop_right(op));
2097         lattice_elem_t a     = l->type;
2098         lattice_elem_t b     = r->type;
2099
2100         if (a.tv == tarval_top || b.tv == tarval_top) {
2101                 node->type.tv = tarval_top;
2102         } else if (is_con(a) && is_con(b)) {
2103                 /* both nodes are constants, we can probably do something */
2104                 if (a.tv == b.tv) {
2105                         /* this case handles symconsts as well */
2106                         node->type = a;
2107                 } else {
2108                         ir_mode *mode   = get_irn_mode(op);
2109                         tarval  *tv_max = get_mode_max(mode);
2110
2111                         if (a.tv == tv_max)
2112                                 node->type = b;
2113                         else if (b.tv == tv_max)
2114                                 node->type = a;
2115                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2116                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2117                                         node->type.tv = a.tv;
2118                                 else
2119                                         node->type.tv = b.tv;
2120                         } else {
2121                                 node->type.tv = tarval_bad;
2122                         }
2123                 }
2124         } else if (r->part == l->part) {
2125                 /* both nodes congruent, we can probably do something */
2126                 node->type = a;
2127         } else {
2128                 node->type.tv = tarval_bottom;
2129         }
2130 }  /* compute_Min */
2131
2132 /**
2133  * (Re-)compute the type for a given node.
2134  *
2135  * @param node  the node
2136  */
2137 static void compute(node_t *node) {
2138         compute_func func;
2139
2140         if (is_no_Block(node->node)) {
2141                 node_t *block = get_irn_node(get_nodes_block(node->node));
2142
2143                 if (block->type.tv == tarval_unreachable) {
2144                         node->type.tv = tarval_top;
2145                         return;
2146                 }
2147         }
2148
2149         func = (compute_func)node->node->op->ops.generic;
2150         if (func != NULL)
2151                 func(node);
2152 }  /* compute */
2153
2154 /*
2155  * Identity functions: Note that one might thing that identity() is just a
2156  * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2157  * here, because it expects that the identity node is one of the inputs, which is NOT
2158  * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2159  * So, we have our own implementation, which copies some parts of equivalent_node()
2160  */
2161
2162 /**
2163  * Calculates the Identity for Phi nodes
2164  */
2165 static node_t *identity_Phi(node_t *node) {
2166         ir_node *phi    = node->node;
2167         ir_node *block  = get_nodes_block(phi);
2168         node_t  *n_part = NULL;
2169         int     i;
2170
2171         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2172                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2173
2174                 if (pred_X->type.tv == tarval_reachable) {
2175                         node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2176
2177                         if (n_part == NULL)
2178                                 n_part = pred;
2179                         else if (n_part->part != pred->part) {
2180                                 /* incongruent inputs, not a follower */
2181                                 return node;
2182                         }
2183                 }
2184         }
2185         /* if n_part is NULL here, all inputs path are dead, the Phi computes
2186          * tarval_top, is in the TOP partition and should NOT being split! */
2187         assert(n_part != NULL);
2188         return n_part;
2189 }  /* identity_Phi */
2190
2191 /**
2192  * Calculates the Identity for commutative 0 neutral nodes.
2193  */
2194 static node_t *identity_comm_zero_binop(node_t *node) {
2195         ir_node *op   = node->node;
2196         node_t  *a    = get_irn_node(get_binop_left(op));
2197         node_t  *b    = get_irn_node(get_binop_right(op));
2198         ir_mode *mode = get_irn_mode(op);
2199         tarval  *zero;
2200
2201         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2202         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2203                 return node;
2204
2205         /* node: no input should be tarval_top, else the binop would be also
2206          * Top and not being split. */
2207         zero = get_mode_null(mode);
2208         if (a->type.tv == zero)
2209                 return b;
2210         if (b->type.tv == zero)
2211                 return a;
2212         return node;
2213 }  /* identity_comm_zero_binop */
2214
2215 /**
2216  * Calculates the Identity for Shift nodes.
2217  */
2218 static node_t *identity_shift(node_t *node) {
2219         ir_node *op   = node->node;
2220         node_t  *b    = get_irn_node(get_binop_right(op));
2221         ir_mode *mode = get_irn_mode(b->node);
2222         tarval  *zero;
2223
2224         /* node: no input should be tarval_top, else the binop would be also
2225          * Top and not being split. */
2226         zero = get_mode_null(mode);
2227         if (b->type.tv == zero)
2228                 return get_irn_node(get_binop_left(op));
2229         return node;
2230 }  /* identity_shift */
2231
2232 /**
2233  * Calculates the Identity for Mul nodes.
2234  */
2235 static node_t *identity_Mul(node_t *node) {
2236         ir_node *op   = node->node;
2237         node_t  *a    = get_irn_node(get_Mul_left(op));
2238         node_t  *b    = get_irn_node(get_Mul_right(op));
2239         ir_mode *mode = get_irn_mode(op);
2240         tarval  *one;
2241
2242         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2243         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2244                 return node;
2245
2246         /* node: no input should be tarval_top, else the binop would be also
2247          * Top and not being split. */
2248         one = get_mode_one(mode);
2249         if (a->type.tv == one)
2250                 return b;
2251         if (b->type.tv == one)
2252                 return a;
2253         return node;
2254 }  /* identity_Mul */
2255
2256 /**
2257  * Calculates the Identity for Sub nodes.
2258  */
2259 static node_t *identity_Sub(node_t *node) {
2260         ir_node *sub  = node->node;
2261         node_t  *b    = get_irn_node(get_Sub_right(sub));
2262         ir_mode *mode = get_irn_mode(sub);
2263
2264         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2265         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2266                 return node;
2267
2268         /* node: no input should be tarval_top, else the binop would be also
2269          * Top and not being split. */
2270         if (b->type.tv == get_mode_null(mode))
2271                 return get_irn_node(get_Sub_left(sub));
2272         return node;
2273 }  /* identity_Mul */
2274
2275 /**
2276  * Calculates the Identity for And nodes.
2277  */
2278 static node_t *identity_And(node_t *node) {
2279         ir_node *and = node->node;
2280         node_t  *a   = get_irn_node(get_And_left(and));
2281         node_t  *b   = get_irn_node(get_And_right(and));
2282         tarval  *neutral = get_mode_all_one(get_irn_mode(and));
2283
2284         /* node: no input should be tarval_top, else the And would be also
2285          * Top and not being split. */
2286         if (a->type.tv == neutral)
2287                 return b;
2288         if (b->type.tv == neutral)
2289                 return a;
2290         return node;
2291 }  /* identity_And */
2292
2293 /**
2294  * Calculates the Identity for Confirm nodes.
2295  */
2296 static node_t *identity_Confirm(node_t *node) {
2297         ir_node *confirm = node->node;
2298
2299         /* a Confirm is always a Copy */
2300         return get_irn_node(get_Confirm_value(confirm));
2301 }  /* identity_Confirm */
2302
2303 /**
2304  * Calculates the Identity for Mux nodes.
2305  */
2306 static node_t *identity_Mux(node_t *node) {
2307         ir_node *mux = node->node;
2308         node_t  *t   = get_irn_node(get_Mux_true(mux));
2309         node_t  *f   = get_irn_node(get_Mux_false(mux));
2310         /*node_t  *sel; */
2311
2312         if (t->part == f->part)
2313                 return t;
2314
2315         /* for now, the 1-input identity is not supported */
2316 #if 0
2317         sel = get_irn_node(get_Mux_sel(mux));
2318
2319         /* Mux sel input is mode_b, so it is always a tarval */
2320         if (sel->type.tv == tarval_b_true)
2321                 return t;
2322         if (sel->type.tv == tarval_b_false)
2323                 return f;
2324 #endif
2325         return node;
2326 }  /* identity_Mux */
2327
2328 /**
2329  * Calculates the Identity for Min nodes.
2330  */
2331 static node_t *identity_Min(node_t *node) {
2332         ir_node *op   = node->node;
2333         node_t  *a    = get_irn_node(get_binop_left(op));
2334         node_t  *b    = get_irn_node(get_binop_right(op));
2335         ir_mode *mode = get_irn_mode(op);
2336         tarval  *tv_max;
2337
2338         if (a->part == b->part) {
2339                 /* leader of multiple predecessors */
2340                 return a;
2341         }
2342
2343         /* works even with NaN */
2344         tv_max = get_mode_max(mode);
2345         if (a->type.tv == tv_max)
2346                 return b;
2347         if (b->type.tv == tv_max)
2348                 return a;
2349         return node;
2350 }  /* identity_Min */
2351
2352 /**
2353  * Calculates the Identity for Max nodes.
2354  */
2355 static node_t *identity_Max(node_t *node) {
2356         ir_node *op   = node->node;
2357         node_t  *a    = get_irn_node(get_binop_left(op));
2358         node_t  *b    = get_irn_node(get_binop_right(op));
2359         ir_mode *mode = get_irn_mode(op);
2360         tarval  *tv_min;
2361
2362         if (a->part == b->part) {
2363                 /* leader of multiple predecessors */
2364                 return a;
2365         }
2366
2367         /* works even with NaN */
2368         tv_min = get_mode_min(mode);
2369         if (a->type.tv == tv_min)
2370                 return b;
2371         if (b->type.tv == tv_min)
2372                 return a;
2373         return node;
2374 }  /* identity_Max */
2375
2376 /**
2377  * Calculates the Identity for nodes.
2378  */
2379 static node_t *identity(node_t *node) {
2380         ir_node *irn = node->node;
2381
2382         switch (get_irn_opcode(irn)) {
2383         case iro_Phi:
2384                 return identity_Phi(node);
2385         case iro_Mul:
2386                 return identity_Mul(node);
2387         case iro_Add:
2388         case iro_Or:
2389         case iro_Eor:
2390                 return identity_comm_zero_binop(node);
2391         case iro_Shr:
2392         case iro_Shl:
2393         case iro_Shrs:
2394         case iro_Rotl:
2395                 return identity_shift(node);
2396         case iro_And:
2397                 return identity_And(node);
2398         case iro_Sub:
2399                 return identity_Sub(node);
2400         case iro_Confirm:
2401                 return identity_Confirm(node);
2402         case iro_Mux:
2403                 return identity_Mux(node);
2404         case iro_Min:
2405                 return identity_Min(node);
2406         case iro_Max:
2407                 return identity_Max(node);
2408         default:
2409                 return node;
2410         }
2411 }  /* identity */
2412
2413 /**
2414  * Node follower is a (new) follower of leader, segregate Leader
2415  * out edges.
2416  */
2417 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2418         ir_node *l   = leader->node;
2419         int     j, i, n = get_irn_n_outs(l);
2420
2421         DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2422         /* The leader edges must remain sorted, but follower edges can
2423            be unsorted. */
2424         for (i = leader->n_followers + 1; i <= n; ++i) {
2425                 if (l->out[i].use == follower) {
2426                         ir_def_use_edge t = l->out[i];
2427
2428                         for (j = i - 1; j >= leader->n_followers + 1; --j)
2429                                 l->out[j + 1] = l->out[j];
2430                         ++leader->n_followers;
2431                         l->out[leader->n_followers] = t;
2432                         break;
2433                 }
2434         }
2435 }  /* segregate_def_use_chain_1 */
2436
2437 /**
2438  * Node follower is a (new) follower of leader, segregate Leader
2439  * out edges. If follower is a n-congruent Input identity, all follower
2440  * inputs congruent to follower are also leader.
2441  *
2442  * @param follower  the follower IR node
2443  */
2444 static void segregate_def_use_chain(const ir_node *follower) {
2445         int i;
2446
2447         for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2448                 node_t *pred = get_irn_node(get_irn_n(follower, i));
2449
2450                 segregate_def_use_chain_1(follower, pred);
2451         }
2452 }  /* segregate_def_use_chain */
2453
2454 /**
2455  * Propagate constant evaluation.
2456  *
2457  * @param env  the environment
2458  */
2459 static void propagate(environment_t *env) {
2460         partition_t    *X, *Y;
2461         node_t         *x;
2462         lattice_elem_t old_type;
2463         node_t         *fallen;
2464         unsigned       n_fallen, old_type_was_T_or_C;
2465         int            i;
2466
2467         while (env->cprop != NULL) {
2468                 void *oldopcode = NULL;
2469
2470                 /* remove the first partition X from cprop */
2471                 X           = env->cprop;
2472                 X->on_cprop = 0;
2473                 env->cprop  = X->cprop_next;
2474
2475                 old_type_was_T_or_C = X->type_is_T_or_C;
2476
2477                 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2478                 fallen   = NULL;
2479                 n_fallen = 0;
2480                 while (! list_empty(&X->cprop)) {
2481                         /* remove the first Node x from X.cprop */
2482                         x = list_entry(X->cprop.next, node_t, cprop_list);
2483                         //assert(x->part == X);
2484                         list_del(&x->cprop_list);
2485                         x->on_cprop = 0;
2486
2487                         if (x->is_follower && identity(x) == x) {
2488                                 /* check the opcode first */
2489                                 if (oldopcode == NULL) {
2490                                         oldopcode = lambda_opcode(get_first_node(X), env);
2491                                 }
2492                                 if (oldopcode != lambda_opcode(x, env)) {
2493                                         if (x->on_fallen == 0) {
2494                                                 /* different opcode -> x falls out of this partition */
2495                                                 x->next      = fallen;
2496                                                 x->on_fallen = 1;
2497                                                 fallen       = x;
2498                                                 ++n_fallen;
2499                                                 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2500                                         }
2501                                 }
2502
2503                                 /* x will make the follower -> leader transition */
2504                                 follower_to_leader(x);
2505                         }
2506
2507                         /* compute a new type for x */
2508                         old_type = x->type;
2509                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2510                         compute(x);
2511                         if (x->type.tv != old_type.tv) {
2512                                 verify_type(old_type, x->type);
2513                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2514
2515                                 if (x->on_fallen == 0) {
2516                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2517                                            not already on the list. */
2518                                         x->next      = fallen;
2519                                         x->on_fallen = 1;
2520                                         fallen       = x;
2521                                         ++n_fallen;
2522                                         DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2523                                 }
2524                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2525                                         ir_node *succ = get_irn_out(x->node, i);
2526                                         node_t  *y    = get_irn_node(succ);
2527
2528                                         /* Add y to y.partition.cprop. */
2529                                         add_to_cprop(y, env);
2530                                 }
2531                         }
2532                 }
2533
2534                 if (n_fallen > 0 && n_fallen != X->n_leader) {
2535                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2536                         Y = split(&X, fallen, env);
2537                         /*
2538                          * We have split out fallen node. The type of the result
2539                          * partition is NOT set yet.
2540                          */
2541                         Y->type_is_T_or_C = 0;
2542                 } else {
2543                         Y = X;
2544                 }
2545                 /* remove the flags from the fallen list */
2546                 for (x = fallen; x != NULL; x = x->next)
2547                         x->on_fallen = 0;
2548
2549                 if (old_type_was_T_or_C) {
2550                         node_t *y, *tmp;
2551
2552                         if (Y->on_worklist == 0)
2553                                 add_to_worklist(Y, env);
2554
2555                         /* check if some nodes will make the leader -> follower transition */
2556                         list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2557                                 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2558                                         node_t *eq_node = identity(y);
2559
2560                                         if (eq_node != y && eq_node->part == y->part) {
2561                                                 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2562                                                 /* move to Follower */
2563                                                 y->is_follower = 1;
2564                                                 list_del(&y->node_list);
2565                                                 list_add_tail(&y->node_list, &Y->Follower);
2566                                                 --Y->n_leader;
2567
2568                                                 segregate_def_use_chain(y->node);
2569                                         }
2570                                 }
2571                         }
2572                 }
2573                 split_by(Y, env);
2574         }
2575 }  /* propagate */
2576
2577 /**
2578  * Get the leader for a given node from its congruence class.
2579  *
2580  * @param irn  the node
2581  */
2582 static ir_node *get_leader(node_t *node) {
2583         partition_t *part = node->part;
2584
2585         if (part->n_leader > 1 || node->is_follower) {
2586                 if (node->is_follower) {
2587                         DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2588                 }
2589                 else
2590                         DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2591
2592                 return get_first_node(part)->node;
2593         }
2594         return node->node;
2595 }  /* get_leader */
2596
2597 /**
2598  * Return non-zero if the control flow predecessor node pred
2599  * is the only reachable control flow exit of its block.
2600  *
2601  * @param pred  the control flow exit
2602  */
2603 static int can_exchange(ir_node *pred) {
2604         if (is_Start(pred))
2605                 return 0;
2606         else if (is_Jmp(pred))
2607                 return 1;
2608         else if (get_irn_mode(pred) == mode_T) {
2609                 int i, k;
2610
2611                 /* if the predecessor block has more than one
2612                 reachable outputs we cannot remove the block */
2613                 k = 0;
2614                 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2615                         ir_node *proj = get_irn_out(pred, i);
2616                         node_t  *node;
2617
2618                         /* skip non-control flow Proj's */
2619                         if (get_irn_mode(proj) != mode_X)
2620                                 continue;
2621
2622                         node = get_irn_node(proj);
2623                         if (node->type.tv == tarval_reachable) {
2624                                 if (++k > 1)
2625                                         return 0;
2626                         }
2627                 }
2628                 return 1;
2629         }
2630         return 0;
2631 }  /* can_exchange */
2632
2633 /**
2634  * Block Post-Walker, apply the analysis results on control flow by
2635  * shortening Phi's and Block inputs.
2636  */
2637 static void apply_cf(ir_node *block, void *ctx) {
2638         environment_t *env = ctx;
2639         node_t        *node = get_irn_node(block);
2640         int           i, j, k, n;
2641         ir_node       **ins, **in_X;
2642         ir_node       *phi, *next;
2643
2644         n = get_Block_n_cfgpreds(block);
2645
2646         if (node->type.tv == tarval_unreachable) {
2647                 env->modified = 1;
2648
2649                 for (i = n - 1; i >= 0; --i) {
2650                         ir_node *pred = get_Block_cfgpred(block, i);
2651
2652                         if (! is_Bad(pred)) {
2653                                 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2654
2655                                 if (pred_bl->flagged == 0) {
2656                                         pred_bl->flagged = 3;
2657
2658                                         if (pred_bl->type.tv == tarval_reachable) {
2659                                                 /*
2660                                                  * We will remove an edge from block to its pred.
2661                                                  * This might leave the pred block as an endless loop
2662                                                  */
2663                                                 if (! is_backedge(block, i))
2664                                                         keep_alive(pred_bl->node);
2665                                         }
2666                                 }
2667                         }
2668                 }
2669
2670                 /* the EndBlock is always reachable even if the analysis
2671                    finds out the opposite :-) */
2672                 if (block != get_irg_end_block(current_ir_graph)) {
2673                         /* mark dead blocks */
2674                         set_Block_dead(block);
2675                         DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
2676                 } else {
2677                         /* the endblock is unreachable */
2678                         set_irn_in(block, 0, NULL);
2679                 }
2680                 return;
2681         }
2682
2683         if (n == 1) {
2684                 /* only one predecessor combine */
2685                 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2686
2687                 if (can_exchange(pred)) {
2688                         ir_node *new_block = get_nodes_block(pred);
2689                         DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
2690                         DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
2691                         exchange(block, new_block);
2692                         node->node = new_block;
2693                         env->modified = 1;
2694                 }
2695                 return;
2696         }
2697
2698         NEW_ARR_A(ir_node *, in_X, n);
2699         k = 0;
2700         for (i = 0; i < n; ++i) {
2701                 ir_node *pred = get_Block_cfgpred(block, i);
2702                 node_t  *node = get_irn_node(pred);
2703
2704                 if (node->type.tv == tarval_reachable) {
2705                         in_X[k++] = pred;
2706                 } else {
2707                         DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
2708                         if (! is_Bad(pred)) {
2709                                 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2710
2711                                 if (pred_bl->flagged == 0) {
2712                                         pred_bl->flagged = 3;
2713
2714                                         if (pred_bl->type.tv == tarval_reachable) {
2715                                                 /*
2716                                                  * We will remove an edge from block to its pred.
2717                                                  * This might leave the pred block as an endless loop
2718                                                  */
2719                                                 if (! is_backedge(block, i))
2720                                                         keep_alive(pred_bl->node);
2721                                         }
2722                                 }
2723                         }
2724                 }
2725         }
2726         if (k >= n)
2727                 return;
2728
2729         NEW_ARR_A(ir_node *, ins, n);
2730         for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2731                 node_t *node = get_irn_node(phi);
2732
2733                 next = get_Phi_next(phi);
2734                 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2735                         /* this Phi is replaced by a constant */
2736                         tarval  *tv = node->type.tv;
2737                         ir_node *c  = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2738
2739                         set_irn_node(c, node);
2740                         node->node = c;
2741                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2742                         DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
2743                         exchange(phi, c);
2744                         env->modified = 1;
2745                 } else {
2746                         j = 0;
2747                         for (i = 0; i < n; ++i) {
2748                                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2749
2750                                 if (pred->type.tv == tarval_reachable) {
2751                                         ins[j++] = get_Phi_pred(phi, i);
2752                                 }
2753                         }
2754                         if (j == 1) {
2755                                 /* this Phi is replaced by a single predecessor */
2756                                 ir_node *s = ins[0];
2757                                 node_t *phi_node = get_irn_node(phi);
2758
2759                                 node->node = s;
2760                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2761                                 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
2762                                 exchange(phi, s);
2763                                 phi_node->node = s;
2764                                 env->modified = 1;
2765                         } else {
2766                                 set_irn_in(phi, j, ins);
2767                                 env->modified = 1;
2768                         }
2769                 }
2770         }
2771
2772         if (k == 1) {
2773                 /* this Block has only one live predecessor */
2774                 ir_node *pred = skip_Proj(in_X[0]);
2775
2776                 if (can_exchange(pred)) {
2777                         ir_node *new_block = get_nodes_block(pred);
2778                         DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
2779                         exchange(block, new_block);
2780                         node->node = new_block;
2781                         env->modified = 1;
2782                 }
2783         } else {
2784                 set_irn_in(block, k, in_X);
2785                 env->modified = 1;
2786         }
2787 }
2788
2789 /**
2790  * Post-Walker, apply the analysis results;
2791  */
2792 static void apply_result(ir_node *irn, void *ctx) {
2793         environment_t *env = ctx;
2794         node_t        *node = get_irn_node(irn);
2795
2796         if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2797                 /* blocks already handled, do not touch the End node */
2798         } else {
2799                 node_t *block = get_irn_node(get_nodes_block(irn));
2800
2801                 if (block->type.tv == tarval_unreachable) {
2802                         ir_node *bad = get_irg_bad(current_ir_graph);
2803
2804                         /* here, bad might already have a node, but this can be safely ignored
2805                            as long as bad has at least ONE valid node */
2806                         set_irn_node(bad, node);
2807                         node->node = bad;
2808                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2809                         exchange(irn, bad);
2810                         env->modified = 1;
2811                 }
2812                 else if (node->type.tv == tarval_unreachable) {
2813                         /* don't kick away Unknown */
2814                         if (! is_Unknown(irn)) {
2815                                 ir_node *bad = get_irg_bad(current_ir_graph);
2816
2817                                 /* see comment above */
2818                                 set_irn_node(bad, node);
2819                                 node->node = bad;
2820                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2821                                 exchange(irn, bad);
2822                                 env->modified = 1;
2823                         }
2824                 }
2825                 else if (get_irn_mode(irn) == mode_X) {
2826                         if (is_Proj(irn)) {
2827                                 /* leave or Jmp */
2828                                 ir_node *cond = get_Proj_pred(irn);
2829
2830                                 if (is_Cond(cond)) {
2831                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
2832
2833                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2834                                                 /* Cond selector is a constant and the Proj is reachable, make a Jmp */
2835                                                 ir_node *jmp  = new_r_Jmp(current_ir_graph, block->node);
2836                                                 set_irn_node(jmp, node);
2837                                                 node->node = jmp;
2838                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2839                                                 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
2840                                                 exchange(irn, jmp);
2841                                                 env->modified = 1;
2842                                         }
2843                                 }
2844                         }
2845                 } else {
2846                         /* normal data node */
2847                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2848                                 tarval *tv = node->type.tv;
2849
2850                                 /*
2851                                  * Beware: never replace mode_T nodes by constants. Currently we must mark
2852                                  * mode_T nodes with constants, but do NOT replace them.
2853                                  */
2854                                 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2855                                         /* can be replaced by a constant */
2856                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2857                                         set_irn_node(c, node);
2858                                         node->node = c;
2859                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2860                                         DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
2861                                         exchange(irn, c);
2862                                         env->modified = 1;
2863                                 }
2864                         } else if (is_entity(node->type.sym.entity_p)) {
2865                                 if (! is_SymConst(irn)) {
2866                                         /* can be replaced by a Symconst */
2867                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2868                                         set_irn_node(symc, node);
2869                                         node->node = symc;
2870
2871                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2872                                         DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
2873                                         exchange(irn, symc);
2874                                         env->modified = 1;
2875                                 }
2876                         } else if (is_Confirm(irn)) {
2877                                 /* Confirms are always follower, but do not kill them here */
2878                         } else {
2879                                 ir_node *leader = get_leader(node);
2880
2881                                 if (leader != irn) {
2882                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2883                                         if (node->is_follower)
2884                                                 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
2885                                         else
2886                                                 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
2887                                         exchange(irn, leader);
2888                                         env->modified = 1;
2889                                 }
2890                         }
2891                 }
2892         }
2893 }  /* apply_result */
2894
2895 /**
2896  * Fix the keep-alives by deleting unreachable ones.
2897  */
2898 static void apply_end(ir_node *end, environment_t *env) {
2899         int i, j,  n = get_End_n_keepalives(end);
2900         ir_node **in;
2901
2902         if (n > 0)
2903                 NEW_ARR_A(ir_node *, in, n);
2904
2905         /* fix the keep alive */
2906         for (i = j = 0; i < n; i++) {
2907                 ir_node *ka   = get_End_keepalive(end, i);
2908                 node_t  *node = get_irn_node(ka);
2909
2910                 if (! is_Block(ka))
2911                         node = get_irn_node(get_nodes_block(ka));
2912
2913                 if (node->type.tv != tarval_unreachable)
2914                         in[j++] = ka;
2915         }
2916         if (j != n) {
2917                 set_End_keepalives(end, j, in);
2918                 env->modified = 1;
2919         }
2920 }  /* apply_end */
2921
2922 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2923
2924 /**
2925  * sets the generic functions to compute.
2926  */
2927 static void set_compute_functions(void) {
2928         int i;
2929
2930         /* set the default compute function */
2931         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2932                 ir_op *op = get_irp_opcode(i);
2933                 op->ops.generic = (op_func)default_compute;
2934         }
2935
2936         /* set specific functions */
2937         SET(Block);
2938         SET(Unknown);
2939         SET(Bad);
2940         SET(Jmp);
2941         SET(Phi);
2942         SET(Add);
2943         SET(Sub);
2944         SET(Eor);
2945         SET(SymConst);
2946         SET(Cmp);
2947         SET(Proj);
2948         SET(Confirm);
2949         SET(End);
2950
2951         if (op_Max != NULL)
2952                 SET(Max);
2953         if (op_Min != NULL)
2954                 SET(Min);
2955
2956 }  /* set_compute_functions */
2957
2958 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2959         ir_node *irn = local != NULL ? local : n;
2960         node_t *node = get_irn_node(irn);
2961
2962         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2963         return 1;
2964 }
2965
2966 void combo(ir_graph *irg) {
2967         environment_t env;
2968         ir_node       *initial_bl;
2969         node_t        *start;
2970         ir_graph      *rem = current_ir_graph;
2971
2972         current_ir_graph = irg;
2973
2974         /* register a debug mask */
2975         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2976         //firm_dbg_set_mask(dbg, SET_LEVEL_3);
2977
2978         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2979
2980         obstack_init(&env.obst);
2981         env.worklist       = NULL;
2982         env.cprop          = NULL;
2983         env.touched        = NULL;
2984         env.initial        = NULL;
2985 #ifdef DEBUG_libfirm
2986         env.dbg_list       = NULL;
2987 #endif
2988         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
2989         env.type2id_map    = pmap_create();
2990         env.end_idx        = get_opt_global_cse() ? 0 : -1;
2991         env.lambda_input   = 0;
2992         env.nonstd_cond    = 0;
2993         env.modified       = 0;
2994
2995         assure_irg_outs(irg);
2996         assure_cf_loop(irg);
2997
2998
2999         /* we have our own value_of function */
3000         set_value_of_func(get_node_tarval);
3001
3002         set_compute_functions();
3003         DEBUG_ONLY(part_nr = 0);
3004
3005         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
3006
3007         /* create the initial partition and place it on the work list */
3008         env.initial = new_partition(&env);
3009         add_to_worklist(env.initial, &env);
3010         irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
3011
3012 #ifdef WITH_UNKNOWN
3013         tarval_UNKNOWN = env.nonstd_cond ? tarval_bad : tarval_top;
3014 #else
3015         tarval_UNKNOWN = tarval_bad;
3016 #endif
3017
3018         /* all nodes on the initial partition have type Top */
3019         env.initial->type_is_T_or_C = 1;
3020
3021         /* Place the START Node's partition on cprop.
3022            Place the START Node on its local worklist. */
3023         initial_bl = get_irg_start_block(irg);
3024         start      = get_irn_node(initial_bl);
3025         add_to_cprop(start, &env);
3026
3027         do {
3028                 propagate(&env);
3029                 if (env.worklist != NULL)
3030                         cause_splits(&env);
3031         } while (env.cprop != NULL || env.worklist != NULL);
3032
3033         dump_all_partitions(&env);
3034         check_all_partitions(&env);
3035
3036 #if 0
3037         set_dump_node_vcgattr_hook(dump_partition_hook);
3038         dump_ir_block_graph(irg, "-partition");
3039         set_dump_node_vcgattr_hook(NULL);
3040 #else
3041         (void)dump_partition_hook;
3042 #endif
3043
3044         /* apply the result */
3045         irg_block_walk_graph(irg, NULL, apply_cf, &env);
3046         irg_walk_graph(irg, NULL, apply_result, &env);
3047         apply_end(get_irg_end(irg), &env);
3048
3049         if (env.modified) {
3050                 /* control flow might changed */
3051                 set_irg_outs_inconsistent(irg);
3052                 set_irg_extblk_inconsistent(irg);
3053                 set_irg_doms_inconsistent(irg);
3054                 set_irg_loopinfo_inconsistent(irg);
3055         }
3056
3057         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
3058
3059         pmap_destroy(env.type2id_map);
3060         del_set(env.opcode2id_map);
3061         obstack_free(&env.obst, NULL);
3062
3063         /* restore value_of() default behavior */
3064         set_value_of_func(NULL);
3065         current_ir_graph = rem;
3066 }  /* combo */