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 * This is a slightly enhanced version of Cliff Clicks combo algorithm
27 * - support for commutative nodes is added, Add(a,b) and Add(b,a) ARE congruent
28 * - supports all Firm direct (by a data edge) identities except Mux
29 * (Mux can be a 2-input or 1-input identity, only 2-input is implemented yet)
30 * - supports Confirm nodes (handle them like Copies but do NOT remove them)
31 * - support for global congruences is implemented but not tested yet
33 * Note further that we use the terminology from Click's work here, which is different
34 * in some cases from Firm terminology. Especially, Click's type is a
35 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
41 #include "iroptimize.h"
49 #include "irgraph_t.h"
56 #include "iropt_dbg.h"
66 /* define this to check that all type translations are monotone */
67 #undef VERIFY_MONOTONE
69 /* define this to check the consistency of partitions */
70 #define CHECK_PARTITIONS
72 /* allow optimization of non-strict programs */
75 typedef struct node_t node_t;
76 typedef struct partition_t partition_t;
77 typedef struct opcode_key_t opcode_key_t;
78 typedef struct listmap_entry_t listmap_entry_t;
80 /** The type of the compute function. */
81 typedef void (*compute_func)(node_t *node);
87 ir_opcode code; /**< The Firm opcode. */
88 ir_mode *mode; /**< The mode of all nodes in the partition. */
89 int arity; /**< The arity of this opcode (needed for Phi etc. */
91 long proj; /**< For Proj nodes, its proj number */
92 ir_entity *ent; /**< For Sel Nodes, its entity */
97 * An entry in the list_map.
99 struct listmap_entry_t {
100 void *id; /**< The id. */
101 node_t *list; /**< The associated list for this id. */
102 listmap_entry_t *next; /**< Link to the next entry in the map. */
105 /** We must map id's to lists. */
106 typedef struct listmap_t {
107 set *map; /**< Map id's to listmap_entry_t's */
108 listmap_entry_t *values; /**< List of all values in the map. */
112 * A lattice element. Because we handle constants and symbolic constants different, we
113 * have to use this union.
124 ir_node *node; /**< The IR-node itself. */
125 list_head node_list; /**< Double-linked list of leader/follower entries. */
126 list_head cprop_list; /**< Double-linked partition.cprop list. */
127 partition_t *part; /**< points to the partition this node belongs to */
128 node_t *next; /**< Next node on local list (partition.touched, fallen). */
129 node_t *race_next; /**< Next node on race list. */
130 lattice_elem_t type; /**< The associated lattice element "type". */
131 int max_user_input; /**< Maximum input number of Def-Use edges. */
132 int next_edge; /**< Index of the next Def-Use edge to use. */
133 int n_followers; /**< Number of Follower in the outs set. */
134 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
135 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
136 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
137 unsigned is_follower:1; /**< Set, if this node is a follower. */
138 unsigned by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
139 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
143 * A partition containing congruent nodes.
146 list_head Leader; /**< The head of partition Leader node list. */
147 list_head Follower; /**< The head of partition Follower node list. */
148 list_head cprop; /**< The head of partition.cprop list. */
149 partition_t *wl_next; /**< Next entry in the work list if any. */
150 partition_t *touched_next; /**< Points to the next partition in the touched set. */
151 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
152 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
153 node_t *touched; /**< The partition.touched set of this partition. */
154 unsigned n_leader; /**< Number of entries in this partition.Leader. */
155 unsigned n_touched; /**< Number of entries in the partition.touched. */
156 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
157 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
158 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
159 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
160 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
162 partition_t *dbg_next; /**< Link all partitions for debugging */
163 unsigned nr; /**< A unique number for (what-)mapping, >0. */
167 typedef struct environment_t {
168 struct obstack obst; /**< obstack to allocate data structures. */
169 partition_t *worklist; /**< The work list. */
170 partition_t *cprop; /**< The constant propagation list. */
171 partition_t *touched; /**< the touched set. */
172 partition_t *initial; /**< The initial partition. */
173 set *opcode2id_map; /**< The opcodeMode->id map. */
174 pmap *type2id_map; /**< The type->id map. */
175 int end_idx; /**< -1 for local and 0 for global congruences. */
176 int lambda_input; /**< Captured argument for lambda_partition(). */
177 char nonstd_cond; /**< Set, if a Condb note has a non-Cmp predecessor. */
178 char modified; /**< Set, if the graph was modified. */
179 char commutative; /**< Set, if commutation nodes should be handled specially. */
181 partition_t *dbg_list; /**< List of all partitions. */
185 /** Type of the what function. */
186 typedef void *(*what_func)(const node_t *node, environment_t *env);
188 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
189 #define set_irn_node(follower, node) set_irn_link(follower, node)
191 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
192 #undef tarval_unreachable
193 #define tarval_unreachable tarval_top
196 /** The debug module handle. */
197 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
199 /** The what reason. */
200 DEBUG_ONLY(static const char *what_reason;)
202 /** Next partition number. */
203 DEBUG_ONLY(static unsigned part_nr = 0);
205 /** The tarval returned by Unknown nodes. */
206 static tarval *tarval_UNKNOWN;
209 static node_t *identity(node_t *node);
211 #ifdef CHECK_PARTITIONS
215 static void check_partition(const partition_t *T) {
219 list_for_each_entry(node_t, node, &T->Leader, node_list) {
220 assert(node->is_follower == 0);
221 assert(node->flagged == 0);
222 assert(node->part == T);
225 assert(n == T->n_leader);
227 list_for_each_entry(node_t, node, &T->Follower, node_list) {
228 assert(node->is_follower == 1);
229 assert(node->flagged == 0);
230 assert(node->part == T);
232 } /* check_partition */
235 * check that all leader nodes in the partition have the same opcode.
237 static void check_opcode(const partition_t *Z) {
242 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
243 ir_node *irn = node->node;
246 key.code = get_irn_opcode(irn);
247 key.mode = get_irn_mode(irn);
248 key.arity = get_irn_arity(irn);
252 switch (get_irn_opcode(irn)) {
254 key.u.proj = get_Proj_proj(irn);
257 key.u.ent = get_Sel_entity(irn);
264 assert(key.code == get_irn_opcode(irn));
265 assert(key.mode == get_irn_mode(irn));
266 assert(key.arity == get_irn_arity(irn));
268 switch (get_irn_opcode(irn)) {
270 assert(key.u.proj == get_Proj_proj(irn));
273 assert(key.u.ent == get_Sel_entity(irn));
282 static void check_all_partitions(environment_t *env) {
287 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
289 if (! P->type_is_T_or_C)
291 list_for_each_entry(node_t, node, &P->Follower, node_list) {
292 node_t *leader = identity(node);
294 assert(leader != node && leader->part == node->part);
303 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
306 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
307 for (e = list; e != NULL; e = NEXT(e)) {
308 assert(e->part == Z);
311 } /* ido_check_list */
314 * Check a local list.
316 static void check_list(const node_t *list, const partition_t *Z) {
317 do_check_list(list, offsetof(node_t, next), Z);
321 #define check_partition(T)
322 #define check_list(list, Z)
323 #define check_all_partitions(env)
324 #endif /* CHECK_PARTITIONS */
327 static inline lattice_elem_t get_partition_type(const partition_t *X);
330 * Dump partition to output.
332 static void dump_partition(const char *msg, const partition_t *part) {
335 lattice_elem_t type = get_partition_type(part);
337 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
338 msg, part->nr, part->type_is_T_or_C ? "*" : "",
339 part->n_leader, type));
340 list_for_each_entry(node_t, node, &part->Leader, node_list) {
341 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
344 if (! list_empty(&part->Follower)) {
345 DB((dbg, LEVEL_2, "\n---\n "));
347 list_for_each_entry(node_t, node, &part->Follower, node_list) {
348 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
352 DB((dbg, LEVEL_2, "\n}\n"));
353 } /* dump_partition */
358 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
362 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
364 DB((dbg, LEVEL_3, "%s = {\n ", msg));
365 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
366 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
369 DB((dbg, LEVEL_3, "\n}\n"));
377 static void dump_race_list(const char *msg, const node_t *list) {
378 do_dump_list(msg, list, offsetof(node_t, race_next));
379 } /* dump_race_list */
382 * Dumps a local list.
384 static void dump_list(const char *msg, const node_t *list) {
385 do_dump_list(msg, list, offsetof(node_t, next));
389 * Dump all partitions.
391 static void dump_all_partitions(const environment_t *env) {
392 const partition_t *P;
394 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
395 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
396 dump_partition("", P);
397 } /* dump_all_partitions */
402 static void dump_split_list(const partition_t *list) {
403 const partition_t *p;
405 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
406 for (p = list; p != NULL; p = p->split_next)
407 DB((dbg, LEVEL_2, "part%u, ", p->nr));
408 DB((dbg, LEVEL_2, "\n}\n"));
409 } /* dump_split_list */
412 #define dump_partition(msg, part)
413 #define dump_race_list(msg, list)
414 #define dump_list(msg, list)
415 #define dump_all_partitions(env)
416 #define dump_split_list(list)
419 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
421 * Verify that a type transition is monotone
423 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
424 if (old_type.tv == new_type.tv) {
428 if (old_type.tv == tarval_top) {
429 /* from Top down-to is always allowed */
432 if (old_type.tv == tarval_reachable) {
433 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
435 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
439 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
442 #define verify_type(old_type, new_type)
446 * Compare two pointer values of a listmap.
448 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
449 const listmap_entry_t *e1 = elt;
450 const listmap_entry_t *e2 = key;
453 return e1->id != e2->id;
454 } /* listmap_cmp_ptr */
457 * Initializes a listmap.
459 * @param map the listmap
461 static void listmap_init(listmap_t *map) {
462 map->map = new_set(listmap_cmp_ptr, 16);
467 * Terminates a listmap.
469 * @param map the listmap
471 static void listmap_term(listmap_t *map) {
476 * Return the associated listmap entry for a given id.
478 * @param map the listmap
479 * @param id the id to search for
481 * @return the associated listmap entry for the given id
483 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
484 listmap_entry_t key, *entry;
489 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
491 if (entry->list == NULL) {
492 /* a new entry, put into the list */
493 entry->next = map->values;
500 * Calculate the hash value for an opcode map entry.
502 * @param entry an opcode map entry
504 * @return a hash value for the given opcode map entry
506 static unsigned opcode_hash(const opcode_key_t *entry) {
507 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent) + entry->arity;
511 * Compare two entries in the opcode map.
513 static int cmp_opcode(const void *elt, const void *key, size_t size) {
514 const opcode_key_t *o1 = elt;
515 const opcode_key_t *o2 = key;
518 return o1->code != o2->code || o1->mode != o2->mode ||
519 o1->arity != o2->arity ||
520 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
524 * Compare two Def-Use edges for input position.
526 static int cmp_def_use_edge(const void *a, const void *b) {
527 const ir_def_use_edge *ea = a;
528 const ir_def_use_edge *eb = b;
530 /* no overrun, because range is [-1, MAXINT] */
531 return ea->pos - eb->pos;
532 } /* cmp_def_use_edge */
535 * We need the Def-Use edges sorted.
537 static void sort_irn_outs(node_t *node) {
538 ir_node *irn = node->node;
539 int n_outs = get_irn_n_outs(irn);
542 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
544 node->max_user_input = irn->out[n_outs].pos;
545 } /* sort_irn_outs */
548 * Return the type of a node.
550 * @param irn an IR-node
552 * @return the associated type of this node
554 static inline lattice_elem_t get_node_type(const ir_node *irn) {
555 return get_irn_node(irn)->type;
556 } /* get_node_type */
559 * Return the tarval of a node.
561 * @param irn an IR-node
563 * @return the associated type of this node
565 static inline tarval *get_node_tarval(const ir_node *irn) {
566 lattice_elem_t type = get_node_type(irn);
568 if (is_tarval(type.tv))
570 return tarval_bottom;
571 } /* get_node_type */
574 * Add a partition to the worklist.
576 static inline void add_to_worklist(partition_t *X, environment_t *env) {
577 assert(X->on_worklist == 0);
578 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
579 X->wl_next = env->worklist;
582 } /* add_to_worklist */
585 * Create a new empty partition.
587 * @param env the environment
589 * @return a newly allocated partition
591 static inline partition_t *new_partition(environment_t *env) {
592 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
594 INIT_LIST_HEAD(&part->Leader);
595 INIT_LIST_HEAD(&part->Follower);
596 INIT_LIST_HEAD(&part->cprop);
597 part->wl_next = NULL;
598 part->touched_next = NULL;
599 part->cprop_next = NULL;
600 part->split_next = NULL;
601 part->touched = NULL;
604 part->max_user_inputs = 0;
605 part->on_worklist = 0;
606 part->on_touched = 0;
608 part->type_is_T_or_C = 0;
610 part->dbg_next = env->dbg_list;
611 env->dbg_list = part;
612 part->nr = part_nr++;
616 } /* new_partition */
619 * Get the first node from a partition.
621 static inline node_t *get_first_node(const partition_t *X) {
622 return list_entry(X->Leader.next, node_t, node_list);
623 } /* get_first_node */
626 * Return the type of a partition (assuming partition is non-empty and
627 * all elements have the same type).
629 * @param X a partition
631 * @return the type of the first element of the partition
633 static inline lattice_elem_t get_partition_type(const partition_t *X) {
634 const node_t *first = get_first_node(X);
636 } /* get_partition_type */
639 * Creates a partition node for the given IR-node and place it
640 * into the given partition.
642 * @param irn an IR-node
643 * @param part a partition to place the node in
644 * @param env the environment
646 * @return the created node
648 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
649 /* create a partition node and place it in the partition */
650 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
652 INIT_LIST_HEAD(&node->node_list);
653 INIT_LIST_HEAD(&node->cprop_list);
657 node->race_next = NULL;
658 node->type.tv = tarval_top;
659 node->max_user_input = 0;
661 node->n_followers = 0;
662 node->on_touched = 0;
665 node->is_follower = 0;
666 node->by_all_const = 0;
668 set_irn_node(irn, node);
670 list_add_tail(&node->node_list, &part->Leader);
674 } /* create_partition_node */
677 * Pre-Walker, init all Block-Phi lists.
679 static void init_block_phis(ir_node *irn, void *env) {
683 set_Block_phis(irn, NULL);
685 } /* init_block_phis */
688 * Post-Walker, initialize all Nodes' type to U or top and place
689 * all nodes into the TOP partition.
691 static void create_initial_partitions(ir_node *irn, void *ctx) {
692 environment_t *env = ctx;
693 partition_t *part = env->initial;
696 node = create_partition_node(irn, part, env);
698 if (node->max_user_input > part->max_user_inputs)
699 part->max_user_inputs = node->max_user_input;
702 add_Block_phi(get_nodes_block(irn), irn);
703 } else if (is_Cond(irn)) {
704 /* check if all Cond's have a Cmp predecessor. */
705 if (get_irn_mode(irn) == mode_b && !is_Cmp(skip_Proj(get_Cond_selector(irn))))
706 env->nonstd_cond = 1;
708 } /* create_initial_partitions */
711 * Add a node to the entry.partition.touched set and
712 * node->partition to the touched set if not already there.
715 * @param env the environment
717 static inline void add_to_touched(node_t *y, environment_t *env) {
718 if (y->on_touched == 0) {
719 partition_t *part = y->part;
721 y->next = part->touched;
726 if (part->on_touched == 0) {
727 part->touched_next = env->touched;
729 part->on_touched = 1;
732 check_list(part->touched, part);
734 } /* add_to_touched */
737 * Place a node on the cprop list.
740 * @param env the environment
742 static void add_to_cprop(node_t *y, environment_t *env) {
743 /* Add y to y.partition.cprop. */
744 if (y->on_cprop == 0) {
745 partition_t *Y = y->part;
747 list_add_tail(&y->cprop_list, &Y->cprop);
750 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
752 /* place its partition on the cprop list */
753 if (Y->on_cprop == 0) {
754 Y->cprop_next = env->cprop;
759 if (get_irn_mode(y->node) == mode_T) {
760 /* mode_T nodes always produce tarval_bottom, so we must explicitly
761 add it's Proj's to get constant evaluation to work */
764 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
765 node_t *proj = get_irn_node(get_irn_out(y->node, i));
767 add_to_cprop(proj, env);
769 } else if (is_Block(y->node)) {
770 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
771 * if someone placed the block. The Block is only placed if the reachability
772 * changes, and this must be re-evaluated in compute_Phi(). */
774 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
775 node_t *p = get_irn_node(phi);
776 add_to_cprop(p, env);
782 * Update the worklist: If Z is on worklist then add Z' to worklist.
783 * Else add the smaller of Z and Z' to worklist.
785 * @param Z the Z partition
786 * @param Z_prime the Z' partition, a previous part of Z
787 * @param env the environment
789 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
790 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
791 add_to_worklist(Z_prime, env);
793 add_to_worklist(Z, env);
795 } /* update_worklist */
798 * Make all inputs to x no longer be F.def_use edges.
802 static void move_edges_to_leader(node_t *x) {
803 ir_node *irn = x->node;
806 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
807 node_t *pred = get_irn_node(get_irn_n(irn, i));
812 n = get_irn_n_outs(p);
813 for (j = 1; j <= pred->n_followers; ++j) {
814 if (p->out[j].pos == i && p->out[j].use == irn) {
815 /* found a follower edge to x, move it to the Leader */
816 ir_def_use_edge edge = p->out[j];
818 /* remove this edge from the Follower set */
819 p->out[j] = p->out[pred->n_followers];
822 /* sort it into the leader set */
823 for (k = pred->n_followers + 2; k <= n; ++k) {
824 if (p->out[k].pos >= edge.pos)
826 p->out[k - 1] = p->out[k];
828 /* place the new edge here */
829 p->out[k - 1] = edge;
831 /* edge found and moved */
836 } /* move_edges_to_leader */
839 * Split a partition that has NO followers by a local list.
841 * @param Z partition to split
842 * @param g a (non-empty) node list
843 * @param env the environment
845 * @return a new partition containing the nodes of g
847 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
848 partition_t *Z_prime;
853 dump_partition("Splitting ", Z);
854 dump_list("by list ", g);
858 /* Remove g from Z. */
859 for (node = g; node != NULL; node = node->next) {
860 assert(node->part == Z);
861 list_del(&node->node_list);
864 assert(n < Z->n_leader);
867 /* Move g to a new partition, Z'. */
868 Z_prime = new_partition(env);
870 for (node = g; node != NULL; node = node->next) {
871 list_add_tail(&node->node_list, &Z_prime->Leader);
872 node->part = Z_prime;
873 if (node->max_user_input > max_input)
874 max_input = node->max_user_input;
876 Z_prime->max_user_inputs = max_input;
877 Z_prime->n_leader = n;
880 check_partition(Z_prime);
882 /* for now, copy the type info tag, it will be adjusted in split_by(). */
883 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
885 update_worklist(Z, Z_prime, env);
887 dump_partition("Now ", Z);
888 dump_partition("Created new ", Z_prime);
890 } /* split_no_followers */
893 * Make the Follower -> Leader transition for a node.
897 static void follower_to_leader(node_t *n) {
898 assert(n->is_follower == 1);
900 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
902 move_edges_to_leader(n);
903 list_del(&n->node_list);
904 list_add_tail(&n->node_list, &n->part->Leader);
906 } /* follower_to_leader */
909 * The environment for one race step.
911 typedef struct step_env {
912 node_t *initial; /**< The initial node list. */
913 node_t *unwalked; /**< The unwalked node list. */
914 node_t *walked; /**< The walked node list. */
915 int index; /**< Next index of Follower use_def edge. */
916 unsigned side; /**< side number. */
920 * Return non-zero, if a input is a real follower
922 * @param irn the node to check
923 * @param input number of the input
925 static int is_real_follower(const ir_node *irn, int input) {
928 switch (get_irn_opcode(irn)) {
931 /* ignore the Confirm bound input */
937 /* ignore the Mux sel input */
942 /* dead inputs are not follower edges */
943 ir_node *block = get_nodes_block(irn);
944 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
946 if (pred->type.tv == tarval_unreachable)
956 /* only a Sub x,0 / Shift x,0 might be a follower */
963 pred = get_irn_node(get_irn_n(irn, input));
964 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
968 pred = get_irn_node(get_irn_n(irn, input));
969 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
973 pred = get_irn_node(get_irn_n(irn, input));
974 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
979 /* all inputs are followers */
982 assert(!"opcode not implemented yet");
986 } /* is_real_follower */
989 * Do one step in the race.
991 static int step(step_env *env) {
994 if (env->initial != NULL) {
995 /* Move node from initial to unwalked */
997 env->initial = n->race_next;
999 n->race_next = env->unwalked;
1005 while (env->unwalked != NULL) {
1006 /* let n be the first node in unwalked */
1008 while (env->index < n->n_followers) {
1009 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1011 /* let m be n.F.def_use[index] */
1012 node_t *m = get_irn_node(edge->use);
1014 assert(m->is_follower);
1016 * Some inputs, like the get_Confirm_bound are NOT
1017 * real followers, sort them out.
1019 if (! is_real_follower(m->node, edge->pos)) {
1025 /* only followers from our partition */
1026 if (m->part != n->part)
1029 if ((m->flagged & env->side) == 0) {
1030 m->flagged |= env->side;
1032 if (m->flagged != 3) {
1033 /* visited the first time */
1034 /* add m to unwalked not as first node (we might still need to
1035 check for more follower node */
1036 m->race_next = n->race_next;
1040 /* else already visited by the other side and on the other list */
1043 /* move n to walked */
1044 env->unwalked = n->race_next;
1045 n->race_next = env->walked;
1053 * Clear the flags from a list and check for
1054 * nodes that where touched from both sides.
1056 * @param list the list
1058 static int clear_flags(node_t *list) {
1062 for (n = list; n != NULL; n = n->race_next) {
1063 if (n->flagged == 3) {
1064 /* we reach a follower from both sides, this will split congruent
1065 * inputs and make it a leader. */
1066 follower_to_leader(n);
1075 * Split a partition by a local list using the race.
1077 * @param pX pointer to the partition to split, might be changed!
1078 * @param gg a (non-empty) node list
1079 * @param env the environment
1081 * @return a new partition containing the nodes of gg
1083 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1084 partition_t *X = *pX;
1085 partition_t *X_prime;
1088 node_t *g, *h, *node, *t;
1089 int max_input, transitions, winner, shf;
1091 DEBUG_ONLY(static int run = 0;)
1093 DB((dbg, LEVEL_2, "Run %d ", run++));
1094 if (list_empty(&X->Follower)) {
1095 /* if the partition has NO follower, we can use the fast
1096 splitting algorithm. */
1097 return split_no_followers(X, gg, env);
1099 /* else do the race */
1101 dump_partition("Splitting ", X);
1102 dump_list("by list ", gg);
1104 INIT_LIST_HEAD(&tmp);
1106 /* Remove gg from X.Leader and put into g */
1108 for (node = gg; node != NULL; node = node->next) {
1109 assert(node->part == X);
1110 assert(node->is_follower == 0);
1112 list_del(&node->node_list);
1113 list_add_tail(&node->node_list, &tmp);
1114 node->race_next = g;
1119 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1120 node->race_next = h;
1123 /* restore X.Leader */
1124 list_splice(&tmp, &X->Leader);
1126 senv[0].initial = g;
1127 senv[0].unwalked = NULL;
1128 senv[0].walked = NULL;
1132 senv[1].initial = h;
1133 senv[1].unwalked = NULL;
1134 senv[1].walked = NULL;
1139 * Some informations on the race that are not stated clearly in Click's
1141 * 1) A follower stays on the side that reach him first.
1142 * 2) If the other side reches a follower, if will be converted to
1143 * a leader. /This must be done after the race is over, else the
1144 * edges we are iterating on are renumbered./
1145 * 3) /New leader might end up on both sides./
1146 * 4) /If one side ends up with new Leaders, we must ensure that
1147 * they can split out by opcode, hence we have to put _every_
1148 * partition with new Leader nodes on the cprop list, as
1149 * opcode splitting is done by split_by() at the end of
1150 * constant propagation./
1153 if (step(&senv[0])) {
1157 if (step(&senv[1])) {
1162 assert(senv[winner].initial == NULL);
1163 assert(senv[winner].unwalked == NULL);
1165 /* clear flags from walked/unwalked */
1167 transitions = clear_flags(senv[0].unwalked) << shf;
1168 transitions |= clear_flags(senv[0].walked) << shf;
1170 transitions |= clear_flags(senv[1].unwalked) << shf;
1171 transitions |= clear_flags(senv[1].walked) << shf;
1173 dump_race_list("winner ", senv[winner].walked);
1175 /* Move walked_{winner} to a new partition, X'. */
1176 X_prime = new_partition(env);
1179 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1180 list_del(&node->node_list);
1181 node->part = X_prime;
1182 if (node->is_follower) {
1183 list_add_tail(&node->node_list, &X_prime->Follower);
1185 list_add_tail(&node->node_list, &X_prime->Leader);
1188 if (node->max_user_input > max_input)
1189 max_input = node->max_user_input;
1191 X_prime->n_leader = n;
1192 X_prime->max_user_inputs = max_input;
1193 X->n_leader -= X_prime->n_leader;
1195 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1196 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1199 * Even if a follower was not checked by both sides, it might have
1200 * loose its congruence, so we need to check this case for all follower.
1202 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1203 if (identity(node) == node) {
1204 follower_to_leader(node);
1210 check_partition(X_prime);
1212 /* X' is the smaller part */
1213 add_to_worklist(X_prime, env);
1216 * If there where follower to leader transitions, ensure that the nodes
1217 * can be split out if necessary.
1219 if (transitions & 1) {
1220 /* place winner partition on the cprop list */
1221 if (X_prime->on_cprop == 0) {
1222 X_prime->cprop_next = env->cprop;
1223 env->cprop = X_prime;
1224 X_prime->on_cprop = 1;
1227 if (transitions & 2) {
1228 /* place other partition on the cprop list */
1229 if (X->on_cprop == 0) {
1230 X->cprop_next = env->cprop;
1236 dump_partition("Now ", X);
1237 dump_partition("Created new ", X_prime);
1239 /* we have to ensure that the partition containing g is returned */
1249 * Returns non-zero if the i'th input of a Phi node is live.
1251 * @param phi a Phi-node
1252 * @param i an input number
1254 * @return non-zero if the i'th input of the given Phi node is live
1256 static int is_live_input(ir_node *phi, int i) {
1258 ir_node *block = get_nodes_block(phi);
1259 ir_node *pred = get_Block_cfgpred(block, i);
1260 lattice_elem_t type = get_node_type(pred);
1262 return type.tv != tarval_unreachable;
1264 /* else it's the control input, always live */
1266 } /* is_live_input */
1269 * Return non-zero if a type is a constant.
1271 static int is_constant_type(lattice_elem_t type) {
1272 if (type.tv != tarval_bottom && type.tv != tarval_top)
1275 } /* is_constant_type */
1278 * Check whether a type is neither Top or a constant.
1279 * Note: U is handled like Top here, R is a constant.
1281 * @param type the type to check
1283 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1284 if (is_tarval(type.tv)) {
1285 if (type.tv == tarval_top)
1287 if (tarval_is_constant(type.tv))
1294 } /* type_is_neither_top_nor_const */
1297 * Collect nodes to the touched list.
1299 * @param list the list which contains the nodes that must be evaluated
1300 * @param idx the index of the def_use edge to evaluate
1301 * @param env the environment
1303 static void collect_touched(list_head *list, int idx, environment_t *env) {
1305 int end_idx = env->end_idx;
1307 list_for_each_entry(node_t, x, list, node_list) {
1311 /* leader edges start AFTER follower edges */
1312 x->next_edge = x->n_followers + 1;
1314 num_edges = get_irn_n_outs(x->node);
1316 /* for all edges in x.L.def_use_{idx} */
1317 while (x->next_edge <= num_edges) {
1318 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1321 /* check if we have necessary edges */
1322 if (edge->pos > idx)
1329 /* only non-commutative nodes */
1330 if (env->commutative &&
1331 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1334 /* ignore the "control input" for non-pinned nodes
1335 if we are running in GCSE mode */
1336 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1339 y = get_irn_node(succ);
1340 assert(get_irn_n(succ, idx) == x->node);
1342 /* ignore block edges touching followers */
1343 if (idx == -1 && y->is_follower)
1346 if (is_constant_type(y->type)) {
1347 ir_opcode code = get_irn_opcode(succ);
1348 if (code == iro_Sub || code == iro_Cmp)
1349 add_to_cprop(y, env);
1352 /* Partitions of constants should not be split simply because their Nodes have unequal
1353 functions or incongruent inputs. */
1354 if (type_is_neither_top_nor_const(y->type) &&
1355 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1356 add_to_touched(y, env);
1360 } /* collect_touched */
1363 * Collect commutative nodes to the touched list.
1365 * @param list the list which contains the nodes that must be evaluated
1366 * @param env the environment
1368 static void collect_commutative_touched(list_head *list, environment_t *env) {
1371 list_for_each_entry(node_t, x, list, node_list) {
1374 num_edges = get_irn_n_outs(x->node);
1376 x->next_edge = x->n_followers + 1;
1378 /* for all edges in x.L.def_use_{idx} */
1379 while (x->next_edge <= num_edges) {
1380 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1383 /* check if we have necessary edges */
1393 /* only commutative nodes */
1394 if (!is_op_commutative(get_irn_op(succ)))
1397 y = get_irn_node(succ);
1398 if (is_constant_type(y->type)) {
1399 ir_opcode code = get_irn_opcode(succ);
1400 if (code == iro_Eor)
1401 add_to_cprop(y, env);
1404 /* Partitions of constants should not be split simply because their Nodes have unequal
1405 functions or incongruent inputs. */
1406 if (type_is_neither_top_nor_const(y->type)) {
1407 add_to_touched(y, env);
1411 } /* collect_commutative_touched */
1414 * Split the partitions if caused by the first entry on the worklist.
1416 * @param env the environment
1418 static void cause_splits(environment_t *env) {
1419 partition_t *X, *Z, *N;
1422 /* remove the first partition from the worklist */
1424 env->worklist = X->wl_next;
1427 dump_partition("Cause_split: ", X);
1429 if (env->commutative) {
1430 /* handle commutative nodes first */
1432 /* empty the touched set: already done, just clear the list */
1433 env->touched = NULL;
1435 collect_commutative_touched(&X->Leader, env);
1436 collect_commutative_touched(&X->Follower, env);
1438 for (Z = env->touched; Z != NULL; Z = N) {
1440 node_t *touched = Z->touched;
1441 unsigned n_touched = Z->n_touched;
1443 assert(Z->touched != NULL);
1445 /* beware, split might change Z */
1446 N = Z->touched_next;
1448 /* remove it from the touched set */
1451 /* Empty local Z.touched. */
1452 for (e = touched; e != NULL; e = e->next) {
1453 assert(e->is_follower == 0);
1459 if (0 < n_touched && n_touched < Z->n_leader) {
1460 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1461 split(&Z, touched, env);
1463 assert(n_touched <= Z->n_leader);
1467 /* combine temporary leader and follower list */
1468 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1469 /* empty the touched set: already done, just clear the list */
1470 env->touched = NULL;
1472 collect_touched(&X->Leader, idx, env);
1473 collect_touched(&X->Follower, idx, env);
1475 for (Z = env->touched; Z != NULL; Z = N) {
1477 node_t *touched = Z->touched;
1478 unsigned n_touched = Z->n_touched;
1480 assert(Z->touched != NULL);
1482 /* beware, split might change Z */
1483 N = Z->touched_next;
1485 /* remove it from the touched set */
1488 /* Empty local Z.touched. */
1489 for (e = touched; e != NULL; e = e->next) {
1490 assert(e->is_follower == 0);
1496 if (0 < n_touched && n_touched < Z->n_leader) {
1497 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1498 split(&Z, touched, env);
1500 assert(n_touched <= Z->n_leader);
1503 } /* cause_splits */
1506 * Implements split_by_what(): Split a partition by characteristics given
1507 * by the what function.
1509 * @param X the partition to split
1510 * @param What a function returning an Id for every node of the partition X
1511 * @param P a list to store the result partitions
1512 * @param env the environment
1516 static partition_t *split_by_what(partition_t *X, what_func What,
1517 partition_t **P, environment_t *env) {
1520 listmap_entry_t *iter;
1523 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1525 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1526 void *id = What(x, env);
1527 listmap_entry_t *entry;
1530 /* input not allowed, ignore */
1533 /* Add x to map[What(x)]. */
1534 entry = listmap_find(&map, id);
1535 x->next = entry->list;
1538 /* Let P be a set of Partitions. */
1540 /* for all sets S except one in the range of map do */
1541 for (iter = map.values; iter != NULL; iter = iter->next) {
1542 if (iter->next == NULL) {
1543 /* this is the last entry, ignore */
1548 /* Add SPLIT( X, S ) to P. */
1549 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1550 R = split(&X, S, env);
1560 } /* split_by_what */
1562 /** lambda n.(n.type) */
1563 static void *lambda_type(const node_t *node, environment_t *env) {
1565 return node->type.tv;
1568 /** lambda n.(n.opcode) */
1569 static void *lambda_opcode(const node_t *node, environment_t *env) {
1570 opcode_key_t key, *entry;
1571 ir_node *irn = node->node;
1573 key.code = get_irn_opcode(irn);
1574 key.mode = get_irn_mode(irn);
1575 key.arity = get_irn_arity(irn);
1579 switch (get_irn_opcode(irn)) {
1581 key.u.proj = get_Proj_proj(irn);
1584 key.u.ent = get_Sel_entity(irn);
1590 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1592 } /* lambda_opcode */
1594 /** lambda n.(n[i].partition) */
1595 static void *lambda_partition(const node_t *node, environment_t *env) {
1596 ir_node *skipped = skip_Proj(node->node);
1599 int i = env->lambda_input;
1601 if (i >= get_irn_arity(node->node)) {
1603 * We are outside the allowed range: This can happen even
1604 * if we have split by opcode first: doing so might move Followers
1605 * to Leaders and those will have a different opcode!
1606 * Note that in this case the partition is on the cprop list and will be
1612 /* ignore the "control input" for non-pinned nodes
1613 if we are running in GCSE mode */
1614 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1617 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1618 p = get_irn_node(pred);
1621 } /* lambda_partition */
1623 /** lambda n.(n[i].partition) for commutative nodes */
1624 static void *lambda_commutative_partition(const node_t *node, environment_t *env) {
1625 ir_node *irn = node->node;
1626 ir_node *skipped = skip_Proj(irn);
1627 ir_node *pred, *left, *right;
1629 partition_t *pl, *pr;
1630 int i = env->lambda_input;
1632 if (i >= get_irn_arity(node->node)) {
1634 * We are outside the allowed range: This can happen even
1635 * if we have split by opcode first: doing so might move Followers
1636 * to Leaders and those will have a different opcode!
1637 * Note that in this case the partition is on the cprop list and will be
1643 /* ignore the "control input" for non-pinned nodes
1644 if we are running in GCSE mode */
1645 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1649 pred = get_irn_n(skipped, i);
1650 p = get_irn_node(pred);
1654 if (is_op_commutative(get_irn_op(irn))) {
1655 /* normalize partition order by returning the "smaller" on input 0,
1656 the "bigger" on input 1. */
1657 left = get_binop_left(irn);
1658 pl = get_irn_node(left)->part;
1659 right = get_binop_right(irn);
1660 pr = get_irn_node(right)->part;
1663 return pl < pr ? pl : pr;
1665 return pl > pr ? pl : pr;
1667 /* a not split out Follower */
1668 pred = get_irn_n(irn, i);
1669 p = get_irn_node(pred);
1673 } /* lambda_commutative_partition */
1676 * Returns true if a type is a constant (and NOT Top
1679 static int is_con(const lattice_elem_t type) {
1680 /* be conservative */
1681 if (is_tarval(type.tv))
1682 return tarval_is_constant(type.tv);
1683 return is_entity(type.sym.entity_p);
1687 * Implements split_by().
1689 * @param X the partition to split
1690 * @param env the environment
1692 static void split_by(partition_t *X, environment_t *env) {
1693 partition_t *I, *P = NULL;
1696 dump_partition("split_by", X);
1698 if (X->n_leader == 1) {
1699 /* we have only one leader, no need to split, just check it's type */
1700 node_t *x = get_first_node(X);
1701 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1705 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1706 P = split_by_what(X, lambda_type, &P, env);
1709 /* adjust the type tags, we have split partitions by type */
1710 for (I = P; I != NULL; I = I->split_next) {
1711 node_t *x = get_first_node(I);
1712 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1719 if (Y->n_leader > 1) {
1720 /* we do not want split the TOP or constant partitions */
1721 if (! Y->type_is_T_or_C) {
1722 partition_t *Q = NULL;
1724 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1725 Q = split_by_what(Y, lambda_opcode, &Q, env);
1732 if (Z->n_leader > 1) {
1733 const node_t *first = get_first_node(Z);
1734 int arity = get_irn_arity(first->node);
1736 what_func what = lambda_partition;
1737 DEBUG_ONLY(char buf[64];)
1739 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1740 what = lambda_commutative_partition;
1743 * BEWARE: during splitting by input 2 for instance we might
1744 * create new partitions which are different by input 1, so collect
1745 * them and split further.
1747 Z->split_next = NULL;
1750 for (input = arity - 1; input >= -1; --input) {
1752 partition_t *Z_prime = R;
1755 if (Z_prime->n_leader > 1) {
1756 env->lambda_input = input;
1757 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1758 DEBUG_ONLY(what_reason = buf;)
1759 S = split_by_what(Z_prime, what, &S, env);
1762 Z_prime->split_next = S;
1765 } while (R != NULL);
1770 } while (Q != NULL);
1773 } while (P != NULL);
1777 * (Re-)compute the type for a given node.
1779 * @param node the node
1781 static void default_compute(node_t *node) {
1783 ir_node *irn = node->node;
1784 node_t *block = get_irn_node(get_nodes_block(irn));
1786 if (block->type.tv == tarval_unreachable) {
1787 node->type.tv = tarval_top;
1791 /* if any of the data inputs have type top, the result is type top */
1792 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1793 ir_node *pred = get_irn_n(irn, i);
1794 node_t *p = get_irn_node(pred);
1796 if (p->type.tv == tarval_top) {
1797 node->type.tv = tarval_top;
1802 if (get_irn_mode(node->node) == mode_X)
1803 node->type.tv = tarval_reachable;
1805 node->type.tv = computed_value(irn);
1806 } /* default_compute */
1809 * (Re-)compute the type for a Block node.
1811 * @param node the node
1813 static void compute_Block(node_t *node) {
1815 ir_node *block = node->node;
1817 if (block == get_irg_start_block(current_ir_graph)) {
1818 /* start block is always reachable */
1819 node->type.tv = tarval_reachable;
1823 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1824 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1826 if (pred->type.tv == tarval_reachable) {
1827 /* A block is reachable, if at least of predecessor is reachable. */
1828 node->type.tv = tarval_reachable;
1832 node->type.tv = tarval_top;
1833 } /* compute_Block */
1836 * (Re-)compute the type for a Bad node.
1838 * @param node the node
1840 static void compute_Bad(node_t *node) {
1841 /* Bad nodes ALWAYS compute Top */
1842 node->type.tv = tarval_top;
1846 * (Re-)compute the type for an Unknown node.
1848 * @param node the node
1850 static void compute_Unknown(node_t *node) {
1851 /* While Unknown nodes should compute Top this is dangerous:
1852 * a Top input to a Cond would lead to BOTH control flows unreachable.
1853 * While this is correct in the given semantics, it would destroy the Firm
1856 * It would be safe to compute Top IF it can be assured, that only Cmp
1857 * nodes are inputs to Conds. We check that first.
1858 * This is the way Frontends typically build Firm, but some optimizations
1859 * (cond_eval for instance) might replace them by Phib's...
1861 node->type.tv = tarval_UNKNOWN;
1862 } /* compute_Unknown */
1865 * (Re-)compute the type for a Jmp node.
1867 * @param node the node
1869 static void compute_Jmp(node_t *node) {
1870 node_t *block = get_irn_node(get_nodes_block(node->node));
1872 node->type = block->type;
1876 * (Re-)compute the type for the Return node.
1878 * @param node the node
1880 static void compute_Return(node_t *node) {
1881 /* The Return node is NOT dead if it is in a reachable block.
1882 * This is already checked in compute(). so we can return
1883 * Reachable here. */
1884 node->type.tv = tarval_reachable;
1885 } /* compute_Return */
1888 * (Re-)compute the type for the End node.
1890 * @param node the node
1892 static void compute_End(node_t *node) {
1893 /* the End node is NOT dead of course */
1894 node->type.tv = tarval_reachable;
1898 * (Re-)compute the type for a SymConst node.
1900 * @param node the node
1902 static void compute_SymConst(node_t *node) {
1903 ir_node *irn = node->node;
1904 node_t *block = get_irn_node(get_nodes_block(irn));
1906 if (block->type.tv == tarval_unreachable) {
1907 node->type.tv = tarval_top;
1910 switch (get_SymConst_kind(irn)) {
1911 case symconst_addr_ent:
1912 /* case symconst_addr_name: cannot handle this yet */
1913 node->type.sym = get_SymConst_symbol(irn);
1916 node->type.tv = computed_value(irn);
1918 } /* compute_SymConst */
1921 * (Re-)compute the type for a Phi node.
1923 * @param node the node
1925 static void compute_Phi(node_t *node) {
1927 ir_node *phi = node->node;
1928 lattice_elem_t type;
1930 /* if a Phi is in a unreachable block, its type is TOP */
1931 node_t *block = get_irn_node(get_nodes_block(phi));
1933 if (block->type.tv == tarval_unreachable) {
1934 node->type.tv = tarval_top;
1938 /* Phi implements the Meet operation */
1939 type.tv = tarval_top;
1940 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1941 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1942 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1944 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1945 /* ignore TOP inputs: We must check here for unreachable blocks,
1946 because Firm constants live in the Start Block are NEVER Top.
1947 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1948 comes from a unreachable input. */
1951 if (pred->type.tv == tarval_bottom) {
1952 node->type.tv = tarval_bottom;
1954 } else if (type.tv == tarval_top) {
1955 /* first constant found */
1957 } else if (type.tv != pred->type.tv) {
1958 /* different constants or tarval_bottom */
1959 node->type.tv = tarval_bottom;
1962 /* else nothing, constants are the same */
1968 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1970 * @param node the node
1972 static void compute_Add(node_t *node) {
1973 ir_node *sub = node->node;
1974 node_t *l = get_irn_node(get_Add_left(sub));
1975 node_t *r = get_irn_node(get_Add_right(sub));
1976 lattice_elem_t a = l->type;
1977 lattice_elem_t b = r->type;
1980 if (a.tv == tarval_top || b.tv == tarval_top) {
1981 node->type.tv = tarval_top;
1982 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1983 node->type.tv = tarval_bottom;
1985 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1986 must call tarval_add() first to handle this case! */
1987 if (is_tarval(a.tv)) {
1988 if (is_tarval(b.tv)) {
1989 node->type.tv = tarval_add(a.tv, b.tv);
1992 mode = get_tarval_mode(a.tv);
1993 if (a.tv == get_mode_null(mode)) {
1997 } else if (is_tarval(b.tv)) {
1998 mode = get_tarval_mode(b.tv);
1999 if (b.tv == get_mode_null(mode)) {
2004 node->type.tv = tarval_bottom;
2009 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2011 * @param node the node
2013 static void compute_Sub(node_t *node) {
2014 ir_node *sub = node->node;
2015 node_t *l = get_irn_node(get_Sub_left(sub));
2016 node_t *r = get_irn_node(get_Sub_right(sub));
2017 lattice_elem_t a = l->type;
2018 lattice_elem_t b = r->type;
2021 if (a.tv == tarval_top || b.tv == tarval_top) {
2022 node->type.tv = tarval_top;
2023 } else if (is_con(a) && is_con(b)) {
2024 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2025 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2026 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2028 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2031 node->type.tv = tarval_bottom;
2033 node->by_all_const = 1;
2034 } else if (r->part == l->part &&
2035 (!mode_is_float(get_irn_mode(l->node)))) {
2037 * BEWARE: a - a is NOT always 0 for floating Point values, as
2038 * NaN op NaN = NaN, so we must check this here.
2040 ir_mode *mode = get_irn_mode(sub);
2041 tv = get_mode_null(mode);
2043 /* if the node was ONCE evaluated by all constants, but now
2044 this breaks AND we get from the argument partitions a different
2045 result, switch to bottom.
2046 This happens because initially all nodes are in the same partition ... */
2047 if (node->by_all_const && node->type.tv != tv)
2051 node->type.tv = tarval_bottom;
2056 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2058 * @param node the node
2060 static void compute_Eor(node_t *node) {
2061 ir_node *eor = node->node;
2062 node_t *l = get_irn_node(get_Eor_left(eor));
2063 node_t *r = get_irn_node(get_Eor_right(eor));
2064 lattice_elem_t a = l->type;
2065 lattice_elem_t b = r->type;
2068 if (a.tv == tarval_top || b.tv == tarval_top) {
2069 node->type.tv = tarval_top;
2070 } else if (is_con(a) && is_con(b)) {
2071 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2072 node->type.tv = tarval_eor(a.tv, b.tv);
2073 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2075 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2078 node->type.tv = tarval_bottom;
2080 node->by_all_const = 1;
2081 } else if (r->part == l->part) {
2082 ir_mode *mode = get_irn_mode(eor);
2083 tv = get_mode_null(mode);
2085 /* if the node was ONCE evaluated by all constants, but now
2086 this breaks AND we get from the argument partitions a different
2087 result, switch to bottom.
2088 This happens because initially all nodes are in the same partition ... */
2089 if (node->by_all_const && node->type.tv != tv)
2093 node->type.tv = tarval_bottom;
2098 * (Re-)compute the type for Cmp.
2100 * @param node the node
2102 static void compute_Cmp(node_t *node) {
2103 ir_node *cmp = node->node;
2104 node_t *l = get_irn_node(get_Cmp_left(cmp));
2105 node_t *r = get_irn_node(get_Cmp_right(cmp));
2106 lattice_elem_t a = l->type;
2107 lattice_elem_t b = r->type;
2109 if (a.tv == tarval_top || b.tv == tarval_top) {
2111 * Top is congruent to any other value, we can
2112 * calculate the compare result.
2114 node->type.tv = tarval_b_true;
2115 } else if (is_con(a) && is_con(b)) {
2116 /* both nodes are constants, we can probably do something */
2117 node->type.tv = tarval_b_true;
2118 } else if (r->part == l->part) {
2119 /* both nodes congruent, we can probably do something */
2120 node->type.tv = tarval_b_true;
2122 node->type.tv = tarval_bottom;
2127 * (Re-)compute the type for a Proj(Cmp).
2129 * @param node the node
2130 * @param cond the predecessor Cmp node
2132 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
2133 ir_node *proj = node->node;
2134 node_t *l = get_irn_node(get_Cmp_left(cmp));
2135 node_t *r = get_irn_node(get_Cmp_right(cmp));
2136 lattice_elem_t a = l->type;
2137 lattice_elem_t b = r->type;
2138 pn_Cmp pnc = get_Proj_proj(proj);
2141 if (a.tv == tarval_top || b.tv == tarval_top) {
2143 * Top is congruent to any other value, we can
2144 * calculate the compare result.
2147 } else if (is_con(a) && is_con(b)) {
2148 default_compute(node);
2149 node->by_all_const = 1;
2150 } else if (r->part == l->part &&
2151 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2153 * BEWARE: a == a is NOT always True for floating Point values, as
2154 * NaN != NaN is defined, so we must check this here.
2157 tv = pnc & pn_Cmp_Eq ? tarval_b_true: tarval_b_false;
2159 /* if the node was ONCE evaluated by all constants, but now
2160 this breaks AND we get from the argument partitions a different
2161 result, switch to bottom.
2162 This happens because initially all nodes are in the same partition ... */
2163 if (node->by_all_const && node->type.tv != tv)
2167 node->type.tv = tarval_bottom;
2169 } /* compute_Proj_Cmp */
2172 * (Re-)compute the type for a Proj(Cond).
2174 * @param node the node
2175 * @param cond the predecessor Cond node
2177 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
2178 ir_node *proj = node->node;
2179 long pnc = get_Proj_proj(proj);
2180 ir_node *sel = get_Cond_selector(cond);
2181 node_t *selector = get_irn_node(sel);
2183 if (get_irn_mode(sel) == mode_b) {
2185 if (pnc == pn_Cond_true) {
2186 if (selector->type.tv == tarval_b_false) {
2187 node->type.tv = tarval_unreachable;
2188 } else if (selector->type.tv == tarval_b_true) {
2189 node->type.tv = tarval_reachable;
2190 } else if (selector->type.tv == tarval_bottom) {
2191 node->type.tv = tarval_reachable;
2193 assert(selector->type.tv == tarval_top);
2194 node->type.tv = tarval_unreachable;
2197 assert(pnc == pn_Cond_false);
2199 if (selector->type.tv == tarval_b_false) {
2200 node->type.tv = tarval_reachable;
2201 } else if (selector->type.tv == tarval_b_true) {
2202 node->type.tv = tarval_unreachable;
2203 } else if (selector->type.tv == tarval_bottom) {
2204 node->type.tv = tarval_reachable;
2206 assert(selector->type.tv == tarval_top);
2207 node->type.tv = tarval_unreachable;
2212 if (selector->type.tv == tarval_bottom) {
2213 node->type.tv = tarval_reachable;
2214 } else if (selector->type.tv == tarval_top) {
2215 node->type.tv = tarval_unreachable;
2217 long value = get_tarval_long(selector->type.tv);
2218 if (pnc == get_Cond_defaultProj(cond)) {
2219 /* default switch, have to check ALL other cases */
2222 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2223 ir_node *succ = get_irn_out(cond, i);
2227 if (value == get_Proj_proj(succ)) {
2228 /* we found a match, will NOT take the default case */
2229 node->type.tv = tarval_unreachable;
2233 /* all cases checked, no match, will take default case */
2234 node->type.tv = tarval_reachable;
2237 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2241 } /* compute_Proj_Cond */
2244 * (Re-)compute the type for a Proj-Node.
2246 * @param node the node
2248 static void compute_Proj(node_t *node) {
2249 ir_node *proj = node->node;
2250 ir_mode *mode = get_irn_mode(proj);
2251 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2252 ir_node *pred = get_Proj_pred(proj);
2254 if (block->type.tv == tarval_unreachable) {
2255 /* a Proj in a unreachable Block stay Top */
2256 node->type.tv = tarval_top;
2259 if (get_irn_node(pred)->type.tv == tarval_top) {
2260 /* if the predecessor is Top, its Proj follow */
2261 node->type.tv = tarval_top;
2265 if (mode == mode_M) {
2266 /* mode M is always bottom */
2267 node->type.tv = tarval_bottom;
2270 if (mode != mode_X) {
2272 compute_Proj_Cmp(node, pred);
2274 default_compute(node);
2277 /* handle mode_X nodes */
2279 switch (get_irn_opcode(pred)) {
2281 /* the Proj_X from the Start is always reachable.
2282 However this is already handled at the top. */
2283 node->type.tv = tarval_reachable;
2286 compute_Proj_Cond(node, pred);
2289 default_compute(node);
2291 } /* compute_Proj */
2294 * (Re-)compute the type for a Confirm.
2296 * @param node the node
2298 static void compute_Confirm(node_t *node) {
2299 ir_node *confirm = node->node;
2300 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2302 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2303 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2305 if (is_con(bound->type)) {
2306 /* is equal to a constant */
2307 node->type = bound->type;
2311 /* a Confirm is a copy OR a Const */
2312 node->type = pred->type;
2313 } /* compute_Confirm */
2316 * (Re-)compute the type for a Max.
2318 * @param node the node
2320 static void compute_Max(node_t *node) {
2321 ir_node *op = node->node;
2322 node_t *l = get_irn_node(get_binop_left(op));
2323 node_t *r = get_irn_node(get_binop_right(op));
2324 lattice_elem_t a = l->type;
2325 lattice_elem_t b = r->type;
2327 if (a.tv == tarval_top || b.tv == tarval_top) {
2328 node->type.tv = tarval_top;
2329 } else if (is_con(a) && is_con(b)) {
2330 /* both nodes are constants, we can probably do something */
2332 /* this case handles SymConsts as well */
2335 ir_mode *mode = get_irn_mode(op);
2336 tarval *tv_min = get_mode_min(mode);
2340 else if (b.tv == tv_min)
2342 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2343 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2344 node->type.tv = a.tv;
2346 node->type.tv = b.tv;
2348 node->type.tv = tarval_bad;
2351 } else if (r->part == l->part) {
2352 /* both nodes congruent, we can probably do something */
2355 node->type.tv = tarval_bottom;
2360 * (Re-)compute the type for a Min.
2362 * @param node the node
2364 static void compute_Min(node_t *node) {
2365 ir_node *op = node->node;
2366 node_t *l = get_irn_node(get_binop_left(op));
2367 node_t *r = get_irn_node(get_binop_right(op));
2368 lattice_elem_t a = l->type;
2369 lattice_elem_t b = r->type;
2371 if (a.tv == tarval_top || b.tv == tarval_top) {
2372 node->type.tv = tarval_top;
2373 } else if (is_con(a) && is_con(b)) {
2374 /* both nodes are constants, we can probably do something */
2376 /* this case handles SymConsts as well */
2379 ir_mode *mode = get_irn_mode(op);
2380 tarval *tv_max = get_mode_max(mode);
2384 else if (b.tv == tv_max)
2386 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2387 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2388 node->type.tv = a.tv;
2390 node->type.tv = b.tv;
2392 node->type.tv = tarval_bad;
2395 } else if (r->part == l->part) {
2396 /* both nodes congruent, we can probably do something */
2399 node->type.tv = tarval_bottom;
2404 * (Re-)compute the type for a given node.
2406 * @param node the node
2408 static void compute(node_t *node) {
2409 ir_node *irn = node->node;
2412 if (is_no_Block(irn)) {
2413 /* for pinned nodes, check its control input */
2414 if (get_irn_pinned(irn) == op_pin_state_pinned) {
2415 node_t *block = get_irn_node(get_nodes_block(irn));
2417 if (block->type.tv == tarval_unreachable) {
2418 node->type.tv = tarval_top;
2424 func = (compute_func)node->node->op->ops.generic;
2430 * Identity functions: Note that one might thing that identity() is just a
2431 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2432 * here, because it expects that the identity node is one of the inputs, which is NOT
2433 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2434 * So, we have our own implementation, which copies some parts of equivalent_node()
2438 * Calculates the Identity for Phi nodes
2440 static node_t *identity_Phi(node_t *node) {
2441 ir_node *phi = node->node;
2442 ir_node *block = get_nodes_block(phi);
2443 node_t *n_part = NULL;
2446 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2447 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2449 if (pred_X->type.tv == tarval_reachable) {
2450 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2454 else if (n_part->part != pred->part) {
2455 /* incongruent inputs, not a follower */
2460 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2461 * tarval_top, is in the TOP partition and should NOT being split! */
2462 assert(n_part != NULL);
2464 } /* identity_Phi */
2467 * Calculates the Identity for commutative 0 neutral nodes.
2469 static node_t *identity_comm_zero_binop(node_t *node) {
2470 ir_node *op = node->node;
2471 node_t *a = get_irn_node(get_binop_left(op));
2472 node_t *b = get_irn_node(get_binop_right(op));
2473 ir_mode *mode = get_irn_mode(op);
2476 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2477 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2480 /* node: no input should be tarval_top, else the binop would be also
2481 * Top and not being split. */
2482 zero = get_mode_null(mode);
2483 if (a->type.tv == zero)
2485 if (b->type.tv == zero)
2488 } /* identity_comm_zero_binop */
2491 * Calculates the Identity for Shift nodes.
2493 static node_t *identity_shift(node_t *node) {
2494 ir_node *op = node->node;
2495 node_t *b = get_irn_node(get_binop_right(op));
2496 ir_mode *mode = get_irn_mode(b->node);
2499 /* node: no input should be tarval_top, else the binop would be also
2500 * Top and not being split. */
2501 zero = get_mode_null(mode);
2502 if (b->type.tv == zero)
2503 return get_irn_node(get_binop_left(op));
2505 } /* identity_shift */
2508 * Calculates the Identity for Mul nodes.
2510 static node_t *identity_Mul(node_t *node) {
2511 ir_node *op = node->node;
2512 node_t *a = get_irn_node(get_Mul_left(op));
2513 node_t *b = get_irn_node(get_Mul_right(op));
2514 ir_mode *mode = get_irn_mode(op);
2517 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2518 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2521 /* node: no input should be tarval_top, else the binop would be also
2522 * Top and not being split. */
2523 one = get_mode_one(mode);
2524 if (a->type.tv == one)
2526 if (b->type.tv == one)
2529 } /* identity_Mul */
2532 * Calculates the Identity for Sub nodes.
2534 static node_t *identity_Sub(node_t *node) {
2535 ir_node *sub = node->node;
2536 node_t *b = get_irn_node(get_Sub_right(sub));
2537 ir_mode *mode = get_irn_mode(sub);
2539 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2540 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2543 /* node: no input should be tarval_top, else the binop would be also
2544 * Top and not being split. */
2545 if (b->type.tv == get_mode_null(mode))
2546 return get_irn_node(get_Sub_left(sub));
2548 } /* identity_Sub */
2551 * Calculates the Identity for And nodes.
2553 static node_t *identity_And(node_t *node) {
2554 ir_node *and = node->node;
2555 node_t *a = get_irn_node(get_And_left(and));
2556 node_t *b = get_irn_node(get_And_right(and));
2557 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2559 /* node: no input should be tarval_top, else the And would be also
2560 * Top and not being split. */
2561 if (a->type.tv == neutral)
2563 if (b->type.tv == neutral)
2566 } /* identity_And */
2569 * Calculates the Identity for Confirm nodes.
2571 static node_t *identity_Confirm(node_t *node) {
2572 ir_node *confirm = node->node;
2574 /* a Confirm is always a Copy */
2575 return get_irn_node(get_Confirm_value(confirm));
2576 } /* identity_Confirm */
2579 * Calculates the Identity for Mux nodes.
2581 static node_t *identity_Mux(node_t *node) {
2582 ir_node *mux = node->node;
2583 node_t *t = get_irn_node(get_Mux_true(mux));
2584 node_t *f = get_irn_node(get_Mux_false(mux));
2587 if (t->part == f->part)
2590 /* for now, the 1-input identity is not supported */
2592 sel = get_irn_node(get_Mux_sel(mux));
2594 /* Mux sel input is mode_b, so it is always a tarval */
2595 if (sel->type.tv == tarval_b_true)
2597 if (sel->type.tv == tarval_b_false)
2601 } /* identity_Mux */
2604 * Calculates the Identity for Min nodes.
2606 static node_t *identity_Min(node_t *node) {
2607 ir_node *op = node->node;
2608 node_t *a = get_irn_node(get_binop_left(op));
2609 node_t *b = get_irn_node(get_binop_right(op));
2610 ir_mode *mode = get_irn_mode(op);
2613 if (a->part == b->part) {
2614 /* leader of multiple predecessors */
2618 /* works even with NaN */
2619 tv_max = get_mode_max(mode);
2620 if (a->type.tv == tv_max)
2622 if (b->type.tv == tv_max)
2625 } /* identity_Min */
2628 * Calculates the Identity for Max nodes.
2630 static node_t *identity_Max(node_t *node) {
2631 ir_node *op = node->node;
2632 node_t *a = get_irn_node(get_binop_left(op));
2633 node_t *b = get_irn_node(get_binop_right(op));
2634 ir_mode *mode = get_irn_mode(op);
2637 if (a->part == b->part) {
2638 /* leader of multiple predecessors */
2642 /* works even with NaN */
2643 tv_min = get_mode_min(mode);
2644 if (a->type.tv == tv_min)
2646 if (b->type.tv == tv_min)
2649 } /* identity_Max */
2652 * Calculates the Identity for nodes.
2654 static node_t *identity(node_t *node) {
2655 ir_node *irn = node->node;
2657 switch (get_irn_opcode(irn)) {
2659 return identity_Phi(node);
2661 return identity_Mul(node);
2665 return identity_comm_zero_binop(node);
2670 return identity_shift(node);
2672 return identity_And(node);
2674 return identity_Sub(node);
2676 return identity_Confirm(node);
2678 return identity_Mux(node);
2680 return identity_Min(node);
2682 return identity_Max(node);
2689 * Node follower is a (new) follower of leader, segregate Leader
2692 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2693 ir_node *l = leader->node;
2694 int j, i, n = get_irn_n_outs(l);
2696 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2697 /* The leader edges must remain sorted, but follower edges can
2699 for (i = leader->n_followers + 1; i <= n; ++i) {
2700 if (l->out[i].use == follower) {
2701 ir_def_use_edge t = l->out[i];
2703 for (j = i - 1; j >= leader->n_followers + 1; --j)
2704 l->out[j + 1] = l->out[j];
2705 ++leader->n_followers;
2706 l->out[leader->n_followers] = t;
2710 } /* segregate_def_use_chain_1 */
2713 * Node follower is a (new) follower segregate its Leader
2716 * @param follower the follower IR node
2718 static void segregate_def_use_chain(const ir_node *follower) {
2721 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2722 node_t *pred = get_irn_node(get_irn_n(follower, i));
2724 segregate_def_use_chain_1(follower, pred);
2726 } /* segregate_def_use_chain */
2729 * Propagate constant evaluation.
2731 * @param env the environment
2733 static void propagate(environment_t *env) {
2736 lattice_elem_t old_type;
2738 unsigned n_fallen, old_type_was_T_or_C;
2741 while (env->cprop != NULL) {
2742 void *oldopcode = NULL;
2744 /* remove the first partition X from cprop */
2747 env->cprop = X->cprop_next;
2749 old_type_was_T_or_C = X->type_is_T_or_C;
2751 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2754 while (! list_empty(&X->cprop)) {
2755 /* remove the first Node x from X.cprop */
2756 x = list_entry(X->cprop.next, node_t, cprop_list);
2757 //assert(x->part == X);
2758 list_del(&x->cprop_list);
2761 if (x->is_follower && identity(x) == x) {
2762 /* check the opcode first */
2763 if (oldopcode == NULL) {
2764 oldopcode = lambda_opcode(get_first_node(X), env);
2766 if (oldopcode != lambda_opcode(x, env)) {
2767 if (x->on_fallen == 0) {
2768 /* different opcode -> x falls out of this partition */
2773 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2777 /* x will make the follower -> leader transition */
2778 follower_to_leader(x);
2781 /* compute a new type for x */
2783 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2785 if (x->type.tv != old_type.tv) {
2786 verify_type(old_type, x->type);
2787 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2789 if (x->on_fallen == 0) {
2790 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2791 not already on the list. */
2796 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2798 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2799 ir_node *succ = get_irn_out(x->node, i);
2800 node_t *y = get_irn_node(succ);
2802 /* Add y to y.partition.cprop. */
2803 add_to_cprop(y, env);
2808 if (n_fallen > 0 && n_fallen != X->n_leader) {
2809 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2810 Y = split(&X, fallen, env);
2812 * We have split out fallen node. The type of the result
2813 * partition is NOT set yet.
2815 Y->type_is_T_or_C = 0;
2819 /* remove the flags from the fallen list */
2820 for (x = fallen; x != NULL; x = x->next)
2823 if (old_type_was_T_or_C) {
2826 /* check if some nodes will make the leader -> follower transition */
2827 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2828 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2829 node_t *eq_node = identity(y);
2831 if (eq_node != y && eq_node->part == y->part) {
2832 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2833 /* move to Follower */
2835 list_del(&y->node_list);
2836 list_add_tail(&y->node_list, &Y->Follower);
2839 segregate_def_use_chain(y->node);
2849 * Get the leader for a given node from its congruence class.
2851 * @param irn the node
2853 static ir_node *get_leader(node_t *node) {
2854 partition_t *part = node->part;
2856 if (part->n_leader > 1 || node->is_follower) {
2857 if (node->is_follower) {
2858 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2861 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2863 return get_first_node(part)->node;
2869 * Return non-zero if the control flow predecessor node pred
2870 * is the only reachable control flow exit of its block.
2872 * @param pred the control flow exit
2874 static int can_exchange(ir_node *pred) {
2877 else if (is_Jmp(pred))
2879 else if (get_irn_mode(pred) == mode_T) {
2882 /* if the predecessor block has more than one
2883 reachable outputs we cannot remove the block */
2885 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2886 ir_node *proj = get_irn_out(pred, i);
2889 /* skip non-control flow Proj's */
2890 if (get_irn_mode(proj) != mode_X)
2893 node = get_irn_node(proj);
2894 if (node->type.tv == tarval_reachable) {
2902 } /* can_exchange */
2905 * Block Post-Walker, apply the analysis results on control flow by
2906 * shortening Phi's and Block inputs.
2908 static void apply_cf(ir_node *block, void *ctx) {
2909 environment_t *env = ctx;
2910 node_t *node = get_irn_node(block);
2912 ir_node **ins, **in_X;
2913 ir_node *phi, *next;
2915 n = get_Block_n_cfgpreds(block);
2917 if (node->type.tv == tarval_unreachable) {
2920 for (i = n - 1; i >= 0; --i) {
2921 ir_node *pred = get_Block_cfgpred(block, i);
2923 if (! is_Bad(pred)) {
2924 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2926 if (pred_bl->flagged == 0) {
2927 pred_bl->flagged = 3;
2929 if (pred_bl->type.tv == tarval_reachable) {
2931 * We will remove an edge from block to its pred.
2932 * This might leave the pred block as an endless loop
2934 if (! is_backedge(block, i))
2935 keep_alive(pred_bl->node);
2941 /* the EndBlock is always reachable even if the analysis
2942 finds out the opposite :-) */
2943 if (block != get_irg_end_block(current_ir_graph)) {
2944 /* mark dead blocks */
2945 set_Block_dead(block);
2946 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
2948 /* the endblock is unreachable */
2949 set_irn_in(block, 0, NULL);
2955 /* only one predecessor combine */
2956 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2958 if (can_exchange(pred)) {
2959 ir_node *new_block = get_nodes_block(pred);
2960 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
2961 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
2962 exchange(block, new_block);
2963 node->node = new_block;
2969 NEW_ARR_A(ir_node *, in_X, n);
2971 for (i = 0; i < n; ++i) {
2972 ir_node *pred = get_Block_cfgpred(block, i);
2973 node_t *node = get_irn_node(pred);
2975 if (node->type.tv == tarval_reachable) {
2978 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
2979 if (! is_Bad(pred)) {
2980 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2982 if (pred_bl->flagged == 0) {
2983 pred_bl->flagged = 3;
2985 if (pred_bl->type.tv == tarval_reachable) {
2987 * We will remove an edge from block to its pred.
2988 * This might leave the pred block as an endless loop
2990 if (! is_backedge(block, i))
2991 keep_alive(pred_bl->node);
3000 NEW_ARR_A(ir_node *, ins, n);
3001 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3002 node_t *node = get_irn_node(phi);
3004 next = get_Phi_next(phi);
3005 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3006 /* this Phi is replaced by a constant */
3007 tarval *tv = node->type.tv;
3008 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
3010 set_irn_node(c, node);
3012 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3013 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3018 for (i = 0; i < n; ++i) {
3019 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3021 if (pred->type.tv == tarval_reachable) {
3022 ins[j++] = get_Phi_pred(phi, i);
3026 /* this Phi is replaced by a single predecessor */
3027 ir_node *s = ins[0];
3028 node_t *phi_node = get_irn_node(phi);
3031 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3032 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3037 set_irn_in(phi, j, ins);
3044 /* this Block has only one live predecessor */
3045 ir_node *pred = skip_Proj(in_X[0]);
3047 if (can_exchange(pred)) {
3048 ir_node *new_block = get_nodes_block(pred);
3049 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3050 exchange(block, new_block);
3051 node->node = new_block;
3055 set_irn_in(block, k, in_X);
3061 * Exchange a node by its leader.
3062 * Beware: in rare cases the mode might be wrong here, for instance
3063 * AddP(x, NULL) is a follower of x, but with different mode.
3066 static void exchange_leader(ir_node *irn, ir_node *leader) {
3067 ir_mode *mode = get_irn_mode(irn);
3068 if (mode != get_irn_mode(leader)) {
3069 /* The conv is a no-op, so we are fre to place in
3070 * either in the block of the leader OR in irn's block.
3071 * Probably placing it into leaders block might reduce
3072 * the number of Conv due to CSE. */
3073 ir_node *block = get_nodes_block(leader);
3074 dbg_info *dbg = get_irn_dbg_info(irn);
3076 leader = new_rd_Conv(dbg, current_ir_graph, block, leader, mode);
3078 exchange(irn, leader);
3082 * Post-Walker, apply the analysis results;
3084 static void apply_result(ir_node *irn, void *ctx) {
3085 environment_t *env = ctx;
3086 node_t *node = get_irn_node(irn);
3088 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3089 /* blocks already handled, do not touch the End node */
3091 node_t *block = get_irn_node(get_nodes_block(irn));
3093 if (block->type.tv == tarval_unreachable) {
3094 ir_node *bad = get_irg_bad(current_ir_graph);
3096 /* here, bad might already have a node, but this can be safely ignored
3097 as long as bad has at least ONE valid node */
3098 set_irn_node(bad, node);
3100 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3104 else if (node->type.tv == tarval_unreachable) {
3105 /* don't kick away Unknown */
3106 if (! is_Unknown(irn)) {
3107 ir_node *bad = get_irg_bad(current_ir_graph);
3109 /* see comment above */
3110 set_irn_node(bad, node);
3112 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3117 else if (get_irn_mode(irn) == mode_X) {
3120 ir_node *cond = get_Proj_pred(irn);
3122 if (is_Cond(cond)) {
3123 node_t *sel = get_irn_node(get_Cond_selector(cond));
3125 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
3126 /* Cond selector is a constant and the Proj is reachable, make a Jmp */
3127 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
3128 set_irn_node(jmp, node);
3130 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3131 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3138 /* normal data node */
3139 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3140 tarval *tv = node->type.tv;
3143 * Beware: never replace mode_T nodes by constants. Currently we must mark
3144 * mode_T nodes with constants, but do NOT replace them.
3146 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3147 /* can be replaced by a constant */
3148 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
3149 set_irn_node(c, node);
3151 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3152 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3153 exchange_leader(irn, c);
3156 } else if (is_entity(node->type.sym.entity_p)) {
3157 if (! is_SymConst(irn)) {
3158 /* can be replaced by a SymConst */
3159 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3160 set_irn_node(symc, node);
3163 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3164 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3165 exchange_leader(irn, symc);
3168 } else if (is_Confirm(irn)) {
3169 /* Confirms are always follower, but do not kill them here */
3171 ir_node *leader = get_leader(node);
3173 if (leader != irn) {
3174 int non_strict_phi = 0;
3177 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3178 * as this might create non-strict programs.
3180 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3183 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3184 ir_node *pred = get_Phi_pred(irn, i);
3186 if (is_Unknown(pred)) {
3192 if (! non_strict_phi) {
3193 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3194 if (node->is_follower)
3195 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3197 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3198 exchange_leader(irn, leader);
3205 } /* apply_result */
3208 * Fix the keep-alives by deleting unreachable ones.
3210 static void apply_end(ir_node *end, environment_t *env) {
3211 int i, j, n = get_End_n_keepalives(end);
3215 NEW_ARR_A(ir_node *, in, n);
3217 /* fix the keep alive */
3218 for (i = j = 0; i < n; i++) {
3219 ir_node *ka = get_End_keepalive(end, i);
3220 node_t *node = get_irn_node(ka);
3223 node = get_irn_node(get_nodes_block(ka));
3225 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3229 set_End_keepalives(end, j, in);
3234 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3237 * sets the generic functions to compute.
3239 static void set_compute_functions(void) {
3242 /* set the default compute function */
3243 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3244 ir_op *op = get_irp_opcode(i);
3245 op->ops.generic = (op_func)default_compute;
3248 /* set specific functions */
3269 } /* set_compute_functions */
3271 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
3272 #ifdef DEBUG_libfirm
3273 ir_node *irn = local != NULL ? local : n;
3274 node_t *node = get_irn_node(irn);
3276 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
3281 void combo(ir_graph *irg) {
3283 ir_node *initial_bl;
3285 ir_graph *rem = current_ir_graph;
3287 current_ir_graph = irg;
3289 /* register a debug mask */
3290 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3292 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3294 obstack_init(&env.obst);
3295 env.worklist = NULL;
3299 #ifdef DEBUG_libfirm
3300 env.dbg_list = NULL;
3302 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3303 env.type2id_map = pmap_create();
3304 env.end_idx = get_opt_global_cse() ? 0 : -1;
3305 env.lambda_input = 0;
3306 env.nonstd_cond = 0;
3307 env.commutative = 1;
3310 assure_irg_outs(irg);
3311 assure_cf_loop(irg);
3313 /* we have our own value_of function */
3314 set_value_of_func(get_node_tarval);
3316 set_compute_functions();
3317 DEBUG_ONLY(part_nr = 0);
3319 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
3321 /* create the initial partition and place it on the work list */
3322 env.initial = new_partition(&env);
3323 add_to_worklist(env.initial, &env);
3324 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
3327 tarval_UNKNOWN = env.nonstd_cond ? tarval_bad : tarval_top;
3329 tarval_UNKNOWN = tarval_bad;
3332 /* all nodes on the initial partition have type Top */
3333 env.initial->type_is_T_or_C = 1;
3335 /* Place the START Node's partition on cprop.
3336 Place the START Node on its local worklist. */
3337 initial_bl = get_irg_start_block(irg);
3338 start = get_irn_node(initial_bl);
3339 add_to_cprop(start, &env);
3343 if (env.worklist != NULL)
3345 } while (env.cprop != NULL || env.worklist != NULL);
3347 dump_all_partitions(&env);
3348 check_all_partitions(&env);
3351 set_dump_node_vcgattr_hook(dump_partition_hook);
3352 dump_ir_block_graph(irg, "-partition");
3353 set_dump_node_vcgattr_hook(NULL);
3355 (void)dump_partition_hook;
3358 /* apply the result */
3359 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3360 irg_walk_graph(irg, NULL, apply_result, &env);
3361 apply_end(get_irg_end(irg), &env);
3364 /* control flow might changed */
3365 set_irg_outs_inconsistent(irg);
3366 set_irg_extblk_inconsistent(irg);
3367 set_irg_doms_inconsistent(irg);
3368 set_irg_loopinfo_inconsistent(irg);
3371 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
3373 pmap_destroy(env.type2id_map);
3374 del_set(env.opcode2id_map);
3375 obstack_free(&env.obst, NULL);
3377 /* restore value_of() default behavior */
3378 set_value_of_func(NULL);
3379 current_ir_graph = rem;