add dumping hook
[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 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, 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 "irflag.h"
38 #include "list.h"
39 #include "array.h"
40 #include "set.h"
41 #include "pmap.h"
42 #include "obstack.h"
43 #include "irgraph_t.h"
44 #include "irnode_t.h"
45 #include "iropt_t.h"
46 #include "irgwalk.h"
47 #include "irop.h"
48 #include "irouts.h"
49 #include "irgmod.h"
50 #include "debug.h"
51
52 /* we need the tarval_R and tarval_U */
53 #define tarval_R  tarval_top
54 #define tarval_U  tarval_bottom
55
56 typedef struct node_t            node_t;
57 typedef struct partition_t       partition_t;
58 typedef struct opcode_key_t      opcode_key_t;
59 typedef struct opcode_entry_t    opcode_entry_t;
60 typedef struct listmap_entry_t   listmap_entry_t;
61
62 /** The type of the compute function. */
63 typedef void (*compute_func)(node_t *node);
64
65 /**
66  * An opcode map key.
67  */
68 struct opcode_key_t {
69         ir_opcode   code;   /**< The Firm opcode. */
70         ir_mode     *mode;  /**< The mode of all nodes in the partition. */
71 };
72
73 /**
74  * An entry in the opcode map.
75  */
76 struct opcode_entry_t {
77         opcode_key_t key;    /**< The key. */
78         partition_t  *part;  /**< The associated partition. */
79 };
80
81 /**
82  * An entry in the list_map.
83  */
84 struct listmap_entry_t {
85         void            *id;    /**< The id. */
86         node_t          *list;  /**< The associated list for this id. */
87         listmap_entry_t *next;  /**< Link to the next entry in the map. */
88 };
89
90 /** We must map id's to lists. */
91 typedef struct listmap_t {
92         set             *map;    /**< Map id's to listmap_entry_t's */
93         listmap_entry_t *values; /**< List of all values in the map. */
94 } listmap_t;
95
96 /**
97  * A lattice element. Because we handle constants and symbolic constants different, we
98  * have to use this union.
99  */
100 typedef union {
101         tarval          *tv;
102         symconst_symbol sym;
103 } lattice_elem_t;
104
105 /**
106  * A node.
107  */
108 struct node_t {
109         ir_node        *node;         /**< The IR-node itself. */
110         list_head      node_list;     /**< Double-linked list of entries. */
111         partition_t    *part;         /**< points to the partition this node belongs to */
112         node_t         *cprop_next;   /**< Next node on partition.cprop list. */
113         node_t         *next;         /**< Next node on local list (partition.touched, fallen). */
114         lattice_elem_t type;         /**< The associated lattice element "type". */
115         unsigned       on_touched:1;  /**< Set, if this node is on the partition.touched set. */
116         unsigned       on_cprop:1;    /**< Set, if this node is on the partition.cprop list. */
117 };
118
119 /**
120  * A partition containing congruent nodes.
121  */
122 struct partition_t {
123         list_head         entries;       /**< The head of partition node list. */
124         node_t            *cprop;        /**< The partition.cprop list. */
125         partition_t       *wl_next;      /**< Next entry in the work list if any. */
126         partition_t       *touched_next; /**< Points to the next partition in the touched set. */
127         partition_t       *cprop_next;   /**< Points to the next partition in the cprop list. */
128         node_t            *touched;      /**< The partition.touched set of this partition. */
129         unsigned          n_nodes;       /**< Number of entries in this partition. */
130         unsigned          n_touched;     /**< Number of entries in the partition.touched. */
131         int               n_inputs;      /**< Maximum number of inputs of all entries. */
132         unsigned          on_worklist:1; /**< Set, if this partition is in the work list. */
133         unsigned          on_touched:1;  /**< Set, if this partition is on the touched set. */
134         unsigned          on_cprop:1;    /**< Set, if this partition is on the cprop list. */
135 #ifdef DEBUG_libfirm
136         partition_t       *dbg_next;     /**< Link all partitions for debugging */
137         unsigned          nr;            /**< A unique number for (what-)mapping, >0. */
138 #endif
139 };
140
141 typedef struct environment_t {
142         struct obstack  obst;           /**< obstack to allocate data structures. */
143         partition_t     *worklist;      /**< The work list. */
144         partition_t     *cprop;         /**< The constant propagation list. */
145         partition_t     *touched;       /**< the touched set. */
146         partition_t     *TOP;           /**< The TOP partition. */
147 #ifdef DEBUG_libfirm
148         partition_t     *dbg_list;      /**< List of all partitions. */
149 #endif
150         set             *opcode_map;    /**< The initial opcode->partition map. */
151         set             *opcode2id_map; /**< The opcodeMode->id map. */
152         pmap            *type2id_map;   /**< The type->id map. */
153         int             end_idx;        /**< -1 for local and 0 for global congruences. */
154         int             lambda_input;   /**< Captured argument for lambda_partition(). */
155 } environment_t;
156
157 /** Type of the what function. */
158 typedef void *(*what_func)(const node_t *node, environment_t *env);
159
160 #define get_irn_node(irn)         ((node_t *)get_irn_link(irn))
161 #define set_irn_node(irn, node)   set_irn_link(irn, node)
162
163 /** The debug module handle. */
164 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
165
166 /** Next partition number. */
167 DEBUG_ONLY(static unsigned part_nr = 0);
168
169 #ifdef DEBUG_libfirm
170 /**
171  * Dump partition to output.
172  */
173 static void dump_partition(const char *msg, partition_t *part) {
174         node_t *node;
175         int    first = 1;
176
177         DB((dbg, LEVEL_2, "%s part%u (%u) {\n  ", msg, part->nr, part->n_nodes));
178         list_for_each_entry(node_t, node, &part->entries, node_list) {
179                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
180                 first = 0;
181         }
182         DB((dbg, LEVEL_2, "\n}\n"));
183 }
184
185 /**
186  * Dump all partitions.
187  */
188 static void dump_all_partitions(environment_t *env) {
189         partition_t *P;
190
191         DB((dbg, LEVEL_2, "All partitions\n===============\n"));
192         for (P = env->dbg_list; P != NULL; P = P->dbg_next)
193                 dump_partition("", P);
194 }
195
196 #else
197 #define dump_partition(msg, part)
198 #define dump_all_partitions(env)
199 #endif
200
201 /**
202  * Compare two pointer values of a listmap.
203  */
204 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
205         const listmap_entry_t *e1 = elt;
206         const listmap_entry_t *e2 = key;
207
208         return e1->id != e2->id;
209 }  /* listmap_cmp_ptr */
210
211 /**
212  * Initializes a listmap.
213  *
214  * @param map  the listmap
215  */
216 static void listmap_init(listmap_t *map) {
217         map->map    = new_set(listmap_cmp_ptr, 16);
218         map->values = NULL;
219 }  /* listmap_init */
220
221 /**
222  * Terminates a listmap.
223  *
224  * @param map  the listmap
225  */
226 static void listmap_term(listmap_t *map) {
227         del_set(map->map);
228 }  /* listmap_term */
229
230 /**
231  * Return the associated listmap entry for a given id.
232  *
233  * @param map  the listmap
234  * @param id   the id to search for
235  *
236  * @return the asociated listmap entry for the given id
237  */
238 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
239         listmap_entry_t key, *entry;
240
241         key.id   = id;
242         key.list = NULL;
243         key.next = NULL;
244         entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
245
246         if (entry->list == NULL) {
247                 /* a new entry, put into the list */
248                 entry->next = map->values;
249                 map->values = entry;
250         }
251         return entry;
252 }  /* listmap_find */
253
254 /**
255  * Calculate the hash value for an opcode map entry.
256  *
257  * @param entry  an opcode map entry
258  *
259  * @return a hash value for the given opcode map entry
260  */
261 static unsigned opcode_hash(const opcode_key_t *entry) {
262         return (entry->mode - (ir_mode *)0) * 9 + entry->code;
263 }  /* opcode_hash */
264
265 /**
266  * Compare two entries in the opcode map.
267  */
268 static int cmp_opcode(const void *elt, const void *key, size_t size) {
269         const opcode_key_t *o1 = elt;
270         const opcode_key_t *o2 = key;
271
272         return o1->code != o2->code || o1->mode != o2->mode;
273 }  /* cmp_opcode */
274
275 /**
276  * Return the type of a node.
277  *
278  * @param irn  an IR-node
279  *
280  * @return the associated type of this node
281  */
282 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
283         return get_irn_node(irn)->type;
284 }  /* get_node_type */
285
286 /**
287  * Return the tarval of a node.
288  *
289  * @param irn  an IR-node
290  *
291  * @return the associated type of this node
292  */
293 static INLINE tarval *get_node_tarval(const ir_node *irn) {
294         lattice_elem_t type = get_node_type(irn);
295
296         if (get_kind(type.tv) == k_tarval)
297                 return type.tv;
298         return tarval_bottom;
299 }  /* get_node_type */
300
301
302 /**
303  * Create a new empty partition.
304  *
305  * @param env   the environment
306  *
307  * @return a newly allocated partition
308  */
309 static INLINE partition_t *new_partition(environment_t *env) {
310         partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
311
312         INIT_LIST_HEAD(&part->entries);
313         part->cprop        = NULL;
314         part->wl_next      = env->worklist;
315         part->touched_next = NULL;
316         part->cprop_next   = NULL;
317         part->touched      = NULL;
318         part->n_nodes      = 0;
319         part->n_touched    = 0;
320         part->n_inputs     = 0;
321         part->on_worklist  = 0;
322         part->on_touched   = 0;
323         part->on_cprop     = 0;
324 #ifdef DEBUG_libfirm
325         part->dbg_next     = env->dbg_list;
326         env->dbg_list      = part;
327         part->nr           = part_nr++;
328 #endif
329
330         return part;
331 }  /* new_partition */
332
333 /**
334  * Get the partition for a given IR-node.
335  *
336  * @param irn   the IR-node
337  * @param env   the environment
338  *
339  * @return the partition where irn lies
340  */
341 static INLINE partition_t *get_partition_for_irn(const ir_node *irn, environment_t *env) {
342         opcode_entry_t key, *entry;
343         unsigned       hash;
344
345         key.key.code = get_irn_opcode(irn);
346         key.key.mode = get_irn_mode(irn);
347         hash         = opcode_hash(&key.key);
348
349         entry = set_find(env->opcode_map, &key, sizeof(key), hash);
350         if (entry == NULL) {
351                 /* create a new partition and place it on the wait queue */
352                 partition_t *part = new_partition(env);
353
354                 part->on_worklist = 1;
355                 env->worklist     = part;
356
357                 key.part = part;
358                 set_insert(env->opcode_map, &key, sizeof(key), hash);
359                 entry = &key;
360         }
361         return entry->part;
362 }  /* get_partition_for_irn */
363
364 /**
365  * Return the type of a partition (assuming partition is non-empty and
366  * all elements have the same type).
367  *
368  * @param X  a partition
369  *
370  * @return the type of the first element of the partition
371  */
372 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
373         const node_t *first = list_entry(X->entries.next, node_t, node_list);
374         return first->type;
375 }  /* get_partition_type */
376
377 /**
378  * Creates a partition node for the given IR-node and place it
379  * into the given partition.
380  *
381  * @param irn   an IR-node
382  * @param part  a partition to place the node in
383  * @param env   the environment
384  */
385 static void create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
386         /* create a partition node and place it in the partition */
387         node_t *node = obstack_alloc(&env->obst, sizeof(*node));
388
389         INIT_LIST_HEAD(&node->node_list);
390         node->node         = irn;
391         node->part         = part;
392         node->cprop_next   = NULL;
393         node->next         = NULL;
394         node->type.tv      = tarval_bottom; /* == tarval_U */
395         node->on_touched   = 0;
396         node->on_cprop     = 0;
397         set_irn_node(irn, node);
398
399         list_add_tail(&node->node_list, &part->entries);
400         ++part->n_nodes;
401
402         DB((dbg, LEVEL_2, "Placing %+F in partition %u\n", irn, part->nr));
403 }  /* create_partition_node */
404
405 /**
406  * Walker, initialize all Nodes' type to U or top and place
407  * all nodes into the TOP partition.
408  */
409 static void create_initial_partitions(ir_node *irn, void *ctx) {
410         environment_t *env  = ctx;
411         partition_t   *part = env->TOP;
412         int           arity;
413
414         create_partition_node(irn, part, env);
415         arity = get_irn_arity(irn);
416         if (arity > part->n_inputs)
417                 part->n_inputs = arity;
418 }  /* create_initial_partitions */
419
420 /**
421  * Add a partition to the touched set if not already there.
422  *
423  * @param part  the partition
424  * @param env   the environment
425  */
426 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
427         if (part->on_touched == 0) {
428                 part->touched_next = env->touched;
429                 env->touched       = part;
430                 part->on_touched   = 1;
431         }
432 }  /* add_to_touched */
433
434 /**
435  * Add a node to the entry.partition.touched set if not already there.
436  *
437  * @param y  a node
438  */
439 static INLINE void add_to_partition_touched(node_t *y) {
440         if (y->on_touched == 0) {
441                 partition_t *part = y->part;
442
443                 y->next       = part->touched;
444                 part->touched = y;
445                 y->on_touched = 1;
446                 ++part->n_touched;
447         }
448 }  /* add_to_partition_touched */
449
450 /**
451  * Update the worklist: If Z is on worklist then add Z' to worklist.
452  * Else add the smaller of Z and Z' to worklist.
453  *
454  * @param Z        the Z partition
455  * @param Z_prime  the Z' partition, a previous part of Z
456  * @param env      the environment
457  */
458 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
459         if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
460                 Z_prime->on_worklist = 1;
461                 Z_prime->wl_next     = env->worklist;
462                 env->worklist        = Z_prime;
463         } else {
464                 Z->on_worklist = 1;
465                 Z->wl_next     = env->worklist;
466                 env->worklist  = Z;
467         }
468 }  /* update_worklist */
469
470 /**
471  * Split a partition by a local list.
472  *
473  * @param Z    the Z partition to split
474  * @param g    a (non-empty) node list
475  * @param env  the environment
476  *
477  * @return  a new partition containing the nodes of g
478  */
479 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
480         partition_t *Z_prime;
481         node_t      *node;
482         unsigned    n = 0;
483         int         n_inputs;
484
485         dump_partition("Splitting ", Z);
486
487         assert(g != NULL);
488
489         /* Remove g from Z. */
490         for (node = g; node != NULL; node = node->next) {
491                 list_del(&node->node_list);
492                 ++n;
493         }
494         assert(n < Z->n_nodes);
495         Z->n_nodes -= n;
496
497         /* Move g to a new partition, Z\92. */
498         Z_prime = new_partition(env);
499         n_inputs = 0;
500         for (node = g; node != NULL; node = node->next) {
501                 int arity = get_irn_arity(node->node);
502                 list_add(&node->node_list, &Z_prime->entries);
503                 node->part = Z_prime;
504                 if (arity > n_inputs)
505                         n_inputs = arity;
506         }
507         Z_prime->n_inputs = n_inputs;
508         Z_prime->n_nodes  = n;
509
510         update_worklist(Z, Z_prime, env);
511
512         dump_partition("Now ", Z);
513         dump_partition("Created new ", Z_prime);
514         return Z_prime;
515 }  /* split */
516
517 /**
518  * Returns non-zero if the i'th input of a Phi node is live.
519  *
520  * @param phi  a Phi-node
521  * @param i    an input number
522  *
523  * @return non-zero if the i'th input of the given Phi node is live
524  */
525 static int is_live_input(ir_node *phi, int i) {
526         if (i >= 0) {
527                 ir_node        *block = get_nodes_block(phi);
528                 ir_node        *pred  = get_Block_cfgpred(block, i);
529                 lattice_elem_t type   = get_node_type(pred);
530
531                 return type.tv != tarval_U;
532         }
533         /* else it's the control input, always live */
534         return 1;
535 }  /* is_live_input */
536
537 /**
538  * Split the partitions if caused by the first entry on the worklist.
539  *
540  * @param env  the environment
541  */
542 static void cause_splits(environment_t *env) {
543         partition_t *X, *Y, *Z;
544         node_t      *x, *y, *e;
545         int         i, end_idx;
546
547         /* remove the first partition from the worklist */
548         X = env->worklist;
549         env->worklist  = X->wl_next;
550         X->on_worklist = 0;
551
552         dump_partition("Cause_split: ", X);
553         end_idx = env->end_idx;
554         for (i = X->n_inputs - 1; i >= -1; --i) {
555                 /* empty the touched set: already done, just clear the list */
556                 env->touched = NULL;
557
558                 list_for_each_entry(node_t, x, &X->entries, node_list) {
559                         /* ignore the "control input" for non-pinned nodes
560                            if we are running in GCSE mode */
561                         if (i < end_idx && get_irn_pinned(x->node) != op_pin_state_pinned)
562                                 continue;
563
564                         /* non-existing input */
565                         if (i >= get_irn_arity(x->node))
566                                 continue;
567
568                         y = get_irn_node(get_irn_n(x->node, i));
569                         Y = y->part;
570
571                         /* Partitions of constants should not be split simply because their Nodes have unequal
572                            functions or incongruent inputs. */
573                         if (get_partition_type(Y).tv != tarval_bottom &&
574                                 (! is_Phi(x->node) || is_live_input(x->node, i))) {
575                                 add_to_touched(Y, env);
576                                 add_to_partition_touched(y);
577                         }
578                 }
579
580                 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
581                         /* remove it from the touched set */
582                         Z->on_touched = 0;
583
584                         if (Z->n_nodes != Z->n_touched) {
585                                 split(Z, Z->touched, env);
586                         }
587                         /* Empty local Z.touched. */
588                         for (e = Z->touched; e != NULL; e = e->next) {
589                                 e->on_touched = 0;
590                         }
591                         Z->touched   = NULL;
592                         Z->n_touched = 0;
593                 }
594         }
595 }  /* cause_splits */
596
597 /**
598  * Implements split_by_what(): Split a partition by characteristics given
599  * by the what function.
600  *
601  * @param X     the partition to split
602  * @param What  a function returning an Id for every node of the partition X
603  * @param P     an flexible array to store the result partitions or NULL
604  * @param env   the environment
605  *
606  * @return if P != NULL P will be filled with the resulting partitions and returned
607  */
608 static partition_t **split_by_what(partition_t *X, what_func What,
609                                                                   partition_t **P, environment_t *env) {
610         node_t          *x, *S;
611         listmap_t       map;
612         listmap_entry_t *iter;
613         partition_t     *R;
614
615         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
616         listmap_init(&map);
617         list_for_each_entry(node_t, x, &X->entries, node_list) {
618                 void            *id = What(x, env);
619                 listmap_entry_t *entry;
620
621                 if (id == NULL) {
622                         /* input not allowed, ignore */
623                         continue;
624                 }
625                 /* Add x to map[What(x)]. */
626                 entry = listmap_find(&map, id);
627                 x->next     = entry->list;
628                 entry->list = x;
629         }
630         /* Let P be a set of Partitions. */
631
632         /* for all sets S except one in the range of map do */
633         for (iter = map.values; iter != NULL; iter = iter->next) {
634                 if (iter->next == NULL) {
635                         /* this is the last entry, ignore */
636                         break;
637                 }
638                 S = iter->list;
639
640                 /* Add SPLIT( X, S ) to P. */
641                 R = split(X, S, env);
642                 if (P != NULL) {
643                         ARR_APP1(partition_t *, P, R);
644                 }
645         }
646         /* Add X to P. */
647         if (P != NULL) {
648                 ARR_APP1(partition_t *, P, X);
649         }
650
651         listmap_term(&map);
652         return P;
653 }  /* split_by_what */
654
655 /** lambda n.(n.type) */
656 static void *lambda_type(const node_t *node, environment_t *env) {
657         (void)env;
658         return node->type.tv;
659 }  /* lambda_type */
660
661 /** lambda n.(n.opcode) */
662 static void *lambda_opcode(const node_t *node, environment_t *env) {
663         opcode_key_t key, *entry;
664
665         key.code = get_irn_opcode(node->node);
666         key.mode = get_irn_mode(node->node);
667         entry = set_insert(env->opcode2id_map, &key, sizeof(&key), opcode_hash(&key));
668         return entry;
669 }  /* lambda_opcode */
670
671 /** lambda n.(n[i].partition) */
672 static void *lambda_partition(const node_t *node, environment_t *env) {
673         ir_node *pred;
674         node_t  *p;
675         int     i = env->lambda_input;
676
677         if (i >= get_irn_arity(node->node)) {
678                 /* we are outside the allowed range */
679                 return NULL;
680         }
681
682         /* ignore the "control input" for non-pinned nodes
683            if we are running in GCSE mode */
684         if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
685                 return NULL;
686
687         pred = get_irn_n(node->node, i);
688         p    = get_irn_node(pred);
689
690         return p->part;
691 }  /* lambda_partition */
692
693 /**
694  * Implements split_by().
695  *
696  * @param X    the partition to split
697  * @param env  the environment
698  */
699 static void split_by(partition_t *X, environment_t *env) {
700         partition_t **P = NEW_ARR_F(partition_t *, 0);
701         int         i, j, k;
702
703         P = split_by_what(X, lambda_type, P, env);
704         for (i = ARR_LEN(P) - 1; i >= 0; --i) {
705                 partition_t *Y = P[i];
706
707                 /* we do not want split the TOP or constant partitions */
708                 if (get_partition_type(Y).tv == tarval_bottom) {
709                         partition_t **Q = NEW_ARR_F(partition_t *, 0);
710
711                         Q = split_by_what(Y, lambda_opcode, Q, env);
712
713                         for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
714                                 partition_t *Z = Q[j];
715
716                                 for (k = Z->n_inputs - 1; k >= -1; --k) {
717                                         env->lambda_input = k;
718                                         split_by_what(Z, lambda_partition, NULL, env);
719                                 }
720                         }
721                         DEL_ARR_F(Q);
722                 }
723         }
724         DEL_ARR_F(P);
725 }  /* split_by */
726
727 /**
728  * (Re-)compute the type for a given node.
729  *
730  * @param node  the node
731  */
732 static void default_compute(node_t *node) {
733         int     i;
734         ir_node *irn = node->node;
735         ir_mode *mode;
736
737         if (get_irn_pinned(irn) == op_pin_state_pinned) {
738                 node_t *block = get_irn_node(get_nodes_block(irn));
739
740                 if (block->type.tv == tarval_U) {
741                         node->type.tv = tarval_top;
742                         return;
743                 }
744         }
745         mode = get_irn_mode(irn);
746         if (! mode_is_data(mode))
747                 return;
748
749         /* if any of the data inputs have type top, the result is type top */
750         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
751                 ir_node *pred = get_irn_n(irn, i);
752                 node_t  *p    = get_irn_node(pred);
753
754                 if (p->type.tv == tarval_top) {
755                         node->type.tv = tarval_top;
756                         return;
757                 }
758         }
759         node->type.tv = computed_value(irn);
760 }  /* default_compute */
761
762 /**
763  * (Re-)compute the type for a Block node.
764  *
765  * @param node  the node
766  */
767 static void compute_Block(node_t *node) {
768         int     i;
769         ir_node *block = node->node;
770
771         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
772                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
773
774                 if (pred->type.tv == tarval_R) {
775                         /* A block is reachable, if at least of predecessor is reachable. */
776                         node->type.tv = tarval_R;
777                         return;
778                 }
779         }
780         node->type.tv = tarval_U;
781 }  /* compute_Block */
782
783 /**
784  * (Re-)compute the type for a Jmp node.
785  *
786  * @param node  the node
787  */
788 static void compute_Jmp(node_t *node) {
789         node_t *block = get_irn_node(get_nodes_block(node->node));
790
791         node->type = block->type;
792 }  /* compute_Jmp */
793
794 /**
795  * (Re-)compute the type for the End node.
796  *
797  * @param node  the node
798  */
799 static void compute_End(node_t *node) {
800         /* the End node is NOT dead of course */
801         node->type.tv = tarval_R;
802 }
803
804 /**
805  * (Re-)compute the type for a SymConst node.
806  *
807  * @param node  the node
808  */
809 static void compute_SymConst(node_t *node) {
810         ir_node *irn = node->node;
811         node_t  *block = get_irn_node(get_nodes_block(irn));
812
813         if (block->type.tv == tarval_U) {
814                 node->type.tv = tarval_top;
815                 return;
816         }
817         switch (get_SymConst_kind(irn)) {
818         case symconst_addr_ent:
819         case symconst_addr_name:
820                 node->type.sym = get_SymConst_symbol(irn);
821                 break;
822         default:
823                 node->type.tv = computed_value(irn);
824         }
825 }  /* compute_SymConst */
826
827 /**
828  * (Re-)compute the type for a Phi node.
829  *
830  * @param node  the node
831  */
832 static void compute_Phi(node_t *node) {
833         int            i;
834         ir_node        *phi = node->node;
835         lattice_elem_t type;
836
837         /* if a Phi is in a unreachable block, its type is TOP */
838         node_t *block = get_irn_node(get_nodes_block(phi));
839
840         if (block->type.tv == tarval_U) {
841                 node->type.tv = tarval_top;
842                 return;
843         }
844
845         /* if any of the data inputs have type top, the result is type top */
846         type.tv = tarval_top;
847         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
848                 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
849
850                 if (pred->type.tv == tarval_top) {
851                         /* ignore TOP inputs */
852                         continue;
853                 }
854                 if (pred->type.tv == tarval_bottom) {
855                         node->type.tv = tarval_bottom;
856                         return;
857                 } else if (type.tv == tarval_top) {
858                         /* first constant found */
859                         type = pred->type;
860                 } else if (type.tv == pred->type.tv) {
861                         /* same constant, continue */
862                         continue;
863                 } else {
864                         /* different constants or tarval_bottom */
865                         node->type.tv = tarval_bottom;
866                         return;
867                 }
868         }
869         node->type = type;
870 }  /* compute_Phi */
871
872 /**
873  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
874  *
875  * @param node  the node
876  */
877 static void compute_Sub(node_t *node) {
878         ir_node        *sub = node->node;
879         node_t         *l   = get_irn_node(get_Sub_left(sub));
880         node_t         *r   = get_irn_node(get_Sub_right(sub));
881         lattice_elem_t a    = l->type;
882         lattice_elem_t b    = r->type;
883         node_t         *block = get_irn_node(get_nodes_block(sub));
884
885         if (block->type.tv == tarval_U) {
886                 node->type.tv = tarval_top;
887                 return;
888         }
889
890         if (a.tv == tarval_top || b.tv == tarval_top) {
891                 node->type.tv = tarval_top;
892         } else if (r->part == l->part) {
893                 ir_mode *mode = get_irn_mode(sub);
894                 node->type.tv = get_mode_null(mode);
895         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
896                 node->type.tv = tarval_bottom;
897         } else {
898                 if (get_kind(a.tv) == k_tarval && get_kind(b.tv)== k_tarval)
899                         node->type.tv = tarval_sub(a.tv, b.tv);
900                 else
901                         node->type.tv = tarval_bottom;
902         }
903 }  /* compute_Sub */
904
905 /**
906  * (Re-)compute the type for a Proj(Cmp).
907  *
908  * @param node  the node
909  */
910 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
911         ir_node        *proj = node->node;
912         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
913         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
914         lattice_elem_t a     = l->type;
915         lattice_elem_t b     = r->type;
916         pn_Cmp         pnc   = get_Proj_proj(proj);
917
918         /*
919          * BEWARE: a == a is NOT always True for floating Point values, as
920          * NaN != NaN is defined, so we must check this here.
921          */
922         if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt ||  pnc == pn_Cmp_Gt) {
923                 if (a.tv == tarval_top || b.tv == tarval_top) {
924                         node->type.tv = tarval_top;
925                 } else if (r->part == l->part) {
926                         node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
927                 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
928                         node->type.tv = tarval_bottom;
929                 } else {
930                         default_compute(node);
931                 }
932         } else {
933                 default_compute(node);
934         }
935 }  /* compute_Proj_Cmp */
936
937 /**
938  * (Re-)compute the type for a Proj-Nodes.
939  *
940  * @param node  the node
941  */
942 static void compute_Proj(node_t *node) {
943         ir_node *proj = node->node;
944         ir_mode *mode = get_irn_mode(proj);
945         ir_node *pred;
946
947         if (mode == mode_M) {
948                 /* mode M is always bottom */
949                 node->type.tv = tarval_bottom;
950                 return;
951         }
952         if (mode != mode_X) {
953                 ir_node *cmp = get_Proj_pred(proj);
954                 if (is_Cmp(cmp))
955                         compute_Proj_Cmp(node, cmp);
956                 else
957                         default_compute(node);
958                 return;
959         }
960         /* handle mode_X nodes */
961         pred = get_Proj_pred(proj);
962
963         switch (get_irn_opcode(pred)) {
964         case iro_Start:
965                 /* the Proj_X from the Start is always reachable */
966                 node->type.tv = tarval_R;
967                 break;
968         default:
969                 default_compute(node);
970         }
971 }  /* compute_Proj */
972
973 /**
974  * (Re-)compute the type for a given node.
975  *
976  * @param node  the node
977  */
978 static void compute(node_t *node) {
979         compute_func func = (compute_func)node->node->op->ops.generic;
980
981         if (func != NULL)
982                 func(node);
983 }  /* compute */
984
985 /**
986  * Place a node on the cprop list.
987  *
988  * @param y    the node
989  * @param env  the environment
990  */
991 static void add_node_to_cprop(node_t *y, environment_t *env) {
992         /* Add y to y.partition.cprop. */
993         if (y->on_cprop == 0) {
994                 partition_t *Y = y->part;
995
996                 y->cprop_next  = Y->cprop;
997                 Y->cprop = y;
998                 y->on_cprop    = 1;
999
1000                 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
1001
1002                 /* place its partition on the cprop list */
1003                 if (Y->on_cprop == 0) {
1004                         Y->cprop_next = env->cprop;
1005                         env->cprop    = Y;
1006                         Y->on_cprop   = 1;
1007                 }
1008         }
1009 }
1010
1011 /**
1012  * Propagate constant evaluation.
1013  *
1014  * @param env  the environment
1015  */
1016 static void propagate(environment_t *env) {
1017         partition_t    *X, *Y;
1018         node_t         *x;
1019         lattice_elem_t old_type;
1020         node_t         *fallen = NULL;
1021         unsigned       n_fallen = 0;
1022         int            i;
1023
1024         while (env->cprop != NULL) {
1025                 /* remove the first partition X from cprop but do not set the bit here */
1026                 X          = env->cprop;
1027                 env->cprop = X->cprop_next;
1028
1029                 dump_partition("Propagate", X);
1030                 do {
1031                         /* remove the first Node x from X.cprop but do NOT set the bit here */
1032                         x        = X->cprop;
1033                         X->cprop = x->cprop_next;
1034
1035                         /* compute a new type for x */
1036                         old_type = x->type;
1037                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1038                         compute(x);
1039                         if (x->type.tv != old_type.tv) {
1040                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1041                                 /* Add x to fallen. */
1042                                 x->next = fallen;
1043                                 fallen  = x;
1044                                 ++n_fallen;
1045
1046                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1047                                         ir_node *succ = get_irn_out(x->node, i);
1048                                         node_t  *y    = get_irn_node(succ);
1049
1050                                         /* Add y to y.partition.cprop. */
1051                                         add_node_to_cprop(y, env);
1052                                 }
1053                         }
1054                         /* now remove x from X.cprop: this ensures that a node is not placed on the list again
1055                            if is its user by itself (happens for Phi nodes and dead code) */
1056                         x->on_cprop = 0;
1057                 } while (X->cprop != NULL);
1058
1059                 /* now remove X from cprop, we have emptied it's local list */
1060                 X->on_cprop = 0;
1061
1062                 if (n_fallen != X->n_nodes) {
1063                         assert(n_fallen > 0);
1064                         Y = split(X, fallen, env);
1065                 } else {
1066                         Y = X;
1067                 }
1068                 split_by(Y, env);
1069         }
1070 }  /* propagate */
1071
1072 /**
1073  * Get the leader for a given node from its congruence class.
1074  *
1075  * @param irn  the node
1076  */
1077 static ir_node *get_leader(ir_node *irn) {
1078         partition_t *part = get_irn_node(irn)->part;
1079
1080         if (part->n_nodes > 1) {
1081                 DB((dbg, LEVEL_2, "Found congruence class for %+F ", irn));
1082                 dump_partition("", part);
1083         }
1084         return irn;
1085 }
1086
1087 /**
1088  * Post-Walker, apply the analysis results;
1089  */
1090 static void apply_result(ir_node *irn, void *ctx) {
1091         environment_t *env = ctx;
1092
1093         if (is_no_Block(irn)) {
1094                 ir_node *leader = get_leader(irn);
1095
1096                 if (leader != irn) {
1097                         exchange(irn, leader);
1098                 }
1099         }
1100 }  /* static void apply_result(ir_node *irn, void *ctx) {
1101  */
1102
1103 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1104
1105 /**
1106  * sets the generic functions to compute.
1107  */
1108 static void set_compute_functions(void) {
1109         int i;
1110
1111         /* set the default compute function */
1112         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1113                 ir_op *op = get_irp_opcode(i);
1114                 op->ops.generic = (op_func)default_compute;
1115         }
1116
1117         /* set specific functions */
1118         SET(Block);
1119         SET(Jmp);
1120         SET(Phi);
1121         SET(Sub);
1122         SET(SymConst);
1123         SET(End);
1124 }  /* set_compute_functions */
1125
1126 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1127         ir_node *irn = local != NULL ? local : n;
1128         node_t *node = get_irn_node(n);
1129
1130         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1131         return 1;
1132 }
1133
1134 void combo(ir_graph *irg) {
1135         environment_t env;
1136         ir_node       *start_bl, *initial_X;
1137         node_t        *start;
1138         ir_graph      *rem = current_ir_graph;
1139
1140         current_ir_graph = irg;
1141
1142         /* register a debug mask */
1143         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1144         firm_dbg_set_mask(dbg, SET_LEVEL_3);
1145
1146         obstack_init(&env.obst);
1147         env.worklist       = NULL;
1148         env.cprop          = NULL;
1149         env.touched        = NULL;
1150         env.TOP            = NULL;
1151 #ifdef DEBUG_libfirm
1152         env.dbg_list       = NULL;
1153 #endif
1154         env.opcode_map     = new_set(cmp_opcode, iro_Last * 4);
1155         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
1156         env.type2id_map    = pmap_create();
1157         env.end_idx        = get_opt_global_cse() ? 0 : -1;
1158         env.lambda_input   = 0;
1159
1160         assure_irg_outs(irg);
1161
1162         /* we have our own value_of function */
1163         set_value_of_func(get_node_tarval);
1164
1165         set_compute_functions();
1166
1167         /* create the initial partition and place it on the work list */
1168         env.TOP = new_partition(&env);
1169         env.TOP->wl_next = env.worklist;
1170         env.worklist     = env.TOP;
1171         irg_walk_graph(irg, NULL, create_initial_partitions, &env);
1172
1173         /* Place the START Node's partition on cprop.
1174            Place the START Node on its local worklist. */
1175         start_bl = get_irg_start_block(irg);
1176         start    = get_irn_node(start_bl);
1177         add_node_to_cprop(start, &env);
1178
1179         /* set the initial exec to R */
1180         initial_X = get_irg_initial_exec(irg);
1181         get_irn_node(initial_X)->type.tv = tarval_R;
1182
1183         do {
1184                 propagate(&env);
1185                 if (env.worklist != NULL)
1186                         cause_splits(&env);
1187         } while (env.cprop != NULL || env.worklist != NULL);
1188
1189         dump_all_partitions(&env);
1190
1191         set_dump_node_vcgattr_hook(dump_partition_hook);
1192         dump_ir_block_graph(irg, "-partition");
1193
1194
1195         /* apply the result */
1196         irg_walk_graph(irg, NULL, apply_result, &env);
1197
1198         pmap_destroy(env.type2id_map);
1199         del_set(env.opcode_map);
1200         del_set(env.opcode2id_map);
1201         obstack_free(&env.obst, NULL);
1202
1203         /* restore value_of() default behavior */
1204         set_value_of_func(NULL);
1205         current_ir_graph = rem;
1206 }  /* combo */