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