2 * Copyright (C) 1995-2011 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 * - let Cmp nodes calculate Top like all othe data nodes: this would let
32 * Mux nodes to calculate Unknown instead of taking the true result
33 * - let Cond(Top) always select FALSE/default: This is tricky. Nodes are only reavaluated
34 * IFF the predecessor changed its type. Because nodes are initialized with Top
35 * this never happens, let all Proj(Cond) be unreachable.
36 * We avoid this condition by the same way we work around Phi: whenever a Block
37 * node is placed on the list, place its Cond nodes (and because they are Tuple
38 * all its Proj-nodes either on the cprop list)
39 * Especially, this changes the meaning of Click's example:
54 * using Click's version while is silent with our.
55 * - support for global congruences is implemented but not tested yet
57 * Note further that we use the terminology from Click's work here, which is different
58 * in some cases from Firm terminology. Especially, Click's type is a
59 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
65 #include "iroptimize.h"
72 #include "irgraph_t.h"
79 #include "iropt_dbg.h"
83 #include "irnodeset.h"
91 /* define this to check that all type translations are monotone */
92 #define VERIFY_MONOTONE
94 /* define this to check the consistency of partitions */
95 #define CHECK_PARTITIONS
97 typedef struct node_t node_t;
98 typedef struct partition_t partition_t;
99 typedef struct opcode_key_t opcode_key_t;
100 typedef struct listmap_entry_t listmap_entry_t;
102 /** The type of the compute function. */
103 typedef void (*compute_func)(node_t *node);
108 struct opcode_key_t {
109 ir_node *irn; /**< An IR node representing this opcode. */
113 * An entry in the list_map.
115 struct listmap_entry_t {
116 void *id; /**< The id. */
117 node_t *list; /**< The associated list for this id. */
118 listmap_entry_t *next; /**< Link to the next entry in the map. */
121 /** We must map id's to lists. */
122 typedef struct listmap_t {
123 set *map; /**< Map id's to listmap_entry_t's */
124 listmap_entry_t *values; /**< List of all values in the map. */
128 * A lattice element. Because we handle constants and symbolic constants different, we
129 * have to use this union.
140 ir_node *node; /**< The IR-node itself. */
141 list_head node_list; /**< Double-linked list of leader/follower entries. */
142 list_head cprop_list; /**< Double-linked partition.cprop list. */
143 partition_t *part; /**< points to the partition this node belongs to */
144 node_t *next; /**< Next node on local list (partition.touched, fallen). */
145 node_t *race_next; /**< Next node on race list. */
146 lattice_elem_t type; /**< The associated lattice element "type". */
147 int max_user_input; /**< Maximum input number of Def-Use edges. */
148 int next_edge; /**< Index of the next Def-Use edge to use. */
149 int n_followers; /**< Number of Follower in the outs set. */
150 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
151 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
152 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
153 unsigned is_follower:1; /**< Set, if this node is a follower. */
154 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
158 * A partition containing congruent nodes.
161 list_head Leader; /**< The head of partition Leader node list. */
162 list_head Follower; /**< The head of partition Follower node list. */
163 list_head cprop; /**< The head of partition.cprop list. */
164 list_head cprop_X; /**< The head of partition.cprop (Cond nodes and its Projs) list. */
165 partition_t *wl_next; /**< Next entry in the work list if any. */
166 partition_t *touched_next; /**< Points to the next partition in the touched set. */
167 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
168 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
169 node_t *touched; /**< The partition.touched set of this partition. */
170 unsigned n_leader; /**< Number of entries in this partition.Leader. */
171 unsigned n_touched; /**< Number of entries in the partition.touched. */
172 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
173 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
174 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
175 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
176 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
178 partition_t *dbg_next; /**< Link all partitions for debugging */
179 unsigned nr; /**< A unique number for (what-)mapping, >0. */
183 typedef struct environment_t {
184 struct obstack obst; /**< obstack to allocate data structures. */
185 partition_t *worklist; /**< The work list. */
186 partition_t *cprop; /**< The constant propagation list. */
187 partition_t *touched; /**< the touched set. */
188 partition_t *initial; /**< The initial partition. */
189 set *opcode2id_map; /**< The opcodeMode->id map. */
190 ir_node **kept_memory; /**< Array of memory nodes that must be kept. */
191 int end_idx; /**< -1 for local and 0 for global congruences. */
192 int lambda_input; /**< Captured argument for lambda_partition(). */
193 unsigned modified:1; /**< Set, if the graph was modified. */
194 unsigned unopt_cf:1; /**< If set, control flow is not optimized due to Unknown. */
195 /* options driving the optimization */
196 unsigned commutative:1; /**< Set, if commutation nodes should be handled specially. */
197 unsigned opt_unknown:1; /**< Set, if non-strict programs should be optimized. */
199 partition_t *dbg_list; /**< List of all partitions. */
203 /** Type of the what function. */
204 typedef void *(*what_func)(const node_t *node, environment_t *env);
206 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
207 #define set_irn_node(irn, node) set_irn_link(irn, node)
209 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
210 #undef tarval_unreachable
211 #define tarval_unreachable tarval_top
214 /** The debug module handle. */
215 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
217 /** The what reason. */
218 DEBUG_ONLY(static const char *what_reason;)
220 /** Next partition number. */
221 DEBUG_ONLY(static unsigned part_nr = 0);
223 /** The tarval returned by Unknown nodes: set to either tarval_bad OR tarval_top. */
224 static ir_tarval *tarval_UNKNOWN;
227 static node_t *identity(node_t *node);
230 * Compare two opcode representatives.
232 static int cmp_irn_opcode(const ir_node *a, const ir_node *b)
236 if ((get_irn_op(a) != get_irn_op(b)) ||
237 (get_irn_mode(a) != get_irn_mode(b)))
240 /* compare if a's in and b's in are of equal length */
241 arity = get_irn_arity(a);
242 if (arity != get_irn_arity(b))
247 * Some ugliness here: Two Blocks having the same
248 * IJmp predecessor would be congruent, which of course is wrong.
249 * We fix it by never letting blocks be congruent
250 * which cannot be detected by combo either.
256 * here, we already now that the nodes are identical except their
259 if (a->op->ops.node_cmp_attr)
260 return a->op->ops.node_cmp_attr(a, b);
263 } /* cmp_irn_opcode */
265 #ifdef CHECK_PARTITIONS
269 static void check_partition(const partition_t *T)
274 list_for_each_entry(node_t, node, &T->Leader, node_list) {
275 assert(node->is_follower == 0);
276 assert(node->flagged == 0);
277 assert(node->part == T);
280 assert(n == T->n_leader);
282 list_for_each_entry(node_t, node, &T->Follower, node_list) {
283 assert(node->is_follower == 1);
284 assert(node->flagged == 0);
285 assert(node->part == T);
287 } /* check_partition */
290 * check that all leader nodes in the partition have the same opcode.
292 static void check_opcode(const partition_t *Z)
298 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
299 ir_node *irn = node->node;
305 assert(cmp_irn_opcode(repr, irn) == 0);
310 static void check_all_partitions(environment_t *env)
316 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
318 if (! P->type_is_T_or_C)
320 list_for_each_entry(node_t, node, &P->Follower, node_list) {
321 node_t *leader = identity(node);
323 assert(leader != node && leader->part == node->part);
334 static void do_check_list(const node_t *list, int ofs, const partition_t *Z)
339 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
340 for (e = list; e != NULL; e = NEXT(e)) {
341 assert(e->part == Z);
349 } /* ido_check_list */
352 * Check a local list.
354 static void check_list(const node_t *list, const partition_t *Z)
356 do_check_list(list, offsetof(node_t, next), Z);
360 #define check_partition(T)
361 #define check_list(list, Z)
362 #define check_all_partitions(env)
363 #endif /* CHECK_PARTITIONS */
366 static inline lattice_elem_t get_partition_type(const partition_t *X);
369 * Dump partition to output.
371 static void dump_partition(const char *msg, const partition_t *part)
375 lattice_elem_t type = get_partition_type(part);
377 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
378 msg, part->nr, part->type_is_T_or_C ? "*" : "",
379 part->n_leader, type));
380 list_for_each_entry(node_t, node, &part->Leader, node_list) {
381 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
384 if (! list_empty(&part->Follower)) {
385 DB((dbg, LEVEL_2, "\n---\n "));
387 list_for_each_entry(node_t, node, &part->Follower, node_list) {
388 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
392 DB((dbg, LEVEL_2, "\n}\n"));
393 } /* dump_partition */
398 static void do_dump_list(const char *msg, const node_t *node, int ofs)
403 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
405 DB((dbg, LEVEL_3, "%s = {\n ", msg));
406 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
407 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
410 DB((dbg, LEVEL_3, "\n}\n"));
418 static void dump_race_list(const char *msg, const node_t *list)
420 do_dump_list(msg, list, offsetof(node_t, race_next));
421 } /* dump_race_list */
424 * Dumps a local list.
426 static void dump_list(const char *msg, const node_t *list)
428 do_dump_list(msg, list, offsetof(node_t, next));
432 * Dump all partitions.
434 static void dump_all_partitions(const environment_t *env)
436 const partition_t *P;
438 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
439 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
440 dump_partition("", P);
441 } /* dump_all_partitions */
446 static void dump_split_list(const partition_t *list)
448 const partition_t *p;
450 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
451 for (p = list; p != NULL; p = p->split_next)
452 DB((dbg, LEVEL_2, "part%u, ", p->nr));
453 DB((dbg, LEVEL_2, "\n}\n"));
454 } /* dump_split_list */
457 * Dump partition and type for a node.
459 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local)
461 ir_node *irn = local != NULL ? local : n;
462 node_t *node = get_irn_node(irn);
464 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
466 } /* dump_partition_hook */
469 #define dump_partition(msg, part)
470 #define dump_race_list(msg, list)
471 #define dump_list(msg, list)
472 #define dump_all_partitions(env)
473 #define dump_split_list(list)
476 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
478 * Verify that a type transition is monotone
480 static void verify_type(const lattice_elem_t old_type, node_t *node)
482 if (old_type.tv == node->type.tv) {
486 if (old_type.tv == tarval_top) {
487 /* from Top down-to is always allowed */
490 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
494 panic("combo: wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
498 #define verify_type(old_type, node)
502 * Compare two pointer values of a listmap.
504 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
506 const listmap_entry_t *e1 = (listmap_entry_t*)elt;
507 const listmap_entry_t *e2 = (listmap_entry_t*)key;
510 return e1->id != e2->id;
511 } /* listmap_cmp_ptr */
514 * Initializes a listmap.
516 * @param map the listmap
518 static void listmap_init(listmap_t *map)
520 map->map = new_set(listmap_cmp_ptr, 16);
525 * Terminates a listmap.
527 * @param map the listmap
529 static void listmap_term(listmap_t *map)
535 * Return the associated listmap entry for a given id.
537 * @param map the listmap
538 * @param id the id to search for
540 * @return the associated listmap entry for the given id
542 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
544 listmap_entry_t key, *entry;
549 entry = (listmap_entry_t*)set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
551 if (entry->list == NULL) {
552 /* a new entry, put into the list */
553 entry->next = map->values;
560 * Calculate the hash value for an opcode map entry.
562 * @param entry an opcode map entry
564 * @return a hash value for the given opcode map entry
566 static unsigned opcode_hash(const opcode_key_t *entry)
568 /* we cannot use the ir ops hash function here, because it hashes the
570 const ir_node *n = entry->irn;
571 ir_opcode code = get_irn_opcode(n);
572 ir_mode *mode = get_irn_mode(n);
573 unsigned hash = (unsigned)(PTR_TO_INT(mode) * 9 + code) + get_irn_arity(n);
575 if (code == iro_Const)
576 hash ^= (unsigned)HASH_PTR(get_Const_tarval(n));
577 else if (code == iro_Proj)
578 hash += (unsigned)get_Proj_proj(n);
583 * Compare two entries in the opcode map.
585 static int cmp_opcode(const void *elt, const void *key, size_t size)
587 const opcode_key_t *o1 = (opcode_key_t*)elt;
588 const opcode_key_t *o2 = (opcode_key_t*)key;
592 return cmp_irn_opcode(o1->irn, o2->irn);
596 * Compare two Def-Use edges for input position.
598 static int cmp_def_use_edge(const void *a, const void *b)
600 const ir_def_use_edge *ea = (const ir_def_use_edge*)a;
601 const ir_def_use_edge *eb = (const ir_def_use_edge*)b;
603 /* no overrun, because range is [-1, MAXINT] */
604 return ea->pos - eb->pos;
605 } /* cmp_def_use_edge */
608 * We need the Def-Use edges sorted.
610 static void sort_irn_outs(node_t *node)
612 ir_node *irn = node->node;
613 int n_outs = get_irn_n_outs(irn);
616 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
618 node->max_user_input = irn->out[n_outs].pos;
619 } /* sort_irn_outs */
622 * Return the type of a node.
624 * @param irn an IR-node
626 * @return the associated type of this node
628 static inline lattice_elem_t get_node_type(const ir_node *irn)
630 return get_irn_node(irn)->type;
631 } /* get_node_type */
634 * Return the tarval of a node.
636 * @param irn an IR-node
638 * @return the associated type of this node
640 static inline ir_tarval *get_node_tarval(const ir_node *irn)
642 lattice_elem_t type = get_node_type(irn);
644 if (is_tarval(type.tv))
646 return tarval_bottom;
647 } /* get_node_type */
650 * Add a partition to the worklist.
652 static inline void add_to_worklist(partition_t *X, environment_t *env)
654 assert(X->on_worklist == 0);
655 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
656 X->wl_next = env->worklist;
659 } /* add_to_worklist */
662 * Create a new empty partition.
664 * @param env the environment
666 * @return a newly allocated partition
668 static inline partition_t *new_partition(environment_t *env)
670 partition_t *part = OALLOC(&env->obst, partition_t);
672 INIT_LIST_HEAD(&part->Leader);
673 INIT_LIST_HEAD(&part->Follower);
674 INIT_LIST_HEAD(&part->cprop);
675 INIT_LIST_HEAD(&part->cprop_X);
676 part->wl_next = NULL;
677 part->touched_next = NULL;
678 part->cprop_next = NULL;
679 part->split_next = NULL;
680 part->touched = NULL;
683 part->max_user_inputs = 0;
684 part->on_worklist = 0;
685 part->on_touched = 0;
687 part->type_is_T_or_C = 0;
689 part->dbg_next = env->dbg_list;
690 env->dbg_list = part;
691 part->nr = part_nr++;
695 } /* new_partition */
698 * Get the first node from a partition.
700 static inline node_t *get_first_node(const partition_t *X)
702 return list_entry(X->Leader.next, node_t, node_list);
703 } /* get_first_node */
706 * Return the type of a partition (assuming partition is non-empty and
707 * all elements have the same type).
709 * @param X a partition
711 * @return the type of the first element of the partition
713 static inline lattice_elem_t get_partition_type(const partition_t *X)
715 const node_t *first = get_first_node(X);
717 } /* get_partition_type */
720 * Creates a partition node for the given IR-node and place it
721 * into the given partition.
723 * @param irn an IR-node
724 * @param part a partition to place the node in
725 * @param env the environment
727 * @return the created node
729 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env)
731 /* create a partition node and place it in the partition */
732 node_t *node = OALLOC(&env->obst, node_t);
734 INIT_LIST_HEAD(&node->node_list);
735 INIT_LIST_HEAD(&node->cprop_list);
739 node->race_next = NULL;
740 node->type.tv = tarval_top;
741 node->max_user_input = 0;
743 node->n_followers = 0;
744 node->on_touched = 0;
747 node->is_follower = 0;
749 set_irn_node(irn, node);
751 list_add_tail(&node->node_list, &part->Leader);
755 } /* create_partition_node */
758 * Pre-Walker, initialize all Nodes' type to U or top and place
759 * all nodes into the TOP partition.
761 static void create_initial_partitions(ir_node *irn, void *ctx)
763 environment_t *env = (environment_t*)ctx;
764 partition_t *part = env->initial;
767 node = create_partition_node(irn, part, env);
769 if (node->max_user_input > part->max_user_inputs)
770 part->max_user_inputs = node->max_user_input;
773 set_Block_phis(irn, NULL);
775 } /* create_initial_partitions */
778 * Post-Walker, collect all Block-Phi lists, set Cond.
780 static void init_block_phis(ir_node *irn, void *ctx)
785 add_Block_phi(get_nodes_block(irn), irn);
787 } /* init_block_phis */
790 * Add a node to the entry.partition.touched set and
791 * node->partition to the touched set if not already there.
794 * @param env the environment
796 static inline void add_to_touched(node_t *y, environment_t *env)
798 if (y->on_touched == 0) {
799 partition_t *part = y->part;
801 y->next = part->touched;
806 if (part->on_touched == 0) {
807 part->touched_next = env->touched;
809 part->on_touched = 1;
812 check_list(part->touched, part);
814 } /* add_to_touched */
817 * Place a node on the cprop list.
820 * @param env the environment
822 static void add_to_cprop(node_t *y, environment_t *env)
826 /* Add y to y.partition.cprop. */
827 if (y->on_cprop == 0) {
828 partition_t *Y = y->part;
829 ir_node *irn = y->node;
831 /* place Conds and all its Projs on the cprop_X list */
832 if (is_Cond(skip_Proj(irn)))
833 list_add_tail(&y->cprop_list, &Y->cprop_X);
835 list_add_tail(&y->cprop_list, &Y->cprop);
838 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
840 /* place its partition on the cprop list */
841 if (Y->on_cprop == 0) {
842 Y->cprop_next = env->cprop;
848 if (get_irn_mode(irn) == mode_T) {
849 /* mode_T nodes always produce tarval_bottom, so we must explicitly
850 add it's Proj's to get constant evaluation to work */
853 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
854 node_t *proj = get_irn_node(get_irn_out(irn, i));
856 add_to_cprop(proj, env);
858 } else if (is_Block(irn)) {
859 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
860 * if someone placed the block. The Block is only placed if the reachability
861 * changes, and this must be re-evaluated in compute_Phi(). */
863 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
864 node_t *p = get_irn_node(phi);
865 add_to_cprop(p, env);
871 * Update the worklist: If Z is on worklist then add Z' to worklist.
872 * Else add the smaller of Z and Z' to worklist.
874 * @param Z the Z partition
875 * @param Z_prime the Z' partition, a previous part of Z
876 * @param env the environment
878 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env)
880 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
881 add_to_worklist(Z_prime, env);
883 add_to_worklist(Z, env);
885 } /* update_worklist */
888 * Make all inputs to x no longer be F.def_use edges.
892 static void move_edges_to_leader(node_t *x)
894 ir_node *irn = x->node;
897 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
898 node_t *pred = get_irn_node(get_irn_n(irn, i));
903 n = get_irn_n_outs(p);
904 for (j = 1; j <= pred->n_followers; ++j) {
905 if (p->out[j].pos == i && p->out[j].use == irn) {
906 /* found a follower edge to x, move it to the Leader */
907 ir_def_use_edge edge = p->out[j];
909 /* remove this edge from the Follower set */
910 p->out[j] = p->out[pred->n_followers];
913 /* sort it into the leader set */
914 for (k = pred->n_followers + 2; k <= n; ++k) {
915 if (p->out[k].pos >= edge.pos)
917 p->out[k - 1] = p->out[k];
919 /* place the new edge here */
920 p->out[k - 1] = edge;
922 /* edge found and moved */
927 } /* move_edges_to_leader */
930 * Split a partition that has NO followers by a local list.
932 * @param Z partition to split
933 * @param g a (non-empty) node list
934 * @param env the environment
936 * @return a new partition containing the nodes of g
938 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env)
940 partition_t *Z_prime;
945 dump_partition("Splitting ", Z);
946 dump_list("by list ", g);
950 /* Remove g from Z. */
951 for (node = g; node != NULL; node = node->next) {
952 assert(node->part == Z);
953 list_del(&node->node_list);
956 assert(n < Z->n_leader);
959 /* Move g to a new partition, Z'. */
960 Z_prime = new_partition(env);
962 for (node = g; node != NULL; node = node->next) {
963 list_add_tail(&node->node_list, &Z_prime->Leader);
964 node->part = Z_prime;
965 if (node->max_user_input > max_input)
966 max_input = node->max_user_input;
968 Z_prime->max_user_inputs = max_input;
969 Z_prime->n_leader = n;
972 check_partition(Z_prime);
974 /* for now, copy the type info tag, it will be adjusted in split_by(). */
975 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
977 update_worklist(Z, Z_prime, env);
979 dump_partition("Now ", Z);
980 dump_partition("Created new ", Z_prime);
982 } /* split_no_followers */
985 * Make the Follower -> Leader transition for a node.
989 static void follower_to_leader(node_t *n)
991 assert(n->is_follower == 1);
993 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
995 move_edges_to_leader(n);
996 list_del(&n->node_list);
997 list_add_tail(&n->node_list, &n->part->Leader);
999 } /* follower_to_leader */
1002 * The environment for one race step.
1004 typedef struct step_env {
1005 node_t *initial; /**< The initial node list. */
1006 node_t *unwalked; /**< The unwalked node list. */
1007 node_t *walked; /**< The walked node list. */
1008 int index; /**< Next index of Follower use_def edge. */
1009 unsigned side; /**< side number. */
1013 * Return non-zero, if a input is a real follower
1015 * @param irn the node to check
1016 * @param input number of the input
1018 static int is_real_follower(const ir_node *irn, int input)
1022 switch (get_irn_opcode(irn)) {
1025 /* ignore the Confirm bound input */
1031 /* ignore the Mux sel input */
1036 /* dead inputs are not follower edges */
1037 ir_node *block = get_nodes_block(irn);
1038 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
1040 if (pred->type.tv == tarval_unreachable)
1050 /* only a Sub x,0 / Shift x,0 might be a follower */
1057 pred = get_irn_node(get_irn_n(irn, input));
1058 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1062 pred = get_irn_node(get_irn_n(irn, input));
1063 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1067 pred = get_irn_node(get_irn_n(irn, input));
1068 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1072 assert(!"opcode not implemented yet");
1076 } /* is_real_follower */
1079 * Do one step in the race.
1081 static int step(step_env *env)
1085 if (env->initial != NULL) {
1086 /* Move node from initial to unwalked */
1088 env->initial = n->race_next;
1090 n->race_next = env->unwalked;
1096 while (env->unwalked != NULL) {
1097 /* let n be the first node in unwalked */
1099 while (env->index < n->n_followers) {
1100 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1102 /* let m be n.F.def_use[index] */
1103 node_t *m = get_irn_node(edge->use);
1105 assert(m->is_follower);
1107 * Some inputs, like the get_Confirm_bound are NOT
1108 * real followers, sort them out.
1110 if (! is_real_follower(m->node, edge->pos)) {
1116 /* only followers from our partition */
1117 if (m->part != n->part)
1120 if ((m->flagged & env->side) == 0) {
1121 m->flagged |= env->side;
1123 if (m->flagged != 3) {
1124 /* visited the first time */
1125 /* add m to unwalked not as first node (we might still need to
1126 check for more follower node */
1127 m->race_next = n->race_next;
1131 /* else already visited by the other side and on the other list */
1134 /* move n to walked */
1135 env->unwalked = n->race_next;
1136 n->race_next = env->walked;
1144 * Clear the flags from a list and check for
1145 * nodes that where touched from both sides.
1147 * @param list the list
1149 static int clear_flags(node_t *list)
1154 for (n = list; n != NULL; n = n->race_next) {
1155 if (n->flagged == 3) {
1156 /* we reach a follower from both sides, this will split congruent
1157 * inputs and make it a leader. */
1158 follower_to_leader(n);
1167 * Split a partition by a local list using the race.
1169 * @param pX pointer to the partition to split, might be changed!
1170 * @param gg a (non-empty) node list
1171 * @param env the environment
1173 * @return a new partition containing the nodes of gg
1175 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env)
1177 partition_t *X = *pX;
1178 partition_t *X_prime;
1181 node_t *g, *h, *node, *t;
1182 int max_input, transitions, winner, shf;
1184 DEBUG_ONLY(static int run = 0;)
1186 DB((dbg, LEVEL_2, "Run %d ", run++));
1187 if (list_empty(&X->Follower)) {
1188 /* if the partition has NO follower, we can use the fast
1189 splitting algorithm. */
1190 return split_no_followers(X, gg, env);
1192 /* else do the race */
1194 dump_partition("Splitting ", X);
1195 dump_list("by list ", gg);
1197 INIT_LIST_HEAD(&tmp);
1199 /* Remove gg from X.Leader and put into g */
1201 for (node = gg; node != NULL; node = node->next) {
1202 assert(node->part == X);
1203 assert(node->is_follower == 0);
1205 list_del(&node->node_list);
1206 list_add_tail(&node->node_list, &tmp);
1207 node->race_next = g;
1212 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1213 node->race_next = h;
1216 /* restore X.Leader */
1217 list_splice(&tmp, &X->Leader);
1219 senv[0].initial = g;
1220 senv[0].unwalked = NULL;
1221 senv[0].walked = NULL;
1225 senv[1].initial = h;
1226 senv[1].unwalked = NULL;
1227 senv[1].walked = NULL;
1232 * Some informations on the race that are not stated clearly in Click's
1234 * 1) A follower stays on the side that reach him first.
1235 * 2) If the other side reches a follower, if will be converted to
1236 * a leader. /This must be done after the race is over, else the
1237 * edges we are iterating on are renumbered./
1238 * 3) /New leader might end up on both sides./
1239 * 4) /If one side ends up with new Leaders, we must ensure that
1240 * they can split out by opcode, hence we have to put _every_
1241 * partition with new Leader nodes on the cprop list, as
1242 * opcode splitting is done by split_by() at the end of
1243 * constant propagation./
1246 if (step(&senv[0])) {
1250 if (step(&senv[1])) {
1255 assert(senv[winner].initial == NULL);
1256 assert(senv[winner].unwalked == NULL);
1258 /* clear flags from walked/unwalked */
1260 transitions = clear_flags(senv[0].unwalked) << shf;
1261 transitions |= clear_flags(senv[0].walked) << shf;
1263 transitions |= clear_flags(senv[1].unwalked) << shf;
1264 transitions |= clear_flags(senv[1].walked) << shf;
1266 dump_race_list("winner ", senv[winner].walked);
1268 /* Move walked_{winner} to a new partition, X'. */
1269 X_prime = new_partition(env);
1272 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1273 list_del(&node->node_list);
1274 node->part = X_prime;
1275 if (node->is_follower) {
1276 list_add_tail(&node->node_list, &X_prime->Follower);
1278 list_add_tail(&node->node_list, &X_prime->Leader);
1281 if (node->max_user_input > max_input)
1282 max_input = node->max_user_input;
1284 X_prime->n_leader = n;
1285 X_prime->max_user_inputs = max_input;
1286 X->n_leader -= X_prime->n_leader;
1288 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1289 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1292 * Even if a follower was not checked by both sides, it might have
1293 * loose its congruence, so we need to check this case for all follower.
1295 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1296 if (identity(node) == node) {
1297 follower_to_leader(node);
1303 check_partition(X_prime);
1305 /* X' is the smaller part */
1306 add_to_worklist(X_prime, env);
1309 * If there where follower to leader transitions, ensure that the nodes
1310 * can be split out if necessary.
1312 if (transitions & 1) {
1313 /* place winner partition on the cprop list */
1314 if (X_prime->on_cprop == 0) {
1315 X_prime->cprop_next = env->cprop;
1316 env->cprop = X_prime;
1317 X_prime->on_cprop = 1;
1320 if (transitions & 2) {
1321 /* place other partition on the cprop list */
1322 if (X->on_cprop == 0) {
1323 X->cprop_next = env->cprop;
1329 dump_partition("Now ", X);
1330 dump_partition("Created new ", X_prime);
1332 /* we have to ensure that the partition containing g is returned */
1342 * Returns non-zero if the i'th input of a Phi node is live.
1344 * @param phi a Phi-node
1345 * @param i an input number
1347 * @return non-zero if the i'th input of the given Phi node is live
1349 static int is_live_input(ir_node *phi, int i)
1352 ir_node *block = get_nodes_block(phi);
1353 ir_node *pred = get_Block_cfgpred(block, i);
1354 lattice_elem_t type = get_node_type(pred);
1356 return type.tv != tarval_unreachable;
1358 /* else it's the control input, always live */
1360 } /* is_live_input */
1363 * Return non-zero if a type is a constant.
1365 static int is_constant_type(lattice_elem_t type)
1367 if (type.tv != tarval_bottom && type.tv != tarval_top)
1370 } /* is_constant_type */
1373 * Check whether a type is neither Top or a constant.
1374 * Note: U is handled like Top here, R is a constant.
1376 * @param type the type to check
1378 static int type_is_neither_top_nor_const(const lattice_elem_t type)
1380 if (is_tarval(type.tv)) {
1381 if (type.tv == tarval_top)
1383 if (tarval_is_constant(type.tv))
1390 } /* type_is_neither_top_nor_const */
1393 * Collect nodes to the touched list.
1395 * @param list the list which contains the nodes that must be evaluated
1396 * @param idx the index of the def_use edge to evaluate
1397 * @param env the environment
1399 static void collect_touched(list_head *list, int idx, environment_t *env)
1402 int end_idx = env->end_idx;
1404 list_for_each_entry(node_t, x, list, node_list) {
1408 /* leader edges start AFTER follower edges */
1409 x->next_edge = x->n_followers + 1;
1411 num_edges = get_irn_n_outs(x->node);
1413 /* for all edges in x.L.def_use_{idx} */
1414 while (x->next_edge <= num_edges) {
1415 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1418 /* check if we have necessary edges */
1419 if (edge->pos > idx)
1426 /* only non-commutative nodes */
1427 if (env->commutative &&
1428 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1431 /* ignore the "control input" for non-pinned nodes
1432 if we are running in GCSE mode */
1433 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1436 y = get_irn_node(succ);
1437 assert(get_irn_n(succ, idx) == x->node);
1439 /* ignore block edges touching followers */
1440 if (idx == -1 && y->is_follower)
1443 if (is_constant_type(y->type)) {
1444 unsigned code = get_irn_opcode(succ);
1445 if (code == iro_Sub || code == iro_Cmp)
1446 add_to_cprop(y, env);
1449 /* Partitions of constants should not be split simply because their Nodes have unequal
1450 functions or incongruent inputs. */
1451 if (type_is_neither_top_nor_const(y->type) &&
1452 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1453 add_to_touched(y, env);
1457 } /* collect_touched */
1460 * Collect commutative nodes to the touched list.
1462 * @param list the list which contains the nodes that must be evaluated
1463 * @param env the environment
1465 static void collect_commutative_touched(list_head *list, environment_t *env)
1469 list_for_each_entry(node_t, x, list, node_list) {
1472 num_edges = get_irn_n_outs(x->node);
1474 x->next_edge = x->n_followers + 1;
1476 /* for all edges in x.L.def_use_{idx} */
1477 while (x->next_edge <= num_edges) {
1478 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1481 /* check if we have necessary edges */
1491 /* only commutative nodes */
1492 if (!is_op_commutative(get_irn_op(succ)))
1495 y = get_irn_node(succ);
1496 if (is_constant_type(y->type)) {
1497 unsigned code = get_irn_opcode(succ);
1498 if (code == iro_Eor)
1499 add_to_cprop(y, env);
1502 /* Partitions of constants should not be split simply because their Nodes have unequal
1503 functions or incongruent inputs. */
1504 if (type_is_neither_top_nor_const(y->type)) {
1505 add_to_touched(y, env);
1509 } /* collect_commutative_touched */
1512 * Split the partitions if caused by the first entry on the worklist.
1514 * @param env the environment
1516 static void cause_splits(environment_t *env)
1518 partition_t *X, *Z, *N;
1521 /* remove the first partition from the worklist */
1523 env->worklist = X->wl_next;
1526 dump_partition("Cause_split: ", X);
1528 if (env->commutative) {
1529 /* handle commutative nodes first */
1531 /* empty the touched set: already done, just clear the list */
1532 env->touched = NULL;
1534 collect_commutative_touched(&X->Leader, env);
1535 collect_commutative_touched(&X->Follower, env);
1537 for (Z = env->touched; Z != NULL; Z = N) {
1539 node_t *touched = Z->touched;
1540 node_t *touched_aa = NULL;
1541 node_t *touched_ab = NULL;
1542 unsigned n_touched_aa = 0;
1543 unsigned n_touched_ab = 0;
1545 assert(Z->touched != NULL);
1547 /* beware, split might change Z */
1548 N = Z->touched_next;
1550 /* remove it from the touched set */
1553 /* Empty local Z.touched. */
1554 for (e = touched; e != NULL; e = n) {
1555 node_t *left = get_irn_node(get_irn_n(e->node, 0));
1556 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1558 assert(e->is_follower == 0);
1563 * Note: op(a, a) is NOT congruent to op(a, b).
1564 * So, we must split the touched list.
1566 if (left->part == right->part) {
1567 e->next = touched_aa;
1571 e->next = touched_ab;
1576 assert(n_touched_aa + n_touched_ab == Z->n_touched);
1580 if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1581 partition_t *Z_prime = Z;
1582 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1583 split(&Z_prime, touched_aa, env);
1585 assert(n_touched_aa <= Z->n_leader);
1587 if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1588 partition_t *Z_prime = Z;
1589 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1590 split(&Z_prime, touched_ab, env);
1592 assert(n_touched_ab <= Z->n_leader);
1596 /* combine temporary leader and follower list */
1597 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1598 /* empty the touched set: already done, just clear the list */
1599 env->touched = NULL;
1601 collect_touched(&X->Leader, idx, env);
1602 collect_touched(&X->Follower, idx, env);
1604 for (Z = env->touched; Z != NULL; Z = N) {
1606 node_t *touched = Z->touched;
1607 unsigned n_touched = Z->n_touched;
1609 assert(Z->touched != NULL);
1611 /* beware, split might change Z */
1612 N = Z->touched_next;
1614 /* remove it from the touched set */
1617 /* Empty local Z.touched. */
1618 for (e = touched; e != NULL; e = e->next) {
1619 assert(e->is_follower == 0);
1625 if (0 < n_touched && n_touched < Z->n_leader) {
1626 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1627 split(&Z, touched, env);
1629 assert(n_touched <= Z->n_leader);
1632 } /* cause_splits */
1635 * Implements split_by_what(): Split a partition by characteristics given
1636 * by the what function.
1638 * @param X the partition to split
1639 * @param What a function returning an Id for every node of the partition X
1640 * @param P a list to store the result partitions
1641 * @param env the environment
1645 static partition_t *split_by_what(partition_t *X, what_func What,
1646 partition_t **P, environment_t *env)
1650 listmap_entry_t *iter;
1653 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1655 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1656 void *id = What(x, env);
1657 listmap_entry_t *entry;
1660 /* input not allowed, ignore */
1663 /* Add x to map[What(x)]. */
1664 entry = listmap_find(&map, id);
1665 x->next = entry->list;
1668 /* Let P be a set of Partitions. */
1670 /* for all sets S except one in the range of map do */
1671 for (iter = map.values; iter != NULL; iter = iter->next) {
1672 if (iter->next == NULL) {
1673 /* this is the last entry, ignore */
1678 /* Add SPLIT( X, S ) to P. */
1679 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1680 R = split(&X, S, env);
1690 } /* split_by_what */
1692 /** lambda n.(n.type) */
1693 static void *lambda_type(const node_t *node, environment_t *env)
1696 return node->type.tv;
1699 /** lambda n.(n.opcode) */
1700 static void *lambda_opcode(const node_t *node, environment_t *env)
1702 opcode_key_t key, *entry;
1704 key.irn = node->node;
1706 entry = (opcode_key_t*)set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1708 } /* lambda_opcode */
1710 /** lambda n.(n[i].partition) */
1711 static void *lambda_partition(const node_t *node, environment_t *env)
1713 ir_node *skipped = skip_Proj(node->node);
1716 int i = env->lambda_input;
1718 if (i >= get_irn_arity(node->node)) {
1720 * We are outside the allowed range: This can happen even
1721 * if we have split by opcode first: doing so might move Followers
1722 * to Leaders and those will have a different opcode!
1723 * Note that in this case the partition is on the cprop list and will be
1729 /* ignore the "control input" for non-pinned nodes
1730 if we are running in GCSE mode */
1731 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1734 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1735 p = get_irn_node(pred);
1737 } /* lambda_partition */
1739 /** lambda n.(n[i].partition) for commutative nodes */
1740 static void *lambda_commutative_partition(const node_t *node, environment_t *env)
1742 ir_node *irn = node->node;
1743 ir_node *skipped = skip_Proj(irn);
1744 ir_node *pred, *left, *right;
1746 partition_t *pl, *pr;
1747 int i = env->lambda_input;
1749 if (i >= get_irn_arity(node->node)) {
1751 * We are outside the allowed range: This can happen even
1752 * if we have split by opcode first: doing so might move Followers
1753 * to Leaders and those will have a different opcode!
1754 * Note that in this case the partition is on the cprop list and will be
1760 /* ignore the "control input" for non-pinned nodes
1761 if we are running in GCSE mode */
1762 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1766 pred = get_irn_n(skipped, i);
1767 p = get_irn_node(pred);
1771 if (is_op_commutative(get_irn_op(irn))) {
1772 /* normalize partition order by returning the "smaller" on input 0,
1773 the "bigger" on input 1. */
1774 left = get_binop_left(irn);
1775 pl = get_irn_node(left)->part;
1776 right = get_binop_right(irn);
1777 pr = get_irn_node(right)->part;
1780 return pl < pr ? pl : pr;
1782 return pl > pr ? pl : pr;
1784 /* a not split out Follower */
1785 pred = get_irn_n(irn, i);
1786 p = get_irn_node(pred);
1790 } /* lambda_commutative_partition */
1793 * Returns true if a type is a constant (and NOT Top
1796 static int is_con(const lattice_elem_t type)
1798 /* be conservative */
1799 if (is_tarval(type.tv))
1800 return tarval_is_constant(type.tv);
1801 return is_entity(type.sym.entity_p);
1805 * Implements split_by().
1807 * @param X the partition to split
1808 * @param env the environment
1810 static void split_by(partition_t *X, environment_t *env)
1812 partition_t *I, *P = NULL;
1815 dump_partition("split_by", X);
1817 if (X->n_leader == 1) {
1818 /* we have only one leader, no need to split, just check it's type */
1819 node_t *x = get_first_node(X);
1820 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1824 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1825 P = split_by_what(X, lambda_type, &P, env);
1828 /* adjust the type tags, we have split partitions by type */
1829 for (I = P; I != NULL; I = I->split_next) {
1830 node_t *x = get_first_node(I);
1831 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1838 if (Y->n_leader > 1) {
1839 /* we do not want split the TOP or constant partitions */
1840 if (! Y->type_is_T_or_C) {
1841 partition_t *Q = NULL;
1843 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1844 Q = split_by_what(Y, lambda_opcode, &Q, env);
1851 if (Z->n_leader > 1) {
1852 const node_t *first = get_first_node(Z);
1853 int arity = get_irn_arity(first->node);
1855 what_func what = lambda_partition;
1856 DEBUG_ONLY(char buf[64];)
1858 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1859 what = lambda_commutative_partition;
1862 * BEWARE: during splitting by input 2 for instance we might
1863 * create new partitions which are different by input 1, so collect
1864 * them and split further.
1866 Z->split_next = NULL;
1869 for (input = arity - 1; input >= -1; --input) {
1871 partition_t *Z_prime = R;
1874 if (Z_prime->n_leader > 1) {
1875 env->lambda_input = input;
1876 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1877 DEBUG_ONLY(what_reason = buf;)
1878 S = split_by_what(Z_prime, what, &S, env);
1881 Z_prime->split_next = S;
1884 } while (R != NULL);
1889 } while (Q != NULL);
1892 } while (P != NULL);
1896 * (Re-)compute the type for a given node.
1898 * @param node the node
1900 static void default_compute(node_t *node)
1903 ir_node *irn = node->node;
1905 /* if any of the data inputs have type top, the result is type top */
1906 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1907 ir_node *pred = get_irn_n(irn, i);
1908 node_t *p = get_irn_node(pred);
1910 if (p->type.tv == tarval_top) {
1911 node->type.tv = tarval_top;
1916 if (get_irn_mode(node->node) == mode_X)
1917 node->type.tv = tarval_reachable;
1919 node->type.tv = computed_value(irn);
1920 } /* default_compute */
1923 * (Re-)compute the type for a Block node.
1925 * @param node the node
1927 static void compute_Block(node_t *node)
1930 ir_node *block = node->node;
1932 if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
1933 /* start block and labelled blocks are always reachable */
1934 node->type.tv = tarval_reachable;
1938 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1939 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1941 if (pred->type.tv == tarval_reachable) {
1942 /* A block is reachable, if at least of predecessor is reachable. */
1943 node->type.tv = tarval_reachable;
1947 node->type.tv = tarval_top;
1948 } /* compute_Block */
1951 * (Re-)compute the type for a Bad node.
1953 * @param node the node
1955 static void compute_Bad(node_t *node)
1957 /* Bad nodes ALWAYS compute Top */
1958 node->type.tv = tarval_top;
1962 * (Re-)compute the type for an Unknown node.
1964 * @param node the node
1966 static void compute_Unknown(node_t *node)
1968 /* While Unknown nodes should compute Top this is dangerous:
1969 * a Top input to a Cond would lead to BOTH control flows unreachable.
1970 * While this is correct in the given semantics, it would destroy the Firm
1973 * It would be safe to compute Top IF it can be assured, that only Cmp
1974 * nodes are inputs to Conds. We check that first.
1975 * This is the way Frontends typically build Firm, but some optimizations
1976 * (jump threading for instance) might replace them by Phib's...
1978 node->type.tv = tarval_UNKNOWN;
1979 } /* compute_Unknown */
1982 * (Re-)compute the type for a Jmp node.
1984 * @param node the node
1986 static void compute_Jmp(node_t *node)
1988 node_t *block = get_irn_node(get_nodes_block(node->node));
1990 node->type = block->type;
1994 * (Re-)compute the type for the Return node.
1996 * @param node the node
1998 static void compute_Return(node_t *node)
2000 /* The Return node is NOT dead if it is in a reachable block.
2001 * This is already checked in compute(). so we can return
2002 * Reachable here. */
2003 node->type.tv = tarval_reachable;
2004 } /* compute_Return */
2007 * (Re-)compute the type for the End node.
2009 * @param node the node
2011 static void compute_End(node_t *node)
2013 /* the End node is NOT dead of course */
2014 node->type.tv = tarval_reachable;
2018 * (Re-)compute the type for a Call.
2020 * @param node the node
2022 static void compute_Call(node_t *node)
2025 * A Call computes always bottom, even if it has Unknown
2028 node->type.tv = tarval_bottom;
2029 } /* compute_Call */
2032 * (Re-)compute the type for a SymConst node.
2034 * @param node the node
2036 static void compute_SymConst(node_t *node)
2038 ir_node *irn = node->node;
2039 node_t *block = get_irn_node(get_nodes_block(irn));
2041 if (block->type.tv == tarval_unreachable) {
2042 node->type.tv = tarval_top;
2045 switch (get_SymConst_kind(irn)) {
2046 case symconst_addr_ent:
2047 node->type.sym = get_SymConst_symbol(irn);
2050 node->type.tv = computed_value(irn);
2052 } /* compute_SymConst */
2055 * (Re-)compute the type for a Phi node.
2057 * @param node the node
2059 static void compute_Phi(node_t *node)
2062 ir_node *phi = node->node;
2063 lattice_elem_t type;
2065 /* if a Phi is in a unreachable block, its type is TOP */
2066 node_t *block = get_irn_node(get_nodes_block(phi));
2068 if (block->type.tv == tarval_unreachable) {
2069 node->type.tv = tarval_top;
2073 /* Phi implements the Meet operation */
2074 type.tv = tarval_top;
2075 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2076 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2077 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2079 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2080 /* ignore TOP inputs: We must check here for unreachable blocks,
2081 because Firm constants live in the Start Block are NEVER Top.
2082 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2083 comes from a unreachable input. */
2086 if (pred->type.tv == tarval_bottom) {
2087 node->type.tv = tarval_bottom;
2089 } else if (type.tv == tarval_top) {
2090 /* first constant found */
2092 } else if (type.tv != pred->type.tv) {
2093 /* different constants or tarval_bottom */
2094 node->type.tv = tarval_bottom;
2097 /* else nothing, constants are the same */
2103 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2105 * @param node the node
2107 static void compute_Add(node_t *node)
2109 ir_node *sub = node->node;
2110 node_t *l = get_irn_node(get_Add_left(sub));
2111 node_t *r = get_irn_node(get_Add_right(sub));
2112 lattice_elem_t a = l->type;
2113 lattice_elem_t b = r->type;
2116 if (a.tv == tarval_top || b.tv == tarval_top) {
2117 node->type.tv = tarval_top;
2118 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2119 node->type.tv = tarval_bottom;
2121 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2122 must call tarval_add() first to handle this case! */
2123 if (is_tarval(a.tv)) {
2124 if (is_tarval(b.tv)) {
2125 node->type.tv = tarval_add(a.tv, b.tv);
2128 mode = get_tarval_mode(a.tv);
2129 if (a.tv == get_mode_null(mode)) {
2133 } else if (is_tarval(b.tv)) {
2134 mode = get_tarval_mode(b.tv);
2135 if (b.tv == get_mode_null(mode)) {
2140 node->type.tv = tarval_bottom;
2145 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2147 * @param node the node
2149 static void compute_Sub(node_t *node)
2151 ir_node *sub = node->node;
2152 node_t *l = get_irn_node(get_Sub_left(sub));
2153 node_t *r = get_irn_node(get_Sub_right(sub));
2154 lattice_elem_t a = l->type;
2155 lattice_elem_t b = r->type;
2158 if (a.tv == tarval_top || b.tv == tarval_top) {
2159 node->type.tv = tarval_top;
2160 } else if (is_con(a) && is_con(b)) {
2161 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2162 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2163 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2165 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2168 node->type.tv = tarval_bottom;
2170 } else if (r->part == l->part &&
2171 (!mode_is_float(get_irn_mode(l->node)))) {
2173 * BEWARE: a - a is NOT always 0 for floating Point values, as
2174 * NaN op NaN = NaN, so we must check this here.
2176 ir_mode *mode = get_irn_mode(sub);
2177 tv = get_mode_null(mode);
2179 /* if the node was ONCE evaluated by all constants, but now
2180 this breaks AND we get from the argument partitions a different
2181 result, switch to bottom.
2182 This happens because initially all nodes are in the same partition ... */
2183 if (node->type.tv != tv)
2187 node->type.tv = tarval_bottom;
2192 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2194 * @param node the node
2196 static void compute_Eor(node_t *node)
2198 ir_node *eor = node->node;
2199 node_t *l = get_irn_node(get_Eor_left(eor));
2200 node_t *r = get_irn_node(get_Eor_right(eor));
2201 lattice_elem_t a = l->type;
2202 lattice_elem_t b = r->type;
2205 if (a.tv == tarval_top || b.tv == tarval_top) {
2206 node->type.tv = tarval_top;
2207 } else if (is_con(a) && is_con(b)) {
2208 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2209 node->type.tv = tarval_eor(a.tv, b.tv);
2210 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2212 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2215 node->type.tv = tarval_bottom;
2217 } else if (r->part == l->part) {
2218 ir_mode *mode = get_irn_mode(eor);
2219 tv = get_mode_null(mode);
2221 /* if the node was ONCE evaluated by all constants, but now
2222 this breaks AND we get from the argument partitions a different
2223 result, switch to bottom.
2224 This happens because initially all nodes are in the same partition ... */
2225 if (node->type.tv != tv)
2229 node->type.tv = tarval_bottom;
2234 * (Re-)compute the type for Cmp.
2236 * @param node the node
2238 static void compute_Cmp(node_t *node)
2240 ir_node *cmp = node->node;
2241 node_t *l = get_irn_node(get_Cmp_left(cmp));
2242 node_t *r = get_irn_node(get_Cmp_right(cmp));
2243 lattice_elem_t a = l->type;
2244 lattice_elem_t b = r->type;
2245 ir_mode *mode = get_irn_mode(get_Cmp_left(cmp));
2247 if (a.tv == tarval_top || b.tv == tarval_top) {
2248 node->type.tv = tarval_top;
2249 } else if (r->part == l->part) {
2250 /* both nodes congruent, we can probably do something */
2251 if (mode_is_float(mode)) {
2252 /* beware of NaN's */
2253 node->type.tv = tarval_bottom;
2255 node->type.tv = tarval_b_true;
2257 } else if (is_con(a) && is_con(b)) {
2258 node->type.tv = tarval_b_true;
2260 node->type.tv = tarval_bottom;
2265 * (Re-)compute the type for a Proj(Cmp).
2267 * @param node the node
2268 * @param cond the predecessor Cmp node
2270 static void compute_Proj_Cmp(node_t *node, ir_node *cmp)
2272 ir_node *proj = node->node;
2273 node_t *l = get_irn_node(get_Cmp_left(cmp));
2274 node_t *r = get_irn_node(get_Cmp_right(cmp));
2275 lattice_elem_t a = l->type;
2276 lattice_elem_t b = r->type;
2277 pn_Cmp pnc = get_Proj_pn_cmp(proj);
2280 if (a.tv == tarval_top || b.tv == tarval_top) {
2281 node->type.tv = tarval_undefined;
2282 } else if (is_con(a) && is_con(b)) {
2283 default_compute(node);
2286 * BEWARE: a == a is NOT always True for floating Point values, as
2287 * NaN != NaN is defined, so we must check this here.
2288 * (while for some pnc we could still optimize we have to stay
2289 * consistent with compute_Cmp, so don't do anything for floats)
2291 } else if (r->part == l->part && !mode_is_float(get_irn_mode(l->node))) {
2292 tv = pnc & pn_Cmp_Eq ? tarval_b_true : tarval_b_false;
2294 /* if the node was ONCE evaluated by all constants, but now
2295 this breaks AND we get from the argument partitions a different
2296 result, switch to bottom.
2297 This happens because initially all nodes are in the same partition ... */
2298 if (node->type.tv != tv)
2302 node->type.tv = tarval_bottom;
2304 } /* compute_Proj_Cmp */
2307 * (Re-)compute the type for a Proj(Cond).
2309 * @param node the node
2310 * @param cond the predecessor Cond node
2312 static void compute_Proj_Cond(node_t *node, ir_node *cond)
2314 ir_node *proj = node->node;
2315 long pnc = get_Proj_proj(proj);
2316 ir_node *sel = get_Cond_selector(cond);
2317 node_t *selector = get_irn_node(sel);
2320 * Note: it is crucial for the monotony that the Proj(Cond)
2321 * are evaluates after all predecessors of the Cond selector are
2327 * Due to the fact that 0 is a const, the Cmp gets immediately
2328 * on the cprop list. It will be evaluated before x is evaluated,
2329 * might leaving x as Top. When later x is evaluated, the Cmp
2330 * might change its value.
2331 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2332 * gets R, and later changed to F if Cmp is evaluated to True!
2334 * We prevent this by putting Conds in an extra cprop_X queue, which
2335 * gets evaluated after the cprop queue is empty.
2337 * Note that this even happens with Click's original algorithm, if
2338 * Cmp(x, 0) is evaluated to True first and later changed to False
2339 * if x was Top first and later changed to a Const ...
2340 * It is unclear how Click solved that problem ...
2342 * However, in rare cases even this does not help, if a Top reaches
2343 * a compare through a Phi, than Proj(Cond) is evaluated changing
2344 * the type of the Phi to something other.
2345 * So, we take the last resort and bind the type to R once
2348 * (This might be even the way Click works around the whole problem).
2350 * Finally, we may miss some optimization possibilities due to this:
2355 * If Top reaches the if first, than we decide for != here.
2356 * If y later is evaluated to 0, we cannot revert this decision
2357 * and must live with both outputs enabled. If this happens,
2358 * we get an unresolved if (true) in the code ...
2360 * In Click's version where this decision is done at the Cmp,
2361 * the Cmp is NOT optimized away than (if y evaluated to 1
2362 * for instance) and we get a if (1 == 0) here ...
2364 * Both solutions are suboptimal.
2365 * At least, we could easily detect this problem and run
2366 * cf_opt() (or even combo) again :-(
2368 if (node->type.tv == tarval_reachable)
2371 if (get_irn_mode(sel) == mode_b) {
2373 if (pnc == pn_Cond_true) {
2374 if (selector->type.tv == tarval_b_false) {
2375 node->type.tv = tarval_unreachable;
2376 } else if (selector->type.tv == tarval_b_true) {
2377 node->type.tv = tarval_reachable;
2378 } else if (selector->type.tv == tarval_bottom) {
2379 node->type.tv = tarval_reachable;
2381 assert(selector->type.tv == tarval_top);
2382 if (tarval_UNKNOWN == tarval_top) {
2383 /* any condition based on Top is "!=" */
2384 node->type.tv = tarval_unreachable;
2386 node->type.tv = tarval_unreachable;
2390 assert(pnc == pn_Cond_false);
2392 if (selector->type.tv == tarval_b_false) {
2393 node->type.tv = tarval_reachable;
2394 } else if (selector->type.tv == tarval_b_true) {
2395 node->type.tv = tarval_unreachable;
2396 } else if (selector->type.tv == tarval_bottom) {
2397 node->type.tv = tarval_reachable;
2399 assert(selector->type.tv == tarval_top);
2400 if (tarval_UNKNOWN == tarval_top) {
2401 /* any condition based on Top is "!=" */
2402 node->type.tv = tarval_reachable;
2404 node->type.tv = tarval_unreachable;
2410 if (selector->type.tv == tarval_bottom) {
2411 node->type.tv = tarval_reachable;
2412 } else if (selector->type.tv == tarval_top) {
2413 if (tarval_UNKNOWN == tarval_top &&
2414 pnc == get_Cond_default_proj(cond)) {
2415 /* a switch based of Top is always "default" */
2416 node->type.tv = tarval_reachable;
2418 node->type.tv = tarval_unreachable;
2421 long value = get_tarval_long(selector->type.tv);
2422 if (pnc == get_Cond_default_proj(cond)) {
2423 /* default switch, have to check ALL other cases */
2426 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2427 ir_node *succ = get_irn_out(cond, i);
2431 if (value == get_Proj_proj(succ)) {
2432 /* we found a match, will NOT take the default case */
2433 node->type.tv = tarval_unreachable;
2437 /* all cases checked, no match, will take default case */
2438 node->type.tv = tarval_reachable;
2441 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2445 } /* compute_Proj_Cond */
2448 * (Re-)compute the type for a Proj-Node.
2450 * @param node the node
2452 static void compute_Proj(node_t *node)
2454 ir_node *proj = node->node;
2455 ir_mode *mode = get_irn_mode(proj);
2456 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2457 ir_node *pred = get_Proj_pred(proj);
2459 if (block->type.tv == tarval_unreachable) {
2460 /* a Proj in a unreachable Block stay Top */
2461 node->type.tv = tarval_top;
2464 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred)) {
2465 /* if the predecessor is Top, its Proj follow */
2466 node->type.tv = tarval_top;
2470 if (mode == mode_M) {
2471 /* mode M is always bottom */
2472 node->type.tv = tarval_bottom;
2475 if (mode != mode_X) {
2477 compute_Proj_Cmp(node, pred);
2479 default_compute(node);
2482 /* handle mode_X nodes */
2484 switch (get_irn_opcode(pred)) {
2486 /* the Proj_X from the Start is always reachable.
2487 However this is already handled at the top. */
2488 node->type.tv = tarval_reachable;
2491 compute_Proj_Cond(node, pred);
2494 default_compute(node);
2496 } /* compute_Proj */
2499 * (Re-)compute the type for a Confirm.
2501 * @param node the node
2503 static void compute_Confirm(node_t *node)
2505 ir_node *confirm = node->node;
2506 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2508 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2509 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2511 if (is_con(bound->type)) {
2512 /* is equal to a constant */
2513 node->type = bound->type;
2517 /* a Confirm is a copy OR a Const */
2518 node->type = pred->type;
2519 } /* compute_Confirm */
2522 * (Re-)compute the type for a given node.
2524 * @param node the node
2526 static void compute(node_t *node)
2528 ir_node *irn = node->node;
2531 #ifndef VERIFY_MONOTONE
2533 * Once a node reaches bottom, the type cannot fall further
2534 * in the lattice and we can stop computation.
2535 * Do not take this exit if the monotony verifier is
2536 * enabled to catch errors.
2538 if (node->type.tv == tarval_bottom)
2542 if (!is_Block(irn)) {
2543 /* for pinned nodes, check its control input */
2544 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2545 node_t *block = get_irn_node(get_nodes_block(irn));
2547 if (block->type.tv == tarval_unreachable) {
2548 node->type.tv = tarval_top;
2554 func = (compute_func)node->node->op->ops.generic;
2560 * Identity functions: Note that one might think that identity() is just a
2561 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2562 * here, because it expects that the identity node is one of the inputs, which is NOT
2563 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2564 * So, we have our own implementation, which copies some parts of equivalent_node()
2568 * Calculates the Identity for Phi nodes
2570 static node_t *identity_Phi(node_t *node)
2572 ir_node *phi = node->node;
2573 ir_node *block = get_nodes_block(phi);
2574 node_t *n_part = NULL;
2577 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2578 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2580 if (pred_X->type.tv == tarval_reachable) {
2581 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2585 else if (n_part->part != pred->part) {
2586 /* incongruent inputs, not a follower */
2591 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2592 * tarval_top, is in the TOP partition and should NOT being split! */
2593 assert(n_part != NULL);
2595 } /* identity_Phi */
2598 * Calculates the Identity for commutative 0 neutral nodes.
2600 static node_t *identity_comm_zero_binop(node_t *node)
2602 ir_node *op = node->node;
2603 node_t *a = get_irn_node(get_binop_left(op));
2604 node_t *b = get_irn_node(get_binop_right(op));
2605 ir_mode *mode = get_irn_mode(op);
2608 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2609 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2612 /* node: no input should be tarval_top, else the binop would be also
2613 * Top and not being split. */
2614 zero = get_mode_null(mode);
2615 if (a->type.tv == zero)
2617 if (b->type.tv == zero)
2620 } /* identity_comm_zero_binop */
2623 * Calculates the Identity for Shift nodes.
2625 static node_t *identity_shift(node_t *node)
2627 ir_node *op = node->node;
2628 node_t *b = get_irn_node(get_binop_right(op));
2629 ir_mode *mode = get_irn_mode(b->node);
2632 /* node: no input should be tarval_top, else the binop would be also
2633 * Top and not being split. */
2634 zero = get_mode_null(mode);
2635 if (b->type.tv == zero)
2636 return get_irn_node(get_binop_left(op));
2638 } /* identity_shift */
2641 * Calculates the Identity for Mul nodes.
2643 static node_t *identity_Mul(node_t *node)
2645 ir_node *op = node->node;
2646 node_t *a = get_irn_node(get_Mul_left(op));
2647 node_t *b = get_irn_node(get_Mul_right(op));
2648 ir_mode *mode = get_irn_mode(op);
2651 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2652 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2655 /* node: no input should be tarval_top, else the binop would be also
2656 * Top and not being split. */
2657 one = get_mode_one(mode);
2658 if (a->type.tv == one)
2660 if (b->type.tv == one)
2663 } /* identity_Mul */
2666 * Calculates the Identity for Sub nodes.
2668 static node_t *identity_Sub(node_t *node)
2670 ir_node *sub = node->node;
2671 node_t *b = get_irn_node(get_Sub_right(sub));
2672 ir_mode *mode = get_irn_mode(sub);
2674 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2675 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2678 /* node: no input should be tarval_top, else the binop would be also
2679 * Top and not being split. */
2680 if (b->type.tv == get_mode_null(mode))
2681 return get_irn_node(get_Sub_left(sub));
2683 } /* identity_Sub */
2686 * Calculates the Identity for And nodes.
2688 static node_t *identity_And(node_t *node)
2690 ir_node *andnode = node->node;
2691 node_t *a = get_irn_node(get_And_left(andnode));
2692 node_t *b = get_irn_node(get_And_right(andnode));
2693 ir_tarval *neutral = get_mode_all_one(get_irn_mode(andnode));
2695 /* node: no input should be tarval_top, else the And would be also
2696 * Top and not being split. */
2697 if (a->type.tv == neutral)
2699 if (b->type.tv == neutral)
2702 } /* identity_And */
2705 * Calculates the Identity for Confirm nodes.
2707 static node_t *identity_Confirm(node_t *node)
2709 ir_node *confirm = node->node;
2711 /* a Confirm is always a Copy */
2712 return get_irn_node(get_Confirm_value(confirm));
2713 } /* identity_Confirm */
2716 * Calculates the Identity for Mux nodes.
2718 static node_t *identity_Mux(node_t *node)
2720 ir_node *mux = node->node;
2721 node_t *t = get_irn_node(get_Mux_true(mux));
2722 node_t *f = get_irn_node(get_Mux_false(mux));
2725 if (t->part == f->part)
2728 /* for now, the 1-input identity is not supported */
2730 sel = get_irn_node(get_Mux_sel(mux));
2732 /* Mux sel input is mode_b, so it is always a tarval */
2733 if (sel->type.tv == tarval_b_true)
2735 if (sel->type.tv == tarval_b_false)
2739 } /* identity_Mux */
2742 * Calculates the Identity for nodes.
2744 static node_t *identity(node_t *node)
2746 ir_node *irn = node->node;
2748 switch (get_irn_opcode(irn)) {
2750 return identity_Phi(node);
2752 return identity_Mul(node);
2756 return identity_comm_zero_binop(node);
2761 return identity_shift(node);
2763 return identity_And(node);
2765 return identity_Sub(node);
2767 return identity_Confirm(node);
2769 return identity_Mux(node);
2776 * Node follower is a (new) follower of leader, segregate Leader
2779 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader)
2781 ir_node *l = leader->node;
2782 int j, i, n = get_irn_n_outs(l);
2784 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2785 /* The leader edges must remain sorted, but follower edges can
2787 for (i = leader->n_followers + 1; i <= n; ++i) {
2788 if (l->out[i].use == follower) {
2789 ir_def_use_edge t = l->out[i];
2791 for (j = i - 1; j >= leader->n_followers + 1; --j)
2792 l->out[j + 1] = l->out[j];
2793 ++leader->n_followers;
2794 l->out[leader->n_followers] = t;
2798 } /* segregate_def_use_chain_1 */
2801 * Node follower is a (new) follower segregate its Leader
2804 * @param follower the follower IR node
2806 static void segregate_def_use_chain(const ir_node *follower)
2810 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2811 node_t *pred = get_irn_node(get_irn_n(follower, i));
2813 segregate_def_use_chain_1(follower, pred);
2815 } /* segregate_def_use_chain */
2818 * Propagate constant evaluation.
2820 * @param env the environment
2822 static void propagate(environment_t *env)
2826 lattice_elem_t old_type;
2828 unsigned n_fallen, old_type_was_T_or_C;
2831 while (env->cprop != NULL) {
2832 void *oldopcode = NULL;
2834 /* remove the first partition X from cprop */
2837 env->cprop = X->cprop_next;
2839 old_type_was_T_or_C = X->type_is_T_or_C;
2841 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2845 int cprop_empty = list_empty(&X->cprop);
2846 int cprop_X_empty = list_empty(&X->cprop_X);
2848 if (cprop_empty && cprop_X_empty) {
2849 /* both cprop lists are empty */
2853 /* remove the first Node x from X.cprop */
2855 /* Get a node from the cprop_X list only if
2856 * all data nodes are processed.
2857 * This ensures, that all inputs of the Cond
2858 * predecessor are processed if its type is still Top.
2860 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2862 x = list_entry(X->cprop.next, node_t, cprop_list);
2865 //assert(x->part == X);
2866 list_del(&x->cprop_list);
2869 if (x->is_follower && identity(x) == x) {
2870 /* check the opcode first */
2871 if (oldopcode == NULL) {
2872 oldopcode = lambda_opcode(get_first_node(X), env);
2874 if (oldopcode != lambda_opcode(x, env)) {
2875 if (x->on_fallen == 0) {
2876 /* different opcode -> x falls out of this partition */
2881 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2885 /* x will make the follower -> leader transition */
2886 follower_to_leader(x);
2889 /* compute a new type for x */
2891 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2893 if (x->type.tv != old_type.tv) {
2894 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2895 verify_type(old_type, x);
2897 if (x->on_fallen == 0) {
2898 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2899 not already on the list. */
2904 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2906 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2907 ir_node *succ = get_irn_out(x->node, i);
2908 node_t *y = get_irn_node(succ);
2910 /* Add y to y.partition.cprop. */
2911 add_to_cprop(y, env);
2916 if (n_fallen > 0 && n_fallen != X->n_leader) {
2917 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2918 Y = split(&X, fallen, env);
2920 * We have split out fallen node. The type of the result
2921 * partition is NOT set yet.
2923 Y->type_is_T_or_C = 0;
2927 /* remove the flags from the fallen list */
2928 for (x = fallen; x != NULL; x = x->next)
2931 if (old_type_was_T_or_C) {
2934 /* check if some nodes will make the leader -> follower transition */
2935 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2936 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2937 node_t *eq_node = identity(y);
2939 if (eq_node != y && eq_node->part == y->part) {
2940 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2941 /* move to Follower */
2943 list_del(&y->node_list);
2944 list_add_tail(&y->node_list, &Y->Follower);
2947 segregate_def_use_chain(y->node);
2957 * Get the leader for a given node from its congruence class.
2959 * @param irn the node
2961 static ir_node *get_leader(node_t *node)
2963 partition_t *part = node->part;
2965 if (part->n_leader > 1 || node->is_follower) {
2966 if (node->is_follower) {
2967 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2970 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2972 return get_first_node(part)->node;
2978 * Returns non-zero if a mode_T node has only one reachable output.
2980 static int only_one_reachable_proj(ir_node *n)
2984 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2985 ir_node *proj = get_irn_out(n, i);
2988 /* skip non-control flow Proj's */
2989 if (get_irn_mode(proj) != mode_X)
2992 node = get_irn_node(proj);
2993 if (node->type.tv == tarval_reachable) {
2999 } /* only_one_reachable_proj */
3002 * Return non-zero if the control flow predecessor node pred
3003 * is the only reachable control flow exit of its block.
3005 * @param pred the control flow exit
3006 * @param block the destination block
3008 static int can_exchange(ir_node *pred, ir_node *block)
3010 if (is_Start(pred) || has_Block_entity(block))
3012 else if (is_Jmp(pred))
3014 else if (get_irn_mode(pred) == mode_T) {
3015 /* if the predecessor block has more than one
3016 reachable outputs we cannot remove the block */
3017 return only_one_reachable_proj(pred);
3020 } /* can_exchange */
3023 * Block Post-Walker, apply the analysis results on control flow by
3024 * shortening Phi's and Block inputs.
3026 static void apply_cf(ir_node *block, void *ctx)
3028 environment_t *env = (environment_t*)ctx;
3029 node_t *node = get_irn_node(block);
3031 ir_node **ins, **in_X;
3032 ir_node *phi, *next;
3034 n = get_Block_n_cfgpreds(block);
3036 if (node->type.tv == tarval_unreachable) {
3039 for (i = n - 1; i >= 0; --i) {
3040 ir_node *pred = get_Block_cfgpred(block, i);
3042 if (! is_Bad(pred)) {
3043 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3045 if (pred_bl->flagged == 0) {
3046 pred_bl->flagged = 3;
3048 if (pred_bl->type.tv == tarval_reachable) {
3050 * We will remove an edge from block to its pred.
3051 * This might leave the pred block as an endless loop
3053 if (! is_backedge(block, i))
3054 keep_alive(pred_bl->node);
3060 /* the EndBlock is always reachable even if the analysis
3061 finds out the opposite :-) */
3062 if (block != get_irg_end_block(current_ir_graph)) {
3063 /* mark dead blocks */
3064 set_Block_dead(block);
3065 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
3067 /* the endblock is unreachable */
3068 set_irn_in(block, 0, NULL);
3074 /* only one predecessor combine */
3075 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3077 if (can_exchange(pred, block)) {
3078 ir_node *new_block = get_nodes_block(pred);
3079 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3080 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3081 exchange(block, new_block);
3082 node->node = new_block;
3088 NEW_ARR_A(ir_node *, in_X, n);
3090 for (i = 0; i < n; ++i) {
3091 ir_node *pred = get_Block_cfgpred(block, i);
3092 node_t *node = get_irn_node(pred);
3094 if (node->type.tv == tarval_reachable) {
3097 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3098 if (! is_Bad(pred)) {
3099 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3101 if (pred_bl->flagged == 0) {
3102 pred_bl->flagged = 3;
3104 if (pred_bl->type.tv == tarval_reachable) {
3106 * We will remove an edge from block to its pred.
3107 * This might leave the pred block as an endless loop
3109 if (! is_backedge(block, i))
3110 keep_alive(pred_bl->node);
3120 NEW_ARR_A(ir_node *, ins, n);
3121 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3122 node_t *node = get_irn_node(phi);
3124 next = get_Phi_next(phi);
3125 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3126 /* this Phi is replaced by a constant */
3127 ir_tarval *tv = node->type.tv;
3128 ir_node *c = new_r_Const(current_ir_graph, tv);
3130 set_irn_node(c, node);
3132 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3133 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3138 for (i = 0; i < n; ++i) {
3139 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3141 if (pred->type.tv == tarval_reachable) {
3142 ins[j++] = get_Phi_pred(phi, i);
3146 /* this Phi is replaced by a single predecessor */
3147 ir_node *s = ins[0];
3148 node_t *phi_node = get_irn_node(phi);
3151 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3152 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3157 set_irn_in(phi, j, ins);
3165 /* this Block has only one live predecessor */
3166 ir_node *pred = skip_Proj(in_X[0]);
3168 if (can_exchange(pred, block)) {
3169 ir_node *new_block = get_nodes_block(pred);
3170 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3171 exchange(block, new_block);
3172 node->node = new_block;
3177 set_irn_in(block, k, in_X);
3182 * Exchange a node by its leader.
3183 * Beware: in rare cases the mode might be wrong here, for instance
3184 * AddP(x, NULL) is a follower of x, but with different mode.
3187 static void exchange_leader(ir_node *irn, ir_node *leader)
3189 ir_mode *mode = get_irn_mode(irn);
3190 if (mode != get_irn_mode(leader)) {
3191 /* The conv is a no-op, so we are free to place it
3192 * either in the block of the leader OR in irn's block.
3193 * Probably placing it into leaders block might reduce
3194 * the number of Conv due to CSE. */
3195 ir_node *block = get_nodes_block(leader);
3196 dbg_info *dbg = get_irn_dbg_info(irn);
3197 ir_node *nlead = new_rd_Conv(dbg, block, leader, mode);
3199 if (nlead != leader) {
3200 /* Note: this newly create irn has no node info because
3201 * it is created after the analysis. However, this node
3202 * replaces the node irn and should not be visited again,
3203 * so set its visited count to the count of irn.
3204 * Otherwise we might visited this node more than once if
3205 * irn had more than one user.
3207 set_irn_node(nlead, NULL);
3208 set_irn_visited(nlead, get_irn_visited(irn));
3212 exchange(irn, leader);
3213 } /* exchange_leader */
3216 * Check, if all users of a mode_M node are dead. Use
3217 * the Def-Use edges for this purpose, as they still
3218 * reflect the situation.
3220 static int all_users_are_dead(const ir_node *irn)
3222 int i, n = get_irn_n_outs(irn);
3224 for (i = 1; i <= n; ++i) {
3225 const ir_node *succ = irn->out[i].use;
3226 const node_t *block = get_irn_node(get_nodes_block(succ));
3229 if (block->type.tv == tarval_unreachable) {
3230 /* block is unreachable */
3233 node = get_irn_node(succ);
3234 if (node->type.tv != tarval_top) {
3235 /* found a reachable user */
3239 /* all users are unreachable */
3241 } /* all_user_are_dead */
3244 * Walker: Find reachable mode_M nodes that have only
3245 * unreachable users. These nodes must be kept later.
3247 static void find_kept_memory(ir_node *irn, void *ctx)
3249 environment_t *env = (environment_t*)ctx;
3250 node_t *node, *block;
3252 if (get_irn_mode(irn) != mode_M)
3255 block = get_irn_node(get_nodes_block(irn));
3256 if (block->type.tv == tarval_unreachable)
3259 node = get_irn_node(irn);
3260 if (node->type.tv == tarval_top)
3263 /* ok, we found a live memory node. */
3264 if (all_users_are_dead(irn)) {
3265 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3266 ARR_APP1(ir_node *, env->kept_memory, irn);
3268 } /* find_kept_memory */
3271 * Post-Walker, apply the analysis results;
3273 static void apply_result(ir_node *irn, void *ctx)
3275 environment_t *env = (environment_t*)ctx;
3276 node_t *node = get_irn_node(irn);
3278 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3279 /* blocks already handled, do not touch the End node */
3281 node_t *block = get_irn_node(get_nodes_block(irn));
3283 if (block->type.tv == tarval_unreachable) {
3284 ir_node *bad = get_irg_bad(current_ir_graph);
3286 /* here, bad might already have a node, but this can be safely ignored
3287 as long as bad has at least ONE valid node */
3288 set_irn_node(bad, node);
3290 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3293 } else if (node->type.tv == tarval_top) {
3294 ir_mode *mode = get_irn_mode(irn);
3296 if (mode == mode_M) {
3297 /* never kill a mode_M node */
3299 ir_node *pred = get_Proj_pred(irn);
3300 node_t *pnode = get_irn_node(pred);
3302 if (pnode->type.tv == tarval_top) {
3303 /* skip the predecessor */
3304 ir_node *mem = get_memop_mem(pred);
3306 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3311 /* leave other nodes, especially PhiM */
3312 } else if (mode == mode_T) {
3313 /* Do not kill mode_T nodes, kill their Projs */
3314 } else if (! is_Unknown(irn)) {
3315 /* don't kick away Unknown's, they might be still needed */
3316 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3318 /* control flow should already be handled at apply_cf() */
3319 assert(mode != mode_X);
3321 /* see comment above */
3322 set_irn_node(unk, node);
3324 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3329 else if (get_irn_mode(irn) == mode_X) {
3332 ir_node *cond = get_Proj_pred(irn);
3334 if (is_Cond(cond)) {
3335 if (only_one_reachable_proj(cond)) {
3336 ir_node *jmp = new_r_Jmp(block->node);
3337 set_irn_node(jmp, node);
3339 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3340 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3344 node_t *sel = get_irn_node(get_Cond_selector(cond));
3345 ir_tarval *tv = sel->type.tv;
3347 if (is_tarval(tv) && tarval_is_constant(tv)) {
3348 /* The selector is a constant, but more
3349 * than one output is active: An unoptimized
3357 /* normal data node */
3358 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3359 ir_tarval *tv = node->type.tv;
3362 * Beware: never replace mode_T nodes by constants. Currently we must mark
3363 * mode_T nodes with constants, but do NOT replace them.
3365 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3366 /* can be replaced by a constant */
3367 ir_node *c = new_r_Const(current_ir_graph, tv);
3368 set_irn_node(c, node);
3370 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3371 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3372 exchange_leader(irn, c);
3375 } else if (is_entity(node->type.sym.entity_p)) {
3376 if (! is_SymConst(irn)) {
3377 /* can be replaced by a SymConst */
3378 ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3379 set_irn_node(symc, node);
3382 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3383 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3384 exchange_leader(irn, symc);
3387 } else if (is_Confirm(irn)) {
3388 /* Confirms are always follower, but do not kill them here */
3390 ir_node *leader = get_leader(node);
3392 if (leader != irn) {
3393 int non_strict_phi = 0;
3396 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3397 * as this might create non-strict programs.
3399 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3402 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3403 ir_node *pred = get_Phi_pred(irn, i);
3405 if (is_Unknown(pred)) {
3411 if (! non_strict_phi) {
3412 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3413 if (node->is_follower)
3414 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3416 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3417 exchange_leader(irn, leader);
3424 } /* apply_result */
3427 * Fix the keep-alives by deleting unreachable ones.
3429 static void apply_end(ir_node *end, environment_t *env)
3431 int i, j, n = get_End_n_keepalives(end);
3432 ir_node **in = NULL;
3435 NEW_ARR_A(ir_node *, in, n);
3437 /* fix the keep alive */
3438 for (i = j = 0; i < n; i++) {
3439 ir_node *ka = get_End_keepalive(end, i);
3440 node_t *node = get_irn_node(ka);
3443 node = get_irn_node(get_nodes_block(ka));
3445 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3449 set_End_keepalives(end, j, in);
3454 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3457 * sets the generic functions to compute.
3459 static void set_compute_functions(void)
3463 /* set the default compute function */
3464 for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
3465 ir_op *op = get_irp_opcode(i);
3466 op->ops.generic = (op_func)default_compute;
3469 /* set specific functions */
3485 } /* set_compute_functions */
3490 static void add_memory_keeps(ir_node **kept_memory, size_t len)
3492 ir_node *end = get_irg_end(current_ir_graph);
3497 ir_nodeset_init(&set);
3499 /* check, if those nodes are already kept */
3500 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3501 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3503 for (idx = 0; idx < len; ++idx) {
3504 ir_node *ka = kept_memory[idx];
3506 if (! ir_nodeset_contains(&set, ka)) {
3507 add_End_keepalive(end, ka);
3510 ir_nodeset_destroy(&set);
3511 } /* add_memory_keeps */
3513 void combo(ir_graph *irg)
3516 ir_node *initial_bl;
3518 ir_graph *rem = current_ir_graph;
3521 current_ir_graph = irg;
3523 /* register a debug mask */
3524 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3526 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3528 obstack_init(&env.obst);
3529 env.worklist = NULL;
3533 #ifdef DEBUG_libfirm
3534 env.dbg_list = NULL;
3536 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3537 env.kept_memory = NEW_ARR_F(ir_node *, 0);
3538 env.end_idx = get_opt_global_cse() ? 0 : -1;
3539 env.lambda_input = 0;
3542 /* options driving the optimization */
3543 env.commutative = 1;
3544 env.opt_unknown = 1;
3546 assure_irg_outs(irg);
3547 assure_cf_loop(irg);
3549 /* we have our own value_of function */
3550 set_value_of_func(get_node_tarval);
3552 set_compute_functions();
3553 DEBUG_ONLY(part_nr = 0);
3555 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3557 if (env.opt_unknown)
3558 tarval_UNKNOWN = tarval_top;
3560 tarval_UNKNOWN = tarval_bad;
3562 /* create the initial partition and place it on the work list */
3563 env.initial = new_partition(&env);
3564 add_to_worklist(env.initial, &env);
3565 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3567 /* set the hook: from now, every node has a partition and a type */
3568 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook));
3570 /* all nodes on the initial partition have type Top */
3571 env.initial->type_is_T_or_C = 1;
3573 /* Place the START Node's partition on cprop.
3574 Place the START Node on its local worklist. */
3575 initial_bl = get_irg_start_block(irg);
3576 start = get_irn_node(initial_bl);
3577 add_to_cprop(start, &env);
3581 if (env.worklist != NULL)
3583 } while (env.cprop != NULL || env.worklist != NULL);
3585 dump_all_partitions(&env);
3586 check_all_partitions(&env);
3589 dump_ir_block_graph(irg, "-partition");
3592 /* apply the result */
3594 /* check, which nodes must be kept */
3595 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3597 /* kill unreachable control flow */
3598 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3599 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3600 * and fixes assertion because dead cf to dead blocks is NOT removed by
3602 apply_end(get_irg_end(irg), &env);
3603 irg_walk_graph(irg, NULL, apply_result, &env);
3605 len = ARR_LEN(env.kept_memory);
3607 add_memory_keeps(env.kept_memory, len);
3610 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3614 /* control flow might changed */
3615 set_irg_outs_inconsistent(irg);
3616 set_irg_extblk_inconsistent(irg);
3617 set_irg_doms_inconsistent(irg);
3618 set_irg_loopinfo_inconsistent(irg);
3619 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
3622 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3624 /* remove the partition hook */
3625 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
3627 DEL_ARR_F(env.kept_memory);
3628 del_set(env.opcode2id_map);
3629 obstack_free(&env.obst, NULL);
3631 /* restore value_of() default behavior */
3632 set_value_of_func(NULL);
3633 current_ir_graph = rem;
3636 /* Creates an ir_graph pass for combo. */
3637 ir_graph_pass_t *combo_pass(const char *name)
3639 return def_graph_pass(name ? name : "combo", combo);