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
25 * This is a slightly enhanced version of Cliff Clicks combo algorithm
26 * - support for commutative nodes is added, Add(a,b) and Add(b,a) ARE congruent
27 * - supports all Firm direct (by a data edge) identities except Mux
28 * (Mux can be a 2-input or 1-input identity, only 2-input is implemented yet)
29 * - supports Confirm nodes (handle them like Copies but do NOT remove them)
30 * - let Cmp nodes calculate Top like all other data nodes: this would let
31 * Mux nodes to calculate Unknown instead of taking the true result
32 * - let Cond(Top) always select FALSE/default: This is tricky. Nodes are only reevaluated
33 * IFF the predecessor changed its type. Because nodes are initialized with Top
34 * this never happens, let all Proj(Cond) be unreachable.
35 * We avoid this condition by the same way we work around Phi: whenever a Block
36 * node is placed on the list, place its Cond nodes (and because they are Tuple
37 * all its Proj-nodes either on the cprop list)
38 * Especially, this changes the meaning of Click's example:
53 * using Click's version while is silent with our.
54 * - support for global congruences is implemented but not tested yet
56 * Note further that we use the terminology from Click's work here, which is different
57 * in some cases from Firm terminology. Especially, Click's type is a
58 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
64 #include "iroptimize.h"
71 #include "irgraph_t.h"
78 #include "iropt_dbg.h"
82 #include "irnodeset.h"
86 #include "firmstat_t.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 unsigned next_edge; /**< Index of the next Def-Use edge to use. */
149 unsigned 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 know 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)
273 list_for_each_entry(node_t, node, &T->Leader, node_list) {
274 assert(node->is_follower == 0);
275 assert(node->flagged == 0);
276 assert(node->part == T);
279 assert(n == T->n_leader);
281 list_for_each_entry(node_t, node, &T->Follower, node_list) {
282 assert(node->is_follower == 1);
283 assert(node->flagged == 0);
284 assert(node->part == T);
286 } /* check_partition */
289 * check that all leader nodes in the partition have the same opcode.
291 static void check_opcode(const partition_t *Z)
293 const ir_node *repr = NULL;
295 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
296 ir_node *irn = node->node;
301 assert(cmp_irn_opcode(repr, irn) == 0);
306 static void check_all_partitions(environment_t *env)
311 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
313 if (! P->type_is_T_or_C)
315 list_for_each_entry(node_t, node, &P->Follower, node_list) {
316 node_t *leader = identity(node);
318 assert(leader != node && leader->part == node->part);
329 static void do_check_list(const node_t *list, int ofs, const partition_t *Z)
334 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
335 for (e = list; e != NULL; e = NEXT(e)) {
336 assert(e->part == Z);
344 } /* ido_check_list */
347 * Check a local list.
349 static void check_list(const node_t *list, const partition_t *Z)
351 do_check_list(list, offsetof(node_t, next), Z);
355 #define check_partition(T)
356 #define check_list(list, Z)
357 #define check_all_partitions(env)
358 #endif /* CHECK_PARTITIONS */
361 static inline lattice_elem_t get_partition_type(const partition_t *X);
364 * Dump partition to output.
366 static void dump_partition(const char *msg, const partition_t *part)
369 lattice_elem_t type = get_partition_type(part);
371 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
372 msg, part->nr, part->type_is_T_or_C ? "*" : "",
373 part->n_leader, type));
374 list_for_each_entry(node_t, node, &part->Leader, node_list) {
375 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
378 if (! list_empty(&part->Follower)) {
379 DB((dbg, LEVEL_2, "\n---\n "));
381 list_for_each_entry(node_t, node, &part->Follower, node_list) {
382 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
386 DB((dbg, LEVEL_2, "\n}\n"));
387 } /* dump_partition */
392 static void do_dump_list(const char *msg, const node_t *node, int ofs)
397 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
399 DB((dbg, LEVEL_3, "%s = {\n ", msg));
400 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
401 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
404 DB((dbg, LEVEL_3, "\n}\n"));
412 static void dump_race_list(const char *msg, const node_t *list)
414 do_dump_list(msg, list, offsetof(node_t, race_next));
415 } /* dump_race_list */
418 * Dumps a local list.
420 static void dump_list(const char *msg, const node_t *list)
422 do_dump_list(msg, list, offsetof(node_t, next));
426 * Dump all partitions.
428 static void dump_all_partitions(const environment_t *env)
430 const partition_t *P;
432 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
433 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
434 dump_partition("", P);
435 } /* dump_all_partitions */
440 static void dump_split_list(const partition_t *list)
442 const partition_t *p;
445 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
446 for (p = list; p != NULL; p = p->split_next) {
447 DB((dbg, LEVEL_2, "%c part%u", split, p->nr));
450 DB((dbg, LEVEL_2, "\n}\n"));
451 } /* dump_split_list */
454 * Dump partition and type for a node.
456 static int dump_partition_hook(FILE *F, const ir_node *n, const ir_node *local)
458 const ir_node *irn = local != NULL ? local : n;
459 node_t *node = get_irn_node(irn);
461 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
463 } /* dump_partition_hook */
466 #define dump_partition(msg, part)
467 #define dump_race_list(msg, list)
468 #define dump_list(msg, list)
469 #define dump_all_partitions(env)
470 #define dump_split_list(list)
473 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
475 * Verify that a type transition is monotone
477 static void verify_type(const lattice_elem_t old_type, node_t *node)
479 if (old_type.tv == node->type.tv) {
483 if (old_type.tv == tarval_top) {
484 /* from Top down-to is always allowed */
487 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
491 panic("wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
495 #define verify_type(old_type, node)
499 * Compare two pointer values of a listmap.
501 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
503 const listmap_entry_t *e1 = (listmap_entry_t*)elt;
504 const listmap_entry_t *e2 = (listmap_entry_t*)key;
507 return e1->id != e2->id;
508 } /* listmap_cmp_ptr */
511 * Initializes a listmap.
513 * @param map the listmap
515 static void listmap_init(listmap_t *map)
517 map->map = new_set(listmap_cmp_ptr, 16);
522 * Terminates a listmap.
524 * @param map the listmap
526 static void listmap_term(listmap_t *map)
532 * Return the associated listmap entry for a given id.
534 * @param map the listmap
535 * @param id the id to search for
537 * @return the associated listmap entry for the given id
539 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
541 listmap_entry_t key, *entry;
546 entry = set_insert(listmap_entry_t, map->map, &key, sizeof(key), hash_ptr(id));
548 if (entry->list == NULL) {
549 /* a new entry, put into the list */
550 entry->next = map->values;
557 * Calculate the hash value for an opcode map entry.
559 * @param entry an opcode map entry
561 * @return a hash value for the given opcode map entry
563 static unsigned opcode_hash(const opcode_key_t *entry)
565 /* we cannot use the ir ops hash function here, because it hashes the
567 const ir_node *n = entry->irn;
568 ir_opcode code = (ir_opcode)get_irn_opcode(n);
569 ir_mode *mode = get_irn_mode(n);
570 unsigned hash = (unsigned)(PTR_TO_INT(mode) * 9 + code) + get_irn_arity(n);
572 if (code == iro_Const)
573 hash ^= (unsigned)hash_ptr(get_Const_tarval(n));
574 else if (code == iro_Proj)
575 hash += (unsigned)get_Proj_proj(n);
580 * Compare two entries in the opcode map.
582 static int cmp_opcode(const void *elt, const void *key, size_t size)
584 const opcode_key_t *o1 = (opcode_key_t*)elt;
585 const opcode_key_t *o2 = (opcode_key_t*)key;
589 return cmp_irn_opcode(o1->irn, o2->irn);
593 * Compare two Def-Use edges for input position.
595 static int cmp_def_use_edge(const void *a, const void *b)
597 const ir_def_use_edge *ea = (const ir_def_use_edge*)a;
598 const ir_def_use_edge *eb = (const ir_def_use_edge*)b;
600 /* no overrun, because range is [-1, MAXINT] */
601 return ea->pos - eb->pos;
602 } /* cmp_def_use_edge */
605 * We need the Def-Use edges sorted.
607 static void sort_irn_outs(node_t *node)
609 ir_node *irn = node->node;
610 unsigned n_outs = get_irn_n_outs(irn);
611 qsort(irn->o.out->edges, n_outs, sizeof(irn->o.out->edges[0]),
613 node->max_user_input = n_outs > 0 ? irn->o.out->edges[n_outs-1].pos : -1;
614 } /* sort_irn_outs */
617 * Return the type of a node.
619 * @param irn an IR-node
621 * @return the associated type of this node
623 static inline lattice_elem_t get_node_type(const ir_node *irn)
625 return get_irn_node(irn)->type;
626 } /* get_node_type */
629 * Return the tarval of a node.
631 * @param irn an IR-node
633 * @return the associated type of this node
635 static inline ir_tarval *get_node_tarval(const ir_node *irn)
637 lattice_elem_t type = get_node_type(irn);
639 if (is_tarval(type.tv))
641 return tarval_bottom;
642 } /* get_node_type */
645 * Add a partition to the worklist.
647 static inline void add_to_worklist(partition_t *X, environment_t *env)
649 assert(X->on_worklist == 0);
650 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
651 X->wl_next = env->worklist;
654 } /* add_to_worklist */
657 * Create a new empty partition.
659 * @param env the environment
661 * @return a newly allocated partition
663 static inline partition_t *new_partition(environment_t *env)
665 partition_t *part = OALLOC(&env->obst, partition_t);
667 INIT_LIST_HEAD(&part->Leader);
668 INIT_LIST_HEAD(&part->Follower);
669 INIT_LIST_HEAD(&part->cprop);
670 INIT_LIST_HEAD(&part->cprop_X);
671 part->wl_next = NULL;
672 part->touched_next = NULL;
673 part->cprop_next = NULL;
674 part->split_next = NULL;
675 part->touched = NULL;
678 part->max_user_inputs = 0;
679 part->on_worklist = 0;
680 part->on_touched = 0;
682 part->type_is_T_or_C = 0;
684 part->dbg_next = env->dbg_list;
685 env->dbg_list = part;
686 part->nr = part_nr++;
690 } /* new_partition */
693 * Get the first node from a partition.
695 static inline node_t *get_first_node(const partition_t *X)
697 return list_entry(X->Leader.next, node_t, node_list);
698 } /* get_first_node */
701 * Return the type of a partition (assuming partition is non-empty and
702 * all elements have the same type).
704 * @param X a partition
706 * @return the type of the first element of the partition
708 static inline lattice_elem_t get_partition_type(const partition_t *X)
710 const node_t *first = get_first_node(X);
712 } /* get_partition_type */
715 * Creates a partition node for the given IR-node and place it
716 * into the given partition.
718 * @param irn an IR-node
719 * @param part a partition to place the node in
720 * @param env the environment
722 * @return the created node
724 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env)
726 /* create a partition node and place it in the partition */
727 node_t *node = OALLOC(&env->obst, node_t);
729 INIT_LIST_HEAD(&node->node_list);
730 INIT_LIST_HEAD(&node->cprop_list);
734 node->race_next = NULL;
735 node->type.tv = tarval_top;
736 node->max_user_input = 0;
738 node->n_followers = 0;
739 node->on_touched = 0;
742 node->is_follower = 0;
744 set_irn_node(irn, node);
746 list_add_tail(&node->node_list, &part->Leader);
750 } /* create_partition_node */
753 * Pre-Walker, initialize all Nodes' type to U or top and place
754 * all nodes into the TOP partition.
756 static void create_initial_partitions(ir_node *irn, void *ctx)
758 environment_t *env = (environment_t*)ctx;
759 partition_t *part = env->initial;
762 node = create_partition_node(irn, part, env);
764 if (node->max_user_input > part->max_user_inputs)
765 part->max_user_inputs = node->max_user_input;
768 set_Block_phis(irn, NULL);
770 } /* create_initial_partitions */
773 * Post-Walker, collect all Block-Phi lists, set Cond.
775 static void init_block_phis(ir_node *irn, void *ctx)
780 ir_node *block = get_nodes_block(irn);
781 add_Block_phi(block, irn);
783 } /* init_block_phis */
786 * Add a node to the entry.partition.touched set and
787 * node->partition to the touched set if not already there.
790 * @param env the environment
792 static inline void add_to_touched(node_t *y, environment_t *env)
794 if (y->on_touched == 0) {
795 partition_t *part = y->part;
797 y->next = part->touched;
802 if (part->on_touched == 0) {
803 part->touched_next = env->touched;
805 part->on_touched = 1;
808 check_list(part->touched, part);
810 } /* add_to_touched */
813 * Place a node on the cprop list.
816 * @param env the environment
818 static void add_to_cprop(node_t *y, environment_t *env)
822 /* Add y to y.partition.cprop. */
823 if (y->on_cprop == 0) {
824 partition_t *Y = y->part;
825 ir_node *irn = y->node;
826 ir_node *skipped = skip_Proj(irn);
828 /* place Conds and all its Projs on the cprop_X list */
829 if (is_Cond(skipped) || is_Switch(skipped))
830 list_add_tail(&y->cprop_list, &Y->cprop_X);
832 list_add_tail(&y->cprop_list, &Y->cprop);
835 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
837 /* place its partition on the cprop list */
838 if (Y->on_cprop == 0) {
839 Y->cprop_next = env->cprop;
845 if (get_irn_mode(irn) == mode_T) {
846 /* mode_T nodes always produce tarval_bottom, so we must explicitly
847 * add its Projs to get constant evaluation to work */
848 for (unsigned i = get_irn_n_outs(irn); i-- > 0; ) {
849 node_t *proj = get_irn_node(get_irn_out(irn, i));
851 add_to_cprop(proj, env);
853 } else if (is_Block(irn)) {
854 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
855 * if someone placed the block. The Block is only placed if the reachability
856 * changes, and this must be re-evaluated in compute_Phi(). */
858 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
859 node_t *p = get_irn_node(phi);
860 add_to_cprop(p, env);
866 * Update the worklist: If Z is on worklist then add Z' to worklist.
867 * Else add the smaller of Z and Z' to worklist.
869 * @param Z the Z partition
870 * @param Z_prime the Z' partition, a previous part of Z
871 * @param env the environment
873 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env)
875 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
876 add_to_worklist(Z_prime, env);
878 add_to_worklist(Z, env);
880 } /* update_worklist */
883 * Make all inputs to x no longer be F.def_use edges.
887 static void move_edges_to_leader(node_t *x)
889 ir_node *irn = x->node;
890 for (int i = get_irn_arity(irn) - 1; i >= 0; --i) {
891 node_t *pred = get_irn_node(get_irn_n(irn, i));
892 ir_node *p = pred->node;
893 unsigned n = get_irn_n_outs(p);
894 for (unsigned j = 0; j < pred->n_followers; ++j) {
895 ir_def_use_edge edge = p->o.out->edges[j];
896 if (edge.pos == i && edge.use == irn) {
897 /* found a follower edge to x, move it to the Leader */
898 /* remove this edge from the Follower set */
900 p->o.out->edges[j] = p->o.out->edges[pred->n_followers];
902 /* sort it into the leader set */
904 for (k = pred->n_followers+1; k < n; ++k) {
905 if (p->o.out->edges[k].pos >= edge.pos)
907 p->o.out->edges[k-1] = p->o.out->edges[k];
909 /* place the new edge here */
910 p->o.out->edges[k-1] = edge;
912 /* edge found and moved */
917 } /* move_edges_to_leader */
920 * Split a partition that has NO followers by a local list.
922 * @param Z partition to split
923 * @param g a (non-empty) node list
924 * @param env the environment
926 * @return a new partition containing the nodes of g
928 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env)
930 partition_t *Z_prime;
935 dump_partition("Splitting ", Z);
936 dump_list("by list ", g);
940 /* Remove g from Z. */
941 for (node = g; node != NULL; node = node->next) {
942 assert(node->part == Z);
943 list_del(&node->node_list);
946 assert(n < Z->n_leader);
949 /* Move g to a new partition, Z'. */
950 Z_prime = new_partition(env);
952 for (node = g; node != NULL; node = node->next) {
953 list_add_tail(&node->node_list, &Z_prime->Leader);
954 node->part = Z_prime;
955 if (node->max_user_input > max_input)
956 max_input = node->max_user_input;
958 Z_prime->max_user_inputs = max_input;
959 Z_prime->n_leader = n;
962 check_partition(Z_prime);
964 /* for now, copy the type info tag, it will be adjusted in split_by(). */
965 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
967 dump_partition("Now ", Z);
968 dump_partition("Created new ", Z_prime);
970 update_worklist(Z, Z_prime, env);
973 } /* split_no_followers */
976 * Make the Follower -> Leader transition for a node.
980 static void follower_to_leader(node_t *n)
982 assert(n->is_follower == 1);
984 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
986 move_edges_to_leader(n);
987 list_del(&n->node_list);
988 list_add_tail(&n->node_list, &n->part->Leader);
990 } /* follower_to_leader */
993 * The environment for one race step.
995 typedef struct step_env {
996 node_t *initial; /**< The initial node list. */
997 node_t *unwalked; /**< The unwalked node list. */
998 node_t *walked; /**< The walked node list. */
999 unsigned index; /**< Next index of Follower use_def edge. */
1000 unsigned side; /**< side number. */
1004 * Return non-zero, if a input is a real follower
1006 * @param irn the node to check
1007 * @param input number of the input
1009 static int is_real_follower(const ir_node *irn, int input)
1013 switch (get_irn_opcode(irn)) {
1016 /* ignore the Confirm bound input */
1022 /* ignore the Mux sel input */
1027 /* dead inputs are not follower edges */
1028 ir_node *block = get_nodes_block(irn);
1029 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
1031 if (pred->type.tv == tarval_unreachable)
1041 /* only a Sub x,0 / Shift x,0 might be a follower */
1048 pred = get_irn_node(get_irn_n(irn, input));
1049 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1053 pred = get_irn_node(get_irn_n(irn, input));
1054 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1058 pred = get_irn_node(get_irn_n(irn, input));
1059 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1063 assert(!"opcode not implemented yet");
1067 } /* is_real_follower */
1070 * Do one step in the race.
1072 static int step(step_env *env)
1076 if (env->initial != NULL) {
1077 /* Move node from initial to unwalked */
1079 env->initial = n->race_next;
1081 n->race_next = env->unwalked;
1087 while (env->unwalked != NULL) {
1088 /* let n be the first node in unwalked */
1090 while (env->index < n->n_followers) {
1091 const ir_def_use_edge *edge = &n->node->o.out->edges[env->index];
1093 /* let m be n.F.def_use[index] */
1094 node_t *m = get_irn_node(edge->use);
1096 assert(m->is_follower);
1098 * Some inputs, like the get_Confirm_bound are NOT
1099 * real followers, sort them out.
1101 if (! is_real_follower(m->node, edge->pos)) {
1107 /* only followers from our partition */
1108 if (m->part != n->part)
1111 if ((m->flagged & env->side) == 0) {
1112 m->flagged |= env->side;
1114 if (m->flagged != 3) {
1115 /* visited the first time */
1116 /* add m to unwalked not as first node (we might still need to
1117 check for more follower node */
1118 m->race_next = n->race_next;
1122 /* else already visited by the other side and on the other list */
1125 /* move n to walked */
1126 env->unwalked = n->race_next;
1127 n->race_next = env->walked;
1135 * Clear the flags from a list and check for
1136 * nodes that where touched from both sides.
1138 * @param list the list
1140 static int clear_flags(node_t *list)
1145 for (n = list; n != NULL; n = n->race_next) {
1146 if (n->flagged == 3) {
1147 /* we reach a follower from both sides, this will split congruent
1148 * inputs and make it a leader. */
1149 follower_to_leader(n);
1158 * Split a partition by a local list using the race.
1160 * @param pX pointer to the partition to split, might be changed!
1161 * @param gg a (non-empty) node list
1162 * @param env the environment
1164 * @return a new partition containing the nodes of gg
1166 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env)
1168 partition_t *X = *pX;
1169 partition_t *X_prime;
1173 int max_input, transitions, winner, shf;
1175 DEBUG_ONLY(static int run = 0;)
1177 DB((dbg, LEVEL_2, "Run %d ", run++));
1178 if (list_empty(&X->Follower)) {
1179 /* if the partition has NO follower, we can use the fast
1180 splitting algorithm. */
1181 return split_no_followers(X, gg, env);
1183 /* else do the race */
1185 dump_partition("Splitting ", X);
1186 dump_list("by list ", gg);
1188 INIT_LIST_HEAD(&tmp);
1190 /* Remove gg from X.Leader and put into g */
1192 for (node_t *node = gg; node != NULL; node = node->next) {
1193 assert(node->part == X);
1194 assert(node->is_follower == 0);
1196 list_del(&node->node_list);
1197 list_add_tail(&node->node_list, &tmp);
1198 node->race_next = g;
1203 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1204 node->race_next = h;
1207 /* restore X.Leader */
1208 list_splice(&tmp, &X->Leader);
1210 senv[0].initial = g;
1211 senv[0].unwalked = NULL;
1212 senv[0].walked = NULL;
1216 senv[1].initial = h;
1217 senv[1].unwalked = NULL;
1218 senv[1].walked = NULL;
1223 * Some informations on the race that are not stated clearly in Click's
1225 * 1) A follower stays on the side that reach him first.
1226 * 2) If the other side reaches a follower, if will be converted to
1227 * a leader. /This must be done after the race is over, else the
1228 * edges we are iterating on are renumbered./
1229 * 3) /New leader might end up on both sides./
1230 * 4) /If one side ends up with new Leaders, we must ensure that
1231 * they can split out by opcode, hence we have to put _every_
1232 * partition with new Leader nodes on the cprop list, as
1233 * opcode splitting is done by split_by() at the end of
1234 * constant propagation./
1237 if (step(&senv[0])) {
1241 if (step(&senv[1])) {
1246 assert(senv[winner].initial == NULL);
1247 assert(senv[winner].unwalked == NULL);
1249 /* clear flags from walked/unwalked */
1251 transitions = clear_flags(senv[0].unwalked) << shf;
1252 transitions |= clear_flags(senv[0].walked) << shf;
1254 transitions |= clear_flags(senv[1].unwalked) << shf;
1255 transitions |= clear_flags(senv[1].walked) << shf;
1257 dump_race_list("winner ", senv[winner].walked);
1259 /* Move walked_{winner} to a new partition, X'. */
1260 X_prime = new_partition(env);
1263 for (node_t *node = senv[winner].walked; node != NULL; node = node->race_next) {
1264 list_del(&node->node_list);
1265 node->part = X_prime;
1266 if (node->is_follower) {
1267 list_add_tail(&node->node_list, &X_prime->Follower);
1269 list_add_tail(&node->node_list, &X_prime->Leader);
1272 if (node->max_user_input > max_input)
1273 max_input = node->max_user_input;
1275 X_prime->n_leader = n;
1276 X_prime->max_user_inputs = max_input;
1277 X->n_leader -= X_prime->n_leader;
1279 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1280 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1283 * Even if a follower was not checked by both sides, it might have
1284 * loose its congruence, so we need to check this case for all follower.
1286 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1287 if (identity(node) == node) {
1288 follower_to_leader(node);
1294 check_partition(X_prime);
1296 dump_partition("Now ", X);
1297 dump_partition("Created new ", X_prime);
1299 /* X' is the smaller part */
1300 add_to_worklist(X_prime, env);
1303 * If there where follower to leader transitions, ensure that the nodes
1304 * can be split out if necessary.
1306 if (transitions & 1) {
1307 /* place winner partition on the cprop list */
1308 if (X_prime->on_cprop == 0) {
1309 X_prime->cprop_next = env->cprop;
1310 env->cprop = X_prime;
1311 X_prime->on_cprop = 1;
1314 if (transitions & 2) {
1315 /* place other partition on the cprop list */
1316 if (X->on_cprop == 0) {
1317 X->cprop_next = env->cprop;
1323 /* we have to ensure that the partition containing g is returned */
1333 * Returns non-zero if the i'th input of a Phi node is live.
1335 * @param phi a Phi-node
1336 * @param i an input number
1338 * @return non-zero if the i'th input of the given Phi node is live
1340 static int is_live_input(ir_node *phi, int i)
1343 ir_node *block = get_nodes_block(phi);
1344 ir_node *pred = get_Block_cfgpred(block, i);
1345 lattice_elem_t type = get_node_type(pred);
1347 return type.tv != tarval_unreachable;
1349 /* else it's the control input, always live */
1351 } /* is_live_input */
1354 * Return non-zero if a type is a constant.
1356 static int is_constant_type(lattice_elem_t type)
1358 if (type.tv != tarval_bottom && type.tv != tarval_top)
1361 } /* is_constant_type */
1364 * Check whether a type is neither Top or a constant.
1365 * Note: U is handled like Top here, R is a constant.
1367 * @param type the type to check
1369 static int type_is_neither_top_nor_const(const lattice_elem_t type)
1371 if (is_tarval(type.tv)) {
1372 if (type.tv == tarval_top)
1374 if (tarval_is_constant(type.tv))
1381 } /* type_is_neither_top_nor_const */
1384 * Collect nodes to the touched list.
1386 * @param list the list which contains the nodes that must be evaluated
1387 * @param idx the index of the def_use edge to evaluate
1388 * @param env the environment
1390 static void collect_touched(list_head *list, int idx, environment_t *env)
1393 int end_idx = env->end_idx;
1395 list_for_each_entry(node_t, x, list, node_list) {
1397 /* leader edges start AFTER follower edges */
1398 x->next_edge = x->n_followers;
1400 unsigned num_edges = get_irn_n_outs(x->node);
1402 /* for all edges in x.L.def_use_{idx} */
1403 while (x->next_edge < num_edges) {
1404 const ir_def_use_edge *edge = &x->node->o.out->edges[x->next_edge];
1407 /* check if we have necessary edges */
1408 if (edge->pos > idx)
1415 /* only non-commutative nodes */
1416 if (env->commutative &&
1417 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1420 /* ignore the "control input" for non-pinned nodes
1421 if we are running in GCSE mode */
1422 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1425 y = get_irn_node(succ);
1426 assert(get_irn_n(succ, idx) == x->node);
1428 /* ignore block edges touching followers */
1429 if (idx == -1 && y->is_follower)
1432 if (is_constant_type(y->type)) {
1433 unsigned code = get_irn_opcode(succ);
1434 if (code == iro_Sub || code == iro_Cmp)
1435 add_to_cprop(y, env);
1438 /* Partitions of constants should not be split simply because their Nodes have unequal
1439 functions or incongruent inputs. */
1440 if (type_is_neither_top_nor_const(y->type) &&
1441 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1442 add_to_touched(y, env);
1446 } /* collect_touched */
1449 * Collect commutative nodes to the touched list.
1451 * @param list the list which contains the nodes that must be evaluated
1452 * @param env the environment
1454 static void collect_commutative_touched(list_head *list, environment_t *env)
1458 list_for_each_entry(node_t, x, list, node_list) {
1459 unsigned num_edges = get_irn_n_outs(x->node);
1461 x->next_edge = x->n_followers;
1463 /* for all edges in x.L.def_use_{idx} */
1464 while (x->next_edge < num_edges) {
1465 const ir_def_use_edge *edge = &x->node->o.out->edges[x->next_edge];
1468 /* check if we have necessary edges */
1478 /* only commutative nodes */
1479 if (!is_op_commutative(get_irn_op(succ)))
1482 y = get_irn_node(succ);
1483 if (is_constant_type(y->type)) {
1484 unsigned code = get_irn_opcode(succ);
1485 if (code == iro_Eor)
1486 add_to_cprop(y, env);
1489 /* Partitions of constants should not be split simply because their Nodes have unequal
1490 functions or incongruent inputs. */
1491 if (type_is_neither_top_nor_const(y->type)) {
1492 add_to_touched(y, env);
1496 } /* collect_commutative_touched */
1499 * Split the partitions if caused by the first entry on the worklist.
1501 * @param env the environment
1503 static void cause_splits(environment_t *env)
1505 partition_t *X, *Z, *N;
1508 /* remove the first partition from the worklist */
1510 env->worklist = X->wl_next;
1513 dump_partition("Cause_split: ", X);
1515 if (env->commutative) {
1516 /* handle commutative nodes first */
1518 /* empty the touched set: already done, just clear the list */
1519 env->touched = NULL;
1521 collect_commutative_touched(&X->Leader, env);
1522 collect_commutative_touched(&X->Follower, env);
1524 for (Z = env->touched; Z != NULL; Z = N) {
1526 node_t *touched = Z->touched;
1527 node_t *touched_aa = NULL;
1528 node_t *touched_ab = NULL;
1529 unsigned n_touched_aa = 0;
1530 unsigned n_touched_ab = 0;
1532 assert(Z->touched != NULL);
1534 /* beware, split might change Z */
1535 N = Z->touched_next;
1537 /* remove it from the touched set */
1540 /* Empty local Z.touched. */
1541 for (e = touched; e != NULL; e = n) {
1542 node_t *left = get_irn_node(get_irn_n(e->node, 0));
1543 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1545 assert(e->is_follower == 0);
1550 * Note: op(a, a) is NOT congruent to op(a, b).
1551 * So, we must split the touched list.
1553 if (left->part == right->part) {
1554 e->next = touched_aa;
1558 e->next = touched_ab;
1563 assert(n_touched_aa + n_touched_ab == Z->n_touched);
1567 if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1568 partition_t *Z_prime = Z;
1569 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1570 split(&Z_prime, touched_aa, env);
1572 assert(n_touched_aa <= Z->n_leader);
1574 if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1575 partition_t *Z_prime = Z;
1576 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1577 split(&Z_prime, touched_ab, env);
1579 assert(n_touched_ab <= Z->n_leader);
1583 /* combine temporary leader and follower list */
1584 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1585 /* empty the touched set: already done, just clear the list */
1586 env->touched = NULL;
1588 collect_touched(&X->Leader, idx, env);
1589 collect_touched(&X->Follower, idx, env);
1591 for (Z = env->touched; Z != NULL; Z = N) {
1593 node_t *touched = Z->touched;
1594 unsigned n_touched = Z->n_touched;
1596 assert(Z->touched != NULL);
1598 /* beware, split might change Z */
1599 N = Z->touched_next;
1601 /* remove it from the touched set */
1604 /* Empty local Z.touched. */
1605 for (e = touched; e != NULL; e = e->next) {
1606 assert(e->is_follower == 0);
1612 if (0 < n_touched && n_touched < Z->n_leader) {
1613 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1614 split(&Z, touched, env);
1616 assert(n_touched <= Z->n_leader);
1619 } /* cause_splits */
1622 * Implements split_by_what(): Split a partition by characteristics given
1623 * by the what function.
1625 * @param X the partition to split
1626 * @param What a function returning an Id for every node of the partition X
1627 * @param P a list to store the result partitions
1628 * @param env the environment
1632 static partition_t *split_by_what(partition_t *X, what_func What,
1633 partition_t **P, environment_t *env)
1637 listmap_entry_t *iter;
1640 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1642 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1643 void *id = What(x, env);
1644 listmap_entry_t *entry;
1647 /* input not allowed, ignore */
1650 /* Add x to map[What(x)]. */
1651 entry = listmap_find(&map, id);
1652 x->next = entry->list;
1655 /* Let P be a set of Partitions. */
1657 /* for all sets S except one in the range of map do */
1658 for (iter = map.values; iter != NULL; iter = iter->next) {
1659 if (iter->next == NULL) {
1660 /* this is the last entry, ignore */
1665 /* Add SPLIT( X, S ) to P. */
1666 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1667 R = split(&X, S, env);
1677 } /* split_by_what */
1679 /** lambda n.(n.type) */
1680 static void *lambda_type(const node_t *node, environment_t *env)
1683 return node->type.tv;
1686 /** lambda n.(n.opcode) */
1687 static void *lambda_opcode(const node_t *node, environment_t *env)
1689 opcode_key_t key, *entry;
1691 key.irn = node->node;
1693 entry = set_insert(opcode_key_t, env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1695 } /* lambda_opcode */
1697 /** lambda n.(n[i].partition) */
1698 static void *lambda_partition(const node_t *node, environment_t *env)
1700 ir_node *skipped = skip_Proj(node->node);
1703 int i = env->lambda_input;
1705 if (i >= get_irn_arity(node->node)) {
1707 * We are outside the allowed range: This can happen even
1708 * if we have split by opcode first: doing so might move Followers
1709 * to Leaders and those will have a different opcode!
1710 * Note that in this case the partition is on the cprop list and will be
1716 /* ignore the "control input" for non-pinned nodes
1717 if we are running in GCSE mode */
1718 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1721 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1722 p = get_irn_node(pred);
1724 } /* lambda_partition */
1726 /** lambda n.(n[i].partition) for commutative nodes */
1727 static void *lambda_commutative_partition(const node_t *node, environment_t *env)
1729 ir_node *irn = node->node;
1730 ir_node *skipped = skip_Proj(irn);
1731 ir_node *pred, *left, *right;
1733 partition_t *pl, *pr;
1734 int i = env->lambda_input;
1736 if (i >= get_irn_arity(node->node)) {
1738 * We are outside the allowed range: This can happen even
1739 * if we have split by opcode first: doing so might move Followers
1740 * to Leaders and those will have a different opcode!
1741 * Note that in this case the partition is on the cprop list and will be
1747 /* ignore the "control input" for non-pinned nodes
1748 if we are running in GCSE mode */
1749 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1753 pred = get_irn_n(skipped, i);
1754 p = get_irn_node(pred);
1758 if (is_op_commutative(get_irn_op(irn))) {
1759 /* normalize partition order by returning the "smaller" on input 0,
1760 the "bigger" on input 1. */
1761 left = get_binop_left(irn);
1762 pl = get_irn_node(left)->part;
1763 right = get_binop_right(irn);
1764 pr = get_irn_node(right)->part;
1767 return pl < pr ? pl : pr;
1769 return pl > pr ? pl : pr;
1771 /* a not split out Follower */
1772 pred = get_irn_n(irn, i);
1773 p = get_irn_node(pred);
1777 } /* lambda_commutative_partition */
1780 * Returns true if a type is a constant (and NOT Top
1783 static int is_con(const lattice_elem_t type)
1785 /* be conservative */
1786 if (is_tarval(type.tv))
1787 return tarval_is_constant(type.tv);
1788 return is_entity(type.sym.entity_p);
1792 * Implements split_by().
1794 * @param X the partition to split
1795 * @param env the environment
1797 static void split_by(partition_t *X, environment_t *env)
1799 partition_t *I, *P = NULL;
1802 dump_partition("split_by", X);
1804 if (X->n_leader == 1) {
1805 /* we have only one leader, no need to split, just check its type */
1806 node_t *x = get_first_node(X);
1807 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1811 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1812 P = split_by_what(X, lambda_type, &P, env);
1815 /* adjust the type tags, we have split partitions by type */
1816 for (I = P; I != NULL; I = I->split_next) {
1817 node_t *x = get_first_node(I);
1818 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1825 if (Y->n_leader > 1) {
1826 /* we do not want split the TOP or constant partitions */
1827 if (! Y->type_is_T_or_C) {
1828 partition_t *Q = NULL;
1830 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1831 Q = split_by_what(Y, lambda_opcode, &Q, env);
1838 if (Z->n_leader > 1) {
1839 const node_t *first = get_first_node(Z);
1840 int arity = get_irn_arity(first->node);
1842 what_func what = lambda_partition;
1843 DEBUG_ONLY(char buf[64];)
1845 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1846 what = lambda_commutative_partition;
1849 * BEWARE: during splitting by input 2 for instance we might
1850 * create new partitions which are different by input 1, so collect
1851 * them and split further.
1853 Z->split_next = NULL;
1856 for (input = arity - 1; input >= -1; --input) {
1858 partition_t *Z_prime = R;
1861 if (Z_prime->n_leader > 1) {
1862 env->lambda_input = input;
1863 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1864 DEBUG_ONLY(what_reason = buf;)
1865 S = split_by_what(Z_prime, what, &S, env);
1868 Z_prime->split_next = S;
1871 } while (R != NULL);
1876 } while (Q != NULL);
1879 } while (P != NULL);
1883 * (Re-)compute the type for a given node.
1885 * @param node the node
1887 static void default_compute(node_t *node)
1890 ir_node *irn = node->node;
1892 /* if any of the data inputs have type top, the result is type top */
1893 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1894 ir_node *pred = get_irn_n(irn, i);
1895 node_t *p = get_irn_node(pred);
1897 if (p->type.tv == tarval_top) {
1898 node->type.tv = tarval_top;
1903 if (get_irn_mode(node->node) == mode_X)
1904 node->type.tv = tarval_reachable;
1906 node->type.tv = computed_value(irn);
1907 } /* default_compute */
1910 * (Re-)compute the type for a Block node.
1912 * @param node the node
1914 static void compute_Block(node_t *node)
1917 ir_node *block = node->node;
1919 ir_graph *const irg = get_Block_irg(block);
1920 if (block == get_irg_start_block(irg) || get_Block_entity(block) != NULL) {
1921 /* start block and labelled blocks are always reachable */
1922 node->type.tv = tarval_reachable;
1926 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1927 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1929 if (pred->type.tv == tarval_reachable) {
1930 /* A block is reachable, if at least of predecessor is reachable. */
1931 node->type.tv = tarval_reachable;
1935 node->type.tv = tarval_top;
1936 } /* compute_Block */
1939 * (Re-)compute the type for a Bad node.
1941 * @param node the node
1943 static void compute_Bad(node_t *node)
1945 /* Bad nodes ALWAYS compute Top */
1946 node->type.tv = tarval_top;
1950 * (Re-)compute the type for an Unknown node.
1952 * @param node the node
1954 static void compute_Unknown(node_t *node)
1956 /* While Unknown nodes should compute Top this is dangerous:
1957 * a Top input to a Cond would lead to BOTH control flows unreachable.
1958 * While this is correct in the given semantics, it would destroy the Firm
1961 * It would be safe to compute Top IF it can be assured, that only Cmp
1962 * nodes are inputs to Conds. We check that first.
1963 * This is the way Frontends typically build Firm, but some optimizations
1964 * (jump threading for instance) might replace them by Phib's...
1966 node->type.tv = tarval_UNKNOWN;
1967 } /* compute_Unknown */
1970 * (Re-)compute the type for a Jmp node.
1972 * @param node the node
1974 static void compute_Jmp(node_t *node)
1976 node_t *block = get_irn_node(get_nodes_block(node->node));
1978 node->type = block->type;
1982 * (Re-)compute the type for the Return node.
1984 * @param node the node
1986 static void compute_Return(node_t *node)
1988 /* The Return node is NOT dead if it is in a reachable block.
1989 * This is already checked in compute(). so we can return
1990 * Reachable here. */
1991 node->type.tv = tarval_reachable;
1992 } /* compute_Return */
1995 * (Re-)compute the type for the End node.
1997 * @param node the node
1999 static void compute_End(node_t *node)
2001 /* the End node is NOT dead of course */
2002 node->type.tv = tarval_reachable;
2006 * (Re-)compute the type for a Call.
2008 * @param node the node
2010 static void compute_Call(node_t *node)
2013 * A Call computes always bottom, even if it has Unknown
2016 node->type.tv = tarval_bottom;
2017 } /* compute_Call */
2020 * (Re-)compute the type for a SymConst node.
2022 * @param node the node
2024 static void compute_SymConst(node_t *node)
2026 ir_node *irn = node->node;
2027 node_t *block = get_irn_node(get_nodes_block(irn));
2029 if (block->type.tv == tarval_unreachable) {
2030 node->type.tv = tarval_top;
2033 switch (get_SymConst_kind(irn)) {
2034 case symconst_addr_ent:
2035 node->type.sym = get_SymConst_symbol(irn);
2038 node->type.tv = computed_value(irn);
2040 } /* compute_SymConst */
2043 * (Re-)compute the type for a Phi node.
2045 * @param node the node
2047 static void compute_Phi(node_t *node)
2050 ir_node *phi = node->node;
2051 lattice_elem_t type;
2053 /* if a Phi is in a unreachable block, its type is TOP */
2054 node_t *block = get_irn_node(get_nodes_block(phi));
2056 if (block->type.tv == tarval_unreachable) {
2057 node->type.tv = tarval_top;
2061 /* Phi implements the Meet operation */
2062 type.tv = tarval_top;
2063 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2064 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2065 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2067 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2068 /* ignore TOP inputs: We must check here for unreachable blocks,
2069 because Firm constants live in the Start Block are NEVER Top.
2070 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2071 comes from a unreachable input. */
2074 if (pred->type.tv == tarval_bottom) {
2075 node->type.tv = tarval_bottom;
2077 } else if (type.tv == tarval_top) {
2078 /* first constant found */
2080 } else if (type.tv != pred->type.tv) {
2081 /* different constants or tarval_bottom */
2082 node->type.tv = tarval_bottom;
2085 /* else nothing, constants are the same */
2091 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2093 * @param node the node
2095 static void compute_Add(node_t *node)
2097 ir_node *sub = node->node;
2098 node_t *l = get_irn_node(get_Add_left(sub));
2099 node_t *r = get_irn_node(get_Add_right(sub));
2100 lattice_elem_t a = l->type;
2101 lattice_elem_t b = r->type;
2104 if (a.tv == tarval_top || b.tv == tarval_top) {
2105 node->type.tv = tarval_top;
2106 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2107 node->type.tv = tarval_bottom;
2109 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2110 must call tarval_add() first to handle this case! */
2111 if (is_tarval(a.tv)) {
2112 if (is_tarval(b.tv)) {
2113 node->type.tv = tarval_add(a.tv, b.tv);
2116 mode = get_tarval_mode(a.tv);
2117 if (a.tv == get_mode_null(mode)) {
2121 } else if (is_tarval(b.tv)) {
2122 mode = get_tarval_mode(b.tv);
2123 if (b.tv == get_mode_null(mode)) {
2128 node->type.tv = tarval_bottom;
2133 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2135 * @param node the node
2137 static void compute_Sub(node_t *node)
2139 ir_node *sub = node->node;
2140 node_t *l = get_irn_node(get_Sub_left(sub));
2141 node_t *r = get_irn_node(get_Sub_right(sub));
2142 lattice_elem_t a = l->type;
2143 lattice_elem_t b = r->type;
2146 if (a.tv == tarval_top || b.tv == tarval_top) {
2147 node->type.tv = tarval_top;
2148 } else if (is_con(a) && is_con(b)) {
2149 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2150 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2151 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2153 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2156 node->type.tv = tarval_bottom;
2158 } else if (r->part == l->part &&
2159 (!mode_is_float(get_irn_mode(l->node)))) {
2161 * BEWARE: a - a is NOT always 0 for floating Point values, as
2162 * NaN op NaN = NaN, so we must check this here.
2164 ir_mode *mode = get_irn_mode(sub);
2165 tv = get_mode_null(mode);
2167 /* if the node was ONCE evaluated by all constants, but now
2168 this breaks AND we get from the argument partitions a different
2169 result, switch to bottom.
2170 This happens because initially all nodes are in the same partition ... */
2171 if (node->type.tv != tv)
2175 node->type.tv = tarval_bottom;
2180 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2182 * @param node the node
2184 static void compute_Eor(node_t *node)
2186 ir_node *eor = node->node;
2187 node_t *l = get_irn_node(get_Eor_left(eor));
2188 node_t *r = get_irn_node(get_Eor_right(eor));
2189 lattice_elem_t a = l->type;
2190 lattice_elem_t b = r->type;
2193 if (a.tv == tarval_top || b.tv == tarval_top) {
2194 node->type.tv = tarval_top;
2195 } else if (is_con(a) && is_con(b)) {
2196 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2197 node->type.tv = tarval_eor(a.tv, b.tv);
2198 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2200 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2203 node->type.tv = tarval_bottom;
2205 } else if (r->part == l->part) {
2206 ir_mode *mode = get_irn_mode(eor);
2207 tv = get_mode_null(mode);
2209 /* if the node was ONCE evaluated by all constants, but now
2210 this breaks AND we get from the argument partitions a different
2211 result, switch to bottom.
2212 This happens because initially all nodes are in the same partition ... */
2213 if (node->type.tv != tv)
2217 node->type.tv = tarval_bottom;
2222 * (Re-)compute the type for Cmp.
2224 * @param node the node
2226 static void compute_Cmp(node_t *node)
2228 ir_node *cmp = node->node;
2229 node_t *l = get_irn_node(get_Cmp_left(cmp));
2230 node_t *r = get_irn_node(get_Cmp_right(cmp));
2231 lattice_elem_t a = l->type;
2232 lattice_elem_t b = r->type;
2233 ir_relation relation = get_Cmp_relation(cmp);
2236 if (a.tv == tarval_top || b.tv == tarval_top) {
2237 node->type.tv = tarval_undefined;
2238 } else if (is_con(a) && is_con(b)) {
2239 default_compute(node);
2242 * BEWARE: a == a is NOT always True for floating Point values, as
2243 * NaN != NaN is defined, so we must check this here.
2244 * (while for some pnc we could still optimize we have to stay
2245 * consistent with compute_Cmp, so don't do anything for floats)
2247 } else if (r->part == l->part && !mode_is_float(get_irn_mode(l->node))) {
2248 tv = relation & ir_relation_equal ? tarval_b_true : tarval_b_false;
2250 /* if the node was ONCE evaluated to a constant, but now
2251 this breaks AND we get from the argument partitions a different
2252 result, ensure monotony by fall to bottom.
2253 This happens because initially all nodes are in the same partition ... */
2254 if (node->type.tv == tarval_bottom)
2256 else if (node->type.tv != tv && is_constant_type(node->type))
2260 node->type.tv = tarval_bottom;
2265 * (Re-)compute the type for a Proj(Cond).
2267 * @param node the node
2268 * @param cond the predecessor Cond node
2270 static void compute_Proj_Cond(node_t *node, ir_node *cond)
2272 ir_node *proj = node->node;
2273 long pnc = get_Proj_proj(proj);
2274 ir_node *sel = get_Cond_selector(cond);
2275 node_t *selector = get_irn_node(sel);
2278 * Note: it is crucial for the monotony that the Proj(Cond)
2279 * are evaluates after all predecessors of the Cond selector are
2285 * Due to the fact that 0 is a const, the Cmp gets immediately
2286 * on the cprop list. It will be evaluated before x is evaluated,
2287 * might leaving x as Top. When later x is evaluated, the Cmp
2288 * might change its value.
2289 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2290 * gets R, and later changed to F if Cmp is evaluated to True!
2292 * We prevent this by putting Conds in an extra cprop_X queue, which
2293 * gets evaluated after the cprop queue is empty.
2295 * Note that this even happens with Click's original algorithm, if
2296 * Cmp(x, 0) is evaluated to True first and later changed to False
2297 * if x was Top first and later changed to a Const ...
2298 * It is unclear how Click solved that problem ...
2300 * However, in rare cases even this does not help, if a Top reaches
2301 * a compare through a Phi, than Proj(Cond) is evaluated changing
2302 * the type of the Phi to something other.
2303 * So, we take the last resort and bind the type to R once
2306 * (This might be even the way Click works around the whole problem).
2308 * Finally, we may miss some optimization possibilities due to this:
2313 * If Top reaches the if first, than we decide for != here.
2314 * If y later is evaluated to 0, we cannot revert this decision
2315 * and must live with both outputs enabled. If this happens,
2316 * we get an unresolved if (true) in the code ...
2318 * In Click's version where this decision is done at the Cmp,
2319 * the Cmp is NOT optimized away than (if y evaluated to 1
2320 * for instance) and we get a if (1 == 0) here ...
2322 * Both solutions are suboptimal.
2323 * At least, we could easily detect this problem and run
2324 * cf_opt() (or even combo) again :-(
2326 if (node->type.tv == tarval_reachable)
2329 if (pnc == pn_Cond_true) {
2330 if (selector->type.tv == tarval_b_false) {
2331 node->type.tv = tarval_unreachable;
2332 } else if (selector->type.tv == tarval_b_true) {
2333 node->type.tv = tarval_reachable;
2334 } else if (selector->type.tv == tarval_bottom) {
2335 node->type.tv = tarval_reachable;
2337 assert(selector->type.tv == tarval_top);
2338 if (tarval_UNKNOWN == tarval_top) {
2339 /* any condition based on Top is "!=" */
2340 node->type.tv = tarval_unreachable;
2342 node->type.tv = tarval_unreachable;
2346 assert(pnc == pn_Cond_false);
2348 if (selector->type.tv == tarval_b_false) {
2349 node->type.tv = tarval_reachable;
2350 } else if (selector->type.tv == tarval_b_true) {
2351 node->type.tv = tarval_unreachable;
2352 } else if (selector->type.tv == tarval_bottom) {
2353 node->type.tv = tarval_reachable;
2355 assert(selector->type.tv == tarval_top);
2356 if (tarval_UNKNOWN == tarval_top) {
2357 /* any condition based on Top is "!=" */
2358 node->type.tv = tarval_reachable;
2360 node->type.tv = tarval_unreachable;
2364 } /* compute_Proj_Cond */
2366 static void compute_Proj_Switch(node_t *node, ir_node *switchn)
2368 ir_node *proj = node->node;
2369 long pnc = get_Proj_proj(proj);
2370 ir_node *sel = get_Switch_selector(switchn);
2371 node_t *selector = get_irn_node(sel);
2373 /* see long comment in compute_Proj_Cond */
2374 if (node->type.tv == tarval_reachable)
2377 if (selector->type.tv == tarval_bottom) {
2378 node->type.tv = tarval_reachable;
2379 } else if (selector->type.tv == tarval_top) {
2380 if (tarval_UNKNOWN == tarval_top && pnc == pn_Switch_default) {
2381 /* a switch based of Top is always "default" */
2382 node->type.tv = tarval_reachable;
2384 node->type.tv = tarval_unreachable;
2387 long value = get_tarval_long(selector->type.tv);
2388 const ir_switch_table *table = get_Switch_table(switchn);
2389 size_t n_entries = ir_switch_table_get_n_entries(table);
2392 for (e = 0; e < n_entries; ++e) {
2393 const ir_switch_table_entry *entry
2394 = ir_switch_table_get_entry_const(table, e);
2395 ir_tarval *min = entry->min;
2396 ir_tarval *max = entry->max;
2398 if (selector->type.tv == min) {
2399 node->type.tv = entry->pn == pnc
2400 ? tarval_reachable : tarval_unreachable;
2404 long minval = get_tarval_long(min);
2405 long maxval = get_tarval_long(max);
2406 if (minval <= value && value <= maxval) {
2407 node->type.tv = entry->pn == pnc
2408 ? tarval_reachable : tarval_unreachable;
2414 /* no entry matched: default */
2416 = pnc == pn_Switch_default ? tarval_reachable : tarval_unreachable;
2421 * (Re-)compute the type for a Proj-Node.
2423 * @param node the node
2425 static void compute_Proj(node_t *node)
2427 ir_node *proj = node->node;
2428 ir_mode *mode = get_irn_mode(proj);
2429 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2430 ir_node *pred = get_Proj_pred(proj);
2432 if (block->type.tv == tarval_unreachable) {
2433 /* a Proj in a unreachable Block stay Top */
2434 node->type.tv = tarval_top;
2437 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred) && !is_Switch(pred)) {
2438 /* if the predecessor is Top, its Proj follow */
2439 node->type.tv = tarval_top;
2443 if (mode == mode_M) {
2444 /* mode M is always bottom */
2445 node->type.tv = tarval_bottom;
2447 } else if (mode == mode_X) {
2448 /* handle mode_X nodes */
2449 switch (get_irn_opcode(pred)) {
2451 /* the Proj_X from the Start is always reachable.
2452 However this is already handled at the top. */
2453 node->type.tv = tarval_reachable;
2456 compute_Proj_Cond(node, pred);
2459 compute_Proj_Switch(node, pred);
2466 default_compute(node);
2467 } /* compute_Proj */
2470 * (Re-)compute the type for a Confirm.
2472 * @param node the node
2474 static void compute_Confirm(node_t *node)
2476 ir_node *confirm = node->node;
2477 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2479 if (get_Confirm_relation(confirm) == ir_relation_equal) {
2480 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2482 if (is_con(bound->type)) {
2483 /* is equal to a constant */
2484 node->type = bound->type;
2488 /* a Confirm is a copy OR a Const */
2489 node->type = pred->type;
2490 } /* compute_Confirm */
2493 * (Re-)compute the type for a given node.
2495 * @param node the node
2497 static void compute(node_t *node)
2499 ir_node *irn = node->node;
2502 #ifndef VERIFY_MONOTONE
2504 * Once a node reaches bottom, the type cannot fall further
2505 * in the lattice and we can stop computation.
2506 * Do not take this exit if the monotony verifier is
2507 * enabled to catch errors.
2509 if (node->type.tv == tarval_bottom)
2513 if (!is_Block(irn)) {
2514 /* for pinned nodes, check its control input */
2515 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2516 node_t *block = get_irn_node(get_nodes_block(irn));
2518 if (block->type.tv == tarval_unreachable) {
2519 node->type.tv = tarval_top;
2525 func = (compute_func)node->node->op->ops.generic;
2531 * Identity functions: Note that one might think that identity() is just a
2532 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2533 * here, because it expects that the identity node is one of the inputs, which is NOT
2534 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2535 * So, we have our own implementation, which copies some parts of equivalent_node()
2539 * Calculates the Identity for Phi nodes
2541 static node_t *identity_Phi(node_t *node)
2543 ir_node *phi = node->node;
2544 ir_node *block = get_nodes_block(phi);
2545 node_t *n_part = NULL;
2548 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2549 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2551 if (pred_X->type.tv == tarval_reachable) {
2552 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2556 else if (n_part->part != pred->part) {
2557 /* incongruent inputs, not a follower */
2562 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2563 * tarval_top, is in the TOP partition and should NOT being split! */
2564 assert(n_part != NULL);
2566 } /* identity_Phi */
2569 * Calculates the Identity for commutative 0 neutral nodes.
2571 static node_t *identity_comm_zero_binop(node_t *node)
2573 ir_node *op = node->node;
2574 node_t *a = get_irn_node(get_binop_left(op));
2575 node_t *b = get_irn_node(get_binop_right(op));
2576 ir_mode *mode = get_irn_mode(op);
2579 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2580 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2583 /* node: no input should be tarval_top, else the binop would be also
2584 * Top and not being split. */
2585 zero = get_mode_null(mode);
2586 if (a->type.tv == zero)
2588 if (b->type.tv == zero)
2591 } /* identity_comm_zero_binop */
2594 * Calculates the Identity for Shift nodes.
2596 static node_t *identity_shift(node_t *node)
2598 ir_node *op = node->node;
2599 node_t *b = get_irn_node(get_binop_right(op));
2600 ir_mode *mode = get_irn_mode(b->node);
2603 /* node: no input should be tarval_top, else the binop would be also
2604 * Top and not being split. */
2605 zero = get_mode_null(mode);
2606 if (b->type.tv == zero)
2607 return get_irn_node(get_binop_left(op));
2609 } /* identity_shift */
2612 * Calculates the Identity for Mul nodes.
2614 static node_t *identity_Mul(node_t *node)
2616 ir_node *op = node->node;
2617 node_t *a = get_irn_node(get_Mul_left(op));
2618 node_t *b = get_irn_node(get_Mul_right(op));
2619 ir_mode *mode = get_irn_mode(op);
2622 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2623 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2626 /* node: no input should be tarval_top, else the binop would be also
2627 * Top and not being split. */
2628 one = get_mode_one(mode);
2629 if (a->type.tv == one)
2631 if (b->type.tv == one)
2634 } /* identity_Mul */
2637 * Calculates the Identity for Sub nodes.
2639 static node_t *identity_Sub(node_t *node)
2641 ir_node *sub = node->node;
2642 node_t *b = get_irn_node(get_Sub_right(sub));
2643 ir_mode *mode = get_irn_mode(sub);
2645 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2646 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2649 /* node: no input should be tarval_top, else the binop would be also
2650 * Top and not being split. */
2651 if (b->type.tv == get_mode_null(mode))
2652 return get_irn_node(get_Sub_left(sub));
2654 } /* identity_Sub */
2657 * Calculates the Identity for And nodes.
2659 static node_t *identity_And(node_t *node)
2661 ir_node *andnode = node->node;
2662 node_t *a = get_irn_node(get_And_left(andnode));
2663 node_t *b = get_irn_node(get_And_right(andnode));
2664 ir_tarval *neutral = get_mode_all_one(get_irn_mode(andnode));
2666 /* node: no input should be tarval_top, else the And would be also
2667 * Top and not being split. */
2668 if (a->type.tv == neutral)
2670 if (b->type.tv == neutral)
2673 } /* identity_And */
2676 * Calculates the Identity for Confirm nodes.
2678 static node_t *identity_Confirm(node_t *node)
2680 ir_node *confirm = node->node;
2682 /* a Confirm is always a Copy */
2683 return get_irn_node(get_Confirm_value(confirm));
2684 } /* identity_Confirm */
2687 * Calculates the Identity for Mux nodes.
2689 static node_t *identity_Mux(node_t *node)
2691 ir_node *mux = node->node;
2692 node_t *t = get_irn_node(get_Mux_true(mux));
2693 node_t *f = get_irn_node(get_Mux_false(mux));
2696 if (t->part == f->part)
2699 /* for now, the 1-input identity is not supported */
2701 sel = get_irn_node(get_Mux_sel(mux));
2703 /* Mux sel input is mode_b, so it is always a tarval */
2704 if (sel->type.tv == tarval_b_true)
2706 if (sel->type.tv == tarval_b_false)
2710 } /* identity_Mux */
2713 * Calculates the Identity for nodes.
2715 static node_t *identity(node_t *node)
2717 ir_node *irn = node->node;
2719 switch (get_irn_opcode(irn)) {
2721 return identity_Phi(node);
2723 return identity_Mul(node);
2727 return identity_comm_zero_binop(node);
2732 return identity_shift(node);
2734 return identity_And(node);
2736 return identity_Sub(node);
2738 return identity_Confirm(node);
2740 return identity_Mux(node);
2747 * Node follower is a (new) follower of leader, segregate Leader
2750 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader)
2752 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2753 /* The leader edges must remain sorted, but follower edges can
2755 ir_node *l = leader->node;
2756 unsigned n = get_irn_n_outs(l);
2757 for (unsigned i = leader->n_followers; i < n; ++i) {
2758 if (l->o.out->edges[i].use == follower) {
2759 ir_def_use_edge t = l->o.out->edges[i];
2761 for (unsigned j = i; j-- > leader->n_followers; )
2762 l->o.out->edges[j+1] = l->o.out->edges[j];
2763 l->o.out->edges[leader->n_followers] = t;
2764 ++leader->n_followers;
2768 } /* segregate_def_use_chain_1 */
2771 * Node follower is a (new) follower segregate its Leader
2774 * @param follower the follower IR node
2776 static void segregate_def_use_chain(const ir_node *follower)
2780 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2781 node_t *pred = get_irn_node(get_irn_n(follower, i));
2783 segregate_def_use_chain_1(follower, pred);
2785 } /* segregate_def_use_chain */
2788 * Propagate constant evaluation.
2790 * @param env the environment
2792 static void propagate(environment_t *env)
2796 lattice_elem_t old_type;
2798 unsigned n_fallen, old_type_was_T_or_C;
2800 while (env->cprop != NULL) {
2801 void *oldopcode = NULL;
2803 /* remove the first partition X from cprop */
2806 env->cprop = X->cprop_next;
2808 old_type_was_T_or_C = X->type_is_T_or_C;
2810 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2814 int cprop_empty = list_empty(&X->cprop);
2815 int cprop_X_empty = list_empty(&X->cprop_X);
2817 if (cprop_empty && cprop_X_empty) {
2818 /* both cprop lists are empty */
2822 /* remove the first Node x from X.cprop */
2824 /* Get a node from the cprop_X list only if
2825 * all data nodes are processed.
2826 * This ensures, that all inputs of the Cond
2827 * predecessor are processed if its type is still Top.
2829 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2831 x = list_entry(X->cprop.next, node_t, cprop_list);
2834 //assert(x->part == X);
2835 list_del(&x->cprop_list);
2838 if (x->is_follower && identity(x) == x) {
2839 /* check the opcode first */
2840 if (oldopcode == NULL) {
2841 oldopcode = lambda_opcode(get_first_node(X), env);
2843 if (oldopcode != lambda_opcode(x, env)) {
2844 if (x->on_fallen == 0) {
2845 /* different opcode -> x falls out of this partition */
2850 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2854 /* x will make the follower -> leader transition */
2855 follower_to_leader(x);
2857 /* In case of a follower -> leader transition of a Phi node
2858 * we have to ensure that the current partition will be split
2859 * by lambda n.(n[i].partition).
2861 * This split may already happened before when some predecessors
2862 * of the Phi's Block are unreachable. Thus, we have to put the
2863 * current partition in the worklist to repeat the check.
2865 if (is_Phi(x->node) && ! x->part->on_worklist)
2866 add_to_worklist(x->part, env);
2869 /* compute a new type for x */
2871 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2873 if (x->type.tv != old_type.tv) {
2874 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2875 verify_type(old_type, x);
2877 if (x->on_fallen == 0) {
2878 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2879 not already on the list. */
2884 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2886 for (unsigned i = get_irn_n_outs(x->node); i-- > 0; ) {
2887 ir_node *succ = get_irn_out(x->node, i);
2888 node_t *y = get_irn_node(succ);
2890 /* Add y to y.partition.cprop. */
2891 add_to_cprop(y, env);
2896 if (n_fallen > 0 && n_fallen != X->n_leader) {
2897 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2898 Y = split(&X, fallen, env);
2900 * We have split out fallen node. The type of the result
2901 * partition is NOT set yet.
2903 Y->type_is_T_or_C = 0;
2907 /* remove the flags from the fallen list */
2908 for (x = fallen; x != NULL; x = x->next)
2911 if (old_type_was_T_or_C) {
2912 /* check if some nodes will make the leader -> follower transition */
2913 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2914 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2915 node_t *eq_node = identity(y);
2917 if (eq_node != y && eq_node->part == y->part) {
2918 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2919 /* move to Follower */
2921 list_del(&y->node_list);
2922 list_add_tail(&y->node_list, &Y->Follower);
2925 segregate_def_use_chain(y->node);
2935 * Get the leader for a given node from its congruence class.
2937 * @param irn the node
2939 static ir_node *get_leader(node_t *node)
2941 partition_t *part = node->part;
2943 if (part->n_leader > 1 || node->is_follower) {
2944 if (node->is_follower) {
2945 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2948 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2950 return get_first_node(part)->node;
2956 * Returns non-zero if a mode_T node has only one reachable output.
2958 static int only_one_reachable_proj(ir_node *n)
2962 for (unsigned i = get_irn_n_outs(n); i-- > 0; ) {
2963 ir_node *proj = get_irn_out(n, i);
2966 /* skip non-control flow Proj's */
2967 if (get_irn_mode(proj) != mode_X)
2970 node = get_irn_node(proj);
2971 if (node->type.tv == tarval_reachable) {
2977 } /* only_one_reachable_proj */
2980 * Return non-zero if the control flow predecessor node pred
2981 * is the only reachable control flow exit of its block.
2983 * @param pred the control flow exit
2984 * @param block the destination block
2986 static int can_exchange(ir_node *pred, ir_node *block)
2988 if (is_Start(pred) || get_Block_entity(block) != NULL)
2990 else if (is_Jmp(pred))
2992 else if (is_Raise(pred)) {
2993 /* Raise is a tuple and usually has only one reachable ProjX,
2994 * but it must not be eliminated like a Jmp */
2997 else if (get_irn_mode(pred) == mode_T) {
2998 /* if the predecessor block has more than one
2999 reachable outputs we cannot remove the block */
3000 return only_one_reachable_proj(pred);
3003 } /* can_exchange */
3006 * Block Post-Walker, apply the analysis results on control flow by
3007 * shortening Phi's and Block inputs.
3009 static void apply_cf(ir_node *block, void *ctx)
3011 environment_t *env = (environment_t*)ctx;
3012 node_t *node = get_irn_node(block);
3014 ir_node **ins, **in_X;
3015 ir_node *phi, *next;
3017 n = get_Block_n_cfgpreds(block);
3019 if (node->type.tv == tarval_unreachable) {
3022 for (i = n - 1; i >= 0; --i) {
3023 ir_node *pred = get_Block_cfgpred(block, i);
3025 if (! is_Bad(pred)) {
3026 ir_node *pred_block = get_nodes_block(skip_Proj(pred));
3027 if (!is_Bad(pred_block)) {
3028 node_t *pred_bl = get_irn_node(pred_block);
3030 if (pred_bl->flagged == 0) {
3031 pred_bl->flagged = 3;
3033 if (pred_bl->type.tv == tarval_reachable) {
3035 * We will remove an edge from block to its pred.
3036 * This might leave the pred block as an endless loop
3038 if (! is_backedge(block, i))
3039 keep_alive(pred_bl->node);
3046 ir_graph *const irg = get_Block_irg(block);
3047 if (block == get_irg_end_block(irg)) {
3048 /* Analysis found out that the end block is unreachable,
3049 * hence we remove all its control flow predecessors. */
3050 set_irn_in(block, 0, NULL);
3056 /* only one predecessor combine */
3057 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3059 if (can_exchange(pred, block)) {
3060 ir_node *new_block = get_nodes_block(pred);
3061 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3062 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3063 exchange(block, new_block);
3064 node->node = new_block;
3070 NEW_ARR_A(ir_node *, in_X, n);
3072 for (i = 0; i < n; ++i) {
3073 ir_node *pred = get_Block_cfgpred(block, i);
3074 node_t *node = get_irn_node(pred);
3076 if (node->type.tv == tarval_reachable) {
3079 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3080 if (! is_Bad(pred)) {
3081 ir_node *pred_block = get_nodes_block(skip_Proj(pred));
3082 if (!is_Bad(pred_block)) {
3083 node_t *pred_bl = get_irn_node(pred_block);
3085 if (!is_Bad(pred_bl->node) && pred_bl->flagged == 0) {
3086 pred_bl->flagged = 3;
3088 if (pred_bl->type.tv == tarval_reachable) {
3090 * We will remove an edge from block to its pred.
3091 * This might leave the pred block as an endless loop
3093 if (! is_backedge(block, i))
3094 keep_alive(pred_bl->node);
3105 NEW_ARR_A(ir_node *, ins, n);
3106 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3107 node_t *node = get_irn_node(phi);
3109 next = get_Phi_next(phi);
3110 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3111 /* this Phi is replaced by a constant */
3112 ir_tarval *tv = node->type.tv;
3113 ir_node *c = new_r_Const(current_ir_graph, tv);
3115 set_irn_node(c, node);
3117 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3118 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3123 for (i = 0; i < n; ++i) {
3124 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3126 if (pred->type.tv == tarval_reachable) {
3127 ins[j++] = get_Phi_pred(phi, i);
3131 /* this Phi is replaced by a single predecessor */
3132 ir_node *s = ins[0];
3133 node_t *phi_node = get_irn_node(phi);
3136 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3137 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3142 set_irn_in(phi, j, ins);
3150 /* this Block has only one live predecessor */
3151 ir_node *pred = skip_Proj(in_X[0]);
3153 if (can_exchange(pred, block)) {
3154 ir_node *new_block = get_nodes_block(pred);
3155 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3156 exchange(block, new_block);
3157 node->node = new_block;
3162 set_irn_in(block, k, in_X);
3167 * Exchange a node by its leader.
3168 * Beware: in rare cases the mode might be wrong here, for instance
3169 * AddP(x, NULL) is a follower of x, but with different mode.
3172 static void exchange_leader(ir_node *irn, ir_node *leader)
3174 ir_mode *mode = get_irn_mode(irn);
3175 if (mode != get_irn_mode(leader)) {
3176 /* The conv is a no-op, so we are free to place it
3177 * either in the block of the leader OR in irn's block.
3178 * Probably placing it into leaders block might reduce
3179 * the number of Conv due to CSE. */
3180 ir_node *block = get_nodes_block(leader);
3181 dbg_info *dbg = get_irn_dbg_info(irn);
3182 ir_node *nlead = new_rd_Conv(dbg, block, leader, mode);
3184 if (nlead != leader) {
3185 /* Note: this newly create irn has no node info because
3186 * it is created after the analysis. However, this node
3187 * replaces the node irn and should not be visited again,
3188 * so set its visited count to the count of irn.
3189 * Otherwise we might visited this node more than once if
3190 * irn had more than one user.
3192 set_irn_node(nlead, NULL);
3193 set_irn_visited(nlead, get_irn_visited(irn));
3197 exchange(irn, leader);
3198 } /* exchange_leader */
3201 * Check, if all users of a mode_M node are dead. Use
3202 * the Def-Use edges for this purpose, as they still
3203 * reflect the situation.
3205 static int all_users_are_dead(const ir_node *irn)
3207 unsigned n = get_irn_n_outs(irn);
3208 for (unsigned i = 0; i < n; ++i) {
3209 const ir_node *succ = get_irn_out(irn, i);
3210 const node_t *block = get_irn_node(get_nodes_block(succ));
3213 if (block->type.tv == tarval_unreachable) {
3214 /* block is unreachable */
3217 node = get_irn_node(succ);
3218 if (node->type.tv != tarval_top) {
3219 /* found a reachable user */
3223 /* all users are unreachable */
3225 } /* all_user_are_dead */
3228 * Walker: Find reachable mode_M nodes that have only
3229 * unreachable users. These nodes must be kept later.
3231 static void find_kept_memory(ir_node *irn, void *ctx)
3233 environment_t *env = (environment_t*)ctx;
3234 node_t *node, *block;
3236 if (get_irn_mode(irn) != mode_M)
3239 block = get_irn_node(get_nodes_block(irn));
3240 if (block->type.tv == tarval_unreachable)
3243 node = get_irn_node(irn);
3244 if (node->type.tv == tarval_top)
3247 /* ok, we found a live memory node. */
3248 if (all_users_are_dead(irn)) {
3249 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3250 ARR_APP1(ir_node *, env->kept_memory, irn);
3252 } /* find_kept_memory */
3255 * Post-Walker, apply the analysis results;
3257 static void apply_result(ir_node *irn, void *ctx)
3259 environment_t *env = (environment_t*)ctx;
3260 node_t *node = get_irn_node(irn);
3262 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3263 /* blocks already handled, do not touch the End node */
3265 node_t *block = get_irn_node(get_nodes_block(irn));
3267 if (block->type.tv == tarval_unreachable) {
3268 ir_graph *irg = get_irn_irg(irn);
3269 ir_mode *mode = get_irn_mode(node->node);
3270 ir_node *bad = new_r_Bad(irg, mode);
3272 /* here, bad might already have a node, but this can be safely ignored
3273 as long as bad has at least ONE valid node */
3274 set_irn_node(bad, node);
3276 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3279 } else if (node->type.tv == tarval_top) {
3280 ir_mode *mode = get_irn_mode(irn);
3282 if (mode == mode_M) {
3283 /* never kill a mode_M node */
3285 ir_node *pred = get_Proj_pred(irn);
3286 node_t *pnode = get_irn_node(pred);
3288 if (pnode->type.tv == tarval_top) {
3289 /* skip the predecessor */
3290 ir_node *mem = get_memop_mem(pred);
3292 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3297 /* leave other nodes, especially PhiM */
3298 } else if (mode == mode_T) {
3299 /* Do not kill mode_T nodes, kill their Projs */
3300 } else if (! is_Unknown(irn)) {
3301 /* don't kick away Unknown's, they might be still needed */
3302 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3304 /* control flow should already be handled at apply_cf() */
3305 assert(mode != mode_X);
3307 /* see comment above */
3308 set_irn_node(unk, node);
3310 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3315 else if (get_irn_mode(irn) == mode_X) {
3318 ir_node *cond = get_Proj_pred(irn);
3320 if (is_Cond(cond) || is_Switch(cond)) {
3321 if (only_one_reachable_proj(cond)) {
3322 ir_node *jmp = new_r_Jmp(block->node);
3323 set_irn_node(jmp, node);
3325 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3326 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3330 if (is_Switch(cond)) {
3331 node_t *sel = get_irn_node(get_Switch_selector(cond));
3332 ir_tarval *tv = sel->type.tv;
3334 if (is_tarval(tv) && tarval_is_constant(tv)) {
3335 /* The selector is a constant, but more
3336 * than one output is active: An unoptimized
3345 /* normal data node */
3346 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3347 ir_tarval *tv = node->type.tv;
3350 * Beware: never replace mode_T nodes by constants. Currently we must mark
3351 * mode_T nodes with constants, but do NOT replace them.
3353 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3354 /* can be replaced by a constant */
3355 ir_node *c = new_r_Const(current_ir_graph, tv);
3356 set_irn_node(c, node);
3358 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3359 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3360 exchange_leader(irn, c);
3363 } else if (is_entity(node->type.sym.entity_p)) {
3364 if (! is_SymConst(irn)) {
3365 /* can be replaced by a SymConst */
3366 ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3367 set_irn_node(symc, node);
3370 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3371 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3372 exchange_leader(irn, symc);
3375 } else if (is_Confirm(irn)) {
3376 /* Confirms are always follower, but do not kill them here */
3378 ir_node *leader = get_leader(node);
3380 if (leader != irn) {
3381 int non_strict_phi = 0;
3384 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3385 * as this might create non-strict programs.
3387 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3390 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3391 ir_node *pred = get_Phi_pred(irn, i);
3393 if (is_Unknown(pred)) {
3399 if (! non_strict_phi) {
3400 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3401 if (node->is_follower)
3402 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3404 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3405 exchange_leader(irn, leader);
3412 } /* apply_result */
3415 * Fix the keep-alives by deleting unreachable ones.
3417 static void apply_end(ir_node *end, environment_t *env)
3419 int i, j, n = get_End_n_keepalives(end);
3420 ir_node **in = NULL;
3423 NEW_ARR_A(ir_node *, in, n);
3425 /* fix the keep alive */
3426 for (i = j = 0; i < n; i++) {
3427 ir_node *ka = get_End_keepalive(end, i);
3433 if (!is_Block(ka)) {
3434 block = get_nodes_block(ka);
3441 node = get_irn_node(block);
3442 if (node->type.tv != tarval_unreachable)
3446 set_End_keepalives(end, j, in);
3451 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3454 * sets the generic functions to compute.
3456 static void set_compute_functions(void)
3460 /* set the default compute function */
3461 for (i = 0, n = ir_get_n_opcodes(); i < n; ++i) {
3462 ir_op *op = ir_get_opcode(i);
3463 op->ops.generic = (op_func)default_compute;
3466 /* set specific functions */
3482 } /* set_compute_functions */
3487 static void add_memory_keeps(ir_node **kept_memory, size_t len)
3489 ir_node *end = get_irg_end(current_ir_graph);
3494 ir_nodeset_init(&set);
3496 /* check, if those nodes are already kept */
3497 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3498 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3500 for (idx = 0; idx < len; ++idx) {
3501 ir_node *ka = kept_memory[idx];
3503 if (! ir_nodeset_contains(&set, ka)) {
3504 add_End_keepalive(end, ka);
3507 ir_nodeset_destroy(&set);
3508 } /* add_memory_keeps */
3510 void combo(ir_graph *irg)
3513 ir_node *initial_bl;
3515 ir_graph *rem = current_ir_graph;
3518 assure_irg_properties(irg,
3519 IR_GRAPH_PROPERTY_NO_BADS
3520 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
3521 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
3523 current_ir_graph = irg;
3525 /* register a debug mask */
3526 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3528 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3530 obstack_init(&env.obst);
3531 env.worklist = NULL;
3535 #ifdef DEBUG_libfirm
3536 env.dbg_list = NULL;
3538 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3539 env.kept_memory = NEW_ARR_F(ir_node *, 0);
3540 env.end_idx = get_opt_global_cse() ? 0 : -1;
3541 env.lambda_input = 0;
3544 /* options driving the optimization */
3545 env.commutative = 1;
3546 env.opt_unknown = 1;
3548 /* we have our own value_of function */
3549 set_value_of_func(get_node_tarval);
3551 set_compute_functions();
3552 DEBUG_ONLY(part_nr = 0;)
3554 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3556 if (env.opt_unknown)
3557 tarval_UNKNOWN = tarval_top;
3559 tarval_UNKNOWN = tarval_bad;
3561 /* create the initial partition and place it on the work list */
3562 env.initial = new_partition(&env);
3563 add_to_worklist(env.initial, &env);
3564 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3566 /* set the hook: from now, every node has a partition and a type */
3567 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook);)
3569 /* all nodes on the initial partition have type Top */
3570 env.initial->type_is_T_or_C = 1;
3572 /* Place the START Node's partition on cprop.
3573 Place the START Node on its local worklist. */
3574 initial_bl = get_irg_start_block(irg);
3575 start = get_irn_node(initial_bl);
3576 add_to_cprop(start, &env);
3580 if (env.worklist != NULL)
3582 } while (env.cprop != NULL || env.worklist != NULL);
3584 dump_all_partitions(&env);
3585 check_all_partitions(&env);
3588 dump_ir_block_graph(irg, "-partition");
3591 /* apply the result */
3593 /* check, which nodes must be kept */
3594 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3596 /* kill unreachable control flow */
3597 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3598 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3599 * and fixes assertion because dead cf to dead blocks is NOT removed by
3601 apply_end(get_irg_end(irg), &env);
3602 irg_walk_graph(irg, NULL, apply_result, &env);
3604 len = ARR_LEN(env.kept_memory);
3606 add_memory_keeps(env.kept_memory, len);
3609 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3612 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3614 /* remove the partition hook */
3615 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL);)
3617 DEL_ARR_F(env.kept_memory);
3618 del_set(env.opcode2id_map);
3619 obstack_free(&env.obst, NULL);
3621 /* restore value_of() default behavior */
3622 set_value_of_func(NULL);
3623 current_ir_graph = rem;
3625 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
3628 /* Creates an ir_graph pass for combo. */
3629 ir_graph_pass_t *combo_pass(const char *name)
3631 return def_graph_pass(name ? name : "combo", combo);