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