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 othe 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 reavaluated
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"
90 /* define this to check that all type translations are monotone */
91 #define VERIFY_MONOTONE
93 /* define this to check the consistency of partitions */
94 #define CHECK_PARTITIONS
96 typedef struct node_t node_t;
97 typedef struct partition_t partition_t;
98 typedef struct opcode_key_t opcode_key_t;
99 typedef struct listmap_entry_t listmap_entry_t;
101 /** The type of the compute function. */
102 typedef void (*compute_func)(node_t *node);
107 struct opcode_key_t {
108 ir_node *irn; /**< An IR node representing this opcode. */
112 * An entry in the list_map.
114 struct listmap_entry_t {
115 void *id; /**< The id. */
116 node_t *list; /**< The associated list for this id. */
117 listmap_entry_t *next; /**< Link to the next entry in the map. */
120 /** We must map id's to lists. */
121 typedef struct listmap_t {
122 set *map; /**< Map id's to listmap_entry_t's */
123 listmap_entry_t *values; /**< List of all values in the map. */
127 * A lattice element. Because we handle constants and symbolic constants different, we
128 * have to use this union.
139 ir_node *node; /**< The IR-node itself. */
140 list_head node_list; /**< Double-linked list of leader/follower entries. */
141 list_head cprop_list; /**< Double-linked partition.cprop list. */
142 partition_t *part; /**< points to the partition this node belongs to */
143 node_t *next; /**< Next node on local list (partition.touched, fallen). */
144 node_t *race_next; /**< Next node on race list. */
145 lattice_elem_t type; /**< The associated lattice element "type". */
146 int max_user_input; /**< Maximum input number of Def-Use edges. */
147 int next_edge; /**< Index of the next Def-Use edge to use. */
148 int n_followers; /**< Number of Follower in the outs set. */
149 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
150 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
151 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
152 unsigned is_follower:1; /**< Set, if this node is a follower. */
153 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
157 * A partition containing congruent nodes.
160 list_head Leader; /**< The head of partition Leader node list. */
161 list_head Follower; /**< The head of partition Follower node list. */
162 list_head cprop; /**< The head of partition.cprop list. */
163 list_head cprop_X; /**< The head of partition.cprop (Cond nodes and its Projs) list. */
164 partition_t *wl_next; /**< Next entry in the work list if any. */
165 partition_t *touched_next; /**< Points to the next partition in the touched set. */
166 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
167 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
168 node_t *touched; /**< The partition.touched set of this partition. */
169 unsigned n_leader; /**< Number of entries in this partition.Leader. */
170 unsigned n_touched; /**< Number of entries in the partition.touched. */
171 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
172 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
173 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
174 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
175 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
177 partition_t *dbg_next; /**< Link all partitions for debugging */
178 unsigned nr; /**< A unique number for (what-)mapping, >0. */
182 typedef struct environment_t {
183 struct obstack obst; /**< obstack to allocate data structures. */
184 partition_t *worklist; /**< The work list. */
185 partition_t *cprop; /**< The constant propagation list. */
186 partition_t *touched; /**< the touched set. */
187 partition_t *initial; /**< The initial partition. */
188 set *opcode2id_map; /**< The opcodeMode->id map. */
189 ir_node **kept_memory; /**< Array of memory nodes that must be kept. */
190 int end_idx; /**< -1 for local and 0 for global congruences. */
191 int lambda_input; /**< Captured argument for lambda_partition(). */
192 unsigned modified:1; /**< Set, if the graph was modified. */
193 unsigned unopt_cf:1; /**< If set, control flow is not optimized due to Unknown. */
194 /* options driving the optimization */
195 unsigned commutative:1; /**< Set, if commutation nodes should be handled specially. */
196 unsigned opt_unknown:1; /**< Set, if non-strict programs should be optimized. */
198 partition_t *dbg_list; /**< List of all partitions. */
202 /** Type of the what function. */
203 typedef void *(*what_func)(const node_t *node, environment_t *env);
205 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
206 #define set_irn_node(irn, node) set_irn_link(irn, node)
208 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
209 #undef tarval_unreachable
210 #define tarval_unreachable tarval_top
213 /** The debug module handle. */
214 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
216 /** The what reason. */
217 DEBUG_ONLY(static const char *what_reason;)
219 /** Next partition number. */
220 DEBUG_ONLY(static unsigned part_nr = 0;)
222 /** The tarval returned by Unknown nodes: set to either tarval_bad OR tarval_top. */
223 static ir_tarval *tarval_UNKNOWN;
226 static node_t *identity(node_t *node);
229 * Compare two opcode representatives.
231 static int cmp_irn_opcode(const ir_node *a, const ir_node *b)
235 if ((get_irn_op(a) != get_irn_op(b)) ||
236 (get_irn_mode(a) != get_irn_mode(b)))
239 /* compare if a's in and b's in are of equal length */
240 arity = get_irn_arity(a);
241 if (arity != get_irn_arity(b))
246 * Some ugliness here: Two Blocks having the same
247 * IJmp predecessor would be congruent, which of course is wrong.
248 * We fix it by never letting blocks be congruent
249 * which cannot be detected by combo either.
255 * here, we already know that the nodes are identical except their
258 if (a->op->ops.node_cmp_attr)
259 return a->op->ops.node_cmp_attr(a, b);
262 } /* cmp_irn_opcode */
264 #ifdef CHECK_PARTITIONS
268 static void check_partition(const partition_t *T)
272 list_for_each_entry(node_t, node, &T->Leader, node_list) {
273 assert(node->is_follower == 0);
274 assert(node->flagged == 0);
275 assert(node->part == T);
278 assert(n == T->n_leader);
280 list_for_each_entry(node_t, node, &T->Follower, node_list) {
281 assert(node->is_follower == 1);
282 assert(node->flagged == 0);
283 assert(node->part == T);
285 } /* check_partition */
288 * check that all leader nodes in the partition have the same opcode.
290 static void check_opcode(const partition_t *Z)
292 const ir_node *repr = NULL;
294 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
295 ir_node *irn = node->node;
300 assert(cmp_irn_opcode(repr, irn) == 0);
305 static void check_all_partitions(environment_t *env)
310 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
312 if (! P->type_is_T_or_C)
314 list_for_each_entry(node_t, node, &P->Follower, node_list) {
315 node_t *leader = identity(node);
317 assert(leader != node && leader->part == node->part);
328 static void do_check_list(const node_t *list, int ofs, const partition_t *Z)
333 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
334 for (e = list; e != NULL; e = NEXT(e)) {
335 assert(e->part == Z);
343 } /* ido_check_list */
346 * Check a local list.
348 static void check_list(const node_t *list, const partition_t *Z)
350 do_check_list(list, offsetof(node_t, next), Z);
354 #define check_partition(T)
355 #define check_list(list, Z)
356 #define check_all_partitions(env)
357 #endif /* CHECK_PARTITIONS */
360 static inline lattice_elem_t get_partition_type(const partition_t *X);
363 * Dump partition to output.
365 static void dump_partition(const char *msg, const partition_t *part)
368 lattice_elem_t type = get_partition_type(part);
370 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
371 msg, part->nr, part->type_is_T_or_C ? "*" : "",
372 part->n_leader, type));
373 list_for_each_entry(node_t, node, &part->Leader, node_list) {
374 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
377 if (! list_empty(&part->Follower)) {
378 DB((dbg, LEVEL_2, "\n---\n "));
380 list_for_each_entry(node_t, node, &part->Follower, node_list) {
381 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
385 DB((dbg, LEVEL_2, "\n}\n"));
386 } /* dump_partition */
391 static void do_dump_list(const char *msg, const node_t *node, int ofs)
396 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
398 DB((dbg, LEVEL_3, "%s = {\n ", msg));
399 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
400 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
403 DB((dbg, LEVEL_3, "\n}\n"));
411 static void dump_race_list(const char *msg, const node_t *list)
413 do_dump_list(msg, list, offsetof(node_t, race_next));
414 } /* dump_race_list */
417 * Dumps a local list.
419 static void dump_list(const char *msg, const node_t *list)
421 do_dump_list(msg, list, offsetof(node_t, next));
425 * Dump all partitions.
427 static void dump_all_partitions(const environment_t *env)
429 const partition_t *P;
431 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
432 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
433 dump_partition("", P);
434 } /* dump_all_partitions */
439 static void dump_split_list(const partition_t *list)
441 const partition_t *p;
443 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
444 for (p = list; p != NULL; p = p->split_next)
445 DB((dbg, LEVEL_2, "part%u, ", p->nr));
446 DB((dbg, LEVEL_2, "\n}\n"));
447 } /* dump_split_list */
450 * Dump partition and type for a node.
452 static int dump_partition_hook(FILE *F, const ir_node *n, const ir_node *local)
454 const ir_node *irn = local != NULL ? local : n;
455 node_t *node = get_irn_node(irn);
457 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
459 } /* dump_partition_hook */
462 #define dump_partition(msg, part)
463 #define dump_race_list(msg, list)
464 #define dump_list(msg, list)
465 #define dump_all_partitions(env)
466 #define dump_split_list(list)
469 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
471 * Verify that a type transition is monotone
473 static void verify_type(const lattice_elem_t old_type, node_t *node)
475 if (old_type.tv == node->type.tv) {
479 if (old_type.tv == tarval_top) {
480 /* from Top down-to is always allowed */
483 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
487 panic("wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
491 #define verify_type(old_type, node)
495 * Compare two pointer values of a listmap.
497 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
499 const listmap_entry_t *e1 = (listmap_entry_t*)elt;
500 const listmap_entry_t *e2 = (listmap_entry_t*)key;
503 return e1->id != e2->id;
504 } /* listmap_cmp_ptr */
507 * Initializes a listmap.
509 * @param map the listmap
511 static void listmap_init(listmap_t *map)
513 map->map = new_set(listmap_cmp_ptr, 16);
518 * Terminates a listmap.
520 * @param map the listmap
522 static void listmap_term(listmap_t *map)
528 * Return the associated listmap entry for a given id.
530 * @param map the listmap
531 * @param id the id to search for
533 * @return the associated listmap entry for the given id
535 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
537 listmap_entry_t key, *entry;
542 entry = set_insert(listmap_entry_t, map->map, &key, sizeof(key), hash_ptr(id));
544 if (entry->list == NULL) {
545 /* a new entry, put into the list */
546 entry->next = map->values;
553 * Calculate the hash value for an opcode map entry.
555 * @param entry an opcode map entry
557 * @return a hash value for the given opcode map entry
559 static unsigned opcode_hash(const opcode_key_t *entry)
561 /* we cannot use the ir ops hash function here, because it hashes the
563 const ir_node *n = entry->irn;
564 ir_opcode code = (ir_opcode)get_irn_opcode(n);
565 ir_mode *mode = get_irn_mode(n);
566 unsigned hash = (unsigned)(PTR_TO_INT(mode) * 9 + code) + get_irn_arity(n);
568 if (code == iro_Const)
569 hash ^= (unsigned)hash_ptr(get_Const_tarval(n));
570 else if (code == iro_Proj)
571 hash += (unsigned)get_Proj_proj(n);
576 * Compare two entries in the opcode map.
578 static int cmp_opcode(const void *elt, const void *key, size_t size)
580 const opcode_key_t *o1 = (opcode_key_t*)elt;
581 const opcode_key_t *o2 = (opcode_key_t*)key;
585 return cmp_irn_opcode(o1->irn, o2->irn);
589 * Compare two Def-Use edges for input position.
591 static int cmp_def_use_edge(const void *a, const void *b)
593 const ir_def_use_edge *ea = (const ir_def_use_edge*)a;
594 const ir_def_use_edge *eb = (const ir_def_use_edge*)b;
596 /* no overrun, because range is [-1, MAXINT] */
597 return ea->pos - eb->pos;
598 } /* cmp_def_use_edge */
601 * We need the Def-Use edges sorted.
603 static void sort_irn_outs(node_t *node)
605 ir_node *irn = node->node;
606 int n_outs = get_irn_n_outs(irn);
609 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
611 node->max_user_input = irn->out[n_outs].pos;
612 } /* sort_irn_outs */
615 * Return the type of a node.
617 * @param irn an IR-node
619 * @return the associated type of this node
621 static inline lattice_elem_t get_node_type(const ir_node *irn)
623 return get_irn_node(irn)->type;
624 } /* get_node_type */
627 * Return the tarval of a node.
629 * @param irn an IR-node
631 * @return the associated type of this node
633 static inline ir_tarval *get_node_tarval(const ir_node *irn)
635 lattice_elem_t type = get_node_type(irn);
637 if (is_tarval(type.tv))
639 return tarval_bottom;
640 } /* get_node_type */
643 * Add a partition to the worklist.
645 static inline void add_to_worklist(partition_t *X, environment_t *env)
647 assert(X->on_worklist == 0);
648 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
649 X->wl_next = env->worklist;
652 } /* add_to_worklist */
655 * Create a new empty partition.
657 * @param env the environment
659 * @return a newly allocated partition
661 static inline partition_t *new_partition(environment_t *env)
663 partition_t *part = OALLOC(&env->obst, partition_t);
665 INIT_LIST_HEAD(&part->Leader);
666 INIT_LIST_HEAD(&part->Follower);
667 INIT_LIST_HEAD(&part->cprop);
668 INIT_LIST_HEAD(&part->cprop_X);
669 part->wl_next = NULL;
670 part->touched_next = NULL;
671 part->cprop_next = NULL;
672 part->split_next = NULL;
673 part->touched = NULL;
676 part->max_user_inputs = 0;
677 part->on_worklist = 0;
678 part->on_touched = 0;
680 part->type_is_T_or_C = 0;
682 part->dbg_next = env->dbg_list;
683 env->dbg_list = part;
684 part->nr = part_nr++;
688 } /* new_partition */
691 * Get the first node from a partition.
693 static inline node_t *get_first_node(const partition_t *X)
695 return list_entry(X->Leader.next, node_t, node_list);
696 } /* get_first_node */
699 * Return the type of a partition (assuming partition is non-empty and
700 * all elements have the same type).
702 * @param X a partition
704 * @return the type of the first element of the partition
706 static inline lattice_elem_t get_partition_type(const partition_t *X)
708 const node_t *first = get_first_node(X);
710 } /* get_partition_type */
713 * Creates a partition node for the given IR-node and place it
714 * into the given partition.
716 * @param irn an IR-node
717 * @param part a partition to place the node in
718 * @param env the environment
720 * @return the created node
722 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env)
724 /* create a partition node and place it in the partition */
725 node_t *node = OALLOC(&env->obst, node_t);
727 INIT_LIST_HEAD(&node->node_list);
728 INIT_LIST_HEAD(&node->cprop_list);
732 node->race_next = NULL;
733 node->type.tv = tarval_top;
734 node->max_user_input = 0;
736 node->n_followers = 0;
737 node->on_touched = 0;
740 node->is_follower = 0;
742 set_irn_node(irn, node);
744 list_add_tail(&node->node_list, &part->Leader);
748 } /* create_partition_node */
751 * Pre-Walker, initialize all Nodes' type to U or top and place
752 * all nodes into the TOP partition.
754 static void create_initial_partitions(ir_node *irn, void *ctx)
756 environment_t *env = (environment_t*)ctx;
757 partition_t *part = env->initial;
760 node = create_partition_node(irn, part, env);
762 if (node->max_user_input > part->max_user_inputs)
763 part->max_user_inputs = node->max_user_input;
766 set_Block_phis(irn, NULL);
768 } /* create_initial_partitions */
771 * Post-Walker, collect all Block-Phi lists, set Cond.
773 static void init_block_phis(ir_node *irn, void *ctx)
778 ir_node *block = get_nodes_block(irn);
779 add_Block_phi(block, irn);
781 } /* init_block_phis */
784 * Add a node to the entry.partition.touched set and
785 * node->partition to the touched set if not already there.
788 * @param env the environment
790 static inline void add_to_touched(node_t *y, environment_t *env)
792 if (y->on_touched == 0) {
793 partition_t *part = y->part;
795 y->next = part->touched;
800 if (part->on_touched == 0) {
801 part->touched_next = env->touched;
803 part->on_touched = 1;
806 check_list(part->touched, part);
808 } /* add_to_touched */
811 * Place a node on the cprop list.
814 * @param env the environment
816 static void add_to_cprop(node_t *y, environment_t *env)
820 /* Add y to y.partition.cprop. */
821 if (y->on_cprop == 0) {
822 partition_t *Y = y->part;
823 ir_node *irn = y->node;
824 ir_node *skipped = skip_Proj(irn);
826 /* place Conds and all its Projs on the cprop_X list */
827 if (is_Cond(skipped) || is_Switch(skipped))
828 list_add_tail(&y->cprop_list, &Y->cprop_X);
830 list_add_tail(&y->cprop_list, &Y->cprop);
833 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
835 /* place its partition on the cprop list */
836 if (Y->on_cprop == 0) {
837 Y->cprop_next = env->cprop;
843 if (get_irn_mode(irn) == mode_T) {
844 /* mode_T nodes always produce tarval_bottom, so we must explicitly
845 * add its Projs to get constant evaluation to work */
848 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
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;
892 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
893 node_t *pred = get_irn_node(get_irn_n(irn, i));
898 n = get_irn_n_outs(p);
899 for (j = 1; j <= pred->n_followers; ++j) {
900 if (p->out[j].pos == i && p->out[j].use == irn) {
901 /* found a follower edge to x, move it to the Leader */
902 ir_def_use_edge edge = p->out[j];
904 /* remove this edge from the Follower set */
905 p->out[j] = p->out[pred->n_followers];
908 /* sort it into the leader set */
909 for (k = pred->n_followers + 2; k <= n; ++k) {
910 if (p->out[k].pos >= edge.pos)
912 p->out[k - 1] = p->out[k];
914 /* place the new edge here */
915 p->out[k - 1] = edge;
917 /* edge found and moved */
922 } /* move_edges_to_leader */
925 * Split a partition that has NO followers by a local list.
927 * @param Z partition to split
928 * @param g a (non-empty) node list
929 * @param env the environment
931 * @return a new partition containing the nodes of g
933 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env)
935 partition_t *Z_prime;
940 dump_partition("Splitting ", Z);
941 dump_list("by list ", g);
945 /* Remove g from Z. */
946 for (node = g; node != NULL; node = node->next) {
947 assert(node->part == Z);
948 list_del(&node->node_list);
951 assert(n < Z->n_leader);
954 /* Move g to a new partition, Z'. */
955 Z_prime = new_partition(env);
957 for (node = g; node != NULL; node = node->next) {
958 list_add_tail(&node->node_list, &Z_prime->Leader);
959 node->part = Z_prime;
960 if (node->max_user_input > max_input)
961 max_input = node->max_user_input;
963 Z_prime->max_user_inputs = max_input;
964 Z_prime->n_leader = n;
967 check_partition(Z_prime);
969 /* for now, copy the type info tag, it will be adjusted in split_by(). */
970 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
972 update_worklist(Z, Z_prime, env);
974 dump_partition("Now ", Z);
975 dump_partition("Created new ", Z_prime);
977 } /* split_no_followers */
980 * Make the Follower -> Leader transition for a node.
984 static void follower_to_leader(node_t *n)
986 assert(n->is_follower == 1);
988 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
990 move_edges_to_leader(n);
991 list_del(&n->node_list);
992 list_add_tail(&n->node_list, &n->part->Leader);
994 } /* follower_to_leader */
997 * The environment for one race step.
999 typedef struct step_env {
1000 node_t *initial; /**< The initial node list. */
1001 node_t *unwalked; /**< The unwalked node list. */
1002 node_t *walked; /**< The walked node list. */
1003 int index; /**< Next index of Follower use_def edge. */
1004 unsigned side; /**< side number. */
1008 * Return non-zero, if a input is a real follower
1010 * @param irn the node to check
1011 * @param input number of the input
1013 static int is_real_follower(const ir_node *irn, int input)
1017 switch (get_irn_opcode(irn)) {
1020 /* ignore the Confirm bound input */
1026 /* ignore the Mux sel input */
1031 /* dead inputs are not follower edges */
1032 ir_node *block = get_nodes_block(irn);
1033 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
1035 if (pred->type.tv == tarval_unreachable)
1045 /* only a Sub x,0 / Shift x,0 might be a follower */
1052 pred = get_irn_node(get_irn_n(irn, input));
1053 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1057 pred = get_irn_node(get_irn_n(irn, input));
1058 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1062 pred = get_irn_node(get_irn_n(irn, input));
1063 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1067 assert(!"opcode not implemented yet");
1071 } /* is_real_follower */
1074 * Do one step in the race.
1076 static int step(step_env *env)
1080 if (env->initial != NULL) {
1081 /* Move node from initial to unwalked */
1083 env->initial = n->race_next;
1085 n->race_next = env->unwalked;
1091 while (env->unwalked != NULL) {
1092 /* let n be the first node in unwalked */
1094 while (env->index < n->n_followers) {
1095 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1097 /* let m be n.F.def_use[index] */
1098 node_t *m = get_irn_node(edge->use);
1100 assert(m->is_follower);
1102 * Some inputs, like the get_Confirm_bound are NOT
1103 * real followers, sort them out.
1105 if (! is_real_follower(m->node, edge->pos)) {
1111 /* only followers from our partition */
1112 if (m->part != n->part)
1115 if ((m->flagged & env->side) == 0) {
1116 m->flagged |= env->side;
1118 if (m->flagged != 3) {
1119 /* visited the first time */
1120 /* add m to unwalked not as first node (we might still need to
1121 check for more follower node */
1122 m->race_next = n->race_next;
1126 /* else already visited by the other side and on the other list */
1129 /* move n to walked */
1130 env->unwalked = n->race_next;
1131 n->race_next = env->walked;
1139 * Clear the flags from a list and check for
1140 * nodes that where touched from both sides.
1142 * @param list the list
1144 static int clear_flags(node_t *list)
1149 for (n = list; n != NULL; n = n->race_next) {
1150 if (n->flagged == 3) {
1151 /* we reach a follower from both sides, this will split congruent
1152 * inputs and make it a leader. */
1153 follower_to_leader(n);
1162 * Split a partition by a local list using the race.
1164 * @param pX pointer to the partition to split, might be changed!
1165 * @param gg a (non-empty) node list
1166 * @param env the environment
1168 * @return a new partition containing the nodes of gg
1170 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env)
1172 partition_t *X = *pX;
1173 partition_t *X_prime;
1177 int max_input, transitions, winner, shf;
1179 DEBUG_ONLY(static int run = 0;)
1181 DB((dbg, LEVEL_2, "Run %d ", run++));
1182 if (list_empty(&X->Follower)) {
1183 /* if the partition has NO follower, we can use the fast
1184 splitting algorithm. */
1185 return split_no_followers(X, gg, env);
1187 /* else do the race */
1189 dump_partition("Splitting ", X);
1190 dump_list("by list ", gg);
1192 INIT_LIST_HEAD(&tmp);
1194 /* Remove gg from X.Leader and put into g */
1196 for (node_t *node = gg; node != NULL; node = node->next) {
1197 assert(node->part == X);
1198 assert(node->is_follower == 0);
1200 list_del(&node->node_list);
1201 list_add_tail(&node->node_list, &tmp);
1202 node->race_next = g;
1207 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1208 node->race_next = h;
1211 /* restore X.Leader */
1212 list_splice(&tmp, &X->Leader);
1214 senv[0].initial = g;
1215 senv[0].unwalked = NULL;
1216 senv[0].walked = NULL;
1220 senv[1].initial = h;
1221 senv[1].unwalked = NULL;
1222 senv[1].walked = NULL;
1227 * Some informations on the race that are not stated clearly in Click's
1229 * 1) A follower stays on the side that reach him first.
1230 * 2) If the other side reches a follower, if will be converted to
1231 * a leader. /This must be done after the race is over, else the
1232 * edges we are iterating on are renumbered./
1233 * 3) /New leader might end up on both sides./
1234 * 4) /If one side ends up with new Leaders, we must ensure that
1235 * they can split out by opcode, hence we have to put _every_
1236 * partition with new Leader nodes on the cprop list, as
1237 * opcode splitting is done by split_by() at the end of
1238 * constant propagation./
1241 if (step(&senv[0])) {
1245 if (step(&senv[1])) {
1250 assert(senv[winner].initial == NULL);
1251 assert(senv[winner].unwalked == NULL);
1253 /* clear flags from walked/unwalked */
1255 transitions = clear_flags(senv[0].unwalked) << shf;
1256 transitions |= clear_flags(senv[0].walked) << shf;
1258 transitions |= clear_flags(senv[1].unwalked) << shf;
1259 transitions |= clear_flags(senv[1].walked) << shf;
1261 dump_race_list("winner ", senv[winner].walked);
1263 /* Move walked_{winner} to a new partition, X'. */
1264 X_prime = new_partition(env);
1267 for (node_t *node = senv[winner].walked; node != NULL; node = node->race_next) {
1268 list_del(&node->node_list);
1269 node->part = X_prime;
1270 if (node->is_follower) {
1271 list_add_tail(&node->node_list, &X_prime->Follower);
1273 list_add_tail(&node->node_list, &X_prime->Leader);
1276 if (node->max_user_input > max_input)
1277 max_input = node->max_user_input;
1279 X_prime->n_leader = n;
1280 X_prime->max_user_inputs = max_input;
1281 X->n_leader -= X_prime->n_leader;
1283 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1284 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1287 * Even if a follower was not checked by both sides, it might have
1288 * loose its congruence, so we need to check this case for all follower.
1290 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1291 if (identity(node) == node) {
1292 follower_to_leader(node);
1298 check_partition(X_prime);
1300 /* X' is the smaller part */
1301 add_to_worklist(X_prime, env);
1304 * If there where follower to leader transitions, ensure that the nodes
1305 * can be split out if necessary.
1307 if (transitions & 1) {
1308 /* place winner partition on the cprop list */
1309 if (X_prime->on_cprop == 0) {
1310 X_prime->cprop_next = env->cprop;
1311 env->cprop = X_prime;
1312 X_prime->on_cprop = 1;
1315 if (transitions & 2) {
1316 /* place other partition on the cprop list */
1317 if (X->on_cprop == 0) {
1318 X->cprop_next = env->cprop;
1324 dump_partition("Now ", X);
1325 dump_partition("Created new ", X_prime);
1327 /* we have to ensure that the partition containing g is returned */
1337 * Returns non-zero if the i'th input of a Phi node is live.
1339 * @param phi a Phi-node
1340 * @param i an input number
1342 * @return non-zero if the i'th input of the given Phi node is live
1344 static int is_live_input(ir_node *phi, int i)
1347 ir_node *block = get_nodes_block(phi);
1348 ir_node *pred = get_Block_cfgpred(block, i);
1349 lattice_elem_t type = get_node_type(pred);
1351 return type.tv != tarval_unreachable;
1353 /* else it's the control input, always live */
1355 } /* is_live_input */
1358 * Return non-zero if a type is a constant.
1360 static int is_constant_type(lattice_elem_t type)
1362 if (type.tv != tarval_bottom && type.tv != tarval_top)
1365 } /* is_constant_type */
1368 * Check whether a type is neither Top or a constant.
1369 * Note: U is handled like Top here, R is a constant.
1371 * @param type the type to check
1373 static int type_is_neither_top_nor_const(const lattice_elem_t type)
1375 if (is_tarval(type.tv)) {
1376 if (type.tv == tarval_top)
1378 if (tarval_is_constant(type.tv))
1385 } /* type_is_neither_top_nor_const */
1388 * Collect nodes to the touched list.
1390 * @param list the list which contains the nodes that must be evaluated
1391 * @param idx the index of the def_use edge to evaluate
1392 * @param env the environment
1394 static void collect_touched(list_head *list, int idx, environment_t *env)
1397 int end_idx = env->end_idx;
1399 list_for_each_entry(node_t, x, list, node_list) {
1403 /* leader edges start AFTER follower edges */
1404 x->next_edge = x->n_followers + 1;
1406 num_edges = get_irn_n_outs(x->node);
1408 /* for all edges in x.L.def_use_{idx} */
1409 while (x->next_edge <= num_edges) {
1410 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1413 /* check if we have necessary edges */
1414 if (edge->pos > idx)
1421 /* only non-commutative nodes */
1422 if (env->commutative &&
1423 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1426 /* ignore the "control input" for non-pinned nodes
1427 if we are running in GCSE mode */
1428 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1431 y = get_irn_node(succ);
1432 assert(get_irn_n(succ, idx) == x->node);
1434 /* ignore block edges touching followers */
1435 if (idx == -1 && y->is_follower)
1438 if (is_constant_type(y->type)) {
1439 unsigned code = get_irn_opcode(succ);
1440 if (code == iro_Sub || code == iro_Cmp)
1441 add_to_cprop(y, env);
1444 /* Partitions of constants should not be split simply because their Nodes have unequal
1445 functions or incongruent inputs. */
1446 if (type_is_neither_top_nor_const(y->type) &&
1447 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1448 add_to_touched(y, env);
1452 } /* collect_touched */
1455 * Collect commutative nodes to the touched list.
1457 * @param list the list which contains the nodes that must be evaluated
1458 * @param env the environment
1460 static void collect_commutative_touched(list_head *list, environment_t *env)
1464 list_for_each_entry(node_t, x, list, node_list) {
1467 num_edges = get_irn_n_outs(x->node);
1469 x->next_edge = x->n_followers + 1;
1471 /* for all edges in x.L.def_use_{idx} */
1472 while (x->next_edge <= num_edges) {
1473 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1476 /* check if we have necessary edges */
1486 /* only commutative nodes */
1487 if (!is_op_commutative(get_irn_op(succ)))
1490 y = get_irn_node(succ);
1491 if (is_constant_type(y->type)) {
1492 unsigned code = get_irn_opcode(succ);
1493 if (code == iro_Eor)
1494 add_to_cprop(y, env);
1497 /* Partitions of constants should not be split simply because their Nodes have unequal
1498 functions or incongruent inputs. */
1499 if (type_is_neither_top_nor_const(y->type)) {
1500 add_to_touched(y, env);
1504 } /* collect_commutative_touched */
1507 * Split the partitions if caused by the first entry on the worklist.
1509 * @param env the environment
1511 static void cause_splits(environment_t *env)
1513 partition_t *X, *Z, *N;
1516 /* remove the first partition from the worklist */
1518 env->worklist = X->wl_next;
1521 dump_partition("Cause_split: ", X);
1523 if (env->commutative) {
1524 /* handle commutative nodes first */
1526 /* empty the touched set: already done, just clear the list */
1527 env->touched = NULL;
1529 collect_commutative_touched(&X->Leader, env);
1530 collect_commutative_touched(&X->Follower, env);
1532 for (Z = env->touched; Z != NULL; Z = N) {
1534 node_t *touched = Z->touched;
1535 node_t *touched_aa = NULL;
1536 node_t *touched_ab = NULL;
1537 unsigned n_touched_aa = 0;
1538 unsigned n_touched_ab = 0;
1540 assert(Z->touched != NULL);
1542 /* beware, split might change Z */
1543 N = Z->touched_next;
1545 /* remove it from the touched set */
1548 /* Empty local Z.touched. */
1549 for (e = touched; e != NULL; e = n) {
1550 node_t *left = get_irn_node(get_irn_n(e->node, 0));
1551 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1553 assert(e->is_follower == 0);
1558 * Note: op(a, a) is NOT congruent to op(a, b).
1559 * So, we must split the touched list.
1561 if (left->part == right->part) {
1562 e->next = touched_aa;
1566 e->next = touched_ab;
1571 assert(n_touched_aa + n_touched_ab == Z->n_touched);
1575 if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1576 partition_t *Z_prime = Z;
1577 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1578 split(&Z_prime, touched_aa, env);
1580 assert(n_touched_aa <= Z->n_leader);
1582 if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1583 partition_t *Z_prime = Z;
1584 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1585 split(&Z_prime, touched_ab, env);
1587 assert(n_touched_ab <= Z->n_leader);
1591 /* combine temporary leader and follower list */
1592 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1593 /* empty the touched set: already done, just clear the list */
1594 env->touched = NULL;
1596 collect_touched(&X->Leader, idx, env);
1597 collect_touched(&X->Follower, idx, env);
1599 for (Z = env->touched; Z != NULL; Z = N) {
1601 node_t *touched = Z->touched;
1602 unsigned n_touched = Z->n_touched;
1604 assert(Z->touched != NULL);
1606 /* beware, split might change Z */
1607 N = Z->touched_next;
1609 /* remove it from the touched set */
1612 /* Empty local Z.touched. */
1613 for (e = touched; e != NULL; e = e->next) {
1614 assert(e->is_follower == 0);
1620 if (0 < n_touched && n_touched < Z->n_leader) {
1621 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1622 split(&Z, touched, env);
1624 assert(n_touched <= Z->n_leader);
1627 } /* cause_splits */
1630 * Implements split_by_what(): Split a partition by characteristics given
1631 * by the what function.
1633 * @param X the partition to split
1634 * @param What a function returning an Id for every node of the partition X
1635 * @param P a list to store the result partitions
1636 * @param env the environment
1640 static partition_t *split_by_what(partition_t *X, what_func What,
1641 partition_t **P, environment_t *env)
1645 listmap_entry_t *iter;
1648 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1650 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1651 void *id = What(x, env);
1652 listmap_entry_t *entry;
1655 /* input not allowed, ignore */
1658 /* Add x to map[What(x)]. */
1659 entry = listmap_find(&map, id);
1660 x->next = entry->list;
1663 /* Let P be a set of Partitions. */
1665 /* for all sets S except one in the range of map do */
1666 for (iter = map.values; iter != NULL; iter = iter->next) {
1667 if (iter->next == NULL) {
1668 /* this is the last entry, ignore */
1673 /* Add SPLIT( X, S ) to P. */
1674 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1675 R = split(&X, S, env);
1685 } /* split_by_what */
1687 /** lambda n.(n.type) */
1688 static void *lambda_type(const node_t *node, environment_t *env)
1691 return node->type.tv;
1694 /** lambda n.(n.opcode) */
1695 static void *lambda_opcode(const node_t *node, environment_t *env)
1697 opcode_key_t key, *entry;
1699 key.irn = node->node;
1701 entry = set_insert(opcode_key_t, env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1703 } /* lambda_opcode */
1705 /** lambda n.(n[i].partition) */
1706 static void *lambda_partition(const node_t *node, environment_t *env)
1708 ir_node *skipped = skip_Proj(node->node);
1711 int i = env->lambda_input;
1713 if (i >= get_irn_arity(node->node)) {
1715 * We are outside the allowed range: This can happen even
1716 * if we have split by opcode first: doing so might move Followers
1717 * to Leaders and those will have a different opcode!
1718 * Note that in this case the partition is on the cprop list and will be
1724 /* ignore the "control input" for non-pinned nodes
1725 if we are running in GCSE mode */
1726 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1729 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1730 p = get_irn_node(pred);
1732 } /* lambda_partition */
1734 /** lambda n.(n[i].partition) for commutative nodes */
1735 static void *lambda_commutative_partition(const node_t *node, environment_t *env)
1737 ir_node *irn = node->node;
1738 ir_node *skipped = skip_Proj(irn);
1739 ir_node *pred, *left, *right;
1741 partition_t *pl, *pr;
1742 int i = env->lambda_input;
1744 if (i >= get_irn_arity(node->node)) {
1746 * We are outside the allowed range: This can happen even
1747 * if we have split by opcode first: doing so might move Followers
1748 * to Leaders and those will have a different opcode!
1749 * Note that in this case the partition is on the cprop list and will be
1755 /* ignore the "control input" for non-pinned nodes
1756 if we are running in GCSE mode */
1757 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1761 pred = get_irn_n(skipped, i);
1762 p = get_irn_node(pred);
1766 if (is_op_commutative(get_irn_op(irn))) {
1767 /* normalize partition order by returning the "smaller" on input 0,
1768 the "bigger" on input 1. */
1769 left = get_binop_left(irn);
1770 pl = get_irn_node(left)->part;
1771 right = get_binop_right(irn);
1772 pr = get_irn_node(right)->part;
1775 return pl < pr ? pl : pr;
1777 return pl > pr ? pl : pr;
1779 /* a not split out Follower */
1780 pred = get_irn_n(irn, i);
1781 p = get_irn_node(pred);
1785 } /* lambda_commutative_partition */
1788 * Returns true if a type is a constant (and NOT Top
1791 static int is_con(const lattice_elem_t type)
1793 /* be conservative */
1794 if (is_tarval(type.tv))
1795 return tarval_is_constant(type.tv);
1796 return is_entity(type.sym.entity_p);
1800 * Implements split_by().
1802 * @param X the partition to split
1803 * @param env the environment
1805 static void split_by(partition_t *X, environment_t *env)
1807 partition_t *I, *P = NULL;
1810 dump_partition("split_by", X);
1812 if (X->n_leader == 1) {
1813 /* we have only one leader, no need to split, just check its type */
1814 node_t *x = get_first_node(X);
1815 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1819 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1820 P = split_by_what(X, lambda_type, &P, env);
1823 /* adjust the type tags, we have split partitions by type */
1824 for (I = P; I != NULL; I = I->split_next) {
1825 node_t *x = get_first_node(I);
1826 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1833 if (Y->n_leader > 1) {
1834 /* we do not want split the TOP or constant partitions */
1835 if (! Y->type_is_T_or_C) {
1836 partition_t *Q = NULL;
1838 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1839 Q = split_by_what(Y, lambda_opcode, &Q, env);
1846 if (Z->n_leader > 1) {
1847 const node_t *first = get_first_node(Z);
1848 int arity = get_irn_arity(first->node);
1850 what_func what = lambda_partition;
1851 DEBUG_ONLY(char buf[64];)
1853 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1854 what = lambda_commutative_partition;
1857 * BEWARE: during splitting by input 2 for instance we might
1858 * create new partitions which are different by input 1, so collect
1859 * them and split further.
1861 Z->split_next = NULL;
1864 for (input = arity - 1; input >= -1; --input) {
1866 partition_t *Z_prime = R;
1869 if (Z_prime->n_leader > 1) {
1870 env->lambda_input = input;
1871 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1872 DEBUG_ONLY(what_reason = buf;)
1873 S = split_by_what(Z_prime, what, &S, env);
1876 Z_prime->split_next = S;
1879 } while (R != NULL);
1884 } while (Q != NULL);
1887 } while (P != NULL);
1891 * (Re-)compute the type for a given node.
1893 * @param node the node
1895 static void default_compute(node_t *node)
1898 ir_node *irn = node->node;
1900 /* if any of the data inputs have type top, the result is type top */
1901 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1902 ir_node *pred = get_irn_n(irn, i);
1903 node_t *p = get_irn_node(pred);
1905 if (p->type.tv == tarval_top) {
1906 node->type.tv = tarval_top;
1911 if (get_irn_mode(node->node) == mode_X)
1912 node->type.tv = tarval_reachable;
1914 node->type.tv = computed_value(irn);
1915 } /* default_compute */
1918 * (Re-)compute the type for a Block node.
1920 * @param node the node
1922 static void compute_Block(node_t *node)
1925 ir_node *block = node->node;
1927 if (block == get_irg_start_block(current_ir_graph) || get_Block_entity(block) != NULL) {
1928 /* start block and labelled blocks are always reachable */
1929 node->type.tv = tarval_reachable;
1933 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1934 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1936 if (pred->type.tv == tarval_reachable) {
1937 /* A block is reachable, if at least of predecessor is reachable. */
1938 node->type.tv = tarval_reachable;
1942 node->type.tv = tarval_top;
1943 } /* compute_Block */
1946 * (Re-)compute the type for a Bad node.
1948 * @param node the node
1950 static void compute_Bad(node_t *node)
1952 /* Bad nodes ALWAYS compute Top */
1953 node->type.tv = tarval_top;
1957 * (Re-)compute the type for an Unknown node.
1959 * @param node the node
1961 static void compute_Unknown(node_t *node)
1963 /* While Unknown nodes should compute Top this is dangerous:
1964 * a Top input to a Cond would lead to BOTH control flows unreachable.
1965 * While this is correct in the given semantics, it would destroy the Firm
1968 * It would be safe to compute Top IF it can be assured, that only Cmp
1969 * nodes are inputs to Conds. We check that first.
1970 * This is the way Frontends typically build Firm, but some optimizations
1971 * (jump threading for instance) might replace them by Phib's...
1973 node->type.tv = tarval_UNKNOWN;
1974 } /* compute_Unknown */
1977 * (Re-)compute the type for a Jmp node.
1979 * @param node the node
1981 static void compute_Jmp(node_t *node)
1983 node_t *block = get_irn_node(get_nodes_block(node->node));
1985 node->type = block->type;
1989 * (Re-)compute the type for the Return node.
1991 * @param node the node
1993 static void compute_Return(node_t *node)
1995 /* The Return node is NOT dead if it is in a reachable block.
1996 * This is already checked in compute(). so we can return
1997 * Reachable here. */
1998 node->type.tv = tarval_reachable;
1999 } /* compute_Return */
2002 * (Re-)compute the type for the End node.
2004 * @param node the node
2006 static void compute_End(node_t *node)
2008 /* the End node is NOT dead of course */
2009 node->type.tv = tarval_reachable;
2013 * (Re-)compute the type for a Call.
2015 * @param node the node
2017 static void compute_Call(node_t *node)
2020 * A Call computes always bottom, even if it has Unknown
2023 node->type.tv = tarval_bottom;
2024 } /* compute_Call */
2027 * (Re-)compute the type for a SymConst node.
2029 * @param node the node
2031 static void compute_SymConst(node_t *node)
2033 ir_node *irn = node->node;
2034 node_t *block = get_irn_node(get_nodes_block(irn));
2036 if (block->type.tv == tarval_unreachable) {
2037 node->type.tv = tarval_top;
2040 switch (get_SymConst_kind(irn)) {
2041 case symconst_addr_ent:
2042 node->type.sym = get_SymConst_symbol(irn);
2045 node->type.tv = computed_value(irn);
2047 } /* compute_SymConst */
2050 * (Re-)compute the type for a Phi node.
2052 * @param node the node
2054 static void compute_Phi(node_t *node)
2057 ir_node *phi = node->node;
2058 lattice_elem_t type;
2060 /* if a Phi is in a unreachable block, its type is TOP */
2061 node_t *block = get_irn_node(get_nodes_block(phi));
2063 if (block->type.tv == tarval_unreachable) {
2064 node->type.tv = tarval_top;
2068 /* Phi implements the Meet operation */
2069 type.tv = tarval_top;
2070 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2071 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2072 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2074 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2075 /* ignore TOP inputs: We must check here for unreachable blocks,
2076 because Firm constants live in the Start Block are NEVER Top.
2077 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2078 comes from a unreachable input. */
2081 if (pred->type.tv == tarval_bottom) {
2082 node->type.tv = tarval_bottom;
2084 } else if (type.tv == tarval_top) {
2085 /* first constant found */
2087 } else if (type.tv != pred->type.tv) {
2088 /* different constants or tarval_bottom */
2089 node->type.tv = tarval_bottom;
2092 /* else nothing, constants are the same */
2098 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2100 * @param node the node
2102 static void compute_Add(node_t *node)
2104 ir_node *sub = node->node;
2105 node_t *l = get_irn_node(get_Add_left(sub));
2106 node_t *r = get_irn_node(get_Add_right(sub));
2107 lattice_elem_t a = l->type;
2108 lattice_elem_t b = r->type;
2111 if (a.tv == tarval_top || b.tv == tarval_top) {
2112 node->type.tv = tarval_top;
2113 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2114 node->type.tv = tarval_bottom;
2116 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2117 must call tarval_add() first to handle this case! */
2118 if (is_tarval(a.tv)) {
2119 if (is_tarval(b.tv)) {
2120 node->type.tv = tarval_add(a.tv, b.tv);
2123 mode = get_tarval_mode(a.tv);
2124 if (a.tv == get_mode_null(mode)) {
2128 } else if (is_tarval(b.tv)) {
2129 mode = get_tarval_mode(b.tv);
2130 if (b.tv == get_mode_null(mode)) {
2135 node->type.tv = tarval_bottom;
2140 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2142 * @param node the node
2144 static void compute_Sub(node_t *node)
2146 ir_node *sub = node->node;
2147 node_t *l = get_irn_node(get_Sub_left(sub));
2148 node_t *r = get_irn_node(get_Sub_right(sub));
2149 lattice_elem_t a = l->type;
2150 lattice_elem_t b = r->type;
2153 if (a.tv == tarval_top || b.tv == tarval_top) {
2154 node->type.tv = tarval_top;
2155 } else if (is_con(a) && is_con(b)) {
2156 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2157 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2158 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2160 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2163 node->type.tv = tarval_bottom;
2165 } else if (r->part == l->part &&
2166 (!mode_is_float(get_irn_mode(l->node)))) {
2168 * BEWARE: a - a is NOT always 0 for floating Point values, as
2169 * NaN op NaN = NaN, so we must check this here.
2171 ir_mode *mode = get_irn_mode(sub);
2172 tv = get_mode_null(mode);
2174 /* if the node was ONCE evaluated by all constants, but now
2175 this breaks AND we get from the argument partitions a different
2176 result, switch to bottom.
2177 This happens because initially all nodes are in the same partition ... */
2178 if (node->type.tv != tv)
2182 node->type.tv = tarval_bottom;
2187 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2189 * @param node the node
2191 static void compute_Eor(node_t *node)
2193 ir_node *eor = node->node;
2194 node_t *l = get_irn_node(get_Eor_left(eor));
2195 node_t *r = get_irn_node(get_Eor_right(eor));
2196 lattice_elem_t a = l->type;
2197 lattice_elem_t b = r->type;
2200 if (a.tv == tarval_top || b.tv == tarval_top) {
2201 node->type.tv = tarval_top;
2202 } else if (is_con(a) && is_con(b)) {
2203 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2204 node->type.tv = tarval_eor(a.tv, b.tv);
2205 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2207 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2210 node->type.tv = tarval_bottom;
2212 } else if (r->part == l->part) {
2213 ir_mode *mode = get_irn_mode(eor);
2214 tv = get_mode_null(mode);
2216 /* if the node was ONCE evaluated by all constants, but now
2217 this breaks AND we get from the argument partitions a different
2218 result, switch to bottom.
2219 This happens because initially all nodes are in the same partition ... */
2220 if (node->type.tv != tv)
2224 node->type.tv = tarval_bottom;
2229 * (Re-)compute the type for Cmp.
2231 * @param node the node
2233 static void compute_Cmp(node_t *node)
2235 ir_node *cmp = node->node;
2236 node_t *l = get_irn_node(get_Cmp_left(cmp));
2237 node_t *r = get_irn_node(get_Cmp_right(cmp));
2238 lattice_elem_t a = l->type;
2239 lattice_elem_t b = r->type;
2240 ir_relation relation = get_Cmp_relation(cmp);
2243 if (a.tv == tarval_top || b.tv == tarval_top) {
2244 node->type.tv = tarval_undefined;
2245 } else if (is_con(a) && is_con(b)) {
2246 default_compute(node);
2249 * BEWARE: a == a is NOT always True for floating Point values, as
2250 * NaN != NaN is defined, so we must check this here.
2251 * (while for some pnc we could still optimize we have to stay
2252 * consistent with compute_Cmp, so don't do anything for floats)
2254 } else if (r->part == l->part && !mode_is_float(get_irn_mode(l->node))) {
2255 tv = relation & ir_relation_equal ? tarval_b_true : tarval_b_false;
2257 /* if the node was ONCE evaluated to a constant, but now
2258 this breaks AND we get from the argument partitions a different
2259 result, ensure monotony by fall to bottom.
2260 This happens because initially all nodes are in the same partition ... */
2261 if (node->type.tv == tarval_bottom)
2263 else if (node->type.tv != tv && is_constant_type(node->type))
2267 node->type.tv = tarval_bottom;
2272 * (Re-)compute the type for a Proj(Cond).
2274 * @param node the node
2275 * @param cond the predecessor Cond node
2277 static void compute_Proj_Cond(node_t *node, ir_node *cond)
2279 ir_node *proj = node->node;
2280 long pnc = get_Proj_proj(proj);
2281 ir_node *sel = get_Cond_selector(cond);
2282 node_t *selector = get_irn_node(sel);
2285 * Note: it is crucial for the monotony that the Proj(Cond)
2286 * are evaluates after all predecessors of the Cond selector are
2292 * Due to the fact that 0 is a const, the Cmp gets immediately
2293 * on the cprop list. It will be evaluated before x is evaluated,
2294 * might leaving x as Top. When later x is evaluated, the Cmp
2295 * might change its value.
2296 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2297 * gets R, and later changed to F if Cmp is evaluated to True!
2299 * We prevent this by putting Conds in an extra cprop_X queue, which
2300 * gets evaluated after the cprop queue is empty.
2302 * Note that this even happens with Click's original algorithm, if
2303 * Cmp(x, 0) is evaluated to True first and later changed to False
2304 * if x was Top first and later changed to a Const ...
2305 * It is unclear how Click solved that problem ...
2307 * However, in rare cases even this does not help, if a Top reaches
2308 * a compare through a Phi, than Proj(Cond) is evaluated changing
2309 * the type of the Phi to something other.
2310 * So, we take the last resort and bind the type to R once
2313 * (This might be even the way Click works around the whole problem).
2315 * Finally, we may miss some optimization possibilities due to this:
2320 * If Top reaches the if first, than we decide for != here.
2321 * If y later is evaluated to 0, we cannot revert this decision
2322 * and must live with both outputs enabled. If this happens,
2323 * we get an unresolved if (true) in the code ...
2325 * In Click's version where this decision is done at the Cmp,
2326 * the Cmp is NOT optimized away than (if y evaluated to 1
2327 * for instance) and we get a if (1 == 0) here ...
2329 * Both solutions are suboptimal.
2330 * At least, we could easily detect this problem and run
2331 * cf_opt() (or even combo) again :-(
2333 if (node->type.tv == tarval_reachable)
2336 if (pnc == pn_Cond_true) {
2337 if (selector->type.tv == tarval_b_false) {
2338 node->type.tv = tarval_unreachable;
2339 } else if (selector->type.tv == tarval_b_true) {
2340 node->type.tv = tarval_reachable;
2341 } else if (selector->type.tv == tarval_bottom) {
2342 node->type.tv = tarval_reachable;
2344 assert(selector->type.tv == tarval_top);
2345 if (tarval_UNKNOWN == tarval_top) {
2346 /* any condition based on Top is "!=" */
2347 node->type.tv = tarval_unreachable;
2349 node->type.tv = tarval_unreachable;
2353 assert(pnc == pn_Cond_false);
2355 if (selector->type.tv == tarval_b_false) {
2356 node->type.tv = tarval_reachable;
2357 } else if (selector->type.tv == tarval_b_true) {
2358 node->type.tv = tarval_unreachable;
2359 } else if (selector->type.tv == tarval_bottom) {
2360 node->type.tv = tarval_reachable;
2362 assert(selector->type.tv == tarval_top);
2363 if (tarval_UNKNOWN == tarval_top) {
2364 /* any condition based on Top is "!=" */
2365 node->type.tv = tarval_reachable;
2367 node->type.tv = tarval_unreachable;
2371 } /* compute_Proj_Cond */
2373 static void compute_Proj_Switch(node_t *node, ir_node *switchn)
2375 ir_node *proj = node->node;
2376 long pnc = get_Proj_proj(proj);
2377 ir_node *sel = get_Switch_selector(switchn);
2378 node_t *selector = get_irn_node(sel);
2380 /* see long comment in compute_Proj_Cond */
2381 if (node->type.tv == tarval_reachable)
2384 if (selector->type.tv == tarval_bottom) {
2385 node->type.tv = tarval_reachable;
2386 } else if (selector->type.tv == tarval_top) {
2387 if (tarval_UNKNOWN == tarval_top && pnc == pn_Switch_default) {
2388 /* a switch based of Top is always "default" */
2389 node->type.tv = tarval_reachable;
2391 node->type.tv = tarval_unreachable;
2394 long value = get_tarval_long(selector->type.tv);
2395 const ir_switch_table *table = get_Switch_table(switchn);
2396 size_t n_entries = ir_switch_table_get_n_entries(table);
2399 for (e = 0; e < n_entries; ++e) {
2400 const ir_switch_table_entry *entry
2401 = ir_switch_table_get_entry_const(table, e);
2402 ir_tarval *min = entry->min;
2403 ir_tarval *max = entry->max;
2405 if (selector->type.tv == min) {
2406 node->type.tv = entry->pn == pnc
2407 ? tarval_reachable : tarval_unreachable;
2411 long minval = get_tarval_long(min);
2412 long maxval = get_tarval_long(max);
2413 if (minval <= value && value <= maxval) {
2414 node->type.tv = entry->pn == pnc
2415 ? tarval_reachable : tarval_unreachable;
2421 /* no entry matched: default */
2423 = pnc == pn_Switch_default ? tarval_reachable : tarval_unreachable;
2428 * (Re-)compute the type for a Proj-Node.
2430 * @param node the node
2432 static void compute_Proj(node_t *node)
2434 ir_node *proj = node->node;
2435 ir_mode *mode = get_irn_mode(proj);
2436 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2437 ir_node *pred = get_Proj_pred(proj);
2439 if (block->type.tv == tarval_unreachable) {
2440 /* a Proj in a unreachable Block stay Top */
2441 node->type.tv = tarval_top;
2444 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred) && !is_Switch(pred)) {
2445 /* if the predecessor is Top, its Proj follow */
2446 node->type.tv = tarval_top;
2450 if (mode == mode_M) {
2451 /* mode M is always bottom */
2452 node->type.tv = tarval_bottom;
2454 } else if (mode == mode_X) {
2455 /* handle mode_X nodes */
2456 switch (get_irn_opcode(pred)) {
2458 /* the Proj_X from the Start is always reachable.
2459 However this is already handled at the top. */
2460 node->type.tv = tarval_reachable;
2463 compute_Proj_Cond(node, pred);
2466 compute_Proj_Switch(node, pred);
2473 default_compute(node);
2474 } /* compute_Proj */
2477 * (Re-)compute the type for a Confirm.
2479 * @param node the node
2481 static void compute_Confirm(node_t *node)
2483 ir_node *confirm = node->node;
2484 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2486 if (get_Confirm_relation(confirm) == ir_relation_equal) {
2487 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2489 if (is_con(bound->type)) {
2490 /* is equal to a constant */
2491 node->type = bound->type;
2495 /* a Confirm is a copy OR a Const */
2496 node->type = pred->type;
2497 } /* compute_Confirm */
2500 * (Re-)compute the type for a given node.
2502 * @param node the node
2504 static void compute(node_t *node)
2506 ir_node *irn = node->node;
2509 #ifndef VERIFY_MONOTONE
2511 * Once a node reaches bottom, the type cannot fall further
2512 * in the lattice and we can stop computation.
2513 * Do not take this exit if the monotony verifier is
2514 * enabled to catch errors.
2516 if (node->type.tv == tarval_bottom)
2520 if (!is_Block(irn)) {
2521 /* for pinned nodes, check its control input */
2522 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2523 node_t *block = get_irn_node(get_nodes_block(irn));
2525 if (block->type.tv == tarval_unreachable) {
2526 node->type.tv = tarval_top;
2532 func = (compute_func)node->node->op->ops.generic;
2538 * Identity functions: Note that one might think that identity() is just a
2539 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2540 * here, because it expects that the identity node is one of the inputs, which is NOT
2541 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2542 * So, we have our own implementation, which copies some parts of equivalent_node()
2546 * Calculates the Identity for Phi nodes
2548 static node_t *identity_Phi(node_t *node)
2550 ir_node *phi = node->node;
2551 ir_node *block = get_nodes_block(phi);
2552 node_t *n_part = NULL;
2555 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2556 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2558 if (pred_X->type.tv == tarval_reachable) {
2559 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2563 else if (n_part->part != pred->part) {
2564 /* incongruent inputs, not a follower */
2569 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2570 * tarval_top, is in the TOP partition and should NOT being split! */
2571 assert(n_part != NULL);
2573 } /* identity_Phi */
2576 * Calculates the Identity for commutative 0 neutral nodes.
2578 static node_t *identity_comm_zero_binop(node_t *node)
2580 ir_node *op = node->node;
2581 node_t *a = get_irn_node(get_binop_left(op));
2582 node_t *b = get_irn_node(get_binop_right(op));
2583 ir_mode *mode = get_irn_mode(op);
2586 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2587 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2590 /* node: no input should be tarval_top, else the binop would be also
2591 * Top and not being split. */
2592 zero = get_mode_null(mode);
2593 if (a->type.tv == zero)
2595 if (b->type.tv == zero)
2598 } /* identity_comm_zero_binop */
2601 * Calculates the Identity for Shift nodes.
2603 static node_t *identity_shift(node_t *node)
2605 ir_node *op = node->node;
2606 node_t *b = get_irn_node(get_binop_right(op));
2607 ir_mode *mode = get_irn_mode(b->node);
2610 /* node: no input should be tarval_top, else the binop would be also
2611 * Top and not being split. */
2612 zero = get_mode_null(mode);
2613 if (b->type.tv == zero)
2614 return get_irn_node(get_binop_left(op));
2616 } /* identity_shift */
2619 * Calculates the Identity for Mul nodes.
2621 static node_t *identity_Mul(node_t *node)
2623 ir_node *op = node->node;
2624 node_t *a = get_irn_node(get_Mul_left(op));
2625 node_t *b = get_irn_node(get_Mul_right(op));
2626 ir_mode *mode = get_irn_mode(op);
2629 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2630 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2633 /* node: no input should be tarval_top, else the binop would be also
2634 * Top and not being split. */
2635 one = get_mode_one(mode);
2636 if (a->type.tv == one)
2638 if (b->type.tv == one)
2641 } /* identity_Mul */
2644 * Calculates the Identity for Sub nodes.
2646 static node_t *identity_Sub(node_t *node)
2648 ir_node *sub = node->node;
2649 node_t *b = get_irn_node(get_Sub_right(sub));
2650 ir_mode *mode = get_irn_mode(sub);
2652 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2653 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2656 /* node: no input should be tarval_top, else the binop would be also
2657 * Top and not being split. */
2658 if (b->type.tv == get_mode_null(mode))
2659 return get_irn_node(get_Sub_left(sub));
2661 } /* identity_Sub */
2664 * Calculates the Identity for And nodes.
2666 static node_t *identity_And(node_t *node)
2668 ir_node *andnode = node->node;
2669 node_t *a = get_irn_node(get_And_left(andnode));
2670 node_t *b = get_irn_node(get_And_right(andnode));
2671 ir_tarval *neutral = get_mode_all_one(get_irn_mode(andnode));
2673 /* node: no input should be tarval_top, else the And would be also
2674 * Top and not being split. */
2675 if (a->type.tv == neutral)
2677 if (b->type.tv == neutral)
2680 } /* identity_And */
2683 * Calculates the Identity for Confirm nodes.
2685 static node_t *identity_Confirm(node_t *node)
2687 ir_node *confirm = node->node;
2689 /* a Confirm is always a Copy */
2690 return get_irn_node(get_Confirm_value(confirm));
2691 } /* identity_Confirm */
2694 * Calculates the Identity for Mux nodes.
2696 static node_t *identity_Mux(node_t *node)
2698 ir_node *mux = node->node;
2699 node_t *t = get_irn_node(get_Mux_true(mux));
2700 node_t *f = get_irn_node(get_Mux_false(mux));
2703 if (t->part == f->part)
2706 /* for now, the 1-input identity is not supported */
2708 sel = get_irn_node(get_Mux_sel(mux));
2710 /* Mux sel input is mode_b, so it is always a tarval */
2711 if (sel->type.tv == tarval_b_true)
2713 if (sel->type.tv == tarval_b_false)
2717 } /* identity_Mux */
2720 * Calculates the Identity for nodes.
2722 static node_t *identity(node_t *node)
2724 ir_node *irn = node->node;
2726 switch (get_irn_opcode(irn)) {
2728 return identity_Phi(node);
2730 return identity_Mul(node);
2734 return identity_comm_zero_binop(node);
2739 return identity_shift(node);
2741 return identity_And(node);
2743 return identity_Sub(node);
2745 return identity_Confirm(node);
2747 return identity_Mux(node);
2754 * Node follower is a (new) follower of leader, segregate Leader
2757 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader)
2759 ir_node *l = leader->node;
2760 int j, i, n = get_irn_n_outs(l);
2762 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2763 /* The leader edges must remain sorted, but follower edges can
2765 for (i = leader->n_followers + 1; i <= n; ++i) {
2766 if (l->out[i].use == follower) {
2767 ir_def_use_edge t = l->out[i];
2769 for (j = i - 1; j >= leader->n_followers + 1; --j)
2770 l->out[j + 1] = l->out[j];
2771 ++leader->n_followers;
2772 l->out[leader->n_followers] = t;
2776 } /* segregate_def_use_chain_1 */
2779 * Node follower is a (new) follower segregate its Leader
2782 * @param follower the follower IR node
2784 static void segregate_def_use_chain(const ir_node *follower)
2788 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2789 node_t *pred = get_irn_node(get_irn_n(follower, i));
2791 segregate_def_use_chain_1(follower, pred);
2793 } /* segregate_def_use_chain */
2796 * Propagate constant evaluation.
2798 * @param env the environment
2800 static void propagate(environment_t *env)
2804 lattice_elem_t old_type;
2806 unsigned n_fallen, old_type_was_T_or_C;
2809 while (env->cprop != NULL) {
2810 void *oldopcode = NULL;
2812 /* remove the first partition X from cprop */
2815 env->cprop = X->cprop_next;
2817 old_type_was_T_or_C = X->type_is_T_or_C;
2819 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2823 int cprop_empty = list_empty(&X->cprop);
2824 int cprop_X_empty = list_empty(&X->cprop_X);
2826 if (cprop_empty && cprop_X_empty) {
2827 /* both cprop lists are empty */
2831 /* remove the first Node x from X.cprop */
2833 /* Get a node from the cprop_X list only if
2834 * all data nodes are processed.
2835 * This ensures, that all inputs of the Cond
2836 * predecessor are processed if its type is still Top.
2838 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2840 x = list_entry(X->cprop.next, node_t, cprop_list);
2843 //assert(x->part == X);
2844 list_del(&x->cprop_list);
2847 if (x->is_follower && identity(x) == x) {
2848 /* check the opcode first */
2849 if (oldopcode == NULL) {
2850 oldopcode = lambda_opcode(get_first_node(X), env);
2852 if (oldopcode != lambda_opcode(x, env)) {
2853 if (x->on_fallen == 0) {
2854 /* different opcode -> x falls out of this partition */
2859 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2863 /* x will make the follower -> leader transition */
2864 follower_to_leader(x);
2867 /* compute a new type for x */
2869 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2871 if (x->type.tv != old_type.tv) {
2872 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2873 verify_type(old_type, x);
2875 if (x->on_fallen == 0) {
2876 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2877 not already on the list. */
2882 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2884 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2885 ir_node *succ = get_irn_out(x->node, i);
2886 node_t *y = get_irn_node(succ);
2888 /* Add y to y.partition.cprop. */
2889 add_to_cprop(y, env);
2894 if (n_fallen > 0 && n_fallen != X->n_leader) {
2895 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2896 Y = split(&X, fallen, env);
2898 * We have split out fallen node. The type of the result
2899 * partition is NOT set yet.
2901 Y->type_is_T_or_C = 0;
2905 /* remove the flags from the fallen list */
2906 for (x = fallen; x != NULL; x = x->next)
2909 if (old_type_was_T_or_C) {
2910 /* check if some nodes will make the leader -> follower transition */
2911 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2912 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2913 node_t *eq_node = identity(y);
2915 if (eq_node != y && eq_node->part == y->part) {
2916 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2917 /* move to Follower */
2919 list_del(&y->node_list);
2920 list_add_tail(&y->node_list, &Y->Follower);
2923 segregate_def_use_chain(y->node);
2933 * Get the leader for a given node from its congruence class.
2935 * @param irn the node
2937 static ir_node *get_leader(node_t *node)
2939 partition_t *part = node->part;
2941 if (part->n_leader > 1 || node->is_follower) {
2942 if (node->is_follower) {
2943 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2946 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2948 return get_first_node(part)->node;
2954 * Returns non-zero if a mode_T node has only one reachable output.
2956 static int only_one_reachable_proj(ir_node *n)
2960 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2961 ir_node *proj = get_irn_out(n, i);
2964 /* skip non-control flow Proj's */
2965 if (get_irn_mode(proj) != mode_X)
2968 node = get_irn_node(proj);
2969 if (node->type.tv == tarval_reachable) {
2975 } /* only_one_reachable_proj */
2978 * Return non-zero if the control flow predecessor node pred
2979 * is the only reachable control flow exit of its block.
2981 * @param pred the control flow exit
2982 * @param block the destination block
2984 static int can_exchange(ir_node *pred, ir_node *block)
2986 if (is_Start(pred) || get_Block_entity(block) != NULL)
2988 else if (is_Jmp(pred))
2990 else if (is_Raise(pred)) {
2991 /* Raise is a tuple and usually has only one reachable ProjX,
2992 * but it must not be eliminated like a Jmp */
2995 else if (get_irn_mode(pred) == mode_T) {
2996 /* if the predecessor block has more than one
2997 reachable outputs we cannot remove the block */
2998 return only_one_reachable_proj(pred);
3001 } /* can_exchange */
3004 * Block Post-Walker, apply the analysis results on control flow by
3005 * shortening Phi's and Block inputs.
3007 static void apply_cf(ir_node *block, void *ctx)
3009 environment_t *env = (environment_t*)ctx;
3010 node_t *node = get_irn_node(block);
3012 ir_node **ins, **in_X;
3013 ir_node *phi, *next;
3015 n = get_Block_n_cfgpreds(block);
3017 if (node->type.tv == tarval_unreachable) {
3020 for (i = n - 1; i >= 0; --i) {
3021 ir_node *pred = get_Block_cfgpred(block, i);
3023 if (! is_Bad(pred)) {
3024 ir_node *pred_block = get_nodes_block(skip_Proj(pred));
3025 if (!is_Bad(pred_block)) {
3026 node_t *pred_bl = get_irn_node(pred_block);
3028 if (pred_bl->flagged == 0) {
3029 pred_bl->flagged = 3;
3031 if (pred_bl->type.tv == tarval_reachable) {
3033 * We will remove an edge from block to its pred.
3034 * This might leave the pred block as an endless loop
3036 if (! is_backedge(block, i))
3037 keep_alive(pred_bl->node);
3044 if (block == get_irg_end_block(current_ir_graph)) {
3045 /* Analysis found out that the end block is unreachable,
3046 * hence we remove all its control flow predecessors. */
3047 set_irn_in(block, 0, NULL);
3053 /* only one predecessor combine */
3054 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3056 if (can_exchange(pred, block)) {
3057 ir_node *new_block = get_nodes_block(pred);
3058 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3059 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3060 exchange(block, new_block);
3061 node->node = new_block;
3067 NEW_ARR_A(ir_node *, in_X, n);
3069 for (i = 0; i < n; ++i) {
3070 ir_node *pred = get_Block_cfgpred(block, i);
3071 node_t *node = get_irn_node(pred);
3073 if (node->type.tv == tarval_reachable) {
3076 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3077 if (! is_Bad(pred)) {
3078 ir_node *pred_block = get_nodes_block(skip_Proj(pred));
3079 if (!is_Bad(pred_block)) {
3080 node_t *pred_bl = get_irn_node(pred_block);
3082 if (!is_Bad(pred_bl->node) && pred_bl->flagged == 0) {
3083 pred_bl->flagged = 3;
3085 if (pred_bl->type.tv == tarval_reachable) {
3087 * We will remove an edge from block to its pred.
3088 * This might leave the pred block as an endless loop
3090 if (! is_backedge(block, i))
3091 keep_alive(pred_bl->node);
3102 NEW_ARR_A(ir_node *, ins, n);
3103 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3104 node_t *node = get_irn_node(phi);
3106 next = get_Phi_next(phi);
3107 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3108 /* this Phi is replaced by a constant */
3109 ir_tarval *tv = node->type.tv;
3110 ir_node *c = new_r_Const(current_ir_graph, tv);
3112 set_irn_node(c, node);
3114 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3115 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3120 for (i = 0; i < n; ++i) {
3121 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3123 if (pred->type.tv == tarval_reachable) {
3124 ins[j++] = get_Phi_pred(phi, i);
3128 /* this Phi is replaced by a single predecessor */
3129 ir_node *s = ins[0];
3130 node_t *phi_node = get_irn_node(phi);
3133 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3134 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3139 set_irn_in(phi, j, ins);
3147 /* this Block has only one live predecessor */
3148 ir_node *pred = skip_Proj(in_X[0]);
3150 if (can_exchange(pred, block)) {
3151 ir_node *new_block = get_nodes_block(pred);
3152 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3153 exchange(block, new_block);
3154 node->node = new_block;
3159 set_irn_in(block, k, in_X);
3164 * Exchange a node by its leader.
3165 * Beware: in rare cases the mode might be wrong here, for instance
3166 * AddP(x, NULL) is a follower of x, but with different mode.
3169 static void exchange_leader(ir_node *irn, ir_node *leader)
3171 ir_mode *mode = get_irn_mode(irn);
3172 if (mode != get_irn_mode(leader)) {
3173 /* The conv is a no-op, so we are free to place it
3174 * either in the block of the leader OR in irn's block.
3175 * Probably placing it into leaders block might reduce
3176 * the number of Conv due to CSE. */
3177 ir_node *block = get_nodes_block(leader);
3178 dbg_info *dbg = get_irn_dbg_info(irn);
3179 ir_node *nlead = new_rd_Conv(dbg, block, leader, mode);
3181 if (nlead != leader) {
3182 /* Note: this newly create irn has no node info because
3183 * it is created after the analysis. However, this node
3184 * replaces the node irn and should not be visited again,
3185 * so set its visited count to the count of irn.
3186 * Otherwise we might visited this node more than once if
3187 * irn had more than one user.
3189 set_irn_node(nlead, NULL);
3190 set_irn_visited(nlead, get_irn_visited(irn));
3194 exchange(irn, leader);
3195 } /* exchange_leader */
3198 * Check, if all users of a mode_M node are dead. Use
3199 * the Def-Use edges for this purpose, as they still
3200 * reflect the situation.
3202 static int all_users_are_dead(const ir_node *irn)
3204 int i, n = get_irn_n_outs(irn);
3206 for (i = 1; i <= n; ++i) {
3207 const ir_node *succ = irn->out[i].use;
3208 const node_t *block = get_irn_node(get_nodes_block(succ));
3211 if (block->type.tv == tarval_unreachable) {
3212 /* block is unreachable */
3215 node = get_irn_node(succ);
3216 if (node->type.tv != tarval_top) {
3217 /* found a reachable user */
3221 /* all users are unreachable */
3223 } /* all_user_are_dead */
3226 * Walker: Find reachable mode_M nodes that have only
3227 * unreachable users. These nodes must be kept later.
3229 static void find_kept_memory(ir_node *irn, void *ctx)
3231 environment_t *env = (environment_t*)ctx;
3232 node_t *node, *block;
3234 if (get_irn_mode(irn) != mode_M)
3237 block = get_irn_node(get_nodes_block(irn));
3238 if (block->type.tv == tarval_unreachable)
3241 node = get_irn_node(irn);
3242 if (node->type.tv == tarval_top)
3245 /* ok, we found a live memory node. */
3246 if (all_users_are_dead(irn)) {
3247 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3248 ARR_APP1(ir_node *, env->kept_memory, irn);
3250 } /* find_kept_memory */
3253 * Post-Walker, apply the analysis results;
3255 static void apply_result(ir_node *irn, void *ctx)
3257 environment_t *env = (environment_t*)ctx;
3258 node_t *node = get_irn_node(irn);
3260 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3261 /* blocks already handled, do not touch the End node */
3263 node_t *block = get_irn_node(get_nodes_block(irn));
3265 if (block->type.tv == tarval_unreachable) {
3266 ir_graph *irg = get_irn_irg(irn);
3267 ir_mode *mode = get_irn_mode(node->node);
3268 ir_node *bad = new_r_Bad(irg, mode);
3270 /* here, bad might already have a node, but this can be safely ignored
3271 as long as bad has at least ONE valid node */
3272 set_irn_node(bad, node);
3274 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3277 } else if (node->type.tv == tarval_top) {
3278 ir_mode *mode = get_irn_mode(irn);
3280 if (mode == mode_M) {
3281 /* never kill a mode_M node */
3283 ir_node *pred = get_Proj_pred(irn);
3284 node_t *pnode = get_irn_node(pred);
3286 if (pnode->type.tv == tarval_top) {
3287 /* skip the predecessor */
3288 ir_node *mem = get_memop_mem(pred);
3290 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3295 /* leave other nodes, especially PhiM */
3296 } else if (mode == mode_T) {
3297 /* Do not kill mode_T nodes, kill their Projs */
3298 } else if (! is_Unknown(irn)) {
3299 /* don't kick away Unknown's, they might be still needed */
3300 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3302 /* control flow should already be handled at apply_cf() */
3303 assert(mode != mode_X);
3305 /* see comment above */
3306 set_irn_node(unk, node);
3308 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3313 else if (get_irn_mode(irn) == mode_X) {
3316 ir_node *cond = get_Proj_pred(irn);
3318 if (is_Cond(cond) || is_Switch(cond)) {
3319 if (only_one_reachable_proj(cond)) {
3320 ir_node *jmp = new_r_Jmp(block->node);
3321 set_irn_node(jmp, node);
3323 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3324 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3328 if (is_Switch(cond)) {
3329 node_t *sel = get_irn_node(get_Switch_selector(cond));
3330 ir_tarval *tv = sel->type.tv;
3332 if (is_tarval(tv) && tarval_is_constant(tv)) {
3333 /* The selector is a constant, but more
3334 * than one output is active: An unoptimized
3343 /* normal data node */
3344 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3345 ir_tarval *tv = node->type.tv;
3348 * Beware: never replace mode_T nodes by constants. Currently we must mark
3349 * mode_T nodes with constants, but do NOT replace them.
3351 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3352 /* can be replaced by a constant */
3353 ir_node *c = new_r_Const(current_ir_graph, tv);
3354 set_irn_node(c, node);
3356 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3357 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3358 exchange_leader(irn, c);
3361 } else if (is_entity(node->type.sym.entity_p)) {
3362 if (! is_SymConst(irn)) {
3363 /* can be replaced by a SymConst */
3364 ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3365 set_irn_node(symc, node);
3368 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3369 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3370 exchange_leader(irn, symc);
3373 } else if (is_Confirm(irn)) {
3374 /* Confirms are always follower, but do not kill them here */
3376 ir_node *leader = get_leader(node);
3378 if (leader != irn) {
3379 int non_strict_phi = 0;
3382 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3383 * as this might create non-strict programs.
3385 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3388 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3389 ir_node *pred = get_Phi_pred(irn, i);
3391 if (is_Unknown(pred)) {
3397 if (! non_strict_phi) {
3398 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3399 if (node->is_follower)
3400 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3402 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3403 exchange_leader(irn, leader);
3410 } /* apply_result */
3413 * Fix the keep-alives by deleting unreachable ones.
3415 static void apply_end(ir_node *end, environment_t *env)
3417 int i, j, n = get_End_n_keepalives(end);
3418 ir_node **in = NULL;
3421 NEW_ARR_A(ir_node *, in, n);
3423 /* fix the keep alive */
3424 for (i = j = 0; i < n; i++) {
3425 ir_node *ka = get_End_keepalive(end, i);
3431 if (!is_Block(ka)) {
3432 block = get_nodes_block(ka);
3439 node = get_irn_node(block);
3440 if (node->type.tv != tarval_unreachable)
3444 set_End_keepalives(end, j, in);
3449 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3452 * sets the generic functions to compute.
3454 static void set_compute_functions(void)
3458 /* set the default compute function */
3459 for (i = 0, n = ir_get_n_opcodes(); i < n; ++i) {
3460 ir_op *op = ir_get_opcode(i);
3461 op->ops.generic = (op_func)default_compute;
3464 /* set specific functions */
3480 } /* set_compute_functions */
3485 static void add_memory_keeps(ir_node **kept_memory, size_t len)
3487 ir_node *end = get_irg_end(current_ir_graph);
3492 ir_nodeset_init(&set);
3494 /* check, if those nodes are already kept */
3495 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3496 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3498 for (idx = 0; idx < len; ++idx) {
3499 ir_node *ka = kept_memory[idx];
3501 if (! ir_nodeset_contains(&set, ka)) {
3502 add_End_keepalive(end, ka);
3505 ir_nodeset_destroy(&set);
3506 } /* add_memory_keeps */
3508 void combo(ir_graph *irg)
3511 ir_node *initial_bl;
3513 ir_graph *rem = current_ir_graph;
3516 assure_irg_properties(irg,
3517 IR_GRAPH_PROPERTY_NO_BADS
3518 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
3519 | IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
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 /* we have our own value_of function */
3547 set_value_of_func(get_node_tarval);
3549 set_compute_functions();
3550 DEBUG_ONLY(part_nr = 0;)
3552 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3554 if (env.opt_unknown)
3555 tarval_UNKNOWN = tarval_top;
3557 tarval_UNKNOWN = tarval_bad;
3559 /* create the initial partition and place it on the work list */
3560 env.initial = new_partition(&env);
3561 add_to_worklist(env.initial, &env);
3562 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3564 /* set the hook: from now, every node has a partition and a type */
3565 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook);)
3567 /* all nodes on the initial partition have type Top */
3568 env.initial->type_is_T_or_C = 1;
3570 /* Place the START Node's partition on cprop.
3571 Place the START Node on its local worklist. */
3572 initial_bl = get_irg_start_block(irg);
3573 start = get_irn_node(initial_bl);
3574 add_to_cprop(start, &env);
3578 if (env.worklist != NULL)
3580 } while (env.cprop != NULL || env.worklist != NULL);
3582 dump_all_partitions(&env);
3583 check_all_partitions(&env);
3586 dump_ir_block_graph(irg, "-partition");
3589 /* apply the result */
3591 /* check, which nodes must be kept */
3592 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3594 /* kill unreachable control flow */
3595 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3596 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3597 * and fixes assertion because dead cf to dead blocks is NOT removed by
3599 apply_end(get_irg_end(irg), &env);
3600 irg_walk_graph(irg, NULL, apply_result, &env);
3602 len = ARR_LEN(env.kept_memory);
3604 add_memory_keeps(env.kept_memory, len);
3607 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3610 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3612 /* remove the partition hook */
3613 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL);)
3615 DEL_ARR_F(env.kept_memory);
3616 del_set(env.opcode2id_map);
3617 obstack_free(&env.obst, NULL);
3619 /* restore value_of() default behavior */
3620 set_value_of_func(NULL);
3621 current_ir_graph = rem;
3623 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
3626 /* Creates an ir_graph pass for combo. */
3627 ir_graph_pass_t *combo_pass(const char *name)
3629 return def_graph_pass(name ? name : "combo", combo);