95629d7285122a77a70814e17a612d0a07c4a1bb
[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 "ircons.h"
39 #include "list.h"
40 #include "array.h"
41 #include "set.h"
42 #include "pmap.h"
43 #include "obstack.h"
44 #include "irgraph_t.h"
45 #include "irnode_t.h"
46 #include "iropt_t.h"
47 #include "irgwalk.h"
48 #include "irop.h"
49 #include "irouts.h"
50 #include "irgmod.h"
51 #include "debug.h"
52
53 #include "tv_t.h"
54
55 #include "irprintf.h"
56 #include "irdump.h"
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 listmap_entry_t   listmap_entry_t;
62
63 /** The type of the compute function. */
64 typedef void (*compute_func)(node_t *node);
65
66 /**
67  * An opcode map key.
68  */
69 struct opcode_key_t {
70         ir_opcode   code;   /**< The Firm opcode. */
71         ir_mode     *mode;  /**< The mode of all nodes in the partition. */
72         union {
73                 long      proj;   /**< For Proj nodes, its proj number */
74                 ir_entity *ent;   /**< For Sel Nodes, its entity */
75         } u;
76 };
77
78 /**
79  * An entry in the list_map.
80  */
81 struct listmap_entry_t {
82         void            *id;    /**< The id. */
83         node_t          *list;  /**< The associated list for this id. */
84         listmap_entry_t *next;  /**< Link to the next entry in the map. */
85 };
86
87 /** We must map id's to lists. */
88 typedef struct listmap_t {
89         set             *map;    /**< Map id's to listmap_entry_t's */
90         listmap_entry_t *values; /**< List of all values in the map. */
91 } listmap_t;
92
93 /**
94  * A lattice element. Because we handle constants and symbolic constants different, we
95  * have to use this union.
96  */
97 typedef union {
98         tarval          *tv;
99         symconst_symbol sym;
100 } lattice_elem_t;
101
102 /**
103  * A node.
104  */
105 struct node_t {
106         ir_node         *node;          /**< The IR-node itself. */
107         list_head       node_list;      /**< Double-linked list of entries. */
108         list_head       cprop_list;     /**< Double-linked partition.cprop list. */
109         partition_t     *part;          /**< points to the partition this node belongs to */
110         node_t          *next;          /**< Next node on local list (partition.touched, fallen). */
111         lattice_elem_t  type;           /**< The associated lattice element "type". */
112         int             max_user_input; /**< Maximum input number of Def-Use edges. */
113         int             next_edge;      /**< Index of the next Def-Use edge to use. */
114         unsigned        on_touched:1;   /**< Set, if this node is on the partition.touched set. */
115         unsigned        on_cprop:1;     /**< Set, if this node is on the partition.cprop list. */
116         unsigned        on_fallen:1;    /**< Set, if this node is on the fallen 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         list_head         cprop;           /**< The head of 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               max_arity;       /**< Maximum arity of all entries. */
132         int               max_user_inputs; /**< Maximum number of user inputs of all entries. */
133         unsigned          on_worklist:1;   /**< Set, if this partition is in the work list. */
134         unsigned          on_touched:1;    /**< Set, if this partition is on the touched set. */
135         unsigned          on_cprop:1;      /**< Set, if this partition is on the cprop list. */
136 #ifdef DEBUG_libfirm
137         partition_t       *dbg_next;       /**< Link all partitions for debugging */
138         unsigned          nr;              /**< A unique number for (what-)mapping, >0. */
139 #endif
140 };
141
142 typedef struct environment_t {
143         struct obstack  obst;           /**< obstack to allocate data structures. */
144         partition_t     *worklist;      /**< The work list. */
145         partition_t     *cprop;         /**< The constant propagation list. */
146         partition_t     *touched;       /**< the touched set. */
147         partition_t     *initial;       /**< The initial partition. */
148 #ifdef DEBUG_libfirm
149         partition_t     *dbg_list;      /**< List of all partitions. */
150 #endif
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 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
171
172 /**
173  * Dump partition to output.
174  */
175 static void dump_partition(const char *msg, const partition_t *part) {
176         const node_t   *node;
177         int            first = 1;
178         lattice_elem_t type = get_partition_type(part);
179
180         DB((dbg, LEVEL_2, "%s part%u (%u, %+F) {\n  ", msg, part->nr, part->n_nodes, type));
181         list_for_each_entry(node_t, node, &part->entries, node_list) {
182                 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
183                 first = 0;
184         }
185         DB((dbg, LEVEL_2, "\n}\n"));
186 }
187
188 /**
189  * Dump all partitions.
190  */
191 static void dump_all_partitions(const environment_t *env) {
192         const partition_t *P;
193
194         DB((dbg, LEVEL_2, "All partitions\n===============\n"));
195         for (P = env->dbg_list; P != NULL; P = P->dbg_next)
196                 dump_partition("", P);
197 }
198
199 #else
200 #define dump_partition(msg, part)
201 #define dump_all_partitions(env)
202 #endif
203
204 /**
205  * Return the "top" value depending on the mode
206  */
207 static tarval *get_top_value(const ir_mode *mode) {
208         return (mode == mode_X || mode == mode_BB) ? tarval_unreachable : tarval_top;
209 }
210
211 /**
212  * Compare two pointer values of a listmap.
213  */
214 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
215         const listmap_entry_t *e1 = elt;
216         const listmap_entry_t *e2 = key;
217
218         (void) size;
219         return e1->id != e2->id;
220 }  /* listmap_cmp_ptr */
221
222 /**
223  * Initializes a listmap.
224  *
225  * @param map  the listmap
226  */
227 static void listmap_init(listmap_t *map) {
228         map->map    = new_set(listmap_cmp_ptr, 16);
229         map->values = NULL;
230 }  /* listmap_init */
231
232 /**
233  * Terminates a listmap.
234  *
235  * @param map  the listmap
236  */
237 static void listmap_term(listmap_t *map) {
238         del_set(map->map);
239 }  /* listmap_term */
240
241 /**
242  * Return the associated listmap entry for a given id.
243  *
244  * @param map  the listmap
245  * @param id   the id to search for
246  *
247  * @return the asociated listmap entry for the given id
248  */
249 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
250         listmap_entry_t key, *entry;
251
252         key.id   = id;
253         key.list = NULL;
254         key.next = NULL;
255         entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
256
257         if (entry->list == NULL) {
258                 /* a new entry, put into the list */
259                 entry->next = map->values;
260                 map->values = entry;
261         }
262         return entry;
263 }  /* listmap_find */
264
265 /**
266  * Calculate the hash value for an opcode map entry.
267  *
268  * @param entry  an opcode map entry
269  *
270  * @return a hash value for the given opcode map entry
271  */
272 static unsigned opcode_hash(const opcode_key_t *entry) {
273         return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
274 }  /* opcode_hash */
275
276 /**
277  * Compare two entries in the opcode map.
278  */
279 static int cmp_opcode(const void *elt, const void *key, size_t size) {
280         const opcode_key_t *o1 = elt;
281         const opcode_key_t *o2 = key;
282
283         (void) size;
284         return o1->code != o2->code || o1->mode != o2->mode ||
285                o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
286 }  /* cmp_opcode */
287
288 /**
289  * Compare two Def-Use edges for input position.
290  */
291 static int cmp_def_use_edge(const void *a, const void *b) {
292         const ir_def_use_edge *ea = a;
293         const ir_def_use_edge *eb = b;
294
295         /* no overrun, because range is [-1, MAXINT] */
296         return ea->pos - eb->pos;
297 }  /* cmp_def_use_edge */
298
299 /**
300  * We need the Def-Use edges sorted.
301  */
302 static void sort_irn_outs(node_t *node) {
303         ir_node *irn = node->node;
304         int n_outs = get_irn_n_outs(irn);
305
306         if (n_outs > 1) {
307                 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
308         }
309         node->max_user_input = irn->out[n_outs + 1].pos;
310 }  /* sort_irn_outs */
311
312 /**
313  * Return the type of a node.
314  *
315  * @param irn  an IR-node
316  *
317  * @return the associated type of this node
318  */
319 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
320         return get_irn_node(irn)->type;
321 }  /* get_node_type */
322
323 /**
324  * Return the tarval of a node.
325  *
326  * @param irn  an IR-node
327  *
328  * @return the associated type of this node
329  */
330 static INLINE tarval *get_node_tarval(const ir_node *irn) {
331         lattice_elem_t type = get_node_type(irn);
332
333         if (is_tarval(type.tv))
334                 return type.tv;
335         return tarval_bottom;
336 }  /* get_node_type */
337
338 /**
339  * Add a partition to the worklist.
340  */
341 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
342         assert(X->on_worklist == 0);
343         X->wl_next     = env->worklist;
344         X->on_worklist = 1;
345         env->worklist  = X;
346 }
347
348 /**
349  * Create a new empty partition.
350  *
351  * @param env   the environment
352  *
353  * @return a newly allocated partition
354  */
355 static INLINE partition_t *new_partition(environment_t *env) {
356         partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
357
358         INIT_LIST_HEAD(&part->entries);
359         INIT_LIST_HEAD(&part->cprop);
360         part->wl_next         = NULL;
361         part->touched_next    = NULL;
362         part->cprop_next      = NULL;
363         part->touched         = NULL;
364         part->n_nodes         = 0;
365         part->n_touched       = 0;
366         part->max_arity       = 0;
367         part->max_user_inputs = 0;
368         part->on_worklist     = 0;
369         part->on_touched      = 0;
370         part->on_cprop        = 0;
371 #ifdef DEBUG_libfirm
372         part->dbg_next        = env->dbg_list;
373         env->dbg_list         = part;
374         part->nr              = part_nr++;
375 #endif
376
377         return part;
378 }  /* new_partition */
379
380 /**
381  * Get the first node from a partition.
382  */
383 static INLINE node_t *get_first_node(const partition_t *X) {
384         return list_entry(X->entries.next, node_t, node_list);
385 }
386
387 /**
388  * Return the type of a partition (assuming partition is non-empty and
389  * all elements have the same type).
390  *
391  * @param X  a partition
392  *
393  * @return the type of the first element of the partition
394  */
395 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
396         const node_t *first = get_first_node(X);
397         return first->type;
398 }  /* get_partition_type */
399
400 /**
401  * Creates a partition node for the given IR-node and place it
402  * into the given partition.
403  *
404  * @param irn   an IR-node
405  * @param part  a partition to place the node in
406  * @param env   the environment
407  *
408  * @return the created node
409  */
410 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
411         /* create a partition node and place it in the partition */
412         node_t *node = obstack_alloc(&env->obst, sizeof(*node));
413         ir_mode *mode = get_irn_mode(irn);
414
415         INIT_LIST_HEAD(&node->node_list);
416         INIT_LIST_HEAD(&node->cprop_list);
417         node->node           = irn;
418         node->part           = part;
419         node->next           = NULL;
420         node->type.tv        = get_top_value(mode);
421         node->max_user_input = 0;
422         node->next_edge      = 0;
423         node->on_touched     = 0;
424         node->on_cprop       = 0;
425         node->on_fallen      = 0;
426         set_irn_node(irn, node);
427
428         list_add_tail(&node->node_list, &part->entries);
429         ++part->n_nodes;
430
431         return node;
432 }  /* create_partition_node */
433
434 /**
435  * Pre-Walker, init all Block-Phi lists.
436  */
437 static void init_block_phis(ir_node *irn, void *env) {
438         (void) env;
439
440         if (is_Block(irn)) {
441                 set_Block_phis(irn, NULL);
442         }
443 }
444
445 /**
446  * Post-Walker, initialize all Nodes' type to U or top and place
447  * all nodes into the TOP partition.
448  */
449 static void create_initial_partitions(ir_node *irn, void *ctx) {
450         environment_t *env  = ctx;
451         partition_t   *part = env->initial;
452         node_t        *node;
453         int           arity;
454
455         node = create_partition_node(irn, part, env);
456         sort_irn_outs(node);
457         arity = get_irn_arity(irn);
458         if (arity > part->max_arity)
459                 part->max_arity = arity;
460         if (node->max_user_input > part->max_user_inputs)
461                 part->max_user_inputs = node->max_user_input;
462
463         if (is_Phi(irn)) {
464                 add_Block_phi(get_nodes_block(irn), irn);
465         }
466 }  /* create_initial_partitions */
467
468 /**
469  * Add a partition to the touched set if not already there.
470  *
471  * @param part  the partition
472  * @param env   the environment
473  */
474 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
475         if (part->on_touched == 0) {
476                 part->touched_next = env->touched;
477                 env->touched       = part;
478                 part->on_touched   = 1;
479         }
480 }  /* add_to_touched */
481
482 /**
483  * Add a node to the entry.partition.touched set if not already there.
484  *
485  * @param y  a node
486  */
487 static INLINE void add_to_partition_touched(node_t *y) {
488         if (y->on_touched == 0) {
489                 partition_t *part = y->part;
490
491                 y->next       = part->touched;
492                 part->touched = y;
493                 y->on_touched = 1;
494                 ++part->n_touched;
495         }
496 }  /* add_to_partition_touched */
497
498 /**
499  * Update the worklist: If Z is on worklist then add Z' to worklist.
500  * Else add the smaller of Z and Z' to worklist.
501  *
502  * @param Z        the Z partition
503  * @param Z_prime  the Z' partition, a previous part of Z
504  * @param env      the environment
505  */
506 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
507         if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
508                 add_to_worklist(Z_prime, env);
509         } else {
510                 add_to_worklist(Z, env);
511         }
512 }  /* update_worklist */
513
514 /**
515  * Split a partition by a local list.
516  *
517  * @param Z    the Z partition to split
518  * @param g    a (non-empty) node list
519  * @param env  the environment
520  *
521  * @return  a new partition containing the nodes of g
522  */
523 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
524         partition_t *Z_prime;
525         node_t      *node;
526         unsigned    n = 0;
527         int         max_input, max_arity, arity;
528
529         dump_partition("Splitting ", Z);
530
531         assert(g != NULL);
532
533         /* Remove g from Z. */
534         for (node = g; node != NULL; node = node->next) {
535                 list_del(&node->node_list);
536                 ++n;
537         }
538         assert(n < Z->n_nodes);
539         Z->n_nodes -= n;
540
541         /* Move g to a new partition, Z\92. */
542         Z_prime = new_partition(env);
543         max_arity = max_input = 0;
544         for (node = g; node != NULL; node = node->next) {
545                 list_add(&node->node_list, &Z_prime->entries);
546                 node->part = Z_prime;
547                 arity = get_irn_arity(node->node);
548                 if (arity > max_arity)
549                         max_arity = arity;
550                 if (node->max_user_input > max_input)
551                         max_input = node->max_user_input;
552         }
553         Z_prime->max_arity       = max_arity;
554         Z_prime->max_user_inputs = max_input;
555         Z_prime->n_nodes         = n;
556
557         update_worklist(Z, Z_prime, env);
558
559         dump_partition("Now ", Z);
560         dump_partition("Created new ", Z_prime);
561         return Z_prime;
562 }  /* split */
563
564 /**
565  * Returns non-zero if the i'th input of a Phi node is live.
566  *
567  * @param phi  a Phi-node
568  * @param i    an input number
569  *
570  * @return non-zero if the i'th input of the given Phi node is live
571  */
572 static int is_live_input(ir_node *phi, int i) {
573         if (i >= 0) {
574                 ir_node        *block = get_nodes_block(phi);
575                 ir_node        *pred  = get_Block_cfgpred(block, i);
576                 lattice_elem_t type   = get_node_type(pred);
577
578                 return type.tv != tarval_unreachable;
579         }
580         /* else it's the control input, always live */
581         return 1;
582 }  /* is_live_input */
583
584 /**
585  * Return non-zero if a type is a constant.
586  */
587 static int is_constant_type(lattice_elem_t type) {
588         if (type.tv != tarval_bottom && type.tv != tarval_top)
589                 return 1;
590         return 0;
591 }  /* is_constant_type */
592
593 /**
594  * Place a node on the cprop list.
595  *
596  * @param y    the node
597  * @param env  the environment
598  */
599 static void add_node_to_cprop(node_t *y, environment_t *env) {
600         /* Add y to y.partition.cprop. */
601         if (y->on_cprop == 0) {
602                 partition_t *Y = y->part;
603
604                 list_add_tail(&y->cprop_list, &Y->cprop);
605                 y->on_cprop   = 1;
606
607                 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
608
609                 /* place its partition on the cprop list */
610                 if (Y->on_cprop == 0) {
611                         Y->cprop_next = env->cprop;
612                         env->cprop    = Y;
613                         Y->on_cprop   = 1;
614                 }
615         }
616         if (get_irn_mode(y->node) == mode_T) {
617                 /* mode_T nodes always produce tarval_bottom, so we must explicitly
618                    add it's Proj's to get constant evaluation to work */
619                 int i;
620
621                 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
622                         node_t *proj = get_irn_node(get_irn_out(y->node, i));
623
624                         add_node_to_cprop(proj, env);
625                 }
626         }
627         if (is_Block(y->node)) {
628                 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
629                  * if someone placeis the block. The Block is only placed if the reachability
630                  * changes, and this must be re-evaluated in compute_Phi(). */
631                 ir_node *phi;
632                 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
633                         node_t *p = get_irn_node(phi);
634                         add_node_to_cprop(p, env);
635                 }
636         }
637 }  /* add_node_to_cprop */
638
639 /**
640  * Split the partitions if caused by the first entry on the worklist.
641  *
642  * @param env  the environment
643  */
644 static void cause_splits(environment_t *env) {
645         partition_t *X, *Y, *Z;
646         node_t      *x, *y, *e;
647         int         i, end_idx;
648         ir_opcode   code;
649         ir_node     *succ;
650
651         /* remove the first partition from the worklist */
652         X = env->worklist;
653         env->worklist  = X->wl_next;
654         X->on_worklist = 0;
655
656         dump_partition("Cause_split: ", X);
657         end_idx = env->end_idx;
658         for (i = -1; i <= X->max_user_inputs; ++i) {
659                 /* empty the touched set: already done, just clear the list */
660                 env->touched = NULL;
661
662                 list_for_each_entry(node_t, x, &X->entries, node_list) {
663                         int num_edges;
664
665                         if (i == -1) {
666                                 x->next_edge = 1;
667                         }
668                         num_edges = get_irn_n_outs(x->node);
669
670                         while (x->next_edge <= num_edges) {
671                                 ir_def_use_edge *edge = &x->node->out[x->next_edge];
672
673                                 /* check if we have necessary edges */
674                                 if (edge->pos > i)
675                                         break;
676
677                                 ++x->next_edge;
678
679                                 succ = edge->use;
680
681                                 /* ignore the "control input" for non-pinned nodes
682                                    if we are running in GCSE mode */
683                                 if (i < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
684                                         continue;
685
686                                 y = get_irn_node(succ);
687                                 if (is_constant_type(y->type)) {
688                                         code = get_irn_opcode(succ);
689                                         if (code == iro_Sub || (code == iro_Proj && is_Cmp(get_Proj_pred(succ))))
690                                                 add_node_to_cprop(y, env);
691                                 }
692
693                                 /* Partitions of constants should not be split simply because their Nodes have unequal
694                                    functions or incongruent inputs. */
695                                 if (y->type.tv == tarval_bottom &&
696                                         (! is_Phi(y->node) || is_live_input(y->node, i))) {
697                                         Y = y->part;
698                                         add_to_touched(Y, env);
699                                         add_to_partition_touched(y);
700                                 }
701                         }
702                 }
703
704                 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
705                         /* remove it from the touched set */
706                         Z->on_touched = 0;
707
708                         if (Z->n_nodes != Z->n_touched) {
709                                 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
710                                 split(Z, Z->touched, env);
711                         }
712                         /* Empty local Z.touched. */
713                         for (e = Z->touched; e != NULL; e = e->next) {
714                                 e->on_touched = 0;
715                         }
716                         Z->touched   = NULL;
717                         Z->n_touched = 0;
718                 }
719         }
720 }  /* cause_splits */
721
722 /**
723  * Implements split_by_what(): Split a partition by characteristics given
724  * by the what function.
725  *
726  * @param X     the partition to split
727  * @param What  a function returning an Id for every node of the partition X
728  * @param P     an flexible array to store the result partitions or NULL
729  * @param env   the environment
730  *
731  * @return if P != NULL P will be filled with the resulting partitions and returned
732  */
733 static partition_t **split_by_what(partition_t *X, what_func What,
734                                    partition_t **P, environment_t *env) {
735         node_t          *x, *S;
736         listmap_t       map;
737         listmap_entry_t *iter;
738         partition_t     *R;
739
740         /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
741         listmap_init(&map);
742         list_for_each_entry(node_t, x, &X->entries, node_list) {
743                 void            *id = What(x, env);
744                 listmap_entry_t *entry;
745
746                 if (id == NULL) {
747                         /* input not allowed, ignore */
748                         continue;
749                 }
750                 /* Add x to map[What(x)]. */
751                 entry = listmap_find(&map, id);
752                 x->next     = entry->list;
753                 entry->list = x;
754         }
755         /* Let P be a set of Partitions. */
756
757         /* for all sets S except one in the range of map do */
758         for (iter = map.values; iter != NULL; iter = iter->next) {
759                 if (iter->next == NULL) {
760                         /* this is the last entry, ignore */
761                         break;
762                 }
763                 S = iter->list;
764
765                 /* Add SPLIT( X, S ) to P. */
766                 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
767                 R = split(X, S, env);
768                 if (P != NULL) {
769                         ARR_APP1(partition_t *, P, R);
770                 }
771         }
772         /* Add X to P. */
773         if (P != NULL) {
774                 ARR_APP1(partition_t *, P, X);
775         }
776
777         listmap_term(&map);
778         return P;
779 }  /* split_by_what */
780
781 /** lambda n.(n.type) */
782 static void *lambda_type(const node_t *node, environment_t *env) {
783         (void)env;
784         return node->type.tv;
785 }  /* lambda_type */
786
787 /** lambda n.(n.opcode) */
788 static void *lambda_opcode(const node_t *node, environment_t *env) {
789         opcode_key_t key, *entry;
790         ir_node      *irn = node->node;
791
792         key.code   = get_irn_opcode(irn);
793         key.mode   = get_irn_mode(irn);
794         key.u.proj = 0;
795         key.u.ent  = NULL;
796
797         switch (get_irn_opcode(irn)) {
798         case iro_Proj:
799                 key.u.proj = get_Proj_proj(irn);
800                 break;
801         case iro_Sel:
802                 key.u.ent = get_Sel_entity(irn);
803                 break;
804         default:
805                 break;
806         }
807
808         entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
809         return entry;
810 }  /* lambda_opcode */
811
812 /** lambda n.(n[i].partition) */
813 static void *lambda_partition(const node_t *node, environment_t *env) {
814         ir_node *pred;
815         node_t  *p;
816         int     i = env->lambda_input;
817
818         if (i >= get_irn_arity(node->node)) {
819                 /* we are outside the allowed range */
820                 return NULL;
821         }
822
823         /* ignore the "control input" for non-pinned nodes
824            if we are running in GCSE mode */
825         if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
826                 return NULL;
827
828         pred = get_irn_n(node->node, i);
829         p    = get_irn_node(pred);
830
831         return p->part;
832 }  /* lambda_partition */
833
834 /**
835  * Checks whether a type is a constant.
836  */
837 static int is_type_constant(lattice_elem_t type) {
838         if (is_tarval(type.tv))
839                 return tarval_is_constant(type.tv);
840         /* else it is a symconst */
841         return 1;
842 }
843
844 /**
845  * Implements split_by().
846  *
847  * @param X    the partition to split
848  * @param env  the environment
849  */
850 static void split_by(partition_t *X, environment_t *env) {
851         partition_t **P = NEW_ARR_F(partition_t *, 0);
852         int         i, j, k;
853
854         DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
855         P = split_by_what(X, lambda_type, P, env);
856         for (i = ARR_LEN(P) - 1; i >= 0; --i) {
857                 partition_t *Y = P[i];
858
859                 if (Y->n_nodes > 1) {
860                         lattice_elem_t type = get_partition_type(Y);
861
862                         /* we do not want split the TOP, unreachable or constant partitions */
863                         if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
864                                 partition_t **Q = NEW_ARR_F(partition_t *, 0);
865
866                                 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
867                                 Q = split_by_what(Y, lambda_opcode, Q, env);
868
869                                 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
870                                         partition_t *Z = Q[j];
871
872                                         for (k = Z->max_arity - 1; k >= -1; --k) {
873                                                 if (Z->n_nodes > 1) {
874                                                         env->lambda_input = k;
875                                                         DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", k, Z->nr));
876                                                         split_by_what(Z, lambda_partition, NULL, env);
877                                                 }
878                                         }
879                                 }
880                                 DEL_ARR_F(Q);
881                         }
882                 }
883         }
884         DEL_ARR_F(P);
885 }  /* split_by */
886
887 /**
888  * (Re-)compute the type for a given node.
889  *
890  * @param node  the node
891  */
892 static void default_compute(node_t *node) {
893         int     i;
894         ir_node *irn = node->node;
895         tarval  *top = tarval_top;
896
897         if (get_irn_mode(node->node) == mode_X)
898                 top = tarval_unreachable;
899
900         if (get_irn_pinned(irn) == op_pin_state_pinned) {
901                 node_t *block = get_irn_node(get_nodes_block(irn));
902
903                 if (block->type.tv == tarval_unreachable) {
904                         node->type.tv = top;
905                         return;
906                 }
907         }
908
909         /* if any of the data inputs have type top, the result is type top */
910         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
911                 ir_node *pred = get_irn_n(irn, i);
912                 node_t  *p    = get_irn_node(pred);
913
914                 if (p->type.tv == tarval_top) {
915                         node->type.tv = top;
916                         return;
917                 }
918         }
919
920         if (get_irn_mode(node->node) == mode_X)
921                 node->type.tv = tarval_reachable;
922         else
923                 node->type.tv = computed_value(irn);
924 }  /* default_compute */
925
926 /**
927  * (Re-)compute the type for a Block node.
928  *
929  * @param node  the node
930  */
931 static void compute_Block(node_t *node) {
932         int     i;
933         ir_node *block = node->node;
934
935         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
936                 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
937
938                 if (pred->type.tv == tarval_reachable) {
939                         /* A block is reachable, if at least of predecessor is reachable. */
940                         node->type.tv = tarval_reachable;
941                         return;
942                 }
943         }
944         node->type.tv = tarval_unreachable;
945 }  /* compute_Block */
946
947 /**
948  * (Re-)compute the type for a Jmp node.
949  *
950  * @param node  the node
951  */
952 static void compute_Jmp(node_t *node) {
953         node_t *block = get_irn_node(get_nodes_block(node->node));
954
955         node->type = block->type;
956 }  /* compute_Jmp */
957
958 /**
959  * (Re-)compute the type for the End node.
960  *
961  * @param node  the node
962  */
963 static void compute_End(node_t *node) {
964         /* the End node is NOT dead of course */
965         node->type.tv = tarval_reachable;
966 }
967
968 /**
969  * (Re-)compute the type for a SymConst node.
970  *
971  * @param node  the node
972  */
973 static void compute_SymConst(node_t *node) {
974         ir_node *irn = node->node;
975         node_t  *block = get_irn_node(get_nodes_block(irn));
976
977         if (block->type.tv == tarval_unreachable) {
978                 node->type.tv = tarval_top;
979                 return;
980         }
981         switch (get_SymConst_kind(irn)) {
982         case symconst_addr_ent:
983         /* case symconst_addr_name: cannot handle this yet */
984                 node->type.sym = get_SymConst_symbol(irn);
985                 break;
986         default:
987                 node->type.tv = computed_value(irn);
988         }
989 }  /* compute_SymConst */
990
991 /**
992  * (Re-)compute the type for a Phi node.
993  *
994  * @param node  the node
995  */
996 static void compute_Phi(node_t *node) {
997         int            i;
998         ir_node        *phi = node->node;
999         lattice_elem_t type;
1000
1001         /* if a Phi is in a unreachable block, its type is TOP */
1002         node_t *block = get_irn_node(get_nodes_block(phi));
1003
1004         if (block->type.tv == tarval_unreachable) {
1005                 node->type.tv = tarval_top;
1006                 return;
1007         }
1008
1009         /* Phi implements the Meet operation */
1010         type.tv = tarval_top;
1011         for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1012                 node_t *pred   = get_irn_node(get_Phi_pred(phi, i));
1013                 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1014
1015                 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1016                         /* ignore TOP inputs: We must check here for unreachable blocks,
1017                            because Firm constants live in the Start Block are NEVER Top.
1018                            Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1019                            comes from a unreachable input. */
1020                         continue;
1021                 }
1022                 if (pred->type.tv == tarval_bottom) {
1023                         node->type.tv = tarval_bottom;
1024                         return;
1025                 } else if (type.tv == tarval_top) {
1026                         /* first constant found */
1027                         type = pred->type;
1028                 } else if (type.tv != pred->type.tv) {
1029                         /* different constants or tarval_bottom */
1030                         node->type.tv = tarval_bottom;
1031                         return;
1032                 }
1033                 /* else nothing, constants are the same */
1034         }
1035         node->type = type;
1036 }  /* compute_Phi */
1037
1038 /**
1039  * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1040  *
1041  * @param node  the node
1042  */
1043 static void compute_Add(node_t *node) {
1044         ir_node        *sub = node->node;
1045         node_t         *l   = get_irn_node(get_Add_left(sub));
1046         node_t         *r   = get_irn_node(get_Add_right(sub));
1047         lattice_elem_t a    = l->type;
1048         lattice_elem_t b    = r->type;
1049         node_t         *block = get_irn_node(get_nodes_block(sub));
1050         ir_mode        *mode;
1051
1052         if (block->type.tv == tarval_unreachable) {
1053                 node->type.tv = tarval_top;
1054                 return;
1055         }
1056
1057         if (a.tv == tarval_top || b.tv == tarval_top) {
1058                 node->type.tv = tarval_top;
1059         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1060                 node->type.tv = tarval_bottom;
1061         } else {
1062                 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1063                    must call tarval_add() first to handle this case! */
1064                 if (is_tarval(a.tv)) {
1065                         if (is_tarval(b.tv)) {
1066                                 node->type.tv = tarval_add(a.tv, b.tv);
1067                                 return;
1068                         }
1069                         mode = get_tarval_mode(a.tv);
1070                         if (a.tv == get_mode_null(mode)) {
1071                                 node->type = b;
1072                                 return;
1073                         }
1074                 } else if (is_tarval(b.tv)) {
1075                         mode = get_tarval_mode(b.tv);
1076                         if (b.tv == get_mode_null(mode)) {
1077                                 node->type = a;
1078                                 return;
1079                         }
1080                 }
1081                 node->type.tv = tarval_bottom;
1082         }
1083 }  /* compute_Add */
1084
1085 /**
1086  * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1087  *
1088  * @param node  the node
1089  */
1090 static void compute_Sub(node_t *node) {
1091         ir_node        *sub = node->node;
1092         node_t         *l   = get_irn_node(get_Sub_left(sub));
1093         node_t         *r   = get_irn_node(get_Sub_right(sub));
1094         lattice_elem_t a    = l->type;
1095         lattice_elem_t b    = r->type;
1096         node_t         *block = get_irn_node(get_nodes_block(sub));
1097
1098         if (block->type.tv == tarval_unreachable) {
1099                 node->type.tv = tarval_top;
1100                 return;
1101         }
1102
1103         if (a.tv == tarval_top || b.tv == tarval_top) {
1104                 node->type.tv = tarval_top;
1105         } else if (r->part == l->part) {
1106                 ir_mode *mode = get_irn_mode(sub);
1107                 node->type.tv = get_mode_null(mode);
1108         } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1109                 node->type.tv = tarval_bottom;
1110         } else {
1111                 if (is_tarval(a.tv) && is_tarval(b.tv))
1112                         node->type.tv = tarval_sub(a.tv, b.tv);
1113                 else
1114                         node->type.tv = tarval_bottom;
1115         }
1116 }  /* compute_Sub */
1117
1118 /**
1119  * (Re-)compute the type for a Proj(Cmp).
1120  *
1121  * @param node  the node
1122  * @param cond  the predecessor Cmp node
1123  */
1124 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1125         ir_node        *proj = node->node;
1126         node_t         *l    = get_irn_node(get_Cmp_left(cmp));
1127         node_t         *r    = get_irn_node(get_Cmp_right(cmp));
1128         lattice_elem_t a     = l->type;
1129         lattice_elem_t b     = r->type;
1130         pn_Cmp         pnc   = get_Proj_proj(proj);
1131
1132         /*
1133          * BEWARE: a == a is NOT always True for floating Point values, as
1134          * NaN != NaN is defined, so we must check this here.
1135          */
1136         if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt ||  pnc == pn_Cmp_Gt) {
1137                 if (a.tv == tarval_top || b.tv == tarval_top) {
1138                         node->type.tv = tarval_top;
1139                 } else if (r->part == l->part) {
1140                         node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1141                 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1142                         node->type.tv = tarval_bottom;
1143                 } else {
1144                         default_compute(node);
1145                 }
1146         } else {
1147                 default_compute(node);
1148         }
1149 }  /* compute_Proj_Cmp */
1150
1151 /**
1152  * (Re-)compute the type for a Proj(Cond).
1153  *
1154  * @param node  the node
1155  * @param cond  the predecessor Cond node
1156  */
1157 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1158         ir_node *proj     = node->node;
1159         long    pnc       = get_Proj_proj(proj);
1160         ir_node *sel      = get_Cond_selector(cond);
1161         node_t  *selector = get_irn_node(sel);
1162
1163         if (get_irn_mode(sel) == mode_b) {
1164                 /* an IF */
1165                 if (pnc == pn_Cond_true) {
1166                         if (selector->type.tv == tarval_b_false) {
1167                                 node->type.tv = tarval_unreachable;
1168                         } else if (selector->type.tv == tarval_b_true) {
1169                                 node->type.tv = tarval_reachable;
1170                         } else if (selector->type.tv == tarval_bottom) {
1171                                 node->type.tv = tarval_reachable;
1172                         } else {
1173                                 assert(selector->type.tv == tarval_top);
1174                                 node->type.tv = tarval_unreachable;
1175                         }
1176                 } else {
1177                         assert(pnc == pn_Cond_false);
1178
1179                         if (selector->type.tv == tarval_b_false) {
1180                                 node->type.tv = tarval_reachable;
1181                         } else if (selector->type.tv == tarval_b_true) {
1182                                 node->type.tv = tarval_unreachable;
1183                         } else if (selector->type.tv == tarval_bottom) {
1184                                 node->type.tv = tarval_reachable;
1185                         } else {
1186                                 assert(selector->type.tv == tarval_top);
1187                                 node->type.tv = tarval_unreachable;
1188                         }
1189                 }
1190         } else {
1191                 /* an SWITCH */
1192                 if (selector->type.tv == tarval_bottom) {
1193                         node->type.tv = tarval_reachable;
1194                 } else if (selector->type.tv == tarval_top) {
1195                         node->type.tv = tarval_unreachable;
1196                 } else {
1197                         long value = get_tarval_long(selector->type.tv);
1198                         if (pnc == get_Cond_defaultProj(cond)) {
1199                                 /* default switch, have to check ALL other cases */
1200                                 int i;
1201
1202                                 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1203                                         ir_node *succ = get_irn_out(cond, i);
1204
1205                                         if (succ == proj)
1206                                                 continue;
1207                                         if (value == get_Proj_proj(succ)) {
1208                                                 /* we found a match, will NOT take the default case */
1209                                                 node->type.tv = tarval_unreachable;
1210                                                 return;
1211                                         }
1212                                 }
1213                                 /* all cases checked, no match, will take default case */
1214                                 node->type.tv = tarval_reachable;
1215                         } else {
1216                                 /* normal case */
1217                                 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1218                         }
1219                 }
1220         }
1221 }  /* compute_Proj_Cond */
1222
1223 /**
1224  * (Re-)compute the type for a Proj-Nodes.
1225  *
1226  * @param node  the node
1227  */
1228 static void compute_Proj(node_t *node) {
1229         ir_node *proj = node->node;
1230         ir_mode *mode = get_irn_mode(proj);
1231         node_t  *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1232         ir_node *pred  = get_Proj_pred(proj);
1233
1234         if (block->type.tv == tarval_unreachable) {
1235                 /* a Proj node in an unreachable block computes Top
1236                    except if it's the initial_exec node. */
1237                 if (get_Proj_proj(proj) != pn_Start_X_initial_exec ||
1238                         ! is_Start(pred)) {
1239                         node->type.tv = get_top_value(mode);
1240                         return;
1241                 }
1242         }
1243
1244         if (mode == mode_M) {
1245                 /* mode M is always bottom */
1246                 node->type.tv = tarval_bottom;
1247                 return;
1248         }
1249         if (mode != mode_X) {
1250                 if (is_Cmp(pred))
1251                         compute_Proj_Cmp(node, pred);
1252                 else
1253                         default_compute(node);
1254                 return;
1255         }
1256         /* handle mode_X nodes */
1257
1258         switch (get_irn_opcode(pred)) {
1259         case iro_Start:
1260                 /* the Proj_X from the Start is always reachable */
1261                 node->type.tv = tarval_reachable;
1262                 break;
1263         case iro_Cond:
1264                 compute_Proj_Cond(node, pred);
1265                 break;
1266         default:
1267                 default_compute(node);
1268         }
1269 }  /* compute_Proj */
1270
1271 /**
1272  * (Re-)compute the type for a given node.
1273  *
1274  * @param node  the node
1275  */
1276 static void compute(node_t *node) {
1277         compute_func func = (compute_func)node->node->op->ops.generic;
1278
1279         if (func != NULL)
1280                 func(node);
1281 }  /* compute */
1282
1283 /**
1284  * Propagate constant evaluation.
1285  *
1286  * @param env  the environment
1287  */
1288 static void propagate(environment_t *env) {
1289         partition_t    *X, *Y;
1290         node_t         *x;
1291         lattice_elem_t old_type;
1292         node_t         *fallen;
1293         unsigned       n_fallen;
1294         int            i;
1295
1296         while (env->cprop != NULL) {
1297                 /* remove the first partition X from cprop */
1298                 X          = env->cprop;
1299                 X->on_cprop = 0;
1300                 env->cprop = X->cprop_next;
1301
1302                 fallen   = NULL;
1303                 n_fallen = 0;
1304                 while (! list_empty(&X->cprop)) {
1305                         /* remove the first Node x from X.cprop */
1306                         x = list_entry(X->cprop.next, node_t, cprop_list);
1307                         list_del(&x->cprop_list);
1308                         x->on_cprop = 0;
1309
1310                         /* compute a new type for x */
1311                         old_type = x->type;
1312                         DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1313                         compute(x);
1314                         if (x->type.tv != old_type.tv) {
1315                                 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1316
1317                                 if (x->on_fallen == 0) {
1318                                         /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
1319                                            not already on the list. */
1320                                         x->next      = fallen;
1321                                         x->on_fallen = 1;
1322                                         fallen       = x;
1323                                         ++n_fallen;
1324                                 }
1325                                 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1326                                         ir_node *succ = get_irn_out(x->node, i);
1327                                         node_t  *y    = get_irn_node(succ);
1328
1329                                         /* Add y to y.partition.cprop. */
1330                                         add_node_to_cprop(y, env);
1331                                 }
1332                         }
1333                 }
1334
1335                 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1336                         DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
1337                         Y = split(X, fallen, env);
1338                 } else {
1339                         Y = X;
1340                 }
1341                 /* remove the nodes from the fallen list */
1342                 for (x = fallen; x != NULL; x = x->next)
1343                         x->on_fallen = 0;
1344
1345                 if (Y->n_nodes > 1)
1346                         split_by(Y, env);
1347         }
1348 }  /* propagate */
1349
1350 /**
1351  * Get the leader for a given node from its congruence class.
1352  *
1353  * @param irn  the node
1354  */
1355 static ir_node *get_leader(node_t *node) {
1356         partition_t *part = node->part;
1357
1358         if (part->n_nodes > 1) {
1359                 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1360
1361                 return get_first_node(part)->node;
1362         }
1363         return node->node;
1364 }
1365
1366 /**
1367  * Post-Walker, apply the analysis results;
1368  */
1369 static void apply_result(ir_node *irn, void *ctx) {
1370         node_t *node = get_irn_node(irn);
1371
1372         (void) ctx;
1373         if (is_Block(irn)) {
1374                 if (irn == get_irg_end_block(current_ir_graph)) {
1375                         /* the EndBlock is always reachable even if the analysis
1376                            finds out the opposite :-) */
1377                         return;
1378                 }
1379
1380                 if (node->type.tv == tarval_unreachable) {
1381                         /* mark dead blocks */
1382                         set_Block_dead(irn);
1383                 }
1384         } else if (is_End(irn)) {
1385                 /* do not touch the End node */
1386         } else {
1387                 node_t *block = get_irn_node(get_nodes_block(irn));
1388
1389                 if (block->type.tv == tarval_unreachable) {
1390                         if (! is_Bad(irn)) {
1391                                 ir_node *bad = get_irg_bad(current_ir_graph);
1392
1393                                 /* here, bad might already have a node, but this can be safely ignored
1394                                    as long as bad has at least ONE valid node */
1395                                 set_irn_node(bad, node);
1396                                 node->node = bad;
1397                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1398                                 exchange(irn, bad);
1399                         }
1400                 }
1401                 else if (get_irn_mode(irn) == mode_X) {
1402                         if (node->type.tv == tarval_unreachable) {
1403                                 ir_node *bad = get_irg_bad(current_ir_graph);
1404
1405                                 /* see comment above */
1406                                 set_irn_node(bad, node);
1407                                 node->node = bad;
1408                                 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1409                                 exchange(irn, bad);
1410                         }
1411                         else if (is_Proj(irn)) {
1412                                 /* leave or Jmp */
1413                                 ir_node *cond = get_Proj_pred(irn);
1414
1415                                 if (is_Cond(cond)) {
1416                                         node_t *sel = get_irn_node(get_Cond_selector(cond));
1417
1418                                         if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1419                                                 /* Cond selector is a constant, make a Jmp */
1420                                                 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1421                                                 set_irn_node(jmp, node);
1422                                                 node->node = jmp;
1423                                                 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1424                                                 exchange(irn, jmp);
1425                                         }
1426                                 }
1427                         }
1428                 } else {
1429                         /* normal data node */
1430                         if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1431                                 tarval *tv = node->type.tv;
1432
1433                                 if (! is_Const(irn)) {
1434                                         /* can be replaced by a constant */
1435                                         ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1436                                         set_irn_node(c, node);
1437                                         node->node = c;
1438                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1439                                         exchange(irn, c);
1440                                 }
1441                         } else if (is_entity(node->type.sym.entity_p)) {
1442                                 if (! is_SymConst(irn)) {
1443                                         /* can be replaced by a Symconst */
1444                                         ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1445                                         set_irn_node(symc, node);
1446                                         node->node = symc;
1447
1448                                         DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1449                                         exchange(irn, symc);
1450                                 }
1451                         } else {
1452                                 ir_node *leader = get_leader(node);
1453
1454                                 if (leader != irn) {
1455                                         DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
1456                                         exchange(irn, leader);
1457                                 }
1458                         }
1459                 }
1460         }
1461 }  /* static void apply_result(ir_node *irn, void *ctx) {
1462  */
1463
1464 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1465
1466 /**
1467  * sets the generic functions to compute.
1468  */
1469 static void set_compute_functions(void) {
1470         int i;
1471
1472         /* set the default compute function */
1473         for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1474                 ir_op *op = get_irp_opcode(i);
1475                 op->ops.generic = (op_func)default_compute;
1476         }
1477
1478         /* set specific functions */
1479         SET(Block);
1480         SET(Jmp);
1481         SET(Phi);
1482         SET(Add);
1483         SET(Sub);
1484         SET(SymConst);
1485         SET(Proj);
1486         SET(End);
1487 }  /* set_compute_functions */
1488
1489 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1490         ir_node *irn = local != NULL ? local : n;
1491         node_t *node = get_irn_node(irn);
1492
1493         ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1494         return 1;
1495 }
1496
1497 void combo(ir_graph *irg) {
1498         environment_t env;
1499         ir_node       *initial_X;
1500         node_t        *start;
1501         ir_graph      *rem = current_ir_graph;
1502
1503         current_ir_graph = irg;
1504
1505         /* register a debug mask */
1506         FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1507         firm_dbg_set_mask(dbg, SET_LEVEL_3);
1508
1509         DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1510
1511         obstack_init(&env.obst);
1512         env.worklist       = NULL;
1513         env.cprop          = NULL;
1514         env.touched        = NULL;
1515         env.initial        = NULL;
1516 #ifdef DEBUG_libfirm
1517         env.dbg_list       = NULL;
1518 #endif
1519         env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
1520         env.type2id_map    = pmap_create();
1521         env.end_idx        = get_opt_global_cse() ? 0 : -1;
1522         env.lambda_input   = 0;
1523
1524         assure_irg_outs(irg);
1525
1526         /* we have our own value_of function */
1527         set_value_of_func(get_node_tarval);
1528
1529         set_compute_functions();
1530         DEBUG_ONLY(part_nr = 0);
1531
1532         /* create the initial partition and place it on the work list */
1533         env.initial = new_partition(&env);
1534         add_to_worklist(env.initial, &env);
1535         irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
1536
1537         /* Place the START Node's partition on cprop.
1538            Place the START Node on its local worklist. */
1539         initial_X = get_irg_initial_exec(irg);
1540         start     = get_irn_node(initial_X);
1541         add_node_to_cprop(start, &env);
1542
1543         do {
1544                 propagate(&env);
1545                 if (env.worklist != NULL)
1546                         cause_splits(&env);
1547         } while (env.cprop != NULL || env.worklist != NULL);
1548
1549         dump_all_partitions(&env);
1550
1551         set_dump_node_vcgattr_hook(dump_partition_hook);
1552         dump_ir_block_graph(irg, "-partition");
1553         set_dump_node_vcgattr_hook(NULL);
1554
1555
1556         /* apply the result */
1557         irg_walk_graph(irg, NULL, apply_result, &env);
1558
1559         pmap_destroy(env.type2id_map);
1560         del_set(env.opcode2id_map);
1561         obstack_free(&env.obst, NULL);
1562
1563         /* restore value_of() default behavior */
1564         set_value_of_func(NULL);
1565         current_ir_graph = rem;
1566 }  /* combo */