2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Cliff Click's Combined Analysis/Optimization
23 * @author Michael Beck
26 * This is a slightly enhanced version of Cliff Clicks combo algorithm
27 * - support for commutative nodes is added, Add(a,b) and Add(b,a) ARE congruent
28 * - supports all Firm direct (by a data edge) identities except Mux
29 * (Mux can be a 2-input or 1-input identity, only 2-input is implemented yet)
30 * - supports Confirm nodes (handle them like Copies but do NOT remove them)
31 * - let Cmp nodes calculate Top like all othe data nodes: this would let
32 * Mux nodes to calculate Unknown instead of taking the true result
33 * - let Cond(Top) always select FALSE/default: This is tricky. Nodes are only reavaluated
34 * IFF the predecessor changed its type. Because nodes are initialized with Top
35 * this never happens, let all Proj(Cond) be unreachable.
36 * We avoid this condition by the same way we work around Phi: whenever a Block
37 * node is placed on the list, place its Cond nodes (and because they are Tuple
38 * all its Proj-nodes either on the cprop list)
39 * Especially, this changes the meaning of Click's example:
54 * using Click's version while is silent with our.
55 * - support for global congruences is implemented but not tested yet
57 * Note further that we use the terminology from Click's work here, which is different
58 * in some cases from Firm terminology. Especially, Click's type is a
59 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
65 #include "iroptimize.h"
72 #include "irgraph_t.h"
79 #include "iropt_dbg.h"
83 #include "irnodeset.h"
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_opcode code; /**< The Firm opcode. */
109 ir_mode *mode; /**< The mode of all nodes in the partition. */
110 int arity; /**< The arity of this opcode (needed for Phi etc. */
112 long proj; /**< For Proj nodes, its proj number */
113 ir_entity *ent; /**< For Sel Nodes, its entity */
114 int intVal; /**< For Conv/Div Nodes: strict/remainderless */
115 unsigned uintVal;/**< for Builtin: the kind */
116 ir_node *block; /**< for Block: itself */
117 void *ptr; /**< generic pointer for hash/cmp */
122 * An entry in the list_map.
124 struct listmap_entry_t {
125 void *id; /**< The id. */
126 node_t *list; /**< The associated list for this id. */
127 listmap_entry_t *next; /**< Link to the next entry in the map. */
130 /** We must map id's to lists. */
131 typedef struct listmap_t {
132 set *map; /**< Map id's to listmap_entry_t's */
133 listmap_entry_t *values; /**< List of all values in the map. */
137 * A lattice element. Because we handle constants and symbolic constants different, we
138 * have to use this union.
149 ir_node *node; /**< The IR-node itself. */
150 list_head node_list; /**< Double-linked list of leader/follower entries. */
151 list_head cprop_list; /**< Double-linked partition.cprop list. */
152 partition_t *part; /**< points to the partition this node belongs to */
153 node_t *next; /**< Next node on local list (partition.touched, fallen). */
154 node_t *race_next; /**< Next node on race list. */
155 lattice_elem_t type; /**< The associated lattice element "type". */
156 int max_user_input; /**< Maximum input number of Def-Use edges. */
157 int next_edge; /**< Index of the next Def-Use edge to use. */
158 int n_followers; /**< Number of Follower in the outs set. */
159 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
160 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
161 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
162 unsigned is_follower:1; /**< Set, if this node is a follower. */
163 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
167 * A partition containing congruent nodes.
170 list_head Leader; /**< The head of partition Leader node list. */
171 list_head Follower; /**< The head of partition Follower node list. */
172 list_head cprop; /**< The head of partition.cprop list. */
173 list_head cprop_X; /**< The head of partition.cprop (Cond nodes and its Projs) list. */
174 partition_t *wl_next; /**< Next entry in the work list if any. */
175 partition_t *touched_next; /**< Points to the next partition in the touched set. */
176 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
177 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
178 node_t *touched; /**< The partition.touched set of this partition. */
179 unsigned n_leader; /**< Number of entries in this partition.Leader. */
180 unsigned n_touched; /**< Number of entries in the partition.touched. */
181 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
182 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
183 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
184 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
185 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
187 partition_t *dbg_next; /**< Link all partitions for debugging */
188 unsigned nr; /**< A unique number for (what-)mapping, >0. */
192 typedef struct environment_t {
193 struct obstack obst; /**< obstack to allocate data structures. */
194 partition_t *worklist; /**< The work list. */
195 partition_t *cprop; /**< The constant propagation list. */
196 partition_t *touched; /**< the touched set. */
197 partition_t *initial; /**< The initial partition. */
198 set *opcode2id_map; /**< The opcodeMode->id map. */
199 pmap *type2id_map; /**< The type->id map. */
200 ir_node **kept_memory; /**< Array of memory nodes that must be kept. */
201 int end_idx; /**< -1 for local and 0 for global congruences. */
202 int lambda_input; /**< Captured argument for lambda_partition(). */
203 unsigned modified:1; /**< Set, if the graph was modified. */
204 unsigned unopt_cf:1; /**< If set, control flow is not optimized due to Unknown. */
205 /* options driving the optimization */
206 unsigned commutative:1; /**< Set, if commutation nodes should be handled specially. */
207 unsigned opt_unknown:1; /**< Set, if non-strict programs should be optimized. */
209 partition_t *dbg_list; /**< List of all partitions. */
213 /** Type of the what function. */
214 typedef void *(*what_func)(const node_t *node, environment_t *env);
216 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
217 #define set_irn_node(irn, node) set_irn_link(irn, node)
219 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
220 #undef tarval_unreachable
221 #define tarval_unreachable tarval_top
224 /** The debug module handle. */
225 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
227 /** The what reason. */
228 DEBUG_ONLY(static const char *what_reason;)
230 /** Next partition number. */
231 DEBUG_ONLY(static unsigned part_nr = 0);
233 /** The tarval returned by Unknown nodes: set to either tarval_bad OR tarval_top. */
234 static tarval *tarval_UNKNOWN;
237 static node_t *identity(node_t *node);
239 #ifdef CHECK_PARTITIONS
243 static void check_partition(const partition_t *T) {
247 list_for_each_entry(node_t, node, &T->Leader, node_list) {
248 assert(node->is_follower == 0);
249 assert(node->flagged == 0);
250 assert(node->part == T);
253 assert(n == T->n_leader);
255 list_for_each_entry(node_t, node, &T->Follower, node_list) {
256 assert(node->is_follower == 1);
257 assert(node->flagged == 0);
258 assert(node->part == T);
260 } /* check_partition */
263 * check that all leader nodes in the partition have the same opcode.
265 static void check_opcode(const partition_t *Z) {
270 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
271 ir_node *irn = node->node;
274 key.code = get_irn_opcode(irn);
275 key.mode = get_irn_mode(irn);
276 key.arity = get_irn_arity(irn);
280 switch (get_irn_opcode(irn)) {
282 key.u.proj = get_Proj_proj(irn);
285 key.u.ent = get_Sel_entity(irn);
288 key.u.intVal = get_Conv_strict(irn);
291 key.u.intVal = get_Div_no_remainder(irn);
297 key.mode = get_Load_mode(irn);
300 key.u.intVal = get_Builtin_kind(irn);
307 assert((unsigned)key.code == get_irn_opcode(irn));
308 assert(key.mode == get_irn_mode(irn));
309 assert(key.arity == get_irn_arity(irn));
311 switch (get_irn_opcode(irn)) {
313 assert(key.u.proj == get_Proj_proj(irn));
316 assert(key.u.ent == get_Sel_entity(irn));
319 assert(key.u.intVal == get_Conv_strict(irn));
322 assert(key.u.intVal == get_Div_no_remainder(irn));
325 assert(key.u.block == irn);
328 assert(key.mode == get_Load_mode(irn));
331 assert(key.u.intVal == (int) get_Builtin_kind(irn));
340 static void check_all_partitions(environment_t *env) {
345 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
347 if (! P->type_is_T_or_C)
349 list_for_each_entry(node_t, node, &P->Follower, node_list) {
350 node_t *leader = identity(node);
352 assert(leader != node && leader->part == node->part);
363 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
367 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
368 for (e = list; e != NULL; e = NEXT(e)) {
369 assert(e->part == Z);
377 } /* ido_check_list */
380 * Check a local list.
382 static void check_list(const node_t *list, const partition_t *Z) {
383 do_check_list(list, offsetof(node_t, next), Z);
387 #define check_partition(T)
388 #define check_list(list, Z)
389 #define check_all_partitions(env)
390 #endif /* CHECK_PARTITIONS */
393 static inline lattice_elem_t get_partition_type(const partition_t *X);
396 * Dump partition to output.
398 static void dump_partition(const char *msg, const partition_t *part) {
401 lattice_elem_t type = get_partition_type(part);
403 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
404 msg, part->nr, part->type_is_T_or_C ? "*" : "",
405 part->n_leader, type));
406 list_for_each_entry(node_t, node, &part->Leader, node_list) {
407 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
410 if (! list_empty(&part->Follower)) {
411 DB((dbg, LEVEL_2, "\n---\n "));
413 list_for_each_entry(node_t, node, &part->Follower, node_list) {
414 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
418 DB((dbg, LEVEL_2, "\n}\n"));
419 } /* dump_partition */
424 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
428 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
430 DB((dbg, LEVEL_3, "%s = {\n ", msg));
431 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
432 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
435 DB((dbg, LEVEL_3, "\n}\n"));
443 static void dump_race_list(const char *msg, const node_t *list) {
444 do_dump_list(msg, list, offsetof(node_t, race_next));
445 } /* dump_race_list */
448 * Dumps a local list.
450 static void dump_list(const char *msg, const node_t *list) {
451 do_dump_list(msg, list, offsetof(node_t, next));
455 * Dump all partitions.
457 static void dump_all_partitions(const environment_t *env) {
458 const partition_t *P;
460 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
461 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
462 dump_partition("", P);
463 } /* dump_all_partitions */
468 static void dump_split_list(const partition_t *list) {
469 const partition_t *p;
471 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
472 for (p = list; p != NULL; p = p->split_next)
473 DB((dbg, LEVEL_2, "part%u, ", p->nr));
474 DB((dbg, LEVEL_2, "\n}\n"));
475 } /* dump_split_list */
478 * Dump partition and type for a node.
480 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
481 ir_node *irn = local != NULL ? local : n;
482 node_t *node = get_irn_node(irn);
484 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
486 } /* dump_partition_hook */
489 #define dump_partition(msg, part)
490 #define dump_race_list(msg, list)
491 #define dump_list(msg, list)
492 #define dump_all_partitions(env)
493 #define dump_split_list(list)
496 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
498 * Verify that a type transition is monotone
500 static void verify_type(const lattice_elem_t old_type, node_t *node) {
501 if (old_type.tv == node->type.tv) {
505 if (old_type.tv == tarval_top) {
506 /* from Top down-to is always allowed */
509 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
513 panic("combo: wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
517 #define verify_type(old_type, node)
521 * Compare two pointer values of a listmap.
523 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
524 const listmap_entry_t *e1 = elt;
525 const listmap_entry_t *e2 = key;
528 return e1->id != e2->id;
529 } /* listmap_cmp_ptr */
532 * Initializes a listmap.
534 * @param map the listmap
536 static void listmap_init(listmap_t *map) {
537 map->map = new_set(listmap_cmp_ptr, 16);
542 * Terminates a listmap.
544 * @param map the listmap
546 static void listmap_term(listmap_t *map) {
551 * Return the associated listmap entry for a given id.
553 * @param map the listmap
554 * @param id the id to search for
556 * @return the associated listmap entry for the given id
558 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
559 listmap_entry_t key, *entry;
564 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
566 if (entry->list == NULL) {
567 /* a new entry, put into the list */
568 entry->next = map->values;
575 * Calculate the hash value for an opcode map entry.
577 * @param entry an opcode map entry
579 * @return a hash value for the given opcode map entry
581 static unsigned opcode_hash(const opcode_key_t *entry) {
582 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ptr) + entry->arity;
586 * Compare two entries in the opcode map.
588 static int cmp_opcode(const void *elt, const void *key, size_t size) {
589 const opcode_key_t *o1 = elt;
590 const opcode_key_t *o2 = key;
593 return o1->code != o2->code || o1->mode != o2->mode ||
594 o1->arity != o2->arity ||
595 o1->u.proj != o2->u.proj ||
596 o1->u.intVal != o2->u.intVal || /* this already checks uIntVal */
597 o1->u.ptr != o2->u.ptr;
601 * Compare two Def-Use edges for input position.
603 static int cmp_def_use_edge(const void *a, const void *b) {
604 const ir_def_use_edge *ea = a;
605 const ir_def_use_edge *eb = b;
607 /* no overrun, because range is [-1, MAXINT] */
608 return ea->pos - eb->pos;
609 } /* cmp_def_use_edge */
612 * We need the Def-Use edges sorted.
614 static void sort_irn_outs(node_t *node) {
615 ir_node *irn = node->node;
616 int n_outs = get_irn_n_outs(irn);
619 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
621 node->max_user_input = irn->out[n_outs].pos;
622 } /* sort_irn_outs */
625 * Return the type of a node.
627 * @param irn an IR-node
629 * @return the associated type of this node
631 static inline lattice_elem_t get_node_type(const ir_node *irn) {
632 return get_irn_node(irn)->type;
633 } /* get_node_type */
636 * Return the tarval of a node.
638 * @param irn an IR-node
640 * @return the associated type of this node
642 static inline tarval *get_node_tarval(const ir_node *irn) {
643 lattice_elem_t type = get_node_type(irn);
645 if (is_tarval(type.tv))
647 return tarval_bottom;
648 } /* get_node_type */
651 * Add a partition to the worklist.
653 static inline void add_to_worklist(partition_t *X, environment_t *env) {
654 assert(X->on_worklist == 0);
655 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
656 X->wl_next = env->worklist;
659 } /* add_to_worklist */
662 * Create a new empty partition.
664 * @param env the environment
666 * @return a newly allocated partition
668 static inline partition_t *new_partition(environment_t *env) {
669 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
671 INIT_LIST_HEAD(&part->Leader);
672 INIT_LIST_HEAD(&part->Follower);
673 INIT_LIST_HEAD(&part->cprop);
674 INIT_LIST_HEAD(&part->cprop_X);
675 part->wl_next = NULL;
676 part->touched_next = NULL;
677 part->cprop_next = NULL;
678 part->split_next = NULL;
679 part->touched = NULL;
682 part->max_user_inputs = 0;
683 part->on_worklist = 0;
684 part->on_touched = 0;
686 part->type_is_T_or_C = 0;
688 part->dbg_next = env->dbg_list;
689 env->dbg_list = part;
690 part->nr = part_nr++;
694 } /* new_partition */
697 * Get the first node from a partition.
699 static inline node_t *get_first_node(const partition_t *X) {
700 return list_entry(X->Leader.next, node_t, node_list);
701 } /* get_first_node */
704 * Return the type of a partition (assuming partition is non-empty and
705 * all elements have the same type).
707 * @param X a partition
709 * @return the type of the first element of the partition
711 static inline lattice_elem_t get_partition_type(const partition_t *X) {
712 const node_t *first = get_first_node(X);
714 } /* get_partition_type */
717 * Creates a partition node for the given IR-node and place it
718 * into the given partition.
720 * @param irn an IR-node
721 * @param part a partition to place the node in
722 * @param env the environment
724 * @return the created node
726 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
727 /* create a partition node and place it in the partition */
728 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
730 INIT_LIST_HEAD(&node->node_list);
731 INIT_LIST_HEAD(&node->cprop_list);
735 node->race_next = NULL;
736 node->type.tv = tarval_top;
737 node->max_user_input = 0;
739 node->n_followers = 0;
740 node->on_touched = 0;
743 node->is_follower = 0;
745 set_irn_node(irn, node);
747 list_add_tail(&node->node_list, &part->Leader);
751 } /* create_partition_node */
754 * Pre-Walker, initialize all Nodes' type to U or top and place
755 * all nodes into the TOP partition.
757 static void create_initial_partitions(ir_node *irn, void *ctx) {
758 environment_t *env = 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) {
779 add_Block_phi(get_nodes_block(irn), 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) {
791 if (y->on_touched == 0) {
792 partition_t *part = y->part;
794 y->next = part->touched;
799 if (part->on_touched == 0) {
800 part->touched_next = env->touched;
802 part->on_touched = 1;
805 check_list(part->touched, part);
807 } /* add_to_touched */
810 * Place a node on the cprop list.
813 * @param env the environment
815 static void add_to_cprop(node_t *y, environment_t *env) {
818 /* Add y to y.partition.cprop. */
819 if (y->on_cprop == 0) {
820 partition_t *Y = y->part;
821 ir_node *irn = y->node;
823 /* place Conds and all its Projs on the cprop_X list */
824 if (is_Cond(skip_Proj(irn)))
825 list_add_tail(&y->cprop_list, &Y->cprop_X);
827 list_add_tail(&y->cprop_list, &Y->cprop);
830 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
832 /* place its partition on the cprop list */
833 if (Y->on_cprop == 0) {
834 Y->cprop_next = env->cprop;
840 if (get_irn_mode(irn) == mode_T) {
841 /* mode_T nodes always produce tarval_bottom, so we must explicitly
842 add it's Proj's to get constant evaluation to work */
845 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
846 node_t *proj = get_irn_node(get_irn_out(irn, i));
848 add_to_cprop(proj, env);
850 } else if (is_Block(irn)) {
851 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
852 * if someone placed the block. The Block is only placed if the reachability
853 * changes, and this must be re-evaluated in compute_Phi(). */
855 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
856 node_t *p = get_irn_node(phi);
857 add_to_cprop(p, env);
863 * Update the worklist: If Z is on worklist then add Z' to worklist.
864 * Else add the smaller of Z and Z' to worklist.
866 * @param Z the Z partition
867 * @param Z_prime the Z' partition, a previous part of Z
868 * @param env the environment
870 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
871 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
872 add_to_worklist(Z_prime, env);
874 add_to_worklist(Z, env);
876 } /* update_worklist */
879 * Make all inputs to x no longer be F.def_use edges.
883 static void move_edges_to_leader(node_t *x) {
884 ir_node *irn = x->node;
887 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
888 node_t *pred = get_irn_node(get_irn_n(irn, i));
893 n = get_irn_n_outs(p);
894 for (j = 1; j <= pred->n_followers; ++j) {
895 if (p->out[j].pos == i && p->out[j].use == irn) {
896 /* found a follower edge to x, move it to the Leader */
897 ir_def_use_edge edge = p->out[j];
899 /* remove this edge from the Follower set */
900 p->out[j] = p->out[pred->n_followers];
903 /* sort it into the leader set */
904 for (k = pred->n_followers + 2; k <= n; ++k) {
905 if (p->out[k].pos >= edge.pos)
907 p->out[k - 1] = p->out[k];
909 /* place the new edge here */
910 p->out[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) {
929 partition_t *Z_prime;
934 dump_partition("Splitting ", Z);
935 dump_list("by list ", g);
939 /* Remove g from Z. */
940 for (node = g; node != NULL; node = node->next) {
941 assert(node->part == Z);
942 list_del(&node->node_list);
945 assert(n < Z->n_leader);
948 /* Move g to a new partition, Z'. */
949 Z_prime = new_partition(env);
951 for (node = g; node != NULL; node = node->next) {
952 list_add_tail(&node->node_list, &Z_prime->Leader);
953 node->part = Z_prime;
954 if (node->max_user_input > max_input)
955 max_input = node->max_user_input;
957 Z_prime->max_user_inputs = max_input;
958 Z_prime->n_leader = n;
961 check_partition(Z_prime);
963 /* for now, copy the type info tag, it will be adjusted in split_by(). */
964 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
966 update_worklist(Z, Z_prime, env);
968 dump_partition("Now ", Z);
969 dump_partition("Created new ", Z_prime);
971 } /* split_no_followers */
974 * Make the Follower -> Leader transition for a node.
978 static void follower_to_leader(node_t *n) {
979 assert(n->is_follower == 1);
981 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
983 move_edges_to_leader(n);
984 list_del(&n->node_list);
985 list_add_tail(&n->node_list, &n->part->Leader);
987 } /* follower_to_leader */
990 * The environment for one race step.
992 typedef struct step_env {
993 node_t *initial; /**< The initial node list. */
994 node_t *unwalked; /**< The unwalked node list. */
995 node_t *walked; /**< The walked node list. */
996 int index; /**< Next index of Follower use_def edge. */
997 unsigned side; /**< side number. */
1001 * Return non-zero, if a input is a real follower
1003 * @param irn the node to check
1004 * @param input number of the input
1006 static int is_real_follower(const ir_node *irn, int input) {
1009 switch (get_irn_opcode(irn)) {
1012 /* ignore the Confirm bound input */
1018 /* ignore the Mux sel input */
1023 /* dead inputs are not follower edges */
1024 ir_node *block = get_nodes_block(irn);
1025 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
1027 if (pred->type.tv == tarval_unreachable)
1037 /* only a Sub x,0 / Shift x,0 might be a follower */
1044 pred = get_irn_node(get_irn_n(irn, input));
1045 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1049 pred = get_irn_node(get_irn_n(irn, input));
1050 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1054 pred = get_irn_node(get_irn_n(irn, input));
1055 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1059 assert(!"opcode not implemented yet");
1063 } /* is_real_follower */
1066 * Do one step in the race.
1068 static int step(step_env *env) {
1071 if (env->initial != NULL) {
1072 /* Move node from initial to unwalked */
1074 env->initial = n->race_next;
1076 n->race_next = env->unwalked;
1082 while (env->unwalked != NULL) {
1083 /* let n be the first node in unwalked */
1085 while (env->index < n->n_followers) {
1086 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1088 /* let m be n.F.def_use[index] */
1089 node_t *m = get_irn_node(edge->use);
1091 assert(m->is_follower);
1093 * Some inputs, like the get_Confirm_bound are NOT
1094 * real followers, sort them out.
1096 if (! is_real_follower(m->node, edge->pos)) {
1102 /* only followers from our partition */
1103 if (m->part != n->part)
1106 if ((m->flagged & env->side) == 0) {
1107 m->flagged |= env->side;
1109 if (m->flagged != 3) {
1110 /* visited the first time */
1111 /* add m to unwalked not as first node (we might still need to
1112 check for more follower node */
1113 m->race_next = n->race_next;
1117 /* else already visited by the other side and on the other list */
1120 /* move n to walked */
1121 env->unwalked = n->race_next;
1122 n->race_next = env->walked;
1130 * Clear the flags from a list and check for
1131 * nodes that where touched from both sides.
1133 * @param list the list
1135 static int clear_flags(node_t *list) {
1139 for (n = list; n != NULL; n = n->race_next) {
1140 if (n->flagged == 3) {
1141 /* we reach a follower from both sides, this will split congruent
1142 * inputs and make it a leader. */
1143 follower_to_leader(n);
1152 * Split a partition by a local list using the race.
1154 * @param pX pointer to the partition to split, might be changed!
1155 * @param gg a (non-empty) node list
1156 * @param env the environment
1158 * @return a new partition containing the nodes of gg
1160 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1161 partition_t *X = *pX;
1162 partition_t *X_prime;
1165 node_t *g, *h, *node, *t;
1166 int max_input, transitions, winner, shf;
1168 DEBUG_ONLY(static int run = 0;)
1170 DB((dbg, LEVEL_2, "Run %d ", run++));
1171 if (list_empty(&X->Follower)) {
1172 /* if the partition has NO follower, we can use the fast
1173 splitting algorithm. */
1174 return split_no_followers(X, gg, env);
1176 /* else do the race */
1178 dump_partition("Splitting ", X);
1179 dump_list("by list ", gg);
1181 INIT_LIST_HEAD(&tmp);
1183 /* Remove gg from X.Leader and put into g */
1185 for (node = gg; node != NULL; node = node->next) {
1186 assert(node->part == X);
1187 assert(node->is_follower == 0);
1189 list_del(&node->node_list);
1190 list_add_tail(&node->node_list, &tmp);
1191 node->race_next = g;
1196 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1197 node->race_next = h;
1200 /* restore X.Leader */
1201 list_splice(&tmp, &X->Leader);
1203 senv[0].initial = g;
1204 senv[0].unwalked = NULL;
1205 senv[0].walked = NULL;
1209 senv[1].initial = h;
1210 senv[1].unwalked = NULL;
1211 senv[1].walked = NULL;
1216 * Some informations on the race that are not stated clearly in Click's
1218 * 1) A follower stays on the side that reach him first.
1219 * 2) If the other side reches a follower, if will be converted to
1220 * a leader. /This must be done after the race is over, else the
1221 * edges we are iterating on are renumbered./
1222 * 3) /New leader might end up on both sides./
1223 * 4) /If one side ends up with new Leaders, we must ensure that
1224 * they can split out by opcode, hence we have to put _every_
1225 * partition with new Leader nodes on the cprop list, as
1226 * opcode splitting is done by split_by() at the end of
1227 * constant propagation./
1230 if (step(&senv[0])) {
1234 if (step(&senv[1])) {
1239 assert(senv[winner].initial == NULL);
1240 assert(senv[winner].unwalked == NULL);
1242 /* clear flags from walked/unwalked */
1244 transitions = clear_flags(senv[0].unwalked) << shf;
1245 transitions |= clear_flags(senv[0].walked) << shf;
1247 transitions |= clear_flags(senv[1].unwalked) << shf;
1248 transitions |= clear_flags(senv[1].walked) << shf;
1250 dump_race_list("winner ", senv[winner].walked);
1252 /* Move walked_{winner} to a new partition, X'. */
1253 X_prime = new_partition(env);
1256 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1257 list_del(&node->node_list);
1258 node->part = X_prime;
1259 if (node->is_follower) {
1260 list_add_tail(&node->node_list, &X_prime->Follower);
1262 list_add_tail(&node->node_list, &X_prime->Leader);
1265 if (node->max_user_input > max_input)
1266 max_input = node->max_user_input;
1268 X_prime->n_leader = n;
1269 X_prime->max_user_inputs = max_input;
1270 X->n_leader -= X_prime->n_leader;
1272 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1273 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1276 * Even if a follower was not checked by both sides, it might have
1277 * loose its congruence, so we need to check this case for all follower.
1279 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1280 if (identity(node) == node) {
1281 follower_to_leader(node);
1287 check_partition(X_prime);
1289 /* X' is the smaller part */
1290 add_to_worklist(X_prime, env);
1293 * If there where follower to leader transitions, ensure that the nodes
1294 * can be split out if necessary.
1296 if (transitions & 1) {
1297 /* place winner partition on the cprop list */
1298 if (X_prime->on_cprop == 0) {
1299 X_prime->cprop_next = env->cprop;
1300 env->cprop = X_prime;
1301 X_prime->on_cprop = 1;
1304 if (transitions & 2) {
1305 /* place other partition on the cprop list */
1306 if (X->on_cprop == 0) {
1307 X->cprop_next = env->cprop;
1313 dump_partition("Now ", X);
1314 dump_partition("Created new ", X_prime);
1316 /* we have to ensure that the partition containing g is returned */
1326 * Returns non-zero if the i'th input of a Phi node is live.
1328 * @param phi a Phi-node
1329 * @param i an input number
1331 * @return non-zero if the i'th input of the given Phi node is live
1333 static int is_live_input(ir_node *phi, int i) {
1335 ir_node *block = get_nodes_block(phi);
1336 ir_node *pred = get_Block_cfgpred(block, i);
1337 lattice_elem_t type = get_node_type(pred);
1339 return type.tv != tarval_unreachable;
1341 /* else it's the control input, always live */
1343 } /* is_live_input */
1346 * Return non-zero if a type is a constant.
1348 static int is_constant_type(lattice_elem_t type) {
1349 if (type.tv != tarval_bottom && type.tv != tarval_top)
1352 } /* is_constant_type */
1355 * Check whether a type is neither Top or a constant.
1356 * Note: U is handled like Top here, R is a constant.
1358 * @param type the type to check
1360 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1361 if (is_tarval(type.tv)) {
1362 if (type.tv == tarval_top)
1364 if (tarval_is_constant(type.tv))
1371 } /* type_is_neither_top_nor_const */
1374 * Collect nodes to the touched list.
1376 * @param list the list which contains the nodes that must be evaluated
1377 * @param idx the index of the def_use edge to evaluate
1378 * @param env the environment
1380 static void collect_touched(list_head *list, int idx, environment_t *env) {
1382 int end_idx = env->end_idx;
1384 list_for_each_entry(node_t, x, list, node_list) {
1388 /* leader edges start AFTER follower edges */
1389 x->next_edge = x->n_followers + 1;
1391 num_edges = get_irn_n_outs(x->node);
1393 /* for all edges in x.L.def_use_{idx} */
1394 while (x->next_edge <= num_edges) {
1395 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1398 /* check if we have necessary edges */
1399 if (edge->pos > idx)
1406 /* only non-commutative nodes */
1407 if (env->commutative &&
1408 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1411 /* ignore the "control input" for non-pinned nodes
1412 if we are running in GCSE mode */
1413 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1416 y = get_irn_node(succ);
1417 assert(get_irn_n(succ, idx) == x->node);
1419 /* ignore block edges touching followers */
1420 if (idx == -1 && y->is_follower)
1423 if (is_constant_type(y->type)) {
1424 ir_opcode code = get_irn_opcode(succ);
1425 if (code == iro_Sub || code == iro_Cmp)
1426 add_to_cprop(y, env);
1429 /* Partitions of constants should not be split simply because their Nodes have unequal
1430 functions or incongruent inputs. */
1431 if (type_is_neither_top_nor_const(y->type) &&
1432 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1433 add_to_touched(y, env);
1437 } /* collect_touched */
1440 * Collect commutative nodes to the touched list.
1442 * @param list the list which contains the nodes that must be evaluated
1443 * @param env the environment
1445 static void collect_commutative_touched(list_head *list, environment_t *env) {
1448 list_for_each_entry(node_t, x, list, node_list) {
1451 num_edges = get_irn_n_outs(x->node);
1453 x->next_edge = x->n_followers + 1;
1455 /* for all edges in x.L.def_use_{idx} */
1456 while (x->next_edge <= num_edges) {
1457 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1460 /* check if we have necessary edges */
1470 /* only commutative nodes */
1471 if (!is_op_commutative(get_irn_op(succ)))
1474 y = get_irn_node(succ);
1475 if (is_constant_type(y->type)) {
1476 ir_opcode code = get_irn_opcode(succ);
1477 if (code == iro_Eor)
1478 add_to_cprop(y, env);
1481 /* Partitions of constants should not be split simply because their Nodes have unequal
1482 functions or incongruent inputs. */
1483 if (type_is_neither_top_nor_const(y->type)) {
1484 add_to_touched(y, env);
1488 } /* collect_commutative_touched */
1491 * Split the partitions if caused by the first entry on the worklist.
1493 * @param env the environment
1495 static void cause_splits(environment_t *env) {
1496 partition_t *X, *Z, *N;
1499 /* remove the first partition from the worklist */
1501 env->worklist = X->wl_next;
1504 dump_partition("Cause_split: ", X);
1506 if (env->commutative) {
1507 /* handle commutative nodes first */
1509 /* empty the touched set: already done, just clear the list */
1510 env->touched = NULL;
1512 collect_commutative_touched(&X->Leader, env);
1513 collect_commutative_touched(&X->Follower, env);
1515 for (Z = env->touched; Z != NULL; Z = N) {
1517 node_t *touched = Z->touched;
1518 node_t *touched_aa = NULL;
1519 node_t *touched_ab = NULL;
1520 unsigned n_touched_aa = 0;
1521 unsigned n_touched_ab = 0;
1523 assert(Z->touched != NULL);
1525 /* beware, split might change Z */
1526 N = Z->touched_next;
1528 /* remove it from the touched set */
1531 /* Empty local Z.touched. */
1532 for (e = touched; e != NULL; e = n) {
1533 node_t *left = get_irn_node(get_irn_n(e->node, 0));
1534 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1536 assert(e->is_follower == 0);
1541 * Note: op(a, a) is NOT congruent to op(a, b).
1542 * So, we must split the touched list.
1544 if (left->part == right->part) {
1545 e->next = touched_aa;
1549 e->next = touched_ab;
1554 assert(n_touched_aa + n_touched_ab == Z->n_touched);
1558 if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1559 partition_t *Z_prime = Z;
1560 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1561 split(&Z_prime, touched_aa, env);
1563 assert(n_touched_aa <= Z->n_leader);
1565 if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1566 partition_t *Z_prime = Z;
1567 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1568 split(&Z_prime, touched_ab, env);
1570 assert(n_touched_ab <= Z->n_leader);
1574 /* combine temporary leader and follower list */
1575 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1576 /* empty the touched set: already done, just clear the list */
1577 env->touched = NULL;
1579 collect_touched(&X->Leader, idx, env);
1580 collect_touched(&X->Follower, idx, env);
1582 for (Z = env->touched; Z != NULL; Z = N) {
1584 node_t *touched = Z->touched;
1585 unsigned n_touched = Z->n_touched;
1587 assert(Z->touched != NULL);
1589 /* beware, split might change Z */
1590 N = Z->touched_next;
1592 /* remove it from the touched set */
1595 /* Empty local Z.touched. */
1596 for (e = touched; e != NULL; e = e->next) {
1597 assert(e->is_follower == 0);
1603 if (0 < n_touched && n_touched < Z->n_leader) {
1604 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1605 split(&Z, touched, env);
1607 assert(n_touched <= Z->n_leader);
1610 } /* cause_splits */
1613 * Implements split_by_what(): Split a partition by characteristics given
1614 * by the what function.
1616 * @param X the partition to split
1617 * @param What a function returning an Id for every node of the partition X
1618 * @param P a list to store the result partitions
1619 * @param env the environment
1623 static partition_t *split_by_what(partition_t *X, what_func What,
1624 partition_t **P, environment_t *env) {
1627 listmap_entry_t *iter;
1630 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1632 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1633 void *id = What(x, env);
1634 listmap_entry_t *entry;
1637 /* input not allowed, ignore */
1640 /* Add x to map[What(x)]. */
1641 entry = listmap_find(&map, id);
1642 x->next = entry->list;
1645 /* Let P be a set of Partitions. */
1647 /* for all sets S except one in the range of map do */
1648 for (iter = map.values; iter != NULL; iter = iter->next) {
1649 if (iter->next == NULL) {
1650 /* this is the last entry, ignore */
1655 /* Add SPLIT( X, S ) to P. */
1656 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1657 R = split(&X, S, env);
1667 } /* split_by_what */
1669 /** lambda n.(n.type) */
1670 static void *lambda_type(const node_t *node, environment_t *env) {
1672 return node->type.tv;
1675 /** lambda n.(n.opcode) */
1676 static void *lambda_opcode(const node_t *node, environment_t *env) {
1677 opcode_key_t key, *entry;
1678 ir_node *irn = node->node;
1680 key.code = get_irn_opcode(irn);
1681 key.mode = get_irn_mode(irn);
1682 key.arity = get_irn_arity(irn);
1686 switch (get_irn_opcode(irn)) {
1688 key.u.proj = get_Proj_proj(irn);
1691 key.u.ent = get_Sel_entity(irn);
1694 key.u.intVal = get_Conv_strict(irn);
1697 key.u.intVal = get_Div_no_remainder(irn);
1701 * Some ugliness here: Two Blocks having the same
1702 * IJmp predecessor would be congruent, which of course is wrong.
1703 * We fix it by never letting blocks be congruent
1704 * which cannot be detected by combo either.
1709 key.mode = get_Load_mode(irn);
1712 key.u.intVal = get_Builtin_kind(irn);
1718 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1720 } /* lambda_opcode */
1722 /** lambda n.(n[i].partition) */
1723 static void *lambda_partition(const node_t *node, environment_t *env) {
1724 ir_node *skipped = skip_Proj(node->node);
1727 int i = env->lambda_input;
1729 if (i >= get_irn_arity(node->node)) {
1731 * We are outside the allowed range: This can happen even
1732 * if we have split by opcode first: doing so might move Followers
1733 * to Leaders and those will have a different opcode!
1734 * Note that in this case the partition is on the cprop list and will be
1740 /* ignore the "control input" for non-pinned nodes
1741 if we are running in GCSE mode */
1742 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1745 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1746 p = get_irn_node(pred);
1748 } /* lambda_partition */
1750 /** lambda n.(n[i].partition) for commutative nodes */
1751 static void *lambda_commutative_partition(const node_t *node, environment_t *env) {
1752 ir_node *irn = node->node;
1753 ir_node *skipped = skip_Proj(irn);
1754 ir_node *pred, *left, *right;
1756 partition_t *pl, *pr;
1757 int i = env->lambda_input;
1759 if (i >= get_irn_arity(node->node)) {
1761 * We are outside the allowed range: This can happen even
1762 * if we have split by opcode first: doing so might move Followers
1763 * to Leaders and those will have a different opcode!
1764 * Note that in this case the partition is on the cprop list and will be
1770 /* ignore the "control input" for non-pinned nodes
1771 if we are running in GCSE mode */
1772 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1776 pred = get_irn_n(skipped, i);
1777 p = get_irn_node(pred);
1781 if (is_op_commutative(get_irn_op(irn))) {
1782 /* normalize partition order by returning the "smaller" on input 0,
1783 the "bigger" on input 1. */
1784 left = get_binop_left(irn);
1785 pl = get_irn_node(left)->part;
1786 right = get_binop_right(irn);
1787 pr = get_irn_node(right)->part;
1790 return pl < pr ? pl : pr;
1792 return pl > pr ? pl : pr;
1794 /* a not split out Follower */
1795 pred = get_irn_n(irn, i);
1796 p = get_irn_node(pred);
1800 } /* lambda_commutative_partition */
1803 * Returns true if a type is a constant (and NOT Top
1806 static int is_con(const lattice_elem_t type) {
1807 /* be conservative */
1808 if (is_tarval(type.tv))
1809 return tarval_is_constant(type.tv);
1810 return is_entity(type.sym.entity_p);
1814 * Implements split_by().
1816 * @param X the partition to split
1817 * @param env the environment
1819 static void split_by(partition_t *X, environment_t *env) {
1820 partition_t *I, *P = NULL;
1823 dump_partition("split_by", X);
1825 if (X->n_leader == 1) {
1826 /* we have only one leader, no need to split, just check it's type */
1827 node_t *x = get_first_node(X);
1828 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1832 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1833 P = split_by_what(X, lambda_type, &P, env);
1836 /* adjust the type tags, we have split partitions by type */
1837 for (I = P; I != NULL; I = I->split_next) {
1838 node_t *x = get_first_node(I);
1839 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1846 if (Y->n_leader > 1) {
1847 /* we do not want split the TOP or constant partitions */
1848 if (! Y->type_is_T_or_C) {
1849 partition_t *Q = NULL;
1851 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1852 Q = split_by_what(Y, lambda_opcode, &Q, env);
1859 if (Z->n_leader > 1) {
1860 const node_t *first = get_first_node(Z);
1861 int arity = get_irn_arity(first->node);
1863 what_func what = lambda_partition;
1864 DEBUG_ONLY(char buf[64];)
1866 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1867 what = lambda_commutative_partition;
1870 * BEWARE: during splitting by input 2 for instance we might
1871 * create new partitions which are different by input 1, so collect
1872 * them and split further.
1874 Z->split_next = NULL;
1877 for (input = arity - 1; input >= -1; --input) {
1879 partition_t *Z_prime = R;
1882 if (Z_prime->n_leader > 1) {
1883 env->lambda_input = input;
1884 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1885 DEBUG_ONLY(what_reason = buf;)
1886 S = split_by_what(Z_prime, what, &S, env);
1889 Z_prime->split_next = S;
1892 } while (R != NULL);
1897 } while (Q != NULL);
1900 } while (P != NULL);
1904 * (Re-)compute the type for a given node.
1906 * @param node the node
1908 static void default_compute(node_t *node) {
1910 ir_node *irn = node->node;
1912 /* if any of the data inputs have type top, the result is type top */
1913 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1914 ir_node *pred = get_irn_n(irn, i);
1915 node_t *p = get_irn_node(pred);
1917 if (p->type.tv == tarval_top) {
1918 node->type.tv = tarval_top;
1923 if (get_irn_mode(node->node) == mode_X)
1924 node->type.tv = tarval_reachable;
1926 node->type.tv = computed_value(irn);
1927 } /* default_compute */
1930 * (Re-)compute the type for a Block node.
1932 * @param node the node
1934 static void compute_Block(node_t *node) {
1936 ir_node *block = node->node;
1938 if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
1939 /* start block and labelled blocks are always reachable */
1940 node->type.tv = tarval_reachable;
1944 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1945 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1947 if (pred->type.tv == tarval_reachable) {
1948 /* A block is reachable, if at least of predecessor is reachable. */
1949 node->type.tv = tarval_reachable;
1953 node->type.tv = tarval_top;
1954 } /* compute_Block */
1957 * (Re-)compute the type for a Bad node.
1959 * @param node the node
1961 static void compute_Bad(node_t *node) {
1962 /* Bad nodes ALWAYS compute Top */
1963 node->type.tv = tarval_top;
1967 * (Re-)compute the type for an Unknown node.
1969 * @param node the node
1971 static void compute_Unknown(node_t *node) {
1972 /* While Unknown nodes should compute Top this is dangerous:
1973 * a Top input to a Cond would lead to BOTH control flows unreachable.
1974 * While this is correct in the given semantics, it would destroy the Firm
1977 * It would be safe to compute Top IF it can be assured, that only Cmp
1978 * nodes are inputs to Conds. We check that first.
1979 * This is the way Frontends typically build Firm, but some optimizations
1980 * (jump threading for instance) might replace them by Phib's...
1982 node->type.tv = tarval_UNKNOWN;
1983 } /* compute_Unknown */
1986 * (Re-)compute the type for a Jmp node.
1988 * @param node the node
1990 static void compute_Jmp(node_t *node) {
1991 node_t *block = get_irn_node(get_nodes_block(node->node));
1993 node->type = block->type;
1997 * (Re-)compute the type for the Return node.
1999 * @param node the node
2001 static void compute_Return(node_t *node) {
2002 /* The Return node is NOT dead if it is in a reachable block.
2003 * This is already checked in compute(). so we can return
2004 * Reachable here. */
2005 node->type.tv = tarval_reachable;
2006 } /* compute_Return */
2009 * (Re-)compute the type for the End node.
2011 * @param node the node
2013 static void compute_End(node_t *node) {
2014 /* the End node is NOT dead of course */
2015 node->type.tv = tarval_reachable;
2019 * (Re-)compute the type for a Call.
2021 * @param node the node
2023 static void compute_Call(node_t *node) {
2025 * A Call computes always bottom, even if it has Unknown
2028 node->type.tv = tarval_bottom;
2029 } /* compute_Call */
2032 * (Re-)compute the type for a SymConst node.
2034 * @param node the node
2036 static void compute_SymConst(node_t *node) {
2037 ir_node *irn = node->node;
2038 node_t *block = get_irn_node(get_nodes_block(irn));
2040 if (block->type.tv == tarval_unreachable) {
2041 node->type.tv = tarval_top;
2044 switch (get_SymConst_kind(irn)) {
2045 case symconst_addr_ent:
2046 /* case symconst_addr_name: cannot handle this yet */
2047 node->type.sym = get_SymConst_symbol(irn);
2050 node->type.tv = computed_value(irn);
2052 } /* compute_SymConst */
2055 * (Re-)compute the type for a Phi node.
2057 * @param node the node
2059 static void compute_Phi(node_t *node) {
2061 ir_node *phi = node->node;
2062 lattice_elem_t type;
2064 /* if a Phi is in a unreachable block, its type is TOP */
2065 node_t *block = get_irn_node(get_nodes_block(phi));
2067 if (block->type.tv == tarval_unreachable) {
2068 node->type.tv = tarval_top;
2072 /* Phi implements the Meet operation */
2073 type.tv = tarval_top;
2074 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2075 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2076 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2078 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2079 /* ignore TOP inputs: We must check here for unreachable blocks,
2080 because Firm constants live in the Start Block are NEVER Top.
2081 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2082 comes from a unreachable input. */
2085 if (pred->type.tv == tarval_bottom) {
2086 node->type.tv = tarval_bottom;
2088 } else if (type.tv == tarval_top) {
2089 /* first constant found */
2091 } else if (type.tv != pred->type.tv) {
2092 /* different constants or tarval_bottom */
2093 node->type.tv = tarval_bottom;
2096 /* else nothing, constants are the same */
2102 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2104 * @param node the node
2106 static void compute_Add(node_t *node) {
2107 ir_node *sub = node->node;
2108 node_t *l = get_irn_node(get_Add_left(sub));
2109 node_t *r = get_irn_node(get_Add_right(sub));
2110 lattice_elem_t a = l->type;
2111 lattice_elem_t b = r->type;
2114 if (a.tv == tarval_top || b.tv == tarval_top) {
2115 node->type.tv = tarval_top;
2116 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2117 node->type.tv = tarval_bottom;
2119 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2120 must call tarval_add() first to handle this case! */
2121 if (is_tarval(a.tv)) {
2122 if (is_tarval(b.tv)) {
2123 node->type.tv = tarval_add(a.tv, b.tv);
2126 mode = get_tarval_mode(a.tv);
2127 if (a.tv == get_mode_null(mode)) {
2131 } else if (is_tarval(b.tv)) {
2132 mode = get_tarval_mode(b.tv);
2133 if (b.tv == get_mode_null(mode)) {
2138 node->type.tv = tarval_bottom;
2143 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2145 * @param node the node
2147 static void compute_Sub(node_t *node) {
2148 ir_node *sub = node->node;
2149 node_t *l = get_irn_node(get_Sub_left(sub));
2150 node_t *r = get_irn_node(get_Sub_right(sub));
2151 lattice_elem_t a = l->type;
2152 lattice_elem_t b = r->type;
2155 if (a.tv == tarval_top || b.tv == tarval_top) {
2156 node->type.tv = tarval_top;
2157 } else if (is_con(a) && is_con(b)) {
2158 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2159 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2160 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2162 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2165 node->type.tv = tarval_bottom;
2167 } else if (r->part == l->part &&
2168 (!mode_is_float(get_irn_mode(l->node)))) {
2170 * BEWARE: a - a is NOT always 0 for floating Point values, as
2171 * NaN op NaN = NaN, so we must check this here.
2173 ir_mode *mode = get_irn_mode(sub);
2174 tv = get_mode_null(mode);
2176 /* if the node was ONCE evaluated by all constants, but now
2177 this breaks AND we get from the argument partitions a different
2178 result, switch to bottom.
2179 This happens because initially all nodes are in the same partition ... */
2180 if (node->type.tv != tv)
2184 node->type.tv = tarval_bottom;
2189 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2191 * @param node the node
2193 static void compute_Eor(node_t *node) {
2194 ir_node *eor = node->node;
2195 node_t *l = get_irn_node(get_Eor_left(eor));
2196 node_t *r = get_irn_node(get_Eor_right(eor));
2197 lattice_elem_t a = l->type;
2198 lattice_elem_t b = r->type;
2201 if (a.tv == tarval_top || b.tv == tarval_top) {
2202 node->type.tv = tarval_top;
2203 } else if (is_con(a) && is_con(b)) {
2204 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2205 node->type.tv = tarval_eor(a.tv, b.tv);
2206 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2208 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2211 node->type.tv = tarval_bottom;
2213 } else if (r->part == l->part) {
2214 ir_mode *mode = get_irn_mode(eor);
2215 tv = get_mode_null(mode);
2217 /* if the node was ONCE evaluated by all constants, but now
2218 this breaks AND we get from the argument partitions a different
2219 result, switch to bottom.
2220 This happens because initially all nodes are in the same partition ... */
2221 if (node->type.tv != tv)
2225 node->type.tv = tarval_bottom;
2230 * (Re-)compute the type for Cmp.
2232 * @param node the node
2234 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;
2241 if (a.tv == tarval_top || b.tv == tarval_top) {
2242 node->type.tv = tarval_top;
2243 } else if (r->part == l->part) {
2244 /* both nodes congruent, we can probably do something */
2245 node->type.tv = tarval_b_true;
2246 } else if (is_con(a) && is_con(b)) {
2247 /* both nodes are constants, we can probably do something */
2248 node->type.tv = tarval_b_true;
2250 node->type.tv = tarval_bottom;
2255 * (Re-)compute the type for a Proj(Cmp).
2257 * @param node the node
2258 * @param cond the predecessor Cmp node
2260 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
2261 ir_node *proj = node->node;
2262 node_t *l = get_irn_node(get_Cmp_left(cmp));
2263 node_t *r = get_irn_node(get_Cmp_right(cmp));
2264 lattice_elem_t a = l->type;
2265 lattice_elem_t b = r->type;
2266 pn_Cmp pnc = get_Proj_proj(proj);
2269 if (a.tv == tarval_top || b.tv == tarval_top) {
2270 node->type.tv = tarval_undefined;
2271 } else if (is_con(a) && is_con(b)) {
2272 default_compute(node);
2273 } else if (r->part == l->part &&
2274 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2276 * BEWARE: a == a is NOT always True for floating Point values, as
2277 * NaN != NaN is defined, so we must check this here.
2279 tv = pnc & pn_Cmp_Eq ? tarval_b_true: tarval_b_false;
2281 /* if the node was ONCE evaluated by all constants, but now
2282 this breaks AND we get from the argument partitions a different
2283 result, switch to bottom.
2284 This happens because initially all nodes are in the same partition ... */
2285 if (node->type.tv != tv)
2289 node->type.tv = tarval_bottom;
2291 } /* compute_Proj_Cmp */
2294 * (Re-)compute the type for a Proj(Cond).
2296 * @param node the node
2297 * @param cond the predecessor Cond node
2299 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
2300 ir_node *proj = node->node;
2301 long pnc = get_Proj_proj(proj);
2302 ir_node *sel = get_Cond_selector(cond);
2303 node_t *selector = get_irn_node(sel);
2306 * Note: it is crucial for the monotony that the Proj(Cond)
2307 * are evaluates after all predecessors of the Cond selector are
2313 * Due to the fact that 0 is a const, the Cmp gets immediately
2314 * on the cprop list. It will be evaluated before x is evaluated,
2315 * might leaving x as Top. When later x is evaluated, the Cmp
2316 * might change its value.
2317 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2318 * gets R, and later changed to F if Cmp is evaluated to True!
2320 * We prevent this by putting Conds in an extra cprop_X queue, which
2321 * gets evaluated after the cprop queue is empty.
2323 * Note that this even happens with Click's original algorithm, if
2324 * Cmp(x, 0) is evaluated to True first and later changed to False
2325 * if x was Top first and later changed to a Const ...
2326 * It is unclear how Click solved that problem ...
2328 * However, in rare cases even this does not help, if a Top reaches
2329 * a compare through a Phi, than Proj(Cond) is evaluated changing
2330 * the type of the Phi to something other.
2331 * So, we take the last resort and bind the type to R once
2334 * (This might be even the way Click works around the whole problem).
2336 * Finally, we may miss some optimization possibilities due to this:
2341 * If Top reaches the if first, than we decide for != here.
2342 * If y later is evaluated to 0, we cannot revert this decision
2343 * and must live with both outputs enabled. If this happens,
2344 * we get an unresolved if (true) in the code ...
2346 * In Click's version where this decision is done at the Cmp,
2347 * the Cmp is NOT optimized away than (if y evaluated to 1
2348 * for instance) and we get a if (1 == 0) here ...
2350 * Both solutions are suboptimal.
2351 * At least, we could easily detect this problem and run
2352 * cf_opt() (or even combo) again :-(
2354 if (node->type.tv == tarval_reachable)
2357 if (get_irn_mode(sel) == mode_b) {
2359 if (pnc == pn_Cond_true) {
2360 if (selector->type.tv == tarval_b_false) {
2361 node->type.tv = tarval_unreachable;
2362 } else if (selector->type.tv == tarval_b_true) {
2363 node->type.tv = tarval_reachable;
2364 } else if (selector->type.tv == tarval_bottom) {
2365 node->type.tv = tarval_reachable;
2367 assert(selector->type.tv == tarval_top);
2368 if (tarval_UNKNOWN == tarval_top) {
2369 /* any condition based on Top is "!=" */
2370 node->type.tv = tarval_unreachable;
2372 node->type.tv = tarval_unreachable;
2376 assert(pnc == pn_Cond_false);
2378 if (selector->type.tv == tarval_b_false) {
2379 node->type.tv = tarval_reachable;
2380 } else if (selector->type.tv == tarval_b_true) {
2381 node->type.tv = tarval_unreachable;
2382 } else if (selector->type.tv == tarval_bottom) {
2383 node->type.tv = tarval_reachable;
2385 assert(selector->type.tv == tarval_top);
2386 if (tarval_UNKNOWN == tarval_top) {
2387 /* any condition based on Top is "!=" */
2388 node->type.tv = tarval_reachable;
2390 node->type.tv = tarval_unreachable;
2396 if (selector->type.tv == tarval_bottom) {
2397 node->type.tv = tarval_reachable;
2398 } else if (selector->type.tv == tarval_top) {
2399 if (tarval_UNKNOWN == tarval_top &&
2400 pnc == get_Cond_default_proj(cond)) {
2401 /* a switch based of Top is always "default" */
2402 node->type.tv = tarval_reachable;
2404 node->type.tv = tarval_unreachable;
2407 long value = get_tarval_long(selector->type.tv);
2408 if (pnc == get_Cond_default_proj(cond)) {
2409 /* default switch, have to check ALL other cases */
2412 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2413 ir_node *succ = get_irn_out(cond, i);
2417 if (value == get_Proj_proj(succ)) {
2418 /* we found a match, will NOT take the default case */
2419 node->type.tv = tarval_unreachable;
2423 /* all cases checked, no match, will take default case */
2424 node->type.tv = tarval_reachable;
2427 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2431 } /* compute_Proj_Cond */
2434 * (Re-)compute the type for a Proj-Node.
2436 * @param node the node
2438 static void compute_Proj(node_t *node) {
2439 ir_node *proj = node->node;
2440 ir_mode *mode = get_irn_mode(proj);
2441 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2442 ir_node *pred = get_Proj_pred(proj);
2444 if (block->type.tv == tarval_unreachable) {
2445 /* a Proj in a unreachable Block stay Top */
2446 node->type.tv = tarval_top;
2449 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred)) {
2450 /* if the predecessor is Top, its Proj follow */
2451 node->type.tv = tarval_top;
2455 if (mode == mode_M) {
2456 /* mode M is always bottom */
2457 node->type.tv = tarval_bottom;
2460 if (mode != mode_X) {
2462 compute_Proj_Cmp(node, pred);
2464 default_compute(node);
2467 /* handle mode_X nodes */
2469 switch (get_irn_opcode(pred)) {
2471 /* the Proj_X from the Start is always reachable.
2472 However this is already handled at the top. */
2473 node->type.tv = tarval_reachable;
2476 compute_Proj_Cond(node, pred);
2479 default_compute(node);
2481 } /* compute_Proj */
2484 * (Re-)compute the type for a Confirm.
2486 * @param node the node
2488 static void compute_Confirm(node_t *node) {
2489 ir_node *confirm = node->node;
2490 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2492 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2493 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2495 if (is_con(bound->type)) {
2496 /* is equal to a constant */
2497 node->type = bound->type;
2501 /* a Confirm is a copy OR a Const */
2502 node->type = pred->type;
2503 } /* compute_Confirm */
2506 * (Re-)compute the type for a given node.
2508 * @param node the node
2510 static void compute(node_t *node) {
2511 ir_node *irn = node->node;
2514 #ifndef VERIFY_MONOTONE
2516 * Once a node reaches bottom, the type cannot fall further
2517 * in the lattice and we can stop computation.
2518 * Do not take this exit if the monotony verifier is
2519 * enabled to catch errors.
2521 if (node->type.tv == tarval_bottom)
2525 if (is_no_Block(irn)) {
2526 /* for pinned nodes, check its control input */
2527 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2528 node_t *block = get_irn_node(get_nodes_block(irn));
2530 if (block->type.tv == tarval_unreachable) {
2531 node->type.tv = tarval_top;
2537 func = (compute_func)node->node->op->ops.generic;
2543 * Identity functions: Note that one might thing that identity() is just a
2544 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2545 * here, because it expects that the identity node is one of the inputs, which is NOT
2546 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2547 * So, we have our own implementation, which copies some parts of equivalent_node()
2551 * Calculates the Identity for Phi nodes
2553 static node_t *identity_Phi(node_t *node) {
2554 ir_node *phi = node->node;
2555 ir_node *block = get_nodes_block(phi);
2556 node_t *n_part = NULL;
2559 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2560 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2562 if (pred_X->type.tv == tarval_reachable) {
2563 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2567 else if (n_part->part != pred->part) {
2568 /* incongruent inputs, not a follower */
2573 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2574 * tarval_top, is in the TOP partition and should NOT being split! */
2575 assert(n_part != NULL);
2577 } /* identity_Phi */
2580 * Calculates the Identity for commutative 0 neutral nodes.
2582 static node_t *identity_comm_zero_binop(node_t *node) {
2583 ir_node *op = node->node;
2584 node_t *a = get_irn_node(get_binop_left(op));
2585 node_t *b = get_irn_node(get_binop_right(op));
2586 ir_mode *mode = get_irn_mode(op);
2589 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2590 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2593 /* node: no input should be tarval_top, else the binop would be also
2594 * Top and not being split. */
2595 zero = get_mode_null(mode);
2596 if (a->type.tv == zero)
2598 if (b->type.tv == zero)
2601 } /* identity_comm_zero_binop */
2604 * Calculates the Identity for Shift nodes.
2606 static node_t *identity_shift(node_t *node) {
2607 ir_node *op = node->node;
2608 node_t *b = get_irn_node(get_binop_right(op));
2609 ir_mode *mode = get_irn_mode(b->node);
2612 /* node: no input should be tarval_top, else the binop would be also
2613 * Top and not being split. */
2614 zero = get_mode_null(mode);
2615 if (b->type.tv == zero)
2616 return get_irn_node(get_binop_left(op));
2618 } /* identity_shift */
2621 * Calculates the Identity for Mul nodes.
2623 static node_t *identity_Mul(node_t *node) {
2624 ir_node *op = node->node;
2625 node_t *a = get_irn_node(get_Mul_left(op));
2626 node_t *b = get_irn_node(get_Mul_right(op));
2627 ir_mode *mode = get_irn_mode(op);
2630 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2631 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2634 /* node: no input should be tarval_top, else the binop would be also
2635 * Top and not being split. */
2636 one = get_mode_one(mode);
2637 if (a->type.tv == one)
2639 if (b->type.tv == one)
2642 } /* identity_Mul */
2645 * Calculates the Identity for Sub nodes.
2647 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) {
2667 ir_node *and = node->node;
2668 node_t *a = get_irn_node(get_And_left(and));
2669 node_t *b = get_irn_node(get_And_right(and));
2670 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2672 /* node: no input should be tarval_top, else the And would be also
2673 * Top and not being split. */
2674 if (a->type.tv == neutral)
2676 if (b->type.tv == neutral)
2679 } /* identity_And */
2682 * Calculates the Identity for Confirm nodes.
2684 static node_t *identity_Confirm(node_t *node) {
2685 ir_node *confirm = node->node;
2687 /* a Confirm is always a Copy */
2688 return get_irn_node(get_Confirm_value(confirm));
2689 } /* identity_Confirm */
2692 * Calculates the Identity for Mux nodes.
2694 static node_t *identity_Mux(node_t *node) {
2695 ir_node *mux = node->node;
2696 node_t *t = get_irn_node(get_Mux_true(mux));
2697 node_t *f = get_irn_node(get_Mux_false(mux));
2700 if (t->part == f->part)
2703 /* for now, the 1-input identity is not supported */
2705 sel = get_irn_node(get_Mux_sel(mux));
2707 /* Mux sel input is mode_b, so it is always a tarval */
2708 if (sel->type.tv == tarval_b_true)
2710 if (sel->type.tv == tarval_b_false)
2714 } /* identity_Mux */
2717 * Calculates the Identity for nodes.
2719 static node_t *identity(node_t *node) {
2720 ir_node *irn = node->node;
2722 switch (get_irn_opcode(irn)) {
2724 return identity_Phi(node);
2726 return identity_Mul(node);
2730 return identity_comm_zero_binop(node);
2735 return identity_shift(node);
2737 return identity_And(node);
2739 return identity_Sub(node);
2741 return identity_Confirm(node);
2743 return identity_Mux(node);
2750 * Node follower is a (new) follower of leader, segregate Leader
2753 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2754 ir_node *l = leader->node;
2755 int j, i, n = get_irn_n_outs(l);
2757 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2758 /* The leader edges must remain sorted, but follower edges can
2760 for (i = leader->n_followers + 1; i <= n; ++i) {
2761 if (l->out[i].use == follower) {
2762 ir_def_use_edge t = l->out[i];
2764 for (j = i - 1; j >= leader->n_followers + 1; --j)
2765 l->out[j + 1] = l->out[j];
2766 ++leader->n_followers;
2767 l->out[leader->n_followers] = t;
2771 } /* segregate_def_use_chain_1 */
2774 * Node follower is a (new) follower segregate its Leader
2777 * @param follower the follower IR node
2779 static void segregate_def_use_chain(const ir_node *follower) {
2782 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2783 node_t *pred = get_irn_node(get_irn_n(follower, i));
2785 segregate_def_use_chain_1(follower, pred);
2787 } /* segregate_def_use_chain */
2790 * Propagate constant evaluation.
2792 * @param env the environment
2794 static void propagate(environment_t *env) {
2797 lattice_elem_t old_type;
2799 unsigned n_fallen, old_type_was_T_or_C;
2802 while (env->cprop != NULL) {
2803 void *oldopcode = NULL;
2805 /* remove the first partition X from cprop */
2808 env->cprop = X->cprop_next;
2810 old_type_was_T_or_C = X->type_is_T_or_C;
2812 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2816 int cprop_empty = list_empty(&X->cprop);
2817 int cprop_X_empty = list_empty(&X->cprop_X);
2819 if (cprop_empty && cprop_X_empty) {
2820 /* both cprop lists are empty */
2824 /* remove the first Node x from X.cprop */
2826 /* Get a node from the cprop_X list only if
2827 * all data nodes are processed.
2828 * This ensures, that all inputs of the Cond
2829 * predecessor are processed if its type is still Top.
2831 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2833 x = list_entry(X->cprop.next, node_t, cprop_list);
2836 //assert(x->part == X);
2837 list_del(&x->cprop_list);
2840 if (x->is_follower && identity(x) == x) {
2841 /* check the opcode first */
2842 if (oldopcode == NULL) {
2843 oldopcode = lambda_opcode(get_first_node(X), env);
2845 if (oldopcode != lambda_opcode(x, env)) {
2846 if (x->on_fallen == 0) {
2847 /* different opcode -> x falls out of this partition */
2852 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2856 /* x will make the follower -> leader transition */
2857 follower_to_leader(x);
2860 /* compute a new type for x */
2862 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2864 if (x->type.tv != old_type.tv) {
2865 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2866 verify_type(old_type, x);
2868 if (x->on_fallen == 0) {
2869 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2870 not already on the list. */
2875 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2877 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2878 ir_node *succ = get_irn_out(x->node, i);
2879 node_t *y = get_irn_node(succ);
2881 /* Add y to y.partition.cprop. */
2882 add_to_cprop(y, env);
2887 if (n_fallen > 0 && n_fallen != X->n_leader) {
2888 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2889 Y = split(&X, fallen, env);
2891 * We have split out fallen node. The type of the result
2892 * partition is NOT set yet.
2894 Y->type_is_T_or_C = 0;
2898 /* remove the flags from the fallen list */
2899 for (x = fallen; x != NULL; x = x->next)
2902 if (old_type_was_T_or_C) {
2905 /* check if some nodes will make the leader -> follower transition */
2906 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2907 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2908 node_t *eq_node = identity(y);
2910 if (eq_node != y && eq_node->part == y->part) {
2911 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2912 /* move to Follower */
2914 list_del(&y->node_list);
2915 list_add_tail(&y->node_list, &Y->Follower);
2918 segregate_def_use_chain(y->node);
2928 * Get the leader for a given node from its congruence class.
2930 * @param irn the node
2932 static ir_node *get_leader(node_t *node) {
2933 partition_t *part = node->part;
2935 if (part->n_leader > 1 || node->is_follower) {
2936 if (node->is_follower) {
2937 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2940 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2942 return get_first_node(part)->node;
2948 * Returns non-zero if a mode_T node has only one reachable output.
2950 static int only_one_reachable_proj(ir_node *n) {
2953 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2954 ir_node *proj = get_irn_out(n, i);
2957 /* skip non-control flow Proj's */
2958 if (get_irn_mode(proj) != mode_X)
2961 node = get_irn_node(proj);
2962 if (node->type.tv == tarval_reachable) {
2968 } /* only_one_reachable_proj */
2971 * Return non-zero if the control flow predecessor node pred
2972 * is the only reachable control flow exit of its block.
2974 * @param pred the control flow exit
2975 * @param block the destination block
2977 static int can_exchange(ir_node *pred, ir_node *block) {
2978 if (is_Start(pred) || has_Block_entity(block))
2980 else if (is_Jmp(pred))
2982 else if (get_irn_mode(pred) == mode_T) {
2983 /* if the predecessor block has more than one
2984 reachable outputs we cannot remove the block */
2985 return only_one_reachable_proj(pred);
2988 } /* can_exchange */
2991 * Block Post-Walker, apply the analysis results on control flow by
2992 * shortening Phi's and Block inputs.
2994 static void apply_cf(ir_node *block, void *ctx) {
2995 environment_t *env = ctx;
2996 node_t *node = get_irn_node(block);
2998 ir_node **ins, **in_X;
2999 ir_node *phi, *next;
3001 n = get_Block_n_cfgpreds(block);
3003 if (node->type.tv == tarval_unreachable) {
3006 for (i = n - 1; i >= 0; --i) {
3007 ir_node *pred = get_Block_cfgpred(block, i);
3009 if (! is_Bad(pred)) {
3010 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3012 if (pred_bl->flagged == 0) {
3013 pred_bl->flagged = 3;
3015 if (pred_bl->type.tv == tarval_reachable) {
3017 * We will remove an edge from block to its pred.
3018 * This might leave the pred block as an endless loop
3020 if (! is_backedge(block, i))
3021 keep_alive(pred_bl->node);
3027 /* the EndBlock is always reachable even if the analysis
3028 finds out the opposite :-) */
3029 if (block != get_irg_end_block(current_ir_graph)) {
3030 /* mark dead blocks */
3031 set_Block_dead(block);
3032 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
3034 /* the endblock is unreachable */
3035 set_irn_in(block, 0, NULL);
3041 /* only one predecessor combine */
3042 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3044 if (can_exchange(pred, block)) {
3045 ir_node *new_block = get_nodes_block(pred);
3046 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3047 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3048 exchange(block, new_block);
3049 node->node = new_block;
3055 NEW_ARR_A(ir_node *, in_X, n);
3057 for (i = 0; i < n; ++i) {
3058 ir_node *pred = get_Block_cfgpred(block, i);
3059 node_t *node = get_irn_node(pred);
3061 if (node->type.tv == tarval_reachable) {
3064 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3065 if (! is_Bad(pred)) {
3066 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3068 if (pred_bl->flagged == 0) {
3069 pred_bl->flagged = 3;
3071 if (pred_bl->type.tv == tarval_reachable) {
3073 * We will remove an edge from block to its pred.
3074 * This might leave the pred block as an endless loop
3076 if (! is_backedge(block, i))
3077 keep_alive(pred_bl->node);
3087 NEW_ARR_A(ir_node *, ins, n);
3088 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3089 node_t *node = get_irn_node(phi);
3091 next = get_Phi_next(phi);
3092 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3093 /* this Phi is replaced by a constant */
3094 tarval *tv = node->type.tv;
3095 ir_node *c = new_Const(tv);
3097 set_irn_node(c, node);
3099 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3100 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3105 for (i = 0; i < n; ++i) {
3106 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3108 if (pred->type.tv == tarval_reachable) {
3109 ins[j++] = get_Phi_pred(phi, i);
3113 /* this Phi is replaced by a single predecessor */
3114 ir_node *s = ins[0];
3115 node_t *phi_node = get_irn_node(phi);
3118 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3119 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3124 set_irn_in(phi, j, ins);
3132 /* this Block has only one live predecessor */
3133 ir_node *pred = skip_Proj(in_X[0]);
3135 if (can_exchange(pred, block)) {
3136 ir_node *new_block = get_nodes_block(pred);
3137 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3138 exchange(block, new_block);
3139 node->node = new_block;
3144 set_irn_in(block, k, in_X);
3149 * Exchange a node by its leader.
3150 * Beware: in rare cases the mode might be wrong here, for instance
3151 * AddP(x, NULL) is a follower of x, but with different mode.
3154 static void exchange_leader(ir_node *irn, ir_node *leader) {
3155 ir_mode *mode = get_irn_mode(irn);
3156 if (mode != get_irn_mode(leader)) {
3157 /* The conv is a no-op, so we are free to place it
3158 * either in the block of the leader OR in irn's block.
3159 * Probably placing it into leaders block might reduce
3160 * the number of Conv due to CSE. */
3161 ir_node *block = get_nodes_block(leader);
3162 dbg_info *dbg = get_irn_dbg_info(irn);
3164 leader = new_rd_Conv(dbg, block, leader, mode);
3166 exchange(irn, leader);
3167 } /* exchange_leader */
3170 * Check, if all users of a mode_M node are dead. Use
3171 * the Def-Use edges for this purpose, as they still
3172 * reflect the situation.
3174 static int all_users_are_dead(const ir_node *irn) {
3175 int i, n = get_irn_n_outs(irn);
3177 for (i = 1; i <= n; ++i) {
3178 const ir_node *succ = irn->out[i].use;
3179 const node_t *block = get_irn_node(get_nodes_block(succ));
3182 if (block->type.tv == tarval_unreachable) {
3183 /* block is unreachable */
3186 node = get_irn_node(succ);
3187 if (node->type.tv != tarval_top) {
3188 /* found a reachable user */
3192 /* all users are unreachable */
3194 } /* all_user_are_dead */
3197 * Walker: Find reachable mode_M nodes that have only
3198 * unreachable users. These nodes must be kept later.
3200 static void find_kept_memory(ir_node *irn, void *ctx) {
3201 environment_t *env = ctx;
3202 node_t *node, *block;
3204 if (get_irn_mode(irn) != mode_M)
3207 block = get_irn_node(get_nodes_block(irn));
3208 if (block->type.tv == tarval_unreachable)
3211 node = get_irn_node(irn);
3212 if (node->type.tv == tarval_top)
3215 /* ok, we found a live memory node. */
3216 if (all_users_are_dead(irn)) {
3217 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3218 ARR_APP1(ir_node *, env->kept_memory, irn);
3220 } /* find_kept_memory */
3223 * Post-Walker, apply the analysis results;
3225 static void apply_result(ir_node *irn, void *ctx) {
3226 environment_t *env = ctx;
3227 node_t *node = get_irn_node(irn);
3229 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3230 /* blocks already handled, do not touch the End node */
3232 node_t *block = get_irn_node(get_nodes_block(irn));
3234 if (block->type.tv == tarval_unreachable) {
3235 ir_node *bad = get_irg_bad(current_ir_graph);
3237 /* here, bad might already have a node, but this can be safely ignored
3238 as long as bad has at least ONE valid node */
3239 set_irn_node(bad, node);
3241 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3244 } else if (node->type.tv == tarval_top) {
3245 ir_mode *mode = get_irn_mode(irn);
3247 if (mode == mode_M) {
3248 /* never kill a mode_M node */
3250 ir_node *pred = get_Proj_pred(irn);
3251 node_t *pnode = get_irn_node(pred);
3253 if (pnode->type.tv == tarval_top) {
3254 /* skip the predecessor */
3255 ir_node *mem = get_memop_mem(pred);
3257 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3262 /* leave other nodes, especially PhiM */
3263 } else if (mode == mode_T) {
3264 /* Do not kill mode_T nodes, kill their Projs */
3265 } else if (! is_Unknown(irn)) {
3266 /* don't kick away Unknown's, they might be still needed */
3267 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3269 /* control flow should already be handled at apply_cf() */
3270 assert(mode != mode_X);
3272 /* see comment above */
3273 set_irn_node(unk, node);
3275 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3280 else if (get_irn_mode(irn) == mode_X) {
3283 ir_node *cond = get_Proj_pred(irn);
3285 if (is_Cond(cond)) {
3286 if (only_one_reachable_proj(cond)) {
3287 ir_node *jmp = new_r_Jmp(block->node);
3288 set_irn_node(jmp, node);
3290 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3291 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3295 node_t *sel = get_irn_node(get_Cond_selector(cond));
3296 tarval *tv = sel->type.tv;
3298 if (is_tarval(tv) && tarval_is_constant(tv)) {
3299 /* The selector is a constant, but more
3300 * than one output is active: An unoptimized
3308 /* normal data node */
3309 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3310 tarval *tv = node->type.tv;
3313 * Beware: never replace mode_T nodes by constants. Currently we must mark
3314 * mode_T nodes with constants, but do NOT replace them.
3316 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3317 /* can be replaced by a constant */
3318 ir_node *c = new_Const(tv);
3319 set_irn_node(c, node);
3321 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3322 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3323 exchange_leader(irn, c);
3326 } else if (is_entity(node->type.sym.entity_p)) {
3327 if (! is_SymConst(irn)) {
3328 /* can be replaced by a SymConst */
3329 ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3330 set_irn_node(symc, node);
3333 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3334 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3335 exchange_leader(irn, symc);
3338 } else if (is_Confirm(irn)) {
3339 /* Confirms are always follower, but do not kill them here */
3341 ir_node *leader = get_leader(node);
3343 if (leader != irn) {
3344 int non_strict_phi = 0;
3347 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3348 * as this might create non-strict programs.
3350 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3353 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3354 ir_node *pred = get_Phi_pred(irn, i);
3356 if (is_Unknown(pred)) {
3362 if (! non_strict_phi) {
3363 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3364 if (node->is_follower)
3365 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3367 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3368 exchange_leader(irn, leader);
3375 } /* apply_result */
3378 * Fix the keep-alives by deleting unreachable ones.
3380 static void apply_end(ir_node *end, environment_t *env) {
3381 int i, j, n = get_End_n_keepalives(end);
3385 NEW_ARR_A(ir_node *, in, n);
3387 /* fix the keep alive */
3388 for (i = j = 0; i < n; i++) {
3389 ir_node *ka = get_End_keepalive(end, i);
3390 node_t *node = get_irn_node(ka);
3393 node = get_irn_node(get_nodes_block(ka));
3395 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3399 set_End_keepalives(end, j, in);
3404 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3407 * sets the generic functions to compute.
3409 static void set_compute_functions(void) {
3412 /* set the default compute function */
3413 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3414 ir_op *op = get_irp_opcode(i);
3415 op->ops.generic = (op_func)default_compute;
3418 /* set specific functions */
3434 } /* set_compute_functions */
3439 static void add_memory_keeps(ir_node **kept_memory, int len) {
3440 ir_node *end = get_irg_end(current_ir_graph);
3444 ir_nodeset_init(&set);
3446 /* check, if those nodes are already kept */
3447 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3448 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3450 for (i = len - 1; i >= 0; --i) {
3451 ir_node *ka = kept_memory[i];
3453 if (! ir_nodeset_contains(&set, ka)) {
3454 add_End_keepalive(end, ka);
3457 ir_nodeset_destroy(&set);
3458 } /* add_memory_keeps */
3460 void combo(ir_graph *irg) {
3462 ir_node *initial_bl;
3464 ir_graph *rem = current_ir_graph;
3467 current_ir_graph = irg;
3469 /* register a debug mask */
3470 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3472 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3474 obstack_init(&env.obst);
3475 env.worklist = NULL;
3479 #ifdef DEBUG_libfirm
3480 env.dbg_list = NULL;
3482 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3483 env.type2id_map = pmap_create();
3484 env.kept_memory = NEW_ARR_F(ir_node *, 0);
3485 env.end_idx = get_opt_global_cse() ? 0 : -1;
3486 env.lambda_input = 0;
3489 /* options driving the optimization */
3490 env.commutative = 1;
3491 env.opt_unknown = 1;
3493 assure_irg_outs(irg);
3494 assure_cf_loop(irg);
3496 /* we have our own value_of function */
3497 set_value_of_func(get_node_tarval);
3499 set_compute_functions();
3500 DEBUG_ONLY(part_nr = 0);
3502 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3504 if (env.opt_unknown)
3505 tarval_UNKNOWN = tarval_top;
3507 tarval_UNKNOWN = tarval_bad;
3509 /* create the initial partition and place it on the work list */
3510 env.initial = new_partition(&env);
3511 add_to_worklist(env.initial, &env);
3512 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3514 /* set the hook: from now, every node has a partition and a type */
3515 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook));
3517 /* all nodes on the initial partition have type Top */
3518 env.initial->type_is_T_or_C = 1;
3520 /* Place the START Node's partition on cprop.
3521 Place the START Node on its local worklist. */
3522 initial_bl = get_irg_start_block(irg);
3523 start = get_irn_node(initial_bl);
3524 add_to_cprop(start, &env);
3528 if (env.worklist != NULL)
3530 } while (env.cprop != NULL || env.worklist != NULL);
3532 dump_all_partitions(&env);
3533 check_all_partitions(&env);
3536 dump_ir_block_graph(irg, "-partition");
3539 /* apply the result */
3541 /* check, which nodes must be kept */
3542 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3544 /* kill unreachable control flow */
3545 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3546 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3547 * and fixes assertion because dead cf to dead blocks is NOT removed by
3549 apply_end(get_irg_end(irg), &env);
3550 irg_walk_graph(irg, NULL, apply_result, &env);
3552 len = ARR_LEN(env.kept_memory);
3554 add_memory_keeps(env.kept_memory, len);
3557 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3561 /* control flow might changed */
3562 set_irg_outs_inconsistent(irg);
3563 set_irg_extblk_inconsistent(irg);
3564 set_irg_doms_inconsistent(irg);
3565 set_irg_loopinfo_inconsistent(irg);
3566 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
3569 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3571 /* remove the partition hook */
3572 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
3574 DEL_ARR_F(env.kept_memory);
3575 pmap_destroy(env.type2id_map);
3576 del_set(env.opcode2id_map);
3577 obstack_free(&env.obst, NULL);
3579 /* restore value_of() default behavior */
3580 set_value_of_func(NULL);
3581 current_ir_graph = rem;
3584 /* Creates an ir_graph pass for combo. */
3585 ir_graph_pass_t *combo_pass(const char *name)
3587 return def_graph_pass(name ? name : "combo", combo);