2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Cliff Click's Combined Analysis/Optimization
23 * @author Michael Beck
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".
36 #include "iroptimize.h"
43 #include "irgraph_t.h"
54 /* we need the tarval_R and tarval_U */
55 #define tarval_R tarval_top
56 #define tarval_U tarval_bottom
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;
64 /** The type of the compute function. */
65 typedef void (*compute_func)(node_t *node);
71 ir_opcode code; /**< The Firm opcode. */
72 ir_mode *mode; /**< The mode of all nodes in the partition. */
76 * An entry in the opcode map.
78 struct opcode_entry_t {
79 opcode_key_t key; /**< The key. */
80 partition_t *part; /**< The associated partition. */
84 * An entry in the list_map.
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. */
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. */
99 * A lattice element. Because we handle constants and symbolic constants different, we
100 * have to use this union.
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. */
122 * A partition containing congruent nodes.
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. */
138 partition_t *dbg_next; /**< Link all partitions for debugging */
139 unsigned nr; /**< A unique number for (what-)mapping, >0. */
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. */
150 partition_t *dbg_list; /**< List of all partitions. */
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(). */
159 /** Type of the what function. */
160 typedef void *(*what_func)(const node_t *node, environment_t *env);
162 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
163 #define set_irn_node(irn, node) set_irn_link(irn, node)
165 /** The debug module handle. */
166 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
168 /** Next partition number. */
169 DEBUG_ONLY(static unsigned part_nr = 0);
173 * Dump partition to output.
175 static void dump_partition(const char *msg, partition_t *part) {
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));
184 DB((dbg, LEVEL_2, "\n}\n"));
188 * Dump all partitions.
190 static void dump_all_partitions(environment_t *env) {
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);
199 #define dump_partition(msg, part)
200 #define dump_all_partitions(env)
204 * Compare two pointer values of a listmap.
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;
210 return e1->id != e2->id;
211 } /* listmap_cmp_ptr */
214 * Initializes a listmap.
216 * @param map the listmap
218 static void listmap_init(listmap_t *map) {
219 map->map = new_set(listmap_cmp_ptr, 16);
224 * Terminates a listmap.
226 * @param map the listmap
228 static void listmap_term(listmap_t *map) {
233 * Return the associated listmap entry for a given id.
235 * @param map the listmap
236 * @param id the id to search for
238 * @return the asociated listmap entry for the given id
240 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
241 listmap_entry_t key, *entry;
246 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
248 if (entry->list == NULL) {
249 /* a new entry, put into the list */
250 entry->next = map->values;
257 * Calculate the hash value for an opcode map entry.
259 * @param entry an opcode map entry
261 * @return a hash value for the given opcode map entry
263 static unsigned opcode_hash(const opcode_key_t *entry) {
264 return (entry->mode - (ir_mode *)0) * 9 + entry->code;
268 * Compare two entries in the opcode map.
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;
274 return o1->code != o2->code || o1->mode != o2->mode;
278 * Return the type of a node.
280 * @param irn an IR-node
282 * @return the associated type of this node
284 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
285 return get_irn_node(irn)->type;
286 } /* get_node_type */
289 * Return the tarval of a node.
291 * @param irn an IR-node
293 * @return the associated type of this node
295 static INLINE tarval *get_node_tarval(const ir_node *irn) {
296 lattice_elem_t type = get_node_type(irn);
298 if (is_tarval(type.tv))
300 return tarval_bottom;
301 } /* get_node_type */
305 * Create a new empty partition.
307 * @param env the environment
309 * @return a newly allocated partition
311 static INLINE partition_t *new_partition(environment_t *env) {
312 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
314 INIT_LIST_HEAD(&part->entries);
316 part->wl_next = env->worklist;
317 part->touched_next = NULL;
318 part->cprop_next = NULL;
319 part->touched = NULL;
323 part->on_worklist = 0;
324 part->on_touched = 0;
327 part->dbg_next = env->dbg_list;
328 env->dbg_list = part;
329 part->nr = part_nr++;
333 } /* new_partition */
336 * Get the partition for a given IR-node.
338 * @param irn the IR-node
339 * @param env the environment
341 * @return the partition where irn lies
343 static INLINE partition_t *get_partition_for_irn(const ir_node *irn, environment_t *env) {
344 opcode_entry_t key, *entry;
347 key.key.code = get_irn_opcode(irn);
348 key.key.mode = get_irn_mode(irn);
349 hash = opcode_hash(&key.key);
351 entry = set_find(env->opcode_map, &key, sizeof(key), hash);
353 /* create a new partition and place it on the wait queue */
354 partition_t *part = new_partition(env);
356 part->on_worklist = 1;
357 env->worklist = part;
360 set_insert(env->opcode_map, &key, sizeof(key), hash);
364 } /* get_partition_for_irn */
367 * Return the type of a partition (assuming partition is non-empty and
368 * all elements have the same type).
370 * @param X a partition
372 * @return the type of the first element of the partition
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);
377 } /* get_partition_type */
380 * Creates a partition node for the given IR-node and place it
381 * into the given partition.
383 * @param irn an IR-node
384 * @param part a partition to place the node in
385 * @param env the environment
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));
391 INIT_LIST_HEAD(&node->node_list);
394 node->cprop_next = NULL;
396 node->type.tv = tarval_bottom; /* == tarval_U */
397 node->on_touched = 0;
399 set_irn_node(irn, node);
401 list_add_tail(&node->node_list, &part->entries);
404 DB((dbg, LEVEL_2, "Placing %+F in partition %u\n", irn, part->nr));
405 } /* create_partition_node */
408 * Walker, initialize all Nodes' type to U or top and place
409 * all nodes into the TOP partition.
411 static void create_initial_partitions(ir_node *irn, void *ctx) {
412 environment_t *env = ctx;
413 partition_t *part = env->TOP;
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 */
423 * Add a partition to the touched set if not already there.
425 * @param part the partition
426 * @param env the environment
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;
432 part->on_touched = 1;
434 } /* add_to_touched */
437 * Add a node to the entry.partition.touched set if not already there.
441 static INLINE void add_to_partition_touched(node_t *y) {
442 if (y->on_touched == 0) {
443 partition_t *part = y->part;
445 y->next = part->touched;
450 } /* add_to_partition_touched */
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.
456 * @param Z the Z partition
457 * @param Z_prime the Z' partition, a previous part of Z
458 * @param env the environment
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;
467 Z->wl_next = env->worklist;
470 } /* update_worklist */
473 * Split a partition by a local list.
475 * @param Z the Z partition to split
476 * @param g a (non-empty) node list
477 * @param env the environment
479 * @return a new partition containing the nodes of g
481 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
482 partition_t *Z_prime;
487 dump_partition("Splitting ", Z);
491 /* Remove g from Z. */
492 for (node = g; node != NULL; node = node->next) {
493 list_del(&node->node_list);
496 assert(n < Z->n_nodes);
499 /* Move g to a new partition, Z
\92. */
500 Z_prime = new_partition(env);
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)
509 Z_prime->n_inputs = n_inputs;
510 Z_prime->n_nodes = n;
512 update_worklist(Z, Z_prime, env);
514 dump_partition("Now ", Z);
515 dump_partition("Created new ", Z_prime);
520 * Returns non-zero if the i'th input of a Phi node is live.
522 * @param phi a Phi-node
523 * @param i an input number
525 * @return non-zero if the i'th input of the given Phi node is live
527 static int is_live_input(ir_node *phi, int i) {
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);
533 return type.tv != tarval_U;
535 /* else it's the control input, always live */
537 } /* is_live_input */
540 * Split the partitions if caused by the first entry on the worklist.
542 * @param env the environment
544 static void cause_splits(environment_t *env) {
545 partition_t *X, *Y, *Z;
549 /* remove the first partition from the worklist */
551 env->worklist = X->wl_next;
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 */
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)
566 /* non-existing input */
567 if (i >= get_irn_arity(x->node))
570 y = get_irn_node(get_irn_n(x->node, i));
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);
582 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
583 /* remove it from the touched set */
586 if (Z->n_nodes != Z->n_touched) {
587 split(Z, Z->touched, env);
589 /* Empty local Z.touched. */
590 for (e = Z->touched; e != NULL; e = e->next) {
600 * Implements split_by_what(): Split a partition by characteristics given
601 * by the what function.
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
608 * @return if P != NULL P will be filled with the resulting partitions and returned
610 static partition_t **split_by_what(partition_t *X, what_func What,
611 partition_t **P, environment_t *env) {
614 listmap_entry_t *iter;
617 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
619 list_for_each_entry(node_t, x, &X->entries, node_list) {
620 void *id = What(x, env);
621 listmap_entry_t *entry;
624 /* input not allowed, ignore */
627 /* Add x to map[What(x)]. */
628 entry = listmap_find(&map, id);
629 x->next = entry->list;
632 /* Let P be a set of Partitions. */
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 */
642 /* Add SPLIT( X, S ) to P. */
643 R = split(X, S, env);
645 ARR_APP1(partition_t *, P, R);
650 ARR_APP1(partition_t *, P, X);
655 } /* split_by_what */
657 /** lambda n.(n.type) */
658 static void *lambda_type(const node_t *node, environment_t *env) {
660 return node->type.tv;
663 /** lambda n.(n.opcode) */
664 static void *lambda_opcode(const node_t *node, environment_t *env) {
665 opcode_key_t key, *entry;
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));
671 } /* lambda_opcode */
673 /** lambda n.(n[i].partition) */
674 static void *lambda_partition(const node_t *node, environment_t *env) {
677 int i = env->lambda_input;
679 if (i >= get_irn_arity(node->node)) {
680 /* we are outside the allowed range */
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)
689 pred = get_irn_n(node->node, i);
690 p = get_irn_node(pred);
693 } /* lambda_partition */
696 * Implements split_by().
698 * @param X the partition to split
699 * @param env the environment
701 static void split_by(partition_t *X, environment_t *env) {
702 partition_t **P = NEW_ARR_F(partition_t *, 0);
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];
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);
713 Q = split_by_what(Y, lambda_opcode, Q, env);
715 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
716 partition_t *Z = Q[j];
718 for (k = Z->n_inputs - 1; k >= -1; --k) {
719 env->lambda_input = k;
720 split_by_what(Z, lambda_partition, NULL, env);
730 * (Re-)compute the type for a given node.
732 * @param node the node
734 static void default_compute(node_t *node) {
736 ir_node *irn = node->node;
738 if (get_irn_pinned(irn) == op_pin_state_pinned) {
739 node_t *block = get_irn_node(get_nodes_block(irn));
741 if (block->type.tv == tarval_U) {
742 node->type.tv = tarval_top;
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);
752 if (p->type.tv == tarval_top) {
753 node->type.tv = tarval_top;
758 node->type.tv = computed_value(irn);
759 } /* default_compute */
762 * (Re-)compute the type for a Block node.
764 * @param node the node
766 static void compute_Block(node_t *node) {
768 ir_node *block = node->node;
770 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
771 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
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;
779 node->type.tv = tarval_U;
780 } /* compute_Block */
783 * (Re-)compute the type for a Jmp node.
785 * @param node the node
787 static void compute_Jmp(node_t *node) {
788 node_t *block = get_irn_node(get_nodes_block(node->node));
790 node->type = block->type;
794 * (Re-)compute the type for the End node.
796 * @param node the node
798 static void compute_End(node_t *node) {
799 /* the End node is NOT dead of course */
800 node->type.tv = tarval_R;
804 * (Re-)compute the type for a SymConst node.
806 * @param node the node
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));
812 if (block->type.tv == tarval_U) {
813 node->type.tv = tarval_top;
816 switch (get_SymConst_kind(irn)) {
817 case symconst_addr_ent:
818 case symconst_addr_name:
819 node->type.sym = get_SymConst_symbol(irn);
822 node->type.tv = computed_value(irn);
824 } /* compute_SymConst */
827 * (Re-)compute the type for a Phi node.
829 * @param node the node
831 static void compute_Phi(node_t *node) {
833 ir_node *phi = node->node;
836 /* if a Phi is in a unreachable block, its type is TOP */
837 node_t *block = get_irn_node(get_nodes_block(phi));
839 if (block->type.tv == tarval_U) {
840 node->type.tv = tarval_top;
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));
849 if (pred->type.tv == tarval_top) {
850 /* ignore TOP inputs */
853 if (pred->type.tv == tarval_bottom) {
854 node->type.tv = tarval_bottom;
856 } else if (type.tv == tarval_top) {
857 /* first constant found */
859 } else if (type.tv == pred->type.tv) {
860 /* same constant, continue */
863 /* different constants or tarval_bottom */
864 node->type.tv = tarval_bottom;
872 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
874 * @param node the node
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));
885 if (block->type.tv == tarval_U) {
886 node->type.tv = tarval_top;
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;
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);
902 mode = get_tarval_mode(a.tv);
903 if (a.tv == get_mode_null(mode)) {
907 } else if (is_tarval(b.tv)) {
908 mode = get_tarval_mode(b.tv);
909 if (b.tv == get_mode_null(mode)) {
914 node->type.tv = tarval_bottom;
919 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
921 * @param node the node
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));
931 if (block->type.tv == tarval_U) {
932 node->type.tv = tarval_top;
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;
944 if (is_tarval(a.tv) && is_tarval(b.tv))
945 node->type.tv = tarval_sub(a.tv, b.tv);
947 node->type.tv = tarval_bottom;
952 * (Re-)compute the type for a Proj(Cmp).
954 * @param node the node
955 * @param cond the predecessor Cmp node
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);
966 * BEWARE: a == a is NOT always True for floating Point values, as
967 * NaN != NaN is defined, so we must check this here.
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;
977 default_compute(node);
980 default_compute(node);
982 } /* compute_Proj_Cmp */
985 * (Re-)compute the type for a Proj(Cond).
987 * @param node the node
988 * @param cond the predecessor Cond node
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);
996 if (get_irn_mode(sel) == mode_b) {
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;
1006 assert(selector->type.tv == tarval_top);
1007 node->type.tv = tarval_U;
1010 assert(pnc == pn_Cond_false);
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;
1019 assert(selector->type.tv == tarval_top);
1020 node->type.tv = tarval_U;
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;
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 */
1035 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1036 ir_node *succ = get_irn_out(cond, i);
1040 if (value == get_Proj_proj(succ)) {
1041 /* we found a match, will NOT take the default case */
1042 node->type.tv = tarval_U;
1046 /* all cases checked, no match, will take default case */
1047 node->type.tv = tarval_R;
1050 node->type.tv = value == pnc ? tarval_R : tarval_U;
1054 } /* compute_Proj_Cond */
1057 * (Re-)compute the type for a Proj-Nodes.
1059 * @param node the node
1061 static void compute_Proj(node_t *node) {
1062 ir_node *proj = node->node;
1063 ir_mode *mode = get_irn_mode(proj);
1066 if (mode == mode_M) {
1067 /* mode M is always bottom */
1068 node->type.tv = tarval_bottom;
1071 if (mode != mode_X) {
1072 ir_node *cmp = get_Proj_pred(proj);
1074 compute_Proj_Cmp(node, cmp);
1076 default_compute(node);
1079 /* handle mode_X nodes */
1080 pred = get_Proj_pred(proj);
1082 switch (get_irn_opcode(pred)) {
1084 /* the Proj_X from the Start is always reachable */
1085 node->type.tv = tarval_R;
1088 compute_Proj_Cond(node, pred);
1091 default_compute(node);
1093 } /* compute_Proj */
1096 * (Re-)compute the type for a given node.
1098 * @param node the node
1100 static void compute(node_t *node) {
1101 compute_func func = (compute_func)node->node->op->ops.generic;
1108 * Place a node on the cprop list.
1111 * @param env the environment
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;
1118 y->cprop_next = Y->cprop;
1122 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
1124 /* place its partition on the cprop list */
1125 if (Y->on_cprop == 0) {
1126 Y->cprop_next = env->cprop;
1134 * Propagate constant evaluation.
1136 * @param env the environment
1138 static void propagate(environment_t *env) {
1141 lattice_elem_t old_type;
1142 node_t *fallen = NULL;
1143 unsigned n_fallen = 0;
1146 while (env->cprop != NULL) {
1147 /* remove the first partition X from cprop but do not set the bit here */
1149 env->cprop = X->cprop_next;
1151 dump_partition("Propagate", X);
1153 /* remove the first Node x from X.cprop but do NOT set the bit here */
1155 X->cprop = x->cprop_next;
1157 /* compute a new type for x */
1159 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
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. */
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);
1172 /* Add y to y.partition.cprop. */
1173 add_node_to_cprop(y, env);
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) */
1179 } while (X->cprop != NULL);
1181 /* now remove X from cprop, we have emptied it's local list */
1184 if (n_fallen != X->n_nodes) {
1185 assert(n_fallen > 0);
1186 Y = split(X, fallen, env);
1195 * Get the leader for a given node from its congruence class.
1197 * @param irn the node
1199 static ir_node *get_leader(ir_node *irn) {
1200 partition_t *part = get_irn_node(irn)->part;
1202 if (part->n_nodes > 1) {
1203 DB((dbg, LEVEL_2, "Found congruence class for %+F ", irn));
1204 dump_partition("", part);
1210 * Post-Walker, apply the analysis results;
1212 static void apply_result(ir_node *irn, void *ctx) {
1213 environment_t *env = ctx;
1215 if (is_no_Block(irn)) {
1216 ir_node *leader = get_leader(irn);
1218 if (leader != irn) {
1219 exchange(irn, leader);
1222 } /* static void apply_result(ir_node *irn, void *ctx) {
1225 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1228 * sets the generic functions to compute.
1230 static void set_compute_functions(void) {
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;
1239 /* set specific functions */
1248 } /* set_compute_functions */
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);
1254 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1258 void combo(ir_graph *irg) {
1262 ir_graph *rem = current_ir_graph;
1264 current_ir_graph = irg;
1266 /* register a debug mask */
1267 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1268 firm_dbg_set_mask(dbg, SET_LEVEL_3);
1270 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1272 obstack_init(&env.obst);
1273 env.worklist = NULL;
1277 #ifdef DEBUG_libfirm
1278 env.dbg_list = NULL;
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;
1286 assure_irg_outs(irg);
1288 /* we have our own value_of function */
1289 set_value_of_func(get_node_tarval);
1291 set_compute_functions();
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);
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);
1307 if (env.worklist != NULL)
1309 } while (env.cprop != NULL || env.worklist != NULL);
1311 dump_all_partitions(&env);
1313 set_dump_node_vcgattr_hook(dump_partition_hook);
1314 dump_ir_block_graph(irg, "-partition");
1317 /* apply the result */
1318 irg_walk_graph(irg, NULL, apply_result, &env);
1320 pmap_destroy(env.type2id_map);
1321 del_set(env.opcode_map);
1322 del_set(env.opcode2id_map);
1323 obstack_free(&env.obst, NULL);
1325 /* restore value_of() default behavior */
1326 set_value_of_func(NULL);
1327 current_ir_graph = rem;