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