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