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