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