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 * - support for global congruences is implemented but not tested yet
33 * Note further that we use the terminology from Click's work here, which is different
34 * in some cases from Firm terminology. Especially, Click's type is a
35 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
41 #include "iroptimize.h"
49 #include "irgraph_t.h"
56 #include "iropt_dbg.h"
66 /* define this to check that all type translations are monotone */
67 #undef VERIFY_MONOTONE
69 /* define this to check the consistency of partitions */
70 #define CHECK_PARTITIONS
72 typedef struct node_t node_t;
73 typedef struct partition_t partition_t;
74 typedef struct opcode_key_t opcode_key_t;
75 typedef struct listmap_entry_t listmap_entry_t;
77 /** The type of the compute function. */
78 typedef void (*compute_func)(node_t *node);
84 ir_opcode code; /**< The Firm opcode. */
85 ir_mode *mode; /**< The mode of all nodes in the partition. */
86 int arity; /**< The arity of this opcode (needed for Phi etc. */
88 long proj; /**< For Proj nodes, its proj number */
89 ir_entity *ent; /**< For Sel Nodes, its entity */
94 * An entry in the list_map.
96 struct listmap_entry_t {
97 void *id; /**< The id. */
98 node_t *list; /**< The associated list for this id. */
99 listmap_entry_t *next; /**< Link to the next entry in the map. */
102 /** We must map id's to lists. */
103 typedef struct listmap_t {
104 set *map; /**< Map id's to listmap_entry_t's */
105 listmap_entry_t *values; /**< List of all values in the map. */
109 * A lattice element. Because we handle constants and symbolic constants different, we
110 * have to use this union.
121 ir_node *node; /**< The IR-node itself. */
122 list_head node_list; /**< Double-linked list of leader/follower entries. */
123 list_head cprop_list; /**< Double-linked partition.cprop list. */
124 partition_t *part; /**< points to the partition this node belongs to */
125 node_t *next; /**< Next node on local list (partition.touched, fallen). */
126 node_t *race_next; /**< Next node on race list. */
127 lattice_elem_t type; /**< The associated lattice element "type". */
128 int max_user_input; /**< Maximum input number of Def-Use edges. */
129 int next_edge; /**< Index of the next Def-Use edge to use. */
130 int n_followers; /**< Number of Follower in the outs set. */
131 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
132 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
133 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
134 unsigned is_follower:1; /**< Set, if this node is a follower. */
135 unsigned by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
136 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
140 * A partition containing congruent nodes.
143 list_head Leader; /**< The head of partition Leader node list. */
144 list_head Follower; /**< The head of partition Follower node list. */
145 list_head cprop; /**< The head of partition.cprop list. */
146 partition_t *wl_next; /**< Next entry in the work list if any. */
147 partition_t *touched_next; /**< Points to the next partition in the touched set. */
148 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
149 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
150 node_t *touched; /**< The partition.touched set of this partition. */
151 unsigned n_leader; /**< Number of entries in this partition.Leader. */
152 unsigned n_touched; /**< Number of entries in the partition.touched. */
153 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
154 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
155 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
156 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
157 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
159 partition_t *dbg_next; /**< Link all partitions for debugging */
160 unsigned nr; /**< A unique number for (what-)mapping, >0. */
164 typedef struct environment_t {
165 struct obstack obst; /**< obstack to allocate data structures. */
166 partition_t *worklist; /**< The work list. */
167 partition_t *cprop; /**< The constant propagation list. */
168 partition_t *touched; /**< the touched set. */
169 partition_t *initial; /**< The initial partition. */
170 set *opcode2id_map; /**< The opcodeMode->id map. */
171 pmap *type2id_map; /**< The type->id map. */
172 int end_idx; /**< -1 for local and 0 for global congruences. */
173 int lambda_input; /**< Captured argument for lambda_partition(). */
174 char nonstd_cond; /**< Set, if a Condb note has a non-Cmp predecessor. */
175 char modified; /**< Set, if the graph was modified. */
176 char commutative; /**< Set, if commutation nodes should be handled specially. */
178 partition_t *dbg_list; /**< List of all partitions. */
182 /** Type of the what function. */
183 typedef void *(*what_func)(const node_t *node, environment_t *env);
185 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
186 #define set_irn_node(follower, node) set_irn_link(follower, node)
188 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
189 #undef tarval_unreachable
190 #define tarval_unreachable tarval_top
193 /** The debug module handle. */
194 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
196 /** The what reason. */
197 DEBUG_ONLY(static const char *what_reason;)
199 /** Next partition number. */
200 DEBUG_ONLY(static unsigned part_nr = 0);
202 /** The tarval returned by Unknown nodes. */
203 static tarval *tarval_UNKNOWN;
206 static node_t *identity(node_t *node);
208 #ifdef CHECK_PARTITIONS
212 static void check_partition(const partition_t *T) {
216 list_for_each_entry(node_t, node, &T->Leader, node_list) {
217 assert(node->is_follower == 0);
218 assert(node->flagged == 0);
219 assert(node->part == T);
222 assert(n == T->n_leader);
224 list_for_each_entry(node_t, node, &T->Follower, node_list) {
225 assert(node->is_follower == 1);
226 assert(node->flagged == 0);
227 assert(node->part == T);
229 } /* check_partition */
232 * check that all leader nodes in the partition have the same opcode.
234 static void check_opcode(const partition_t *Z) {
239 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
240 ir_node *irn = node->node;
243 key.code = get_irn_opcode(irn);
244 key.mode = get_irn_mode(irn);
245 key.arity = get_irn_arity(irn);
249 switch (get_irn_opcode(irn)) {
251 key.u.proj = get_Proj_proj(irn);
254 key.u.ent = get_Sel_entity(irn);
261 assert(key.code == get_irn_opcode(irn));
262 assert(key.mode == get_irn_mode(irn));
263 assert(key.arity == get_irn_arity(irn));
265 switch (get_irn_opcode(irn)) {
267 assert(key.u.proj == get_Proj_proj(irn));
270 assert(key.u.ent == get_Sel_entity(irn));
279 static void check_all_partitions(environment_t *env) {
284 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
286 if (! P->type_is_T_or_C)
288 list_for_each_entry(node_t, node, &P->Follower, node_list) {
289 node_t *leader = identity(node);
291 assert(leader != node && leader->part == node->part);
300 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
303 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
304 for (e = list; e != NULL; e = NEXT(e)) {
305 assert(e->part == Z);
308 } /* ido_check_list */
311 * Check a local list.
313 static void check_list(const node_t *list, const partition_t *Z) {
314 do_check_list(list, offsetof(node_t, next), Z);
318 #define check_partition(T)
319 #define check_list(list, Z)
320 #define check_all_partitions(env)
321 #endif /* CHECK_PARTITIONS */
324 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
327 * Dump partition to output.
329 static void dump_partition(const char *msg, const partition_t *part) {
332 lattice_elem_t type = get_partition_type(part);
334 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
335 msg, part->nr, part->type_is_T_or_C ? "*" : "",
336 part->n_leader, type));
337 list_for_each_entry(node_t, node, &part->Leader, node_list) {
338 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
341 if (! list_empty(&part->Follower)) {
342 DB((dbg, LEVEL_2, "\n---\n "));
344 list_for_each_entry(node_t, node, &part->Follower, node_list) {
345 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
349 DB((dbg, LEVEL_2, "\n}\n"));
350 } /* dump_partition */
355 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
359 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
361 DB((dbg, LEVEL_3, "%s = {\n ", msg));
362 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
363 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
366 DB((dbg, LEVEL_3, "\n}\n"));
374 static void dump_race_list(const char *msg, const node_t *list) {
375 do_dump_list(msg, list, offsetof(node_t, race_next));
376 } /* dump_race_list */
379 * Dumps a local list.
381 static void dump_list(const char *msg, const node_t *list) {
382 do_dump_list(msg, list, offsetof(node_t, next));
386 * Dump all partitions.
388 static void dump_all_partitions(const environment_t *env) {
389 const partition_t *P;
391 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
392 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
393 dump_partition("", P);
394 } /* dump_all_partitions */
399 static void dump_split_list(const partition_t *list) {
400 const partition_t *p;
402 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
403 for (p = list; p != NULL; p = p->split_next)
404 DB((dbg, LEVEL_2, "part%u, ", p->nr));
405 DB((dbg, LEVEL_2, "\n}\n"));
406 } /* dump_split_list */
409 #define dump_partition(msg, part)
410 #define dump_race_list(msg, list)
411 #define dump_list(msg, list)
412 #define dump_all_partitions(env)
413 #define dump_split_list(list)
416 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
418 * Verify that a type transition is monotone
420 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
421 if (old_type.tv == new_type.tv) {
425 if (old_type.tv == tarval_top) {
426 /* from Top down-to is always allowed */
429 if (old_type.tv == tarval_reachable) {
430 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
432 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
436 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
439 #define verify_type(old_type, new_type)
443 * Compare two pointer values of a listmap.
445 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
446 const listmap_entry_t *e1 = elt;
447 const listmap_entry_t *e2 = key;
450 return e1->id != e2->id;
451 } /* listmap_cmp_ptr */
454 * Initializes a listmap.
456 * @param map the listmap
458 static void listmap_init(listmap_t *map) {
459 map->map = new_set(listmap_cmp_ptr, 16);
464 * Terminates a listmap.
466 * @param map the listmap
468 static void listmap_term(listmap_t *map) {
473 * Return the associated listmap entry for a given id.
475 * @param map the listmap
476 * @param id the id to search for
478 * @return the associated listmap entry for the given id
480 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
481 listmap_entry_t key, *entry;
486 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
488 if (entry->list == NULL) {
489 /* a new entry, put into the list */
490 entry->next = map->values;
497 * Calculate the hash value for an opcode map entry.
499 * @param entry an opcode map entry
501 * @return a hash value for the given opcode map entry
503 static unsigned opcode_hash(const opcode_key_t *entry) {
504 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent) + entry->arity;
508 * Compare two entries in the opcode map.
510 static int cmp_opcode(const void *elt, const void *key, size_t size) {
511 const opcode_key_t *o1 = elt;
512 const opcode_key_t *o2 = key;
515 return o1->code != o2->code || o1->mode != o2->mode ||
516 o1->arity != o2->arity ||
517 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
521 * Compare two Def-Use edges for input position.
523 static int cmp_def_use_edge(const void *a, const void *b) {
524 const ir_def_use_edge *ea = a;
525 const ir_def_use_edge *eb = b;
527 /* no overrun, because range is [-1, MAXINT] */
528 return ea->pos - eb->pos;
529 } /* cmp_def_use_edge */
532 * We need the Def-Use edges sorted.
534 static void sort_irn_outs(node_t *node) {
535 ir_node *irn = node->node;
536 int n_outs = get_irn_n_outs(irn);
539 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
541 node->max_user_input = irn->out[n_outs].pos;
542 } /* sort_irn_outs */
545 * Return the type of a node.
547 * @param irn an IR-node
549 * @return the associated type of this node
551 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
552 return get_irn_node(irn)->type;
553 } /* get_node_type */
556 * Return the tarval of a node.
558 * @param irn an IR-node
560 * @return the associated type of this node
562 static INLINE tarval *get_node_tarval(const ir_node *irn) {
563 lattice_elem_t type = get_node_type(irn);
565 if (is_tarval(type.tv))
567 return tarval_bottom;
568 } /* get_node_type */
571 * Add a partition to the worklist.
573 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
574 assert(X->on_worklist == 0);
575 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
576 X->wl_next = env->worklist;
579 } /* add_to_worklist */
582 * Create a new empty partition.
584 * @param env the environment
586 * @return a newly allocated partition
588 static INLINE partition_t *new_partition(environment_t *env) {
589 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
591 INIT_LIST_HEAD(&part->Leader);
592 INIT_LIST_HEAD(&part->Follower);
593 INIT_LIST_HEAD(&part->cprop);
594 part->wl_next = NULL;
595 part->touched_next = NULL;
596 part->cprop_next = NULL;
597 part->split_next = NULL;
598 part->touched = NULL;
601 part->max_user_inputs = 0;
602 part->on_worklist = 0;
603 part->on_touched = 0;
605 part->type_is_T_or_C = 0;
607 part->dbg_next = env->dbg_list;
608 env->dbg_list = part;
609 part->nr = part_nr++;
613 } /* new_partition */
616 * Get the first node from a partition.
618 static INLINE node_t *get_first_node(const partition_t *X) {
619 return list_entry(X->Leader.next, node_t, node_list);
620 } /* get_first_node */
623 * Return the type of a partition (assuming partition is non-empty and
624 * all elements have the same type).
626 * @param X a partition
628 * @return the type of the first element of the partition
630 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
631 const node_t *first = get_first_node(X);
633 } /* get_partition_type */
636 * Creates a partition node for the given IR-node and place it
637 * into the given partition.
639 * @param irn an IR-node
640 * @param part a partition to place the node in
641 * @param env the environment
643 * @return the created node
645 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
646 /* create a partition node and place it in the partition */
647 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
649 INIT_LIST_HEAD(&node->node_list);
650 INIT_LIST_HEAD(&node->cprop_list);
654 node->race_next = NULL;
655 node->type.tv = tarval_top;
656 node->max_user_input = 0;
658 node->n_followers = 0;
659 node->on_touched = 0;
662 node->is_follower = 0;
663 node->by_all_const = 0;
665 set_irn_node(irn, node);
667 list_add_tail(&node->node_list, &part->Leader);
671 } /* create_partition_node */
674 * Pre-Walker, init all Block-Phi lists.
676 static void init_block_phis(ir_node *irn, void *env) {
680 set_Block_phis(irn, NULL);
682 } /* init_block_phis */
685 * Post-Walker, initialize all Nodes' type to U or top and place
686 * all nodes into the TOP partition.
688 static void create_initial_partitions(ir_node *irn, void *ctx) {
689 environment_t *env = ctx;
690 partition_t *part = env->initial;
693 node = create_partition_node(irn, part, env);
695 if (node->max_user_input > part->max_user_inputs)
696 part->max_user_inputs = node->max_user_input;
699 add_Block_phi(get_nodes_block(irn), irn);
700 } else if (is_Cond(irn)) {
701 /* check if all Cond's have a Cmp predecessor. */
702 if (get_irn_mode(irn) == mode_b && !is_Cmp(skip_Proj(get_Cond_selector(irn))))
703 env->nonstd_cond = 1;
705 } /* create_initial_partitions */
708 * Add a node to the entry.partition.touched set and
709 * node->partition to the touched set if not already there.
712 * @param env the environment
714 static INLINE void add_to_touched(node_t *y, environment_t *env) {
715 if (y->on_touched == 0) {
716 partition_t *part = y->part;
718 y->next = part->touched;
723 if (part->on_touched == 0) {
724 part->touched_next = env->touched;
726 part->on_touched = 1;
729 check_list(part->touched, part);
731 } /* add_to_touched */
734 * Place a node on the cprop list.
737 * @param env the environment
739 static void add_to_cprop(node_t *y, environment_t *env) {
740 /* Add y to y.partition.cprop. */
741 if (y->on_cprop == 0) {
742 partition_t *Y = y->part;
744 list_add_tail(&y->cprop_list, &Y->cprop);
747 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
749 /* place its partition on the cprop list */
750 if (Y->on_cprop == 0) {
751 Y->cprop_next = env->cprop;
756 if (get_irn_mode(y->node) == mode_T) {
757 /* mode_T nodes always produce tarval_bottom, so we must explicitly
758 add it's Proj's to get constant evaluation to work */
761 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
762 node_t *proj = get_irn_node(get_irn_out(y->node, i));
764 add_to_cprop(proj, env);
766 } else if (is_Block(y->node)) {
767 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
768 * if someone placed the block. The Block is only placed if the reachability
769 * changes, and this must be re-evaluated in compute_Phi(). */
771 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
772 node_t *p = get_irn_node(phi);
773 add_to_cprop(p, env);
779 * Update the worklist: If Z is on worklist then add Z' to worklist.
780 * Else add the smaller of Z and Z' to worklist.
782 * @param Z the Z partition
783 * @param Z_prime the Z' partition, a previous part of Z
784 * @param env the environment
786 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
787 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
788 add_to_worklist(Z_prime, env);
790 add_to_worklist(Z, env);
792 } /* update_worklist */
795 * Make all inputs to x no longer be F.def_use edges.
799 static void move_edges_to_leader(node_t *x) {
800 ir_node *irn = x->node;
803 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
804 node_t *pred = get_irn_node(get_irn_n(irn, i));
809 n = get_irn_n_outs(p);
810 for (j = 1; j <= pred->n_followers; ++j) {
811 if (p->out[j].pos == i && p->out[j].use == irn) {
812 /* found a follower edge to x, move it to the Leader */
813 ir_def_use_edge edge = p->out[j];
815 /* remove this edge from the Follower set */
816 p->out[j] = p->out[pred->n_followers];
819 /* sort it into the leader set */
820 for (k = pred->n_followers + 2; k <= n; ++k) {
821 if (p->out[k].pos >= edge.pos)
823 p->out[k - 1] = p->out[k];
825 /* place the new edge here */
826 p->out[k - 1] = edge;
828 /* edge found and moved */
833 } /* move_edges_to_leader */
836 * Split a partition that has NO followers by a local list.
838 * @param Z partition to split
839 * @param g a (non-empty) node list
840 * @param env the environment
842 * @return a new partition containing the nodes of g
844 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
845 partition_t *Z_prime;
850 dump_partition("Splitting ", Z);
851 dump_list("by list ", g);
855 /* Remove g from Z. */
856 for (node = g; node != NULL; node = node->next) {
857 assert(node->part == Z);
858 list_del(&node->node_list);
861 assert(n < Z->n_leader);
864 /* Move g to a new partition, Z'. */
865 Z_prime = new_partition(env);
867 for (node = g; node != NULL; node = node->next) {
868 list_add_tail(&node->node_list, &Z_prime->Leader);
869 node->part = Z_prime;
870 if (node->max_user_input > max_input)
871 max_input = node->max_user_input;
873 Z_prime->max_user_inputs = max_input;
874 Z_prime->n_leader = n;
877 check_partition(Z_prime);
879 /* for now, copy the type info tag, it will be adjusted in split_by(). */
880 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
882 update_worklist(Z, Z_prime, env);
884 dump_partition("Now ", Z);
885 dump_partition("Created new ", Z_prime);
887 } /* split_no_followers */
890 * Make the Follower -> Leader transition for a node.
894 static void follower_to_leader(node_t *n) {
895 assert(n->is_follower == 1);
897 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
899 move_edges_to_leader(n);
900 list_del(&n->node_list);
901 list_add_tail(&n->node_list, &n->part->Leader);
903 } /* follower_to_leader */
906 * The environment for one race step.
908 typedef struct step_env {
909 node_t *initial; /**< The initial node list. */
910 node_t *unwalked; /**< The unwalked node list. */
911 node_t *walked; /**< The walked node list. */
912 int index; /**< Next index of Follower use_def edge. */
913 unsigned side; /**< side number. */
917 * Return non-zero, if a input is a real follower
919 * @param irn the node to check
920 * @param input number of the input
922 static int is_real_follower(const ir_node *irn, int input) {
925 switch (get_irn_opcode(irn)) {
928 /* ignore the Confirm bound input */
934 /* ignore the Mux sel input */
939 /* dead inputs are not follower edges */
940 ir_node *block = get_nodes_block(irn);
941 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
943 if (pred->type.tv == tarval_unreachable)
953 /* only a Sub x,0 / Shift x,0 might be a follower */
960 pred = get_irn_node(get_irn_n(irn, input));
961 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
965 pred = get_irn_node(get_irn_n(irn, input));
966 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
970 pred = get_irn_node(get_irn_n(irn, input));
971 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
976 /* all inputs are followers */
979 assert(!"opcode not implemented yet");
983 } /* is_real_follower */
986 * Do one step in the race.
988 static int step(step_env *env) {
991 if (env->initial != NULL) {
992 /* Move node from initial to unwalked */
994 env->initial = n->race_next;
996 n->race_next = env->unwalked;
1002 while (env->unwalked != NULL) {
1003 /* let n be the first node in unwalked */
1005 while (env->index < n->n_followers) {
1006 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1008 /* let m be n.F.def_use[index] */
1009 node_t *m = get_irn_node(edge->use);
1011 assert(m->is_follower);
1013 * Some inputs, like the get_Confirm_bound are NOT
1014 * real followers, sort them out.
1016 if (! is_real_follower(m->node, edge->pos)) {
1022 /* only followers from our partition */
1023 if (m->part != n->part)
1026 if ((m->flagged & env->side) == 0) {
1027 m->flagged |= env->side;
1029 if (m->flagged != 3) {
1030 /* visited the first time */
1031 /* add m to unwalked not as first node (we might still need to
1032 check for more follower node */
1033 m->race_next = n->race_next;
1037 /* else already visited by the other side and on the other list */
1040 /* move n to walked */
1041 env->unwalked = n->race_next;
1042 n->race_next = env->walked;
1050 * Clear the flags from a list and check for
1051 * nodes that where touched from both sides.
1053 * @param list the list
1055 static int clear_flags(node_t *list) {
1059 for (n = list; n != NULL; n = n->race_next) {
1060 if (n->flagged == 3) {
1061 /* we reach a follower from both sides, this will split congruent
1062 * inputs and make it a leader. */
1063 follower_to_leader(n);
1072 * Split a partition by a local list using the race.
1074 * @param pX pointer to the partition to split, might be changed!
1075 * @param gg a (non-empty) node list
1076 * @param env the environment
1078 * @return a new partition containing the nodes of gg
1080 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1081 partition_t *X = *pX;
1082 partition_t *X_prime;
1084 step_env env1, env2, *winner;
1085 node_t *g, *h, *node, *t;
1086 int max_input, transitions;
1088 DEBUG_ONLY(static int run = 0;)
1090 DB((dbg, LEVEL_2, "Run %d ", run++));
1091 if (list_empty(&X->Follower)) {
1092 /* if the partition has NO follower, we can use the fast
1093 splitting algorithm. */
1094 return split_no_followers(X, gg, env);
1096 /* else do the race */
1098 dump_partition("Splitting ", X);
1099 dump_list("by list ", gg);
1101 INIT_LIST_HEAD(&tmp);
1103 /* Remove gg from X.Leader and put into g */
1105 for (node = gg; node != NULL; node = node->next) {
1106 assert(node->part == X);
1107 assert(node->is_follower == 0);
1109 list_del(&node->node_list);
1110 list_add_tail(&node->node_list, &tmp);
1111 node->race_next = g;
1116 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1117 node->race_next = h;
1120 /* restore X.Leader */
1121 list_splice(&tmp, &X->Leader);
1124 env1.unwalked = NULL;
1130 env2.unwalked = NULL;
1145 assert(winner->initial == NULL);
1146 assert(winner->unwalked == NULL);
1148 /* clear flags from walked/unwalked */
1149 transitions = clear_flags(env1.unwalked);
1150 transitions |= clear_flags(env1.walked);
1151 transitions |= clear_flags(env2.unwalked);
1152 transitions |= clear_flags(env2.walked);
1154 dump_race_list("winner ", winner->walked);
1156 /* Move walked_{winner} to a new partition, X'. */
1157 X_prime = new_partition(env);
1160 for (node = winner->walked; node != NULL; node = node->race_next) {
1161 list_del(&node->node_list);
1162 node->part = X_prime;
1163 if (node->is_follower) {
1164 list_add_tail(&node->node_list, &X_prime->Follower);
1166 list_add_tail(&node->node_list, &X_prime->Leader);
1169 if (node->max_user_input > max_input)
1170 max_input = node->max_user_input;
1172 X_prime->n_leader = n;
1173 X_prime->max_user_inputs = max_input;
1174 X->n_leader -= X_prime->n_leader;
1176 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1177 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1180 * Even if a follower was not checked by both sides, it might have
1181 * loose its congruence, so we need to check this case for all follower.
1183 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1184 if (identity(node) == node) {
1185 follower_to_leader(node);
1191 check_partition(X_prime);
1193 /* X' is the smaller part */
1194 add_to_worklist(X_prime, env);
1197 * If there where follower to leader transitions, ensure that the nodes
1198 * can be split out if necessary.
1201 /* place partitions on the cprop list */
1202 if (X_prime->on_cprop == 0) {
1203 X_prime->cprop_next = env->cprop;
1204 env->cprop = X_prime;
1205 X_prime->on_cprop = 1;
1209 dump_partition("Now ", X);
1210 dump_partition("Created new ", X_prime);
1212 /* we have to ensure that the partition containing g is returned */
1213 if (winner == &env2) {
1222 * Returns non-zero if the i'th input of a Phi node is live.
1224 * @param phi a Phi-node
1225 * @param i an input number
1227 * @return non-zero if the i'th input of the given Phi node is live
1229 static int is_live_input(ir_node *phi, int i) {
1231 ir_node *block = get_nodes_block(phi);
1232 ir_node *pred = get_Block_cfgpred(block, i);
1233 lattice_elem_t type = get_node_type(pred);
1235 return type.tv != tarval_unreachable;
1237 /* else it's the control input, always live */
1239 } /* is_live_input */
1242 * Return non-zero if a type is a constant.
1244 static int is_constant_type(lattice_elem_t type) {
1245 if (type.tv != tarval_bottom && type.tv != tarval_top)
1248 } /* is_constant_type */
1251 * Check whether a type is neither Top or a constant.
1252 * Note: U is handled like Top here, R is a constant.
1254 * @param type the type to check
1256 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1257 if (is_tarval(type.tv)) {
1258 if (type.tv == tarval_top)
1260 if (tarval_is_constant(type.tv))
1267 } /* type_is_neither_top_nor_const */
1270 * Collect nodes to the touched list.
1272 * @param list the list which contains the nodes that must be evaluated
1273 * @param idx the index of the def_use edge to evaluate
1274 * @param env the environment
1276 static void collect_touched(list_head *list, int idx, environment_t *env) {
1278 int end_idx = env->end_idx;
1280 list_for_each_entry(node_t, x, list, node_list) {
1284 /* leader edges start AFTER follower edges */
1285 x->next_edge = x->n_followers + 1;
1287 num_edges = get_irn_n_outs(x->node);
1289 /* for all edges in x.L.def_use_{idx} */
1290 while (x->next_edge <= num_edges) {
1291 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1294 /* check if we have necessary edges */
1295 if (edge->pos > idx)
1302 /* only non-commutative nodes */
1303 if (env->commutative &&
1304 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1307 /* ignore the "control input" for non-pinned nodes
1308 if we are running in GCSE mode */
1309 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1312 y = get_irn_node(succ);
1313 assert(get_irn_n(succ, idx) == x->node);
1315 /* ignore block edges touching followers */
1316 if (idx == -1 && y->is_follower)
1319 if (is_constant_type(y->type)) {
1320 ir_opcode code = get_irn_opcode(succ);
1321 if (code == iro_Sub || code == iro_Cmp)
1322 add_to_cprop(y, env);
1325 /* Partitions of constants should not be split simply because their Nodes have unequal
1326 functions or incongruent inputs. */
1327 if (type_is_neither_top_nor_const(y->type) &&
1328 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1329 add_to_touched(y, env);
1333 } /* collect_touched */
1336 * Collect commutative nodes to the touched list.
1338 * @param list the list which contains the nodes that must be evaluated
1339 * @param env the environment
1341 static void collect_commutative_touched(list_head *list, environment_t *env) {
1344 list_for_each_entry(node_t, x, list, node_list) {
1347 num_edges = get_irn_n_outs(x->node);
1349 x->next_edge = x->n_followers + 1;
1351 /* for all edges in x.L.def_use_{idx} */
1352 while (x->next_edge <= num_edges) {
1353 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1356 /* check if we have necessary edges */
1366 /* only commutative nodes */
1367 if (!is_op_commutative(get_irn_op(succ)))
1370 y = get_irn_node(succ);
1371 if (is_constant_type(y->type)) {
1372 ir_opcode code = get_irn_opcode(succ);
1373 if (code == iro_Eor)
1374 add_to_cprop(y, env);
1377 /* Partitions of constants should not be split simply because their Nodes have unequal
1378 functions or incongruent inputs. */
1379 if (type_is_neither_top_nor_const(y->type)) {
1380 add_to_touched(y, env);
1384 } /* collect_commutative_touched */
1387 * Split the partitions if caused by the first entry on the worklist.
1389 * @param env the environment
1391 static void cause_splits(environment_t *env) {
1392 partition_t *X, *Z, *N;
1395 /* remove the first partition from the worklist */
1397 env->worklist = X->wl_next;
1400 dump_partition("Cause_split: ", X);
1402 if (env->commutative) {
1403 /* handle commutative nodes first */
1405 /* empty the touched set: already done, just clear the list */
1406 env->touched = NULL;
1408 collect_commutative_touched(&X->Leader, env);
1409 collect_commutative_touched(&X->Follower, env);
1411 for (Z = env->touched; Z != NULL; Z = N) {
1413 node_t *touched = Z->touched;
1414 unsigned n_touched = Z->n_touched;
1416 assert(Z->touched != NULL);
1418 /* beware, split might change Z */
1419 N = Z->touched_next;
1421 /* remove it from the touched set */
1424 /* Empty local Z.touched. */
1425 for (e = touched; e != NULL; e = e->next) {
1426 assert(e->is_follower == 0);
1432 if (0 < n_touched && n_touched < Z->n_leader) {
1433 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1434 split(&Z, touched, env);
1436 assert(n_touched <= Z->n_leader);
1440 /* combine temporary leader and follower list */
1441 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1442 /* empty the touched set: already done, just clear the list */
1443 env->touched = NULL;
1445 collect_touched(&X->Leader, idx, env);
1446 collect_touched(&X->Follower, idx, env);
1448 for (Z = env->touched; Z != NULL; Z = N) {
1450 node_t *touched = Z->touched;
1451 unsigned n_touched = Z->n_touched;
1453 assert(Z->touched != NULL);
1455 /* beware, split might change Z */
1456 N = Z->touched_next;
1458 /* remove it from the touched set */
1461 /* Empty local Z.touched. */
1462 for (e = touched; e != NULL; e = e->next) {
1463 assert(e->is_follower == 0);
1469 if (0 < n_touched && n_touched < Z->n_leader) {
1470 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1471 split(&Z, touched, env);
1473 assert(n_touched <= Z->n_leader);
1476 } /* cause_splits */
1479 * Implements split_by_what(): Split a partition by characteristics given
1480 * by the what function.
1482 * @param X the partition to split
1483 * @param What a function returning an Id for every node of the partition X
1484 * @param P a list to store the result partitions
1485 * @param env the environment
1489 static partition_t *split_by_what(partition_t *X, what_func What,
1490 partition_t **P, environment_t *env) {
1493 listmap_entry_t *iter;
1496 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1498 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1499 void *id = What(x, env);
1500 listmap_entry_t *entry;
1503 /* input not allowed, ignore */
1506 /* Add x to map[What(x)]. */
1507 entry = listmap_find(&map, id);
1508 x->next = entry->list;
1511 /* Let P be a set of Partitions. */
1513 /* for all sets S except one in the range of map do */
1514 for (iter = map.values; iter != NULL; iter = iter->next) {
1515 if (iter->next == NULL) {
1516 /* this is the last entry, ignore */
1521 /* Add SPLIT( X, S ) to P. */
1522 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1523 R = split(&X, S, env);
1533 } /* split_by_what */
1535 /** lambda n.(n.type) */
1536 static void *lambda_type(const node_t *node, environment_t *env) {
1538 return node->type.tv;
1541 /** lambda n.(n.opcode) */
1542 static void *lambda_opcode(const node_t *node, environment_t *env) {
1543 opcode_key_t key, *entry;
1544 ir_node *irn = node->node;
1546 key.code = get_irn_opcode(irn);
1547 key.mode = get_irn_mode(irn);
1548 key.arity = get_irn_arity(irn);
1552 switch (get_irn_opcode(irn)) {
1554 key.u.proj = get_Proj_proj(irn);
1557 key.u.ent = get_Sel_entity(irn);
1563 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1565 } /* lambda_opcode */
1567 /** lambda n.(n[i].partition) */
1568 static void *lambda_partition(const node_t *node, environment_t *env) {
1569 ir_node *skipped = skip_Proj(node->node);
1572 int i = env->lambda_input;
1574 if (i >= get_irn_arity(node->node)) {
1576 * We are outside the allowed range: This can happen even
1577 * if we have split by opcode first: doing so might move Followers
1578 * to Leaders and those will have a different opcode!
1579 * Note that in this case the partition is on the cprop list and will be
1585 /* ignore the "control input" for non-pinned nodes
1586 if we are running in GCSE mode */
1587 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1590 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1591 p = get_irn_node(pred);
1594 } /* lambda_partition */
1596 /** lambda n.(n[i].partition) for commutative nodes */
1597 static void *lambda_commutative_partition(const node_t *node, environment_t *env) {
1598 ir_node *irn = node->node;
1599 ir_node *skipped = skip_Proj(irn);
1600 ir_node *pred, *left, *right;
1602 partition_t *pl, *pr;
1603 int i = env->lambda_input;
1605 if (i >= get_irn_arity(node->node)) {
1607 * We are outside the allowed range: This can happen even
1608 * if we have split by opcode first: doing so might move Followers
1609 * to Leaders and those will have a different opcode!
1610 * Note that in this case the partition is on the cprop list and will be
1616 /* ignore the "control input" for non-pinned nodes
1617 if we are running in GCSE mode */
1618 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1622 pred = get_irn_n(skipped, i);
1623 p = get_irn_node(pred);
1627 if (is_op_commutative(get_irn_op(irn))) {
1628 /* normalize partition order by returning the "smaller" on input 0,
1629 the "bigger" on input 1. */
1630 left = get_binop_left(irn);
1631 pl = get_irn_node(left)->part;
1632 right = get_binop_right(irn);
1633 pr = get_irn_node(right)->part;
1636 return pl < pr ? pl : pr;
1638 return pl > pr ? pl : pr;
1640 /* a not split out Follower */
1641 pred = get_irn_n(irn, i);
1642 p = get_irn_node(pred);
1646 } /* lambda_commutative_partition */
1649 * Returns true if a type is a constant.
1651 static int is_con(const lattice_elem_t type) {
1652 /* be conservative */
1653 if (is_tarval(type.tv))
1654 return tarval_is_constant(type.tv);
1655 return is_entity(type.sym.entity_p);
1659 * Implements split_by().
1661 * @param X the partition to split
1662 * @param env the environment
1664 static void split_by(partition_t *X, environment_t *env) {
1665 partition_t *I, *P = NULL;
1668 dump_partition("split_by", X);
1670 if (X->n_leader == 1) {
1671 /* we have only one leader, no need to split, just check it's type */
1672 node_t *x = get_first_node(X);
1673 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1677 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1678 P = split_by_what(X, lambda_type, &P, env);
1681 /* adjust the type tags, we have split partitions by type */
1682 for (I = P; I != NULL; I = I->split_next) {
1683 node_t *x = get_first_node(I);
1684 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1691 if (Y->n_leader > 1) {
1692 /* we do not want split the TOP or constant partitions */
1693 if (! Y->type_is_T_or_C) {
1694 partition_t *Q = NULL;
1696 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1697 Q = split_by_what(Y, lambda_opcode, &Q, env);
1704 if (Z->n_leader > 1) {
1705 const node_t *first = get_first_node(Z);
1706 int arity = get_irn_arity(first->node);
1708 what_func what = lambda_partition;
1709 DEBUG_ONLY(char buf[64];)
1711 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1712 what = lambda_commutative_partition;
1715 * BEWARE: during splitting by input 2 for instance we might
1716 * create new partitions which are different by input 1, so collect
1717 * them and split further.
1719 Z->split_next = NULL;
1722 for (input = arity - 1; input >= -1; --input) {
1724 partition_t *Z_prime = R;
1727 if (Z_prime->n_leader > 1) {
1728 env->lambda_input = input;
1729 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1730 DEBUG_ONLY(what_reason = buf;)
1731 S = split_by_what(Z_prime, what, &S, env);
1734 Z_prime->split_next = S;
1737 } while (R != NULL);
1742 } while (Q != NULL);
1745 } while (P != NULL);
1749 * (Re-)compute the type for a given node.
1751 * @param node the node
1753 static void default_compute(node_t *node) {
1755 ir_node *irn = node->node;
1756 node_t *block = get_irn_node(get_nodes_block(irn));
1758 if (block->type.tv == tarval_unreachable) {
1759 node->type.tv = tarval_top;
1763 /* if any of the data inputs have type top, the result is type top */
1764 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1765 ir_node *pred = get_irn_n(irn, i);
1766 node_t *p = get_irn_node(pred);
1768 if (p->type.tv == tarval_top) {
1769 node->type.tv = tarval_top;
1774 if (get_irn_mode(node->node) == mode_X)
1775 node->type.tv = tarval_reachable;
1777 node->type.tv = computed_value(irn);
1778 } /* default_compute */
1781 * (Re-)compute the type for a Block node.
1783 * @param node the node
1785 static void compute_Block(node_t *node) {
1787 ir_node *block = node->node;
1789 if (block == get_irg_start_block(current_ir_graph)) {
1790 /* start block is always reachable */
1791 node->type.tv = tarval_reachable;
1795 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1796 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1798 if (pred->type.tv == tarval_reachable) {
1799 /* A block is reachable, if at least of predecessor is reachable. */
1800 node->type.tv = tarval_reachable;
1804 node->type.tv = tarval_top;
1805 } /* compute_Block */
1808 * (Re-)compute the type for a Bad node.
1810 * @param node the node
1812 static void compute_Bad(node_t *node) {
1813 /* Bad nodes ALWAYS compute Top */
1814 node->type.tv = tarval_top;
1818 * (Re-)compute the type for an Unknown node.
1820 * @param node the node
1822 static void compute_Unknown(node_t *node) {
1823 /* While Unknown nodes should compute Top this is dangerous:
1824 * a Top input to a Cond would lead to BOTH control flows unreachable.
1825 * While this is correct in the given semantics, it would destroy the Firm
1828 * It would be safe to compute Top IF it can be assured, that only Cmp
1829 * nodes are inputs to Conds. We check that first.
1830 * This is the way Frontends typically build Firm, but some optimizations
1831 * (cond_eval for instance) might replace them by Phib's...
1833 node->type.tv = tarval_UNKNOWN;
1834 } /* compute_Unknown */
1837 * (Re-)compute the type for a Jmp node.
1839 * @param node the node
1841 static void compute_Jmp(node_t *node) {
1842 node_t *block = get_irn_node(get_nodes_block(node->node));
1844 node->type = block->type;
1848 * (Re-)compute the type for the End node.
1850 * @param node the node
1852 static void compute_End(node_t *node) {
1853 /* the End node is NOT dead of course */
1854 node->type.tv = tarval_reachable;
1858 * (Re-)compute the type for a SymConst node.
1860 * @param node the node
1862 static void compute_SymConst(node_t *node) {
1863 ir_node *irn = node->node;
1864 node_t *block = get_irn_node(get_nodes_block(irn));
1866 if (block->type.tv == tarval_unreachable) {
1867 node->type.tv = tarval_top;
1870 switch (get_SymConst_kind(irn)) {
1871 case symconst_addr_ent:
1872 /* case symconst_addr_name: cannot handle this yet */
1873 node->type.sym = get_SymConst_symbol(irn);
1876 node->type.tv = computed_value(irn);
1878 } /* compute_SymConst */
1881 * (Re-)compute the type for a Phi node.
1883 * @param node the node
1885 static void compute_Phi(node_t *node) {
1887 ir_node *phi = node->node;
1888 lattice_elem_t type;
1890 /* if a Phi is in a unreachable block, its type is TOP */
1891 node_t *block = get_irn_node(get_nodes_block(phi));
1893 if (block->type.tv == tarval_unreachable) {
1894 node->type.tv = tarval_top;
1898 /* Phi implements the Meet operation */
1899 type.tv = tarval_top;
1900 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1901 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1902 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1904 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1905 /* ignore TOP inputs: We must check here for unreachable blocks,
1906 because Firm constants live in the Start Block are NEVER Top.
1907 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1908 comes from a unreachable input. */
1911 if (pred->type.tv == tarval_bottom) {
1912 node->type.tv = tarval_bottom;
1914 } else if (type.tv == tarval_top) {
1915 /* first constant found */
1917 } else if (type.tv != pred->type.tv) {
1918 /* different constants or tarval_bottom */
1919 node->type.tv = tarval_bottom;
1922 /* else nothing, constants are the same */
1928 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1930 * @param node the node
1932 static void compute_Add(node_t *node) {
1933 ir_node *sub = node->node;
1934 node_t *l = get_irn_node(get_Add_left(sub));
1935 node_t *r = get_irn_node(get_Add_right(sub));
1936 lattice_elem_t a = l->type;
1937 lattice_elem_t b = r->type;
1940 if (a.tv == tarval_top || b.tv == tarval_top) {
1941 node->type.tv = tarval_top;
1942 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1943 node->type.tv = tarval_bottom;
1945 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1946 must call tarval_add() first to handle this case! */
1947 if (is_tarval(a.tv)) {
1948 if (is_tarval(b.tv)) {
1949 node->type.tv = tarval_add(a.tv, b.tv);
1952 mode = get_tarval_mode(a.tv);
1953 if (a.tv == get_mode_null(mode)) {
1957 } else if (is_tarval(b.tv)) {
1958 mode = get_tarval_mode(b.tv);
1959 if (b.tv == get_mode_null(mode)) {
1964 node->type.tv = tarval_bottom;
1969 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1971 * @param node the node
1973 static void compute_Sub(node_t *node) {
1974 ir_node *sub = node->node;
1975 node_t *l = get_irn_node(get_Sub_left(sub));
1976 node_t *r = get_irn_node(get_Sub_right(sub));
1977 lattice_elem_t a = l->type;
1978 lattice_elem_t b = r->type;
1981 if (a.tv == tarval_top || b.tv == tarval_top) {
1982 node->type.tv = tarval_top;
1983 } else if (is_con(a) && is_con(b)) {
1984 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1985 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1986 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1988 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1991 node->type.tv = tarval_bottom;
1993 node->by_all_const = 1;
1994 } else if (r->part == l->part &&
1995 (!mode_is_float(get_irn_mode(l->node)))) {
1997 * BEWARE: a - a is NOT always 0 for floating Point values, as
1998 * NaN op NaN = NaN, so we must check this here.
2000 ir_mode *mode = get_irn_mode(sub);
2001 tv = get_mode_null(mode);
2003 /* if the node was ONCE evaluated by all constants, but now
2004 this breaks AND we get from the argument partitions a different
2005 result, switch to bottom.
2006 This happens because initially all nodes are in the same partition ... */
2007 if (node->by_all_const && node->type.tv != tv)
2011 node->type.tv = tarval_bottom;
2016 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2018 * @param node the node
2020 static void compute_Eor(node_t *node) {
2021 ir_node *eor = node->node;
2022 node_t *l = get_irn_node(get_Eor_left(eor));
2023 node_t *r = get_irn_node(get_Eor_right(eor));
2024 lattice_elem_t a = l->type;
2025 lattice_elem_t b = r->type;
2028 if (a.tv == tarval_top || b.tv == tarval_top) {
2029 node->type.tv = tarval_top;
2030 } else if (is_con(a) && is_con(b)) {
2031 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2032 node->type.tv = tarval_eor(a.tv, b.tv);
2033 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2035 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2038 node->type.tv = tarval_bottom;
2040 node->by_all_const = 1;
2041 } else if (r->part == l->part) {
2042 ir_mode *mode = get_irn_mode(eor);
2043 tv = get_mode_null(mode);
2045 /* if the node was ONCE evaluated by all constants, but now
2046 this breaks AND we get from the argument partitions a different
2047 result, switch to bottom.
2048 This happens because initially all nodes are in the same partition ... */
2049 if (node->by_all_const && node->type.tv != tv)
2053 node->type.tv = tarval_bottom;
2058 * (Re-)compute the type for Cmp.
2060 * @param node the node
2062 static void compute_Cmp(node_t *node) {
2063 ir_node *cmp = node->node;
2064 node_t *l = get_irn_node(get_Cmp_left(cmp));
2065 node_t *r = get_irn_node(get_Cmp_right(cmp));
2066 lattice_elem_t a = l->type;
2067 lattice_elem_t b = r->type;
2069 if (a.tv == tarval_top || b.tv == tarval_top) {
2072 * Top is congruent to any other value, we can
2073 * calculate the compare result.
2075 node->type.tv = tarval_b_true;
2077 node->type.tv = tarval_top;
2079 } else if (is_con(a) && is_con(b)) {
2080 /* both nodes are constants, we can probably do something */
2081 node->type.tv = tarval_b_true;
2082 } else if (r->part == l->part) {
2083 /* both nodes congruent, we can probably do something */
2084 node->type.tv = tarval_b_true;
2086 node->type.tv = tarval_bottom;
2088 } /* compute_Proj_Cmp */
2091 * (Re-)compute the type for a Proj(Cmp).
2093 * @param node the node
2094 * @param cond the predecessor Cmp node
2096 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
2097 ir_node *proj = node->node;
2098 node_t *l = get_irn_node(get_Cmp_left(cmp));
2099 node_t *r = get_irn_node(get_Cmp_right(cmp));
2100 lattice_elem_t a = l->type;
2101 lattice_elem_t b = r->type;
2102 pn_Cmp pnc = get_Proj_proj(proj);
2105 if (a.tv == tarval_top || b.tv == tarval_top) {
2108 tv = new_tarval_from_long((pnc & pn_Cmp_Eq) ^ pn_Cmp_Eq, mode_b);
2111 node->type.tv = tarval_top;
2113 } else if (is_con(a) && is_con(b)) {
2114 default_compute(node);
2115 node->by_all_const = 1;
2116 } else if (r->part == l->part &&
2117 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2119 * BEWARE: a == a is NOT always True for floating Point values, as
2120 * NaN != NaN is defined, so we must check this here.
2122 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
2127 /* if the node was ONCE evaluated by all constants, but now
2128 this breaks AND we get from the argument partitions a different
2129 result, switch to bottom.
2130 This happens because initially all nodes are in the same partition ... */
2131 if (node->by_all_const && node->type.tv != tv)
2135 node->type.tv = tarval_bottom;
2137 } /* compute_Proj_Cmp */
2140 * (Re-)compute the type for a Proj(Cond).
2142 * @param node the node
2143 * @param cond the predecessor Cond node
2145 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
2146 ir_node *proj = node->node;
2147 long pnc = get_Proj_proj(proj);
2148 ir_node *sel = get_Cond_selector(cond);
2149 node_t *selector = get_irn_node(sel);
2151 if (get_irn_mode(sel) == mode_b) {
2153 if (pnc == pn_Cond_true) {
2154 if (selector->type.tv == tarval_b_false) {
2155 node->type.tv = tarval_unreachable;
2156 } else if (selector->type.tv == tarval_b_true) {
2157 node->type.tv = tarval_reachable;
2158 } else if (selector->type.tv == tarval_bottom) {
2159 node->type.tv = tarval_reachable;
2161 assert(selector->type.tv == tarval_top);
2162 node->type.tv = tarval_unreachable;
2165 assert(pnc == pn_Cond_false);
2167 if (selector->type.tv == tarval_b_false) {
2168 node->type.tv = tarval_reachable;
2169 } else if (selector->type.tv == tarval_b_true) {
2170 node->type.tv = tarval_unreachable;
2171 } else if (selector->type.tv == tarval_bottom) {
2172 node->type.tv = tarval_reachable;
2174 assert(selector->type.tv == tarval_top);
2175 node->type.tv = tarval_unreachable;
2180 if (selector->type.tv == tarval_bottom) {
2181 node->type.tv = tarval_reachable;
2182 } else if (selector->type.tv == tarval_top) {
2183 node->type.tv = tarval_unreachable;
2185 long value = get_tarval_long(selector->type.tv);
2186 if (pnc == get_Cond_defaultProj(cond)) {
2187 /* default switch, have to check ALL other cases */
2190 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2191 ir_node *succ = get_irn_out(cond, i);
2195 if (value == get_Proj_proj(succ)) {
2196 /* we found a match, will NOT take the default case */
2197 node->type.tv = tarval_unreachable;
2201 /* all cases checked, no match, will take default case */
2202 node->type.tv = tarval_reachable;
2205 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2209 } /* compute_Proj_Cond */
2212 * (Re-)compute the type for a Proj-Node.
2214 * @param node the node
2216 static void compute_Proj(node_t *node) {
2217 ir_node *proj = node->node;
2218 ir_mode *mode = get_irn_mode(proj);
2219 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2220 ir_node *pred = get_Proj_pred(proj);
2222 if (block->type.tv == tarval_unreachable) {
2223 /* a Proj in a unreachable Block stay Top */
2224 node->type.tv = tarval_top;
2227 if (get_irn_node(pred)->type.tv == tarval_top) {
2228 /* if the predecessor is Top, its Proj follow */
2229 node->type.tv = tarval_top;
2233 if (mode == mode_M) {
2234 /* mode M is always bottom */
2235 node->type.tv = tarval_bottom;
2238 if (mode != mode_X) {
2240 compute_Proj_Cmp(node, pred);
2242 default_compute(node);
2245 /* handle mode_X nodes */
2247 switch (get_irn_opcode(pred)) {
2249 /* the Proj_X from the Start is always reachable.
2250 However this is already handled at the top. */
2251 node->type.tv = tarval_reachable;
2254 compute_Proj_Cond(node, pred);
2257 default_compute(node);
2259 } /* compute_Proj */
2262 * (Re-)compute the type for a Confirm.
2264 * @param node the node
2266 static void compute_Confirm(node_t *node) {
2267 ir_node *confirm = node->node;
2268 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2270 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2271 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2273 if (is_con(bound->type)) {
2274 /* is equal to a constant */
2275 node->type = bound->type;
2279 /* a Confirm is a copy OR a Const */
2280 node->type = pred->type;
2281 } /* compute_Confirm */
2284 * (Re-)compute the type for a Max.
2286 * @param node the node
2288 static void compute_Max(node_t *node) {
2289 ir_node *op = node->node;
2290 node_t *l = get_irn_node(get_binop_left(op));
2291 node_t *r = get_irn_node(get_binop_right(op));
2292 lattice_elem_t a = l->type;
2293 lattice_elem_t b = r->type;
2295 if (a.tv == tarval_top || b.tv == tarval_top) {
2296 node->type.tv = tarval_top;
2297 } else if (is_con(a) && is_con(b)) {
2298 /* both nodes are constants, we can probably do something */
2300 /* this case handles SymConsts as well */
2303 ir_mode *mode = get_irn_mode(op);
2304 tarval *tv_min = get_mode_min(mode);
2308 else if (b.tv == tv_min)
2310 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2311 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2312 node->type.tv = a.tv;
2314 node->type.tv = b.tv;
2316 node->type.tv = tarval_bad;
2319 } else if (r->part == l->part) {
2320 /* both nodes congruent, we can probably do something */
2323 node->type.tv = tarval_bottom;
2328 * (Re-)compute the type for a Min.
2330 * @param node the node
2332 static void compute_Min(node_t *node) {
2333 ir_node *op = node->node;
2334 node_t *l = get_irn_node(get_binop_left(op));
2335 node_t *r = get_irn_node(get_binop_right(op));
2336 lattice_elem_t a = l->type;
2337 lattice_elem_t b = r->type;
2339 if (a.tv == tarval_top || b.tv == tarval_top) {
2340 node->type.tv = tarval_top;
2341 } else if (is_con(a) && is_con(b)) {
2342 /* both nodes are constants, we can probably do something */
2344 /* this case handles SymConsts as well */
2347 ir_mode *mode = get_irn_mode(op);
2348 tarval *tv_max = get_mode_max(mode);
2352 else if (b.tv == tv_max)
2354 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
2355 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
2356 node->type.tv = a.tv;
2358 node->type.tv = b.tv;
2360 node->type.tv = tarval_bad;
2363 } else if (r->part == l->part) {
2364 /* both nodes congruent, we can probably do something */
2367 node->type.tv = tarval_bottom;
2372 * (Re-)compute the type for a given node.
2374 * @param node the node
2376 static void compute(node_t *node) {
2379 if (is_no_Block(node->node)) {
2380 node_t *block = get_irn_node(get_nodes_block(node->node));
2382 if (block->type.tv == tarval_unreachable) {
2383 node->type.tv = tarval_top;
2388 func = (compute_func)node->node->op->ops.generic;
2394 * Identity functions: Note that one might thing that identity() is just a
2395 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2396 * here, because it expects that the identity node is one of the inputs, which is NOT
2397 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2398 * So, we have our own implementation, which copies some parts of equivalent_node()
2402 * Calculates the Identity for Phi nodes
2404 static node_t *identity_Phi(node_t *node) {
2405 ir_node *phi = node->node;
2406 ir_node *block = get_nodes_block(phi);
2407 node_t *n_part = NULL;
2410 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2411 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2413 if (pred_X->type.tv == tarval_reachable) {
2414 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2418 else if (n_part->part != pred->part) {
2419 /* incongruent inputs, not a follower */
2424 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2425 * tarval_top, is in the TOP partition and should NOT being split! */
2426 assert(n_part != NULL);
2428 } /* identity_Phi */
2431 * Calculates the Identity for commutative 0 neutral nodes.
2433 static node_t *identity_comm_zero_binop(node_t *node) {
2434 ir_node *op = node->node;
2435 node_t *a = get_irn_node(get_binop_left(op));
2436 node_t *b = get_irn_node(get_binop_right(op));
2437 ir_mode *mode = get_irn_mode(op);
2440 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2441 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2444 /* node: no input should be tarval_top, else the binop would be also
2445 * Top and not being split. */
2446 zero = get_mode_null(mode);
2447 if (a->type.tv == zero)
2449 if (b->type.tv == zero)
2452 } /* identity_comm_zero_binop */
2455 * Calculates the Identity for Shift nodes.
2457 static node_t *identity_shift(node_t *node) {
2458 ir_node *op = node->node;
2459 node_t *b = get_irn_node(get_binop_right(op));
2460 ir_mode *mode = get_irn_mode(b->node);
2463 /* node: no input should be tarval_top, else the binop would be also
2464 * Top and not being split. */
2465 zero = get_mode_null(mode);
2466 if (b->type.tv == zero)
2467 return get_irn_node(get_binop_left(op));
2469 } /* identity_shift */
2472 * Calculates the Identity for Mul nodes.
2474 static node_t *identity_Mul(node_t *node) {
2475 ir_node *op = node->node;
2476 node_t *a = get_irn_node(get_Mul_left(op));
2477 node_t *b = get_irn_node(get_Mul_right(op));
2478 ir_mode *mode = get_irn_mode(op);
2481 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2482 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2485 /* node: no input should be tarval_top, else the binop would be also
2486 * Top and not being split. */
2487 one = get_mode_one(mode);
2488 if (a->type.tv == one)
2490 if (b->type.tv == one)
2493 } /* identity_Mul */
2496 * Calculates the Identity for Sub nodes.
2498 static node_t *identity_Sub(node_t *node) {
2499 ir_node *sub = node->node;
2500 node_t *b = get_irn_node(get_Sub_right(sub));
2501 ir_mode *mode = get_irn_mode(sub);
2503 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2504 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2507 /* node: no input should be tarval_top, else the binop would be also
2508 * Top and not being split. */
2509 if (b->type.tv == get_mode_null(mode))
2510 return get_irn_node(get_Sub_left(sub));
2512 } /* identity_Sub */
2515 * Calculates the Identity for And nodes.
2517 static node_t *identity_And(node_t *node) {
2518 ir_node *and = node->node;
2519 node_t *a = get_irn_node(get_And_left(and));
2520 node_t *b = get_irn_node(get_And_right(and));
2521 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2523 /* node: no input should be tarval_top, else the And would be also
2524 * Top and not being split. */
2525 if (a->type.tv == neutral)
2527 if (b->type.tv == neutral)
2530 } /* identity_And */
2533 * Calculates the Identity for Confirm nodes.
2535 static node_t *identity_Confirm(node_t *node) {
2536 ir_node *confirm = node->node;
2538 /* a Confirm is always a Copy */
2539 return get_irn_node(get_Confirm_value(confirm));
2540 } /* identity_Confirm */
2543 * Calculates the Identity for Mux nodes.
2545 static node_t *identity_Mux(node_t *node) {
2546 ir_node *mux = node->node;
2547 node_t *t = get_irn_node(get_Mux_true(mux));
2548 node_t *f = get_irn_node(get_Mux_false(mux));
2551 if (t->part == f->part)
2554 /* for now, the 1-input identity is not supported */
2556 sel = get_irn_node(get_Mux_sel(mux));
2558 /* Mux sel input is mode_b, so it is always a tarval */
2559 if (sel->type.tv == tarval_b_true)
2561 if (sel->type.tv == tarval_b_false)
2565 } /* identity_Mux */
2568 * Calculates the Identity for Min nodes.
2570 static node_t *identity_Min(node_t *node) {
2571 ir_node *op = node->node;
2572 node_t *a = get_irn_node(get_binop_left(op));
2573 node_t *b = get_irn_node(get_binop_right(op));
2574 ir_mode *mode = get_irn_mode(op);
2577 if (a->part == b->part) {
2578 /* leader of multiple predecessors */
2582 /* works even with NaN */
2583 tv_max = get_mode_max(mode);
2584 if (a->type.tv == tv_max)
2586 if (b->type.tv == tv_max)
2589 } /* identity_Min */
2592 * Calculates the Identity for Max nodes.
2594 static node_t *identity_Max(node_t *node) {
2595 ir_node *op = node->node;
2596 node_t *a = get_irn_node(get_binop_left(op));
2597 node_t *b = get_irn_node(get_binop_right(op));
2598 ir_mode *mode = get_irn_mode(op);
2601 if (a->part == b->part) {
2602 /* leader of multiple predecessors */
2606 /* works even with NaN */
2607 tv_min = get_mode_min(mode);
2608 if (a->type.tv == tv_min)
2610 if (b->type.tv == tv_min)
2613 } /* identity_Max */
2616 * Calculates the Identity for nodes.
2618 static node_t *identity(node_t *node) {
2619 ir_node *irn = node->node;
2621 switch (get_irn_opcode(irn)) {
2623 return identity_Phi(node);
2625 return identity_Mul(node);
2629 return identity_comm_zero_binop(node);
2634 return identity_shift(node);
2636 return identity_And(node);
2638 return identity_Sub(node);
2640 return identity_Confirm(node);
2642 return identity_Mux(node);
2644 return identity_Min(node);
2646 return identity_Max(node);
2653 * Node follower is a (new) follower of leader, segregate Leader
2656 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2657 ir_node *l = leader->node;
2658 int j, i, n = get_irn_n_outs(l);
2660 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2661 /* The leader edges must remain sorted, but follower edges can
2663 for (i = leader->n_followers + 1; i <= n; ++i) {
2664 if (l->out[i].use == follower) {
2665 ir_def_use_edge t = l->out[i];
2667 for (j = i - 1; j >= leader->n_followers + 1; --j)
2668 l->out[j + 1] = l->out[j];
2669 ++leader->n_followers;
2670 l->out[leader->n_followers] = t;
2674 } /* segregate_def_use_chain_1 */
2677 * Node follower is a (new) follower segregate its Leader
2680 * @param follower the follower IR node
2682 static void segregate_def_use_chain(const ir_node *follower) {
2685 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2686 node_t *pred = get_irn_node(get_irn_n(follower, i));
2688 segregate_def_use_chain_1(follower, pred);
2690 } /* segregate_def_use_chain */
2693 * Propagate constant evaluation.
2695 * @param env the environment
2697 static void propagate(environment_t *env) {
2700 lattice_elem_t old_type;
2702 unsigned n_fallen, old_type_was_T_or_C;
2705 while (env->cprop != NULL) {
2706 void *oldopcode = NULL;
2708 /* remove the first partition X from cprop */
2711 env->cprop = X->cprop_next;
2713 old_type_was_T_or_C = X->type_is_T_or_C;
2715 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2718 while (! list_empty(&X->cprop)) {
2719 /* remove the first Node x from X.cprop */
2720 x = list_entry(X->cprop.next, node_t, cprop_list);
2721 //assert(x->part == X);
2722 list_del(&x->cprop_list);
2725 if (x->is_follower && identity(x) == x) {
2726 /* check the opcode first */
2727 if (oldopcode == NULL) {
2728 oldopcode = lambda_opcode(get_first_node(X), env);
2730 if (oldopcode != lambda_opcode(x, env)) {
2731 if (x->on_fallen == 0) {
2732 /* different opcode -> x falls out of this partition */
2737 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2741 /* x will make the follower -> leader transition */
2742 follower_to_leader(x);
2745 /* compute a new type for x */
2747 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2749 if (x->type.tv != old_type.tv) {
2750 verify_type(old_type, x->type);
2751 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2753 if (x->on_fallen == 0) {
2754 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2755 not already on the list. */
2760 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2762 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2763 ir_node *succ = get_irn_out(x->node, i);
2764 node_t *y = get_irn_node(succ);
2766 /* Add y to y.partition.cprop. */
2767 add_to_cprop(y, env);
2772 if (n_fallen > 0 && n_fallen != X->n_leader) {
2773 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2774 Y = split(&X, fallen, env);
2776 * We have split out fallen node. The type of the result
2777 * partition is NOT set yet.
2779 Y->type_is_T_or_C = 0;
2783 /* remove the flags from the fallen list */
2784 for (x = fallen; x != NULL; x = x->next)
2787 if (old_type_was_T_or_C) {
2790 /* check if some nodes will make the leader -> follower transition */
2791 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2792 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2793 node_t *eq_node = identity(y);
2795 if (eq_node != y && eq_node->part == y->part) {
2796 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2797 /* move to Follower */
2799 list_del(&y->node_list);
2800 list_add_tail(&y->node_list, &Y->Follower);
2803 segregate_def_use_chain(y->node);
2813 * Get the leader for a given node from its congruence class.
2815 * @param irn the node
2817 static ir_node *get_leader(node_t *node) {
2818 partition_t *part = node->part;
2820 if (part->n_leader > 1 || node->is_follower) {
2821 if (node->is_follower) {
2822 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2825 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2827 return get_first_node(part)->node;
2833 * Return non-zero if the control flow predecessor node pred
2834 * is the only reachable control flow exit of its block.
2836 * @param pred the control flow exit
2838 static int can_exchange(ir_node *pred) {
2841 else if (is_Jmp(pred))
2843 else if (get_irn_mode(pred) == mode_T) {
2846 /* if the predecessor block has more than one
2847 reachable outputs we cannot remove the block */
2849 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2850 ir_node *proj = get_irn_out(pred, i);
2853 /* skip non-control flow Proj's */
2854 if (get_irn_mode(proj) != mode_X)
2857 node = get_irn_node(proj);
2858 if (node->type.tv == tarval_reachable) {
2866 } /* can_exchange */
2869 * Block Post-Walker, apply the analysis results on control flow by
2870 * shortening Phi's and Block inputs.
2872 static void apply_cf(ir_node *block, void *ctx) {
2873 environment_t *env = ctx;
2874 node_t *node = get_irn_node(block);
2876 ir_node **ins, **in_X;
2877 ir_node *phi, *next;
2879 n = get_Block_n_cfgpreds(block);
2881 if (node->type.tv == tarval_unreachable) {
2884 for (i = n - 1; i >= 0; --i) {
2885 ir_node *pred = get_Block_cfgpred(block, i);
2887 if (! is_Bad(pred)) {
2888 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2890 if (pred_bl->flagged == 0) {
2891 pred_bl->flagged = 3;
2893 if (pred_bl->type.tv == tarval_reachable) {
2895 * We will remove an edge from block to its pred.
2896 * This might leave the pred block as an endless loop
2898 if (! is_backedge(block, i))
2899 keep_alive(pred_bl->node);
2905 /* the EndBlock is always reachable even if the analysis
2906 finds out the opposite :-) */
2907 if (block != get_irg_end_block(current_ir_graph)) {
2908 /* mark dead blocks */
2909 set_Block_dead(block);
2910 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
2912 /* the endblock is unreachable */
2913 set_irn_in(block, 0, NULL);
2919 /* only one predecessor combine */
2920 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2922 if (can_exchange(pred)) {
2923 ir_node *new_block = get_nodes_block(pred);
2924 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
2925 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
2926 exchange(block, new_block);
2927 node->node = new_block;
2933 NEW_ARR_A(ir_node *, in_X, n);
2935 for (i = 0; i < n; ++i) {
2936 ir_node *pred = get_Block_cfgpred(block, i);
2937 node_t *node = get_irn_node(pred);
2939 if (node->type.tv == tarval_reachable) {
2942 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
2943 if (! is_Bad(pred)) {
2944 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
2946 if (pred_bl->flagged == 0) {
2947 pred_bl->flagged = 3;
2949 if (pred_bl->type.tv == tarval_reachable) {
2951 * We will remove an edge from block to its pred.
2952 * This might leave the pred block as an endless loop
2954 if (! is_backedge(block, i))
2955 keep_alive(pred_bl->node);
2964 NEW_ARR_A(ir_node *, ins, n);
2965 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2966 node_t *node = get_irn_node(phi);
2968 next = get_Phi_next(phi);
2969 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2970 /* this Phi is replaced by a constant */
2971 tarval *tv = node->type.tv;
2972 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2974 set_irn_node(c, node);
2976 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2977 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
2982 for (i = 0; i < n; ++i) {
2983 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2985 if (pred->type.tv == tarval_reachable) {
2986 ins[j++] = get_Phi_pred(phi, i);
2990 /* this Phi is replaced by a single predecessor */
2991 ir_node *s = ins[0];
2992 node_t *phi_node = get_irn_node(phi);
2995 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2996 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3001 set_irn_in(phi, j, ins);
3008 /* this Block has only one live predecessor */
3009 ir_node *pred = skip_Proj(in_X[0]);
3011 if (can_exchange(pred)) {
3012 ir_node *new_block = get_nodes_block(pred);
3013 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3014 exchange(block, new_block);
3015 node->node = new_block;
3019 set_irn_in(block, k, in_X);
3025 * Exchange a node by its leader.
3026 * Beware: in rare cases the mode might be wrong here, for instance
3027 * AddP(x, NULL) is a follower of x, but with different mode.
3030 static void exchange_leader(ir_node *irn, ir_node *leader) {
3031 ir_mode *mode = get_irn_mode(irn);
3032 if (mode != get_irn_mode(leader)) {
3033 /* The conv is a no-op, so we are fre to place in
3034 * either in the block of the leader OR in irn's block.
3035 * Propably placing it into leaders block might reduce
3036 * the number of Conv due to CSE. */
3037 ir_node *block = get_nodes_block(leader);
3038 dbg_info *dbg = get_irn_dbg_info(irn);
3040 leader = new_rd_Conv(dbg, current_ir_graph, block, leader, mode);
3042 exchange(irn, leader);
3046 * Post-Walker, apply the analysis results;
3048 static void apply_result(ir_node *irn, void *ctx) {
3049 environment_t *env = ctx;
3050 node_t *node = get_irn_node(irn);
3052 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3053 /* blocks already handled, do not touch the End node */
3055 node_t *block = get_irn_node(get_nodes_block(irn));
3057 if (block->type.tv == tarval_unreachable) {
3058 ir_node *bad = get_irg_bad(current_ir_graph);
3060 /* here, bad might already have a node, but this can be safely ignored
3061 as long as bad has at least ONE valid node */
3062 set_irn_node(bad, node);
3064 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3068 else if (node->type.tv == tarval_unreachable) {
3069 /* don't kick away Unknown */
3070 if (! is_Unknown(irn)) {
3071 ir_node *bad = get_irg_bad(current_ir_graph);
3073 /* see comment above */
3074 set_irn_node(bad, node);
3076 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3081 else if (get_irn_mode(irn) == mode_X) {
3084 ir_node *cond = get_Proj_pred(irn);
3086 if (is_Cond(cond)) {
3087 node_t *sel = get_irn_node(get_Cond_selector(cond));
3089 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
3090 /* Cond selector is a constant and the Proj is reachable, make a Jmp */
3091 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
3092 set_irn_node(jmp, node);
3094 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3095 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3102 /* normal data node */
3103 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3104 tarval *tv = node->type.tv;
3107 * Beware: never replace mode_T nodes by constants. Currently we must mark
3108 * mode_T nodes with constants, but do NOT replace them.
3110 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3111 /* can be replaced by a constant */
3112 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
3113 set_irn_node(c, node);
3115 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3116 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3117 exchange_leader(irn, c);
3120 } else if (is_entity(node->type.sym.entity_p)) {
3121 if (! is_SymConst(irn)) {
3122 /* can be replaced by a SymConst */
3123 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3124 set_irn_node(symc, node);
3127 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3128 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3129 exchange_leader(irn, symc);
3132 } else if (is_Confirm(irn)) {
3133 /* Confirms are always follower, but do not kill them here */
3135 ir_node *leader = get_leader(node);
3137 if (leader != irn) {
3138 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3139 if (node->is_follower)
3140 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3142 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3143 exchange_leader(irn, leader);
3149 } /* apply_result */
3152 * Fix the keep-alives by deleting unreachable ones.
3154 static void apply_end(ir_node *end, environment_t *env) {
3155 int i, j, n = get_End_n_keepalives(end);
3159 NEW_ARR_A(ir_node *, in, n);
3161 /* fix the keep alive */
3162 for (i = j = 0; i < n; i++) {
3163 ir_node *ka = get_End_keepalive(end, i);
3164 node_t *node = get_irn_node(ka);
3167 node = get_irn_node(get_nodes_block(ka));
3169 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3173 set_End_keepalives(end, j, in);
3178 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3181 * sets the generic functions to compute.
3183 static void set_compute_functions(void) {
3186 /* set the default compute function */
3187 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3188 ir_op *op = get_irp_opcode(i);
3189 op->ops.generic = (op_func)default_compute;
3192 /* set specific functions */
3212 } /* set_compute_functions */
3214 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
3215 #ifdef DEBUG_libfirm
3216 ir_node *irn = local != NULL ? local : n;
3217 node_t *node = get_irn_node(irn);
3219 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
3224 void combo(ir_graph *irg) {
3226 ir_node *initial_bl;
3228 ir_graph *rem = current_ir_graph;
3230 current_ir_graph = irg;
3232 /* register a debug mask */
3233 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3235 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3237 obstack_init(&env.obst);
3238 env.worklist = NULL;
3242 #ifdef DEBUG_libfirm
3243 env.dbg_list = NULL;
3245 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3246 env.type2id_map = pmap_create();
3247 env.end_idx = get_opt_global_cse() ? 0 : -1;
3248 env.lambda_input = 0;
3249 env.nonstd_cond = 0;
3250 env.commutative = 1;
3253 assure_irg_outs(irg);
3254 assure_cf_loop(irg);
3256 /* we have our own value_of function */
3257 set_value_of_func(get_node_tarval);
3259 set_compute_functions();
3260 DEBUG_ONLY(part_nr = 0);
3262 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
3264 /* create the initial partition and place it on the work list */
3265 env.initial = new_partition(&env);
3266 add_to_worklist(env.initial, &env);
3267 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
3270 tarval_UNKNOWN = env.nonstd_cond ? tarval_bad : tarval_top;
3272 tarval_UNKNOWN = tarval_bad;
3275 /* all nodes on the initial partition have type Top */
3276 env.initial->type_is_T_or_C = 1;
3278 /* Place the START Node's partition on cprop.
3279 Place the START Node on its local worklist. */
3280 initial_bl = get_irg_start_block(irg);
3281 start = get_irn_node(initial_bl);
3282 add_to_cprop(start, &env);
3286 if (env.worklist != NULL)
3288 } while (env.cprop != NULL || env.worklist != NULL);
3290 dump_all_partitions(&env);
3291 check_all_partitions(&env);
3294 set_dump_node_vcgattr_hook(dump_partition_hook);
3295 dump_ir_block_graph(irg, "-partition");
3296 set_dump_node_vcgattr_hook(NULL);
3298 (void)dump_partition_hook;
3301 /* apply the result */
3302 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3303 irg_walk_graph(irg, NULL, apply_result, &env);
3304 apply_end(get_irg_end(irg), &env);
3307 /* control flow might changed */
3308 set_irg_outs_inconsistent(irg);
3309 set_irg_extblk_inconsistent(irg);
3310 set_irg_doms_inconsistent(irg);
3311 set_irg_loopinfo_inconsistent(irg);
3314 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
3316 pmap_destroy(env.type2id_map);
3317 del_set(env.opcode2id_map);
3318 obstack_free(&env.obst, NULL);
3320 /* restore value_of() default behavior */
3321 set_value_of_func(NULL);
3322 current_ir_graph = rem;