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 int modified; /**< Set, if the graph was modified. */
172 partition_t *dbg_list; /**< List of all partitions. */
176 /** Type of the what function. */
177 typedef void *(*what_func)(const node_t *node, environment_t *env);
179 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
180 #define set_irn_node(follower, node) set_irn_link(follower, node)
182 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
183 #undef tarval_unreachable
184 #define tarval_unreachable tarval_top
187 /** The debug module handle. */
188 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
190 /** Next partition number. */
191 DEBUG_ONLY(static unsigned part_nr = 0);
194 static node_t *identity(node_t *node);
196 #ifdef CHECK_PARTITIONS
200 static void check_partition(const partition_t *T) {
204 list_for_each_entry(node_t, node, &T->Leader, node_list) {
205 assert(node->is_follower == 0);
206 assert(node->flagged == 0);
207 assert(node->part == T);
210 assert(n == T->n_leader);
212 list_for_each_entry(node_t, node, &T->Follower, node_list) {
213 assert(node->is_follower == 1);
214 assert(node->flagged == 0);
215 assert(node->part == T);
217 } /* check_partition */
219 static void check_all_partitions(environment_t *env) {
223 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
225 list_for_each_entry(node_t, node, &P->Follower, node_list) {
226 node_t *leader = identity(node);
228 assert(leader != node && leader->part == node->part);
236 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
239 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
240 for (e = list; e != NULL; e = NEXT(e)) {
241 assert(e->part == Z);
244 } /* ido_check_list */
247 * Check a local list.
249 static void check_list(const node_t *list, const partition_t *Z) {
250 do_check_list(list, offsetof(node_t, next), Z);
254 #define check_partition(T)
255 #define check_list(list, Z)
256 #define check_all_partitions(env)
257 #endif /* CHECK_PARTITIONS */
260 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
263 * Dump partition to output.
265 static void dump_partition(const char *msg, const partition_t *part) {
268 lattice_elem_t type = get_partition_type(part);
270 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
271 msg, part->nr, part->type_is_T_or_C ? "*" : "",
272 part->n_leader, type));
273 list_for_each_entry(node_t, node, &part->Leader, node_list) {
274 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
277 if (! list_empty(&part->Follower)) {
278 DB((dbg, LEVEL_2, "\n---\n "));
280 list_for_each_entry(node_t, node, &part->Follower, node_list) {
281 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
285 DB((dbg, LEVEL_2, "\n}\n"));
286 } /* dump_partition */
291 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
295 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
297 DB((dbg, LEVEL_3, "%s = {\n ", msg));
298 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
299 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
302 DB((dbg, LEVEL_3, "\n}\n"));
310 static void dump_race_list(const char *msg, const node_t *list) {
311 do_dump_list(msg, list, offsetof(node_t, race_next));
315 * Dumps a local list.
317 static void dump_list(const char *msg, const node_t *list) {
318 do_dump_list(msg, list, offsetof(node_t, next));
322 * Dump all partitions.
324 static void dump_all_partitions(const environment_t *env) {
325 const partition_t *P;
327 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
328 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
329 dump_partition("", P);
333 #define dump_partition(msg, part)
334 #define dump_race_list(msg, list)
335 #define dump_list(msg, list)
336 #define dump_all_partitions(env)
339 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
341 * Verify that a type transition is monotone
343 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
344 if (old_type.tv == new_type.tv) {
348 if (old_type.tv == tarval_top) {
349 /* from Top down-to is always allowed */
352 if (old_type.tv == tarval_reachable) {
353 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
355 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
359 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
362 #define verify_type(old_type, new_type)
366 * Compare two pointer values of a listmap.
368 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
369 const listmap_entry_t *e1 = elt;
370 const listmap_entry_t *e2 = key;
373 return e1->id != e2->id;
374 } /* listmap_cmp_ptr */
377 * Initializes a listmap.
379 * @param map the listmap
381 static void listmap_init(listmap_t *map) {
382 map->map = new_set(listmap_cmp_ptr, 16);
387 * Terminates a listmap.
389 * @param map the listmap
391 static void listmap_term(listmap_t *map) {
396 * Return the associated listmap entry for a given id.
398 * @param map the listmap
399 * @param id the id to search for
401 * @return the asociated listmap entry for the given id
403 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
404 listmap_entry_t key, *entry;
409 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
411 if (entry->list == NULL) {
412 /* a new entry, put into the list */
413 entry->next = map->values;
420 * Calculate the hash value for an opcode map entry.
422 * @param entry an opcode map entry
424 * @return a hash value for the given opcode map entry
426 static unsigned opcode_hash(const opcode_key_t *entry) {
427 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
431 * Compare two entries in the opcode map.
433 static int cmp_opcode(const void *elt, const void *key, size_t size) {
434 const opcode_key_t *o1 = elt;
435 const opcode_key_t *o2 = key;
438 return o1->code != o2->code || o1->mode != o2->mode ||
439 o1->arity != o2->arity ||
440 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
444 * Compare two Def-Use edges for input position.
446 static int cmp_def_use_edge(const void *a, const void *b) {
447 const ir_def_use_edge *ea = a;
448 const ir_def_use_edge *eb = b;
450 /* no overrun, because range is [-1, MAXINT] */
451 return ea->pos - eb->pos;
452 } /* cmp_def_use_edge */
455 * We need the Def-Use edges sorted.
457 static void sort_irn_outs(node_t *node) {
458 ir_node *irn = node->node;
459 int n_outs = get_irn_n_outs(irn);
462 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
464 node->max_user_input = irn->out[n_outs].pos;
465 } /* sort_irn_outs */
468 * Return the type of a node.
470 * @param irn an IR-node
472 * @return the associated type of this node
474 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
475 return get_irn_node(irn)->type;
476 } /* get_node_type */
479 * Return the tarval of a node.
481 * @param irn an IR-node
483 * @return the associated type of this node
485 static INLINE tarval *get_node_tarval(const ir_node *irn) {
486 lattice_elem_t type = get_node_type(irn);
488 if (is_tarval(type.tv))
490 return tarval_bottom;
491 } /* get_node_type */
494 * Add a partition to the worklist.
496 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
497 assert(X->on_worklist == 0);
498 X->wl_next = env->worklist;
501 } /* add_to_worklist */
504 * Create a new empty partition.
506 * @param env the environment
508 * @return a newly allocated partition
510 static INLINE partition_t *new_partition(environment_t *env) {
511 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
513 INIT_LIST_HEAD(&part->Leader);
514 INIT_LIST_HEAD(&part->Follower);
515 INIT_LIST_HEAD(&part->cprop);
516 part->wl_next = NULL;
517 part->touched_next = NULL;
518 part->cprop_next = NULL;
519 part->split_next = NULL;
520 part->touched = NULL;
523 part->max_user_inputs = 0;
524 part->on_worklist = 0;
525 part->on_touched = 0;
527 part->type_is_T_or_C = 0;
529 part->dbg_next = env->dbg_list;
530 env->dbg_list = part;
531 part->nr = part_nr++;
535 } /* new_partition */
538 * Get the first node from a partition.
540 static INLINE node_t *get_first_node(const partition_t *X) {
541 return list_entry(X->Leader.next, node_t, node_list);
542 } /* get_first_node */
545 * Return the type of a partition (assuming partition is non-empty and
546 * all elements have the same type).
548 * @param X a partition
550 * @return the type of the first element of the partition
552 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
553 const node_t *first = get_first_node(X);
555 } /* get_partition_type */
558 * Creates a partition node for the given IR-node and place it
559 * into the given partition.
561 * @param irn an IR-node
562 * @param part a partition to place the node in
563 * @param env the environment
565 * @return the created node
567 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
568 /* create a partition node and place it in the partition */
569 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
571 INIT_LIST_HEAD(&node->node_list);
572 INIT_LIST_HEAD(&node->cprop_list);
576 node->race_next = NULL;
577 node->type.tv = tarval_top;
578 node->max_user_input = 0;
580 node->n_followers = 0;
581 node->on_touched = 0;
584 node->is_follower = 0;
585 node->by_all_const = 0;
587 set_irn_node(irn, node);
589 list_add_tail(&node->node_list, &part->Leader);
593 } /* create_partition_node */
596 * Pre-Walker, init all Block-Phi lists.
598 static void init_block_phis(ir_node *irn, void *env) {
602 set_Block_phis(irn, NULL);
604 } /* init_block_phis */
607 * Post-Walker, initialize all Nodes' type to U or top and place
608 * all nodes into the TOP partition.
610 static void create_initial_partitions(ir_node *irn, void *ctx) {
611 environment_t *env = ctx;
612 partition_t *part = env->initial;
615 node = create_partition_node(irn, part, env);
617 if (node->max_user_input > part->max_user_inputs)
618 part->max_user_inputs = node->max_user_input;
621 add_Block_phi(get_nodes_block(irn), irn);
623 } /* create_initial_partitions */
626 * Add a node to the entry.partition.touched set and
627 * node->partition to the touched set if not already there.
630 * @param env the environment
632 static INLINE void add_to_touched(node_t *y, environment_t *env) {
633 if (y->on_touched == 0) {
634 partition_t *part = y->part;
636 y->next = part->touched;
641 if (part->on_touched == 0) {
642 part->touched_next = env->touched;
644 part->on_touched = 1;
647 check_list(part->touched, part);
649 } /* add_to_touched */
652 * Place a node on the cprop list.
655 * @param env the environment
657 static void add_to_cprop(node_t *y, environment_t *env) {
658 /* Add y to y.partition.cprop. */
659 if (y->on_cprop == 0) {
660 partition_t *Y = y->part;
662 list_add_tail(&y->cprop_list, &Y->cprop);
665 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
667 /* place its partition on the cprop list */
668 if (Y->on_cprop == 0) {
669 Y->cprop_next = env->cprop;
674 if (get_irn_mode(y->node) == mode_T) {
675 /* mode_T nodes always produce tarval_bottom, so we must explicitly
676 add it's Proj's to get constant evaluation to work */
679 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
680 node_t *proj = get_irn_node(get_irn_out(y->node, i));
682 add_to_cprop(proj, env);
684 } else if (is_Block(y->node)) {
685 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
686 * if someone placed the block. The Block is only placed if the reachability
687 * changes, and this must be re-evaluated in compute_Phi(). */
689 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
690 node_t *p = get_irn_node(phi);
691 add_to_cprop(p, env);
697 * Update the worklist: If Z is on worklist then add Z' to worklist.
698 * Else add the smaller of Z and Z' to worklist.
700 * @param Z the Z partition
701 * @param Z_prime the Z' partition, a previous part of Z
702 * @param env the environment
704 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
705 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
706 add_to_worklist(Z_prime, env);
708 add_to_worklist(Z, env);
710 } /* update_worklist */
713 * Make all inputs to x no longer be F.def_use edges.
717 static void move_edges_to_leader(node_t *x) {
718 ir_node *irn = x->node;
721 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
722 node_t *pred = get_irn_node(get_irn_n(irn, i));
727 n = get_irn_n_outs(p);
728 for (j = 1; j <= pred->n_followers; ++j) {
729 if (p->out[j].pos == i && p->out[j].use == irn) {
730 /* found a follower edge to x, move it to the Leader */
731 ir_def_use_edge edge = p->out[j];
733 /* remove this edge from the Follower set */
734 p->out[j] = p->out[pred->n_followers];
737 /* sort it into the leader set */
738 for (k = pred->n_followers + 2; k <= n; ++k) {
739 if (p->out[k].pos >= edge.pos)
741 p->out[k - 1] = p->out[k];
743 /* place the new edge here */
744 p->out[k - 1] = edge;
746 /* edge found and moved */
751 } /* move_edges_to_leader */
754 * Split a partition that has NO followers by a local list.
756 * @param Z partition to split
757 * @param g a (non-empty) node list
758 * @param env the environment
760 * @return a new partition containing the nodes of g
762 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
763 partition_t *Z_prime;
768 dump_partition("Splitting ", Z);
769 dump_list("by list ", g);
773 /* Remove g from Z. */
774 for (node = g; node != NULL; node = node->next) {
775 assert(node->part == Z);
776 list_del(&node->node_list);
779 assert(n < Z->n_leader);
782 /* Move g to a new partition, Z'. */
783 Z_prime = new_partition(env);
785 for (node = g; node != NULL; node = node->next) {
786 list_add_tail(&node->node_list, &Z_prime->Leader);
787 node->part = Z_prime;
788 if (node->max_user_input > max_input)
789 max_input = node->max_user_input;
791 Z_prime->max_user_inputs = max_input;
792 Z_prime->n_leader = n;
795 check_partition(Z_prime);
797 /* for now, copy the type info tag, it will be adjusted in split_by(). */
798 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
800 update_worklist(Z, Z_prime, env);
802 dump_partition("Now ", Z);
803 dump_partition("Created new ", Z_prime);
805 } /* split_no_followers */
809 #define split(Z, g, env) split_no_followers(*(Z), g, env)
814 * Make the Follower -> Leader transition for a node.
818 static void follower_to_leader(node_t *n) {
819 assert(n->is_follower == 1);
821 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
823 move_edges_to_leader(n);
824 list_del(&n->node_list);
825 list_add_tail(&n->node_list, &n->part->Leader);
827 } /* follower_to_leader */
830 * The environment for one race step.
832 typedef struct step_env {
833 node_t *initial; /**< The initial node list. */
834 node_t *unwalked; /**< The unwalked node list. */
835 node_t *walked; /**< The walked node list. */
836 int index; /**< Next index of Follower use_def edge. */
837 unsigned side; /**< side number. */
841 * Return non-zero, if a input is a real follower
843 * @param irn the node to check
844 * @param input number of the input
846 static int is_real_follower(const ir_node *irn, int input) {
849 switch (get_irn_opcode(irn)) {
852 /* ignore the Confirm bound input */
858 /* ignore the Mux sel input */
863 /* dead inputs are not follower edges */
864 ir_node *block = get_nodes_block(irn);
865 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
867 if (pred->type.tv == tarval_unreachable)
873 /* only a Sub x,0 might be a follower */
879 pred = get_irn_node(get_irn_n(irn, input));
880 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
884 pred = get_irn_node(get_irn_n(irn, input));
885 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
889 pred = get_irn_node(get_irn_n(irn, input));
890 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
895 /* all inputs are followers */
898 assert(!"opcode not implemented yet");
905 * Do one step in the race.
907 static int step(step_env *env) {
910 if (env->initial != NULL) {
911 /* Move node from initial to unwalked */
913 env->initial = n->race_next;
915 n->race_next = env->unwalked;
921 while (env->unwalked != NULL) {
922 /* let n be the first node in unwalked */
924 while (env->index < n->n_followers) {
925 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
927 /* let m be n.F.def_use[index] */
928 node_t *m = get_irn_node(edge->use);
930 assert(m->is_follower);
932 * Some inputs, like the get_Confirm_bound are NOT
933 * real followers, sort them out.
935 if (! is_real_follower(m->node, edge->pos)) {
941 /* only followers from our partition */
942 if (m->part != n->part)
945 if ((m->flagged & env->side) == 0) {
946 m->flagged |= env->side;
948 if (m->flagged != 3) {
949 /* visited the first time */
950 /* add m to unwalked not as first node (we might still need to
951 check for more follower node */
952 m->race_next = n->race_next;
956 /* else already visited by the other side and on the other list */
959 /* move n to walked */
960 env->unwalked = n->race_next;
961 n->race_next = env->walked;
969 * Clear the flags from a list and check for
970 * nodes that where touched from both sides.
972 * @param list the list
974 static int clear_flags(node_t *list) {
978 for (n = list; n != NULL; n = n->race_next) {
979 if (n->flagged == 3) {
980 /* we reach a follower from both sides, this will split congruent
981 * inputs and make it a leader. */
982 follower_to_leader(n);
991 * Split a partition by a local list using the race.
993 * @param pX pointer to the partition to split, might be changed!
994 * @param gg a (non-empty) node list
995 * @param env the environment
997 * @return a new partition containing the nodes of gg
999 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1000 partition_t *X = *pX;
1001 partition_t *X_prime;
1003 step_env env1, env2, *winner;
1004 node_t *g, *h, *node, *t;
1005 int max_input, transitions;
1007 DEBUG_ONLY(static int run = 0;)
1009 DB((dbg, LEVEL_2, "Run %d ", run++));
1010 if (list_empty(&X->Follower)) {
1011 /* if the partition has NO follower, we can use the fast
1012 splitting algorithm. */
1013 return split_no_followers(X, gg, env);
1015 /* else do the race */
1017 dump_partition("Splitting ", X);
1018 dump_list("by list ", gg);
1020 INIT_LIST_HEAD(&tmp);
1022 /* Remove gg from X.Leader and put into g */
1024 for (node = gg; node != NULL; node = node->next) {
1025 assert(node->part == X);
1026 assert(node->is_follower == 0);
1028 list_del(&node->node_list);
1029 list_add_tail(&node->node_list, &tmp);
1030 node->race_next = g;
1035 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1036 node->race_next = h;
1039 /* restore X.Leader */
1040 list_splice(&tmp, &X->Leader);
1043 env1.unwalked = NULL;
1049 env2.unwalked = NULL;
1064 assert(winner->initial == NULL);
1065 assert(winner->unwalked == NULL);
1067 /* clear flags from walked/unwalked */
1068 transitions = clear_flags(env1.unwalked);
1069 transitions |= clear_flags(env1.walked);
1070 transitions |= clear_flags(env2.unwalked);
1071 transitions |= clear_flags(env2.walked);
1073 dump_race_list("winner ", winner->walked);
1075 /* Move walked_{winner} to a new partition, X'. */
1076 X_prime = new_partition(env);
1079 for (node = winner->walked; node != NULL; node = node->race_next) {
1080 list_del(&node->node_list);
1081 node->part = X_prime;
1082 if (node->is_follower) {
1083 list_add_tail(&node->node_list, &X_prime->Follower);
1085 list_add_tail(&node->node_list, &X_prime->Leader);
1088 if (node->max_user_input > max_input)
1089 max_input = node->max_user_input;
1091 X_prime->n_leader = n;
1092 X_prime->max_user_inputs = max_input;
1093 X->n_leader -= X_prime->n_leader;
1095 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1096 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1099 * Even if a follower was not checked by both sides, it might have
1100 * loose its congruence, so we need to check this case for all follower.
1102 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1103 if (identity(node) == node) {
1104 follower_to_leader(node);
1110 check_partition(X_prime);
1112 /* X' is the smaller part */
1113 add_to_worklist(X_prime, env);
1116 * If there where follower to leader transitions, ensure that the nodes
1117 * can be split out if necessary.
1120 /* place partitions on the cprop list */
1121 if (X_prime->on_cprop == 0) {
1122 X_prime->cprop_next = env->cprop;
1123 env->cprop = X_prime;
1124 X_prime->on_cprop = 1;
1128 dump_partition("Now ", X);
1129 dump_partition("Created new ", X_prime);
1131 /* we have to ensure that the partition containing g is returned */
1132 if (winner == &env2) {
1139 #endif /* NO_FOLLOWER */
1142 * Returns non-zero if the i'th input of a Phi node is live.
1144 * @param phi a Phi-node
1145 * @param i an input number
1147 * @return non-zero if the i'th input of the given Phi node is live
1149 static int is_live_input(ir_node *phi, int i) {
1151 ir_node *block = get_nodes_block(phi);
1152 ir_node *pred = get_Block_cfgpred(block, i);
1153 lattice_elem_t type = get_node_type(pred);
1155 return type.tv != tarval_unreachable;
1157 /* else it's the control input, always live */
1159 } /* is_live_input */
1162 * Return non-zero if a type is a constant.
1164 static int is_constant_type(lattice_elem_t type) {
1165 if (type.tv != tarval_bottom && type.tv != tarval_top)
1168 } /* is_constant_type */
1171 * Check whether a type is neither Top or a constant.
1172 * Note: U is handled like Top here, R is a constant.
1174 * @param type the type to check
1176 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1177 if (is_tarval(type.tv)) {
1178 if (type.tv == tarval_top)
1180 if (tarval_is_constant(type.tv))
1190 * Collect nodes to the touched list.
1192 * @param list the list which contains the nodes that must be evaluated
1193 * @param idx the index of the def_use edge to evaluate
1194 * @param env the environment
1196 static void collect_touched(list_head *list, int idx, environment_t *env) {
1198 int end_idx = env->end_idx;
1200 list_for_each_entry(node_t, x, list, node_list) {
1204 /* leader edges start AFTER follower edges */
1205 x->next_edge = x->n_followers + 1;
1207 num_edges = get_irn_n_outs(x->node);
1209 /* for all edges in x.L.def_use_{idx} */
1210 while (x->next_edge <= num_edges) {
1211 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1214 /* check if we have necessary edges */
1215 if (edge->pos > idx)
1222 /* ignore the "control input" for non-pinned nodes
1223 if we are running in GCSE mode */
1224 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1227 y = get_irn_node(succ);
1228 assert(get_irn_n(succ, idx) == x->node);
1230 /* ignore block edges touching followers */
1231 if (idx == -1 && y->is_follower)
1234 if (is_constant_type(y->type)) {
1235 ir_opcode code = get_irn_opcode(succ);
1236 if (code == iro_Sub || code == iro_Cmp)
1237 add_to_cprop(y, env);
1240 /* Partitions of constants should not be split simply because their Nodes have unequal
1241 functions or incongruent inputs. */
1242 if (type_is_neither_top_nor_const(y->type) &&
1243 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1244 add_to_touched(y, env);
1248 } /* collect_touched */
1251 * Split the partitions if caused by the first entry on the worklist.
1253 * @param env the environment
1255 static void cause_splits(environment_t *env) {
1256 partition_t *X, *Z, *N;
1259 /* remove the first partition from the worklist */
1261 env->worklist = X->wl_next;
1264 dump_partition("Cause_split: ", X);
1266 /* combine temporary leader and follower list */
1267 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1268 /* empty the touched set: already done, just clear the list */
1269 env->touched = NULL;
1271 collect_touched(&X->Leader, idx, env);
1272 collect_touched(&X->Follower, idx, env);
1274 for (Z = env->touched; Z != NULL; Z = N) {
1276 node_t *touched = Z->touched;
1277 unsigned n_touched = Z->n_touched;
1279 assert(Z->touched != NULL);
1281 /* beware, split might change Z */
1282 N = Z->touched_next;
1284 /* remove it from the touched set */
1287 /* Empty local Z.touched. */
1288 for (e = touched; e != NULL; e = e->next) {
1289 assert(e->is_follower == 0);
1295 if (0 < n_touched && n_touched < Z->n_leader) {
1296 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1297 split(&Z, touched, env);
1299 assert(n_touched <= Z->n_leader);
1302 } /* cause_splits */
1305 * Implements split_by_what(): Split a partition by characteristics given
1306 * by the what function.
1308 * @param X the partition to split
1309 * @param What a function returning an Id for every node of the partition X
1310 * @param P a list to store the result partitions
1311 * @param env the environment
1315 static partition_t *split_by_what(partition_t *X, what_func What,
1316 partition_t **P, environment_t *env) {
1319 listmap_entry_t *iter;
1322 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1324 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1325 void *id = What(x, env);
1326 listmap_entry_t *entry;
1329 /* input not allowed, ignore */
1332 /* Add x to map[What(x)]. */
1333 entry = listmap_find(&map, id);
1334 x->next = entry->list;
1337 /* Let P be a set of Partitions. */
1339 /* for all sets S except one in the range of map do */
1340 for (iter = map.values; iter != NULL; iter = iter->next) {
1341 if (iter->next == NULL) {
1342 /* this is the last entry, ignore */
1347 /* Add SPLIT( X, S ) to P. */
1348 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1349 R = split(&X, S, env);
1359 } /* split_by_what */
1361 /** lambda n.(n.type) */
1362 static void *lambda_type(const node_t *node, environment_t *env) {
1364 return node->type.tv;
1367 /** lambda n.(n.opcode) */
1368 static void *lambda_opcode(const node_t *node, environment_t *env) {
1369 opcode_key_t key, *entry;
1370 ir_node *irn = node->node;
1372 key.code = get_irn_opcode(irn);
1373 key.mode = get_irn_mode(irn);
1374 key.arity = get_irn_arity(irn);
1378 switch (get_irn_opcode(irn)) {
1380 key.u.proj = get_Proj_proj(irn);
1383 key.u.ent = get_Sel_entity(irn);
1389 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1391 } /* lambda_opcode */
1393 /** lambda n.(n[i].partition) */
1394 static void *lambda_partition(const node_t *node, environment_t *env) {
1395 ir_node *skipped = skip_Proj(node->node);
1398 int i = env->lambda_input;
1400 if (i >= get_irn_arity(node->node)) {
1401 /* we are outside the allowed range */
1405 /* ignore the "control input" for non-pinned nodes
1406 if we are running in GCSE mode */
1407 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1410 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1411 p = get_irn_node(pred);
1414 } /* lambda_partition */
1417 * Returns true if a type is a constant.
1419 static int is_con(const lattice_elem_t type) {
1420 /* be conservative */
1421 if (is_tarval(type.tv))
1422 return tarval_is_constant(type.tv);
1423 return is_entity(type.sym.entity_p);
1427 * Implements split_by().
1429 * @param X the partition to split
1430 * @param env the environment
1432 static void split_by(partition_t *X, environment_t *env) {
1433 partition_t *I, *P = NULL;
1436 dump_partition("split_by", X);
1438 if (X->n_leader == 1) {
1439 /* we have only one leader, no need to split, just check it's type */
1440 node_t *x = get_first_node(X);
1441 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1445 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1446 P = split_by_what(X, lambda_type, &P, env);
1448 /* adjust the type tags, we have split partitions by type */
1449 for (I = P; I != NULL; I = I->split_next) {
1450 node_t *x = get_first_node(I);
1451 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1458 if (Y->n_leader > 1) {
1459 /* we do not want split the TOP or constant partitions */
1460 if (! Y->type_is_T_or_C) {
1461 partition_t *Q = NULL;
1463 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1464 Q = split_by_what(Y, lambda_opcode, &Q, env);
1470 if (Z->n_leader > 1) {
1471 const node_t *first = get_first_node(Z);
1472 int arity = get_irn_arity(first->node);
1476 * BEWARE: during splitting by input 2 for instance we might
1477 * create new partitions which are different by input 1, so collect
1478 * them and split further.
1480 Z->split_next = NULL;
1483 for (input = arity - 1; input >= -1; --input) {
1485 partition_t *Z_prime = R;
1488 if (Z_prime->n_leader > 1) {
1489 env->lambda_input = input;
1490 DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1491 S = split_by_what(Z_prime, lambda_partition, &S, env);
1493 Z_prime->split_next = S;
1496 } while (R != NULL);
1501 } while (Q != NULL);
1504 } while (P != NULL);
1508 * (Re-)compute the type for a given node.
1510 * @param node the node
1512 static void default_compute(node_t *node) {
1514 ir_node *irn = node->node;
1515 node_t *block = get_irn_node(get_nodes_block(irn));
1517 if (block->type.tv == tarval_unreachable) {
1518 node->type.tv = tarval_top;
1522 /* if any of the data inputs have type top, the result is type top */
1523 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1524 ir_node *pred = get_irn_n(irn, i);
1525 node_t *p = get_irn_node(pred);
1527 if (p->type.tv == tarval_top) {
1528 node->type.tv = tarval_top;
1533 if (get_irn_mode(node->node) == mode_X)
1534 node->type.tv = tarval_reachable;
1536 node->type.tv = computed_value(irn);
1537 } /* default_compute */
1540 * (Re-)compute the type for a Block node.
1542 * @param node the node
1544 static void compute_Block(node_t *node) {
1546 ir_node *block = node->node;
1548 if (block == get_irg_start_block(current_ir_graph)) {
1549 /* start block is always reachable */
1550 node->type.tv = tarval_reachable;
1554 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1555 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1557 if (pred->type.tv == tarval_reachable) {
1558 /* A block is reachable, if at least of predecessor is reachable. */
1559 node->type.tv = tarval_reachable;
1563 node->type.tv = tarval_top;
1564 } /* compute_Block */
1567 * (Re-)compute the type for a Bad node.
1569 * @param node the node
1571 static void compute_Bad(node_t *node) {
1572 /* Bad nodes ALWAYS compute Top */
1573 node->type.tv = tarval_top;
1577 * (Re-)compute the type for an Unknown node.
1579 * @param node the node
1581 static void compute_Unknown(node_t *node) {
1582 /* While Unknown nodes should compute Top this is dangerous:
1583 * a Top input to a Cond would lead to BOTH control flows unreachable.
1584 * While this is correct in the given semantics, it would destroy the Firm
1587 * It would be safe to compute Top IF it can be assured, that only Cmp
1588 * nodes are inputs to Conds. We check that first.
1589 * This is the way Frontends typically build Firm, but some optimizations
1590 * (cond_eval for instance) might replace them by Phib's...
1592 * For now, we compute bottom here.
1594 node->type.tv = tarval_bottom;
1595 } /* compute_Unknown */
1598 * (Re-)compute the type for a Jmp node.
1600 * @param node the node
1602 static void compute_Jmp(node_t *node) {
1603 node_t *block = get_irn_node(get_nodes_block(node->node));
1605 node->type = block->type;
1609 * (Re-)compute the type for the End node.
1611 * @param node the node
1613 static void compute_End(node_t *node) {
1614 /* the End node is NOT dead of course */
1615 node->type.tv = tarval_reachable;
1619 * (Re-)compute the type for a SymConst node.
1621 * @param node the node
1623 static void compute_SymConst(node_t *node) {
1624 ir_node *irn = node->node;
1625 node_t *block = get_irn_node(get_nodes_block(irn));
1627 if (block->type.tv == tarval_unreachable) {
1628 node->type.tv = tarval_top;
1631 switch (get_SymConst_kind(irn)) {
1632 case symconst_addr_ent:
1633 /* case symconst_addr_name: cannot handle this yet */
1634 node->type.sym = get_SymConst_symbol(irn);
1637 node->type.tv = computed_value(irn);
1639 } /* compute_SymConst */
1642 * (Re-)compute the type for a Phi node.
1644 * @param node the node
1646 static void compute_Phi(node_t *node) {
1648 ir_node *phi = node->node;
1649 lattice_elem_t type;
1651 /* if a Phi is in a unreachable block, its type is TOP */
1652 node_t *block = get_irn_node(get_nodes_block(phi));
1654 if (block->type.tv == tarval_unreachable) {
1655 node->type.tv = tarval_top;
1659 /* Phi implements the Meet operation */
1660 type.tv = tarval_top;
1661 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1662 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1663 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1665 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1666 /* ignore TOP inputs: We must check here for unreachable blocks,
1667 because Firm constants live in the Start Block are NEVER Top.
1668 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1669 comes from a unreachable input. */
1672 if (pred->type.tv == tarval_bottom) {
1673 node->type.tv = tarval_bottom;
1675 } else if (type.tv == tarval_top) {
1676 /* first constant found */
1678 } else if (type.tv != pred->type.tv) {
1679 /* different constants or tarval_bottom */
1680 node->type.tv = tarval_bottom;
1683 /* else nothing, constants are the same */
1689 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1691 * @param node the node
1693 static void compute_Add(node_t *node) {
1694 ir_node *sub = node->node;
1695 node_t *l = get_irn_node(get_Add_left(sub));
1696 node_t *r = get_irn_node(get_Add_right(sub));
1697 lattice_elem_t a = l->type;
1698 lattice_elem_t b = r->type;
1701 if (a.tv == tarval_top || b.tv == tarval_top) {
1702 node->type.tv = tarval_top;
1703 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1704 node->type.tv = tarval_bottom;
1706 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1707 must call tarval_add() first to handle this case! */
1708 if (is_tarval(a.tv)) {
1709 if (is_tarval(b.tv)) {
1710 node->type.tv = tarval_add(a.tv, b.tv);
1713 mode = get_tarval_mode(a.tv);
1714 if (a.tv == get_mode_null(mode)) {
1718 } else if (is_tarval(b.tv)) {
1719 mode = get_tarval_mode(b.tv);
1720 if (b.tv == get_mode_null(mode)) {
1725 node->type.tv = tarval_bottom;
1730 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1732 * @param node the node
1734 static void compute_Sub(node_t *node) {
1735 ir_node *sub = node->node;
1736 node_t *l = get_irn_node(get_Sub_left(sub));
1737 node_t *r = get_irn_node(get_Sub_right(sub));
1738 lattice_elem_t a = l->type;
1739 lattice_elem_t b = r->type;
1742 if (a.tv == tarval_top || b.tv == tarval_top) {
1743 node->type.tv = tarval_top;
1744 } else if (is_con(a) && is_con(b)) {
1745 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1746 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1747 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1749 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1752 node->type.tv = tarval_bottom;
1754 node->by_all_const = 1;
1755 } else if (r->part == l->part &&
1756 (!mode_is_float(get_irn_mode(l->node)))) {
1758 * BEWARE: a - a is NOT always 0 for floating Point values, as
1759 * NaN op NaN = NaN, so we must check this here.
1761 ir_mode *mode = get_irn_mode(sub);
1762 tv = get_mode_null(mode);
1764 /* if the node was ONCE evaluated by all constants, but now
1765 this breakes AND we cat by partition a different result, switch to bottom.
1766 This happens because initially all nodes are in the same partition ... */
1767 if (node->by_all_const && node->type.tv != tv)
1771 node->type.tv = tarval_bottom;
1776 * (Re-)compute the type for Cmp.
1778 * @param node the node
1780 static void compute_Cmp(node_t *node) {
1781 ir_node *cmp = node->node;
1782 node_t *l = get_irn_node(get_Cmp_left(cmp));
1783 node_t *r = get_irn_node(get_Cmp_right(cmp));
1784 lattice_elem_t a = l->type;
1785 lattice_elem_t b = r->type;
1787 if (a.tv == tarval_top || b.tv == tarval_top) {
1788 node->type.tv = tarval_top;
1789 } else if (is_con(a) && is_con(b)) {
1790 /* both nodes are constants, we can probably do something */
1791 node->type.tv = tarval_b_true;
1792 } else if (r->part == l->part) {
1793 /* both nodes congruent, we can probably do something */
1794 node->type.tv = tarval_b_true;
1796 node->type.tv = tarval_bottom;
1798 } /* compute_Proj_Cmp */
1801 * (Re-)compute the type for a Proj(Cmp).
1803 * @param node the node
1804 * @param cond the predecessor Cmp node
1806 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1807 ir_node *proj = node->node;
1808 node_t *l = get_irn_node(get_Cmp_left(cmp));
1809 node_t *r = get_irn_node(get_Cmp_right(cmp));
1810 lattice_elem_t a = l->type;
1811 lattice_elem_t b = r->type;
1812 pn_Cmp pnc = get_Proj_proj(proj);
1815 if (a.tv == tarval_top || b.tv == tarval_top) {
1816 node->type.tv = tarval_top;
1817 } else if (is_con(a) && is_con(b)) {
1818 default_compute(node);
1819 node->by_all_const = 1;
1820 } else if (r->part == l->part &&
1821 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1823 * BEWARE: a == a is NOT always True for floating Point values, as
1824 * NaN != NaN is defined, so we must check this here.
1826 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1828 /* if the node was ONCE evaluated by all constants, but now
1829 this breakes AND we cat by partition a different result, switch to bottom.
1830 This happens because initially all nodes are in the same partition ... */
1831 if (node->by_all_const && node->type.tv != tv)
1835 node->type.tv = tarval_bottom;
1837 } /* compute_Proj_Cmp */
1840 * (Re-)compute the type for a Proj(Cond).
1842 * @param node the node
1843 * @param cond the predecessor Cond node
1845 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1846 ir_node *proj = node->node;
1847 long pnc = get_Proj_proj(proj);
1848 ir_node *sel = get_Cond_selector(cond);
1849 node_t *selector = get_irn_node(sel);
1851 if (get_irn_mode(sel) == mode_b) {
1853 if (pnc == pn_Cond_true) {
1854 if (selector->type.tv == tarval_b_false) {
1855 node->type.tv = tarval_unreachable;
1856 } else if (selector->type.tv == tarval_b_true) {
1857 node->type.tv = tarval_reachable;
1858 } else if (selector->type.tv == tarval_bottom) {
1859 node->type.tv = tarval_reachable;
1861 assert(selector->type.tv == tarval_top);
1862 node->type.tv = tarval_unreachable;
1865 assert(pnc == pn_Cond_false);
1867 if (selector->type.tv == tarval_b_false) {
1868 node->type.tv = tarval_reachable;
1869 } else if (selector->type.tv == tarval_b_true) {
1870 node->type.tv = tarval_unreachable;
1871 } else if (selector->type.tv == tarval_bottom) {
1872 node->type.tv = tarval_reachable;
1874 assert(selector->type.tv == tarval_top);
1875 node->type.tv = tarval_unreachable;
1880 if (selector->type.tv == tarval_bottom) {
1881 node->type.tv = tarval_reachable;
1882 } else if (selector->type.tv == tarval_top) {
1883 node->type.tv = tarval_unreachable;
1885 long value = get_tarval_long(selector->type.tv);
1886 if (pnc == get_Cond_defaultProj(cond)) {
1887 /* default switch, have to check ALL other cases */
1890 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1891 ir_node *succ = get_irn_out(cond, i);
1895 if (value == get_Proj_proj(succ)) {
1896 /* we found a match, will NOT take the default case */
1897 node->type.tv = tarval_unreachable;
1901 /* all cases checked, no match, will take default case */
1902 node->type.tv = tarval_reachable;
1905 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1909 } /* compute_Proj_Cond */
1912 * (Re-)compute the type for a Proj-Node.
1914 * @param node the node
1916 static void compute_Proj(node_t *node) {
1917 ir_node *proj = node->node;
1918 ir_mode *mode = get_irn_mode(proj);
1919 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1920 ir_node *pred = get_Proj_pred(proj);
1922 if (block->type.tv == tarval_unreachable) {
1923 /* a Proj in a unreachable Block stay Top */
1924 node->type.tv = tarval_top;
1927 if (get_irn_node(pred)->type.tv == tarval_top) {
1928 /* if the predecessor is Top, its Proj follow */
1929 node->type.tv = tarval_top;
1933 if (mode == mode_M) {
1934 /* mode M is always bottom */
1935 node->type.tv = tarval_bottom;
1938 if (mode != mode_X) {
1940 compute_Proj_Cmp(node, pred);
1942 default_compute(node);
1945 /* handle mode_X nodes */
1947 switch (get_irn_opcode(pred)) {
1949 /* the Proj_X from the Start is always reachable.
1950 However this is already handled at the top. */
1951 node->type.tv = tarval_reachable;
1954 compute_Proj_Cond(node, pred);
1957 default_compute(node);
1959 } /* compute_Proj */
1962 * (Re-)compute the type for a Confirm.
1964 * @param node the node
1966 static void compute_Confirm(node_t *node) {
1967 ir_node *confirm = node->node;
1968 node_t *pred = get_irn_node(get_Confirm_value(confirm));
1970 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1971 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1973 if (is_con(bound->type)) {
1974 /* is equal to a constant */
1975 node->type = bound->type;
1979 /* a Confirm is a copy OR a Const */
1980 node->type = pred->type;
1981 } /* compute_Confirm */
1984 * (Re-)compute the type for a Max.
1986 * @param node the node
1988 static void compute_Max(node_t *node) {
1989 ir_node *op = node->node;
1990 node_t *l = get_irn_node(get_binop_left(op));
1991 node_t *r = get_irn_node(get_binop_right(op));
1992 lattice_elem_t a = l->type;
1993 lattice_elem_t b = r->type;
1995 if (a.tv == tarval_top || b.tv == tarval_top) {
1996 node->type.tv = tarval_top;
1997 } else if (is_con(a) && is_con(b)) {
1998 /* both nodes are constants, we can probably do something */
2000 /* this case handles symconsts as well */
2003 ir_mode *mode = get_irn_mode(op);
2004 tarval *tv_min = get_mode_min(mode);
2008 else if (b.tv == tv_min)
2010 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2011 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2012 node->type.tv = a.tv;
2014 node->type.tv = b.tv;
2016 node->type.tv = tarval_bad;
2019 } else if (r->part == l->part) {
2020 /* both nodes congruent, we can probably do something */
2023 node->type.tv = tarval_bottom;
2028 * (Re-)compute the type for a Min.
2030 * @param node the node
2032 static void compute_Min(node_t *node) {
2033 ir_node *op = node->node;
2034 node_t *l = get_irn_node(get_binop_left(op));
2035 node_t *r = get_irn_node(get_binop_right(op));
2036 lattice_elem_t a = l->type;
2037 lattice_elem_t b = r->type;
2039 if (a.tv == tarval_top || b.tv == tarval_top) {
2040 node->type.tv = tarval_top;
2041 } else if (is_con(a) && is_con(b)) {
2042 /* both nodes are constants, we can probably do something */
2044 /* this case handles symconsts as well */
2047 ir_mode *mode = get_irn_mode(op);
2048 tarval *tv_max = get_mode_max(mode);
2052 else if (b.tv == tv_max)
2054 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2055 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2056 node->type.tv = a.tv;
2058 node->type.tv = b.tv;
2060 node->type.tv = tarval_bad;
2063 } else if (r->part == l->part) {
2064 /* both nodes congruent, we can probably do something */
2067 node->type.tv = tarval_bottom;
2072 * (Re-)compute the type for a given node.
2074 * @param node the node
2076 static void compute(node_t *node) {
2079 if (is_no_Block(node->node)) {
2080 node_t *block = get_irn_node(get_nodes_block(node->node));
2082 if (block->type.tv == tarval_unreachable) {
2083 node->type.tv = tarval_top;
2088 func = (compute_func)node->node->op->ops.generic;
2094 * Identity functions: Note that one might thing that identity() is just a
2095 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2096 * here, because it expects that the identity node is one of the inputs, which is NOT
2097 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2098 * So, we have our own implementation, which copies some parts of equivalent_node()
2102 * Calculates the Identity for Phi nodes
2104 static node_t *identity_Phi(node_t *node) {
2105 ir_node *phi = node->node;
2106 ir_node *block = get_nodes_block(phi);
2107 node_t *n_part = NULL;
2110 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2111 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2113 if (pred_X->type.tv == tarval_reachable) {
2114 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2118 else if (n_part->part != pred->part) {
2119 /* incongruent inputs, not a follower */
2124 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2125 * tarval_top, is in the TOP partition and should NOT being split! */
2126 assert(n_part != NULL);
2128 } /* identity_Phi */
2131 * Calculates the Identity for commutative 0 neutral nodes.
2133 static node_t *identity_comm_zero_binop(node_t *node) {
2134 ir_node *op = node->node;
2135 node_t *a = get_irn_node(get_binop_left(op));
2136 node_t *b = get_irn_node(get_binop_right(op));
2137 ir_mode *mode = get_irn_mode(op);
2140 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2141 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2144 /* node: no input should be tarval_top, else the binop would be also
2145 * Top and not being split. */
2146 zero = get_mode_null(mode);
2147 if (a->type.tv == zero)
2149 if (b->type.tv == zero)
2152 } /* identity_comm_zero_binop */
2154 #define identity_Add identity_comm_zero_binop
2155 #define identity_Or identity_comm_zero_binop
2158 * Calculates the Identity for Mul nodes.
2160 static node_t *identity_Mul(node_t *node) {
2161 ir_node *op = node->node;
2162 node_t *a = get_irn_node(get_Mul_left(op));
2163 node_t *b = get_irn_node(get_Mul_right(op));
2164 ir_mode *mode = get_irn_mode(op);
2167 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2168 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2171 /* node: no input should be tarval_top, else the binop would be also
2172 * Top and not being split. */
2173 one = get_mode_one(mode);
2174 if (a->type.tv == one)
2176 if (b->type.tv == one)
2179 } /* identity_Mul */
2182 * Calculates the Identity for Sub nodes.
2184 static node_t *identity_Sub(node_t *node) {
2185 ir_node *sub = node->node;
2186 node_t *b = get_irn_node(get_Sub_right(sub));
2187 ir_mode *mode = get_irn_mode(sub);
2189 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2190 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2193 /* node: no input should be tarval_top, else the binop would be also
2194 * Top and not being split. */
2195 if (b->type.tv == get_mode_null(mode))
2196 return get_irn_node(get_Sub_left(sub));
2198 } /* identity_Mul */
2201 * Calculates the Identity for And nodes.
2203 static node_t *identity_And(node_t *node) {
2204 ir_node *and = node->node;
2205 node_t *a = get_irn_node(get_And_left(and));
2206 node_t *b = get_irn_node(get_And_right(and));
2207 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2209 /* node: no input should be tarval_top, else the And would be also
2210 * Top and not being split. */
2211 if (a->type.tv == neutral)
2213 if (b->type.tv == neutral)
2216 } /* identity_And */
2219 * Calculates the Identity for Confirm nodes.
2221 static node_t *identity_Confirm(node_t *node) {
2222 ir_node *confirm = node->node;
2224 /* a Confirm is always a Copy */
2225 return get_irn_node(get_Confirm_value(confirm));
2226 } /* identity_Confirm */
2229 * Calculates the Identity for Mux nodes.
2231 static node_t *identity_Mux(node_t *node) {
2232 ir_node *mux = node->node;
2233 node_t *t = get_irn_node(get_Mux_true(mux));
2234 node_t *f = get_irn_node(get_Mux_false(mux));
2237 if (t->part == f->part)
2240 /* for now, the 1-input identity is not supported */
2242 sel = get_irn_node(get_Mux_sel(mux));
2244 /* Mux sel input is mode_b, so it is always a tarval */
2245 if (sel->type.tv == tarval_b_true)
2247 if (sel->type.tv == tarval_b_false)
2251 } /* identity_Mux */
2254 * Calculates the Identity for Min nodes.
2256 static node_t *identity_Min(node_t *node) {
2257 ir_node *op = node->node;
2258 node_t *a = get_irn_node(get_binop_left(op));
2259 node_t *b = get_irn_node(get_binop_right(op));
2260 ir_mode *mode = get_irn_mode(op);
2263 if (a->part == b->part) {
2264 /* leader of multiple predecessors */
2268 /* works even with NaN */
2269 tv_max = get_mode_max(mode);
2270 if (a->type.tv == tv_max)
2272 if (b->type.tv == tv_max)
2275 } /* identity_Min */
2278 * Calculates the Identity for Max nodes.
2280 static node_t *identity_Max(node_t *node) {
2281 ir_node *op = node->node;
2282 node_t *a = get_irn_node(get_binop_left(op));
2283 node_t *b = get_irn_node(get_binop_right(op));
2284 ir_mode *mode = get_irn_mode(op);
2287 if (a->part == b->part) {
2288 /* leader of multiple predecessors */
2292 /* works even with NaN */
2293 tv_min = get_mode_min(mode);
2294 if (a->type.tv == tv_min)
2296 if (b->type.tv == tv_min)
2299 } /* identity_Max */
2302 * Calculates the Identity for nodes.
2304 static node_t *identity(node_t *node) {
2305 ir_node *irn = node->node;
2307 switch (get_irn_opcode(irn)) {
2309 return identity_Phi(node);
2311 return identity_Add(node);
2313 return identity_Mul(node);
2315 return identity_Or(node);
2317 return identity_And(node);
2319 return identity_Sub(node);
2321 return identity_Confirm(node);
2323 return identity_Mux(node);
2325 return identity_Min(node);
2327 return identity_Max(node);
2334 * Node follower is a (new) follower of leader, segregate Leader
2337 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2338 ir_node *l = leader->node;
2339 int j, i, n = get_irn_n_outs(l);
2341 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2342 /* The leader edges must remain sorted, but follower edges can
2344 for (i = leader->n_followers + 1; i <= n; ++i) {
2345 if (l->out[i].use == follower) {
2346 ir_def_use_edge t = l->out[i];
2348 for (j = i - 1; j >= leader->n_followers + 1; --j)
2349 l->out[j + 1] = l->out[j];
2350 ++leader->n_followers;
2351 l->out[leader->n_followers] = t;
2355 } /* segregate_def_use_chain_1 */
2358 * Node follower is a (new) follower of leader, segregate Leader
2359 * out edges. If follower is a n-congruent Input identity, all follower
2360 * inputs congruent to follower are also leader.
2362 * @param follower the follower IR node
2364 static void segregate_def_use_chain(const ir_node *follower) {
2367 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2368 node_t *pred = get_irn_node(get_irn_n(follower, i));
2370 segregate_def_use_chain_1(follower, pred);
2372 } /* segregate_def_use_chain */
2375 * Propagate constant evaluation.
2377 * @param env the environment
2379 static void propagate(environment_t *env) {
2382 lattice_elem_t old_type;
2384 unsigned n_fallen, old_type_was_T_or_C;
2387 while (env->cprop != NULL) {
2388 void *oldopcode = NULL;
2390 /* remove the first partition X from cprop */
2393 env->cprop = X->cprop_next;
2395 old_type_was_T_or_C = X->type_is_T_or_C;
2397 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2400 while (! list_empty(&X->cprop)) {
2401 /* remove the first Node x from X.cprop */
2402 x = list_entry(X->cprop.next, node_t, cprop_list);
2403 //assert(x->part == X);
2404 list_del(&x->cprop_list);
2407 if (x->is_follower && identity(x) == x) {
2408 /* check the opcode first */
2409 if (oldopcode == NULL) {
2410 oldopcode = lambda_opcode(get_first_node(X), env);
2412 if (oldopcode != lambda_opcode(x, env)) {
2413 if (x->on_fallen == 0) {
2414 /* different opcode -> x falls out of this partition */
2419 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2423 /* x will make the follower -> leader transition */
2424 follower_to_leader(x);
2427 /* compute a new type for x */
2429 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2431 if (x->type.tv != old_type.tv) {
2432 verify_type(old_type, x->type);
2433 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2435 if (x->on_fallen == 0) {
2436 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2437 not already on the list. */
2442 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2444 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2445 ir_node *succ = get_irn_out(x->node, i);
2446 node_t *y = get_irn_node(succ);
2448 /* Add y to y.partition.cprop. */
2449 add_to_cprop(y, env);
2454 if (n_fallen > 0 && n_fallen != X->n_leader) {
2455 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2456 Y = split(&X, fallen, env);
2458 * We have split out fallen node. The type of the result
2459 * partition is NOT set yet.
2461 Y->type_is_T_or_C = 0;
2465 /* remove the flags from the fallen list */
2466 for (x = fallen; x != NULL; x = x->next)
2470 if (old_type_was_T_or_C) {
2473 if (Y->on_worklist == 0)
2474 add_to_worklist(Y, env);
2476 /* check if some nodes will make the leader -> follower transition */
2477 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2478 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2479 node_t *eq_node = identity(y);
2481 if (eq_node != y && eq_node->part == y->part) {
2482 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2483 /* move to Follower */
2485 list_del(&y->node_list);
2486 list_add_tail(&y->node_list, &Y->Follower);
2489 segregate_def_use_chain(y->node);
2500 * Get the leader for a given node from its congruence class.
2502 * @param irn the node
2504 static ir_node *get_leader(node_t *node) {
2505 partition_t *part = node->part;
2507 if (part->n_leader > 1 || node->is_follower) {
2508 if (node->is_follower) {
2509 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2512 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2514 return get_first_node(part)->node;
2520 * Return non-zero if the control flow predecessor node pred
2521 * is the only reachable control flow exit of its block.
2523 * @param pred the control flow exit
2525 static int can_exchange(ir_node *pred) {
2528 else if (is_Jmp(pred))
2530 else if (get_irn_mode(pred) == mode_T) {
2533 /* if the predecessor block has more than one
2534 reachable outputs we cannot remove the block */
2536 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2537 ir_node *proj = get_irn_out(pred, i);
2540 /* skip non-control flow Proj's */
2541 if (get_irn_mode(proj) != mode_X)
2544 node = get_irn_node(proj);
2545 if (node->type.tv == tarval_reachable) {
2556 * Block Post-Walker, apply the analysis results on control flow by
2557 * shortening Phi's and Block inputs.
2559 static void apply_cf(ir_node *block, void *ctx) {
2560 environment_t *env = ctx;
2561 node_t *node = get_irn_node(block);
2563 ir_node **ins, **in_X;
2564 ir_node *phi, *next;
2566 if (block == get_irg_end_block(current_ir_graph) ||
2567 block == get_irg_start_block(current_ir_graph)) {
2568 /* the EndBlock is always reachable even if the analysis
2569 finds out the opposite :-) */
2572 if (node->type.tv == tarval_unreachable) {
2573 /* mark dead blocks */
2574 set_Block_dead(block);
2578 n = get_Block_n_cfgpreds(block);
2581 /* only one predecessor combine */
2582 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2584 if (can_exchange(pred)) {
2585 exchange(block, get_nodes_block(pred));
2591 NEW_ARR_A(ir_node *, in_X, n);
2593 for (i = 0; i < n; ++i) {
2594 ir_node *pred = get_Block_cfgpred(block, i);
2595 node_t *node = get_irn_node(pred);
2597 if (node->type.tv == tarval_reachable) {
2604 NEW_ARR_A(ir_node *, ins, n);
2605 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2606 node_t *node = get_irn_node(phi);
2608 next = get_Phi_next(phi);
2609 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2610 /* this Phi is replaced by a constant */
2611 tarval *tv = node->type.tv;
2612 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2614 set_irn_node(c, node);
2616 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2621 for (i = 0; i < n; ++i) {
2622 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2624 if (pred->type.tv == tarval_reachable) {
2625 ins[j++] = get_Phi_pred(phi, i);
2629 /* this Phi is replaced by a single predecessor */
2630 ir_node *s = ins[0];
2633 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2637 set_irn_in(phi, j, ins);
2644 /* this Block has only one live predecessor */
2645 ir_node *pred = skip_Proj(in_X[0]);
2647 if (can_exchange(pred)) {
2648 exchange(block, get_nodes_block(pred));
2652 set_irn_in(block, k, in_X);
2658 * Post-Walker, apply the analysis results;
2660 static void apply_result(ir_node *irn, void *ctx) {
2661 environment_t *env = ctx;
2662 node_t *node = get_irn_node(irn);
2664 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2665 /* blocks already handled, do not touch the End node */
2667 node_t *block = get_irn_node(get_nodes_block(irn));
2669 if (block->type.tv == tarval_unreachable) {
2670 ir_node *bad = get_irg_bad(current_ir_graph);
2672 /* here, bad might already have a node, but this can be safely ignored
2673 as long as bad has at least ONE valid node */
2674 set_irn_node(bad, node);
2676 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2680 else if (node->type.tv == tarval_unreachable) {
2681 ir_node *bad = get_irg_bad(current_ir_graph);
2683 /* see comment above */
2684 set_irn_node(bad, node);
2686 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2690 else if (get_irn_mode(irn) == mode_X) {
2693 ir_node *cond = get_Proj_pred(irn);
2695 if (is_Cond(cond)) {
2696 node_t *sel = get_irn_node(get_Cond_selector(cond));
2698 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2699 /* Cond selector is a constant, make a Jmp */
2700 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2701 set_irn_node(jmp, node);
2703 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2710 /* normal data node */
2711 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2712 tarval *tv = node->type.tv;
2715 * Beware: never replace mode_T nodes by constants. Currently we must mark
2716 * mode_T nodes with constants, but do NOT replace them.
2718 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2719 /* can be replaced by a constant */
2720 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2721 set_irn_node(c, node);
2723 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2727 } else if (is_entity(node->type.sym.entity_p)) {
2728 if (! is_SymConst(irn)) {
2729 /* can be replaced by a Symconst */
2730 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2731 set_irn_node(symc, node);
2734 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2735 exchange(irn, symc);
2738 } else if (is_Confirm(irn)) {
2739 /* Confirms are always follower, but do not kill them here */
2741 ir_node *leader = get_leader(node);
2743 if (leader != irn) {
2744 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2745 exchange(irn, leader);
2751 } /* apply_result */
2754 * Fix the keep-alives by deleting unreachable ones.
2756 static void apply_end(ir_node *end, environment_t *env) {
2757 int i, j, n = get_End_n_keepalives(end);
2761 NEW_ARR_A(ir_node *, in, n);
2763 /* fix the keep alive */
2764 for (i = j = 0; i < n; i++) {
2765 ir_node *ka = get_End_keepalive(end, i);
2766 node_t *node = get_irn_node(ka);
2768 /* Use the flagged bits to mark already visited nodes.
2769 * This should not be ready but better safe than sorry. */
2770 if (node->flagged == 0) {
2774 node = get_irn_node(get_nodes_block(ka));
2776 if (node->type.tv != tarval_unreachable)
2781 set_End_keepalives(end, j, in);
2786 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2789 * sets the generic functions to compute.
2791 static void set_compute_functions(void) {
2794 /* set the default compute function */
2795 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2796 ir_op *op = get_irp_opcode(i);
2797 op->ops.generic = (op_func)default_compute;
2800 /* set specific functions */
2819 } /* set_compute_functions */
2821 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2822 ir_node *irn = local != NULL ? local : n;
2823 node_t *node = get_irn_node(irn);
2825 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2829 void combo(ir_graph *irg) {
2831 ir_node *initial_bl;
2833 ir_graph *rem = current_ir_graph;
2835 current_ir_graph = irg;
2837 /* register a debug mask */
2838 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2839 //firm_dbg_set_mask(dbg, SET_LEVEL_3);
2841 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2843 obstack_init(&env.obst);
2844 env.worklist = NULL;
2848 #ifdef DEBUG_libfirm
2849 env.dbg_list = NULL;
2851 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
2852 env.type2id_map = pmap_create();
2853 env.end_idx = get_opt_global_cse() ? 0 : -1;
2854 env.lambda_input = 0;
2857 assure_irg_outs(irg);
2859 /* we have our own value_of function */
2860 set_value_of_func(get_node_tarval);
2862 set_compute_functions();
2863 DEBUG_ONLY(part_nr = 0);
2865 /* create the initial partition and place it on the work list */
2866 env.initial = new_partition(&env);
2867 add_to_worklist(env.initial, &env);
2868 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2870 /* all nodes on the initial partition have type Top */
2871 env.initial->type_is_T_or_C = 1;
2873 /* Place the START Node's partition on cprop.
2874 Place the START Node on its local worklist. */
2875 initial_bl = get_irg_start_block(irg);
2876 start = get_irn_node(initial_bl);
2877 add_to_cprop(start, &env);
2881 if (env.worklist != NULL)
2883 } while (env.cprop != NULL || env.worklist != NULL);
2885 dump_all_partitions(&env);
2886 check_all_partitions(&env);
2889 set_dump_node_vcgattr_hook(dump_partition_hook);
2890 dump_ir_block_graph(irg, "-partition");
2891 set_dump_node_vcgattr_hook(NULL);
2893 (void)dump_partition_hook;
2896 /* apply the result */
2897 irg_block_walk_graph(irg, NULL, apply_cf, &env);
2898 irg_walk_graph(irg, NULL, apply_result, &env);
2899 apply_end(get_irg_end(irg), &env);
2902 /* control flow might changed */
2903 set_irg_outs_inconsistent(irg);
2904 set_irg_extblk_inconsistent(irg);
2905 set_irg_doms_inconsistent(irg);
2906 set_irg_loopinfo_inconsistent(irg);
2909 pmap_destroy(env.type2id_map);
2910 del_set(env.opcode2id_map);
2911 obstack_free(&env.obst, NULL);
2913 /* restore value_of() default behavior */
2914 set_value_of_func(NULL);
2915 current_ir_graph = rem;