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