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