2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Cliff Click's Combined Analysis/Optimization
23 * @author Michael Beck
26 * This is a slightly enhanced version of Cliff Clicks combo algorithm
27 * - support for commutative nodes is added, Add(a,b) and Add(b,a) ARE congruent
28 * - supports all Firm direct (by a data edge) identities except Mux
29 * (Mux can be a 2-input or 1-input identity, only 2-input is implemented yet)
30 * - supports Confirm nodes (handle them like Copies but do NOT remove them)
31 * - let Cmp nodes calculate Top like all othe data nodes: this would let
32 * Mux nodes to calculate Unknown instead of taking the true result
33 * - let Cond(Top) always select FALSE/default: This is tricky. Nodes are only reavaluated
34 * IFF the predecessor changed its type. Because nodes are initialized with Top
35 * this never happens, let all Proj(Cond) be unreachable.
36 * We avoid this condition by the same way we work around Phi: whenever a Block
37 * node is placed on the list, place its Cond nodes (and because they are Tuple
38 * all its Proj-nodes either on the cprop list)
39 * Especially, this changes the meaning of Click's example:
54 * using Click's version while is silent with our.
55 * - support for global congruences is implemented but not tested yet
57 * Note further that we use the terminology from Click's work here, which is different
58 * in some cases from Firm terminology. Especially, Click's type is a
59 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
65 #include "iroptimize.h"
72 #include "irgraph_t.h"
79 #include "iropt_dbg.h"
83 #include "irnodeset.h"
91 /* define this to check that all type translations are monotone */
92 #define VERIFY_MONOTONE
94 /* define this to check the consistency of partitions */
95 #define CHECK_PARTITIONS
97 typedef struct node_t node_t;
98 typedef struct partition_t partition_t;
99 typedef struct opcode_key_t opcode_key_t;
100 typedef struct listmap_entry_t listmap_entry_t;
102 /** The type of the compute function. */
103 typedef void (*compute_func)(node_t *node);
108 struct opcode_key_t {
109 ir_node *irn; /**< An IR node representing this opcode. */
113 * An entry in the list_map.
115 struct listmap_entry_t {
116 void *id; /**< The id. */
117 node_t *list; /**< The associated list for this id. */
118 listmap_entry_t *next; /**< Link to the next entry in the map. */
121 /** We must map id's to lists. */
122 typedef struct listmap_t {
123 set *map; /**< Map id's to listmap_entry_t's */
124 listmap_entry_t *values; /**< List of all values in the map. */
128 * A lattice element. Because we handle constants and symbolic constants different, we
129 * have to use this union.
140 ir_node *node; /**< The IR-node itself. */
141 list_head node_list; /**< Double-linked list of leader/follower entries. */
142 list_head cprop_list; /**< Double-linked partition.cprop list. */
143 partition_t *part; /**< points to the partition this node belongs to */
144 node_t *next; /**< Next node on local list (partition.touched, fallen). */
145 node_t *race_next; /**< Next node on race list. */
146 lattice_elem_t type; /**< The associated lattice element "type". */
147 int max_user_input; /**< Maximum input number of Def-Use edges. */
148 int next_edge; /**< Index of the next Def-Use edge to use. */
149 int n_followers; /**< Number of Follower in the outs set. */
150 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
151 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
152 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
153 unsigned is_follower:1; /**< Set, if this node is a follower. */
154 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
158 * A partition containing congruent nodes.
161 list_head Leader; /**< The head of partition Leader node list. */
162 list_head Follower; /**< The head of partition Follower node list. */
163 list_head cprop; /**< The head of partition.cprop list. */
164 list_head cprop_X; /**< The head of partition.cprop (Cond nodes and its Projs) list. */
165 partition_t *wl_next; /**< Next entry in the work list if any. */
166 partition_t *touched_next; /**< Points to the next partition in the touched set. */
167 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
168 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
169 node_t *touched; /**< The partition.touched set of this partition. */
170 unsigned n_leader; /**< Number of entries in this partition.Leader. */
171 unsigned n_touched; /**< Number of entries in the partition.touched. */
172 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
173 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
174 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
175 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
176 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
178 partition_t *dbg_next; /**< Link all partitions for debugging */
179 unsigned nr; /**< A unique number for (what-)mapping, >0. */
183 typedef struct environment_t {
184 struct obstack obst; /**< obstack to allocate data structures. */
185 partition_t *worklist; /**< The work list. */
186 partition_t *cprop; /**< The constant propagation list. */
187 partition_t *touched; /**< the touched set. */
188 partition_t *initial; /**< The initial partition. */
189 set *opcode2id_map; /**< The opcodeMode->id map. */
190 ir_node **kept_memory; /**< Array of memory nodes that must be kept. */
191 int end_idx; /**< -1 for local and 0 for global congruences. */
192 int lambda_input; /**< Captured argument for lambda_partition(). */
193 unsigned modified:1; /**< Set, if the graph was modified. */
194 unsigned unopt_cf:1; /**< If set, control flow is not optimized due to Unknown. */
195 /* options driving the optimization */
196 unsigned commutative:1; /**< Set, if commutation nodes should be handled specially. */
197 unsigned opt_unknown:1; /**< Set, if non-strict programs should be optimized. */
199 partition_t *dbg_list; /**< List of all partitions. */
203 /** Type of the what function. */
204 typedef void *(*what_func)(const node_t *node, environment_t *env);
206 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
207 #define set_irn_node(irn, node) set_irn_link(irn, node)
209 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
210 #undef tarval_unreachable
211 #define tarval_unreachable tarval_top
214 /** The debug module handle. */
215 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
217 /** The what reason. */
218 DEBUG_ONLY(static const char *what_reason;)
220 /** Next partition number. */
221 DEBUG_ONLY(static unsigned part_nr = 0);
223 /** The tarval returned by Unknown nodes: set to either tarval_bad OR tarval_top. */
224 static ir_tarval *tarval_UNKNOWN;
227 static node_t *identity(node_t *node);
230 * Compare two opcode representatives.
232 static int cmp_irn_opcode(const ir_node *a, const ir_node *b)
236 if ((get_irn_op(a) != get_irn_op(b)) ||
237 (get_irn_mode(a) != get_irn_mode(b)))
240 /* compare if a's in and b's in are of equal length */
241 arity = get_irn_arity(a);
242 if (arity != get_irn_arity(b))
247 * Some ugliness here: Two Blocks having the same
248 * IJmp predecessor would be congruent, which of course is wrong.
249 * We fix it by never letting blocks be congruent
250 * which cannot be detected by combo either.
256 * here, we already know that the nodes are identical except their
259 if (a->op->ops.node_cmp_attr)
260 return a->op->ops.node_cmp_attr(a, b);
263 } /* cmp_irn_opcode */
265 #ifdef CHECK_PARTITIONS
269 static void check_partition(const partition_t *T)
274 list_for_each_entry(node_t, node, &T->Leader, node_list) {
275 assert(node->is_follower == 0);
276 assert(node->flagged == 0);
277 assert(node->part == T);
280 assert(n == T->n_leader);
282 list_for_each_entry(node_t, node, &T->Follower, node_list) {
283 assert(node->is_follower == 1);
284 assert(node->flagged == 0);
285 assert(node->part == T);
287 } /* check_partition */
290 * check that all leader nodes in the partition have the same opcode.
292 static void check_opcode(const partition_t *Z)
295 const ir_node *repr = NULL;
297 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
298 ir_node *irn = node->node;
303 assert(cmp_irn_opcode(repr, irn) == 0);
308 static void check_all_partitions(environment_t *env)
314 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
316 if (! P->type_is_T_or_C)
318 list_for_each_entry(node_t, node, &P->Follower, node_list) {
319 node_t *leader = identity(node);
321 assert(leader != node && leader->part == node->part);
332 static void do_check_list(const node_t *list, int ofs, const partition_t *Z)
337 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
338 for (e = list; e != NULL; e = NEXT(e)) {
339 assert(e->part == Z);
347 } /* ido_check_list */
350 * Check a local list.
352 static void check_list(const node_t *list, const partition_t *Z)
354 do_check_list(list, offsetof(node_t, next), Z);
358 #define check_partition(T)
359 #define check_list(list, Z)
360 #define check_all_partitions(env)
361 #endif /* CHECK_PARTITIONS */
364 static inline lattice_elem_t get_partition_type(const partition_t *X);
367 * Dump partition to output.
369 static void dump_partition(const char *msg, const partition_t *part)
373 lattice_elem_t type = get_partition_type(part);
375 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
376 msg, part->nr, part->type_is_T_or_C ? "*" : "",
377 part->n_leader, type));
378 list_for_each_entry(node_t, node, &part->Leader, node_list) {
379 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
382 if (! list_empty(&part->Follower)) {
383 DB((dbg, LEVEL_2, "\n---\n "));
385 list_for_each_entry(node_t, node, &part->Follower, node_list) {
386 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
390 DB((dbg, LEVEL_2, "\n}\n"));
391 } /* dump_partition */
396 static void do_dump_list(const char *msg, const node_t *node, int ofs)
401 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
403 DB((dbg, LEVEL_3, "%s = {\n ", msg));
404 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
405 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
408 DB((dbg, LEVEL_3, "\n}\n"));
416 static void dump_race_list(const char *msg, const node_t *list)
418 do_dump_list(msg, list, offsetof(node_t, race_next));
419 } /* dump_race_list */
422 * Dumps a local list.
424 static void dump_list(const char *msg, const node_t *list)
426 do_dump_list(msg, list, offsetof(node_t, next));
430 * Dump all partitions.
432 static void dump_all_partitions(const environment_t *env)
434 const partition_t *P;
436 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
437 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
438 dump_partition("", P);
439 } /* dump_all_partitions */
444 static void dump_split_list(const partition_t *list)
446 const partition_t *p;
448 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
449 for (p = list; p != NULL; p = p->split_next)
450 DB((dbg, LEVEL_2, "part%u, ", p->nr));
451 DB((dbg, LEVEL_2, "\n}\n"));
452 } /* dump_split_list */
455 * Dump partition and type for a node.
457 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local)
459 ir_node *irn = local != NULL ? local : n;
460 node_t *node = get_irn_node(irn);
462 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
464 } /* dump_partition_hook */
467 #define dump_partition(msg, part)
468 #define dump_race_list(msg, list)
469 #define dump_list(msg, list)
470 #define dump_all_partitions(env)
471 #define dump_split_list(list)
474 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
476 * Verify that a type transition is monotone
478 static void verify_type(const lattice_elem_t old_type, node_t *node)
480 if (old_type.tv == node->type.tv) {
484 if (old_type.tv == tarval_top) {
485 /* from Top down-to is always allowed */
488 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
492 panic("combo: wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
496 #define verify_type(old_type, node)
500 * Compare two pointer values of a listmap.
502 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
504 const listmap_entry_t *e1 = (listmap_entry_t*)elt;
505 const listmap_entry_t *e2 = (listmap_entry_t*)key;
508 return e1->id != e2->id;
509 } /* listmap_cmp_ptr */
512 * Initializes a listmap.
514 * @param map the listmap
516 static void listmap_init(listmap_t *map)
518 map->map = new_set(listmap_cmp_ptr, 16);
523 * Terminates a listmap.
525 * @param map the listmap
527 static void listmap_term(listmap_t *map)
533 * Return the associated listmap entry for a given id.
535 * @param map the listmap
536 * @param id the id to search for
538 * @return the associated listmap entry for the given id
540 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
542 listmap_entry_t key, *entry;
547 entry = (listmap_entry_t*)set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
549 if (entry->list == NULL) {
550 /* a new entry, put into the list */
551 entry->next = map->values;
558 * Calculate the hash value for an opcode map entry.
560 * @param entry an opcode map entry
562 * @return a hash value for the given opcode map entry
564 static unsigned opcode_hash(const opcode_key_t *entry)
566 /* we cannot use the ir ops hash function here, because it hashes the
568 const ir_node *n = entry->irn;
569 ir_opcode code = get_irn_opcode(n);
570 ir_mode *mode = get_irn_mode(n);
571 unsigned hash = (unsigned)(PTR_TO_INT(mode) * 9 + code) + get_irn_arity(n);
573 if (code == iro_Const)
574 hash ^= (unsigned)HASH_PTR(get_Const_tarval(n));
575 else if (code == iro_Proj)
576 hash += (unsigned)get_Proj_proj(n);
581 * Compare two entries in the opcode map.
583 static int cmp_opcode(const void *elt, const void *key, size_t size)
585 const opcode_key_t *o1 = (opcode_key_t*)elt;
586 const opcode_key_t *o2 = (opcode_key_t*)key;
590 return cmp_irn_opcode(o1->irn, o2->irn);
594 * Compare two Def-Use edges for input position.
596 static int cmp_def_use_edge(const void *a, const void *b)
598 const ir_def_use_edge *ea = (const ir_def_use_edge*)a;
599 const ir_def_use_edge *eb = (const ir_def_use_edge*)b;
601 /* no overrun, because range is [-1, MAXINT] */
602 return ea->pos - eb->pos;
603 } /* cmp_def_use_edge */
606 * We need the Def-Use edges sorted.
608 static void sort_irn_outs(node_t *node)
610 ir_node *irn = node->node;
611 int n_outs = get_irn_n_outs(irn);
614 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
616 node->max_user_input = irn->out[n_outs].pos;
617 } /* sort_irn_outs */
620 * Return the type of a node.
622 * @param irn an IR-node
624 * @return the associated type of this node
626 static inline lattice_elem_t get_node_type(const ir_node *irn)
628 return get_irn_node(irn)->type;
629 } /* get_node_type */
632 * Return the tarval of a node.
634 * @param irn an IR-node
636 * @return the associated type of this node
638 static inline ir_tarval *get_node_tarval(const ir_node *irn)
640 lattice_elem_t type = get_node_type(irn);
642 if (is_tarval(type.tv))
644 return tarval_bottom;
645 } /* get_node_type */
648 * Add a partition to the worklist.
650 static inline void add_to_worklist(partition_t *X, environment_t *env)
652 assert(X->on_worklist == 0);
653 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
654 X->wl_next = env->worklist;
657 } /* add_to_worklist */
660 * Create a new empty partition.
662 * @param env the environment
664 * @return a newly allocated partition
666 static inline partition_t *new_partition(environment_t *env)
668 partition_t *part = OALLOC(&env->obst, partition_t);
670 INIT_LIST_HEAD(&part->Leader);
671 INIT_LIST_HEAD(&part->Follower);
672 INIT_LIST_HEAD(&part->cprop);
673 INIT_LIST_HEAD(&part->cprop_X);
674 part->wl_next = NULL;
675 part->touched_next = NULL;
676 part->cprop_next = NULL;
677 part->split_next = NULL;
678 part->touched = NULL;
681 part->max_user_inputs = 0;
682 part->on_worklist = 0;
683 part->on_touched = 0;
685 part->type_is_T_or_C = 0;
687 part->dbg_next = env->dbg_list;
688 env->dbg_list = part;
689 part->nr = part_nr++;
693 } /* new_partition */
696 * Get the first node from a partition.
698 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)
713 const node_t *first = get_first_node(X);
715 } /* get_partition_type */
718 * Creates a partition node for the given IR-node and place it
719 * into the given partition.
721 * @param irn an IR-node
722 * @param part a partition to place the node in
723 * @param env the environment
725 * @return the created node
727 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env)
729 /* create a partition node and place it in the partition */
730 node_t *node = OALLOC(&env->obst, node_t);
732 INIT_LIST_HEAD(&node->node_list);
733 INIT_LIST_HEAD(&node->cprop_list);
737 node->race_next = NULL;
738 node->type.tv = tarval_top;
739 node->max_user_input = 0;
741 node->n_followers = 0;
742 node->on_touched = 0;
745 node->is_follower = 0;
747 set_irn_node(irn, node);
749 list_add_tail(&node->node_list, &part->Leader);
753 } /* create_partition_node */
756 * Pre-Walker, initialize all Nodes' type to U or top and place
757 * all nodes into the TOP partition.
759 static void create_initial_partitions(ir_node *irn, void *ctx)
761 environment_t *env = (environment_t*)ctx;
762 partition_t *part = env->initial;
765 node = create_partition_node(irn, part, env);
767 if (node->max_user_input > part->max_user_inputs)
768 part->max_user_inputs = node->max_user_input;
771 set_Block_phis(irn, NULL);
773 } /* create_initial_partitions */
776 * Post-Walker, collect all Block-Phi lists, set Cond.
778 static void init_block_phis(ir_node *irn, void *ctx)
783 add_Block_phi(get_nodes_block(irn), irn);
785 } /* init_block_phis */
788 * Add a node to the entry.partition.touched set and
789 * node->partition to the touched set if not already there.
792 * @param env the environment
794 static inline void add_to_touched(node_t *y, environment_t *env)
796 if (y->on_touched == 0) {
797 partition_t *part = y->part;
799 y->next = part->touched;
804 if (part->on_touched == 0) {
805 part->touched_next = env->touched;
807 part->on_touched = 1;
810 check_list(part->touched, part);
812 } /* add_to_touched */
815 * Place a node on the cprop list.
818 * @param env the environment
820 static void add_to_cprop(node_t *y, environment_t *env)
824 /* Add y to y.partition.cprop. */
825 if (y->on_cprop == 0) {
826 partition_t *Y = y->part;
827 ir_node *irn = y->node;
829 /* place Conds and all its Projs on the cprop_X list */
830 if (is_Cond(skip_Proj(irn)))
831 list_add_tail(&y->cprop_list, &Y->cprop_X);
833 list_add_tail(&y->cprop_list, &Y->cprop);
836 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
838 /* place its partition on the cprop list */
839 if (Y->on_cprop == 0) {
840 Y->cprop_next = env->cprop;
846 if (get_irn_mode(irn) == mode_T) {
847 /* mode_T nodes always produce tarval_bottom, so we must explicitly
848 * add its Projs to get constant evaluation to work */
851 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
852 node_t *proj = get_irn_node(get_irn_out(irn, i));
854 add_to_cprop(proj, env);
856 } else if (is_Block(irn)) {
857 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
858 * if someone placed the block. The Block is only placed if the reachability
859 * changes, and this must be re-evaluated in compute_Phi(). */
861 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
862 node_t *p = get_irn_node(phi);
863 add_to_cprop(p, env);
869 * Update the worklist: If Z is on worklist then add Z' to worklist.
870 * Else add the smaller of Z and Z' to worklist.
872 * @param Z the Z partition
873 * @param Z_prime the Z' partition, a previous part of Z
874 * @param env the environment
876 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env)
878 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
879 add_to_worklist(Z_prime, env);
881 add_to_worklist(Z, env);
883 } /* update_worklist */
886 * Make all inputs to x no longer be F.def_use edges.
890 static void move_edges_to_leader(node_t *x)
892 ir_node *irn = x->node;
895 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
896 node_t *pred = get_irn_node(get_irn_n(irn, i));
901 n = get_irn_n_outs(p);
902 for (j = 1; j <= pred->n_followers; ++j) {
903 if (p->out[j].pos == i && p->out[j].use == irn) {
904 /* found a follower edge to x, move it to the Leader */
905 ir_def_use_edge edge = p->out[j];
907 /* remove this edge from the Follower set */
908 p->out[j] = p->out[pred->n_followers];
911 /* sort it into the leader set */
912 for (k = pred->n_followers + 2; k <= n; ++k) {
913 if (p->out[k].pos >= edge.pos)
915 p->out[k - 1] = p->out[k];
917 /* place the new edge here */
918 p->out[k - 1] = edge;
920 /* edge found and moved */
925 } /* move_edges_to_leader */
928 * Split a partition that has NO followers by a local list.
930 * @param Z partition to split
931 * @param g a (non-empty) node list
932 * @param env the environment
934 * @return a new partition containing the nodes of g
936 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env)
938 partition_t *Z_prime;
943 dump_partition("Splitting ", Z);
944 dump_list("by list ", g);
948 /* Remove g from Z. */
949 for (node = g; node != NULL; node = node->next) {
950 assert(node->part == Z);
951 list_del(&node->node_list);
954 assert(n < Z->n_leader);
957 /* Move g to a new partition, Z'. */
958 Z_prime = new_partition(env);
960 for (node = g; node != NULL; node = node->next) {
961 list_add_tail(&node->node_list, &Z_prime->Leader);
962 node->part = Z_prime;
963 if (node->max_user_input > max_input)
964 max_input = node->max_user_input;
966 Z_prime->max_user_inputs = max_input;
967 Z_prime->n_leader = n;
970 check_partition(Z_prime);
972 /* for now, copy the type info tag, it will be adjusted in split_by(). */
973 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
975 update_worklist(Z, Z_prime, env);
977 dump_partition("Now ", Z);
978 dump_partition("Created new ", Z_prime);
980 } /* split_no_followers */
983 * Make the Follower -> Leader transition for a node.
987 static void follower_to_leader(node_t *n)
989 assert(n->is_follower == 1);
991 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
993 move_edges_to_leader(n);
994 list_del(&n->node_list);
995 list_add_tail(&n->node_list, &n->part->Leader);
997 } /* follower_to_leader */
1000 * The environment for one race step.
1002 typedef struct step_env {
1003 node_t *initial; /**< The initial node list. */
1004 node_t *unwalked; /**< The unwalked node list. */
1005 node_t *walked; /**< The walked node list. */
1006 int index; /**< Next index of Follower use_def edge. */
1007 unsigned side; /**< side number. */
1011 * Return non-zero, if a input is a real follower
1013 * @param irn the node to check
1014 * @param input number of the input
1016 static int is_real_follower(const ir_node *irn, int input)
1020 switch (get_irn_opcode(irn)) {
1023 /* ignore the Confirm bound input */
1029 /* ignore the Mux sel input */
1034 /* dead inputs are not follower edges */
1035 ir_node *block = get_nodes_block(irn);
1036 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
1038 if (pred->type.tv == tarval_unreachable)
1048 /* only a Sub x,0 / Shift x,0 might be a follower */
1055 pred = get_irn_node(get_irn_n(irn, input));
1056 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1060 pred = get_irn_node(get_irn_n(irn, input));
1061 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1065 pred = get_irn_node(get_irn_n(irn, input));
1066 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1070 assert(!"opcode not implemented yet");
1074 } /* is_real_follower */
1077 * Do one step in the race.
1079 static int step(step_env *env)
1083 if (env->initial != NULL) {
1084 /* Move node from initial to unwalked */
1086 env->initial = n->race_next;
1088 n->race_next = env->unwalked;
1094 while (env->unwalked != NULL) {
1095 /* let n be the first node in unwalked */
1097 while (env->index < n->n_followers) {
1098 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1100 /* let m be n.F.def_use[index] */
1101 node_t *m = get_irn_node(edge->use);
1103 assert(m->is_follower);
1105 * Some inputs, like the get_Confirm_bound are NOT
1106 * real followers, sort them out.
1108 if (! is_real_follower(m->node, edge->pos)) {
1114 /* only followers from our partition */
1115 if (m->part != n->part)
1118 if ((m->flagged & env->side) == 0) {
1119 m->flagged |= env->side;
1121 if (m->flagged != 3) {
1122 /* visited the first time */
1123 /* add m to unwalked not as first node (we might still need to
1124 check for more follower node */
1125 m->race_next = n->race_next;
1129 /* else already visited by the other side and on the other list */
1132 /* move n to walked */
1133 env->unwalked = n->race_next;
1134 n->race_next = env->walked;
1142 * Clear the flags from a list and check for
1143 * nodes that where touched from both sides.
1145 * @param list the list
1147 static int clear_flags(node_t *list)
1152 for (n = list; n != NULL; n = n->race_next) {
1153 if (n->flagged == 3) {
1154 /* we reach a follower from both sides, this will split congruent
1155 * inputs and make it a leader. */
1156 follower_to_leader(n);
1165 * Split a partition by a local list using the race.
1167 * @param pX pointer to the partition to split, might be changed!
1168 * @param gg a (non-empty) node list
1169 * @param env the environment
1171 * @return a new partition containing the nodes of gg
1173 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env)
1175 partition_t *X = *pX;
1176 partition_t *X_prime;
1179 node_t *g, *h, *node, *t;
1180 int max_input, transitions, winner, shf;
1182 DEBUG_ONLY(static int run = 0;)
1184 DB((dbg, LEVEL_2, "Run %d ", run++));
1185 if (list_empty(&X->Follower)) {
1186 /* if the partition has NO follower, we can use the fast
1187 splitting algorithm. */
1188 return split_no_followers(X, gg, env);
1190 /* else do the race */
1192 dump_partition("Splitting ", X);
1193 dump_list("by list ", gg);
1195 INIT_LIST_HEAD(&tmp);
1197 /* Remove gg from X.Leader and put into g */
1199 for (node = gg; node != NULL; node = node->next) {
1200 assert(node->part == X);
1201 assert(node->is_follower == 0);
1203 list_del(&node->node_list);
1204 list_add_tail(&node->node_list, &tmp);
1205 node->race_next = g;
1210 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1211 node->race_next = h;
1214 /* restore X.Leader */
1215 list_splice(&tmp, &X->Leader);
1217 senv[0].initial = g;
1218 senv[0].unwalked = NULL;
1219 senv[0].walked = NULL;
1223 senv[1].initial = h;
1224 senv[1].unwalked = NULL;
1225 senv[1].walked = NULL;
1230 * Some informations on the race that are not stated clearly in Click's
1232 * 1) A follower stays on the side that reach him first.
1233 * 2) If the other side reches a follower, if will be converted to
1234 * a leader. /This must be done after the race is over, else the
1235 * edges we are iterating on are renumbered./
1236 * 3) /New leader might end up on both sides./
1237 * 4) /If one side ends up with new Leaders, we must ensure that
1238 * they can split out by opcode, hence we have to put _every_
1239 * partition with new Leader nodes on the cprop list, as
1240 * opcode splitting is done by split_by() at the end of
1241 * constant propagation./
1244 if (step(&senv[0])) {
1248 if (step(&senv[1])) {
1253 assert(senv[winner].initial == NULL);
1254 assert(senv[winner].unwalked == NULL);
1256 /* clear flags from walked/unwalked */
1258 transitions = clear_flags(senv[0].unwalked) << shf;
1259 transitions |= clear_flags(senv[0].walked) << shf;
1261 transitions |= clear_flags(senv[1].unwalked) << shf;
1262 transitions |= clear_flags(senv[1].walked) << shf;
1264 dump_race_list("winner ", senv[winner].walked);
1266 /* Move walked_{winner} to a new partition, X'. */
1267 X_prime = new_partition(env);
1270 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1271 list_del(&node->node_list);
1272 node->part = X_prime;
1273 if (node->is_follower) {
1274 list_add_tail(&node->node_list, &X_prime->Follower);
1276 list_add_tail(&node->node_list, &X_prime->Leader);
1279 if (node->max_user_input > max_input)
1280 max_input = node->max_user_input;
1282 X_prime->n_leader = n;
1283 X_prime->max_user_inputs = max_input;
1284 X->n_leader -= X_prime->n_leader;
1286 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1287 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1290 * Even if a follower was not checked by both sides, it might have
1291 * loose its congruence, so we need to check this case for all follower.
1293 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1294 if (identity(node) == node) {
1295 follower_to_leader(node);
1301 check_partition(X_prime);
1303 /* X' is the smaller part */
1304 add_to_worklist(X_prime, env);
1307 * If there where follower to leader transitions, ensure that the nodes
1308 * can be split out if necessary.
1310 if (transitions & 1) {
1311 /* place winner partition on the cprop list */
1312 if (X_prime->on_cprop == 0) {
1313 X_prime->cprop_next = env->cprop;
1314 env->cprop = X_prime;
1315 X_prime->on_cprop = 1;
1318 if (transitions & 2) {
1319 /* place other partition on the cprop list */
1320 if (X->on_cprop == 0) {
1321 X->cprop_next = env->cprop;
1327 dump_partition("Now ", X);
1328 dump_partition("Created new ", X_prime);
1330 /* we have to ensure that the partition containing g is returned */
1340 * Returns non-zero if the i'th input of a Phi node is live.
1342 * @param phi a Phi-node
1343 * @param i an input number
1345 * @return non-zero if the i'th input of the given Phi node is live
1347 static int is_live_input(ir_node *phi, int i)
1350 ir_node *block = get_nodes_block(phi);
1351 ir_node *pred = get_Block_cfgpred(block, i);
1352 lattice_elem_t type = get_node_type(pred);
1354 return type.tv != tarval_unreachable;
1356 /* else it's the control input, always live */
1358 } /* is_live_input */
1361 * Return non-zero if a type is a constant.
1363 static int is_constant_type(lattice_elem_t type)
1365 if (type.tv != tarval_bottom && type.tv != tarval_top)
1368 } /* is_constant_type */
1371 * Check whether a type is neither Top or a constant.
1372 * Note: U is handled like Top here, R is a constant.
1374 * @param type the type to check
1376 static int type_is_neither_top_nor_const(const lattice_elem_t type)
1378 if (is_tarval(type.tv)) {
1379 if (type.tv == tarval_top)
1381 if (tarval_is_constant(type.tv))
1388 } /* type_is_neither_top_nor_const */
1391 * Collect nodes to the touched list.
1393 * @param list the list which contains the nodes that must be evaluated
1394 * @param idx the index of the def_use edge to evaluate
1395 * @param env the environment
1397 static void collect_touched(list_head *list, int idx, environment_t *env)
1400 int end_idx = env->end_idx;
1402 list_for_each_entry(node_t, x, list, node_list) {
1406 /* leader edges start AFTER follower edges */
1407 x->next_edge = x->n_followers + 1;
1409 num_edges = get_irn_n_outs(x->node);
1411 /* for all edges in x.L.def_use_{idx} */
1412 while (x->next_edge <= num_edges) {
1413 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1416 /* check if we have necessary edges */
1417 if (edge->pos > idx)
1424 /* only non-commutative nodes */
1425 if (env->commutative &&
1426 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1429 /* ignore the "control input" for non-pinned nodes
1430 if we are running in GCSE mode */
1431 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1434 y = get_irn_node(succ);
1435 assert(get_irn_n(succ, idx) == x->node);
1437 /* ignore block edges touching followers */
1438 if (idx == -1 && y->is_follower)
1441 if (is_constant_type(y->type)) {
1442 unsigned code = get_irn_opcode(succ);
1443 if (code == iro_Sub || code == iro_Cmp)
1444 add_to_cprop(y, env);
1447 /* Partitions of constants should not be split simply because their Nodes have unequal
1448 functions or incongruent inputs. */
1449 if (type_is_neither_top_nor_const(y->type) &&
1450 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1451 add_to_touched(y, env);
1455 } /* collect_touched */
1458 * Collect commutative nodes to the touched list.
1460 * @param list the list which contains the nodes that must be evaluated
1461 * @param env the environment
1463 static void collect_commutative_touched(list_head *list, environment_t *env)
1467 list_for_each_entry(node_t, x, list, node_list) {
1470 num_edges = get_irn_n_outs(x->node);
1472 x->next_edge = x->n_followers + 1;
1474 /* for all edges in x.L.def_use_{idx} */
1475 while (x->next_edge <= num_edges) {
1476 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1479 /* check if we have necessary edges */
1489 /* only commutative nodes */
1490 if (!is_op_commutative(get_irn_op(succ)))
1493 y = get_irn_node(succ);
1494 if (is_constant_type(y->type)) {
1495 unsigned code = get_irn_opcode(succ);
1496 if (code == iro_Eor)
1497 add_to_cprop(y, env);
1500 /* Partitions of constants should not be split simply because their Nodes have unequal
1501 functions or incongruent inputs. */
1502 if (type_is_neither_top_nor_const(y->type)) {
1503 add_to_touched(y, env);
1507 } /* collect_commutative_touched */
1510 * Split the partitions if caused by the first entry on the worklist.
1512 * @param env the environment
1514 static void cause_splits(environment_t *env)
1516 partition_t *X, *Z, *N;
1519 /* remove the first partition from the worklist */
1521 env->worklist = X->wl_next;
1524 dump_partition("Cause_split: ", X);
1526 if (env->commutative) {
1527 /* handle commutative nodes first */
1529 /* empty the touched set: already done, just clear the list */
1530 env->touched = NULL;
1532 collect_commutative_touched(&X->Leader, env);
1533 collect_commutative_touched(&X->Follower, env);
1535 for (Z = env->touched; Z != NULL; Z = N) {
1537 node_t *touched = Z->touched;
1538 node_t *touched_aa = NULL;
1539 node_t *touched_ab = NULL;
1540 unsigned n_touched_aa = 0;
1541 unsigned n_touched_ab = 0;
1543 assert(Z->touched != NULL);
1545 /* beware, split might change Z */
1546 N = Z->touched_next;
1548 /* remove it from the touched set */
1551 /* Empty local Z.touched. */
1552 for (e = touched; e != NULL; e = n) {
1553 node_t *left = get_irn_node(get_irn_n(e->node, 0));
1554 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1556 assert(e->is_follower == 0);
1561 * Note: op(a, a) is NOT congruent to op(a, b).
1562 * So, we must split the touched list.
1564 if (left->part == right->part) {
1565 e->next = touched_aa;
1569 e->next = touched_ab;
1574 assert(n_touched_aa + n_touched_ab == Z->n_touched);
1578 if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1579 partition_t *Z_prime = Z;
1580 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1581 split(&Z_prime, touched_aa, env);
1583 assert(n_touched_aa <= Z->n_leader);
1585 if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1586 partition_t *Z_prime = Z;
1587 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1588 split(&Z_prime, touched_ab, env);
1590 assert(n_touched_ab <= Z->n_leader);
1594 /* combine temporary leader and follower list */
1595 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1596 /* empty the touched set: already done, just clear the list */
1597 env->touched = NULL;
1599 collect_touched(&X->Leader, idx, env);
1600 collect_touched(&X->Follower, idx, env);
1602 for (Z = env->touched; Z != NULL; Z = N) {
1604 node_t *touched = Z->touched;
1605 unsigned n_touched = Z->n_touched;
1607 assert(Z->touched != NULL);
1609 /* beware, split might change Z */
1610 N = Z->touched_next;
1612 /* remove it from the touched set */
1615 /* Empty local Z.touched. */
1616 for (e = touched; e != NULL; e = e->next) {
1617 assert(e->is_follower == 0);
1623 if (0 < n_touched && n_touched < Z->n_leader) {
1624 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1625 split(&Z, touched, env);
1627 assert(n_touched <= Z->n_leader);
1630 } /* cause_splits */
1633 * Implements split_by_what(): Split a partition by characteristics given
1634 * by the what function.
1636 * @param X the partition to split
1637 * @param What a function returning an Id for every node of the partition X
1638 * @param P a list to store the result partitions
1639 * @param env the environment
1643 static partition_t *split_by_what(partition_t *X, what_func What,
1644 partition_t **P, environment_t *env)
1648 listmap_entry_t *iter;
1651 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1653 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1654 void *id = What(x, env);
1655 listmap_entry_t *entry;
1658 /* input not allowed, ignore */
1661 /* Add x to map[What(x)]. */
1662 entry = listmap_find(&map, id);
1663 x->next = entry->list;
1666 /* Let P be a set of Partitions. */
1668 /* for all sets S except one in the range of map do */
1669 for (iter = map.values; iter != NULL; iter = iter->next) {
1670 if (iter->next == NULL) {
1671 /* this is the last entry, ignore */
1676 /* Add SPLIT( X, S ) to P. */
1677 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1678 R = split(&X, S, env);
1688 } /* split_by_what */
1690 /** lambda n.(n.type) */
1691 static void *lambda_type(const node_t *node, environment_t *env)
1694 return node->type.tv;
1697 /** lambda n.(n.opcode) */
1698 static void *lambda_opcode(const node_t *node, environment_t *env)
1700 opcode_key_t key, *entry;
1702 key.irn = node->node;
1704 entry = (opcode_key_t*)set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1706 } /* lambda_opcode */
1708 /** lambda n.(n[i].partition) */
1709 static void *lambda_partition(const node_t *node, environment_t *env)
1711 ir_node *skipped = skip_Proj(node->node);
1714 int i = env->lambda_input;
1716 if (i >= get_irn_arity(node->node)) {
1718 * We are outside the allowed range: This can happen even
1719 * if we have split by opcode first: doing so might move Followers
1720 * to Leaders and those will have a different opcode!
1721 * Note that in this case the partition is on the cprop list and will be
1727 /* ignore the "control input" for non-pinned nodes
1728 if we are running in GCSE mode */
1729 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1732 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1733 p = get_irn_node(pred);
1735 } /* lambda_partition */
1737 /** lambda n.(n[i].partition) for commutative nodes */
1738 static void *lambda_commutative_partition(const node_t *node, environment_t *env)
1740 ir_node *irn = node->node;
1741 ir_node *skipped = skip_Proj(irn);
1742 ir_node *pred, *left, *right;
1744 partition_t *pl, *pr;
1745 int i = env->lambda_input;
1747 if (i >= get_irn_arity(node->node)) {
1749 * We are outside the allowed range: This can happen even
1750 * if we have split by opcode first: doing so might move Followers
1751 * to Leaders and those will have a different opcode!
1752 * Note that in this case the partition is on the cprop list and will be
1758 /* ignore the "control input" for non-pinned nodes
1759 if we are running in GCSE mode */
1760 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1764 pred = get_irn_n(skipped, i);
1765 p = get_irn_node(pred);
1769 if (is_op_commutative(get_irn_op(irn))) {
1770 /* normalize partition order by returning the "smaller" on input 0,
1771 the "bigger" on input 1. */
1772 left = get_binop_left(irn);
1773 pl = get_irn_node(left)->part;
1774 right = get_binop_right(irn);
1775 pr = get_irn_node(right)->part;
1778 return pl < pr ? pl : pr;
1780 return pl > pr ? pl : pr;
1782 /* a not split out Follower */
1783 pred = get_irn_n(irn, i);
1784 p = get_irn_node(pred);
1788 } /* lambda_commutative_partition */
1791 * Returns true if a type is a constant (and NOT Top
1794 static int is_con(const lattice_elem_t type)
1796 /* be conservative */
1797 if (is_tarval(type.tv))
1798 return tarval_is_constant(type.tv);
1799 return is_entity(type.sym.entity_p);
1803 * Implements split_by().
1805 * @param X the partition to split
1806 * @param env the environment
1808 static void split_by(partition_t *X, environment_t *env)
1810 partition_t *I, *P = NULL;
1813 dump_partition("split_by", X);
1815 if (X->n_leader == 1) {
1816 /* we have only one leader, no need to split, just check its type */
1817 node_t *x = get_first_node(X);
1818 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1822 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1823 P = split_by_what(X, lambda_type, &P, env);
1826 /* adjust the type tags, we have split partitions by type */
1827 for (I = P; I != NULL; I = I->split_next) {
1828 node_t *x = get_first_node(I);
1829 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1836 if (Y->n_leader > 1) {
1837 /* we do not want split the TOP or constant partitions */
1838 if (! Y->type_is_T_or_C) {
1839 partition_t *Q = NULL;
1841 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1842 Q = split_by_what(Y, lambda_opcode, &Q, env);
1849 if (Z->n_leader > 1) {
1850 const node_t *first = get_first_node(Z);
1851 int arity = get_irn_arity(first->node);
1853 what_func what = lambda_partition;
1854 DEBUG_ONLY(char buf[64];)
1856 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1857 what = lambda_commutative_partition;
1860 * BEWARE: during splitting by input 2 for instance we might
1861 * create new partitions which are different by input 1, so collect
1862 * them and split further.
1864 Z->split_next = NULL;
1867 for (input = arity - 1; input >= -1; --input) {
1869 partition_t *Z_prime = R;
1872 if (Z_prime->n_leader > 1) {
1873 env->lambda_input = input;
1874 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1875 DEBUG_ONLY(what_reason = buf;)
1876 S = split_by_what(Z_prime, what, &S, env);
1879 Z_prime->split_next = S;
1882 } while (R != NULL);
1887 } while (Q != NULL);
1890 } while (P != NULL);
1894 * (Re-)compute the type for a given node.
1896 * @param node the node
1898 static void default_compute(node_t *node)
1901 ir_node *irn = node->node;
1903 /* if any of the data inputs have type top, the result is type top */
1904 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1905 ir_node *pred = get_irn_n(irn, i);
1906 node_t *p = get_irn_node(pred);
1908 if (p->type.tv == tarval_top) {
1909 node->type.tv = tarval_top;
1914 if (get_irn_mode(node->node) == mode_X)
1915 node->type.tv = tarval_reachable;
1917 node->type.tv = computed_value(irn);
1918 } /* default_compute */
1921 * (Re-)compute the type for a Block node.
1923 * @param node the node
1925 static void compute_Block(node_t *node)
1928 ir_node *block = node->node;
1930 if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
1931 /* start block and labelled blocks are always reachable */
1932 node->type.tv = tarval_reachable;
1936 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1937 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1939 if (pred->type.tv == tarval_reachable) {
1940 /* A block is reachable, if at least of predecessor is reachable. */
1941 node->type.tv = tarval_reachable;
1945 node->type.tv = tarval_top;
1946 } /* compute_Block */
1949 * (Re-)compute the type for a Bad node.
1951 * @param node the node
1953 static void compute_Bad(node_t *node)
1955 /* Bad nodes ALWAYS compute Top */
1956 node->type.tv = tarval_top;
1960 * (Re-)compute the type for an Unknown node.
1962 * @param node the node
1964 static void compute_Unknown(node_t *node)
1966 /* While Unknown nodes should compute Top this is dangerous:
1967 * a Top input to a Cond would lead to BOTH control flows unreachable.
1968 * While this is correct in the given semantics, it would destroy the Firm
1971 * It would be safe to compute Top IF it can be assured, that only Cmp
1972 * nodes are inputs to Conds. We check that first.
1973 * This is the way Frontends typically build Firm, but some optimizations
1974 * (jump threading for instance) might replace them by Phib's...
1976 node->type.tv = tarval_UNKNOWN;
1977 } /* compute_Unknown */
1980 * (Re-)compute the type for a Jmp node.
1982 * @param node the node
1984 static void compute_Jmp(node_t *node)
1986 node_t *block = get_irn_node(get_nodes_block(node->node));
1988 node->type = block->type;
1992 * (Re-)compute the type for the Return node.
1994 * @param node the node
1996 static void compute_Return(node_t *node)
1998 /* The Return node is NOT dead if it is in a reachable block.
1999 * This is already checked in compute(). so we can return
2000 * Reachable here. */
2001 node->type.tv = tarval_reachable;
2002 } /* compute_Return */
2005 * (Re-)compute the type for the End node.
2007 * @param node the node
2009 static void compute_End(node_t *node)
2011 /* the End node is NOT dead of course */
2012 node->type.tv = tarval_reachable;
2016 * (Re-)compute the type for a Call.
2018 * @param node the node
2020 static void compute_Call(node_t *node)
2023 * A Call computes always bottom, even if it has Unknown
2026 node->type.tv = tarval_bottom;
2027 } /* compute_Call */
2030 * (Re-)compute the type for a SymConst node.
2032 * @param node the node
2034 static void compute_SymConst(node_t *node)
2036 ir_node *irn = node->node;
2037 node_t *block = get_irn_node(get_nodes_block(irn));
2039 if (block->type.tv == tarval_unreachable) {
2040 node->type.tv = tarval_top;
2043 switch (get_SymConst_kind(irn)) {
2044 case symconst_addr_ent:
2045 node->type.sym = get_SymConst_symbol(irn);
2048 node->type.tv = computed_value(irn);
2050 } /* compute_SymConst */
2053 * (Re-)compute the type for a Phi node.
2055 * @param node the node
2057 static void compute_Phi(node_t *node)
2060 ir_node *phi = node->node;
2061 lattice_elem_t type;
2063 /* if a Phi is in a unreachable block, its type is TOP */
2064 node_t *block = get_irn_node(get_nodes_block(phi));
2066 if (block->type.tv == tarval_unreachable) {
2067 node->type.tv = tarval_top;
2071 /* Phi implements the Meet operation */
2072 type.tv = tarval_top;
2073 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2074 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2075 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2077 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2078 /* ignore TOP inputs: We must check here for unreachable blocks,
2079 because Firm constants live in the Start Block are NEVER Top.
2080 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2081 comes from a unreachable input. */
2084 if (pred->type.tv == tarval_bottom) {
2085 node->type.tv = tarval_bottom;
2087 } else if (type.tv == tarval_top) {
2088 /* first constant found */
2090 } else if (type.tv != pred->type.tv) {
2091 /* different constants or tarval_bottom */
2092 node->type.tv = tarval_bottom;
2095 /* else nothing, constants are the same */
2101 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2103 * @param node the node
2105 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)
2149 ir_node *sub = node->node;
2150 node_t *l = get_irn_node(get_Sub_left(sub));
2151 node_t *r = get_irn_node(get_Sub_right(sub));
2152 lattice_elem_t a = l->type;
2153 lattice_elem_t b = r->type;
2156 if (a.tv == tarval_top || b.tv == tarval_top) {
2157 node->type.tv = tarval_top;
2158 } else if (is_con(a) && is_con(b)) {
2159 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2160 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2161 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2163 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2166 node->type.tv = tarval_bottom;
2168 } else if (r->part == l->part &&
2169 (!mode_is_float(get_irn_mode(l->node)))) {
2171 * BEWARE: a - a is NOT always 0 for floating Point values, as
2172 * NaN op NaN = NaN, so we must check this here.
2174 ir_mode *mode = get_irn_mode(sub);
2175 tv = get_mode_null(mode);
2177 /* if the node was ONCE evaluated by all constants, but now
2178 this breaks AND we get from the argument partitions a different
2179 result, switch to bottom.
2180 This happens because initially all nodes are in the same partition ... */
2181 if (node->type.tv != tv)
2185 node->type.tv = tarval_bottom;
2190 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2192 * @param node the node
2194 static void compute_Eor(node_t *node)
2196 ir_node *eor = node->node;
2197 node_t *l = get_irn_node(get_Eor_left(eor));
2198 node_t *r = get_irn_node(get_Eor_right(eor));
2199 lattice_elem_t a = l->type;
2200 lattice_elem_t b = r->type;
2203 if (a.tv == tarval_top || b.tv == tarval_top) {
2204 node->type.tv = tarval_top;
2205 } else if (is_con(a) && is_con(b)) {
2206 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2207 node->type.tv = tarval_eor(a.tv, b.tv);
2208 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2210 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2213 node->type.tv = tarval_bottom;
2215 } else if (r->part == l->part) {
2216 ir_mode *mode = get_irn_mode(eor);
2217 tv = get_mode_null(mode);
2219 /* if the node was ONCE evaluated by all constants, but now
2220 this breaks AND we get from the argument partitions a different
2221 result, switch to bottom.
2222 This happens because initially all nodes are in the same partition ... */
2223 if (node->type.tv != tv)
2227 node->type.tv = tarval_bottom;
2232 * (Re-)compute the type for Cmp.
2234 * @param node the node
2236 static void compute_Cmp(node_t *node)
2238 ir_node *cmp = node->node;
2239 node_t *l = get_irn_node(get_Cmp_left(cmp));
2240 node_t *r = get_irn_node(get_Cmp_right(cmp));
2241 lattice_elem_t a = l->type;
2242 lattice_elem_t b = r->type;
2243 ir_relation relation = get_Cmp_relation(cmp);
2246 if (a.tv == tarval_top || b.tv == tarval_top) {
2247 node->type.tv = tarval_undefined;
2248 } else if (is_con(a) && is_con(b)) {
2249 default_compute(node);
2252 * BEWARE: a == a is NOT always True for floating Point values, as
2253 * NaN != NaN is defined, so we must check this here.
2254 * (while for some pnc we could still optimize we have to stay
2255 * consistent with compute_Cmp, so don't do anything for floats)
2257 } else if (r->part == l->part && !mode_is_float(get_irn_mode(l->node))) {
2258 tv = relation & ir_relation_equal ? tarval_b_true : tarval_b_false;
2260 /* if the node was ONCE evaluated by all constants, but now
2261 this breaks AND we get from the argument partitions a different
2262 result, switch to bottom.
2263 This happens because initially all nodes are in the same partition ... */
2264 if (node->type.tv != tv)
2268 node->type.tv = tarval_bottom;
2273 * (Re-)compute the type for a Proj(Cond).
2275 * @param node the node
2276 * @param cond the predecessor Cond node
2278 static void compute_Proj_Cond(node_t *node, ir_node *cond)
2280 ir_node *proj = node->node;
2281 long pnc = get_Proj_proj(proj);
2282 ir_node *sel = get_Cond_selector(cond);
2283 node_t *selector = get_irn_node(sel);
2286 * Note: it is crucial for the monotony that the Proj(Cond)
2287 * are evaluates after all predecessors of the Cond selector are
2293 * Due to the fact that 0 is a const, the Cmp gets immediately
2294 * on the cprop list. It will be evaluated before x is evaluated,
2295 * might leaving x as Top. When later x is evaluated, the Cmp
2296 * might change its value.
2297 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2298 * gets R, and later changed to F if Cmp is evaluated to True!
2300 * We prevent this by putting Conds in an extra cprop_X queue, which
2301 * gets evaluated after the cprop queue is empty.
2303 * Note that this even happens with Click's original algorithm, if
2304 * Cmp(x, 0) is evaluated to True first and later changed to False
2305 * if x was Top first and later changed to a Const ...
2306 * It is unclear how Click solved that problem ...
2308 * However, in rare cases even this does not help, if a Top reaches
2309 * a compare through a Phi, than Proj(Cond) is evaluated changing
2310 * the type of the Phi to something other.
2311 * So, we take the last resort and bind the type to R once
2314 * (This might be even the way Click works around the whole problem).
2316 * Finally, we may miss some optimization possibilities due to this:
2321 * If Top reaches the if first, than we decide for != here.
2322 * If y later is evaluated to 0, we cannot revert this decision
2323 * and must live with both outputs enabled. If this happens,
2324 * we get an unresolved if (true) in the code ...
2326 * In Click's version where this decision is done at the Cmp,
2327 * the Cmp is NOT optimized away than (if y evaluated to 1
2328 * for instance) and we get a if (1 == 0) here ...
2330 * Both solutions are suboptimal.
2331 * At least, we could easily detect this problem and run
2332 * cf_opt() (or even combo) again :-(
2334 if (node->type.tv == tarval_reachable)
2337 if (get_irn_mode(sel) == mode_b) {
2339 if (pnc == pn_Cond_true) {
2340 if (selector->type.tv == tarval_b_false) {
2341 node->type.tv = tarval_unreachable;
2342 } else if (selector->type.tv == tarval_b_true) {
2343 node->type.tv = tarval_reachable;
2344 } else if (selector->type.tv == tarval_bottom) {
2345 node->type.tv = tarval_reachable;
2347 assert(selector->type.tv == tarval_top);
2348 if (tarval_UNKNOWN == tarval_top) {
2349 /* any condition based on Top is "!=" */
2350 node->type.tv = tarval_unreachable;
2352 node->type.tv = tarval_unreachable;
2356 assert(pnc == pn_Cond_false);
2358 if (selector->type.tv == tarval_b_false) {
2359 node->type.tv = tarval_reachable;
2360 } else if (selector->type.tv == tarval_b_true) {
2361 node->type.tv = tarval_unreachable;
2362 } else if (selector->type.tv == tarval_bottom) {
2363 node->type.tv = tarval_reachable;
2365 assert(selector->type.tv == tarval_top);
2366 if (tarval_UNKNOWN == tarval_top) {
2367 /* any condition based on Top is "!=" */
2368 node->type.tv = tarval_reachable;
2370 node->type.tv = tarval_unreachable;
2376 if (selector->type.tv == tarval_bottom) {
2377 node->type.tv = tarval_reachable;
2378 } else if (selector->type.tv == tarval_top) {
2379 if (tarval_UNKNOWN == tarval_top &&
2380 pnc == get_Cond_default_proj(cond)) {
2381 /* a switch based of Top is always "default" */
2382 node->type.tv = tarval_reachable;
2384 node->type.tv = tarval_unreachable;
2387 long value = get_tarval_long(selector->type.tv);
2388 if (pnc == get_Cond_default_proj(cond)) {
2389 /* default switch, have to check ALL other cases */
2392 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2393 ir_node *succ = get_irn_out(cond, i);
2397 if (value == get_Proj_proj(succ)) {
2398 /* we found a match, will NOT take the default case */
2399 node->type.tv = tarval_unreachable;
2403 /* all cases checked, no match, will take default case */
2404 node->type.tv = tarval_reachable;
2407 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2411 } /* compute_Proj_Cond */
2414 * (Re-)compute the type for a Proj-Node.
2416 * @param node the node
2418 static void compute_Proj(node_t *node)
2420 ir_node *proj = node->node;
2421 ir_mode *mode = get_irn_mode(proj);
2422 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2423 ir_node *pred = get_Proj_pred(proj);
2425 if (block->type.tv == tarval_unreachable) {
2426 /* a Proj in a unreachable Block stay Top */
2427 node->type.tv = tarval_top;
2430 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred)) {
2431 /* if the predecessor is Top, its Proj follow */
2432 node->type.tv = tarval_top;
2436 if (mode == mode_M) {
2437 /* mode M is always bottom */
2438 node->type.tv = tarval_bottom;
2440 } else if (mode == mode_X) {
2441 /* handle mode_X nodes */
2442 switch (get_irn_opcode(pred)) {
2444 /* the Proj_X from the Start is always reachable.
2445 However this is already handled at the top. */
2446 node->type.tv = tarval_reachable;
2449 compute_Proj_Cond(node, pred);
2456 default_compute(node);
2457 } /* compute_Proj */
2460 * (Re-)compute the type for a Confirm.
2462 * @param node the node
2464 static void compute_Confirm(node_t *node)
2466 ir_node *confirm = node->node;
2467 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2469 if (get_Confirm_relation(confirm) == ir_relation_equal) {
2470 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2472 if (is_con(bound->type)) {
2473 /* is equal to a constant */
2474 node->type = bound->type;
2478 /* a Confirm is a copy OR a Const */
2479 node->type = pred->type;
2480 } /* compute_Confirm */
2483 * (Re-)compute the type for a given node.
2485 * @param node the node
2487 static void compute(node_t *node)
2489 ir_node *irn = node->node;
2492 #ifndef VERIFY_MONOTONE
2494 * Once a node reaches bottom, the type cannot fall further
2495 * in the lattice and we can stop computation.
2496 * Do not take this exit if the monotony verifier is
2497 * enabled to catch errors.
2499 if (node->type.tv == tarval_bottom)
2503 if (!is_Block(irn)) {
2504 /* for pinned nodes, check its control input */
2505 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2506 node_t *block = get_irn_node(get_nodes_block(irn));
2508 if (block->type.tv == tarval_unreachable) {
2509 node->type.tv = tarval_top;
2515 func = (compute_func)node->node->op->ops.generic;
2521 * Identity functions: Note that one might think that identity() is just a
2522 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2523 * here, because it expects that the identity node is one of the inputs, which is NOT
2524 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2525 * So, we have our own implementation, which copies some parts of equivalent_node()
2529 * Calculates the Identity for Phi nodes
2531 static node_t *identity_Phi(node_t *node)
2533 ir_node *phi = node->node;
2534 ir_node *block = get_nodes_block(phi);
2535 node_t *n_part = NULL;
2538 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2539 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2541 if (pred_X->type.tv == tarval_reachable) {
2542 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2546 else if (n_part->part != pred->part) {
2547 /* incongruent inputs, not a follower */
2552 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2553 * tarval_top, is in the TOP partition and should NOT being split! */
2554 assert(n_part != NULL);
2556 } /* identity_Phi */
2559 * Calculates the Identity for commutative 0 neutral nodes.
2561 static node_t *identity_comm_zero_binop(node_t *node)
2563 ir_node *op = node->node;
2564 node_t *a = get_irn_node(get_binop_left(op));
2565 node_t *b = get_irn_node(get_binop_right(op));
2566 ir_mode *mode = get_irn_mode(op);
2569 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2570 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2573 /* node: no input should be tarval_top, else the binop would be also
2574 * Top and not being split. */
2575 zero = get_mode_null(mode);
2576 if (a->type.tv == zero)
2578 if (b->type.tv == zero)
2581 } /* identity_comm_zero_binop */
2584 * Calculates the Identity for Shift nodes.
2586 static node_t *identity_shift(node_t *node)
2588 ir_node *op = node->node;
2589 node_t *b = get_irn_node(get_binop_right(op));
2590 ir_mode *mode = get_irn_mode(b->node);
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 (b->type.tv == zero)
2597 return get_irn_node(get_binop_left(op));
2599 } /* identity_shift */
2602 * Calculates the Identity for Mul nodes.
2604 static node_t *identity_Mul(node_t *node)
2606 ir_node *op = node->node;
2607 node_t *a = get_irn_node(get_Mul_left(op));
2608 node_t *b = get_irn_node(get_Mul_right(op));
2609 ir_mode *mode = get_irn_mode(op);
2612 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2613 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2616 /* node: no input should be tarval_top, else the binop would be also
2617 * Top and not being split. */
2618 one = get_mode_one(mode);
2619 if (a->type.tv == one)
2621 if (b->type.tv == one)
2624 } /* identity_Mul */
2627 * Calculates the Identity for Sub nodes.
2629 static node_t *identity_Sub(node_t *node)
2631 ir_node *sub = node->node;
2632 node_t *b = get_irn_node(get_Sub_right(sub));
2633 ir_mode *mode = get_irn_mode(sub);
2635 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2636 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2639 /* node: no input should be tarval_top, else the binop would be also
2640 * Top and not being split. */
2641 if (b->type.tv == get_mode_null(mode))
2642 return get_irn_node(get_Sub_left(sub));
2644 } /* identity_Sub */
2647 * Calculates the Identity for And nodes.
2649 static node_t *identity_And(node_t *node)
2651 ir_node *andnode = node->node;
2652 node_t *a = get_irn_node(get_And_left(andnode));
2653 node_t *b = get_irn_node(get_And_right(andnode));
2654 ir_tarval *neutral = get_mode_all_one(get_irn_mode(andnode));
2656 /* node: no input should be tarval_top, else the And would be also
2657 * Top and not being split. */
2658 if (a->type.tv == neutral)
2660 if (b->type.tv == neutral)
2663 } /* identity_And */
2666 * Calculates the Identity for Confirm nodes.
2668 static node_t *identity_Confirm(node_t *node)
2670 ir_node *confirm = node->node;
2672 /* a Confirm is always a Copy */
2673 return get_irn_node(get_Confirm_value(confirm));
2674 } /* identity_Confirm */
2677 * Calculates the Identity for Mux nodes.
2679 static node_t *identity_Mux(node_t *node)
2681 ir_node *mux = node->node;
2682 node_t *t = get_irn_node(get_Mux_true(mux));
2683 node_t *f = get_irn_node(get_Mux_false(mux));
2686 if (t->part == f->part)
2689 /* for now, the 1-input identity is not supported */
2691 sel = get_irn_node(get_Mux_sel(mux));
2693 /* Mux sel input is mode_b, so it is always a tarval */
2694 if (sel->type.tv == tarval_b_true)
2696 if (sel->type.tv == tarval_b_false)
2700 } /* identity_Mux */
2703 * Calculates the Identity for nodes.
2705 static node_t *identity(node_t *node)
2707 ir_node *irn = node->node;
2709 switch (get_irn_opcode(irn)) {
2711 return identity_Phi(node);
2713 return identity_Mul(node);
2717 return identity_comm_zero_binop(node);
2722 return identity_shift(node);
2724 return identity_And(node);
2726 return identity_Sub(node);
2728 return identity_Confirm(node);
2730 return identity_Mux(node);
2737 * Node follower is a (new) follower of leader, segregate Leader
2740 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader)
2742 ir_node *l = leader->node;
2743 int j, i, n = get_irn_n_outs(l);
2745 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2746 /* The leader edges must remain sorted, but follower edges can
2748 for (i = leader->n_followers + 1; i <= n; ++i) {
2749 if (l->out[i].use == follower) {
2750 ir_def_use_edge t = l->out[i];
2752 for (j = i - 1; j >= leader->n_followers + 1; --j)
2753 l->out[j + 1] = l->out[j];
2754 ++leader->n_followers;
2755 l->out[leader->n_followers] = t;
2759 } /* segregate_def_use_chain_1 */
2762 * Node follower is a (new) follower segregate its Leader
2765 * @param follower the follower IR node
2767 static void segregate_def_use_chain(const ir_node *follower)
2771 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2772 node_t *pred = get_irn_node(get_irn_n(follower, i));
2774 segregate_def_use_chain_1(follower, pred);
2776 } /* segregate_def_use_chain */
2779 * Propagate constant evaluation.
2781 * @param env the environment
2783 static void propagate(environment_t *env)
2787 lattice_elem_t old_type;
2789 unsigned n_fallen, old_type_was_T_or_C;
2792 while (env->cprop != NULL) {
2793 void *oldopcode = NULL;
2795 /* remove the first partition X from cprop */
2798 env->cprop = X->cprop_next;
2800 old_type_was_T_or_C = X->type_is_T_or_C;
2802 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2806 int cprop_empty = list_empty(&X->cprop);
2807 int cprop_X_empty = list_empty(&X->cprop_X);
2809 if (cprop_empty && cprop_X_empty) {
2810 /* both cprop lists are empty */
2814 /* remove the first Node x from X.cprop */
2816 /* Get a node from the cprop_X list only if
2817 * all data nodes are processed.
2818 * This ensures, that all inputs of the Cond
2819 * predecessor are processed if its type is still Top.
2821 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2823 x = list_entry(X->cprop.next, node_t, cprop_list);
2826 //assert(x->part == X);
2827 list_del(&x->cprop_list);
2830 if (x->is_follower && identity(x) == x) {
2831 /* check the opcode first */
2832 if (oldopcode == NULL) {
2833 oldopcode = lambda_opcode(get_first_node(X), env);
2835 if (oldopcode != lambda_opcode(x, env)) {
2836 if (x->on_fallen == 0) {
2837 /* different opcode -> x falls out of this partition */
2842 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2846 /* x will make the follower -> leader transition */
2847 follower_to_leader(x);
2850 /* compute a new type for x */
2852 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2854 if (x->type.tv != old_type.tv) {
2855 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2856 verify_type(old_type, x);
2858 if (x->on_fallen == 0) {
2859 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2860 not already on the list. */
2865 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2867 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2868 ir_node *succ = get_irn_out(x->node, i);
2869 node_t *y = get_irn_node(succ);
2871 /* Add y to y.partition.cprop. */
2872 add_to_cprop(y, env);
2877 if (n_fallen > 0 && n_fallen != X->n_leader) {
2878 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2879 Y = split(&X, fallen, env);
2881 * We have split out fallen node. The type of the result
2882 * partition is NOT set yet.
2884 Y->type_is_T_or_C = 0;
2888 /* remove the flags from the fallen list */
2889 for (x = fallen; x != NULL; x = x->next)
2892 if (old_type_was_T_or_C) {
2895 /* check if some nodes will make the leader -> follower transition */
2896 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2897 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2898 node_t *eq_node = identity(y);
2900 if (eq_node != y && eq_node->part == y->part) {
2901 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2902 /* move to Follower */
2904 list_del(&y->node_list);
2905 list_add_tail(&y->node_list, &Y->Follower);
2908 segregate_def_use_chain(y->node);
2918 * Get the leader for a given node from its congruence class.
2920 * @param irn the node
2922 static ir_node *get_leader(node_t *node)
2924 partition_t *part = node->part;
2926 if (part->n_leader > 1 || node->is_follower) {
2927 if (node->is_follower) {
2928 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2931 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2933 return get_first_node(part)->node;
2939 * Returns non-zero if a mode_T node has only one reachable output.
2941 static int only_one_reachable_proj(ir_node *n)
2945 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2946 ir_node *proj = get_irn_out(n, i);
2949 /* skip non-control flow Proj's */
2950 if (get_irn_mode(proj) != mode_X)
2953 node = get_irn_node(proj);
2954 if (node->type.tv == tarval_reachable) {
2960 } /* only_one_reachable_proj */
2963 * Return non-zero if the control flow predecessor node pred
2964 * is the only reachable control flow exit of its block.
2966 * @param pred the control flow exit
2967 * @param block the destination block
2969 static int can_exchange(ir_node *pred, ir_node *block)
2971 if (is_Start(pred) || has_Block_entity(block))
2973 else if (is_Jmp(pred))
2975 else if (get_irn_mode(pred) == mode_T) {
2976 /* if the predecessor block has more than one
2977 reachable outputs we cannot remove the block */
2978 return only_one_reachable_proj(pred);
2981 } /* can_exchange */
2984 * Block Post-Walker, apply the analysis results on control flow by
2985 * shortening Phi's and Block inputs.
2987 static void apply_cf(ir_node *block, void *ctx)
2989 environment_t *env = (environment_t*)ctx;
2990 node_t *node = get_irn_node(block);
2992 ir_node **ins, **in_X;
2993 ir_node *phi, *next;
2995 n = get_Block_n_cfgpreds(block);
2997 if (node->type.tv == tarval_unreachable) {
3000 for (i = n - 1; i >= 0; --i) {
3001 ir_node *pred = get_Block_cfgpred(block, i);
3003 if (! is_Bad(pred)) {
3004 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3006 if (pred_bl->flagged == 0) {
3007 pred_bl->flagged = 3;
3009 if (pred_bl->type.tv == tarval_reachable) {
3011 * We will remove an edge from block to its pred.
3012 * This might leave the pred block as an endless loop
3014 if (! is_backedge(block, i))
3015 keep_alive(pred_bl->node);
3021 /* the EndBlock is always reachable even if the analysis
3022 finds out the opposite :-) */
3023 if (block != get_irg_end_block(current_ir_graph)) {
3024 /* mark dead blocks */
3025 set_Block_dead(block);
3026 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
3028 /* the endblock is unreachable */
3029 set_irn_in(block, 0, NULL);
3035 /* only one predecessor combine */
3036 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3038 if (can_exchange(pred, block)) {
3039 ir_node *new_block = get_nodes_block(pred);
3040 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3041 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3042 exchange(block, new_block);
3043 node->node = new_block;
3049 NEW_ARR_A(ir_node *, in_X, n);
3051 for (i = 0; i < n; ++i) {
3052 ir_node *pred = get_Block_cfgpred(block, i);
3053 node_t *node = get_irn_node(pred);
3055 if (node->type.tv == tarval_reachable) {
3058 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3059 if (! is_Bad(pred)) {
3060 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3062 if (pred_bl->flagged == 0) {
3063 pred_bl->flagged = 3;
3065 if (pred_bl->type.tv == tarval_reachable) {
3067 * We will remove an edge from block to its pred.
3068 * This might leave the pred block as an endless loop
3070 if (! is_backedge(block, i))
3071 keep_alive(pred_bl->node);
3081 NEW_ARR_A(ir_node *, ins, n);
3082 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3083 node_t *node = get_irn_node(phi);
3085 next = get_Phi_next(phi);
3086 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3087 /* this Phi is replaced by a constant */
3088 ir_tarval *tv = node->type.tv;
3089 ir_node *c = new_r_Const(current_ir_graph, tv);
3091 set_irn_node(c, node);
3093 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3094 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3099 for (i = 0; i < n; ++i) {
3100 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3102 if (pred->type.tv == tarval_reachable) {
3103 ins[j++] = get_Phi_pred(phi, i);
3107 /* this Phi is replaced by a single predecessor */
3108 ir_node *s = ins[0];
3109 node_t *phi_node = get_irn_node(phi);
3112 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3113 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3118 set_irn_in(phi, j, ins);
3126 /* this Block has only one live predecessor */
3127 ir_node *pred = skip_Proj(in_X[0]);
3129 if (can_exchange(pred, block)) {
3130 ir_node *new_block = get_nodes_block(pred);
3131 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3132 exchange(block, new_block);
3133 node->node = new_block;
3138 set_irn_in(block, k, in_X);
3143 * Exchange a node by its leader.
3144 * Beware: in rare cases the mode might be wrong here, for instance
3145 * AddP(x, NULL) is a follower of x, but with different mode.
3148 static void exchange_leader(ir_node *irn, ir_node *leader)
3150 ir_mode *mode = get_irn_mode(irn);
3151 if (mode != get_irn_mode(leader)) {
3152 /* The conv is a no-op, so we are free to place it
3153 * either in the block of the leader OR in irn's block.
3154 * Probably placing it into leaders block might reduce
3155 * the number of Conv due to CSE. */
3156 ir_node *block = get_nodes_block(leader);
3157 dbg_info *dbg = get_irn_dbg_info(irn);
3158 ir_node *nlead = new_rd_Conv(dbg, block, leader, mode);
3160 if (nlead != leader) {
3161 /* Note: this newly create irn has no node info because
3162 * it is created after the analysis. However, this node
3163 * replaces the node irn and should not be visited again,
3164 * so set its visited count to the count of irn.
3165 * Otherwise we might visited this node more than once if
3166 * irn had more than one user.
3168 set_irn_node(nlead, NULL);
3169 set_irn_visited(nlead, get_irn_visited(irn));
3173 exchange(irn, leader);
3174 } /* exchange_leader */
3177 * Check, if all users of a mode_M node are dead. Use
3178 * the Def-Use edges for this purpose, as they still
3179 * reflect the situation.
3181 static int all_users_are_dead(const ir_node *irn)
3183 int i, n = get_irn_n_outs(irn);
3185 for (i = 1; i <= n; ++i) {
3186 const ir_node *succ = irn->out[i].use;
3187 const node_t *block = get_irn_node(get_nodes_block(succ));
3190 if (block->type.tv == tarval_unreachable) {
3191 /* block is unreachable */
3194 node = get_irn_node(succ);
3195 if (node->type.tv != tarval_top) {
3196 /* found a reachable user */
3200 /* all users are unreachable */
3202 } /* all_user_are_dead */
3205 * Walker: Find reachable mode_M nodes that have only
3206 * unreachable users. These nodes must be kept later.
3208 static void find_kept_memory(ir_node *irn, void *ctx)
3210 environment_t *env = (environment_t*)ctx;
3211 node_t *node, *block;
3213 if (get_irn_mode(irn) != mode_M)
3216 block = get_irn_node(get_nodes_block(irn));
3217 if (block->type.tv == tarval_unreachable)
3220 node = get_irn_node(irn);
3221 if (node->type.tv == tarval_top)
3224 /* ok, we found a live memory node. */
3225 if (all_users_are_dead(irn)) {
3226 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3227 ARR_APP1(ir_node *, env->kept_memory, irn);
3229 } /* find_kept_memory */
3232 * Post-Walker, apply the analysis results;
3234 static void apply_result(ir_node *irn, void *ctx)
3236 environment_t *env = (environment_t*)ctx;
3237 node_t *node = get_irn_node(irn);
3239 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3240 /* blocks already handled, do not touch the End node */
3242 node_t *block = get_irn_node(get_nodes_block(irn));
3244 if (block->type.tv == tarval_unreachable) {
3245 ir_node *bad = get_irg_bad(current_ir_graph);
3247 /* here, bad might already have a node, but this can be safely ignored
3248 as long as bad has at least ONE valid node */
3249 set_irn_node(bad, node);
3251 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3254 } else if (node->type.tv == tarval_top) {
3255 ir_mode *mode = get_irn_mode(irn);
3257 if (mode == mode_M) {
3258 /* never kill a mode_M node */
3260 ir_node *pred = get_Proj_pred(irn);
3261 node_t *pnode = get_irn_node(pred);
3263 if (pnode->type.tv == tarval_top) {
3264 /* skip the predecessor */
3265 ir_node *mem = get_memop_mem(pred);
3267 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3272 /* leave other nodes, especially PhiM */
3273 } else if (mode == mode_T) {
3274 /* Do not kill mode_T nodes, kill their Projs */
3275 } else if (! is_Unknown(irn)) {
3276 /* don't kick away Unknown's, they might be still needed */
3277 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3279 /* control flow should already be handled at apply_cf() */
3280 assert(mode != mode_X);
3282 /* see comment above */
3283 set_irn_node(unk, node);
3285 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3290 else if (get_irn_mode(irn) == mode_X) {
3293 ir_node *cond = get_Proj_pred(irn);
3295 if (is_Cond(cond)) {
3296 if (only_one_reachable_proj(cond)) {
3297 ir_node *jmp = new_r_Jmp(block->node);
3298 set_irn_node(jmp, node);
3300 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3301 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3305 node_t *sel = get_irn_node(get_Cond_selector(cond));
3306 ir_tarval *tv = sel->type.tv;
3308 if (is_tarval(tv) && tarval_is_constant(tv)) {
3309 /* The selector is a constant, but more
3310 * than one output is active: An unoptimized
3318 /* normal data node */
3319 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3320 ir_tarval *tv = node->type.tv;
3323 * Beware: never replace mode_T nodes by constants. Currently we must mark
3324 * mode_T nodes with constants, but do NOT replace them.
3326 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3327 /* can be replaced by a constant */
3328 ir_node *c = new_r_Const(current_ir_graph, tv);
3329 set_irn_node(c, node);
3331 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3332 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3333 exchange_leader(irn, c);
3336 } else if (is_entity(node->type.sym.entity_p)) {
3337 if (! is_SymConst(irn)) {
3338 /* can be replaced by a SymConst */
3339 ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3340 set_irn_node(symc, node);
3343 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3344 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3345 exchange_leader(irn, symc);
3348 } else if (is_Confirm(irn)) {
3349 /* Confirms are always follower, but do not kill them here */
3351 ir_node *leader = get_leader(node);
3353 if (leader != irn) {
3354 int non_strict_phi = 0;
3357 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3358 * as this might create non-strict programs.
3360 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3363 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3364 ir_node *pred = get_Phi_pred(irn, i);
3366 if (is_Unknown(pred)) {
3372 if (! non_strict_phi) {
3373 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3374 if (node->is_follower)
3375 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3377 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3378 exchange_leader(irn, leader);
3385 } /* apply_result */
3388 * Fix the keep-alives by deleting unreachable ones.
3390 static void apply_end(ir_node *end, environment_t *env)
3392 int i, j, n = get_End_n_keepalives(end);
3393 ir_node **in = NULL;
3396 NEW_ARR_A(ir_node *, in, n);
3398 /* fix the keep alive */
3399 for (i = j = 0; i < n; i++) {
3400 ir_node *ka = get_End_keepalive(end, i);
3401 node_t *node = get_irn_node(ka);
3404 node = get_irn_node(get_nodes_block(ka));
3406 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3410 set_End_keepalives(end, j, in);
3415 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3418 * sets the generic functions to compute.
3420 static void set_compute_functions(void)
3424 /* set the default compute function */
3425 for (i = 0, n = get_irp_n_opcodes(); i < n; ++i) {
3426 ir_op *op = get_irp_opcode(i);
3427 op->ops.generic = (op_func)default_compute;
3430 /* set specific functions */
3446 } /* set_compute_functions */
3451 static void add_memory_keeps(ir_node **kept_memory, size_t len)
3453 ir_node *end = get_irg_end(current_ir_graph);
3458 ir_nodeset_init(&set);
3460 /* check, if those nodes are already kept */
3461 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3462 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3464 for (idx = 0; idx < len; ++idx) {
3465 ir_node *ka = kept_memory[idx];
3467 if (! ir_nodeset_contains(&set, ka)) {
3468 add_End_keepalive(end, ka);
3471 ir_nodeset_destroy(&set);
3472 } /* add_memory_keeps */
3474 void combo(ir_graph *irg)
3477 ir_node *initial_bl;
3479 ir_graph *rem = current_ir_graph;
3482 current_ir_graph = irg;
3484 /* register a debug mask */
3485 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3487 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3489 obstack_init(&env.obst);
3490 env.worklist = NULL;
3494 #ifdef DEBUG_libfirm
3495 env.dbg_list = NULL;
3497 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3498 env.kept_memory = NEW_ARR_F(ir_node *, 0);
3499 env.end_idx = get_opt_global_cse() ? 0 : -1;
3500 env.lambda_input = 0;
3503 /* options driving the optimization */
3504 env.commutative = 1;
3505 env.opt_unknown = 1;
3507 assure_irg_outs(irg);
3508 assure_cf_loop(irg);
3510 /* we have our own value_of function */
3511 set_value_of_func(get_node_tarval);
3513 set_compute_functions();
3514 DEBUG_ONLY(part_nr = 0);
3516 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3518 if (env.opt_unknown)
3519 tarval_UNKNOWN = tarval_top;
3521 tarval_UNKNOWN = tarval_bad;
3523 /* create the initial partition and place it on the work list */
3524 env.initial = new_partition(&env);
3525 add_to_worklist(env.initial, &env);
3526 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3528 /* set the hook: from now, every node has a partition and a type */
3529 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook));
3531 /* all nodes on the initial partition have type Top */
3532 env.initial->type_is_T_or_C = 1;
3534 /* Place the START Node's partition on cprop.
3535 Place the START Node on its local worklist. */
3536 initial_bl = get_irg_start_block(irg);
3537 start = get_irn_node(initial_bl);
3538 add_to_cprop(start, &env);
3542 if (env.worklist != NULL)
3544 } while (env.cprop != NULL || env.worklist != NULL);
3546 dump_all_partitions(&env);
3547 check_all_partitions(&env);
3550 dump_ir_block_graph(irg, "-partition");
3553 /* apply the result */
3555 /* check, which nodes must be kept */
3556 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3558 /* kill unreachable control flow */
3559 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3560 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3561 * and fixes assertion because dead cf to dead blocks is NOT removed by
3563 apply_end(get_irg_end(irg), &env);
3564 irg_walk_graph(irg, NULL, apply_result, &env);
3566 len = ARR_LEN(env.kept_memory);
3568 add_memory_keeps(env.kept_memory, len);
3571 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3575 /* control flow might changed */
3576 set_irg_outs_inconsistent(irg);
3577 set_irg_extblk_inconsistent(irg);
3578 set_irg_doms_inconsistent(irg);
3579 set_irg_loopinfo_inconsistent(irg);
3580 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
3583 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3585 /* remove the partition hook */
3586 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
3588 DEL_ARR_F(env.kept_memory);
3589 del_set(env.opcode2id_map);
3590 obstack_free(&env.obst, NULL);
3592 /* restore value_of() default behavior */
3593 set_value_of_func(NULL);
3594 current_ir_graph = rem;
3597 /* Creates an ir_graph pass for combo. */
3598 ir_graph_pass_t *combo_pass(const char *name)
3600 return def_graph_pass(name ? name : "combo", combo);