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"
52 /* we need the tarval_R and tarval_U */
53 #define tarval_R tarval_top
54 #define tarval_U tarval_bottom
56 typedef struct node_t node_t;
57 typedef struct partition_t partition_t;
58 typedef struct opcode_key_t opcode_key_t;
59 typedef struct opcode_entry_t opcode_entry_t;
60 typedef struct listmap_entry_t listmap_entry_t;
62 /** The type of the compute function. */
63 typedef void (*compute_func)(node_t *node);
69 ir_opcode code; /**< The Firm opcode. */
70 ir_mode *mode; /**< The mode of all nodes in the partition. */
74 * An entry in the opcode map.
76 struct opcode_entry_t {
77 opcode_key_t key; /**< The key. */
78 partition_t *part; /**< The associated partition. */
82 * An entry in the list_map.
84 struct listmap_entry_t {
85 void *id; /**< The id. */
86 node_t *list; /**< The associated list for this id. */
87 listmap_entry_t *next; /**< Link to the next entry in the map. */
90 /** We must map id's to lists. */
91 typedef struct listmap_t {
92 set *map; /**< Map id's to listmap_entry_t's */
93 listmap_entry_t *values; /**< List of all values in the map. */
97 * A lattice element. Because we handle constants and symbolic constants different, we
98 * have to use this union.
109 ir_node *node; /**< The IR-node itself. */
110 list_head node_list; /**< Double-linked list of entries. */
111 partition_t *part; /**< points to the partition this node belongs to */
112 node_t *cprop_next; /**< Next node on partition.cprop list. */
113 node_t *next; /**< Next node on local list (partition.touched, fallen). */
114 lattice_elem_t type; /**< The associated lattice element "type". */
115 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
116 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
120 * A partition containing congruent nodes.
123 list_head entries; /**< The head of partition node list. */
124 node_t *cprop; /**< The partition.cprop list. */
125 partition_t *wl_next; /**< Next entry in the work list if any. */
126 partition_t *touched_next; /**< Points to the next partition in the touched set. */
127 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
128 node_t *touched; /**< The partition.touched set of this partition. */
129 unsigned n_nodes; /**< Number of entries in this partition. */
130 unsigned n_touched; /**< Number of entries in the partition.touched. */
131 int n_inputs; /**< Maximum number of inputs of all entries. */
132 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
133 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
134 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
136 partition_t *dbg_next; /**< Link all partitions for debugging */
137 unsigned nr; /**< A unique number for (what-)mapping, >0. */
141 typedef struct environment_t {
142 struct obstack obst; /**< obstack to allocate data structures. */
143 partition_t *worklist; /**< The work list. */
144 partition_t *cprop; /**< The constant propagation list. */
145 partition_t *touched; /**< the touched set. */
146 partition_t *TOP; /**< The TOP partition. */
148 partition_t *dbg_list; /**< List of all partitions. */
150 set *opcode_map; /**< The initial opcode->partition map. */
151 set *opcode2id_map; /**< The opcodeMode->id map. */
152 pmap *type2id_map; /**< The type->id map. */
153 int end_idx; /**< -1 for local and 0 for global congruences. */
154 int lambda_input; /**< Captured argument for lambda_partition(). */
157 /** Type of the what function. */
158 typedef void *(*what_func)(const node_t *node, environment_t *env);
160 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
161 #define set_irn_node(irn, node) set_irn_link(irn, node)
163 /** The debug module handle. */
164 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
166 /** Next partition number. */
167 DEBUG_ONLY(static unsigned part_nr = 0);
171 * Dump partition to output.
173 static void dump_partition(const char *msg, partition_t *part) {
177 DB((dbg, LEVEL_2, "%s part%u (%u) {\n ", msg, part->nr, part->n_nodes));
178 list_for_each_entry(node_t, node, &part->entries, node_list) {
179 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
182 DB((dbg, LEVEL_2, "\n}\n"));
186 * Dump all partitions.
188 static void dump_all_partitions(environment_t *env) {
191 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
192 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
193 dump_partition("", P);
197 #define dump_partition(msg, part)
198 #define dump_all_partitions(env)
202 * Compare two pointer values of a listmap.
204 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
205 const listmap_entry_t *e1 = elt;
206 const listmap_entry_t *e2 = key;
208 return e1->id != e2->id;
209 } /* listmap_cmp_ptr */
212 * Initializes a listmap.
214 * @param map the listmap
216 static void listmap_init(listmap_t *map) {
217 map->map = new_set(listmap_cmp_ptr, 16);
222 * Terminates a listmap.
224 * @param map the listmap
226 static void listmap_term(listmap_t *map) {
231 * Return the associated listmap entry for a given id.
233 * @param map the listmap
234 * @param id the id to search for
236 * @return the asociated listmap entry for the given id
238 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
239 listmap_entry_t key, *entry;
244 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
246 if (entry->list == NULL) {
247 /* a new entry, put into the list */
248 entry->next = map->values;
255 * Calculate the hash value for an opcode map entry.
257 * @param entry an opcode map entry
259 * @return a hash value for the given opcode map entry
261 static unsigned opcode_hash(const opcode_key_t *entry) {
262 return (entry->mode - (ir_mode *)0) * 9 + entry->code;
266 * Compare two entries in the opcode map.
268 static int cmp_opcode(const void *elt, const void *key, size_t size) {
269 const opcode_key_t *o1 = elt;
270 const opcode_key_t *o2 = key;
272 return o1->code != o2->code || o1->mode != o2->mode;
276 * Return the type of a node.
278 * @param irn an IR-node
280 * @return the associated type of this node
282 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
283 return get_irn_node(irn)->type;
284 } /* get_node_type */
287 * Return the tarval of a node.
289 * @param irn an IR-node
291 * @return the associated type of this node
293 static INLINE tarval *get_node_tarval(const ir_node *irn) {
294 lattice_elem_t type = get_node_type(irn);
296 if (get_kind(type.tv) == k_tarval)
298 return tarval_bottom;
299 } /* get_node_type */
303 * Create a new empty partition.
305 * @param env the environment
307 * @return a newly allocated partition
309 static INLINE partition_t *new_partition(environment_t *env) {
310 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
312 INIT_LIST_HEAD(&part->entries);
314 part->wl_next = env->worklist;
315 part->touched_next = NULL;
316 part->cprop_next = NULL;
317 part->touched = NULL;
321 part->on_worklist = 0;
322 part->on_touched = 0;
325 part->dbg_next = env->dbg_list;
326 env->dbg_list = part;
327 part->nr = part_nr++;
331 } /* new_partition */
334 * Get the partition for a given IR-node.
336 * @param irn the IR-node
337 * @param env the environment
339 * @return the partition where irn lies
341 static INLINE partition_t *get_partition_for_irn(const ir_node *irn, environment_t *env) {
342 opcode_entry_t key, *entry;
345 key.key.code = get_irn_opcode(irn);
346 key.key.mode = get_irn_mode(irn);
347 hash = opcode_hash(&key.key);
349 entry = set_find(env->opcode_map, &key, sizeof(key), hash);
351 /* create a new partition and place it on the wait queue */
352 partition_t *part = new_partition(env);
354 part->on_worklist = 1;
355 env->worklist = part;
358 set_insert(env->opcode_map, &key, sizeof(key), hash);
362 } /* get_partition_for_irn */
365 * Return the type of a partition (assuming partition is non-empty and
366 * all elements have the same type).
368 * @param X a partition
370 * @return the type of the first element of the partition
372 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
373 const node_t *first = list_entry(X->entries.next, node_t, node_list);
375 } /* get_partition_type */
378 * Creates a partition node for the given IR-node and place it
379 * into the given partition.
381 * @param irn an IR-node
382 * @param part a partition to place the node in
383 * @param env the environment
385 static void create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
386 /* create a partition node and place it in the partition */
387 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
389 INIT_LIST_HEAD(&node->node_list);
392 node->cprop_next = NULL;
394 node->type.tv = tarval_bottom; /* == tarval_U */
395 node->on_touched = 0;
397 set_irn_node(irn, node);
399 list_add_tail(&node->node_list, &part->entries);
402 DB((dbg, LEVEL_2, "Placing %+F in partition %u\n", irn, part->nr));
403 } /* create_partition_node */
406 * Walker, initialize all Nodes' type to U or top and place
407 * all nodes into the TOP partition.
409 static void create_initial_partitions(ir_node *irn, void *ctx) {
410 environment_t *env = ctx;
411 partition_t *part = env->TOP;
414 create_partition_node(irn, part, env);
415 arity = get_irn_arity(irn);
416 if (arity > part->n_inputs)
417 part->n_inputs = arity;
418 } /* create_initial_partitions */
421 * Add a partition to the touched set if not already there.
423 * @param part the partition
424 * @param env the environment
426 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
427 if (part->on_touched == 0) {
428 part->touched_next = env->touched;
430 part->on_touched = 1;
432 } /* add_to_touched */
435 * Add a node to the entry.partition.touched set if not already there.
439 static INLINE void add_to_partition_touched(node_t *y) {
440 if (y->on_touched == 0) {
441 partition_t *part = y->part;
443 y->next = part->touched;
448 } /* add_to_partition_touched */
451 * Update the worklist: If Z is on worklist then add Z' to worklist.
452 * Else add the smaller of Z and Z' to worklist.
454 * @param Z the Z partition
455 * @param Z_prime the Z' partition, a previous part of Z
456 * @param env the environment
458 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
459 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
460 Z_prime->on_worklist = 1;
461 Z_prime->wl_next = env->worklist;
462 env->worklist = Z_prime;
465 Z->wl_next = env->worklist;
468 } /* update_worklist */
471 * Split a partition by a local list.
473 * @param Z the Z partition to split
474 * @param g a (non-empty) node list
475 * @param env the environment
477 * @return a new partition containing the nodes of g
479 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
480 partition_t *Z_prime;
485 dump_partition("Splitting ", Z);
489 /* Remove g from Z. */
490 for (node = g; node != NULL; node = node->next) {
491 list_del(&node->node_list);
494 assert(n < Z->n_nodes);
497 /* Move g to a new partition, Z
\92. */
498 Z_prime = new_partition(env);
500 for (node = g; node != NULL; node = node->next) {
501 int arity = get_irn_arity(node->node);
502 list_add(&node->node_list, &Z_prime->entries);
503 node->part = Z_prime;
504 if (arity > n_inputs)
507 Z_prime->n_inputs = n_inputs;
508 Z_prime->n_nodes = n;
510 update_worklist(Z, Z_prime, env);
512 dump_partition("Now ", Z);
513 dump_partition("Created new ", Z_prime);
518 * Returns non-zero if the i'th input of a Phi node is live.
520 * @param phi a Phi-node
521 * @param i an input number
523 * @return non-zero if the i'th input of the given Phi node is live
525 static int is_live_input(ir_node *phi, int i) {
527 ir_node *block = get_nodes_block(phi);
528 ir_node *pred = get_Block_cfgpred(block, i);
529 lattice_elem_t type = get_node_type(pred);
531 return type.tv != tarval_U;
533 /* else it's the control input, always live */
535 } /* is_live_input */
538 * Split the partitions if caused by the first entry on the worklist.
540 * @param env the environment
542 static void cause_splits(environment_t *env) {
543 partition_t *X, *Y, *Z;
547 /* remove the first partition from the worklist */
549 env->worklist = X->wl_next;
552 dump_partition("Cause_split: ", X);
553 end_idx = env->end_idx;
554 for (i = X->n_inputs - 1; i >= -1; --i) {
555 /* empty the touched set: already done, just clear the list */
558 list_for_each_entry(node_t, x, &X->entries, node_list) {
559 /* ignore the "control input" for non-pinned nodes
560 if we are running in GCSE mode */
561 if (i < end_idx && get_irn_pinned(x->node) != op_pin_state_pinned)
564 /* non-existing input */
565 if (i >= get_irn_arity(x->node))
568 y = get_irn_node(get_irn_n(x->node, i));
571 /* Partitions of constants should not be split simply because their Nodes have unequal
572 functions or incongruent inputs. */
573 if (get_partition_type(Y).tv != tarval_bottom &&
574 (! is_Phi(x->node) || is_live_input(x->node, i))) {
575 add_to_touched(Y, env);
576 add_to_partition_touched(y);
580 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
581 /* remove it from the touched set */
584 if (Z->n_nodes != Z->n_touched) {
585 split(Z, Z->touched, env);
587 /* Empty local Z.touched. */
588 for (e = Z->touched; e != NULL; e = e->next) {
598 * Implements split_by_what(): Split a partition by characteristics given
599 * by the what function.
601 * @param X the partition to split
602 * @param What a function returning an Id for every node of the partition X
603 * @param P an flexible array to store the result partitions or NULL
604 * @param env the environment
606 * @return if P != NULL P will be filled with the resulting partitions and returned
608 static partition_t **split_by_what(partition_t *X, what_func What,
609 partition_t **P, environment_t *env) {
612 listmap_entry_t *iter;
615 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
617 list_for_each_entry(node_t, x, &X->entries, node_list) {
618 void *id = What(x, env);
619 listmap_entry_t *entry;
622 /* input not allowed, ignore */
625 /* Add x to map[What(x)]. */
626 entry = listmap_find(&map, id);
627 x->next = entry->list;
630 /* Let P be a set of Partitions. */
632 /* for all sets S except one in the range of map do */
633 for (iter = map.values; iter != NULL; iter = iter->next) {
634 if (iter->next == NULL) {
635 /* this is the last entry, ignore */
640 /* Add SPLIT( X, S ) to P. */
641 R = split(X, S, env);
643 ARR_APP1(partition_t *, P, R);
648 ARR_APP1(partition_t *, P, X);
653 } /* split_by_what */
655 /** lambda n.(n.type) */
656 static void *lambda_type(const node_t *node, environment_t *env) {
658 return node->type.tv;
661 /** lambda n.(n.opcode) */
662 static void *lambda_opcode(const node_t *node, environment_t *env) {
663 opcode_key_t key, *entry;
665 key.code = get_irn_opcode(node->node);
666 key.mode = get_irn_mode(node->node);
667 entry = set_insert(env->opcode2id_map, &key, sizeof(&key), opcode_hash(&key));
669 } /* lambda_opcode */
671 /** lambda n.(n[i].partition) */
672 static void *lambda_partition(const node_t *node, environment_t *env) {
675 int i = env->lambda_input;
677 if (i >= get_irn_arity(node->node)) {
678 /* we are outside the allowed range */
682 /* ignore the "control input" for non-pinned nodes
683 if we are running in GCSE mode */
684 if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
687 pred = get_irn_n(node->node, i);
688 p = get_irn_node(pred);
691 } /* lambda_partition */
694 * Implements split_by().
696 * @param X the partition to split
697 * @param env the environment
699 static void split_by(partition_t *X, environment_t *env) {
700 partition_t **P = NEW_ARR_F(partition_t *, 0);
703 P = split_by_what(X, lambda_type, P, env);
704 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
705 partition_t *Y = P[i];
707 /* we do not want split the TOP or constant partitions */
708 if (get_partition_type(Y).tv == tarval_bottom) {
709 partition_t **Q = NEW_ARR_F(partition_t *, 0);
711 Q = split_by_what(Y, lambda_opcode, Q, env);
713 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
714 partition_t *Z = Q[j];
716 for (k = Z->n_inputs - 1; k >= -1; --k) {
717 env->lambda_input = k;
718 split_by_what(Z, lambda_partition, NULL, env);
728 * (Re-)compute the type for a given node.
730 * @param node the node
732 static void default_compute(node_t *node) {
734 ir_node *irn = node->node;
737 if (get_irn_pinned(irn) == op_pin_state_pinned) {
738 node_t *block = get_irn_node(get_nodes_block(irn));
740 if (block->type.tv == tarval_U) {
741 node->type.tv = tarval_top;
745 mode = get_irn_mode(irn);
746 if (! mode_is_data(mode))
749 /* if any of the data inputs have type top, the result is type top */
750 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
751 ir_node *pred = get_irn_n(irn, i);
752 node_t *p = get_irn_node(pred);
754 if (p->type.tv == tarval_top) {
755 node->type.tv = tarval_top;
759 node->type.tv = computed_value(irn);
760 } /* default_compute */
763 * (Re-)compute the type for a Block node.
765 * @param node the node
767 static void compute_Block(node_t *node) {
769 ir_node *block = node->node;
771 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
772 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
774 if (pred->type.tv == tarval_R) {
775 /* A block is reachable, if at least of predecessor is reachable. */
776 node->type.tv = tarval_R;
780 node->type.tv = tarval_U;
781 } /* compute_Block */
784 * (Re-)compute the type for a Jmp node.
786 * @param node the node
788 static void compute_Jmp(node_t *node) {
789 node_t *block = get_irn_node(get_nodes_block(node->node));
791 node->type = block->type;
795 * (Re-)compute the type for the End node.
797 * @param node the node
799 static void compute_End(node_t *node) {
800 /* the End node is NOT dead of course */
801 node->type.tv = tarval_R;
805 * (Re-)compute the type for a SymConst node.
807 * @param node the node
809 static void compute_SymConst(node_t *node) {
810 ir_node *irn = node->node;
811 node_t *block = get_irn_node(get_nodes_block(irn));
813 if (block->type.tv == tarval_U) {
814 node->type.tv = tarval_top;
817 switch (get_SymConst_kind(irn)) {
818 case symconst_addr_ent:
819 case symconst_addr_name:
820 node->type.sym = get_SymConst_symbol(irn);
823 node->type.tv = computed_value(irn);
825 } /* compute_SymConst */
828 * (Re-)compute the type for a Phi node.
830 * @param node the node
832 static void compute_Phi(node_t *node) {
834 ir_node *phi = node->node;
837 /* if a Phi is in a unreachable block, its type is TOP */
838 node_t *block = get_irn_node(get_nodes_block(phi));
840 if (block->type.tv == tarval_U) {
841 node->type.tv = tarval_top;
845 /* if any of the data inputs have type top, the result is type top */
846 type.tv = tarval_top;
847 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
848 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
850 if (pred->type.tv == tarval_top) {
851 /* ignore TOP inputs */
854 if (pred->type.tv == tarval_bottom) {
855 node->type.tv = tarval_bottom;
857 } else if (type.tv == tarval_top) {
858 /* first constant found */
860 } else if (type.tv == pred->type.tv) {
861 /* same constant, continue */
864 /* different constants or tarval_bottom */
865 node->type.tv = tarval_bottom;
873 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
875 * @param node the node
877 static void compute_Sub(node_t *node) {
878 ir_node *sub = node->node;
879 node_t *l = get_irn_node(get_Sub_left(sub));
880 node_t *r = get_irn_node(get_Sub_right(sub));
881 lattice_elem_t a = l->type;
882 lattice_elem_t b = r->type;
883 node_t *block = get_irn_node(get_nodes_block(sub));
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 (r->part == l->part) {
893 ir_mode *mode = get_irn_mode(sub);
894 node->type.tv = get_mode_null(mode);
895 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
896 node->type.tv = tarval_bottom;
898 if (get_kind(a.tv) == k_tarval && get_kind(b.tv)== k_tarval)
899 node->type.tv = tarval_sub(a.tv, b.tv);
901 node->type.tv = tarval_bottom;
906 * (Re-)compute the type for a Proj(Cmp).
908 * @param node the node
910 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
911 ir_node *proj = node->node;
912 node_t *l = get_irn_node(get_Cmp_left(cmp));
913 node_t *r = get_irn_node(get_Cmp_right(cmp));
914 lattice_elem_t a = l->type;
915 lattice_elem_t b = r->type;
916 pn_Cmp pnc = get_Proj_proj(proj);
919 * BEWARE: a == a is NOT always True for floating Point values, as
920 * NaN != NaN is defined, so we must check this here.
922 if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt) {
923 if (a.tv == tarval_top || b.tv == tarval_top) {
924 node->type.tv = tarval_top;
925 } else if (r->part == l->part) {
926 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
927 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
928 node->type.tv = tarval_bottom;
930 default_compute(node);
933 default_compute(node);
935 } /* compute_Proj_Cmp */
938 * (Re-)compute the type for a Proj-Nodes.
940 * @param node the node
942 static void compute_Proj(node_t *node) {
943 ir_node *proj = node->node;
944 ir_mode *mode = get_irn_mode(proj);
947 if (mode == mode_M) {
948 /* mode M is always bottom */
949 node->type.tv = tarval_bottom;
952 if (mode != mode_X) {
953 ir_node *cmp = get_Proj_pred(proj);
955 compute_Proj_Cmp(node, cmp);
957 default_compute(node);
960 /* handle mode_X nodes */
961 pred = get_Proj_pred(proj);
963 switch (get_irn_opcode(pred)) {
965 /* the Proj_X from the Start is always reachable */
966 node->type.tv = tarval_R;
969 default_compute(node);
974 * (Re-)compute the type for a given node.
976 * @param node the node
978 static void compute(node_t *node) {
979 compute_func func = (compute_func)node->node->op->ops.generic;
986 * Place a node on the cprop list.
989 * @param env the environment
991 static void add_node_to_cprop(node_t *y, environment_t *env) {
992 /* Add y to y.partition.cprop. */
993 if (y->on_cprop == 0) {
994 partition_t *Y = y->part;
996 y->cprop_next = Y->cprop;
1000 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
1002 /* place its partition on the cprop list */
1003 if (Y->on_cprop == 0) {
1004 Y->cprop_next = env->cprop;
1012 * Propagate constant evaluation.
1014 * @param env the environment
1016 static void propagate(environment_t *env) {
1019 lattice_elem_t old_type;
1020 node_t *fallen = NULL;
1021 unsigned n_fallen = 0;
1024 while (env->cprop != NULL) {
1025 /* remove the first partition X from cprop but do not set the bit here */
1027 env->cprop = X->cprop_next;
1029 dump_partition("Propagate", X);
1031 /* remove the first Node x from X.cprop but do NOT set the bit here */
1033 X->cprop = x->cprop_next;
1035 /* compute a new type for x */
1037 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1039 if (x->type.tv != old_type.tv) {
1040 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1041 /* Add x to fallen. */
1046 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1047 ir_node *succ = get_irn_out(x->node, i);
1048 node_t *y = get_irn_node(succ);
1050 /* Add y to y.partition.cprop. */
1051 add_node_to_cprop(y, env);
1054 /* now remove x from X.cprop: this ensures that a node is not placed on the list again
1055 if is its user by itself (happens for Phi nodes and dead code) */
1057 } while (X->cprop != NULL);
1059 /* now remove X from cprop, we have emptied it's local list */
1062 if (n_fallen != X->n_nodes) {
1063 assert(n_fallen > 0);
1064 Y = split(X, fallen, env);
1073 * Get the leader for a given node from its congruence class.
1075 * @param irn the node
1077 static ir_node *get_leader(ir_node *irn) {
1078 partition_t *part = get_irn_node(irn)->part;
1080 if (part->n_nodes > 1) {
1081 DB((dbg, LEVEL_2, "Found congruence class for %+F ", irn));
1082 dump_partition("", part);
1088 * Post-Walker, apply the analysis results;
1090 static void apply_result(ir_node *irn, void *ctx) {
1091 environment_t *env = ctx;
1093 if (is_no_Block(irn)) {
1094 ir_node *leader = get_leader(irn);
1096 if (leader != irn) {
1097 exchange(irn, leader);
1100 } /* static void apply_result(ir_node *irn, void *ctx) {
1103 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1106 * sets the generic functions to compute.
1108 static void set_compute_functions(void) {
1111 /* set the default compute function */
1112 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1113 ir_op *op = get_irp_opcode(i);
1114 op->ops.generic = (op_func)default_compute;
1117 /* set specific functions */
1124 } /* set_compute_functions */
1126 void combo(ir_graph *irg) {
1128 ir_node *start_bl, *initial_X;
1130 ir_graph *rem = current_ir_graph;
1132 current_ir_graph = irg;
1134 /* register a debug mask */
1135 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1136 firm_dbg_set_mask(dbg, SET_LEVEL_3);
1138 obstack_init(&env.obst);
1139 env.worklist = NULL;
1143 #ifdef DEBUG_libfirm
1144 env.dbg_list = NULL;
1146 env.opcode_map = new_set(cmp_opcode, iro_Last * 4);
1147 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1148 env.type2id_map = pmap_create();
1149 env.end_idx = get_opt_global_cse() ? 0 : -1;
1150 env.lambda_input = 0;
1152 assure_irg_outs(irg);
1154 /* we have our own value_of function */
1155 set_value_of_func(get_node_tarval);
1157 set_compute_functions();
1159 /* create the initial partition and place it on the work list */
1160 env.TOP = new_partition(&env);
1161 env.TOP->wl_next = env.worklist;
1162 env.worklist = env.TOP;
1163 irg_walk_graph(irg, NULL, create_initial_partitions, &env);
1165 /* Place the START Node's partition on cprop.
1166 Place the START Node on its local worklist. */
1167 start_bl = get_irg_start_block(irg);
1168 start = get_irn_node(start_bl);
1169 add_node_to_cprop(start, &env);
1171 /* set the initial exec to R */
1172 initial_X = get_irg_initial_exec(irg);
1173 get_irn_node(initial_X)->type.tv = tarval_R;
1177 if (env.worklist != NULL)
1179 } while (env.cprop != NULL || env.worklist != NULL);
1181 dump_all_partitions(&env);
1183 /* apply the result */
1184 irg_walk_graph(irg, NULL, apply_result, &env);
1186 pmap_destroy(env.type2id_map);
1187 del_set(env.opcode_map);
1188 del_set(env.opcode2id_map);
1189 obstack_free(&env.obst, NULL);
1191 /* restore value_of() default behavior */
1192 set_value_of_func(NULL);
1193 current_ir_graph = rem;