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 further 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/entity, nevertheless we call it type here for "maximum compatibility".
36 #include "iroptimize.h"
44 #include "irgraph_t.h"
59 /* define this to check that all type translations are monotone */
60 #define VERIFY_MONOTONE
62 /* define this to check the consistency of partitions */
63 #define CHECK_PARTITIONS
65 /* define this to disable followers (may be buggy) */
68 typedef struct node_t node_t;
69 typedef struct partition_t partition_t;
70 typedef struct opcode_key_t opcode_key_t;
71 typedef struct listmap_entry_t listmap_entry_t;
73 /** The type of the compute function. */
74 typedef void (*compute_func)(node_t *node);
80 ir_opcode code; /**< The Firm opcode. */
81 ir_mode *mode; /**< The mode of all nodes in the partition. */
82 int arity; /**< The arity of this opcode (needed for Phi etc. */
84 long proj; /**< For Proj nodes, its proj number */
85 ir_entity *ent; /**< For Sel Nodes, its entity */
90 * An entry in the list_map.
92 struct listmap_entry_t {
93 void *id; /**< The id. */
94 node_t *list; /**< The associated list for this id. */
95 listmap_entry_t *next; /**< Link to the next entry in the map. */
98 /** We must map id's to lists. */
99 typedef struct listmap_t {
100 set *map; /**< Map id's to listmap_entry_t's */
101 listmap_entry_t *values; /**< List of all values in the map. */
105 * A lattice element. Because we handle constants and symbolic constants different, we
106 * have to use this union.
117 ir_node *node; /**< The IR-node itself. */
118 list_head node_list; /**< Double-linked list of leader/follower entries. */
119 list_head cprop_list; /**< Double-linked partition.cprop list. */
120 partition_t *part; /**< points to the partition this node belongs to */
121 node_t *next; /**< Next node on local list (partition.touched, fallen). */
122 node_t *race_next; /**< Next node on race list. */
123 lattice_elem_t type; /**< The associated lattice element "type". */
124 int max_user_input; /**< Maximum input number of Def-Use edges. */
125 int next_edge; /**< Index of the next Def-Use edge to use. */
126 int n_followers; /**< Number of Follower in the outs set. */
127 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
128 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
129 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
130 unsigned is_follower:1; /**< Set, if this node is a follower. */
131 unsigned by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
132 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
136 * A partition containing congruent nodes.
139 list_head Leader; /**< The head of partition Leader node list. */
140 list_head Follower; /**< The head of partition Follower node list. */
141 list_head cprop; /**< The head of partition.cprop list. */
142 partition_t *wl_next; /**< Next entry in the work list if any. */
143 partition_t *touched_next; /**< Points to the next partition in the touched set. */
144 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
145 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
146 node_t *touched; /**< The partition.touched set of this partition. */
147 unsigned n_leader; /**< Number of entries in this partition.Leader. */
148 unsigned n_touched; /**< Number of entries in the partition.touched. */
149 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
150 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
151 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
152 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
153 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
155 partition_t *dbg_next; /**< Link all partitions for debugging */
156 unsigned nr; /**< A unique number for (what-)mapping, >0. */
160 typedef struct environment_t {
161 struct obstack obst; /**< obstack to allocate data structures. */
162 partition_t *worklist; /**< The work list. */
163 partition_t *cprop; /**< The constant propagation list. */
164 partition_t *touched; /**< the touched set. */
165 partition_t *initial; /**< The initial partition. */
166 set *opcode2id_map; /**< The opcodeMode->id map. */
167 pmap *type2id_map; /**< The type->id map. */
168 int end_idx; /**< -1 for local and 0 for global congruences. */
169 int lambda_input; /**< Captured argument for lambda_partition(). */
170 char nonstd_cond; /**< Set, if a Condb note has a non-Cmp predecessor. */
171 char modified; /**< Set, if the graph was modified. */
173 partition_t *dbg_list; /**< List of all partitions. */
177 /** Type of the what function. */
178 typedef void *(*what_func)(const node_t *node, environment_t *env);
180 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
181 #define set_irn_node(follower, node) set_irn_link(follower, node)
183 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
184 #undef tarval_unreachable
185 #define tarval_unreachable tarval_top
188 /** The debug module handle. */
189 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
191 /** Next partition number. */
192 DEBUG_ONLY(static unsigned part_nr = 0);
194 /** The tarval returned by Unknown nodes. */
195 static tarval *tarval_UNKNOWN;
198 static node_t *identity(node_t *node);
200 #ifdef CHECK_PARTITIONS
204 static void check_partition(const partition_t *T) {
208 list_for_each_entry(node_t, node, &T->Leader, node_list) {
209 assert(node->is_follower == 0);
210 assert(node->flagged == 0);
211 assert(node->part == T);
214 assert(n == T->n_leader);
216 list_for_each_entry(node_t, node, &T->Follower, node_list) {
217 assert(node->is_follower == 1);
218 assert(node->flagged == 0);
219 assert(node->part == T);
221 } /* check_partition */
223 static void check_all_partitions(environment_t *env) {
227 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
229 list_for_each_entry(node_t, node, &P->Follower, node_list) {
230 node_t *leader = identity(node);
232 assert(leader != node && leader->part == node->part);
240 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
243 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
244 for (e = list; e != NULL; e = NEXT(e)) {
245 assert(e->part == Z);
248 } /* ido_check_list */
251 * Check a local list.
253 static void check_list(const node_t *list, const partition_t *Z) {
254 do_check_list(list, offsetof(node_t, next), Z);
258 #define check_partition(T)
259 #define check_list(list, Z)
260 #define check_all_partitions(env)
261 #endif /* CHECK_PARTITIONS */
264 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
267 * Dump partition to output.
269 static void dump_partition(const char *msg, const partition_t *part) {
272 lattice_elem_t type = get_partition_type(part);
274 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
275 msg, part->nr, part->type_is_T_or_C ? "*" : "",
276 part->n_leader, type));
277 list_for_each_entry(node_t, node, &part->Leader, node_list) {
278 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
281 if (! list_empty(&part->Follower)) {
282 DB((dbg, LEVEL_2, "\n---\n "));
284 list_for_each_entry(node_t, node, &part->Follower, node_list) {
285 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
289 DB((dbg, LEVEL_2, "\n}\n"));
290 } /* dump_partition */
295 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
299 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
301 DB((dbg, LEVEL_3, "%s = {\n ", msg));
302 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
303 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
306 DB((dbg, LEVEL_3, "\n}\n"));
314 static void dump_race_list(const char *msg, const node_t *list) {
315 do_dump_list(msg, list, offsetof(node_t, race_next));
319 * Dumps a local list.
321 static void dump_list(const char *msg, const node_t *list) {
322 do_dump_list(msg, list, offsetof(node_t, next));
326 * Dump all partitions.
328 static void dump_all_partitions(const environment_t *env) {
329 const partition_t *P;
331 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
332 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
333 dump_partition("", P);
337 #define dump_partition(msg, part)
338 #define dump_race_list(msg, list)
339 #define dump_list(msg, list)
340 #define dump_all_partitions(env)
343 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
345 * Verify that a type transition is monotone
347 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
348 if (old_type.tv == new_type.tv) {
352 if (old_type.tv == tarval_top) {
353 /* from Top down-to is always allowed */
356 if (old_type.tv == tarval_reachable) {
357 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
359 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
363 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
366 #define verify_type(old_type, new_type)
370 * Compare two pointer values of a listmap.
372 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
373 const listmap_entry_t *e1 = elt;
374 const listmap_entry_t *e2 = key;
377 return e1->id != e2->id;
378 } /* listmap_cmp_ptr */
381 * Initializes a listmap.
383 * @param map the listmap
385 static void listmap_init(listmap_t *map) {
386 map->map = new_set(listmap_cmp_ptr, 16);
391 * Terminates a listmap.
393 * @param map the listmap
395 static void listmap_term(listmap_t *map) {
400 * Return the associated listmap entry for a given id.
402 * @param map the listmap
403 * @param id the id to search for
405 * @return the asociated listmap entry for the given id
407 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
408 listmap_entry_t key, *entry;
413 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
415 if (entry->list == NULL) {
416 /* a new entry, put into the list */
417 entry->next = map->values;
424 * Calculate the hash value for an opcode map entry.
426 * @param entry an opcode map entry
428 * @return a hash value for the given opcode map entry
430 static unsigned opcode_hash(const opcode_key_t *entry) {
431 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
435 * Compare two entries in the opcode map.
437 static int cmp_opcode(const void *elt, const void *key, size_t size) {
438 const opcode_key_t *o1 = elt;
439 const opcode_key_t *o2 = key;
442 return o1->code != o2->code || o1->mode != o2->mode ||
443 o1->arity != o2->arity ||
444 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
448 * Compare two Def-Use edges for input position.
450 static int cmp_def_use_edge(const void *a, const void *b) {
451 const ir_def_use_edge *ea = a;
452 const ir_def_use_edge *eb = b;
454 /* no overrun, because range is [-1, MAXINT] */
455 return ea->pos - eb->pos;
456 } /* cmp_def_use_edge */
459 * We need the Def-Use edges sorted.
461 static void sort_irn_outs(node_t *node) {
462 ir_node *irn = node->node;
463 int n_outs = get_irn_n_outs(irn);
466 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
468 node->max_user_input = irn->out[n_outs].pos;
469 } /* sort_irn_outs */
472 * Return the type of a node.
474 * @param irn an IR-node
476 * @return the associated type of this node
478 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
479 return get_irn_node(irn)->type;
480 } /* get_node_type */
483 * Return the tarval of a node.
485 * @param irn an IR-node
487 * @return the associated type of this node
489 static INLINE tarval *get_node_tarval(const ir_node *irn) {
490 lattice_elem_t type = get_node_type(irn);
492 if (is_tarval(type.tv))
494 return tarval_bottom;
495 } /* get_node_type */
498 * Add a partition to the worklist.
500 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
501 assert(X->on_worklist == 0);
502 X->wl_next = env->worklist;
505 } /* add_to_worklist */
508 * Create a new empty partition.
510 * @param env the environment
512 * @return a newly allocated partition
514 static INLINE partition_t *new_partition(environment_t *env) {
515 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
517 INIT_LIST_HEAD(&part->Leader);
518 INIT_LIST_HEAD(&part->Follower);
519 INIT_LIST_HEAD(&part->cprop);
520 part->wl_next = NULL;
521 part->touched_next = NULL;
522 part->cprop_next = NULL;
523 part->split_next = NULL;
524 part->touched = NULL;
527 part->max_user_inputs = 0;
528 part->on_worklist = 0;
529 part->on_touched = 0;
531 part->type_is_T_or_C = 0;
533 part->dbg_next = env->dbg_list;
534 env->dbg_list = part;
535 part->nr = part_nr++;
539 } /* new_partition */
542 * Get the first node from a partition.
544 static INLINE node_t *get_first_node(const partition_t *X) {
545 return list_entry(X->Leader.next, node_t, node_list);
546 } /* get_first_node */
549 * Return the type of a partition (assuming partition is non-empty and
550 * all elements have the same type).
552 * @param X a partition
554 * @return the type of the first element of the partition
556 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
557 const node_t *first = get_first_node(X);
559 } /* get_partition_type */
562 * Creates a partition node for the given IR-node and place it
563 * into the given partition.
565 * @param irn an IR-node
566 * @param part a partition to place the node in
567 * @param env the environment
569 * @return the created node
571 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
572 /* create a partition node and place it in the partition */
573 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
575 INIT_LIST_HEAD(&node->node_list);
576 INIT_LIST_HEAD(&node->cprop_list);
580 node->race_next = NULL;
581 node->type.tv = tarval_top;
582 node->max_user_input = 0;
584 node->n_followers = 0;
585 node->on_touched = 0;
588 node->is_follower = 0;
589 node->by_all_const = 0;
591 set_irn_node(irn, node);
593 list_add_tail(&node->node_list, &part->Leader);
597 } /* create_partition_node */
600 * Pre-Walker, init all Block-Phi lists.
602 static void init_block_phis(ir_node *irn, void *env) {
606 set_Block_phis(irn, NULL);
608 } /* init_block_phis */
611 * Post-Walker, initialize all Nodes' type to U or top and place
612 * all nodes into the TOP partition.
614 static void create_initial_partitions(ir_node *irn, void *ctx) {
615 environment_t *env = ctx;
616 partition_t *part = env->initial;
619 node = create_partition_node(irn, part, env);
621 if (node->max_user_input > part->max_user_inputs)
622 part->max_user_inputs = node->max_user_input;
625 add_Block_phi(get_nodes_block(irn), irn);
626 } else if (is_Cond(irn)) {
627 /* check if all Cond's have a Cmp predecessor. */
628 if (get_irn_mode(irn) == mode_b && !is_Cmp(skip_Proj(get_Cond_selector(irn))))
629 env->nonstd_cond = 1;
632 } /* create_initial_partitions */
635 * Add a node to the entry.partition.touched set and
636 * node->partition to the touched set if not already there.
639 * @param env the environment
641 static INLINE void add_to_touched(node_t *y, environment_t *env) {
642 if (y->on_touched == 0) {
643 partition_t *part = y->part;
645 y->next = part->touched;
650 if (part->on_touched == 0) {
651 part->touched_next = env->touched;
653 part->on_touched = 1;
656 check_list(part->touched, part);
658 } /* add_to_touched */
661 * Place a node on the cprop list.
664 * @param env the environment
666 static void add_to_cprop(node_t *y, environment_t *env) {
667 /* Add y to y.partition.cprop. */
668 if (y->on_cprop == 0) {
669 partition_t *Y = y->part;
671 list_add_tail(&y->cprop_list, &Y->cprop);
674 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
676 /* place its partition on the cprop list */
677 if (Y->on_cprop == 0) {
678 Y->cprop_next = env->cprop;
683 if (get_irn_mode(y->node) == mode_T) {
684 /* mode_T nodes always produce tarval_bottom, so we must explicitly
685 add it's Proj's to get constant evaluation to work */
688 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
689 node_t *proj = get_irn_node(get_irn_out(y->node, i));
691 add_to_cprop(proj, env);
693 } else if (is_Block(y->node)) {
694 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
695 * if someone placed the block. The Block is only placed if the reachability
696 * changes, and this must be re-evaluated in compute_Phi(). */
698 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
699 node_t *p = get_irn_node(phi);
700 add_to_cprop(p, env);
706 * Update the worklist: If Z is on worklist then add Z' to worklist.
707 * Else add the smaller of Z and Z' to worklist.
709 * @param Z the Z partition
710 * @param Z_prime the Z' partition, a previous part of Z
711 * @param env the environment
713 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
714 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
715 add_to_worklist(Z_prime, env);
717 add_to_worklist(Z, env);
719 } /* update_worklist */
722 * Make all inputs to x no longer be F.def_use edges.
726 static void move_edges_to_leader(node_t *x) {
727 ir_node *irn = x->node;
730 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
731 node_t *pred = get_irn_node(get_irn_n(irn, i));
736 n = get_irn_n_outs(p);
737 for (j = 1; j <= pred->n_followers; ++j) {
738 if (p->out[j].pos == i && p->out[j].use == irn) {
739 /* found a follower edge to x, move it to the Leader */
740 ir_def_use_edge edge = p->out[j];
742 /* remove this edge from the Follower set */
743 p->out[j] = p->out[pred->n_followers];
746 /* sort it into the leader set */
747 for (k = pred->n_followers + 2; k <= n; ++k) {
748 if (p->out[k].pos >= edge.pos)
750 p->out[k - 1] = p->out[k];
752 /* place the new edge here */
753 p->out[k - 1] = edge;
755 /* edge found and moved */
760 } /* move_edges_to_leader */
763 * Split a partition that has NO followers by a local list.
765 * @param Z partition to split
766 * @param g a (non-empty) node list
767 * @param env the environment
769 * @return a new partition containing the nodes of g
771 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
772 partition_t *Z_prime;
777 dump_partition("Splitting ", Z);
778 dump_list("by list ", g);
782 /* Remove g from Z. */
783 for (node = g; node != NULL; node = node->next) {
784 assert(node->part == Z);
785 list_del(&node->node_list);
788 assert(n < Z->n_leader);
791 /* Move g to a new partition, Z'. */
792 Z_prime = new_partition(env);
794 for (node = g; node != NULL; node = node->next) {
795 list_add_tail(&node->node_list, &Z_prime->Leader);
796 node->part = Z_prime;
797 if (node->max_user_input > max_input)
798 max_input = node->max_user_input;
800 Z_prime->max_user_inputs = max_input;
801 Z_prime->n_leader = n;
804 check_partition(Z_prime);
806 /* for now, copy the type info tag, it will be adjusted in split_by(). */
807 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
809 update_worklist(Z, Z_prime, env);
811 dump_partition("Now ", Z);
812 dump_partition("Created new ", Z_prime);
814 } /* split_no_followers */
818 #define split(Z, g, env) split_no_followers(*(Z), g, env)
823 * Make the Follower -> Leader transition for a node.
827 static void follower_to_leader(node_t *n) {
828 assert(n->is_follower == 1);
830 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
832 move_edges_to_leader(n);
833 list_del(&n->node_list);
834 list_add_tail(&n->node_list, &n->part->Leader);
836 } /* follower_to_leader */
839 * The environment for one race step.
841 typedef struct step_env {
842 node_t *initial; /**< The initial node list. */
843 node_t *unwalked; /**< The unwalked node list. */
844 node_t *walked; /**< The walked node list. */
845 int index; /**< Next index of Follower use_def edge. */
846 unsigned side; /**< side number. */
850 * Return non-zero, if a input is a real follower
852 * @param irn the node to check
853 * @param input number of the input
855 static int is_real_follower(const ir_node *irn, int input) {
858 switch (get_irn_opcode(irn)) {
861 /* ignore the Confirm bound input */
867 /* ignore the Mux sel input */
872 /* dead inputs are not follower edges */
873 ir_node *block = get_nodes_block(irn);
874 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
876 if (pred->type.tv == tarval_unreachable)
882 /* only a Sub x,0 might be a follower */
888 pred = get_irn_node(get_irn_n(irn, input));
889 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
893 pred = get_irn_node(get_irn_n(irn, input));
894 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
898 pred = get_irn_node(get_irn_n(irn, input));
899 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
904 /* all inputs are followers */
907 assert(!"opcode not implemented yet");
914 * Do one step in the race.
916 static int step(step_env *env) {
919 if (env->initial != NULL) {
920 /* Move node from initial to unwalked */
922 env->initial = n->race_next;
924 n->race_next = env->unwalked;
930 while (env->unwalked != NULL) {
931 /* let n be the first node in unwalked */
933 while (env->index < n->n_followers) {
934 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
936 /* let m be n.F.def_use[index] */
937 node_t *m = get_irn_node(edge->use);
939 assert(m->is_follower);
941 * Some inputs, like the get_Confirm_bound are NOT
942 * real followers, sort them out.
944 if (! is_real_follower(m->node, edge->pos)) {
950 /* only followers from our partition */
951 if (m->part != n->part)
954 if ((m->flagged & env->side) == 0) {
955 m->flagged |= env->side;
957 if (m->flagged != 3) {
958 /* visited the first time */
959 /* add m to unwalked not as first node (we might still need to
960 check for more follower node */
961 m->race_next = n->race_next;
965 /* else already visited by the other side and on the other list */
968 /* move n to walked */
969 env->unwalked = n->race_next;
970 n->race_next = env->walked;
978 * Clear the flags from a list and check for
979 * nodes that where touched from both sides.
981 * @param list the list
983 static int clear_flags(node_t *list) {
987 for (n = list; n != NULL; n = n->race_next) {
988 if (n->flagged == 3) {
989 /* we reach a follower from both sides, this will split congruent
990 * inputs and make it a leader. */
991 follower_to_leader(n);
1000 * Split a partition by a local list using the race.
1002 * @param pX pointer to the partition to split, might be changed!
1003 * @param gg a (non-empty) node list
1004 * @param env the environment
1006 * @return a new partition containing the nodes of gg
1008 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1009 partition_t *X = *pX;
1010 partition_t *X_prime;
1012 step_env env1, env2, *winner;
1013 node_t *g, *h, *node, *t;
1014 int max_input, transitions;
1016 DEBUG_ONLY(static int run = 0;)
1018 DB((dbg, LEVEL_2, "Run %d ", run++));
1019 if (list_empty(&X->Follower)) {
1020 /* if the partition has NO follower, we can use the fast
1021 splitting algorithm. */
1022 return split_no_followers(X, gg, env);
1024 /* else do the race */
1026 dump_partition("Splitting ", X);
1027 dump_list("by list ", gg);
1029 INIT_LIST_HEAD(&tmp);
1031 /* Remove gg from X.Leader and put into g */
1033 for (node = gg; node != NULL; node = node->next) {
1034 assert(node->part == X);
1035 assert(node->is_follower == 0);
1037 list_del(&node->node_list);
1038 list_add_tail(&node->node_list, &tmp);
1039 node->race_next = g;
1044 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1045 node->race_next = h;
1048 /* restore X.Leader */
1049 list_splice(&tmp, &X->Leader);
1052 env1.unwalked = NULL;
1058 env2.unwalked = NULL;
1073 assert(winner->initial == NULL);
1074 assert(winner->unwalked == NULL);
1076 /* clear flags from walked/unwalked */
1077 transitions = clear_flags(env1.unwalked);
1078 transitions |= clear_flags(env1.walked);
1079 transitions |= clear_flags(env2.unwalked);
1080 transitions |= clear_flags(env2.walked);
1082 dump_race_list("winner ", winner->walked);
1084 /* Move walked_{winner} to a new partition, X'. */
1085 X_prime = new_partition(env);
1088 for (node = winner->walked; node != NULL; node = node->race_next) {
1089 list_del(&node->node_list);
1090 node->part = X_prime;
1091 if (node->is_follower) {
1092 list_add_tail(&node->node_list, &X_prime->Follower);
1094 list_add_tail(&node->node_list, &X_prime->Leader);
1097 if (node->max_user_input > max_input)
1098 max_input = node->max_user_input;
1100 X_prime->n_leader = n;
1101 X_prime->max_user_inputs = max_input;
1102 X->n_leader -= X_prime->n_leader;
1104 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1105 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1108 * Even if a follower was not checked by both sides, it might have
1109 * loose its congruence, so we need to check this case for all follower.
1111 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1112 if (identity(node) == node) {
1113 follower_to_leader(node);
1119 check_partition(X_prime);
1121 /* X' is the smaller part */
1122 add_to_worklist(X_prime, env);
1125 * If there where follower to leader transitions, ensure that the nodes
1126 * can be split out if necessary.
1129 /* place partitions on the cprop list */
1130 if (X_prime->on_cprop == 0) {
1131 X_prime->cprop_next = env->cprop;
1132 env->cprop = X_prime;
1133 X_prime->on_cprop = 1;
1137 dump_partition("Now ", X);
1138 dump_partition("Created new ", X_prime);
1140 /* we have to ensure that the partition containing g is returned */
1141 if (winner == &env2) {
1148 #endif /* NO_FOLLOWER */
1151 * Returns non-zero if the i'th input of a Phi node is live.
1153 * @param phi a Phi-node
1154 * @param i an input number
1156 * @return non-zero if the i'th input of the given Phi node is live
1158 static int is_live_input(ir_node *phi, int i) {
1160 ir_node *block = get_nodes_block(phi);
1161 ir_node *pred = get_Block_cfgpred(block, i);
1162 lattice_elem_t type = get_node_type(pred);
1164 return type.tv != tarval_unreachable;
1166 /* else it's the control input, always live */
1168 } /* is_live_input */
1171 * Return non-zero if a type is a constant.
1173 static int is_constant_type(lattice_elem_t type) {
1174 if (type.tv != tarval_bottom && type.tv != tarval_top)
1177 } /* is_constant_type */
1180 * Check whether a type is neither Top or a constant.
1181 * Note: U is handled like Top here, R is a constant.
1183 * @param type the type to check
1185 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1186 if (is_tarval(type.tv)) {
1187 if (type.tv == tarval_top)
1189 if (tarval_is_constant(type.tv))
1199 * Collect nodes to the touched list.
1201 * @param list the list which contains the nodes that must be evaluated
1202 * @param idx the index of the def_use edge to evaluate
1203 * @param env the environment
1205 static void collect_touched(list_head *list, int idx, environment_t *env) {
1207 int end_idx = env->end_idx;
1209 list_for_each_entry(node_t, x, list, node_list) {
1213 /* leader edges start AFTER follower edges */
1214 x->next_edge = x->n_followers + 1;
1216 num_edges = get_irn_n_outs(x->node);
1218 /* for all edges in x.L.def_use_{idx} */
1219 while (x->next_edge <= num_edges) {
1220 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1223 /* check if we have necessary edges */
1224 if (edge->pos > idx)
1231 /* ignore the "control input" for non-pinned nodes
1232 if we are running in GCSE mode */
1233 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1236 y = get_irn_node(succ);
1237 assert(get_irn_n(succ, idx) == x->node);
1239 /* ignore block edges touching followers */
1240 if (idx == -1 && y->is_follower)
1243 if (is_constant_type(y->type)) {
1244 ir_opcode code = get_irn_opcode(succ);
1245 if (code == iro_Sub || code == iro_Cmp)
1246 add_to_cprop(y, env);
1249 /* Partitions of constants should not be split simply because their Nodes have unequal
1250 functions or incongruent inputs. */
1251 if (type_is_neither_top_nor_const(y->type) &&
1252 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1253 add_to_touched(y, env);
1257 } /* collect_touched */
1260 * Split the partitions if caused by the first entry on the worklist.
1262 * @param env the environment
1264 static void cause_splits(environment_t *env) {
1265 partition_t *X, *Z, *N;
1268 /* remove the first partition from the worklist */
1270 env->worklist = X->wl_next;
1273 dump_partition("Cause_split: ", X);
1275 /* combine temporary leader and follower list */
1276 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1277 /* empty the touched set: already done, just clear the list */
1278 env->touched = NULL;
1280 collect_touched(&X->Leader, idx, env);
1281 collect_touched(&X->Follower, idx, env);
1283 for (Z = env->touched; Z != NULL; Z = N) {
1285 node_t *touched = Z->touched;
1286 unsigned n_touched = Z->n_touched;
1288 assert(Z->touched != NULL);
1290 /* beware, split might change Z */
1291 N = Z->touched_next;
1293 /* remove it from the touched set */
1296 /* Empty local Z.touched. */
1297 for (e = touched; e != NULL; e = e->next) {
1298 assert(e->is_follower == 0);
1304 if (0 < n_touched && n_touched < Z->n_leader) {
1305 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1306 split(&Z, touched, env);
1308 assert(n_touched <= Z->n_leader);
1311 } /* cause_splits */
1314 * Implements split_by_what(): Split a partition by characteristics given
1315 * by the what function.
1317 * @param X the partition to split
1318 * @param What a function returning an Id for every node of the partition X
1319 * @param P a list to store the result partitions
1320 * @param env the environment
1324 static partition_t *split_by_what(partition_t *X, what_func What,
1325 partition_t **P, environment_t *env) {
1328 listmap_entry_t *iter;
1331 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1333 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1334 void *id = What(x, env);
1335 listmap_entry_t *entry;
1338 /* input not allowed, ignore */
1341 /* Add x to map[What(x)]. */
1342 entry = listmap_find(&map, id);
1343 x->next = entry->list;
1346 /* Let P be a set of Partitions. */
1348 /* for all sets S except one in the range of map do */
1349 for (iter = map.values; iter != NULL; iter = iter->next) {
1350 if (iter->next == NULL) {
1351 /* this is the last entry, ignore */
1356 /* Add SPLIT( X, S ) to P. */
1357 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1358 R = split(&X, S, env);
1368 } /* split_by_what */
1370 /** lambda n.(n.type) */
1371 static void *lambda_type(const node_t *node, environment_t *env) {
1373 return node->type.tv;
1376 /** lambda n.(n.opcode) */
1377 static void *lambda_opcode(const node_t *node, environment_t *env) {
1378 opcode_key_t key, *entry;
1379 ir_node *irn = node->node;
1381 key.code = get_irn_opcode(irn);
1382 key.mode = get_irn_mode(irn);
1383 key.arity = get_irn_arity(irn);
1387 switch (get_irn_opcode(irn)) {
1389 key.u.proj = get_Proj_proj(irn);
1392 key.u.ent = get_Sel_entity(irn);
1398 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1400 } /* lambda_opcode */
1402 /** lambda n.(n[i].partition) */
1403 static void *lambda_partition(const node_t *node, environment_t *env) {
1404 ir_node *skipped = skip_Proj(node->node);
1407 int i = env->lambda_input;
1409 if (i >= get_irn_arity(node->node)) {
1410 /* we are outside the allowed range */
1414 /* ignore the "control input" for non-pinned nodes
1415 if we are running in GCSE mode */
1416 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1419 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1420 p = get_irn_node(pred);
1423 } /* lambda_partition */
1426 * Returns true if a type is a constant.
1428 static int is_con(const lattice_elem_t type) {
1429 /* be conservative */
1430 if (is_tarval(type.tv))
1431 return tarval_is_constant(type.tv);
1432 return is_entity(type.sym.entity_p);
1436 * Implements split_by().
1438 * @param X the partition to split
1439 * @param env the environment
1441 static void split_by(partition_t *X, environment_t *env) {
1442 partition_t *I, *P = NULL;
1445 dump_partition("split_by", X);
1447 if (X->n_leader == 1) {
1448 /* we have only one leader, no need to split, just check it's type */
1449 node_t *x = get_first_node(X);
1450 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1454 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1455 P = split_by_what(X, lambda_type, &P, env);
1457 /* adjust the type tags, we have split partitions by type */
1458 for (I = P; I != NULL; I = I->split_next) {
1459 node_t *x = get_first_node(I);
1460 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1467 if (Y->n_leader > 1) {
1468 /* we do not want split the TOP or constant partitions */
1469 if (! Y->type_is_T_or_C) {
1470 partition_t *Q = NULL;
1472 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1473 Q = split_by_what(Y, lambda_opcode, &Q, env);
1479 if (Z->n_leader > 1) {
1480 const node_t *first = get_first_node(Z);
1481 int arity = get_irn_arity(first->node);
1485 * BEWARE: during splitting by input 2 for instance we might
1486 * create new partitions which are different by input 1, so collect
1487 * them and split further.
1489 Z->split_next = NULL;
1492 for (input = arity - 1; input >= -1; --input) {
1494 partition_t *Z_prime = R;
1497 if (Z_prime->n_leader > 1) {
1498 env->lambda_input = input;
1499 DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1500 S = split_by_what(Z_prime, lambda_partition, &S, env);
1502 Z_prime->split_next = S;
1505 } while (R != NULL);
1510 } while (Q != NULL);
1513 } while (P != NULL);
1517 * (Re-)compute the type for a given node.
1519 * @param node the node
1521 static void default_compute(node_t *node) {
1523 ir_node *irn = node->node;
1524 node_t *block = get_irn_node(get_nodes_block(irn));
1526 if (block->type.tv == tarval_unreachable) {
1527 node->type.tv = tarval_top;
1531 /* if any of the data inputs have type top, the result is type top */
1532 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1533 ir_node *pred = get_irn_n(irn, i);
1534 node_t *p = get_irn_node(pred);
1536 if (p->type.tv == tarval_top) {
1537 node->type.tv = tarval_top;
1542 if (get_irn_mode(node->node) == mode_X)
1543 node->type.tv = tarval_reachable;
1545 node->type.tv = computed_value(irn);
1546 } /* default_compute */
1549 * (Re-)compute the type for a Block node.
1551 * @param node the node
1553 static void compute_Block(node_t *node) {
1555 ir_node *block = node->node;
1557 if (block == get_irg_start_block(current_ir_graph)) {
1558 /* start block is always reachable */
1559 node->type.tv = tarval_reachable;
1563 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1564 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1566 if (pred->type.tv == tarval_reachable) {
1567 /* A block is reachable, if at least of predecessor is reachable. */
1568 node->type.tv = tarval_reachable;
1572 node->type.tv = tarval_top;
1573 } /* compute_Block */
1576 * (Re-)compute the type for a Bad node.
1578 * @param node the node
1580 static void compute_Bad(node_t *node) {
1581 /* Bad nodes ALWAYS compute Top */
1582 node->type.tv = tarval_top;
1586 * (Re-)compute the type for an Unknown node.
1588 * @param node the node
1590 static void compute_Unknown(node_t *node) {
1591 /* While Unknown nodes should compute Top this is dangerous:
1592 * a Top input to a Cond would lead to BOTH control flows unreachable.
1593 * While this is correct in the given semantics, it would destroy the Firm
1596 * It would be safe to compute Top IF it can be assured, that only Cmp
1597 * nodes are inputs to Conds. We check that first.
1598 * This is the way Frontends typically build Firm, but some optimizations
1599 * (cond_eval for instance) might replace them by Phib's...
1601 node->type.tv = tarval_UNKNOWN;
1602 } /* compute_Unknown */
1605 * (Re-)compute the type for a Jmp node.
1607 * @param node the node
1609 static void compute_Jmp(node_t *node) {
1610 node_t *block = get_irn_node(get_nodes_block(node->node));
1612 node->type = block->type;
1616 * (Re-)compute the type for the End node.
1618 * @param node the node
1620 static void compute_End(node_t *node) {
1621 /* the End node is NOT dead of course */
1622 node->type.tv = tarval_reachable;
1626 * (Re-)compute the type for a SymConst node.
1628 * @param node the node
1630 static void compute_SymConst(node_t *node) {
1631 ir_node *irn = node->node;
1632 node_t *block = get_irn_node(get_nodes_block(irn));
1634 if (block->type.tv == tarval_unreachable) {
1635 node->type.tv = tarval_top;
1638 switch (get_SymConst_kind(irn)) {
1639 case symconst_addr_ent:
1640 /* case symconst_addr_name: cannot handle this yet */
1641 node->type.sym = get_SymConst_symbol(irn);
1644 node->type.tv = computed_value(irn);
1646 } /* compute_SymConst */
1649 * (Re-)compute the type for a Phi node.
1651 * @param node the node
1653 static void compute_Phi(node_t *node) {
1655 ir_node *phi = node->node;
1656 lattice_elem_t type;
1658 /* if a Phi is in a unreachable block, its type is TOP */
1659 node_t *block = get_irn_node(get_nodes_block(phi));
1661 if (block->type.tv == tarval_unreachable) {
1662 node->type.tv = tarval_top;
1666 /* Phi implements the Meet operation */
1667 type.tv = tarval_top;
1668 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1669 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1670 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1672 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1673 /* ignore TOP inputs: We must check here for unreachable blocks,
1674 because Firm constants live in the Start Block are NEVER Top.
1675 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1676 comes from a unreachable input. */
1679 if (pred->type.tv == tarval_bottom) {
1680 node->type.tv = tarval_bottom;
1682 } else if (type.tv == tarval_top) {
1683 /* first constant found */
1685 } else if (type.tv != pred->type.tv) {
1686 /* different constants or tarval_bottom */
1687 node->type.tv = tarval_bottom;
1690 /* else nothing, constants are the same */
1696 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1698 * @param node the node
1700 static void compute_Add(node_t *node) {
1701 ir_node *sub = node->node;
1702 node_t *l = get_irn_node(get_Add_left(sub));
1703 node_t *r = get_irn_node(get_Add_right(sub));
1704 lattice_elem_t a = l->type;
1705 lattice_elem_t b = r->type;
1708 if (a.tv == tarval_top || b.tv == tarval_top) {
1709 node->type.tv = tarval_top;
1710 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1711 node->type.tv = tarval_bottom;
1713 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1714 must call tarval_add() first to handle this case! */
1715 if (is_tarval(a.tv)) {
1716 if (is_tarval(b.tv)) {
1717 node->type.tv = tarval_add(a.tv, b.tv);
1720 mode = get_tarval_mode(a.tv);
1721 if (a.tv == get_mode_null(mode)) {
1725 } else if (is_tarval(b.tv)) {
1726 mode = get_tarval_mode(b.tv);
1727 if (b.tv == get_mode_null(mode)) {
1732 node->type.tv = tarval_bottom;
1737 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1739 * @param node the node
1741 static void compute_Sub(node_t *node) {
1742 ir_node *sub = node->node;
1743 node_t *l = get_irn_node(get_Sub_left(sub));
1744 node_t *r = get_irn_node(get_Sub_right(sub));
1745 lattice_elem_t a = l->type;
1746 lattice_elem_t b = r->type;
1749 if (a.tv == tarval_top || b.tv == tarval_top) {
1750 node->type.tv = tarval_top;
1751 } else if (is_con(a) && is_con(b)) {
1752 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1753 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1754 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1756 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1759 node->type.tv = tarval_bottom;
1761 node->by_all_const = 1;
1762 } else if (r->part == l->part &&
1763 (!mode_is_float(get_irn_mode(l->node)))) {
1765 * BEWARE: a - a is NOT always 0 for floating Point values, as
1766 * NaN op NaN = NaN, so we must check this here.
1768 ir_mode *mode = get_irn_mode(sub);
1769 tv = get_mode_null(mode);
1771 /* if the node was ONCE evaluated by all constants, but now
1772 this breakes AND we cat by partition a different result, switch to bottom.
1773 This happens because initially all nodes are in the same partition ... */
1774 if (node->by_all_const && node->type.tv != tv)
1778 node->type.tv = tarval_bottom;
1783 * (Re-)compute the type for Cmp.
1785 * @param node the node
1787 static void compute_Cmp(node_t *node) {
1788 ir_node *cmp = node->node;
1789 node_t *l = get_irn_node(get_Cmp_left(cmp));
1790 node_t *r = get_irn_node(get_Cmp_right(cmp));
1791 lattice_elem_t a = l->type;
1792 lattice_elem_t b = r->type;
1794 if (a.tv == tarval_top || b.tv == tarval_top) {
1796 * Top is congruent to any other value, we can
1797 * calculate the compare result.
1799 node->type.tv = tarval_b_true;
1800 } else if (is_con(a) && is_con(b)) {
1801 /* both nodes are constants, we can probably do something */
1802 node->type.tv = tarval_b_true;
1803 } else if (r->part == l->part) {
1804 /* both nodes congruent, we can probably do something */
1805 node->type.tv = tarval_b_true;
1807 node->type.tv = tarval_bottom;
1809 } /* compute_Proj_Cmp */
1812 * (Re-)compute the type for a Proj(Cmp).
1814 * @param node the node
1815 * @param cond the predecessor Cmp node
1817 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1818 ir_node *proj = node->node;
1819 node_t *l = get_irn_node(get_Cmp_left(cmp));
1820 node_t *r = get_irn_node(get_Cmp_right(cmp));
1821 lattice_elem_t a = l->type;
1822 lattice_elem_t b = r->type;
1823 pn_Cmp pnc = get_Proj_proj(proj);
1826 if (a.tv == tarval_top || b.tv == tarval_top) {
1828 tv = new_tarval_from_long((pnc & pn_Cmp_Eq) ^ pn_Cmp_Eq, mode_b);
1830 } else if (is_con(a) && is_con(b)) {
1831 default_compute(node);
1832 node->by_all_const = 1;
1833 } else if (r->part == l->part &&
1834 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1836 * BEWARE: a == a is NOT always True for floating Point values, as
1837 * NaN != NaN is defined, so we must check this here.
1839 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1842 /* if the node was ONCE evaluated by all constants, but now
1843 this breakes AND we cat by partition a different result, switch to bottom.
1844 This happens because initially all nodes are in the same partition ... */
1845 if (node->by_all_const && node->type.tv != tv)
1849 node->type.tv = tarval_bottom;
1851 } /* compute_Proj_Cmp */
1854 * (Re-)compute the type for a Proj(Cond).
1856 * @param node the node
1857 * @param cond the predecessor Cond node
1859 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1860 ir_node *proj = node->node;
1861 long pnc = get_Proj_proj(proj);
1862 ir_node *sel = get_Cond_selector(cond);
1863 node_t *selector = get_irn_node(sel);
1865 if (get_irn_mode(sel) == mode_b) {
1867 if (pnc == pn_Cond_true) {
1868 if (selector->type.tv == tarval_b_false) {
1869 node->type.tv = tarval_unreachable;
1870 } else if (selector->type.tv == tarval_b_true) {
1871 node->type.tv = tarval_reachable;
1872 } else if (selector->type.tv == tarval_bottom) {
1873 node->type.tv = tarval_reachable;
1875 assert(selector->type.tv == tarval_top);
1876 node->type.tv = tarval_unreachable;
1879 assert(pnc == pn_Cond_false);
1881 if (selector->type.tv == tarval_b_false) {
1882 node->type.tv = tarval_reachable;
1883 } else if (selector->type.tv == tarval_b_true) {
1884 node->type.tv = tarval_unreachable;
1885 } else if (selector->type.tv == tarval_bottom) {
1886 node->type.tv = tarval_reachable;
1888 assert(selector->type.tv == tarval_top);
1889 node->type.tv = tarval_unreachable;
1894 if (selector->type.tv == tarval_bottom) {
1895 node->type.tv = tarval_reachable;
1896 } else if (selector->type.tv == tarval_top) {
1897 node->type.tv = tarval_unreachable;
1899 long value = get_tarval_long(selector->type.tv);
1900 if (pnc == get_Cond_defaultProj(cond)) {
1901 /* default switch, have to check ALL other cases */
1904 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1905 ir_node *succ = get_irn_out(cond, i);
1909 if (value == get_Proj_proj(succ)) {
1910 /* we found a match, will NOT take the default case */
1911 node->type.tv = tarval_unreachable;
1915 /* all cases checked, no match, will take default case */
1916 node->type.tv = tarval_reachable;
1919 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1923 } /* compute_Proj_Cond */
1926 * (Re-)compute the type for a Proj-Node.
1928 * @param node the node
1930 static void compute_Proj(node_t *node) {
1931 ir_node *proj = node->node;
1932 ir_mode *mode = get_irn_mode(proj);
1933 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1934 ir_node *pred = get_Proj_pred(proj);
1936 if (block->type.tv == tarval_unreachable) {
1937 /* a Proj in a unreachable Block stay Top */
1938 node->type.tv = tarval_top;
1941 if (get_irn_node(pred)->type.tv == tarval_top) {
1942 /* if the predecessor is Top, its Proj follow */
1943 node->type.tv = tarval_top;
1947 if (mode == mode_M) {
1948 /* mode M is always bottom */
1949 node->type.tv = tarval_bottom;
1952 if (mode != mode_X) {
1954 compute_Proj_Cmp(node, pred);
1956 default_compute(node);
1959 /* handle mode_X nodes */
1961 switch (get_irn_opcode(pred)) {
1963 /* the Proj_X from the Start is always reachable.
1964 However this is already handled at the top. */
1965 node->type.tv = tarval_reachable;
1968 compute_Proj_Cond(node, pred);
1971 default_compute(node);
1973 } /* compute_Proj */
1976 * (Re-)compute the type for a Confirm.
1978 * @param node the node
1980 static void compute_Confirm(node_t *node) {
1981 ir_node *confirm = node->node;
1982 node_t *pred = get_irn_node(get_Confirm_value(confirm));
1984 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1985 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1987 if (is_con(bound->type)) {
1988 /* is equal to a constant */
1989 node->type = bound->type;
1993 /* a Confirm is a copy OR a Const */
1994 node->type = pred->type;
1995 } /* compute_Confirm */
1998 * (Re-)compute the type for a Max.
2000 * @param node the node
2002 static void compute_Max(node_t *node) {
2003 ir_node *op = node->node;
2004 node_t *l = get_irn_node(get_binop_left(op));
2005 node_t *r = get_irn_node(get_binop_right(op));
2006 lattice_elem_t a = l->type;
2007 lattice_elem_t b = r->type;
2009 if (a.tv == tarval_top || b.tv == tarval_top) {
2010 node->type.tv = tarval_top;
2011 } else if (is_con(a) && is_con(b)) {
2012 /* both nodes are constants, we can probably do something */
2014 /* this case handles symconsts as well */
2017 ir_mode *mode = get_irn_mode(op);
2018 tarval *tv_min = get_mode_min(mode);
2022 else if (b.tv == tv_min)
2024 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2025 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2026 node->type.tv = a.tv;
2028 node->type.tv = b.tv;
2030 node->type.tv = tarval_bad;
2033 } else if (r->part == l->part) {
2034 /* both nodes congruent, we can probably do something */
2037 node->type.tv = tarval_bottom;
2042 * (Re-)compute the type for a Min.
2044 * @param node the node
2046 static void compute_Min(node_t *node) {
2047 ir_node *op = node->node;
2048 node_t *l = get_irn_node(get_binop_left(op));
2049 node_t *r = get_irn_node(get_binop_right(op));
2050 lattice_elem_t a = l->type;
2051 lattice_elem_t b = r->type;
2053 if (a.tv == tarval_top || b.tv == tarval_top) {
2054 node->type.tv = tarval_top;
2055 } else if (is_con(a) && is_con(b)) {
2056 /* both nodes are constants, we can probably do something */
2058 /* this case handles symconsts as well */
2061 ir_mode *mode = get_irn_mode(op);
2062 tarval *tv_max = get_mode_max(mode);
2066 else if (b.tv == tv_max)
2068 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2069 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2070 node->type.tv = a.tv;
2072 node->type.tv = b.tv;
2074 node->type.tv = tarval_bad;
2077 } else if (r->part == l->part) {
2078 /* both nodes congruent, we can probably do something */
2081 node->type.tv = tarval_bottom;
2086 * (Re-)compute the type for a given node.
2088 * @param node the node
2090 static void compute(node_t *node) {
2093 if (is_no_Block(node->node)) {
2094 node_t *block = get_irn_node(get_nodes_block(node->node));
2096 if (block->type.tv == tarval_unreachable) {
2097 node->type.tv = tarval_top;
2102 func = (compute_func)node->node->op->ops.generic;
2108 * Identity functions: Note that one might thing that identity() is just a
2109 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2110 * here, because it expects that the identity node is one of the inputs, which is NOT
2111 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2112 * So, we have our own implementation, which copies some parts of equivalent_node()
2116 * Calculates the Identity for Phi nodes
2118 static node_t *identity_Phi(node_t *node) {
2119 ir_node *phi = node->node;
2120 ir_node *block = get_nodes_block(phi);
2121 node_t *n_part = NULL;
2124 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2125 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2127 if (pred_X->type.tv == tarval_reachable) {
2128 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2132 else if (n_part->part != pred->part) {
2133 /* incongruent inputs, not a follower */
2138 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2139 * tarval_top, is in the TOP partition and should NOT being split! */
2140 assert(n_part != NULL);
2142 } /* identity_Phi */
2145 * Calculates the Identity for commutative 0 neutral nodes.
2147 static node_t *identity_comm_zero_binop(node_t *node) {
2148 ir_node *op = node->node;
2149 node_t *a = get_irn_node(get_binop_left(op));
2150 node_t *b = get_irn_node(get_binop_right(op));
2151 ir_mode *mode = get_irn_mode(op);
2154 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2155 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2158 /* node: no input should be tarval_top, else the binop would be also
2159 * Top and not being split. */
2160 zero = get_mode_null(mode);
2161 if (a->type.tv == zero)
2163 if (b->type.tv == zero)
2166 } /* identity_comm_zero_binop */
2168 #define identity_Add identity_comm_zero_binop
2169 #define identity_Or identity_comm_zero_binop
2172 * Calculates the Identity for Mul nodes.
2174 static node_t *identity_Mul(node_t *node) {
2175 ir_node *op = node->node;
2176 node_t *a = get_irn_node(get_Mul_left(op));
2177 node_t *b = get_irn_node(get_Mul_right(op));
2178 ir_mode *mode = get_irn_mode(op);
2181 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2182 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2185 /* node: no input should be tarval_top, else the binop would be also
2186 * Top and not being split. */
2187 one = get_mode_one(mode);
2188 if (a->type.tv == one)
2190 if (b->type.tv == one)
2193 } /* identity_Mul */
2196 * Calculates the Identity for Sub nodes.
2198 static node_t *identity_Sub(node_t *node) {
2199 ir_node *sub = node->node;
2200 node_t *b = get_irn_node(get_Sub_right(sub));
2201 ir_mode *mode = get_irn_mode(sub);
2203 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2204 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2207 /* node: no input should be tarval_top, else the binop would be also
2208 * Top and not being split. */
2209 if (b->type.tv == get_mode_null(mode))
2210 return get_irn_node(get_Sub_left(sub));
2212 } /* identity_Mul */
2215 * Calculates the Identity for And nodes.
2217 static node_t *identity_And(node_t *node) {
2218 ir_node *and = node->node;
2219 node_t *a = get_irn_node(get_And_left(and));
2220 node_t *b = get_irn_node(get_And_right(and));
2221 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2223 /* node: no input should be tarval_top, else the And would be also
2224 * Top and not being split. */
2225 if (a->type.tv == neutral)
2227 if (b->type.tv == neutral)
2230 } /* identity_And */
2233 * Calculates the Identity for Confirm nodes.
2235 static node_t *identity_Confirm(node_t *node) {
2236 ir_node *confirm = node->node;
2238 /* a Confirm is always a Copy */
2239 return get_irn_node(get_Confirm_value(confirm));
2240 } /* identity_Confirm */
2243 * Calculates the Identity for Mux nodes.
2245 static node_t *identity_Mux(node_t *node) {
2246 ir_node *mux = node->node;
2247 node_t *t = get_irn_node(get_Mux_true(mux));
2248 node_t *f = get_irn_node(get_Mux_false(mux));
2251 if (t->part == f->part)
2254 /* for now, the 1-input identity is not supported */
2256 sel = get_irn_node(get_Mux_sel(mux));
2258 /* Mux sel input is mode_b, so it is always a tarval */
2259 if (sel->type.tv == tarval_b_true)
2261 if (sel->type.tv == tarval_b_false)
2265 } /* identity_Mux */
2268 * Calculates the Identity for Min nodes.
2270 static node_t *identity_Min(node_t *node) {
2271 ir_node *op = node->node;
2272 node_t *a = get_irn_node(get_binop_left(op));
2273 node_t *b = get_irn_node(get_binop_right(op));
2274 ir_mode *mode = get_irn_mode(op);
2277 if (a->part == b->part) {
2278 /* leader of multiple predecessors */
2282 /* works even with NaN */
2283 tv_max = get_mode_max(mode);
2284 if (a->type.tv == tv_max)
2286 if (b->type.tv == tv_max)
2289 } /* identity_Min */
2292 * Calculates the Identity for Max nodes.
2294 static node_t *identity_Max(node_t *node) {
2295 ir_node *op = node->node;
2296 node_t *a = get_irn_node(get_binop_left(op));
2297 node_t *b = get_irn_node(get_binop_right(op));
2298 ir_mode *mode = get_irn_mode(op);
2301 if (a->part == b->part) {
2302 /* leader of multiple predecessors */
2306 /* works even with NaN */
2307 tv_min = get_mode_min(mode);
2308 if (a->type.tv == tv_min)
2310 if (b->type.tv == tv_min)
2313 } /* identity_Max */
2316 * Calculates the Identity for nodes.
2318 static node_t *identity(node_t *node) {
2319 ir_node *irn = node->node;
2321 switch (get_irn_opcode(irn)) {
2323 return identity_Phi(node);
2325 return identity_Add(node);
2327 return identity_Mul(node);
2329 return identity_Or(node);
2331 return identity_And(node);
2333 return identity_Sub(node);
2335 return identity_Confirm(node);
2337 return identity_Mux(node);
2339 return identity_Min(node);
2341 return identity_Max(node);
2348 * Node follower is a (new) follower of leader, segregate Leader
2351 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2352 ir_node *l = leader->node;
2353 int j, i, n = get_irn_n_outs(l);
2355 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2356 /* The leader edges must remain sorted, but follower edges can
2358 for (i = leader->n_followers + 1; i <= n; ++i) {
2359 if (l->out[i].use == follower) {
2360 ir_def_use_edge t = l->out[i];
2362 for (j = i - 1; j >= leader->n_followers + 1; --j)
2363 l->out[j + 1] = l->out[j];
2364 ++leader->n_followers;
2365 l->out[leader->n_followers] = t;
2369 } /* segregate_def_use_chain_1 */
2372 * Node follower is a (new) follower of leader, segregate Leader
2373 * out edges. If follower is a n-congruent Input identity, all follower
2374 * inputs congruent to follower are also leader.
2376 * @param follower the follower IR node
2378 static void segregate_def_use_chain(const ir_node *follower) {
2381 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2382 node_t *pred = get_irn_node(get_irn_n(follower, i));
2384 segregate_def_use_chain_1(follower, pred);
2386 } /* segregate_def_use_chain */
2389 * Propagate constant evaluation.
2391 * @param env the environment
2393 static void propagate(environment_t *env) {
2396 lattice_elem_t old_type;
2398 unsigned n_fallen, old_type_was_T_or_C;
2401 while (env->cprop != NULL) {
2402 void *oldopcode = NULL;
2404 /* remove the first partition X from cprop */
2407 env->cprop = X->cprop_next;
2409 old_type_was_T_or_C = X->type_is_T_or_C;
2411 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2414 while (! list_empty(&X->cprop)) {
2415 /* remove the first Node x from X.cprop */
2416 x = list_entry(X->cprop.next, node_t, cprop_list);
2417 //assert(x->part == X);
2418 list_del(&x->cprop_list);
2421 if (x->is_follower && identity(x) == x) {
2422 /* check the opcode first */
2423 if (oldopcode == NULL) {
2424 oldopcode = lambda_opcode(get_first_node(X), env);
2426 if (oldopcode != lambda_opcode(x, env)) {
2427 if (x->on_fallen == 0) {
2428 /* different opcode -> x falls out of this partition */
2433 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2437 /* x will make the follower -> leader transition */
2438 follower_to_leader(x);
2441 /* compute a new type for x */
2443 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2445 if (x->type.tv != old_type.tv) {
2446 verify_type(old_type, x->type);
2447 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2449 if (x->on_fallen == 0) {
2450 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2451 not already on the list. */
2456 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2458 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2459 ir_node *succ = get_irn_out(x->node, i);
2460 node_t *y = get_irn_node(succ);
2462 /* Add y to y.partition.cprop. */
2463 add_to_cprop(y, env);
2468 if (n_fallen > 0 && n_fallen != X->n_leader) {
2469 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2470 Y = split(&X, fallen, env);
2472 * We have split out fallen node. The type of the result
2473 * partition is NOT set yet.
2475 Y->type_is_T_or_C = 0;
2479 /* remove the flags from the fallen list */
2480 for (x = fallen; x != NULL; x = x->next)
2484 if (old_type_was_T_or_C) {
2487 if (Y->on_worklist == 0)
2488 add_to_worklist(Y, env);
2490 /* check if some nodes will make the leader -> follower transition */
2491 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2492 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2493 node_t *eq_node = identity(y);
2495 if (eq_node != y && eq_node->part == y->part) {
2496 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2497 /* move to Follower */
2499 list_del(&y->node_list);
2500 list_add_tail(&y->node_list, &Y->Follower);
2503 segregate_def_use_chain(y->node);
2514 * Get the leader for a given node from its congruence class.
2516 * @param irn the node
2518 static ir_node *get_leader(node_t *node) {
2519 partition_t *part = node->part;
2521 if (part->n_leader > 1 || node->is_follower) {
2522 if (node->is_follower) {
2523 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2526 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2528 return get_first_node(part)->node;
2534 * Return non-zero if the control flow predecessor node pred
2535 * is the only reachable control flow exit of its block.
2537 * @param pred the control flow exit
2539 static int can_exchange(ir_node *pred) {
2542 else if (is_Jmp(pred))
2544 else if (get_irn_mode(pred) == mode_T) {
2547 /* if the predecessor block has more than one
2548 reachable outputs we cannot remove the block */
2550 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2551 ir_node *proj = get_irn_out(pred, i);
2554 /* skip non-control flow Proj's */
2555 if (get_irn_mode(proj) != mode_X)
2558 node = get_irn_node(proj);
2559 if (node->type.tv == tarval_reachable) {
2570 * Block Post-Walker, apply the analysis results on control flow by
2571 * shortening Phi's and Block inputs.
2573 static void apply_cf(ir_node *block, void *ctx) {
2574 environment_t *env = ctx;
2575 node_t *node = get_irn_node(block);
2577 ir_node **ins, **in_X;
2578 ir_node *phi, *next;
2580 if (block == get_irg_end_block(current_ir_graph) ||
2581 block == get_irg_start_block(current_ir_graph)) {
2582 /* the EndBlock is always reachable even if the analysis
2583 finds out the opposite :-) */
2586 if (node->type.tv == tarval_unreachable) {
2587 /* mark dead blocks */
2588 set_Block_dead(block);
2592 n = get_Block_n_cfgpreds(block);
2595 /* only one predecessor combine */
2596 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2598 if (can_exchange(pred)) {
2599 exchange(block, get_nodes_block(pred));
2605 NEW_ARR_A(ir_node *, in_X, n);
2607 for (i = 0; i < n; ++i) {
2608 ir_node *pred = get_Block_cfgpred(block, i);
2609 node_t *node = get_irn_node(pred);
2611 if (node->type.tv == tarval_reachable) {
2618 NEW_ARR_A(ir_node *, ins, n);
2619 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2620 node_t *node = get_irn_node(phi);
2622 next = get_Phi_next(phi);
2623 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2624 /* this Phi is replaced by a constant */
2625 tarval *tv = node->type.tv;
2626 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2628 set_irn_node(c, node);
2630 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2635 for (i = 0; i < n; ++i) {
2636 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2638 if (pred->type.tv == tarval_reachable) {
2639 ins[j++] = get_Phi_pred(phi, i);
2643 /* this Phi is replaced by a single predecessor */
2644 ir_node *s = ins[0];
2647 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2651 set_irn_in(phi, j, ins);
2658 /* this Block has only one live predecessor */
2659 ir_node *pred = skip_Proj(in_X[0]);
2661 if (can_exchange(pred)) {
2662 exchange(block, get_nodes_block(pred));
2666 set_irn_in(block, k, in_X);
2672 * Post-Walker, apply the analysis results;
2674 static void apply_result(ir_node *irn, void *ctx) {
2675 environment_t *env = ctx;
2676 node_t *node = get_irn_node(irn);
2678 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2679 /* blocks already handled, do not touch the End node */
2681 node_t *block = get_irn_node(get_nodes_block(irn));
2683 if (block->type.tv == tarval_unreachable) {
2684 ir_node *bad = get_irg_bad(current_ir_graph);
2686 /* here, bad might already have a node, but this can be safely ignored
2687 as long as bad has at least ONE valid node */
2688 set_irn_node(bad, node);
2690 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2694 else if (node->type.tv == tarval_unreachable) {
2695 ir_node *bad = get_irg_bad(current_ir_graph);
2697 /* see comment above */
2698 set_irn_node(bad, node);
2700 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2704 else if (get_irn_mode(irn) == mode_X) {
2707 ir_node *cond = get_Proj_pred(irn);
2709 if (is_Cond(cond)) {
2710 node_t *sel = get_irn_node(get_Cond_selector(cond));
2712 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2713 /* Cond selector is a constant, make a Jmp */
2714 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2715 set_irn_node(jmp, node);
2717 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2724 /* normal data node */
2725 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2726 tarval *tv = node->type.tv;
2729 * Beware: never replace mode_T nodes by constants. Currently we must mark
2730 * mode_T nodes with constants, but do NOT replace them.
2732 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2733 /* can be replaced by a constant */
2734 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2735 set_irn_node(c, node);
2737 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2741 } else if (is_entity(node->type.sym.entity_p)) {
2742 if (! is_SymConst(irn)) {
2743 /* can be replaced by a Symconst */
2744 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2745 set_irn_node(symc, node);
2748 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2749 exchange(irn, symc);
2752 } else if (is_Confirm(irn)) {
2753 /* Confirms are always follower, but do not kill them here */
2755 ir_node *leader = get_leader(node);
2757 if (leader != irn) {
2758 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2759 exchange(irn, leader);
2765 } /* apply_result */
2768 * Fix the keep-alives by deleting unreachable ones.
2770 static void apply_end(ir_node *end, environment_t *env) {
2771 int i, j, n = get_End_n_keepalives(end);
2775 NEW_ARR_A(ir_node *, in, n);
2777 /* fix the keep alive */
2778 for (i = j = 0; i < n; i++) {
2779 ir_node *ka = get_End_keepalive(end, i);
2780 node_t *node = get_irn_node(ka);
2782 /* Use the flagged bits to mark already visited nodes.
2783 * This should not be ready but better safe than sorry. */
2784 if (node->flagged == 0) {
2788 node = get_irn_node(get_nodes_block(ka));
2790 if (node->type.tv != tarval_unreachable)
2795 set_End_keepalives(end, j, in);
2800 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2803 * sets the generic functions to compute.
2805 static void set_compute_functions(void) {
2808 /* set the default compute function */
2809 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2810 ir_op *op = get_irp_opcode(i);
2811 op->ops.generic = (op_func)default_compute;
2814 /* set specific functions */
2833 } /* set_compute_functions */
2835 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2836 ir_node *irn = local != NULL ? local : n;
2837 node_t *node = get_irn_node(irn);
2839 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2843 void combo(ir_graph *irg) {
2845 ir_node *initial_bl;
2847 ir_graph *rem = current_ir_graph;
2849 current_ir_graph = irg;
2851 /* register a debug mask */
2852 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2853 //firm_dbg_set_mask(dbg, SET_LEVEL_3);
2855 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2857 obstack_init(&env.obst);
2858 env.worklist = NULL;
2862 #ifdef DEBUG_libfirm
2863 env.dbg_list = NULL;
2865 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
2866 env.type2id_map = pmap_create();
2867 env.end_idx = get_opt_global_cse() ? 0 : -1;
2868 env.lambda_input = 0;
2869 env.nonstd_cond = 0;
2872 assure_irg_outs(irg);
2874 /* we have our own value_of function */
2875 set_value_of_func(get_node_tarval);
2877 set_compute_functions();
2878 DEBUG_ONLY(part_nr = 0);
2880 /* create the initial partition and place it on the work list */
2881 env.initial = new_partition(&env);
2882 add_to_worklist(env.initial, &env);
2883 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2885 tarval_UNKNOWN = env.nonstd_cond ? tarval_bad : tarval_top;
2887 /* all nodes on the initial partition have type Top */
2888 env.initial->type_is_T_or_C = 1;
2890 /* Place the START Node's partition on cprop.
2891 Place the START Node on its local worklist. */
2892 initial_bl = get_irg_start_block(irg);
2893 start = get_irn_node(initial_bl);
2894 add_to_cprop(start, &env);
2898 if (env.worklist != NULL)
2900 } while (env.cprop != NULL || env.worklist != NULL);
2902 dump_all_partitions(&env);
2903 check_all_partitions(&env);
2906 set_dump_node_vcgattr_hook(dump_partition_hook);
2907 dump_ir_block_graph(irg, "-partition");
2908 set_dump_node_vcgattr_hook(NULL);
2910 (void)dump_partition_hook;
2913 /* apply the result */
2914 irg_block_walk_graph(irg, NULL, apply_cf, &env);
2915 irg_walk_graph(irg, NULL, apply_result, &env);
2916 apply_end(get_irg_end(irg), &env);
2919 /* control flow might changed */
2920 set_irg_outs_inconsistent(irg);
2921 set_irg_extblk_inconsistent(irg);
2922 set_irg_doms_inconsistent(irg);
2923 set_irg_loopinfo_inconsistent(irg);
2926 pmap_destroy(env.type2id_map);
2927 del_set(env.opcode2id_map);
2928 obstack_free(&env.obst, NULL);
2930 /* restore value_of() default behavior */
2931 set_value_of_func(NULL);
2932 current_ir_graph = rem;