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