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