196364c23c6da7f1d0fcd4603fb92fba42c9e943
[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  * Note further that we use the terminology from Click's work here, which is different
27  * in some cases from Firm terminology.  Especially, Click's type is a
28  * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
29  */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <assert.h>
35
36 #include "iroptimize.h"
37 #include "archop.h"
38 #include "irflag.h"
39 #include "ircons.h"
40 #include "list.h"
41 #include "set.h"
42 #include "pmap.h"
43 #include "obstack.h"
44 #include "irgraph_t.h"
45 #include "irnode_t.h"
46 #include "iropt_t.h"
47 #include "irgwalk.h"
48 #include "irop.h"
49 #include "irouts.h"
50 #include "irgmod.h"
51 #include "debug.h"
52 #include "error.h"
53
54 #include "tv_t.h"
55
56 #include "irprintf.h"
57 #include "irdump.h"
58
59 /* define this to check that all type translations are monotone */
60 #define VERIFY_MONOTONE
61
62 #undef NO_FOLLOWER
63
64 typedef struct node_t            node_t;
65 typedef struct partition_t       partition_t;
66 typedef struct opcode_key_t      opcode_key_t;
67 typedef struct listmap_entry_t   listmap_entry_t;
68
69 /** The type of the compute function. */
70 typedef void (*compute_func)(node_t *node);
71
72 /**
73  * An opcode map key.
74  */
75 struct opcode_key_t {
76         ir_opcode   code;   /**< The Firm opcode. */
77         ir_mode     *mode;  /**< The mode of all nodes in the partition. */
78         union {
79                 long      proj;   /**< For Proj nodes, its proj number */
80                 ir_entity *ent;   /**< For Sel Nodes, its entity */
81         } u;
82 };
83
84 /**
85  * An entry in the list_map.
86  */
87 struct listmap_entry_t {
88         void            *id;    /**< The id. */
89         node_t          *list;  /**< The associated list for this id. */
90         listmap_entry_t *next;  /**< Link to the next entry in the map. */
91 };
92
93 /** We must map id's to lists. */
94 typedef struct listmap_t {
95         set             *map;    /**< Map id's to listmap_entry_t's */
96         listmap_entry_t *values; /**< List of all values in the map. */
97 } listmap_t;
98
99 /**
100  * A lattice element. Because we handle constants and symbolic constants different, we
101  * have to use this union.
102  */
103 typedef union {
104         tarval          *tv;
105         symconst_symbol sym;
106 } lattice_elem_t;
107
108 /**
109  * A node.
110  */
111 struct node_t {
112         ir_node         *node;          /**< The IR-node itself. */
113         list_head       node_list;      /**< Double-linked list of leader/follower entries. */
114         list_head       cprop_list;     /**< Double-linked partition.cprop list. */
115         partition_t     *part;          /**< points to the partition this node belongs to */
116         node_t          *next;          /**< Next node on local list (partition.touched, fallen). */
117         node_t          *race_next;     /**< Next node on race list. */
118         lattice_elem_t  type;           /**< The associated lattice element "type". */
119         int             max_user_input; /**< Maximum input number of Def-Use edges. */
120         int             next_edge;      /**< Index of the next Def-Use edge to use. */
121         int             n_followers;    /**< Number of Follower in the outs set. */
122         unsigned        on_touched:1;   /**< Set, if this node is on the partition.touched set. */
123         unsigned        on_cprop:1;     /**< Set, if this node is on the partition.cprop list. */
124         unsigned        on_fallen:1;    /**< Set, if this node is on the fallen list. */
125         unsigned        is_follower:1;  /**< Set, if this node is a follower. */
126         unsigned        is_flagged:1;   /**< Set, if this node is flagged by step(). */
127         unsigned        by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
128 };
129
130 /**
131  * A partition containing congruent nodes.
132  */
133 struct partition_t {
134         list_head         Leader;          /**< The head of partition Leader node list. */
135         list_head         Follower;        /**< The head of partition Follower node list. */
136         list_head         cprop;           /**< The head of partition.cprop list. */
137         partition_t       *wl_next;        /**< Next entry in the work list if any. */
138         partition_t       *touched_next;   /**< Points to the next partition in the touched set. */
139         partition_t       *cprop_next;     /**< Points to the next partition in the cprop list. */
140         partition_t       *split_next;     /**< Points to the next partition in the list that must be split by split_by(). */
141         node_t            *touched;        /**< The partition.touched set of this partition. */
142         unsigned          n_leader;        /**< Number of entries in this partition.Leader. */
143         unsigned          n_touched;       /**< Number of entries in the partition.touched. */
144         int               max_user_inputs; /**< Maximum number of user inputs of all entries. */
145         unsigned          on_worklist:1;   /**< Set, if this partition is in the work list. */
146         unsigned          on_touched:1;    /**< Set, if this partition is on the touched set. */
147         unsigned          on_cprop:1;      /**< Set, if this partition is on the cprop list. */
148         unsigned          type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
149 #ifdef DEBUG_libfirm
150         partition_t       *dbg_next;       /**< Link all partitions for debugging */
151         unsigned          nr;              /**< A unique number for (what-)mapping, >0. */
152 #endif
153 };
154
155 typedef struct environment_t {
156         struct obstack  obst;           /**< obstack to allocate data structures. */
157         partition_t     *worklist;      /**< The work list. */
158         partition_t     *cprop;         /**< The constant propagation list. */
159         partition_t     *touched;       /**< the touched set. */
160         partition_t     *initial;       /**< The initial partition. */
161         set             *opcode2id_map; /**< The opcodeMode->id map. */
162         pmap            *type2id_map;   /**< The type->id map. */
163         int             end_idx;        /**< -1 for local and 0 for global congruences. */
164         int             lambda_input;   /**< Captured argument for lambda_partition(). */
165         int             modified;       /**< Set, if the graph was modified. */
166 #ifdef DEBUG_libfirm
167         partition_t     *dbg_list;      /**< List of all partitions. */
168 #endif
169 } environment_t;
170
171 /** Type of the what function. */
172 typedef void *(*what_func)(const node_t *node, environment_t *env);
173
174 #define get_irn_node(follower)         ((node_t *)get_irn_link(follower))
175 #define set_irn_node(follower, node)   set_irn_link(follower, node)
176
177 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
178 #undef tarval_unreachable
179 #define tarval_unreachable tarval_top
180
181
182 /** The debug module handle. */
183 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
184
185 /** Next partition number. */
186 DEBUG_ONLY(static unsigned part_nr = 0);
187
188 #ifdef DEBUG_libfirm
189 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
190
191 /**
192  * Dump partition to output.
193  */
194 static void dump_partition(const char *msg, const partition_t *part) {
195         const node_t   *node;
196         int            first = 1;
197         lattice_elem_t type = get_partition_type(part);
198
199         DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n  ",
200                 msg, part->nr, part->type_is_T_or_C ? "*" : "",
201                 part->n_leader, type));
202         list_for_each_entry(node_t, node, &part->Leader, node_list) {
203                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
204                 first = 0;
205         }
206         if (! list_empty(&part->Follower)) {
207                 DB((dbg, LEVEL_2, "\n---\n  "));
208                 first = 1;
209                 list_for_each_entry(node_t, node, &part->Follower, node_list) {
210                         DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
211                         first = 0;
212                 }
213         }
214         DB((dbg, LEVEL_2, "\n}\n"));
215 }  /* dump_partition */
216
217 /**
218  * Dumps a list.
219  */
220 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
221         const node_t *p;
222         int          first = 1;
223
224 #define GET_LINK(p, ofs)  *((const node_t **)((char *)(p) + (ofs)))
225
226         DB((dbg, LEVEL_3, "%s = {\n  ", msg));
227         for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
228                 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
229                 first = 0;
230         }
231         DB((dbg, LEVEL_3, "\n}\n"));
232
233 #undef GET_LINK
234 }
235
236 /**
237  * Dumps a race list.
238  */
239 static void dump_race_list(const char *msg, const node_t *list) {
240         do_dump_list(msg, list, offsetof(node_t, race_next));
241 }
242
243 /**
244  * Dumps a local list.
245  */
246 static void dump_list(const char *msg, const node_t *list) {
247         do_dump_list(msg, list, offsetof(node_t, next));
248 }
249
250 /**
251  * Dump all partitions.
252  */
253 static void dump_all_partitions(const environment_t *env) {
254         const partition_t *P;
255
256         DB((dbg, LEVEL_2, "All partitions\n===============\n"));
257         for (P = env->dbg_list; P != NULL; P = P->dbg_next)
258                 dump_partition("", P);
259 }
260
261 #else
262 #define dump_partition(msg, part)
263 #define dump_race_list(msg, list)
264 #define dump_list(msg, list)
265 #define dump_all_partitions(env)
266 #endif
267
268 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
269 /**
270  * Verify that a type transition is monotone
271  */
272 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
273         if (old_type.tv == new_type.tv) {
274                 /* no change */
275                 return;
276         }
277         if (old_type.tv == tarval_top) {
278                 /* from Top down-to is always allowed */
279                 return;
280         }
281         if (old_type.tv == tarval_reachable) {
282                 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
283         }
284         if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
285                 /* bottom reached */
286                 return;
287         }
288         panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
289 }
290 #else
291 #define verify_type(old_type, new_type)
292 #endif
293
294 /**
295  * Compare two pointer values of a listmap.
296  */
297 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
298         const listmap_entry_t *e1 = elt;
299         const listmap_entry_t *e2 = key;
300
301         (void) size;
302         return e1->id != e2->id;
303 }  /* listmap_cmp_ptr */
304
305 /**
306  * Initializes a listmap.
307  *
308  * @param map  the listmap
309  */
310 static void listmap_init(listmap_t *map) {
311         map->map    = new_set(listmap_cmp_ptr, 16);
312         map->values = NULL;
313 }  /* listmap_init */
314
315 /**
316  * Terminates a listmap.
317  *
318  * @param map  the listmap
319  */
320 static void listmap_term(listmap_t *map) {
321         del_set(map->map);
322 }  /* listmap_term */
323
324 /**
325  * Return the associated listmap entry for a given id.
326  *
327  * @param map  the listmap
328  * @param id   the id to search for
329  *
330  * @return the asociated listmap entry for the given id
331  */
332 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
333         listmap_entry_t key, *entry;
334
335         key.id   = id;
336         key.list = NULL;
337         key.next = NULL;
338         entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
339
340         if (entry->list == NULL) {
341                 /* a new entry, put into the list */
342                 entry->next = map->values;
343                 map->values = entry;
344         }
345         return entry;
346 }  /* listmap_find */
347
348 /**
349  * Calculate the hash value for an opcode map entry.
350  *
351  * @param entry  an opcode map entry
352  *
353  * @return a hash value for the given opcode map entry
354  */
355 static unsigned opcode_hash(const opcode_key_t *entry) {
356         return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
357 }  /* opcode_hash */
358
359 /**
360  * Compare two entries in the opcode map.
361  */
362 static int cmp_opcode(const void *elt, const void *key, size_t size) {
363         const opcode_key_t *o1 = elt;
364         const opcode_key_t *o2 = key;
365
366         (void) size;
367         return o1->code != o2->code || o1->mode != o2->mode ||
368                o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
369 }  /* cmp_opcode */
370
371 /**
372  * Compare two Def-Use edges for input position.
373  */
374 static int cmp_def_use_edge(const void *a, const void *b) {
375         const ir_def_use_edge *ea = a;
376         const ir_def_use_edge *eb = b;
377
378         /* no overrun, because range is [-1, MAXINT] */
379         return ea->pos - eb->pos;
380 }  /* cmp_def_use_edge */
381
382 /**
383  * We need the Def-Use edges sorted.
384  */
385 static void sort_irn_outs(node_t *node) {
386         ir_node *irn = node->node;
387         int n_outs = get_irn_n_outs(irn);
388
389         if (n_outs > 1) {
390                 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
391         }
392         node->max_user_input = irn->out[n_outs].pos;
393 }  /* sort_irn_outs */
394
395 /**
396  * Return the type of a node.
397  *
398  * @param irn  an IR-node
399  *
400  * @return the associated type of this node
401  */
402 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
403         return get_irn_node(irn)->type;
404 }  /* get_node_type */
405
406 /**
407  * Return the tarval of a node.
408  *
409  * @param irn  an IR-node
410  *
411  * @return the associated type of this node
412  */
413 static INLINE tarval *get_node_tarval(const ir_node *irn) {
414         lattice_elem_t type = get_node_type(irn);
415
416         if (is_tarval(type.tv))
417                 return type.tv;
418         return tarval_bottom;
419 }  /* get_node_type */
420
421 /**
422  * Add a partition to the worklist.
423  */
424 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
425         assert(X->on_worklist == 0);
426         X->wl_next     = env->worklist;
427         X->on_worklist = 1;
428         env->worklist  = X;
429 }  /* add_to_worklist */
430
431 /**
432  * Create a new empty partition.
433  *
434  * @param env   the environment
435  *
436  * @return a newly allocated partition
437  */
438 static INLINE partition_t *new_partition(environment_t *env) {
439         partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
440
441         INIT_LIST_HEAD(&part->Leader);
442         INIT_LIST_HEAD(&part->Follower);
443         INIT_LIST_HEAD(&part->cprop);
444         part->wl_next         = NULL;
445         part->touched_next    = NULL;
446         part->cprop_next      = NULL;
447         part->split_next      = NULL;
448         part->touched         = NULL;
449         part->n_leader        = 0;
450         part->n_touched       = 0;
451         part->max_user_inputs = 0;
452         part->on_worklist     = 0;
453         part->on_touched      = 0;
454         part->on_cprop        = 0;
455         part->type_is_T_or_C  = 0;
456 #ifdef DEBUG_libfirm
457         part->dbg_next        = env->dbg_list;
458         env->dbg_list         = part;
459         part->nr              = part_nr++;
460 #endif
461
462         return part;
463 }  /* new_partition */
464
465 /**
466  * Get the first node from a partition.
467  */
468 static INLINE node_t *get_first_node(const partition_t *X) {
469         return list_entry(X->Leader.next, node_t, node_list);
470 }
471
472 /**
473  * Return the type of a partition (assuming partition is non-empty and
474  * all elements have the same type).
475  *
476  * @param X  a partition
477  *
478  * @return the type of the first element of the partition
479  */
480 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
481         const node_t *first = get_first_node(X);
482         return first->type;
483 }  /* get_partition_type */
484
485 /**
486  * Creates a partition node for the given IR-node and place it
487  * into the given partition.
488  *
489  * @param irn   an IR-node
490  * @param part  a partition to place the node in
491  * @param env   the environment
492  *
493  * @return the created node
494  */
495 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
496         /* create a partition node and place it in the partition */
497         node_t *node = obstack_alloc(&env->obst, sizeof(*node));
498
499         INIT_LIST_HEAD(&node->node_list);
500         INIT_LIST_HEAD(&node->cprop_list);
501         node->node           = irn;
502         node->part           = part;
503         node->next           = NULL;
504         node->race_next      = NULL;
505         node->type.tv        = tarval_top;
506         node->max_user_input = 0;
507         node->next_edge      = 0;
508         node->n_followers    = 0;
509         node->on_touched     = 0;
510         node->on_cprop       = 0;
511         node->on_fallen      = 0;
512         node->is_follower    = 0;
513         node->is_flagged     = 0;
514         node->by_all_const   = 0;
515         set_irn_node(irn, node);
516
517         list_add_tail(&node->node_list, &part->Leader);
518         ++part->n_leader;
519
520         return node;
521 }  /* create_partition_node */
522
523 /**
524  * Pre-Walker, init all Block-Phi lists.
525  */
526 static void init_block_phis(ir_node *irn, void *env) {
527         (void) env;
528
529         if (is_Block(irn)) {
530                 set_Block_phis(irn, NULL);
531         }
532 }
533
534 /**
535  * Post-Walker, initialize all Nodes' type to U or top and place
536  * all nodes into the TOP partition.
537  */
538 static void create_initial_partitions(ir_node *irn, void *ctx) {
539         environment_t *env  = ctx;
540         partition_t   *part = env->initial;
541         node_t        *node;
542
543         node = create_partition_node(irn, part, env);
544         sort_irn_outs(node);
545         if (node->max_user_input > part->max_user_inputs)
546                 part->max_user_inputs = node->max_user_input;
547
548         if (is_Phi(irn)) {
549                 add_Block_phi(get_nodes_block(irn), irn);
550         }
551 }  /* create_initial_partitions */
552
553 /**
554  * Add a partition to the touched set if not already there.
555  *
556  * @param part  the partition
557  * @param env   the environment
558  */
559 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
560         if (part->on_touched == 0) {
561                 part->touched_next = env->touched;
562                 env->touched       = part;
563                 part->on_touched   = 1;
564         }
565 }  /* add_to_touched */
566
567 /**
568  * Add a node to the entry.partition.touched set if not already there.
569  *
570  * @param y  a node
571  */
572 static INLINE void add_to_partition_touched(node_t *y) {
573         if (y->on_touched == 0) {
574                 partition_t *part = y->part;
575
576                 y->next       = part->touched;
577                 part->touched = y;
578                 y->on_touched = 1;
579                 ++part->n_touched;
580         }
581 }  /* add_to_partition_touched */
582
583 /**
584  * Update the worklist: If Z is on worklist then add Z' to worklist.
585  * Else add the smaller of Z and Z' to worklist.
586  *
587  * @param Z        the Z partition
588  * @param Z_prime  the Z' partition, a previous part of Z
589  * @param env      the environment
590  */
591 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
592         if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
593                 add_to_worklist(Z_prime, env);
594         } else {
595                 add_to_worklist(Z, env);
596         }
597 }  /* update_worklist */
598
599 /**
600  * Split a partition that has NO followers by a local list.
601  *
602  * @param Z    the Z partition to split
603  * @param g    a (non-empty) node list
604  * @param env  the environment
605  *
606  * @return  a new partition containing the nodes of g
607  */
608 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
609         partition_t *Z_prime;
610         node_t      *node;
611         unsigned    n = 0;
612         int         max_input;
613
614         dump_partition("Splitting ", Z);
615
616         assert(g != NULL);
617
618         /* Remove g from Z. */
619         for (node = g; node != NULL; node = node->next) {
620                 list_del(&node->node_list);
621                 ++n;
622         }
623         assert(n < Z->n_leader);
624         Z->n_leader -= n;
625
626         /* Move g to a new partition, Z\92. */
627         Z_prime = new_partition(env);
628         max_input = 0;
629         for (node = g; node != NULL; node = node->next) {
630                 list_add(&node->node_list, &Z_prime->Leader);
631                 node->part = Z_prime;
632                 if (node->max_user_input > max_input)
633                         max_input = node->max_user_input;
634         }
635         Z_prime->max_user_inputs = max_input;
636         Z_prime->n_leader       = n;
637
638         /* for now, copy the type info tag. it will be adjusted
639            in split_by(). */
640         Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
641
642         update_worklist(Z, Z_prime, env);
643
644         dump_partition("Now ", Z);
645         dump_partition("Created new ", Z_prime);
646         return Z_prime;
647 }  /* split_no_followers */
648
649 #ifdef NO_FOLLOWER
650
651 #define split(Z, g, env) split_no_followers(Z, g, env)
652
653 #else
654
655 /**
656  * The environment for one race step.
657  */
658 typedef struct step_env {
659         node_t *initial;       /**< The initial node list. */
660         node_t *unwalked;      /**< The unwalked node list. */
661         node_t *unwalked_last; /**< Points to the last element of the unwalked node list. */
662         node_t *walked;        /**< The walked node list. */
663         int    index;          /**< Next index of Follower use_def edge. */
664         int    n_leader;       /**< number of Leader in initial. */
665 } step_env;
666
667 /**
668  * Do one step in the race.
669  */
670 static int step(step_env *env) {
671         node_t *n;
672
673         if (env->initial != NULL) {
674                 /* Move node from initial to unwalked */
675                 n = env->initial;
676                 env->initial = n->race_next;
677
678                 if (env->unwalked_last == NULL)
679                         env->unwalked_last = n;
680
681                 n->race_next  = env->unwalked;
682                 env->unwalked = n;
683
684                 return 0;
685         }
686
687         while (env->unwalked != NULL) {
688                 /* let n be the first node in unwalked */
689                 n = env->unwalked;
690                 while (env->index < n->n_followers) {
691                         /* let m be n.F.def_use[index] */
692                         node_t *m = get_irn_node(n->node->out[1 + env->index].use);
693
694                         assert(m->is_follower);
695                         ++env->index;
696
697                         /* only followers from our partition */
698                         if (m->part != n->part)
699                                 continue;
700
701                         if (! m->is_flagged) {
702                                 m->is_flagged = 1;
703
704                                 /* add m to unwalked not as first node */
705                                 m->race_next = NULL;
706                                 if (env->unwalked == NULL) {
707                                         env->unwalked = m;
708                                 } else {
709                                         env->unwalked_last->race_next = m;
710                                 }
711                                 env->unwalked_last = m;
712                                 return 0;
713                         }
714                 }
715                 /* move n to walked */
716                 env->unwalked = n->race_next;
717                 n->race_next  = env->walked;
718                 env->walked   = n;
719                 env->index    = 0;
720         }
721         return 1;
722 }  /* step */
723
724 /**
725  * Clear the flags from a list.
726  *
727  * @param list  the list
728  */
729 static void clear_flags(node_t *list) {
730         node_t *n;
731
732         for (n = list; n != NULL; n = n->race_next)
733                 n->is_flagged = 0;
734 }  /* clear_flags */
735
736 /**
737  * Split a partition by a local list using the race.
738  *
739  * @param X    the partition to split
740  * @param gg   a (non-empty) node list
741  * @param env  the environment
742  *
743  * @return  a new partition containing the nodes of gg
744  */
745 static partition_t *split(partition_t *X, node_t *gg, environment_t *env) {
746         partition_t *X_prime;
747         list_head   tmp;
748         step_env    env1, env2, *winner;
749         node_t      *g, *h, *node;
750         int         max_input, n, m;
751
752         if (list_empty(&X->Follower)) {
753                 /* if the partition has NO follower, we can use the fast
754                    splitting algorithm. */
755                 return split_no_followers(X, gg, env);
756         }
757         /* else do the race */
758
759         dump_partition("Splitting ", X);
760         dump_list("by list ", gg);
761
762         INIT_LIST_HEAD(&tmp);
763
764         /* Remove gg from X.Leader and put into g */
765         g = NULL;
766         n = 0;
767         for (node = gg; node != NULL; node = node->next) {
768                 list_del(&node->node_list);
769                 list_add_tail(&node->node_list, &tmp);
770                 node->race_next = g;
771                 g               = node;
772                 ++n;
773         }
774         /* produce h */
775         h = NULL;
776         m = 0;
777         list_for_each_entry(node_t, node, &X->Leader, node_list) {
778                 node->race_next = h;
779                 h               = node;
780                 ++m;
781         }
782         /* restore X.Leader */
783         list_splice(&tmp, &X->Leader);
784
785         env1.initial       = g;
786         env1.unwalked      = NULL;
787         env1.unwalked_last = NULL;
788         env1.walked        = NULL;
789         env1.index         = 0;
790         env1.n_leader      = n;
791
792         env2.initial       = h;
793         env2.unwalked      = NULL;
794         env2.unwalked_last = NULL;
795         env2.walked        = NULL;
796         env2.index         = 0;
797         env2.n_leader      = m;
798
799         for (;;) {
800                 if (step(&env1)) {
801                         winner = &env1;
802                         break;
803                 }
804                 if (step(&env2)) {
805                         winner = &env2;
806                         break;
807                 }
808         }
809         assert(winner->initial == NULL);
810         assert(winner->unwalked == NULL);
811
812         /* clear flags from walked/unwalked */
813         clear_flags(env1.unwalked);
814         clear_flags(env1.walked);
815         clear_flags(env2.unwalked);
816         clear_flags(env2.walked);
817
818         dump_race_list("winner ", winner->walked);
819
820         /* Move walked_{winner} to a new partition, X\92. */
821         X_prime = new_partition(env);
822         max_input = 0;
823         for (node = winner->walked; node != NULL; node = node->race_next) {
824                 list_del(&node->node_list);
825                 if (node->is_follower) {
826                         list_add(&node->node_list, &X_prime->Follower);
827                 } else {
828                         list_add(&node->node_list, &X_prime->Leader);
829                         ++X_prime->n_leader;
830                 }
831                 node->part = X_prime;
832                 if (node->max_user_input > max_input)
833                         max_input = node->max_user_input;
834         }
835         X_prime->max_user_inputs = max_input;
836         X->n_leader             -= winner->n_leader;
837
838         /* for now, copy the type info tag. it will be adjusted
839            in split_by(). */
840         X_prime->type_is_T_or_C = X->type_is_T_or_C;
841
842         update_worklist(X, X_prime, env);
843
844         dump_partition("Now ", X);
845         dump_partition("Created new ", X_prime);
846         return X_prime;
847 }  /* split */
848 #endif /* NO_FOLLOWER */
849
850 /**
851  * Returns non-zero if the i'th input of a Phi node is live.
852  *
853  * @param phi  a Phi-node
854  * @param i    an input number
855  *
856  * @return non-zero if the i'th input of the given Phi node is live
857  */
858 static int is_live_input(ir_node *phi, int i) {
859         if (i >= 0) {
860                 ir_node        *block = get_nodes_block(phi);
861                 ir_node        *pred  = get_Block_cfgpred(block, i);
862                 lattice_elem_t type   = get_node_type(pred);
863
864                 return type.tv != tarval_unreachable;
865         }
866         /* else it's the control input, always live */
867         return 1;
868 }  /* is_live_input */
869
870 /**
871  * Return non-zero if a type is a constant.
872  */
873 static int is_constant_type(lattice_elem_t type) {
874         if (type.tv != tarval_bottom && type.tv != tarval_top)
875                 return 1;
876         return 0;
877 }  /* is_constant_type */
878
879 /**
880  * Place a node on the cprop list.
881  *
882  * @param y    the node
883  * @param env  the environment
884  */
885 static void add_node_to_cprop(node_t *y, environment_t *env) {
886         /* Add y to y.partition.cprop. */
887         if (y->on_cprop == 0) {
888                 partition_t *Y = y->part;
889
890                 list_add_tail(&y->cprop_list, &Y->cprop);
891                 y->on_cprop   = 1;
892
893                 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
894
895                 /* place its partition on the cprop list */
896                 if (Y->on_cprop == 0) {
897                         Y->cprop_next = env->cprop;
898                         env->cprop    = Y;
899                         Y->on_cprop   = 1;
900                 }
901         }
902         if (get_irn_mode(y->node) == mode_T) {
903                 /* mode_T nodes always produce tarval_bottom, so we must explicitly
904                    add it's Proj's to get constant evaluation to work */
905                 int i;
906
907                 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
908                         node_t *proj = get_irn_node(get_irn_out(y->node, i));
909
910                         add_node_to_cprop(proj, env);
911                 }
912         }
913
914         if (is_Block(y->node)) {
915                 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
916                  * if someone placed the block. The Block is only placed if the reachability
917                  * changes, and this must be re-evaluated in compute_Phi(). */
918                 ir_node *phi;
919                 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
920                         node_t *p = get_irn_node(phi);
921                         add_node_to_cprop(p, env);
922                 }
923         }
924 }  /* add_node_to_cprop */
925
926 /**
927  * Check whether a type is neither Top or a constant.
928  * Note: U is handled like Top here, R is a constant.
929  *
930  * @param type  the type to check
931  */
932 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
933         if (is_tarval(type.tv)) {
934                 if (type.tv == tarval_top)
935                         return 0;
936                 if (tarval_is_constant(type.tv))
937                         return 0;
938         } else {
939                 /* is a symconst */
940                 return 0;
941         }
942         return 1;
943 }
944
945 /**
946  * Collect nodes to the touched list.
947  *
948  * @param list  the list which contains the nodes that must be evaluated
949  * @param idx   the index of the def_use edge to evaluate
950  * @param env   the environment
951  */
952 static void collect_touched(list_head *list, int idx, environment_t *env) {
953         node_t  *x, *y;
954         int     end_idx = env->end_idx;
955
956         list_for_each_entry(node_t, x, list, node_list) {
957                 int num_edges;
958
959                 if (idx == -1) {
960                         /* leader edges start AFTER follower edges */
961                         x->next_edge = 1 + x->n_followers;
962                 }
963                 num_edges = get_irn_n_outs(x->node);
964
965                 /* for all edges in x.L.def_use_{idx} */
966                 while (x->next_edge <= num_edges) {
967                         ir_def_use_edge *edge = &x->node->out[x->next_edge];
968                         ir_node         *succ;
969
970                         /* check if we have necessary edges */
971                         if (edge->pos > idx)
972                                 break;
973
974                         ++x->next_edge;
975
976                         succ = edge->use;
977
978                         /* ignore the "control input" for non-pinned nodes
979                         if we are running in GCSE mode */
980                         if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
981                                 continue;
982
983                         y = get_irn_node(succ);
984                         if (is_constant_type(y->type)) {
985                                 ir_opcode code = get_irn_opcode(succ);
986                                 if (code == iro_Sub || code == iro_Cmp)
987                                         add_node_to_cprop(y, env);
988                         }
989
990                         /* Partitions of constants should not be split simply because their Nodes have unequal
991                         functions or incongruent inputs. */
992                         if (type_is_neither_top_nor_const(y->type) &&
993                                 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
994                                         partition_t *Y = y->part;
995                                         add_to_touched(Y, env);
996                                         add_to_partition_touched(y);
997                         }
998                 }
999         }
1000 }
1001 /**
1002  * Split the partitions if caused by the first entry on the worklist.
1003  *
1004  * @param env  the environment
1005  */
1006 static void cause_splits(environment_t *env) {
1007         partition_t *X, *Z;
1008         node_t      *e;
1009         int         idx;
1010
1011         /* remove the first partition from the worklist */
1012         X = env->worklist;
1013         env->worklist  = X->wl_next;
1014         X->on_worklist = 0;
1015
1016         dump_partition("Cause_split: ", X);
1017
1018         /* combine temporary leader and follower list */
1019         for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1020                 /* empty the touched set: already done, just clear the list */
1021                 env->touched = NULL;
1022
1023                 collect_touched(&X->Leader, idx, env);
1024                 collect_touched(&X->Follower, idx, env);
1025
1026                 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
1027                         /* remove it from the touched set */
1028                         Z->on_touched = 0;
1029
1030                         if (Z->n_leader != Z->n_touched) {
1031                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1032                                 split(Z, Z->touched, env);
1033                         }
1034                         /* Empty local Z.touched. */
1035                         for (e = Z->touched; e != NULL; e = e->next) {
1036                                 e->on_touched = 0;
1037                         }
1038                         Z->touched   = NULL;
1039                         Z->n_touched = 0;
1040                 }
1041         }
1042 }  /* cause_splits */
1043
1044 /**
1045  * Implements split_by_what(): Split a partition by characteristics given
1046  * by the what function.
1047  *
1048  * @param X     the partition to split
1049  * @param What  a function returning an Id for every node of the partition X
1050  * @param P     a list to store the result partitions
1051  * @param env   the environment
1052  *
1053  * @return *P
1054  */
1055 static partition_t *split_by_what(partition_t *X, what_func What,
1056                                   partition_t **P, environment_t *env) {
1057         node_t          *x, *S;
1058         listmap_t       map;
1059         listmap_entry_t *iter;
1060         partition_t     *R;
1061
1062         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1063         listmap_init(&map);
1064         list_for_each_entry(node_t, x, &X->Leader, node_list) {
1065                 void            *id = What(x, env);
1066                 listmap_entry_t *entry;
1067
1068                 if (id == NULL) {
1069                         /* input not allowed, ignore */
1070                         continue;
1071                 }
1072                 /* Add x to map[What(x)]. */
1073                 entry = listmap_find(&map, id);
1074                 x->next     = entry->list;
1075                 entry->list = x;
1076         }
1077         /* Let P be a set of Partitions. */
1078
1079         /* for all sets S except one in the range of map do */
1080         for (iter = map.values; iter != NULL; iter = iter->next) {
1081                 if (iter->next == NULL) {
1082                         /* this is the last entry, ignore */
1083                         break;
1084                 }
1085                 S = iter->list;
1086
1087                 /* Add SPLIT( X, S ) to P. */
1088                 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1089                 R = split(X, S, env);
1090                 R->split_next = *P;
1091                 *P            = R;
1092         }
1093         /* Add X to P. */
1094         X->split_next = *P;
1095         *P            = X;
1096
1097         listmap_term(&map);
1098         return *P;
1099 }  /* split_by_what */
1100
1101 /** lambda n.(n.type) */
1102 static void *lambda_type(const node_t *node, environment_t *env) {
1103         (void)env;
1104         return node->type.tv;
1105 }  /* lambda_type */
1106
1107 /** lambda n.(n.opcode) */
1108 static void *lambda_opcode(const node_t *node, environment_t *env) {
1109         opcode_key_t key, *entry;
1110         ir_node      *irn = node->node;
1111
1112         key.code   = get_irn_opcode(irn);
1113         key.mode   = get_irn_mode(irn);
1114         key.u.proj = 0;
1115         key.u.ent  = NULL;
1116
1117         switch (get_irn_opcode(irn)) {
1118         case iro_Proj:
1119                 key.u.proj = get_Proj_proj(irn);
1120                 break;
1121         case iro_Sel:
1122                 key.u.ent = get_Sel_entity(irn);
1123                 break;
1124         default:
1125                 break;
1126         }
1127
1128         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1129         return entry;
1130 }  /* lambda_opcode */
1131
1132 /** lambda n.(n[i].partition) */
1133 static void *lambda_partition(const node_t *node, environment_t *env) {
1134         ir_node *skipped = skip_Proj(node->node);
1135         ir_node *pred;
1136         node_t  *p;
1137         int     i = env->lambda_input;
1138
1139         if (i >= get_irn_arity(node->node)) {
1140                 /* we are outside the allowed range */
1141                 return NULL;
1142         }
1143
1144         /* ignore the "control input" for non-pinned nodes
1145            if we are running in GCSE mode */
1146         if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1147                 return NULL;
1148
1149         pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1150         p    = get_irn_node(pred);
1151
1152         return p->part;
1153 }  /* lambda_partition */
1154
1155 /**
1156  * Returns true if a type is a constant.
1157  */
1158 static int is_con(const lattice_elem_t type) {
1159         /* be conservative */
1160         if (is_tarval(type.tv))
1161                 return tarval_is_constant(type.tv);
1162         return is_entity(type.sym.entity_p);
1163 }  /* is_con */
1164
1165 /**
1166  * Implements split_by().
1167  *
1168  * @param X    the partition to split
1169  * @param env  the environment
1170  */
1171 static void split_by(partition_t *X, environment_t *env) {
1172         partition_t *I, *P = NULL;
1173         int         input;
1174
1175         dump_partition("split_by", X);
1176
1177         if (X->n_leader == 1) {
1178                 /* we have only one leader, no need to split, just check it's type */
1179                 node_t *x = get_first_node(X);
1180                 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1181                 return;
1182         }
1183
1184         DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1185         P = split_by_what(X, lambda_type, &P, env);
1186
1187         /* adjust the type tags, we have split partitions by type */
1188         for (I = P; I != NULL; I = I->split_next) {
1189                 node_t *x = get_first_node(I);
1190                 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1191         }
1192
1193         do {
1194                 partition_t *Y = P;
1195
1196                 P = P->split_next;
1197                 if (Y->n_leader > 1) {
1198                         /* we do not want split the TOP or constant partitions */
1199                         if (! Y->type_is_T_or_C) {
1200                                 partition_t *Q = NULL;
1201
1202                                 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1203                                 Q = split_by_what(Y, lambda_opcode, &Q, env);
1204
1205                                 do {
1206                                         partition_t *Z = Q;
1207
1208                                         Q = Q->split_next;
1209                                         if (Z->n_leader > 1) {
1210                                                 const node_t *first = get_first_node(Z);
1211                                                 int          arity  = get_irn_arity(first->node);
1212                                                 partition_t  *R, *S;
1213
1214                                                 /*
1215                                                  * BEWARE: during splitting by input 2 for instance we might
1216                                                  * create new partitions which are different by input 1, so collect
1217                                                  * them and split further.
1218                                                  */
1219                                                 Z->split_next = NULL;
1220                                                 R             = Z;
1221                                                 S             = NULL;
1222                                                 for (input = arity - 1; input >= -1; --input) {
1223                                                         do {
1224                                                                 partition_t *Z_prime = R;
1225
1226                                                                 R = R->split_next;
1227                                                                 if (Z_prime->n_leader > 1) {
1228                                                                         env->lambda_input = input;
1229                                                                         DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1230                                                                         S = split_by_what(Z_prime, lambda_partition, &S, env);
1231                                                                 } else {
1232                                                                         Z_prime->split_next = S;
1233                                                                         S                   = Z_prime;
1234                                                                 }
1235                                                         } while (R != NULL);
1236                                                         R = S;
1237                                                         S = NULL;
1238                                                 }
1239                                         }
1240                                 } while (Q != NULL);
1241                         }
1242                 }
1243         } while (P != NULL);
1244 }  /* split_by */
1245
1246 /**
1247  * (Re-)compute the type for a given node.
1248  *
1249  * @param node  the node
1250  */
1251 static void default_compute(node_t *node) {
1252         int     i;
1253         ir_node *irn = node->node;
1254         node_t  *block = get_irn_node(get_nodes_block(irn));
1255
1256         if (block->type.tv == tarval_unreachable) {
1257                 node->type.tv = tarval_top;
1258                 return;
1259         }
1260
1261         /* if any of the data inputs have type top, the result is type top */
1262         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1263                 ir_node *pred = get_irn_n(irn, i);
1264                 node_t  *p    = get_irn_node(pred);
1265
1266                 if (p->type.tv == tarval_top) {
1267                         node->type.tv = tarval_top;
1268                         return;
1269                 }
1270         }
1271
1272         if (get_irn_mode(node->node) == mode_X)
1273                 node->type.tv = tarval_reachable;
1274         else
1275                 node->type.tv = computed_value(irn);
1276 }  /* default_compute */
1277
1278 /**
1279  * (Re-)compute the type for a Block node.
1280  *
1281  * @param node  the node
1282  */
1283 static void compute_Block(node_t *node) {
1284         int     i;
1285         ir_node *block = node->node;
1286
1287         if (block == get_irg_start_block(current_ir_graph)) {
1288                 /* start block is always reachable */
1289                 node->type.tv = tarval_reachable;
1290                 return;
1291         }
1292
1293         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1294                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1295
1296                 if (pred->type.tv == tarval_reachable) {
1297                         /* A block is reachable, if at least of predecessor is reachable. */
1298                         node->type.tv = tarval_reachable;
1299                         return;
1300                 }
1301         }
1302         node->type.tv = tarval_top;
1303 }  /* compute_Block */
1304
1305 /**
1306  * (Re-)compute the type for a Bad node.
1307  *
1308  * @param node  the node
1309  */
1310 static void compute_Bad(node_t *node) {
1311         /* Bad nodes ALWAYS compute Top */
1312         node->type.tv = tarval_top;
1313 }  /* compute_Bad */
1314
1315 /**
1316  * (Re-)compute the type for an Unknown node.
1317  *
1318  * @param node  the node
1319  */
1320 static void compute_Unknown(node_t *node) {
1321         /* While Unknown nodes should compute Top this is dangerous:
1322          * a Top input to a Cond would lead to BOTH control flows unreachable.
1323          * While this is correct in the given semantics, it would destroy the Firm
1324          * graph.
1325          *
1326          * It would be safe to compute Top IF it can be assured, that only Cmp
1327          * nodes are inputs to Conds. We check that first.
1328          * This is the way Frontends typically build Firm, but some optimizations
1329          * (cond_eval for instance) might replace them by Phib's...
1330          *
1331          * For now, we compute bottom here.
1332          */
1333         node->type.tv = tarval_bottom;
1334 }  /* compute_Unknown */
1335
1336 /**
1337  * (Re-)compute the type for a Jmp node.
1338  *
1339  * @param node  the node
1340  */
1341 static void compute_Jmp(node_t *node) {
1342         node_t *block = get_irn_node(get_nodes_block(node->node));
1343
1344         node->type = block->type;
1345 }  /* compute_Jmp */
1346
1347 /**
1348  * (Re-)compute the type for the End node.
1349  *
1350  * @param node  the node
1351  */
1352 static void compute_End(node_t *node) {
1353         /* the End node is NOT dead of course */
1354         node->type.tv = tarval_reachable;
1355 }
1356
1357 /**
1358  * (Re-)compute the type for a SymConst node.
1359  *
1360  * @param node  the node
1361  */
1362 static void compute_SymConst(node_t *node) {
1363         ir_node *irn = node->node;
1364         node_t  *block = get_irn_node(get_nodes_block(irn));
1365
1366         if (block->type.tv == tarval_unreachable) {
1367                 node->type.tv = tarval_top;
1368                 return;
1369         }
1370         switch (get_SymConst_kind(irn)) {
1371         case symconst_addr_ent:
1372         /* case symconst_addr_name: cannot handle this yet */
1373                 node->type.sym = get_SymConst_symbol(irn);
1374                 break;
1375         default:
1376                 node->type.tv = computed_value(irn);
1377         }
1378 }  /* compute_SymConst */
1379
1380 /**
1381  * (Re-)compute the type for a Phi node.
1382  *
1383  * @param node  the node
1384  */
1385 static void compute_Phi(node_t *node) {
1386         int            i;
1387         ir_node        *phi = node->node;
1388         lattice_elem_t type;
1389
1390         /* if a Phi is in a unreachable block, its type is TOP */
1391         node_t *block = get_irn_node(get_nodes_block(phi));
1392
1393         if (block->type.tv == tarval_unreachable) {
1394                 node->type.tv = tarval_top;
1395                 return;
1396         }
1397
1398         /* Phi implements the Meet operation */
1399         type.tv = tarval_top;
1400         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1401                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
1402                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1403
1404                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1405                         /* ignore TOP inputs: We must check here for unreachable blocks,
1406                            because Firm constants live in the Start Block are NEVER Top.
1407                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1408                            comes from a unreachable input. */
1409                         continue;
1410                 }
1411                 if (pred->type.tv == tarval_bottom) {
1412                         node->type.tv = tarval_bottom;
1413                         return;
1414                 } else if (type.tv == tarval_top) {
1415                         /* first constant found */
1416                         type = pred->type;
1417                 } else if (type.tv != pred->type.tv) {
1418                         /* different constants or tarval_bottom */
1419                         node->type.tv = tarval_bottom;
1420                         return;
1421                 }
1422                 /* else nothing, constants are the same */
1423         }
1424         node->type = type;
1425 }  /* compute_Phi */
1426
1427 /**
1428  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1429  *
1430  * @param node  the node
1431  */
1432 static void compute_Add(node_t *node) {
1433         ir_node        *sub = node->node;
1434         node_t         *l   = get_irn_node(get_Add_left(sub));
1435         node_t         *r   = get_irn_node(get_Add_right(sub));
1436         lattice_elem_t a    = l->type;
1437         lattice_elem_t b    = r->type;
1438         ir_mode        *mode;
1439
1440         if (a.tv == tarval_top || b.tv == tarval_top) {
1441                 node->type.tv = tarval_top;
1442         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1443                 node->type.tv = tarval_bottom;
1444         } else {
1445                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1446                    must call tarval_add() first to handle this case! */
1447                 if (is_tarval(a.tv)) {
1448                         if (is_tarval(b.tv)) {
1449                                 node->type.tv = tarval_add(a.tv, b.tv);
1450                                 return;
1451                         }
1452                         mode = get_tarval_mode(a.tv);
1453                         if (a.tv == get_mode_null(mode)) {
1454                                 node->type = b;
1455                                 return;
1456                         }
1457                 } else if (is_tarval(b.tv)) {
1458                         mode = get_tarval_mode(b.tv);
1459                         if (b.tv == get_mode_null(mode)) {
1460                                 node->type = a;
1461                                 return;
1462                         }
1463                 }
1464                 node->type.tv = tarval_bottom;
1465         }
1466 }  /* compute_Add */
1467
1468 /**
1469  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1470  *
1471  * @param node  the node
1472  */
1473 static void compute_Sub(node_t *node) {
1474         ir_node        *sub = node->node;
1475         node_t         *l   = get_irn_node(get_Sub_left(sub));
1476         node_t         *r   = get_irn_node(get_Sub_right(sub));
1477         lattice_elem_t a    = l->type;
1478         lattice_elem_t b    = r->type;
1479         tarval         *tv;
1480
1481         if (a.tv == tarval_top || b.tv == tarval_top) {
1482                 node->type.tv = tarval_top;
1483         } else if (is_con(a) && is_con(b)) {
1484                 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1485                         node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1486                 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1487                         node->type = b;
1488                 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1489                         node->type = a;
1490                 } else {
1491                         node->type.tv = tarval_bottom;
1492                 }
1493                 node->by_all_const = 1;
1494         } else if (r->part == l->part &&
1495                    (!mode_is_float(get_irn_mode(l->node)))) {
1496                 /*
1497                  * BEWARE: a - a is NOT always 0 for floating Point values, as
1498                  * NaN op NaN = NaN, so we must check this here.
1499                  */
1500                 ir_mode *mode = get_irn_mode(sub);
1501                 tv = get_mode_null(mode);
1502
1503                 /* if the node was ONCE evaluated by all constants, but now
1504                    this breakes AND we cat by partition a different result, switch to bottom.
1505                    This happens because initially all nodes are in the same partition ... */
1506                 if (node->by_all_const && node->type.tv != tv)
1507                         tv = tarval_bottom;
1508                 node->type.tv = tv;
1509         } else {
1510                 node->type.tv = tarval_bottom;
1511         }
1512 }  /* compute_Sub */
1513
1514 /**
1515  * (Re-)compute the type for Cmp.
1516  *
1517  * @param node  the node
1518  */
1519 static void compute_Cmp(node_t *node) {
1520         ir_node        *cmp  = node->node;
1521         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1522         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1523         lattice_elem_t a     = l->type;
1524         lattice_elem_t b     = r->type;
1525
1526         if (a.tv == tarval_top || b.tv == tarval_top) {
1527                 node->type.tv = tarval_top;
1528         } else if (is_con(a) && is_con(b)) {
1529                 /* both nodes are constants, we can probably do something */
1530                 node->type.tv = tarval_b_true;
1531         } else if (r->part == l->part) {
1532                 /* both nodes congruent, we can probably do something */
1533                 node->type.tv = tarval_b_true;
1534         } else {
1535                 node->type.tv = tarval_bottom;
1536         }
1537 }  /* compute_Proj_Cmp */
1538
1539 /**
1540  * (Re-)compute the type for a Proj(Cmp).
1541  *
1542  * @param node  the node
1543  * @param cond  the predecessor Cmp node
1544  */
1545 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1546         ir_node        *proj = node->node;
1547         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1548         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1549         lattice_elem_t a     = l->type;
1550         lattice_elem_t b     = r->type;
1551         pn_Cmp         pnc   = get_Proj_proj(proj);
1552         tarval         *tv;
1553
1554         if (a.tv == tarval_top || b.tv == tarval_top) {
1555                 node->type.tv = tarval_top;
1556         } else if (is_con(a) && is_con(b)) {
1557                 default_compute(node);
1558                 node->by_all_const = 1;
1559         } else if (r->part == l->part &&
1560                    (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1561                 /*
1562                  * BEWARE: a == a is NOT always True for floating Point values, as
1563                  * NaN != NaN is defined, so we must check this here.
1564                  */
1565                 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1566
1567                 /* if the node was ONCE evaluated by all constants, but now
1568                    this breakes AND we cat by partition a different result, switch to bottom.
1569                    This happens because initially all nodes are in the same partition ... */
1570                 if (node->by_all_const && node->type.tv != tv)
1571                         tv = tarval_bottom;
1572                 node->type.tv = tv;
1573         } else {
1574                 node->type.tv = tarval_bottom;
1575         }
1576 }  /* compute_Proj_Cmp */
1577
1578 /**
1579  * (Re-)compute the type for a Proj(Cond).
1580  *
1581  * @param node  the node
1582  * @param cond  the predecessor Cond node
1583  */
1584 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1585         ir_node *proj     = node->node;
1586         long    pnc       = get_Proj_proj(proj);
1587         ir_node *sel      = get_Cond_selector(cond);
1588         node_t  *selector = get_irn_node(sel);
1589
1590         if (get_irn_mode(sel) == mode_b) {
1591                 /* an IF */
1592                 if (pnc == pn_Cond_true) {
1593                         if (selector->type.tv == tarval_b_false) {
1594                                 node->type.tv = tarval_unreachable;
1595                         } else if (selector->type.tv == tarval_b_true) {
1596                                 node->type.tv = tarval_reachable;
1597                         } else if (selector->type.tv == tarval_bottom) {
1598                                 node->type.tv = tarval_reachable;
1599                         } else {
1600                                 assert(selector->type.tv == tarval_top);
1601                                 node->type.tv = tarval_unreachable;
1602                         }
1603                 } else {
1604                         assert(pnc == pn_Cond_false);
1605
1606                         if (selector->type.tv == tarval_b_false) {
1607                                 node->type.tv = tarval_reachable;
1608                         } else if (selector->type.tv == tarval_b_true) {
1609                                 node->type.tv = tarval_unreachable;
1610                         } else if (selector->type.tv == tarval_bottom) {
1611                                 node->type.tv = tarval_reachable;
1612                         } else {
1613                                 assert(selector->type.tv == tarval_top);
1614                                 node->type.tv = tarval_unreachable;
1615                         }
1616                 }
1617         } else {
1618                 /* an SWITCH */
1619                 if (selector->type.tv == tarval_bottom) {
1620                         node->type.tv = tarval_reachable;
1621                 } else if (selector->type.tv == tarval_top) {
1622                         node->type.tv = tarval_unreachable;
1623                 } else {
1624                         long value = get_tarval_long(selector->type.tv);
1625                         if (pnc == get_Cond_defaultProj(cond)) {
1626                                 /* default switch, have to check ALL other cases */
1627                                 int i;
1628
1629                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1630                                         ir_node *succ = get_irn_out(cond, i);
1631
1632                                         if (succ == proj)
1633                                                 continue;
1634                                         if (value == get_Proj_proj(succ)) {
1635                                                 /* we found a match, will NOT take the default case */
1636                                                 node->type.tv = tarval_unreachable;
1637                                                 return;
1638                                         }
1639                                 }
1640                                 /* all cases checked, no match, will take default case */
1641                                 node->type.tv = tarval_reachable;
1642                         } else {
1643                                 /* normal case */
1644                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1645                         }
1646                 }
1647         }
1648 }  /* compute_Proj_Cond */
1649
1650 /**
1651  * (Re-)compute the type for a Proj-Node.
1652  *
1653  * @param node  the node
1654  */
1655 static void compute_Proj(node_t *node) {
1656         ir_node *proj = node->node;
1657         ir_mode *mode = get_irn_mode(proj);
1658         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1659         ir_node *pred  = get_Proj_pred(proj);
1660
1661         if (block->type.tv == tarval_unreachable) {
1662                 /* a Proj in a unreachable Block stay Top */
1663                 node->type.tv = tarval_top;
1664                 return;
1665         }
1666         if (get_irn_node(pred)->type.tv == tarval_top) {
1667                 /* if the predecessor is Top, its Proj follow */
1668                 node->type.tv = tarval_top;
1669                 return;
1670         }
1671
1672         if (mode == mode_M) {
1673                 /* mode M is always bottom */
1674                 node->type.tv = tarval_bottom;
1675                 return;
1676         }
1677         if (mode != mode_X) {
1678                 if (is_Cmp(pred))
1679                         compute_Proj_Cmp(node, pred);
1680                 else
1681                         default_compute(node);
1682                 return;
1683         }
1684         /* handle mode_X nodes */
1685
1686         switch (get_irn_opcode(pred)) {
1687         case iro_Start:
1688                 /* the Proj_X from the Start is always reachable.
1689                    However this is already handled at the top. */
1690                 node->type.tv = tarval_reachable;
1691                 break;
1692         case iro_Cond:
1693                 compute_Proj_Cond(node, pred);
1694                 break;
1695         default:
1696                 default_compute(node);
1697         }
1698 }  /* compute_Proj */
1699
1700 /**
1701  * (Re-)compute the type for a Confirm.
1702  *
1703  * @param node  the node
1704  */
1705 static void compute_Confirm(node_t *node) {
1706         ir_node *confirm = node->node;
1707         node_t  *pred = get_irn_node(get_Confirm_value(confirm));
1708
1709         if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1710                 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1711
1712                 if (is_con(bound->type)) {
1713                         /* is equal to a constant */
1714                         node->type = bound->type;
1715                         return;
1716                 }
1717         }
1718         /* a Confirm is a copy OR a Const */
1719         node->type = pred->type;
1720 }  /* compute_Confirm */
1721
1722 /**
1723  * (Re-)compute the type for a Max.
1724  *
1725  * @param node  the node
1726  */
1727 static void compute_Max(node_t *node) {
1728         ir_node        *op   = node->node;
1729         node_t         *l    = get_irn_node(get_binop_left(op));
1730         node_t         *r    = get_irn_node(get_binop_right(op));
1731         lattice_elem_t a     = l->type;
1732         lattice_elem_t b     = r->type;
1733
1734         if (a.tv == tarval_top || b.tv == tarval_top) {
1735                 node->type.tv = tarval_top;
1736         } else if (is_con(a) && is_con(b)) {
1737                 /* both nodes are constants, we can probably do something */
1738                 if (a.tv == b.tv) {
1739                         /* this case handles symconsts as well */
1740                         node->type = a;
1741                 } else {
1742                         ir_mode *mode   = get_irn_mode(op);
1743                         tarval  *tv_min = get_mode_min(mode);
1744
1745                         if (a.tv == tv_min)
1746                                 node->type = b;
1747                         else if (b.tv == tv_min)
1748                                 node->type = a;
1749                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1750                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1751                                         node->type.tv = a.tv;
1752                                 else
1753                                         node->type.tv = b.tv;
1754                         } else {
1755                                 node->type.tv = tarval_bad;
1756                         }
1757                 }
1758         } else if (r->part == l->part) {
1759                 /* both nodes congruent, we can probably do something */
1760                 node->type = a;
1761         } else {
1762                 node->type.tv = tarval_bottom;
1763         }
1764 }  /* compute_Max */
1765
1766 /**
1767  * (Re-)compute the type for a Min.
1768  *
1769  * @param node  the node
1770  */
1771 static void compute_Min(node_t *node) {
1772         ir_node        *op   = node->node;
1773         node_t         *l    = get_irn_node(get_binop_left(op));
1774         node_t         *r    = get_irn_node(get_binop_right(op));
1775         lattice_elem_t a     = l->type;
1776         lattice_elem_t b     = r->type;
1777
1778         if (a.tv == tarval_top || b.tv == tarval_top) {
1779                 node->type.tv = tarval_top;
1780         } else if (is_con(a) && is_con(b)) {
1781                 /* both nodes are constants, we can probably do something */
1782                 if (a.tv == b.tv) {
1783                         /* this case handles symconsts as well */
1784                         node->type = a;
1785                 } else {
1786                         ir_mode *mode   = get_irn_mode(op);
1787                         tarval  *tv_max = get_mode_max(mode);
1788
1789                         if (a.tv == tv_max)
1790                                 node->type = b;
1791                         else if (b.tv == tv_max)
1792                                 node->type = a;
1793                         else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1794                                 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1795                                         node->type.tv = a.tv;
1796                                 else
1797                                         node->type.tv = b.tv;
1798                         } else {
1799                                 node->type.tv = tarval_bad;
1800                         }
1801                 }
1802         } else if (r->part == l->part) {
1803                 /* both nodes congruent, we can probably do something */
1804                 node->type = a;
1805         } else {
1806                 node->type.tv = tarval_bottom;
1807         }
1808 }  /* compute_Min */
1809
1810 /**
1811  * (Re-)compute the type for a given node.
1812  *
1813  * @param node  the node
1814  */
1815 static void compute(node_t *node) {
1816         compute_func func;
1817
1818         if (is_no_Block(node->node)) {
1819                 node_t *block = get_irn_node(get_nodes_block(node->node));
1820
1821                 if (block->type.tv == tarval_unreachable) {
1822                         node->type.tv = tarval_top;
1823                         return;
1824                 }
1825         }
1826
1827         func = (compute_func)node->node->op->ops.generic;
1828         if (func != NULL)
1829                 func(node);
1830 }  /* compute */
1831
1832 /*
1833  * Identity functions: Note that one might thing that identity() is just a
1834  * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
1835  * here, because it expects that the identity node is one of the inputs, which is NOT
1836  * always true for equivalent_node() which can handle (and does sometimes) DAGs.
1837  * So, we have our own implementation, which copies some parts of equivalent_node()
1838  */
1839
1840 /**
1841  * Calculates the Identity for Phi nodes
1842  */
1843 static node_t *identity_Phi(node_t *node) {
1844         ir_node *phi    = node->node;
1845         ir_node *block  = get_nodes_block(phi);
1846         node_t  *n_part = NULL;
1847         int     i;
1848
1849         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1850                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
1851
1852                 if (pred_X->type.tv == tarval_reachable) {
1853                         node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1854
1855                         if (n_part == NULL)
1856                                 n_part = pred;
1857                         else if (n_part->part != pred->part) {
1858                                 /* incongruent inputs, not a follower */
1859                                 return node;
1860                         }
1861                 }
1862         }
1863         /* if n_part is NULL here, all inputs path are dead, the Phi computes
1864          * tarval_top, is in the TOP partition and should NOT being split! */
1865         assert(n_part != NULL);
1866         return n_part;
1867 }  /* identity_Phi */
1868
1869 /**
1870  * Calculates the Identity for commutative 0 neutral nodes.
1871  */
1872 static node_t *identity_comm_zero_binop(node_t *node) {
1873         ir_node *op   = node->node;
1874         node_t  *a    = get_irn_node(get_binop_left(op));
1875         node_t  *b    = get_irn_node(get_binop_right(op));
1876         ir_mode *mode = get_irn_mode(op);
1877         tarval  *zero;
1878
1879         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1880         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1881                 return node;
1882
1883         /* node: no input should be tarval_top, else the binop would be also
1884          * Top and not being split. */
1885         zero = get_mode_null(mode);
1886         if (a->type.tv == zero)
1887                 return b;
1888         if (b->type.tv == zero)
1889                 return a;
1890         return node;
1891 }  /* identity_comm_zero_binop */
1892
1893 #define identity_Add  identity_comm_zero_binop
1894 #define identity_Or   identity_comm_zero_binop
1895
1896 /**
1897  * Calculates the Identity for Mul nodes.
1898  */
1899 static node_t *identity_Mul(node_t *node) {
1900         ir_node *op   = node->node;
1901         node_t  *a    = get_irn_node(get_Mul_left(op));
1902         node_t  *b    = get_irn_node(get_Mul_right(op));
1903         ir_mode *mode = get_irn_mode(op);
1904         tarval  *one;
1905
1906         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1907         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1908                 return node;
1909
1910         /* node: no input should be tarval_top, else the binop would be also
1911          * Top and not being split. */
1912         one = get_mode_one(mode);
1913         if (a->type.tv == one)
1914                 return b;
1915         if (b->type.tv == one)
1916                 return a;
1917         return node;
1918 }  /* identity_Mul */
1919
1920 /**
1921  * Calculates the Identity for Sub nodes.
1922  */
1923 static node_t *identity_Sub(node_t *node) {
1924         ir_node *sub  = node->node;
1925         node_t  *b    = get_irn_node(get_Sub_right(sub));
1926         ir_mode *mode = get_irn_mode(sub);
1927
1928         /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1929         if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1930                 return node;
1931
1932         /* node: no input should be tarval_top, else the binop would be also
1933          * Top and not being split. */
1934         if (b->type.tv == get_mode_null(mode))
1935                 return get_irn_node(get_Sub_left(sub));
1936         return node;
1937 }  /* identity_Mul */
1938
1939 /**
1940  * Calculates the Identity for And nodes.
1941  */
1942 static node_t *identity_And(node_t *node) {
1943         ir_node *and = node->node;
1944         node_t  *a   = get_irn_node(get_And_left(and));
1945         node_t  *b   = get_irn_node(get_And_right(and));
1946         tarval  *neutral = get_mode_all_one(get_irn_mode(and));
1947
1948         /* node: no input should be tarval_top, else the And would be also
1949          * Top and not being split. */
1950         if (a->type.tv == neutral)
1951                 return b;
1952         if (b->type.tv == neutral)
1953                 return a;
1954         return node;
1955 }  /* identity_And */
1956
1957 /**
1958  * Calculates the Identity for Confirm nodes.
1959  */
1960 static node_t *identity_Confirm(node_t *node) {
1961         ir_node *confirm = node->node;
1962
1963         /* a Confirm is always a Copy */
1964         return get_irn_node(get_Confirm_value(confirm));
1965 }  /* identity_Confirm */
1966
1967 /**
1968  * Calculates the Identity for Mux nodes.
1969  */
1970 static node_t *identity_Mux(node_t *node) {
1971         ir_node *mux = node->node;
1972         node_t  *sel = get_irn_node(get_Mux_sel(mux));
1973         node_t  *t   = get_irn_node(get_Mux_true(mux));
1974         node_t  *f   = get_irn_node(get_Mux_false(mux));
1975
1976         if (t->part == f->part)
1977                 return t;
1978
1979         /* Mux sel input is mode_b, so it is always a tarval */
1980         if (sel->type.tv == tarval_b_true)
1981                 return t;
1982         if (sel->type.tv == tarval_b_false)
1983                 return f;
1984         return node;
1985 }  /* identity_Mux */
1986
1987 /**
1988  * Calculates the Identity for Min nodes.
1989  */
1990 static node_t *identity_Min(node_t *node) {
1991         ir_node *op   = node->node;
1992         node_t  *a    = get_irn_node(get_binop_left(op));
1993         node_t  *b    = get_irn_node(get_binop_right(op));
1994         ir_mode *mode = get_irn_mode(op);
1995         tarval  *tv_max;
1996
1997         if (a->part == b->part) {
1998                 /* leader of multiple predecessors */
1999                 return a;
2000         }
2001
2002         /* works even with NaN */
2003         tv_max = get_mode_max(mode);
2004         if (a->type.tv == tv_max)
2005                 return b;
2006         if (b->type.tv == tv_max)
2007                 return a;
2008         return node;
2009 }  /* identity_Min */
2010
2011 /**
2012  * Calculates the Identity for Max nodes.
2013  */
2014 static node_t *identity_Max(node_t *node) {
2015         ir_node *op   = node->node;
2016         node_t  *a    = get_irn_node(get_binop_left(op));
2017         node_t  *b    = get_irn_node(get_binop_right(op));
2018         ir_mode *mode = get_irn_mode(op);
2019         tarval  *tv_min;
2020
2021         if (a->part == b->part) {
2022                 /* leader of multiple predecessors */
2023                 return a;
2024         }
2025
2026         /* works even with NaN */
2027         tv_min = get_mode_min(mode);
2028         if (a->type.tv == tv_min)
2029                 return b;
2030         if (b->type.tv == tv_min)
2031                 return a;
2032         return node;
2033 }  /* identity_Max */
2034
2035 /**
2036  * Calculates the Identity for nodes.
2037  */
2038 static node_t *identity(node_t *node) {
2039         ir_node *irn = node->node;
2040
2041         switch (get_irn_opcode(irn)) {
2042         case iro_Phi:
2043                 return identity_Phi(node);
2044         case iro_Add:
2045                 return identity_Add(node);
2046         case iro_Mul:
2047                 return identity_Mul(node);
2048         case iro_Or:
2049                 return identity_Or(node);
2050         case iro_And:
2051                 return identity_And(node);
2052         case iro_Sub:
2053                 return identity_Sub(node);
2054         case iro_Confirm:
2055                 return identity_Confirm(node);
2056         case iro_Mux:
2057                 return identity_Mux(node);
2058         case iro_Min:
2059                 return identity_Min(node);
2060         case iro_Max:
2061                 return identity_Max(node);
2062         default:
2063                 return node;
2064         }
2065 }  /* identity */
2066
2067 /**
2068  * Node follower is a (new) follower of leader, segregate Leader
2069  * out edges.
2070  */
2071 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2072         ir_node *l = leader->node;
2073         int     j, i, n = get_irn_n_outs(l);
2074
2075         DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2076         /* The leader edges must remain sorted, but follower edges can
2077            be unsorted. */
2078         for (i = leader->n_followers + 1; i <= n; ++i) {
2079                 if (l->out[i].use == follower) {
2080                         ir_def_use_edge t = l->out[i];
2081
2082                         for (j = i - 1; j >= leader->n_followers + 1; --j)
2083                                 l->out[j + 1] = l->out[j];
2084                         ++leader->n_followers;
2085                         l->out[leader->n_followers] = t;
2086
2087                         /* note: a node might be a n-fold follower, for instance
2088                          * if x = max(a,a), so no break here. */
2089                 }
2090         }
2091 }  /* segregate_def_use_chain_1 */
2092
2093 /**
2094  * Node follower is a (new) follower of leader, segregate Leader
2095  * out edges. If follower is a n-congruent Input identity, all follower
2096  * inputs congruent to follower are also leader.
2097  */
2098 static void segregate_def_use_chain(const ir_node *follower, node_t *leader) {
2099         ir_op *op = get_irn_op(follower);
2100
2101         if (op == op_Phi) {
2102                 /* n-Congruent Input Identity for Phi's */
2103                 int i;
2104                 ir_node *block = get_nodes_block(follower);
2105
2106                 DB((dbg, LEVEL_2, "n-Congruent follower %+F\n", follower));
2107                 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2108                         node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2109
2110                         /* beware: we are NOT followers of dead inputs */
2111                         if (pred_X->type.tv == tarval_reachable) {
2112                                 node_t *pred = get_irn_node(get_irn_n(follower, i));
2113
2114                                 if (pred->part == leader->part)
2115                                         segregate_def_use_chain_1(follower, pred);
2116                         }
2117                 }
2118         } else if (op == op_Mux || op == op_Max || op == op_Min) {
2119                 /* n-Congruent Input Identity */
2120                 int i;
2121
2122                 DB((dbg, LEVEL_2, "n-Congruent follower %+F\n", follower));
2123                 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2124                         node_t *pred = get_irn_node(get_irn_n(follower, i));
2125
2126                         if (pred->part == leader->part)
2127                                 segregate_def_use_chain_1(follower, pred);
2128                 }
2129         } else {
2130                 /* 1-Congruent Input Identity */
2131                 segregate_def_use_chain_1(follower, leader);
2132         }
2133 }  /* segregate_def_use_chain */
2134
2135 /**
2136  * Make all inputs to x from inside X no longer be F.def_use edges.
2137  */
2138 static void move_edges_to_leader(node_t *x) {
2139         ir_node     *irn = x->node;
2140         int         i, j, k;
2141
2142         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
2143                 node_t  *pred = get_irn_node(get_irn_n(irn, i));
2144                 ir_node *p;
2145                 int     n;
2146
2147                 p = pred->node;
2148                 n = get_irn_n_outs(p);
2149                 for (j = 1; j <= pred->n_followers; ++j) {
2150                         if (p->out[j].pos == i && p->out[j].use == irn) {
2151                                 /* found a follower edge to x, move it to the Leader */
2152                                 ir_def_use_edge edge = p->out[j];
2153
2154                                 /* remove this edge from the Follower set */
2155                                 p->out[j] = p->out[pred->n_followers];
2156                                 --pred->n_followers;
2157
2158                                 /* sort it into the leader set */
2159                                 for (k = pred->n_followers + 2; k <= n; ++k) {
2160                                         if (p->out[k].pos >= edge.pos)
2161                                                 break;
2162                                         p->out[k - 1] = p->out[k];
2163                                 }
2164                                 /* place the new edge here */
2165                                 p->out[k - 1] = edge;
2166
2167                                 /* edge found and moved */
2168                                 break;
2169                         }
2170                 }
2171         }
2172 }
2173
2174 /**
2175  * Propagate constant evaluation.
2176  *
2177  * @param env  the environment
2178  */
2179 static void propagate(environment_t *env) {
2180         partition_t    *X, *Y;
2181         node_t         *x;
2182         lattice_elem_t old_type;
2183         node_t         *fallen;
2184         unsigned       n_fallen, old_type_was_T_or_C;
2185         int            i;
2186
2187         while (env->cprop != NULL) {
2188                 void *oldopcode = NULL;
2189
2190                 /* remove the first partition X from cprop */
2191                 X           = env->cprop;
2192                 X->on_cprop = 0;
2193                 env->cprop  = X->cprop_next;
2194
2195                 old_type_was_T_or_C = X->type_is_T_or_C;
2196
2197                 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2198                 fallen   = NULL;
2199                 n_fallen = 0;
2200                 while (! list_empty(&X->cprop)) {
2201                         /* remove the first Node x from X.cprop */
2202                         x = list_entry(X->cprop.next, node_t, cprop_list);
2203                         list_del(&x->cprop_list);
2204                         x->on_cprop = 0;
2205
2206                         if (x->is_follower && identity(x) == x) {
2207                                 /* x will make the follower -> leader transition */
2208                                 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", x->node));
2209                                 if (oldopcode == NULL) {
2210                                         oldopcode = lambda_opcode(get_first_node(X), env);
2211                                 }
2212                                 if (oldopcode != lambda_opcode(x, env)) {
2213                                         /* different opcode -> x falls out of this partition */
2214                                         x->next      = fallen;
2215                                         x->on_fallen = 1;
2216                                         fallen       = x;
2217                                         ++n_fallen;
2218                                         DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2219                                 }
2220
2221                                 /* move x from X.Follower to X.Leader */
2222                                 list_del(&x->node_list);
2223                                 list_add_tail(&x->node_list, &X->Leader);
2224                                 x->is_follower = 0;
2225                                 X->n_leader++;
2226
2227                                 /* Make all inputs to x from inside X no longer be F.def_use edges */
2228                                 move_edges_to_leader(x);
2229                         }
2230
2231                         /* compute a new type for x */
2232                         old_type = x->type;
2233                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2234                         compute(x);
2235                         if (x->type.tv != old_type.tv) {
2236                                 verify_type(old_type, x->type);
2237                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2238
2239                                 if (x->on_fallen == 0) {
2240                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2241                                            not already on the list. */
2242                                         x->next      = fallen;
2243                                         x->on_fallen = 1;
2244                                         fallen       = x;
2245                                         ++n_fallen;
2246                                         DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2247                                 }
2248                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2249                                         ir_node *succ = get_irn_out(x->node, i);
2250                                         node_t  *y    = get_irn_node(succ);
2251
2252                                         /* Add y to y.partition.cprop. */
2253                                         add_node_to_cprop(y, env);
2254                                 }
2255                         }
2256                 }
2257
2258                 if (n_fallen > 0 && n_fallen != X->n_leader) {
2259                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2260                         Y = split(X, fallen, env);
2261                 } else {
2262                         Y = X;
2263                 }
2264                 /* remove the flags from the fallen list */
2265                 for (x = fallen; x != NULL; x = x->next)
2266                         x->on_fallen = 0;
2267
2268 #ifndef NO_FOLLOWER
2269                 if (old_type_was_T_or_C) {
2270                         node_t *y, *tmp;
2271
2272                         /* check if some nodes will make the leader -> follower transition */
2273                         list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2274                                 if (! is_con(y->type)) {
2275                                         node_t *eq_node = identity(y);
2276
2277                                         if (eq_node != y) {
2278                                                 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2279                                                 /* move to Follower */
2280                                                 y->is_follower = 1;
2281                                                 list_del(&y->node_list);
2282                                                 --Y->n_leader;
2283
2284                                                 list_add_tail(&y->node_list, &Y->Follower);
2285                                                 segregate_def_use_chain(y->node, eq_node);
2286                                         }
2287                                 }
2288                         }
2289                 }
2290 #endif
2291                 split_by(Y, env);
2292         }
2293 }  /* propagate */
2294
2295 /**
2296  * Get the leader for a given node from its congruence class.
2297  *
2298  * @param irn  the node
2299  */
2300 static ir_node *get_leader(node_t *node) {
2301         partition_t *part = node->part;
2302
2303         if (part->n_leader > 1 || node->is_follower) {
2304                 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2305
2306                 return get_first_node(part)->node;
2307         }
2308         return node->node;
2309 }  /* get_leader */
2310
2311 /**
2312  * Return non-zero if the control flow predecessor node pred
2313  * is the only reachable control flow exit of its block.
2314  *
2315  * @param pred  the control flow exit
2316  */
2317 static int can_exchange(ir_node *pred) {
2318         if (is_Start(pred))
2319                 return 0;
2320         else if (is_Jmp(pred))
2321                 return 1;
2322         else if (get_irn_mode(pred) == mode_T) {
2323                 int i, k;
2324
2325                 /* if the predecessor block has more than one
2326                 reachable outputs we cannot remove the block */
2327                 k = 0;
2328                 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2329                         ir_node *proj = get_irn_out(pred, i);
2330                         node_t  *node;
2331
2332                         /* skip non-control flow Proj's */
2333                         if (get_irn_mode(proj) != mode_X)
2334                                 continue;
2335
2336                         node = get_irn_node(proj);
2337                         if (node->type.tv == tarval_reachable) {
2338                                 if (++k > 1)
2339                                         return 0;
2340                         }
2341                 }
2342                 return 1;
2343         }
2344         return 0;
2345 }
2346
2347 /**
2348  * Block Post-Walker, apply the analysis results on control flow by
2349  * shortening Phi's and Block inputs.
2350  */
2351 static void apply_cf(ir_node *block, void *ctx) {
2352         environment_t *env = ctx;
2353         node_t        *node = get_irn_node(block);
2354         int           i, j, k, n;
2355         ir_node       **ins, **in_X;
2356         ir_node       *phi, *next;
2357
2358         if (block == get_irg_end_block(current_ir_graph) ||
2359             block == get_irg_start_block(current_ir_graph)) {
2360                 /* the EndBlock is always reachable even if the analysis
2361                    finds out the opposite :-) */
2362                 return;
2363         }
2364         if (node->type.tv == tarval_unreachable) {
2365                 /* mark dead blocks */
2366                 set_Block_dead(block);
2367                 return;
2368         }
2369
2370         n = get_Block_n_cfgpreds(block);
2371
2372         if (n == 1) {
2373                 /* only one predecessor combine */
2374                 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2375
2376                 if (can_exchange(pred)) {
2377                         exchange(block, get_nodes_block(pred));
2378                         env->modified = 1;
2379                 }
2380                 return;
2381         }
2382
2383         NEW_ARR_A(ir_node *, in_X, n);
2384         k = 0;
2385         for (i = 0; i < n; ++i) {
2386                 ir_node *pred = get_Block_cfgpred(block, i);
2387                 node_t  *node = get_irn_node(pred);
2388
2389                 if (node->type.tv == tarval_reachable) {
2390                         in_X[k++] = pred;
2391                 }
2392         }
2393         if (k >= n)
2394                 return;
2395
2396         NEW_ARR_A(ir_node *, ins, n);
2397         for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2398                 node_t *node = get_irn_node(phi);
2399
2400                 next = get_Phi_next(phi);
2401                 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2402                         /* this Phi is replaced by a constant */
2403                         tarval  *tv = node->type.tv;
2404                         ir_node *c  = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2405
2406                         set_irn_node(c, node);
2407                         node->node = c;
2408                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2409                         exchange(phi, c);
2410                         env->modified = 1;
2411                 } else {
2412                         j = 0;
2413                         for (i = 0; i < n; ++i) {
2414                                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2415
2416                                 if (pred->type.tv == tarval_reachable) {
2417                                         ins[j++] = get_Phi_pred(phi, i);
2418                                 }
2419                         }
2420                         if (j <= 1) {
2421                                 /* this Phi is replaced by a single predecessor */
2422                                 ir_node *s = ins[0];
2423
2424                                 node->node = s;
2425                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2426                                 exchange(phi, s);
2427                                 env->modified = 1;
2428                         } else {
2429                                 set_irn_in(phi, j, ins);
2430                                 env->modified = 1;
2431                         }
2432                 }
2433         }
2434
2435         if (k <= 1) {
2436                 /* this Block has only one live predecessor */
2437                 ir_node *pred = skip_Proj(in_X[0]);
2438
2439                 if (can_exchange(pred)) {
2440                         exchange(block, get_nodes_block(pred));
2441                         env->modified = 1;
2442                 }
2443         } else {
2444                 set_irn_in(block, k, in_X);
2445                 env->modified = 1;
2446         }
2447 }
2448
2449 /**
2450  * Post-Walker, apply the analysis results;
2451  */
2452 static void apply_result(ir_node *irn, void *ctx) {
2453         environment_t *env = ctx;
2454         node_t        *node = get_irn_node(irn);
2455
2456         if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2457                 /* blocks already handled, do not touch the End node */
2458         } else {
2459                 node_t *block = get_irn_node(get_nodes_block(irn));
2460
2461                 if (block->type.tv == tarval_unreachable) {
2462                         ir_node *bad = get_irg_bad(current_ir_graph);
2463
2464                         /* here, bad might already have a node, but this can be safely ignored
2465                            as long as bad has at least ONE valid node */
2466                         set_irn_node(bad, node);
2467                         node->node = bad;
2468                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2469                         exchange(irn, bad);
2470                         env->modified = 1;
2471                 }
2472                 else if (node->type.tv == tarval_unreachable) {
2473                         ir_node *bad = get_irg_bad(current_ir_graph);
2474
2475                         /* see comment above */
2476                         set_irn_node(bad, node);
2477                         node->node = bad;
2478                         DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2479                         exchange(irn, bad);
2480                         env->modified = 1;
2481                 }
2482                 else if (get_irn_mode(irn) == mode_X) {
2483                         if (is_Proj(irn)) {
2484                                 /* leave or Jmp */
2485                                 ir_node *cond = get_Proj_pred(irn);
2486
2487                                 if (is_Cond(cond)) {
2488                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
2489
2490                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2491                                                 /* Cond selector is a constant, make a Jmp */
2492                                                 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2493                                                 set_irn_node(jmp, node);
2494                                                 node->node = jmp;
2495                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2496                                                 exchange(irn, jmp);
2497                                                 env->modified = 1;
2498                                         }
2499                                 }
2500                         }
2501                 } else {
2502                         /* normal data node */
2503                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2504                                 tarval *tv = node->type.tv;
2505
2506                                 /*
2507                                  * Beware: never replace mode_T nodes by constants. Currently we must mark
2508                                  * mode_T nodes with constants, but do NOT replace them.
2509                                  */
2510                                 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2511                                         /* can be replaced by a constant */
2512                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2513                                         set_irn_node(c, node);
2514                                         node->node = c;
2515                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2516                                         exchange(irn, c);
2517                                         env->modified = 1;
2518                                 }
2519                         } else if (is_entity(node->type.sym.entity_p)) {
2520                                 if (! is_SymConst(irn)) {
2521                                         /* can be replaced by a Symconst */
2522                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2523                                         set_irn_node(symc, node);
2524                                         node->node = symc;
2525
2526                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2527                                         exchange(irn, symc);
2528                                         env->modified = 1;
2529                                 }
2530                         } else {
2531                                 ir_node *leader = get_leader(node);
2532
2533                                 if (leader != irn) {
2534                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2535                                         exchange(irn, leader);
2536                                         env->modified = 1;
2537                                 }
2538                         }
2539                 }
2540         }
2541 }  /* apply_result */
2542
2543 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2544
2545 /**
2546  * sets the generic functions to compute.
2547  */
2548 static void set_compute_functions(void) {
2549         int i;
2550
2551         /* set the default compute function */
2552         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2553                 ir_op *op = get_irp_opcode(i);
2554                 op->ops.generic = (op_func)default_compute;
2555         }
2556
2557         /* set specific functions */
2558         SET(Block);
2559         SET(Unknown);
2560         SET(Bad);
2561         SET(Jmp);
2562         SET(Phi);
2563         SET(Add);
2564         SET(Sub);
2565         SET(SymConst);
2566         SET(Cmp);
2567         SET(Proj);
2568         SET(Confirm);
2569         SET(End);
2570
2571         if (op_Max != NULL)
2572                 SET(Max);
2573         if (op_Min != NULL)
2574                 SET(Min);
2575
2576 }  /* set_compute_functions */
2577
2578 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2579         ir_node *irn = local != NULL ? local : n;
2580         node_t *node = get_irn_node(irn);
2581
2582         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2583         return 1;
2584 }
2585
2586 void combo(ir_graph *irg) {
2587         environment_t env;
2588         ir_node       *initial_bl;
2589         node_t        *start;
2590         ir_graph      *rem = current_ir_graph;
2591
2592         current_ir_graph = irg;
2593
2594         /* register a debug mask */
2595         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2596         //firm_dbg_set_mask(dbg, SET_LEVEL_3);
2597
2598         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2599
2600         obstack_init(&env.obst);
2601         env.worklist       = NULL;
2602         env.cprop          = NULL;
2603         env.touched        = NULL;
2604         env.initial        = NULL;
2605 #ifdef DEBUG_libfirm
2606         env.dbg_list       = NULL;
2607 #endif
2608         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
2609         env.type2id_map    = pmap_create();
2610         env.end_idx        = get_opt_global_cse() ? 0 : -1;
2611         env.lambda_input   = 0;
2612         env.modified       = 0;
2613
2614         assure_irg_outs(irg);
2615
2616         /* we have our own value_of function */
2617         set_value_of_func(get_node_tarval);
2618
2619         set_compute_functions();
2620         DEBUG_ONLY(part_nr = 0);
2621
2622         /* create the initial partition and place it on the work list */
2623         env.initial = new_partition(&env);
2624         add_to_worklist(env.initial, &env);
2625         irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2626
2627         /* all nodes on the initial partition have type Top */
2628         env.initial->type_is_T_or_C = 1;
2629
2630         /* Place the START Node's partition on cprop.
2631            Place the START Node on its local worklist. */
2632         initial_bl = get_irg_start_block(irg);
2633         start      = get_irn_node(initial_bl);
2634         add_node_to_cprop(start, &env);
2635
2636         do {
2637                 propagate(&env);
2638                 if (env.worklist != NULL)
2639                         cause_splits(&env);
2640         } while (env.cprop != NULL || env.worklist != NULL);
2641
2642         dump_all_partitions(&env);
2643
2644 #if 0
2645         set_dump_node_vcgattr_hook(dump_partition_hook);
2646         dump_ir_block_graph(irg, "-partition");
2647         set_dump_node_vcgattr_hook(NULL);
2648 #else
2649         (void)dump_partition_hook;
2650 #endif
2651
2652         /* apply the result */
2653         irg_block_walk_graph(irg, NULL, apply_cf, &env);
2654         irg_walk_graph(irg, NULL, apply_result, &env);
2655
2656         if (env.modified) {
2657                 /* control flow might changed */
2658                 set_irg_outs_inconsistent(irg);
2659                 set_irg_extblk_inconsistent(irg);
2660                 set_irg_doms_inconsistent(irg);
2661                 set_irg_loopinfo_inconsistent(irg);
2662         }
2663
2664         pmap_destroy(env.type2id_map);
2665         del_set(env.opcode2id_map);
2666         obstack_free(&env.obst, NULL);
2667
2668         /* restore value_of() default behavior */
2669         set_value_of_func(NULL);
2670         current_ir_graph = rem;
2671 }  /* combo */