2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Cliff Click's Combined Analysis/Optimization
23 * @author Michael Beck
26 * This is a slightly enhanced version of Cliff Clicks combo algorithm
27 * - support for commutative nodes is added, Add(a,b) and Add(b,a) ARE congruent
28 * - supports all Firm direct (by a data edge) identities except Mux
29 * (Mux can be a 2-input or 1-input identity, only 2-input is implemented yet)
30 * - supports Confirm nodes (handle them like Copies but do NOT remove them)
31 * - let Cmp nodes calculate Top like all othe data nodes: this would let
32 * Mux nodes to calculate Unknown instead of taking the true result
33 * - let Cond(Top) always select FALSE/default: This is tricky. Nodes are only reavaluated
34 * IFF the predecessor changed its type. Because nodes are initialized with Top
35 * this never happens, let all Proj(Cond) be unreachable.
36 * We avoid this condition by the same way we work around Phi: whenever a Block
37 * node is placed on the list, place its Cond nodes (and because they are Tuple
38 * all its Proj-nodes either on the cprop list)
39 * Especially, this changes the meaning of Click's example:
54 * using Click's version while is silent with our.
55 * - support for global congruences is implemented but not tested yet
57 * Note further that we use the terminology from Click's work here, which is different
58 * in some cases from Firm terminology. Especially, Click's type is a
59 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
65 #include "iroptimize.h"
72 #include "irgraph_t.h"
79 #include "iropt_dbg.h"
83 #include "irnodeset.h"
90 /* define this to check that all type translations are monotone */
91 #define VERIFY_MONOTONE
93 /* define this to check the consistency of partitions */
94 #define CHECK_PARTITIONS
96 typedef struct node_t node_t;
97 typedef struct partition_t partition_t;
98 typedef struct opcode_key_t opcode_key_t;
99 typedef struct listmap_entry_t listmap_entry_t;
101 /** The type of the compute function. */
102 typedef void (*compute_func)(node_t *node);
107 struct opcode_key_t {
108 ir_opcode code; /**< The Firm opcode. */
109 ir_mode *mode; /**< The mode of all nodes in the partition. */
110 int arity; /**< The arity of this opcode (needed for Phi etc. */
112 long proj; /**< For Proj nodes, its proj number */
113 ir_entity *ent; /**< For Sel Nodes, its entity */
114 int intVal; /**< For Conv/Div Nodes: strict/remainderless */
115 unsigned uintVal;/**< for Builtin: the kind */
116 ir_node *block; /**< for Block: itself */
117 void *ptr; /**< generic pointer for hash/cmp */
122 * An entry in the list_map.
124 struct listmap_entry_t {
125 void *id; /**< The id. */
126 node_t *list; /**< The associated list for this id. */
127 listmap_entry_t *next; /**< Link to the next entry in the map. */
130 /** We must map id's to lists. */
131 typedef struct listmap_t {
132 set *map; /**< Map id's to listmap_entry_t's */
133 listmap_entry_t *values; /**< List of all values in the map. */
137 * A lattice element. Because we handle constants and symbolic constants different, we
138 * have to use this union.
149 ir_node *node; /**< The IR-node itself. */
150 list_head node_list; /**< Double-linked list of leader/follower entries. */
151 list_head cprop_list; /**< Double-linked partition.cprop list. */
152 partition_t *part; /**< points to the partition this node belongs to */
153 node_t *next; /**< Next node on local list (partition.touched, fallen). */
154 node_t *race_next; /**< Next node on race list. */
155 lattice_elem_t type; /**< The associated lattice element "type". */
156 int max_user_input; /**< Maximum input number of Def-Use edges. */
157 int next_edge; /**< Index of the next Def-Use edge to use. */
158 int n_followers; /**< Number of Follower in the outs set. */
159 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
160 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
161 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
162 unsigned is_follower:1; /**< Set, if this node is a follower. */
163 unsigned flagged:2; /**< 2 Bits, set if this node was visited by race 1 or 2. */
167 * A partition containing congruent nodes.
170 list_head Leader; /**< The head of partition Leader node list. */
171 list_head Follower; /**< The head of partition Follower node list. */
172 list_head cprop; /**< The head of partition.cprop list. */
173 list_head cprop_X; /**< The head of partition.cprop (Cond nodes and its Projs) list. */
174 partition_t *wl_next; /**< Next entry in the work list if any. */
175 partition_t *touched_next; /**< Points to the next partition in the touched set. */
176 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
177 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
178 node_t *touched; /**< The partition.touched set of this partition. */
179 unsigned n_leader; /**< Number of entries in this partition.Leader. */
180 unsigned n_touched; /**< Number of entries in the partition.touched. */
181 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
182 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
183 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
184 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
185 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
187 partition_t *dbg_next; /**< Link all partitions for debugging */
188 unsigned nr; /**< A unique number for (what-)mapping, >0. */
192 typedef struct environment_t {
193 struct obstack obst; /**< obstack to allocate data structures. */
194 partition_t *worklist; /**< The work list. */
195 partition_t *cprop; /**< The constant propagation list. */
196 partition_t *touched; /**< the touched set. */
197 partition_t *initial; /**< The initial partition. */
198 set *opcode2id_map; /**< The opcodeMode->id map. */
199 ir_node **kept_memory; /**< Array of memory nodes that must be kept. */
200 int end_idx; /**< -1 for local and 0 for global congruences. */
201 int lambda_input; /**< Captured argument for lambda_partition(). */
202 unsigned modified:1; /**< Set, if the graph was modified. */
203 unsigned unopt_cf:1; /**< If set, control flow is not optimized due to Unknown. */
204 /* options driving the optimization */
205 unsigned commutative:1; /**< Set, if commutation nodes should be handled specially. */
206 unsigned opt_unknown:1; /**< Set, if non-strict programs should be optimized. */
208 partition_t *dbg_list; /**< List of all partitions. */
212 /** Type of the what function. */
213 typedef void *(*what_func)(const node_t *node, environment_t *env);
215 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
216 #define set_irn_node(irn, node) set_irn_link(irn, node)
218 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
219 #undef tarval_unreachable
220 #define tarval_unreachable tarval_top
223 /** The debug module handle. */
224 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
226 /** The what reason. */
227 DEBUG_ONLY(static const char *what_reason;)
229 /** Next partition number. */
230 DEBUG_ONLY(static unsigned part_nr = 0);
232 /** The tarval returned by Unknown nodes: set to either tarval_bad OR tarval_top. */
233 static tarval *tarval_UNKNOWN;
236 static node_t *identity(node_t *node);
238 #ifdef CHECK_PARTITIONS
242 static void check_partition(const partition_t *T)
247 list_for_each_entry(node_t, node, &T->Leader, node_list) {
248 assert(node->is_follower == 0);
249 assert(node->flagged == 0);
250 assert(node->part == T);
253 assert(n == T->n_leader);
255 list_for_each_entry(node_t, node, &T->Follower, node_list) {
256 assert(node->is_follower == 1);
257 assert(node->flagged == 0);
258 assert(node->part == T);
260 } /* check_partition */
263 * check that all leader nodes in the partition have the same opcode.
265 static void check_opcode(const partition_t *Z)
271 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
272 ir_node *irn = node->node;
275 key.code = get_irn_opcode(irn);
276 key.mode = get_irn_mode(irn);
277 key.arity = get_irn_arity(irn);
281 switch (get_irn_opcode(irn)) {
283 key.u.proj = get_Proj_proj(irn);
286 key.u.ent = get_Sel_entity(irn);
289 key.u.intVal = get_Conv_strict(irn);
292 key.u.intVal = get_Div_no_remainder(irn);
298 key.mode = get_Load_mode(irn);
301 key.u.intVal = get_Builtin_kind(irn);
308 assert((unsigned)key.code == get_irn_opcode(irn));
309 assert(key.mode == get_irn_mode(irn));
310 assert(key.arity == get_irn_arity(irn));
312 switch (get_irn_opcode(irn)) {
314 assert(key.u.proj == get_Proj_proj(irn));
317 assert(key.u.ent == get_Sel_entity(irn));
320 assert(key.u.intVal == get_Conv_strict(irn));
323 assert(key.u.intVal == get_Div_no_remainder(irn));
326 assert(key.u.block == irn);
329 assert(key.mode == get_Load_mode(irn));
332 assert(key.u.intVal == (int) get_Builtin_kind(irn));
341 static void check_all_partitions(environment_t *env)
347 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
349 if (! P->type_is_T_or_C)
351 list_for_each_entry(node_t, node, &P->Follower, node_list) {
352 node_t *leader = identity(node);
354 assert(leader != node && leader->part == node->part);
365 static void do_check_list(const node_t *list, int ofs, const partition_t *Z)
370 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
371 for (e = list; e != NULL; e = NEXT(e)) {
372 assert(e->part == Z);
380 } /* ido_check_list */
383 * Check a local list.
385 static void check_list(const node_t *list, const partition_t *Z)
387 do_check_list(list, offsetof(node_t, next), Z);
391 #define check_partition(T)
392 #define check_list(list, Z)
393 #define check_all_partitions(env)
394 #endif /* CHECK_PARTITIONS */
397 static inline lattice_elem_t get_partition_type(const partition_t *X);
400 * Dump partition to output.
402 static void dump_partition(const char *msg, const partition_t *part)
406 lattice_elem_t type = get_partition_type(part);
408 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
409 msg, part->nr, part->type_is_T_or_C ? "*" : "",
410 part->n_leader, type));
411 list_for_each_entry(node_t, node, &part->Leader, node_list) {
412 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
415 if (! list_empty(&part->Follower)) {
416 DB((dbg, LEVEL_2, "\n---\n "));
418 list_for_each_entry(node_t, node, &part->Follower, node_list) {
419 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
423 DB((dbg, LEVEL_2, "\n}\n"));
424 } /* dump_partition */
429 static void do_dump_list(const char *msg, const node_t *node, int ofs)
434 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
436 DB((dbg, LEVEL_3, "%s = {\n ", msg));
437 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
438 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
441 DB((dbg, LEVEL_3, "\n}\n"));
449 static void dump_race_list(const char *msg, const node_t *list)
451 do_dump_list(msg, list, offsetof(node_t, race_next));
452 } /* dump_race_list */
455 * Dumps a local list.
457 static void dump_list(const char *msg, const node_t *list)
459 do_dump_list(msg, list, offsetof(node_t, next));
463 * Dump all partitions.
465 static void dump_all_partitions(const environment_t *env)
467 const partition_t *P;
469 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
470 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
471 dump_partition("", P);
472 } /* dump_all_partitions */
477 static void dump_split_list(const partition_t *list)
479 const partition_t *p;
481 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
482 for (p = list; p != NULL; p = p->split_next)
483 DB((dbg, LEVEL_2, "part%u, ", p->nr));
484 DB((dbg, LEVEL_2, "\n}\n"));
485 } /* dump_split_list */
488 * Dump partition and type for a node.
490 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local)
492 ir_node *irn = local != NULL ? local : n;
493 node_t *node = get_irn_node(irn);
495 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
497 } /* dump_partition_hook */
500 #define dump_partition(msg, part)
501 #define dump_race_list(msg, list)
502 #define dump_list(msg, list)
503 #define dump_all_partitions(env)
504 #define dump_split_list(list)
507 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
509 * Verify that a type transition is monotone
511 static void verify_type(const lattice_elem_t old_type, node_t *node)
513 if (old_type.tv == node->type.tv) {
517 if (old_type.tv == tarval_top) {
518 /* from Top down-to is always allowed */
521 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
525 panic("combo: wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
529 #define verify_type(old_type, node)
533 * Compare two pointer values of a listmap.
535 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
537 const listmap_entry_t *e1 = elt;
538 const listmap_entry_t *e2 = key;
541 return e1->id != e2->id;
542 } /* listmap_cmp_ptr */
545 * Initializes a listmap.
547 * @param map the listmap
549 static void listmap_init(listmap_t *map)
551 map->map = new_set(listmap_cmp_ptr, 16);
556 * Terminates a listmap.
558 * @param map the listmap
560 static void listmap_term(listmap_t *map)
566 * Return the associated listmap entry for a given id.
568 * @param map the listmap
569 * @param id the id to search for
571 * @return the associated listmap entry for the given id
573 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
575 listmap_entry_t key, *entry;
580 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
582 if (entry->list == NULL) {
583 /* a new entry, put into the list */
584 entry->next = map->values;
591 * Calculate the hash value for an opcode map entry.
593 * @param entry an opcode map entry
595 * @return a hash value for the given opcode map entry
597 static unsigned opcode_hash(const opcode_key_t *entry)
599 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ptr) + entry->arity;
603 * Compare two entries in the opcode map.
605 static int cmp_opcode(const void *elt, const void *key, size_t size)
607 const opcode_key_t *o1 = elt;
608 const opcode_key_t *o2 = key;
611 return o1->code != o2->code || o1->mode != o2->mode ||
612 o1->arity != o2->arity ||
613 o1->u.proj != o2->u.proj ||
614 o1->u.intVal != o2->u.intVal || /* this already checks uIntVal */
615 o1->u.ptr != o2->u.ptr;
619 * Compare two Def-Use edges for input position.
621 static int cmp_def_use_edge(const void *a, const void *b)
623 const ir_def_use_edge *ea = a;
624 const ir_def_use_edge *eb = b;
626 /* no overrun, because range is [-1, MAXINT] */
627 return ea->pos - eb->pos;
628 } /* cmp_def_use_edge */
631 * We need the Def-Use edges sorted.
633 static void sort_irn_outs(node_t *node)
635 ir_node *irn = node->node;
636 int n_outs = get_irn_n_outs(irn);
639 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
641 node->max_user_input = irn->out[n_outs].pos;
642 } /* sort_irn_outs */
645 * Return the type of a node.
647 * @param irn an IR-node
649 * @return the associated type of this node
651 static inline lattice_elem_t get_node_type(const ir_node *irn)
653 return get_irn_node(irn)->type;
654 } /* get_node_type */
657 * Return the tarval of a node.
659 * @param irn an IR-node
661 * @return the associated type of this node
663 static inline tarval *get_node_tarval(const ir_node *irn)
665 lattice_elem_t type = get_node_type(irn);
667 if (is_tarval(type.tv))
669 return tarval_bottom;
670 } /* get_node_type */
673 * Add a partition to the worklist.
675 static inline void add_to_worklist(partition_t *X, environment_t *env)
677 assert(X->on_worklist == 0);
678 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
679 X->wl_next = env->worklist;
682 } /* add_to_worklist */
685 * Create a new empty partition.
687 * @param env the environment
689 * @return a newly allocated partition
691 static inline partition_t *new_partition(environment_t *env)
693 partition_t *part = OALLOC(&env->obst, partition_t);
695 INIT_LIST_HEAD(&part->Leader);
696 INIT_LIST_HEAD(&part->Follower);
697 INIT_LIST_HEAD(&part->cprop);
698 INIT_LIST_HEAD(&part->cprop_X);
699 part->wl_next = NULL;
700 part->touched_next = NULL;
701 part->cprop_next = NULL;
702 part->split_next = NULL;
703 part->touched = NULL;
706 part->max_user_inputs = 0;
707 part->on_worklist = 0;
708 part->on_touched = 0;
710 part->type_is_T_or_C = 0;
712 part->dbg_next = env->dbg_list;
713 env->dbg_list = part;
714 part->nr = part_nr++;
718 } /* new_partition */
721 * Get the first node from a partition.
723 static inline node_t *get_first_node(const partition_t *X)
725 return list_entry(X->Leader.next, node_t, node_list);
726 } /* get_first_node */
729 * Return the type of a partition (assuming partition is non-empty and
730 * all elements have the same type).
732 * @param X a partition
734 * @return the type of the first element of the partition
736 static inline lattice_elem_t get_partition_type(const partition_t *X)
738 const node_t *first = get_first_node(X);
740 } /* get_partition_type */
743 * Creates a partition node for the given IR-node and place it
744 * into the given partition.
746 * @param irn an IR-node
747 * @param part a partition to place the node in
748 * @param env the environment
750 * @return the created node
752 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env)
754 /* create a partition node and place it in the partition */
755 node_t *node = OALLOC(&env->obst, node_t);
757 INIT_LIST_HEAD(&node->node_list);
758 INIT_LIST_HEAD(&node->cprop_list);
762 node->race_next = NULL;
763 node->type.tv = tarval_top;
764 node->max_user_input = 0;
766 node->n_followers = 0;
767 node->on_touched = 0;
770 node->is_follower = 0;
772 set_irn_node(irn, node);
774 list_add_tail(&node->node_list, &part->Leader);
778 } /* create_partition_node */
781 * Pre-Walker, initialize all Nodes' type to U or top and place
782 * all nodes into the TOP partition.
784 static void create_initial_partitions(ir_node *irn, void *ctx)
786 environment_t *env = ctx;
787 partition_t *part = env->initial;
790 node = create_partition_node(irn, part, env);
792 if (node->max_user_input > part->max_user_inputs)
793 part->max_user_inputs = node->max_user_input;
796 set_Block_phis(irn, NULL);
798 } /* create_initial_partitions */
801 * Post-Walker, collect all Block-Phi lists, set Cond.
803 static void init_block_phis(ir_node *irn, void *ctx)
808 add_Block_phi(get_nodes_block(irn), irn);
810 } /* init_block_phis */
813 * Add a node to the entry.partition.touched set and
814 * node->partition to the touched set if not already there.
817 * @param env the environment
819 static inline void add_to_touched(node_t *y, environment_t *env)
821 if (y->on_touched == 0) {
822 partition_t *part = y->part;
824 y->next = part->touched;
829 if (part->on_touched == 0) {
830 part->touched_next = env->touched;
832 part->on_touched = 1;
835 check_list(part->touched, part);
837 } /* add_to_touched */
840 * Place a node on the cprop list.
843 * @param env the environment
845 static void add_to_cprop(node_t *y, environment_t *env)
849 /* Add y to y.partition.cprop. */
850 if (y->on_cprop == 0) {
851 partition_t *Y = y->part;
852 ir_node *irn = y->node;
854 /* place Conds and all its Projs on the cprop_X list */
855 if (is_Cond(skip_Proj(irn)))
856 list_add_tail(&y->cprop_list, &Y->cprop_X);
858 list_add_tail(&y->cprop_list, &Y->cprop);
861 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
863 /* place its partition on the cprop list */
864 if (Y->on_cprop == 0) {
865 Y->cprop_next = env->cprop;
871 if (get_irn_mode(irn) == mode_T) {
872 /* mode_T nodes always produce tarval_bottom, so we must explicitly
873 add it's Proj's to get constant evaluation to work */
876 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
877 node_t *proj = get_irn_node(get_irn_out(irn, i));
879 add_to_cprop(proj, env);
881 } else if (is_Block(irn)) {
882 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
883 * if someone placed the block. The Block is only placed if the reachability
884 * changes, and this must be re-evaluated in compute_Phi(). */
886 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
887 node_t *p = get_irn_node(phi);
888 add_to_cprop(p, env);
894 * Update the worklist: If Z is on worklist then add Z' to worklist.
895 * Else add the smaller of Z and Z' to worklist.
897 * @param Z the Z partition
898 * @param Z_prime the Z' partition, a previous part of Z
899 * @param env the environment
901 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env)
903 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
904 add_to_worklist(Z_prime, env);
906 add_to_worklist(Z, env);
908 } /* update_worklist */
911 * Make all inputs to x no longer be F.def_use edges.
915 static void move_edges_to_leader(node_t *x)
917 ir_node *irn = x->node;
920 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
921 node_t *pred = get_irn_node(get_irn_n(irn, i));
926 n = get_irn_n_outs(p);
927 for (j = 1; j <= pred->n_followers; ++j) {
928 if (p->out[j].pos == i && p->out[j].use == irn) {
929 /* found a follower edge to x, move it to the Leader */
930 ir_def_use_edge edge = p->out[j];
932 /* remove this edge from the Follower set */
933 p->out[j] = p->out[pred->n_followers];
936 /* sort it into the leader set */
937 for (k = pred->n_followers + 2; k <= n; ++k) {
938 if (p->out[k].pos >= edge.pos)
940 p->out[k - 1] = p->out[k];
942 /* place the new edge here */
943 p->out[k - 1] = edge;
945 /* edge found and moved */
950 } /* move_edges_to_leader */
953 * Split a partition that has NO followers by a local list.
955 * @param Z partition to split
956 * @param g a (non-empty) node list
957 * @param env the environment
959 * @return a new partition containing the nodes of g
961 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env)
963 partition_t *Z_prime;
968 dump_partition("Splitting ", Z);
969 dump_list("by list ", g);
973 /* Remove g from Z. */
974 for (node = g; node != NULL; node = node->next) {
975 assert(node->part == Z);
976 list_del(&node->node_list);
979 assert(n < Z->n_leader);
982 /* Move g to a new partition, Z'. */
983 Z_prime = new_partition(env);
985 for (node = g; node != NULL; node = node->next) {
986 list_add_tail(&node->node_list, &Z_prime->Leader);
987 node->part = Z_prime;
988 if (node->max_user_input > max_input)
989 max_input = node->max_user_input;
991 Z_prime->max_user_inputs = max_input;
992 Z_prime->n_leader = n;
995 check_partition(Z_prime);
997 /* for now, copy the type info tag, it will be adjusted in split_by(). */
998 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
1000 update_worklist(Z, Z_prime, env);
1002 dump_partition("Now ", Z);
1003 dump_partition("Created new ", Z_prime);
1005 } /* split_no_followers */
1008 * Make the Follower -> Leader transition for a node.
1012 static void follower_to_leader(node_t *n)
1014 assert(n->is_follower == 1);
1016 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
1018 move_edges_to_leader(n);
1019 list_del(&n->node_list);
1020 list_add_tail(&n->node_list, &n->part->Leader);
1021 ++n->part->n_leader;
1022 } /* follower_to_leader */
1025 * The environment for one race step.
1027 typedef struct step_env {
1028 node_t *initial; /**< The initial node list. */
1029 node_t *unwalked; /**< The unwalked node list. */
1030 node_t *walked; /**< The walked node list. */
1031 int index; /**< Next index of Follower use_def edge. */
1032 unsigned side; /**< side number. */
1036 * Return non-zero, if a input is a real follower
1038 * @param irn the node to check
1039 * @param input number of the input
1041 static int is_real_follower(const ir_node *irn, int input)
1045 switch (get_irn_opcode(irn)) {
1048 /* ignore the Confirm bound input */
1054 /* ignore the Mux sel input */
1059 /* dead inputs are not follower edges */
1060 ir_node *block = get_nodes_block(irn);
1061 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
1063 if (pred->type.tv == tarval_unreachable)
1073 /* only a Sub x,0 / Shift x,0 might be a follower */
1080 pred = get_irn_node(get_irn_n(irn, input));
1081 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1085 pred = get_irn_node(get_irn_n(irn, input));
1086 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1090 pred = get_irn_node(get_irn_n(irn, input));
1091 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1095 assert(!"opcode not implemented yet");
1099 } /* is_real_follower */
1102 * Do one step in the race.
1104 static int step(step_env *env)
1108 if (env->initial != NULL) {
1109 /* Move node from initial to unwalked */
1111 env->initial = n->race_next;
1113 n->race_next = env->unwalked;
1119 while (env->unwalked != NULL) {
1120 /* let n be the first node in unwalked */
1122 while (env->index < n->n_followers) {
1123 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1125 /* let m be n.F.def_use[index] */
1126 node_t *m = get_irn_node(edge->use);
1128 assert(m->is_follower);
1130 * Some inputs, like the get_Confirm_bound are NOT
1131 * real followers, sort them out.
1133 if (! is_real_follower(m->node, edge->pos)) {
1139 /* only followers from our partition */
1140 if (m->part != n->part)
1143 if ((m->flagged & env->side) == 0) {
1144 m->flagged |= env->side;
1146 if (m->flagged != 3) {
1147 /* visited the first time */
1148 /* add m to unwalked not as first node (we might still need to
1149 check for more follower node */
1150 m->race_next = n->race_next;
1154 /* else already visited by the other side and on the other list */
1157 /* move n to walked */
1158 env->unwalked = n->race_next;
1159 n->race_next = env->walked;
1167 * Clear the flags from a list and check for
1168 * nodes that where touched from both sides.
1170 * @param list the list
1172 static int clear_flags(node_t *list)
1177 for (n = list; n != NULL; n = n->race_next) {
1178 if (n->flagged == 3) {
1179 /* we reach a follower from both sides, this will split congruent
1180 * inputs and make it a leader. */
1181 follower_to_leader(n);
1190 * Split a partition by a local list using the race.
1192 * @param pX pointer to the partition to split, might be changed!
1193 * @param gg a (non-empty) node list
1194 * @param env the environment
1196 * @return a new partition containing the nodes of gg
1198 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env)
1200 partition_t *X = *pX;
1201 partition_t *X_prime;
1204 node_t *g, *h, *node, *t;
1205 int max_input, transitions, winner, shf;
1207 DEBUG_ONLY(static int run = 0;)
1209 DB((dbg, LEVEL_2, "Run %d ", run++));
1210 if (list_empty(&X->Follower)) {
1211 /* if the partition has NO follower, we can use the fast
1212 splitting algorithm. */
1213 return split_no_followers(X, gg, env);
1215 /* else do the race */
1217 dump_partition("Splitting ", X);
1218 dump_list("by list ", gg);
1220 INIT_LIST_HEAD(&tmp);
1222 /* Remove gg from X.Leader and put into g */
1224 for (node = gg; node != NULL; node = node->next) {
1225 assert(node->part == X);
1226 assert(node->is_follower == 0);
1228 list_del(&node->node_list);
1229 list_add_tail(&node->node_list, &tmp);
1230 node->race_next = g;
1235 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1236 node->race_next = h;
1239 /* restore X.Leader */
1240 list_splice(&tmp, &X->Leader);
1242 senv[0].initial = g;
1243 senv[0].unwalked = NULL;
1244 senv[0].walked = NULL;
1248 senv[1].initial = h;
1249 senv[1].unwalked = NULL;
1250 senv[1].walked = NULL;
1255 * Some informations on the race that are not stated clearly in Click's
1257 * 1) A follower stays on the side that reach him first.
1258 * 2) If the other side reches a follower, if will be converted to
1259 * a leader. /This must be done after the race is over, else the
1260 * edges we are iterating on are renumbered./
1261 * 3) /New leader might end up on both sides./
1262 * 4) /If one side ends up with new Leaders, we must ensure that
1263 * they can split out by opcode, hence we have to put _every_
1264 * partition with new Leader nodes on the cprop list, as
1265 * opcode splitting is done by split_by() at the end of
1266 * constant propagation./
1269 if (step(&senv[0])) {
1273 if (step(&senv[1])) {
1278 assert(senv[winner].initial == NULL);
1279 assert(senv[winner].unwalked == NULL);
1281 /* clear flags from walked/unwalked */
1283 transitions = clear_flags(senv[0].unwalked) << shf;
1284 transitions |= clear_flags(senv[0].walked) << shf;
1286 transitions |= clear_flags(senv[1].unwalked) << shf;
1287 transitions |= clear_flags(senv[1].walked) << shf;
1289 dump_race_list("winner ", senv[winner].walked);
1291 /* Move walked_{winner} to a new partition, X'. */
1292 X_prime = new_partition(env);
1295 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1296 list_del(&node->node_list);
1297 node->part = X_prime;
1298 if (node->is_follower) {
1299 list_add_tail(&node->node_list, &X_prime->Follower);
1301 list_add_tail(&node->node_list, &X_prime->Leader);
1304 if (node->max_user_input > max_input)
1305 max_input = node->max_user_input;
1307 X_prime->n_leader = n;
1308 X_prime->max_user_inputs = max_input;
1309 X->n_leader -= X_prime->n_leader;
1311 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1312 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1315 * Even if a follower was not checked by both sides, it might have
1316 * loose its congruence, so we need to check this case for all follower.
1318 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1319 if (identity(node) == node) {
1320 follower_to_leader(node);
1326 check_partition(X_prime);
1328 /* X' is the smaller part */
1329 add_to_worklist(X_prime, env);
1332 * If there where follower to leader transitions, ensure that the nodes
1333 * can be split out if necessary.
1335 if (transitions & 1) {
1336 /* place winner partition on the cprop list */
1337 if (X_prime->on_cprop == 0) {
1338 X_prime->cprop_next = env->cprop;
1339 env->cprop = X_prime;
1340 X_prime->on_cprop = 1;
1343 if (transitions & 2) {
1344 /* place other partition on the cprop list */
1345 if (X->on_cprop == 0) {
1346 X->cprop_next = env->cprop;
1352 dump_partition("Now ", X);
1353 dump_partition("Created new ", X_prime);
1355 /* we have to ensure that the partition containing g is returned */
1365 * Returns non-zero if the i'th input of a Phi node is live.
1367 * @param phi a Phi-node
1368 * @param i an input number
1370 * @return non-zero if the i'th input of the given Phi node is live
1372 static int is_live_input(ir_node *phi, int i)
1375 ir_node *block = get_nodes_block(phi);
1376 ir_node *pred = get_Block_cfgpred(block, i);
1377 lattice_elem_t type = get_node_type(pred);
1379 return type.tv != tarval_unreachable;
1381 /* else it's the control input, always live */
1383 } /* is_live_input */
1386 * Return non-zero if a type is a constant.
1388 static int is_constant_type(lattice_elem_t type)
1390 if (type.tv != tarval_bottom && type.tv != tarval_top)
1393 } /* is_constant_type */
1396 * Check whether a type is neither Top or a constant.
1397 * Note: U is handled like Top here, R is a constant.
1399 * @param type the type to check
1401 static int type_is_neither_top_nor_const(const lattice_elem_t type)
1403 if (is_tarval(type.tv)) {
1404 if (type.tv == tarval_top)
1406 if (tarval_is_constant(type.tv))
1413 } /* type_is_neither_top_nor_const */
1416 * Collect nodes to the touched list.
1418 * @param list the list which contains the nodes that must be evaluated
1419 * @param idx the index of the def_use edge to evaluate
1420 * @param env the environment
1422 static void collect_touched(list_head *list, int idx, environment_t *env)
1425 int end_idx = env->end_idx;
1427 list_for_each_entry(node_t, x, list, node_list) {
1431 /* leader edges start AFTER follower edges */
1432 x->next_edge = x->n_followers + 1;
1434 num_edges = get_irn_n_outs(x->node);
1436 /* for all edges in x.L.def_use_{idx} */
1437 while (x->next_edge <= num_edges) {
1438 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1441 /* check if we have necessary edges */
1442 if (edge->pos > idx)
1449 /* only non-commutative nodes */
1450 if (env->commutative &&
1451 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1454 /* ignore the "control input" for non-pinned nodes
1455 if we are running in GCSE mode */
1456 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1459 y = get_irn_node(succ);
1460 assert(get_irn_n(succ, idx) == x->node);
1462 /* ignore block edges touching followers */
1463 if (idx == -1 && y->is_follower)
1466 if (is_constant_type(y->type)) {
1467 ir_opcode code = get_irn_opcode(succ);
1468 if (code == iro_Sub || code == iro_Cmp)
1469 add_to_cprop(y, env);
1472 /* Partitions of constants should not be split simply because their Nodes have unequal
1473 functions or incongruent inputs. */
1474 if (type_is_neither_top_nor_const(y->type) &&
1475 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1476 add_to_touched(y, env);
1480 } /* collect_touched */
1483 * Collect commutative nodes to the touched list.
1485 * @param list the list which contains the nodes that must be evaluated
1486 * @param env the environment
1488 static void collect_commutative_touched(list_head *list, environment_t *env)
1492 list_for_each_entry(node_t, x, list, node_list) {
1495 num_edges = get_irn_n_outs(x->node);
1497 x->next_edge = x->n_followers + 1;
1499 /* for all edges in x.L.def_use_{idx} */
1500 while (x->next_edge <= num_edges) {
1501 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1504 /* check if we have necessary edges */
1514 /* only commutative nodes */
1515 if (!is_op_commutative(get_irn_op(succ)))
1518 y = get_irn_node(succ);
1519 if (is_constant_type(y->type)) {
1520 ir_opcode code = get_irn_opcode(succ);
1521 if (code == iro_Eor)
1522 add_to_cprop(y, env);
1525 /* Partitions of constants should not be split simply because their Nodes have unequal
1526 functions or incongruent inputs. */
1527 if (type_is_neither_top_nor_const(y->type)) {
1528 add_to_touched(y, env);
1532 } /* collect_commutative_touched */
1535 * Split the partitions if caused by the first entry on the worklist.
1537 * @param env the environment
1539 static void cause_splits(environment_t *env)
1541 partition_t *X, *Z, *N;
1544 /* remove the first partition from the worklist */
1546 env->worklist = X->wl_next;
1549 dump_partition("Cause_split: ", X);
1551 if (env->commutative) {
1552 /* handle commutative nodes first */
1554 /* empty the touched set: already done, just clear the list */
1555 env->touched = NULL;
1557 collect_commutative_touched(&X->Leader, env);
1558 collect_commutative_touched(&X->Follower, env);
1560 for (Z = env->touched; Z != NULL; Z = N) {
1562 node_t *touched = Z->touched;
1563 node_t *touched_aa = NULL;
1564 node_t *touched_ab = NULL;
1565 unsigned n_touched_aa = 0;
1566 unsigned n_touched_ab = 0;
1568 assert(Z->touched != NULL);
1570 /* beware, split might change Z */
1571 N = Z->touched_next;
1573 /* remove it from the touched set */
1576 /* Empty local Z.touched. */
1577 for (e = touched; e != NULL; e = n) {
1578 node_t *left = get_irn_node(get_irn_n(e->node, 0));
1579 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1581 assert(e->is_follower == 0);
1586 * Note: op(a, a) is NOT congruent to op(a, b).
1587 * So, we must split the touched list.
1589 if (left->part == right->part) {
1590 e->next = touched_aa;
1594 e->next = touched_ab;
1599 assert(n_touched_aa + n_touched_ab == Z->n_touched);
1603 if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1604 partition_t *Z_prime = Z;
1605 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1606 split(&Z_prime, touched_aa, env);
1608 assert(n_touched_aa <= Z->n_leader);
1610 if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1611 partition_t *Z_prime = Z;
1612 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1613 split(&Z_prime, touched_ab, env);
1615 assert(n_touched_ab <= Z->n_leader);
1619 /* combine temporary leader and follower list */
1620 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1621 /* empty the touched set: already done, just clear the list */
1622 env->touched = NULL;
1624 collect_touched(&X->Leader, idx, env);
1625 collect_touched(&X->Follower, idx, env);
1627 for (Z = env->touched; Z != NULL; Z = N) {
1629 node_t *touched = Z->touched;
1630 unsigned n_touched = Z->n_touched;
1632 assert(Z->touched != NULL);
1634 /* beware, split might change Z */
1635 N = Z->touched_next;
1637 /* remove it from the touched set */
1640 /* Empty local Z.touched. */
1641 for (e = touched; e != NULL; e = e->next) {
1642 assert(e->is_follower == 0);
1648 if (0 < n_touched && n_touched < Z->n_leader) {
1649 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1650 split(&Z, touched, env);
1652 assert(n_touched <= Z->n_leader);
1655 } /* cause_splits */
1658 * Implements split_by_what(): Split a partition by characteristics given
1659 * by the what function.
1661 * @param X the partition to split
1662 * @param What a function returning an Id for every node of the partition X
1663 * @param P a list to store the result partitions
1664 * @param env the environment
1668 static partition_t *split_by_what(partition_t *X, what_func What,
1669 partition_t **P, environment_t *env) {
1672 listmap_entry_t *iter;
1675 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1677 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1678 void *id = What(x, env);
1679 listmap_entry_t *entry;
1682 /* input not allowed, ignore */
1685 /* Add x to map[What(x)]. */
1686 entry = listmap_find(&map, id);
1687 x->next = entry->list;
1690 /* Let P be a set of Partitions. */
1692 /* for all sets S except one in the range of map do */
1693 for (iter = map.values; iter != NULL; iter = iter->next) {
1694 if (iter->next == NULL) {
1695 /* this is the last entry, ignore */
1700 /* Add SPLIT( X, S ) to P. */
1701 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1702 R = split(&X, S, env);
1712 } /* split_by_what */
1714 /** lambda n.(n.type) */
1715 static void *lambda_type(const node_t *node, environment_t *env)
1718 return node->type.tv;
1721 /** lambda n.(n.opcode) */
1722 static void *lambda_opcode(const node_t *node, environment_t *env)
1724 opcode_key_t key, *entry;
1725 ir_node *irn = node->node;
1727 key.code = get_irn_opcode(irn);
1728 key.mode = get_irn_mode(irn);
1729 key.arity = get_irn_arity(irn);
1733 switch (get_irn_opcode(irn)) {
1735 key.u.proj = get_Proj_proj(irn);
1738 key.u.ent = get_Sel_entity(irn);
1741 key.u.intVal = get_Conv_strict(irn);
1744 key.u.intVal = get_Div_no_remainder(irn);
1748 * Some ugliness here: Two Blocks having the same
1749 * IJmp predecessor would be congruent, which of course is wrong.
1750 * We fix it by never letting blocks be congruent
1751 * which cannot be detected by combo either.
1756 key.mode = get_Load_mode(irn);
1759 key.u.intVal = get_Builtin_kind(irn);
1765 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1767 } /* lambda_opcode */
1769 /** lambda n.(n[i].partition) */
1770 static void *lambda_partition(const node_t *node, environment_t *env)
1772 ir_node *skipped = skip_Proj(node->node);
1775 int i = env->lambda_input;
1777 if (i >= get_irn_arity(node->node)) {
1779 * We are outside the allowed range: This can happen even
1780 * if we have split by opcode first: doing so might move Followers
1781 * to Leaders and those will have a different opcode!
1782 * Note that in this case the partition is on the cprop list and will be
1788 /* ignore the "control input" for non-pinned nodes
1789 if we are running in GCSE mode */
1790 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1793 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1794 p = get_irn_node(pred);
1796 } /* lambda_partition */
1798 /** lambda n.(n[i].partition) for commutative nodes */
1799 static void *lambda_commutative_partition(const node_t *node, environment_t *env)
1801 ir_node *irn = node->node;
1802 ir_node *skipped = skip_Proj(irn);
1803 ir_node *pred, *left, *right;
1805 partition_t *pl, *pr;
1806 int i = env->lambda_input;
1808 if (i >= get_irn_arity(node->node)) {
1810 * We are outside the allowed range: This can happen even
1811 * if we have split by opcode first: doing so might move Followers
1812 * to Leaders and those will have a different opcode!
1813 * Note that in this case the partition is on the cprop list and will be
1819 /* ignore the "control input" for non-pinned nodes
1820 if we are running in GCSE mode */
1821 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1825 pred = get_irn_n(skipped, i);
1826 p = get_irn_node(pred);
1830 if (is_op_commutative(get_irn_op(irn))) {
1831 /* normalize partition order by returning the "smaller" on input 0,
1832 the "bigger" on input 1. */
1833 left = get_binop_left(irn);
1834 pl = get_irn_node(left)->part;
1835 right = get_binop_right(irn);
1836 pr = get_irn_node(right)->part;
1839 return pl < pr ? pl : pr;
1841 return pl > pr ? pl : pr;
1843 /* a not split out Follower */
1844 pred = get_irn_n(irn, i);
1845 p = get_irn_node(pred);
1849 } /* lambda_commutative_partition */
1852 * Returns true if a type is a constant (and NOT Top
1855 static int is_con(const lattice_elem_t type)
1857 /* be conservative */
1858 if (is_tarval(type.tv))
1859 return tarval_is_constant(type.tv);
1860 return is_entity(type.sym.entity_p);
1864 * Implements split_by().
1866 * @param X the partition to split
1867 * @param env the environment
1869 static void split_by(partition_t *X, environment_t *env)
1871 partition_t *I, *P = NULL;
1874 dump_partition("split_by", X);
1876 if (X->n_leader == 1) {
1877 /* we have only one leader, no need to split, just check it's type */
1878 node_t *x = get_first_node(X);
1879 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1883 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1884 P = split_by_what(X, lambda_type, &P, env);
1887 /* adjust the type tags, we have split partitions by type */
1888 for (I = P; I != NULL; I = I->split_next) {
1889 node_t *x = get_first_node(I);
1890 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1897 if (Y->n_leader > 1) {
1898 /* we do not want split the TOP or constant partitions */
1899 if (! Y->type_is_T_or_C) {
1900 partition_t *Q = NULL;
1902 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1903 Q = split_by_what(Y, lambda_opcode, &Q, env);
1910 if (Z->n_leader > 1) {
1911 const node_t *first = get_first_node(Z);
1912 int arity = get_irn_arity(first->node);
1914 what_func what = lambda_partition;
1915 DEBUG_ONLY(char buf[64];)
1917 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1918 what = lambda_commutative_partition;
1921 * BEWARE: during splitting by input 2 for instance we might
1922 * create new partitions which are different by input 1, so collect
1923 * them and split further.
1925 Z->split_next = NULL;
1928 for (input = arity - 1; input >= -1; --input) {
1930 partition_t *Z_prime = R;
1933 if (Z_prime->n_leader > 1) {
1934 env->lambda_input = input;
1935 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1936 DEBUG_ONLY(what_reason = buf;)
1937 S = split_by_what(Z_prime, what, &S, env);
1940 Z_prime->split_next = S;
1943 } while (R != NULL);
1948 } while (Q != NULL);
1951 } while (P != NULL);
1955 * (Re-)compute the type for a given node.
1957 * @param node the node
1959 static void default_compute(node_t *node)
1962 ir_node *irn = node->node;
1964 /* if any of the data inputs have type top, the result is type top */
1965 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1966 ir_node *pred = get_irn_n(irn, i);
1967 node_t *p = get_irn_node(pred);
1969 if (p->type.tv == tarval_top) {
1970 node->type.tv = tarval_top;
1975 if (get_irn_mode(node->node) == mode_X)
1976 node->type.tv = tarval_reachable;
1978 node->type.tv = computed_value(irn);
1979 } /* default_compute */
1982 * (Re-)compute the type for a Block node.
1984 * @param node the node
1986 static void compute_Block(node_t *node)
1989 ir_node *block = node->node;
1991 if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
1992 /* start block and labelled blocks are always reachable */
1993 node->type.tv = tarval_reachable;
1997 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1998 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2000 if (pred->type.tv == tarval_reachable) {
2001 /* A block is reachable, if at least of predecessor is reachable. */
2002 node->type.tv = tarval_reachable;
2006 node->type.tv = tarval_top;
2007 } /* compute_Block */
2010 * (Re-)compute the type for a Bad node.
2012 * @param node the node
2014 static void compute_Bad(node_t *node)
2016 /* Bad nodes ALWAYS compute Top */
2017 node->type.tv = tarval_top;
2021 * (Re-)compute the type for an Unknown node.
2023 * @param node the node
2025 static void compute_Unknown(node_t *node)
2027 /* While Unknown nodes should compute Top this is dangerous:
2028 * a Top input to a Cond would lead to BOTH control flows unreachable.
2029 * While this is correct in the given semantics, it would destroy the Firm
2032 * It would be safe to compute Top IF it can be assured, that only Cmp
2033 * nodes are inputs to Conds. We check that first.
2034 * This is the way Frontends typically build Firm, but some optimizations
2035 * (jump threading for instance) might replace them by Phib's...
2037 node->type.tv = tarval_UNKNOWN;
2038 } /* compute_Unknown */
2041 * (Re-)compute the type for a Jmp node.
2043 * @param node the node
2045 static void compute_Jmp(node_t *node)
2047 node_t *block = get_irn_node(get_nodes_block(node->node));
2049 node->type = block->type;
2053 * (Re-)compute the type for the Return node.
2055 * @param node the node
2057 static void compute_Return(node_t *node)
2059 /* The Return node is NOT dead if it is in a reachable block.
2060 * This is already checked in compute(). so we can return
2061 * Reachable here. */
2062 node->type.tv = tarval_reachable;
2063 } /* compute_Return */
2066 * (Re-)compute the type for the End node.
2068 * @param node the node
2070 static void compute_End(node_t *node)
2072 /* the End node is NOT dead of course */
2073 node->type.tv = tarval_reachable;
2077 * (Re-)compute the type for a Call.
2079 * @param node the node
2081 static void compute_Call(node_t *node)
2084 * A Call computes always bottom, even if it has Unknown
2087 node->type.tv = tarval_bottom;
2088 } /* compute_Call */
2091 * (Re-)compute the type for a SymConst node.
2093 * @param node the node
2095 static void compute_SymConst(node_t *node)
2097 ir_node *irn = node->node;
2098 node_t *block = get_irn_node(get_nodes_block(irn));
2100 if (block->type.tv == tarval_unreachable) {
2101 node->type.tv = tarval_top;
2104 switch (get_SymConst_kind(irn)) {
2105 case symconst_addr_ent:
2106 node->type.sym = get_SymConst_symbol(irn);
2109 node->type.tv = computed_value(irn);
2111 } /* compute_SymConst */
2114 * (Re-)compute the type for a Phi node.
2116 * @param node the node
2118 static void compute_Phi(node_t *node)
2121 ir_node *phi = node->node;
2122 lattice_elem_t type;
2124 /* if a Phi is in a unreachable block, its type is TOP */
2125 node_t *block = get_irn_node(get_nodes_block(phi));
2127 if (block->type.tv == tarval_unreachable) {
2128 node->type.tv = tarval_top;
2132 /* Phi implements the Meet operation */
2133 type.tv = tarval_top;
2134 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2135 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2136 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2138 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2139 /* ignore TOP inputs: We must check here for unreachable blocks,
2140 because Firm constants live in the Start Block are NEVER Top.
2141 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2142 comes from a unreachable input. */
2145 if (pred->type.tv == tarval_bottom) {
2146 node->type.tv = tarval_bottom;
2148 } else if (type.tv == tarval_top) {
2149 /* first constant found */
2151 } else if (type.tv != pred->type.tv) {
2152 /* different constants or tarval_bottom */
2153 node->type.tv = tarval_bottom;
2156 /* else nothing, constants are the same */
2162 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2164 * @param node the node
2166 static void compute_Add(node_t *node)
2168 ir_node *sub = node->node;
2169 node_t *l = get_irn_node(get_Add_left(sub));
2170 node_t *r = get_irn_node(get_Add_right(sub));
2171 lattice_elem_t a = l->type;
2172 lattice_elem_t b = r->type;
2175 if (a.tv == tarval_top || b.tv == tarval_top) {
2176 node->type.tv = tarval_top;
2177 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2178 node->type.tv = tarval_bottom;
2180 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2181 must call tarval_add() first to handle this case! */
2182 if (is_tarval(a.tv)) {
2183 if (is_tarval(b.tv)) {
2184 node->type.tv = tarval_add(a.tv, b.tv);
2187 mode = get_tarval_mode(a.tv);
2188 if (a.tv == get_mode_null(mode)) {
2192 } else if (is_tarval(b.tv)) {
2193 mode = get_tarval_mode(b.tv);
2194 if (b.tv == get_mode_null(mode)) {
2199 node->type.tv = tarval_bottom;
2204 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2206 * @param node the node
2208 static void compute_Sub(node_t *node)
2210 ir_node *sub = node->node;
2211 node_t *l = get_irn_node(get_Sub_left(sub));
2212 node_t *r = get_irn_node(get_Sub_right(sub));
2213 lattice_elem_t a = l->type;
2214 lattice_elem_t b = r->type;
2217 if (a.tv == tarval_top || b.tv == tarval_top) {
2218 node->type.tv = tarval_top;
2219 } else if (is_con(a) && is_con(b)) {
2220 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2221 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2222 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2224 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2227 node->type.tv = tarval_bottom;
2229 } else if (r->part == l->part &&
2230 (!mode_is_float(get_irn_mode(l->node)))) {
2232 * BEWARE: a - a is NOT always 0 for floating Point values, as
2233 * NaN op NaN = NaN, so we must check this here.
2235 ir_mode *mode = get_irn_mode(sub);
2236 tv = get_mode_null(mode);
2238 /* if the node was ONCE evaluated by all constants, but now
2239 this breaks AND we get from the argument partitions a different
2240 result, switch to bottom.
2241 This happens because initially all nodes are in the same partition ... */
2242 if (node->type.tv != tv)
2246 node->type.tv = tarval_bottom;
2251 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2253 * @param node the node
2255 static void compute_Eor(node_t *node)
2257 ir_node *eor = node->node;
2258 node_t *l = get_irn_node(get_Eor_left(eor));
2259 node_t *r = get_irn_node(get_Eor_right(eor));
2260 lattice_elem_t a = l->type;
2261 lattice_elem_t b = r->type;
2264 if (a.tv == tarval_top || b.tv == tarval_top) {
2265 node->type.tv = tarval_top;
2266 } else if (is_con(a) && is_con(b)) {
2267 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2268 node->type.tv = tarval_eor(a.tv, b.tv);
2269 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2271 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2274 node->type.tv = tarval_bottom;
2276 } else if (r->part == l->part) {
2277 ir_mode *mode = get_irn_mode(eor);
2278 tv = get_mode_null(mode);
2280 /* if the node was ONCE evaluated by all constants, but now
2281 this breaks AND we get from the argument partitions a different
2282 result, switch to bottom.
2283 This happens because initially all nodes are in the same partition ... */
2284 if (node->type.tv != tv)
2288 node->type.tv = tarval_bottom;
2293 * (Re-)compute the type for Cmp.
2295 * @param node the node
2297 static void compute_Cmp(node_t *node)
2299 ir_node *cmp = node->node;
2300 node_t *l = get_irn_node(get_Cmp_left(cmp));
2301 node_t *r = get_irn_node(get_Cmp_right(cmp));
2302 lattice_elem_t a = l->type;
2303 lattice_elem_t b = r->type;
2305 if (a.tv == tarval_top || b.tv == tarval_top) {
2306 node->type.tv = tarval_top;
2307 } else if (r->part == l->part) {
2308 /* both nodes congruent, we can probably do something */
2309 node->type.tv = tarval_b_true;
2310 } else if (is_con(a) && is_con(b)) {
2311 /* both nodes are constants, we can probably do something */
2312 node->type.tv = tarval_b_true;
2314 node->type.tv = tarval_bottom;
2319 * (Re-)compute the type for a Proj(Cmp).
2321 * @param node the node
2322 * @param cond the predecessor Cmp node
2324 static void compute_Proj_Cmp(node_t *node, ir_node *cmp)
2326 ir_node *proj = node->node;
2327 node_t *l = get_irn_node(get_Cmp_left(cmp));
2328 node_t *r = get_irn_node(get_Cmp_right(cmp));
2329 lattice_elem_t a = l->type;
2330 lattice_elem_t b = r->type;
2331 pn_Cmp pnc = get_Proj_proj(proj);
2334 if (a.tv == tarval_top || b.tv == tarval_top) {
2335 node->type.tv = tarval_undefined;
2336 } else if (is_con(a) && is_con(b)) {
2337 default_compute(node);
2338 } else if (r->part == l->part &&
2339 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2341 * BEWARE: a == a is NOT always True for floating Point values, as
2342 * NaN != NaN is defined, so we must check this here.
2344 tv = pnc & pn_Cmp_Eq ? tarval_b_true: tarval_b_false;
2346 /* if the node was ONCE evaluated by all constants, but now
2347 this breaks AND we get from the argument partitions a different
2348 result, switch to bottom.
2349 This happens because initially all nodes are in the same partition ... */
2350 if (node->type.tv != tv)
2354 node->type.tv = tarval_bottom;
2356 } /* compute_Proj_Cmp */
2359 * (Re-)compute the type for a Proj(Cond).
2361 * @param node the node
2362 * @param cond the predecessor Cond node
2364 static void compute_Proj_Cond(node_t *node, ir_node *cond)
2366 ir_node *proj = node->node;
2367 long pnc = get_Proj_proj(proj);
2368 ir_node *sel = get_Cond_selector(cond);
2369 node_t *selector = get_irn_node(sel);
2372 * Note: it is crucial for the monotony that the Proj(Cond)
2373 * are evaluates after all predecessors of the Cond selector are
2379 * Due to the fact that 0 is a const, the Cmp gets immediately
2380 * on the cprop list. It will be evaluated before x is evaluated,
2381 * might leaving x as Top. When later x is evaluated, the Cmp
2382 * might change its value.
2383 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2384 * gets R, and later changed to F if Cmp is evaluated to True!
2386 * We prevent this by putting Conds in an extra cprop_X queue, which
2387 * gets evaluated after the cprop queue is empty.
2389 * Note that this even happens with Click's original algorithm, if
2390 * Cmp(x, 0) is evaluated to True first and later changed to False
2391 * if x was Top first and later changed to a Const ...
2392 * It is unclear how Click solved that problem ...
2394 * However, in rare cases even this does not help, if a Top reaches
2395 * a compare through a Phi, than Proj(Cond) is evaluated changing
2396 * the type of the Phi to something other.
2397 * So, we take the last resort and bind the type to R once
2400 * (This might be even the way Click works around the whole problem).
2402 * Finally, we may miss some optimization possibilities due to this:
2407 * If Top reaches the if first, than we decide for != here.
2408 * If y later is evaluated to 0, we cannot revert this decision
2409 * and must live with both outputs enabled. If this happens,
2410 * we get an unresolved if (true) in the code ...
2412 * In Click's version where this decision is done at the Cmp,
2413 * the Cmp is NOT optimized away than (if y evaluated to 1
2414 * for instance) and we get a if (1 == 0) here ...
2416 * Both solutions are suboptimal.
2417 * At least, we could easily detect this problem and run
2418 * cf_opt() (or even combo) again :-(
2420 if (node->type.tv == tarval_reachable)
2423 if (get_irn_mode(sel) == mode_b) {
2425 if (pnc == pn_Cond_true) {
2426 if (selector->type.tv == tarval_b_false) {
2427 node->type.tv = tarval_unreachable;
2428 } else if (selector->type.tv == tarval_b_true) {
2429 node->type.tv = tarval_reachable;
2430 } else if (selector->type.tv == tarval_bottom) {
2431 node->type.tv = tarval_reachable;
2433 assert(selector->type.tv == tarval_top);
2434 if (tarval_UNKNOWN == tarval_top) {
2435 /* any condition based on Top is "!=" */
2436 node->type.tv = tarval_unreachable;
2438 node->type.tv = tarval_unreachable;
2442 assert(pnc == pn_Cond_false);
2444 if (selector->type.tv == tarval_b_false) {
2445 node->type.tv = tarval_reachable;
2446 } else if (selector->type.tv == tarval_b_true) {
2447 node->type.tv = tarval_unreachable;
2448 } else if (selector->type.tv == tarval_bottom) {
2449 node->type.tv = tarval_reachable;
2451 assert(selector->type.tv == tarval_top);
2452 if (tarval_UNKNOWN == tarval_top) {
2453 /* any condition based on Top is "!=" */
2454 node->type.tv = tarval_reachable;
2456 node->type.tv = tarval_unreachable;
2462 if (selector->type.tv == tarval_bottom) {
2463 node->type.tv = tarval_reachable;
2464 } else if (selector->type.tv == tarval_top) {
2465 if (tarval_UNKNOWN == tarval_top &&
2466 pnc == get_Cond_default_proj(cond)) {
2467 /* a switch based of Top is always "default" */
2468 node->type.tv = tarval_reachable;
2470 node->type.tv = tarval_unreachable;
2473 long value = get_tarval_long(selector->type.tv);
2474 if (pnc == get_Cond_default_proj(cond)) {
2475 /* default switch, have to check ALL other cases */
2478 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2479 ir_node *succ = get_irn_out(cond, i);
2483 if (value == get_Proj_proj(succ)) {
2484 /* we found a match, will NOT take the default case */
2485 node->type.tv = tarval_unreachable;
2489 /* all cases checked, no match, will take default case */
2490 node->type.tv = tarval_reachable;
2493 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2497 } /* compute_Proj_Cond */
2500 * (Re-)compute the type for a Proj-Node.
2502 * @param node the node
2504 static void compute_Proj(node_t *node)
2506 ir_node *proj = node->node;
2507 ir_mode *mode = get_irn_mode(proj);
2508 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2509 ir_node *pred = get_Proj_pred(proj);
2511 if (block->type.tv == tarval_unreachable) {
2512 /* a Proj in a unreachable Block stay Top */
2513 node->type.tv = tarval_top;
2516 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred)) {
2517 /* if the predecessor is Top, its Proj follow */
2518 node->type.tv = tarval_top;
2522 if (mode == mode_M) {
2523 /* mode M is always bottom */
2524 node->type.tv = tarval_bottom;
2527 if (mode != mode_X) {
2529 compute_Proj_Cmp(node, pred);
2531 default_compute(node);
2534 /* handle mode_X nodes */
2536 switch (get_irn_opcode(pred)) {
2538 /* the Proj_X from the Start is always reachable.
2539 However this is already handled at the top. */
2540 node->type.tv = tarval_reachable;
2543 compute_Proj_Cond(node, pred);
2546 default_compute(node);
2548 } /* compute_Proj */
2551 * (Re-)compute the type for a Confirm.
2553 * @param node the node
2555 static void compute_Confirm(node_t *node)
2557 ir_node *confirm = node->node;
2558 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2560 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2561 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2563 if (is_con(bound->type)) {
2564 /* is equal to a constant */
2565 node->type = bound->type;
2569 /* a Confirm is a copy OR a Const */
2570 node->type = pred->type;
2571 } /* compute_Confirm */
2574 * (Re-)compute the type for a given node.
2576 * @param node the node
2578 static void compute(node_t *node)
2580 ir_node *irn = node->node;
2583 #ifndef VERIFY_MONOTONE
2585 * Once a node reaches bottom, the type cannot fall further
2586 * in the lattice and we can stop computation.
2587 * Do not take this exit if the monotony verifier is
2588 * enabled to catch errors.
2590 if (node->type.tv == tarval_bottom)
2594 if (is_no_Block(irn)) {
2595 /* for pinned nodes, check its control input */
2596 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2597 node_t *block = get_irn_node(get_nodes_block(irn));
2599 if (block->type.tv == tarval_unreachable) {
2600 node->type.tv = tarval_top;
2606 func = (compute_func)node->node->op->ops.generic;
2612 * Identity functions: Note that one might thing that identity() is just a
2613 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2614 * here, because it expects that the identity node is one of the inputs, which is NOT
2615 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2616 * So, we have our own implementation, which copies some parts of equivalent_node()
2620 * Calculates the Identity for Phi nodes
2622 static node_t *identity_Phi(node_t *node)
2624 ir_node *phi = node->node;
2625 ir_node *block = get_nodes_block(phi);
2626 node_t *n_part = NULL;
2629 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2630 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2632 if (pred_X->type.tv == tarval_reachable) {
2633 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2637 else if (n_part->part != pred->part) {
2638 /* incongruent inputs, not a follower */
2643 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2644 * tarval_top, is in the TOP partition and should NOT being split! */
2645 assert(n_part != NULL);
2647 } /* identity_Phi */
2650 * Calculates the Identity for commutative 0 neutral nodes.
2652 static node_t *identity_comm_zero_binop(node_t *node)
2654 ir_node *op = node->node;
2655 node_t *a = get_irn_node(get_binop_left(op));
2656 node_t *b = get_irn_node(get_binop_right(op));
2657 ir_mode *mode = get_irn_mode(op);
2660 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2661 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2664 /* node: no input should be tarval_top, else the binop would be also
2665 * Top and not being split. */
2666 zero = get_mode_null(mode);
2667 if (a->type.tv == zero)
2669 if (b->type.tv == zero)
2672 } /* identity_comm_zero_binop */
2675 * Calculates the Identity for Shift nodes.
2677 static node_t *identity_shift(node_t *node)
2679 ir_node *op = node->node;
2680 node_t *b = get_irn_node(get_binop_right(op));
2681 ir_mode *mode = get_irn_mode(b->node);
2684 /* node: no input should be tarval_top, else the binop would be also
2685 * Top and not being split. */
2686 zero = get_mode_null(mode);
2687 if (b->type.tv == zero)
2688 return get_irn_node(get_binop_left(op));
2690 } /* identity_shift */
2693 * Calculates the Identity for Mul nodes.
2695 static node_t *identity_Mul(node_t *node)
2697 ir_node *op = node->node;
2698 node_t *a = get_irn_node(get_Mul_left(op));
2699 node_t *b = get_irn_node(get_Mul_right(op));
2700 ir_mode *mode = get_irn_mode(op);
2703 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2704 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2707 /* node: no input should be tarval_top, else the binop would be also
2708 * Top and not being split. */
2709 one = get_mode_one(mode);
2710 if (a->type.tv == one)
2712 if (b->type.tv == one)
2715 } /* identity_Mul */
2718 * Calculates the Identity for Sub nodes.
2720 static node_t *identity_Sub(node_t *node)
2722 ir_node *sub = node->node;
2723 node_t *b = get_irn_node(get_Sub_right(sub));
2724 ir_mode *mode = get_irn_mode(sub);
2726 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2727 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2730 /* node: no input should be tarval_top, else the binop would be also
2731 * Top and not being split. */
2732 if (b->type.tv == get_mode_null(mode))
2733 return get_irn_node(get_Sub_left(sub));
2735 } /* identity_Sub */
2738 * Calculates the Identity for And nodes.
2740 static node_t *identity_And(node_t *node)
2742 ir_node *and = node->node;
2743 node_t *a = get_irn_node(get_And_left(and));
2744 node_t *b = get_irn_node(get_And_right(and));
2745 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2747 /* node: no input should be tarval_top, else the And would be also
2748 * Top and not being split. */
2749 if (a->type.tv == neutral)
2751 if (b->type.tv == neutral)
2754 } /* identity_And */
2757 * Calculates the Identity for Confirm nodes.
2759 static node_t *identity_Confirm(node_t *node)
2761 ir_node *confirm = node->node;
2763 /* a Confirm is always a Copy */
2764 return get_irn_node(get_Confirm_value(confirm));
2765 } /* identity_Confirm */
2768 * Calculates the Identity for Mux nodes.
2770 static node_t *identity_Mux(node_t *node)
2772 ir_node *mux = node->node;
2773 node_t *t = get_irn_node(get_Mux_true(mux));
2774 node_t *f = get_irn_node(get_Mux_false(mux));
2777 if (t->part == f->part)
2780 /* for now, the 1-input identity is not supported */
2782 sel = get_irn_node(get_Mux_sel(mux));
2784 /* Mux sel input is mode_b, so it is always a tarval */
2785 if (sel->type.tv == tarval_b_true)
2787 if (sel->type.tv == tarval_b_false)
2791 } /* identity_Mux */
2794 * Calculates the Identity for nodes.
2796 static node_t *identity(node_t *node)
2798 ir_node *irn = node->node;
2800 switch (get_irn_opcode(irn)) {
2802 return identity_Phi(node);
2804 return identity_Mul(node);
2808 return identity_comm_zero_binop(node);
2813 return identity_shift(node);
2815 return identity_And(node);
2817 return identity_Sub(node);
2819 return identity_Confirm(node);
2821 return identity_Mux(node);
2828 * Node follower is a (new) follower of leader, segregate Leader
2831 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader)
2833 ir_node *l = leader->node;
2834 int j, i, n = get_irn_n_outs(l);
2836 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2837 /* The leader edges must remain sorted, but follower edges can
2839 for (i = leader->n_followers + 1; i <= n; ++i) {
2840 if (l->out[i].use == follower) {
2841 ir_def_use_edge t = l->out[i];
2843 for (j = i - 1; j >= leader->n_followers + 1; --j)
2844 l->out[j + 1] = l->out[j];
2845 ++leader->n_followers;
2846 l->out[leader->n_followers] = t;
2850 } /* segregate_def_use_chain_1 */
2853 * Node follower is a (new) follower segregate its Leader
2856 * @param follower the follower IR node
2858 static void segregate_def_use_chain(const ir_node *follower)
2862 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2863 node_t *pred = get_irn_node(get_irn_n(follower, i));
2865 segregate_def_use_chain_1(follower, pred);
2867 } /* segregate_def_use_chain */
2870 * Propagate constant evaluation.
2872 * @param env the environment
2874 static void propagate(environment_t *env)
2878 lattice_elem_t old_type;
2880 unsigned n_fallen, old_type_was_T_or_C;
2883 while (env->cprop != NULL) {
2884 void *oldopcode = NULL;
2886 /* remove the first partition X from cprop */
2889 env->cprop = X->cprop_next;
2891 old_type_was_T_or_C = X->type_is_T_or_C;
2893 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2897 int cprop_empty = list_empty(&X->cprop);
2898 int cprop_X_empty = list_empty(&X->cprop_X);
2900 if (cprop_empty && cprop_X_empty) {
2901 /* both cprop lists are empty */
2905 /* remove the first Node x from X.cprop */
2907 /* Get a node from the cprop_X list only if
2908 * all data nodes are processed.
2909 * This ensures, that all inputs of the Cond
2910 * predecessor are processed if its type is still Top.
2912 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2914 x = list_entry(X->cprop.next, node_t, cprop_list);
2917 //assert(x->part == X);
2918 list_del(&x->cprop_list);
2921 if (x->is_follower && identity(x) == x) {
2922 /* check the opcode first */
2923 if (oldopcode == NULL) {
2924 oldopcode = lambda_opcode(get_first_node(X), env);
2926 if (oldopcode != lambda_opcode(x, env)) {
2927 if (x->on_fallen == 0) {
2928 /* different opcode -> x falls out of this partition */
2933 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2937 /* x will make the follower -> leader transition */
2938 follower_to_leader(x);
2941 /* compute a new type for x */
2943 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2945 if (x->type.tv != old_type.tv) {
2946 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2947 verify_type(old_type, x);
2949 if (x->on_fallen == 0) {
2950 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2951 not already on the list. */
2956 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2958 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2959 ir_node *succ = get_irn_out(x->node, i);
2960 node_t *y = get_irn_node(succ);
2962 /* Add y to y.partition.cprop. */
2963 add_to_cprop(y, env);
2968 if (n_fallen > 0 && n_fallen != X->n_leader) {
2969 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2970 Y = split(&X, fallen, env);
2972 * We have split out fallen node. The type of the result
2973 * partition is NOT set yet.
2975 Y->type_is_T_or_C = 0;
2979 /* remove the flags from the fallen list */
2980 for (x = fallen; x != NULL; x = x->next)
2983 if (old_type_was_T_or_C) {
2986 /* check if some nodes will make the leader -> follower transition */
2987 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2988 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2989 node_t *eq_node = identity(y);
2991 if (eq_node != y && eq_node->part == y->part) {
2992 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2993 /* move to Follower */
2995 list_del(&y->node_list);
2996 list_add_tail(&y->node_list, &Y->Follower);
2999 segregate_def_use_chain(y->node);
3009 * Get the leader for a given node from its congruence class.
3011 * @param irn the node
3013 static ir_node *get_leader(node_t *node)
3015 partition_t *part = node->part;
3017 if (part->n_leader > 1 || node->is_follower) {
3018 if (node->is_follower) {
3019 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
3022 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
3024 return get_first_node(part)->node;
3030 * Returns non-zero if a mode_T node has only one reachable output.
3032 static int only_one_reachable_proj(ir_node *n)
3036 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
3037 ir_node *proj = get_irn_out(n, i);
3040 /* skip non-control flow Proj's */
3041 if (get_irn_mode(proj) != mode_X)
3044 node = get_irn_node(proj);
3045 if (node->type.tv == tarval_reachable) {
3051 } /* only_one_reachable_proj */
3054 * Return non-zero if the control flow predecessor node pred
3055 * is the only reachable control flow exit of its block.
3057 * @param pred the control flow exit
3058 * @param block the destination block
3060 static int can_exchange(ir_node *pred, ir_node *block)
3062 if (is_Start(pred) || has_Block_entity(block))
3064 else if (is_Jmp(pred))
3066 else if (get_irn_mode(pred) == mode_T) {
3067 /* if the predecessor block has more than one
3068 reachable outputs we cannot remove the block */
3069 return only_one_reachable_proj(pred);
3072 } /* can_exchange */
3075 * Block Post-Walker, apply the analysis results on control flow by
3076 * shortening Phi's and Block inputs.
3078 static void apply_cf(ir_node *block, void *ctx)
3080 environment_t *env = ctx;
3081 node_t *node = get_irn_node(block);
3083 ir_node **ins, **in_X;
3084 ir_node *phi, *next;
3086 n = get_Block_n_cfgpreds(block);
3088 if (node->type.tv == tarval_unreachable) {
3091 for (i = n - 1; i >= 0; --i) {
3092 ir_node *pred = get_Block_cfgpred(block, i);
3094 if (! is_Bad(pred)) {
3095 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3097 if (pred_bl->flagged == 0) {
3098 pred_bl->flagged = 3;
3100 if (pred_bl->type.tv == tarval_reachable) {
3102 * We will remove an edge from block to its pred.
3103 * This might leave the pred block as an endless loop
3105 if (! is_backedge(block, i))
3106 keep_alive(pred_bl->node);
3112 /* the EndBlock is always reachable even if the analysis
3113 finds out the opposite :-) */
3114 if (block != get_irg_end_block(current_ir_graph)) {
3115 /* mark dead blocks */
3116 set_Block_dead(block);
3117 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
3119 /* the endblock is unreachable */
3120 set_irn_in(block, 0, NULL);
3126 /* only one predecessor combine */
3127 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3129 if (can_exchange(pred, block)) {
3130 ir_node *new_block = get_nodes_block(pred);
3131 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3132 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3133 exchange(block, new_block);
3134 node->node = new_block;
3140 NEW_ARR_A(ir_node *, in_X, n);
3142 for (i = 0; i < n; ++i) {
3143 ir_node *pred = get_Block_cfgpred(block, i);
3144 node_t *node = get_irn_node(pred);
3146 if (node->type.tv == tarval_reachable) {
3149 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3150 if (! is_Bad(pred)) {
3151 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3153 if (pred_bl->flagged == 0) {
3154 pred_bl->flagged = 3;
3156 if (pred_bl->type.tv == tarval_reachable) {
3158 * We will remove an edge from block to its pred.
3159 * This might leave the pred block as an endless loop
3161 if (! is_backedge(block, i))
3162 keep_alive(pred_bl->node);
3172 NEW_ARR_A(ir_node *, ins, n);
3173 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3174 node_t *node = get_irn_node(phi);
3176 next = get_Phi_next(phi);
3177 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3178 /* this Phi is replaced by a constant */
3179 tarval *tv = node->type.tv;
3180 ir_node *c = new_Const(tv);
3182 set_irn_node(c, node);
3184 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3185 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3190 for (i = 0; i < n; ++i) {
3191 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3193 if (pred->type.tv == tarval_reachable) {
3194 ins[j++] = get_Phi_pred(phi, i);
3198 /* this Phi is replaced by a single predecessor */
3199 ir_node *s = ins[0];
3200 node_t *phi_node = get_irn_node(phi);
3203 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3204 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3209 set_irn_in(phi, j, ins);
3217 /* this Block has only one live predecessor */
3218 ir_node *pred = skip_Proj(in_X[0]);
3220 if (can_exchange(pred, block)) {
3221 ir_node *new_block = get_nodes_block(pred);
3222 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3223 exchange(block, new_block);
3224 node->node = new_block;
3229 set_irn_in(block, k, in_X);
3234 * Exchange a node by its leader.
3235 * Beware: in rare cases the mode might be wrong here, for instance
3236 * AddP(x, NULL) is a follower of x, but with different mode.
3239 static void exchange_leader(ir_node *irn, ir_node *leader)
3241 ir_mode *mode = get_irn_mode(irn);
3242 if (mode != get_irn_mode(leader)) {
3243 /* The conv is a no-op, so we are free to place it
3244 * either in the block of the leader OR in irn's block.
3245 * Probably placing it into leaders block might reduce
3246 * the number of Conv due to CSE. */
3247 ir_node *block = get_nodes_block(leader);
3248 dbg_info *dbg = get_irn_dbg_info(irn);
3250 leader = new_rd_Conv(dbg, block, leader, mode);
3252 exchange(irn, leader);
3253 } /* exchange_leader */
3256 * Check, if all users of a mode_M node are dead. Use
3257 * the Def-Use edges for this purpose, as they still
3258 * reflect the situation.
3260 static int all_users_are_dead(const ir_node *irn)
3262 int i, n = get_irn_n_outs(irn);
3264 for (i = 1; i <= n; ++i) {
3265 const ir_node *succ = irn->out[i].use;
3266 const node_t *block = get_irn_node(get_nodes_block(succ));
3269 if (block->type.tv == tarval_unreachable) {
3270 /* block is unreachable */
3273 node = get_irn_node(succ);
3274 if (node->type.tv != tarval_top) {
3275 /* found a reachable user */
3279 /* all users are unreachable */
3281 } /* all_user_are_dead */
3284 * Walker: Find reachable mode_M nodes that have only
3285 * unreachable users. These nodes must be kept later.
3287 static void find_kept_memory(ir_node *irn, void *ctx)
3289 environment_t *env = ctx;
3290 node_t *node, *block;
3292 if (get_irn_mode(irn) != mode_M)
3295 block = get_irn_node(get_nodes_block(irn));
3296 if (block->type.tv == tarval_unreachable)
3299 node = get_irn_node(irn);
3300 if (node->type.tv == tarval_top)
3303 /* ok, we found a live memory node. */
3304 if (all_users_are_dead(irn)) {
3305 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3306 ARR_APP1(ir_node *, env->kept_memory, irn);
3308 } /* find_kept_memory */
3311 * Post-Walker, apply the analysis results;
3313 static void apply_result(ir_node *irn, void *ctx)
3315 environment_t *env = ctx;
3316 node_t *node = get_irn_node(irn);
3318 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3319 /* blocks already handled, do not touch the End node */
3321 node_t *block = get_irn_node(get_nodes_block(irn));
3323 if (block->type.tv == tarval_unreachable) {
3324 ir_node *bad = get_irg_bad(current_ir_graph);
3326 /* here, bad might already have a node, but this can be safely ignored
3327 as long as bad has at least ONE valid node */
3328 set_irn_node(bad, node);
3330 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3333 } else if (node->type.tv == tarval_top) {
3334 ir_mode *mode = get_irn_mode(irn);
3336 if (mode == mode_M) {
3337 /* never kill a mode_M node */
3339 ir_node *pred = get_Proj_pred(irn);
3340 node_t *pnode = get_irn_node(pred);
3342 if (pnode->type.tv == tarval_top) {
3343 /* skip the predecessor */
3344 ir_node *mem = get_memop_mem(pred);
3346 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3351 /* leave other nodes, especially PhiM */
3352 } else if (mode == mode_T) {
3353 /* Do not kill mode_T nodes, kill their Projs */
3354 } else if (! is_Unknown(irn)) {
3355 /* don't kick away Unknown's, they might be still needed */
3356 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3358 /* control flow should already be handled at apply_cf() */
3359 assert(mode != mode_X);
3361 /* see comment above */
3362 set_irn_node(unk, node);
3364 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3369 else if (get_irn_mode(irn) == mode_X) {
3372 ir_node *cond = get_Proj_pred(irn);
3374 if (is_Cond(cond)) {
3375 if (only_one_reachable_proj(cond)) {
3376 ir_node *jmp = new_r_Jmp(block->node);
3377 set_irn_node(jmp, node);
3379 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3380 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3384 node_t *sel = get_irn_node(get_Cond_selector(cond));
3385 tarval *tv = sel->type.tv;
3387 if (is_tarval(tv) && tarval_is_constant(tv)) {
3388 /* The selector is a constant, but more
3389 * than one output is active: An unoptimized
3397 /* normal data node */
3398 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3399 tarval *tv = node->type.tv;
3402 * Beware: never replace mode_T nodes by constants. Currently we must mark
3403 * mode_T nodes with constants, but do NOT replace them.
3405 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3406 /* can be replaced by a constant */
3407 ir_node *c = new_Const(tv);
3408 set_irn_node(c, node);
3410 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3411 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3412 exchange_leader(irn, c);
3415 } else if (is_entity(node->type.sym.entity_p)) {
3416 if (! is_SymConst(irn)) {
3417 /* can be replaced by a SymConst */
3418 ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3419 set_irn_node(symc, node);
3422 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3423 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3424 exchange_leader(irn, symc);
3427 } else if (is_Confirm(irn)) {
3428 /* Confirms are always follower, but do not kill them here */
3430 ir_node *leader = get_leader(node);
3432 if (leader != irn) {
3433 int non_strict_phi = 0;
3436 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3437 * as this might create non-strict programs.
3439 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3442 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3443 ir_node *pred = get_Phi_pred(irn, i);
3445 if (is_Unknown(pred)) {
3451 if (! non_strict_phi) {
3452 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3453 if (node->is_follower)
3454 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3456 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3457 exchange_leader(irn, leader);
3464 } /* apply_result */
3467 * Fix the keep-alives by deleting unreachable ones.
3469 static void apply_end(ir_node *end, environment_t *env)
3471 int i, j, n = get_End_n_keepalives(end);
3475 NEW_ARR_A(ir_node *, in, n);
3477 /* fix the keep alive */
3478 for (i = j = 0; i < n; i++) {
3479 ir_node *ka = get_End_keepalive(end, i);
3480 node_t *node = get_irn_node(ka);
3483 node = get_irn_node(get_nodes_block(ka));
3485 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3489 set_End_keepalives(end, j, in);
3494 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3497 * sets the generic functions to compute.
3499 static void set_compute_functions(void)
3503 /* set the default compute function */
3504 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3505 ir_op *op = get_irp_opcode(i);
3506 op->ops.generic = (op_func)default_compute;
3509 /* set specific functions */
3525 } /* set_compute_functions */
3530 static void add_memory_keeps(ir_node **kept_memory, int len)
3532 ir_node *end = get_irg_end(current_ir_graph);
3536 ir_nodeset_init(&set);
3538 /* check, if those nodes are already kept */
3539 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3540 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3542 for (i = len - 1; i >= 0; --i) {
3543 ir_node *ka = kept_memory[i];
3545 if (! ir_nodeset_contains(&set, ka)) {
3546 add_End_keepalive(end, ka);
3549 ir_nodeset_destroy(&set);
3550 } /* add_memory_keeps */
3552 void combo(ir_graph *irg)
3555 ir_node *initial_bl;
3557 ir_graph *rem = current_ir_graph;
3560 current_ir_graph = irg;
3562 /* register a debug mask */
3563 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3565 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3567 obstack_init(&env.obst);
3568 env.worklist = NULL;
3572 #ifdef DEBUG_libfirm
3573 env.dbg_list = NULL;
3575 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3576 env.kept_memory = NEW_ARR_F(ir_node *, 0);
3577 env.end_idx = get_opt_global_cse() ? 0 : -1;
3578 env.lambda_input = 0;
3581 /* options driving the optimization */
3582 env.commutative = 1;
3583 env.opt_unknown = 1;
3585 assure_irg_outs(irg);
3586 assure_cf_loop(irg);
3588 /* we have our own value_of function */
3589 set_value_of_func(get_node_tarval);
3591 set_compute_functions();
3592 DEBUG_ONLY(part_nr = 0);
3594 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3596 if (env.opt_unknown)
3597 tarval_UNKNOWN = tarval_top;
3599 tarval_UNKNOWN = tarval_bad;
3601 /* create the initial partition and place it on the work list */
3602 env.initial = new_partition(&env);
3603 add_to_worklist(env.initial, &env);
3604 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3606 /* set the hook: from now, every node has a partition and a type */
3607 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook));
3609 /* all nodes on the initial partition have type Top */
3610 env.initial->type_is_T_or_C = 1;
3612 /* Place the START Node's partition on cprop.
3613 Place the START Node on its local worklist. */
3614 initial_bl = get_irg_start_block(irg);
3615 start = get_irn_node(initial_bl);
3616 add_to_cprop(start, &env);
3620 if (env.worklist != NULL)
3622 } while (env.cprop != NULL || env.worklist != NULL);
3624 dump_all_partitions(&env);
3625 check_all_partitions(&env);
3628 dump_ir_block_graph(irg, "-partition");
3631 /* apply the result */
3633 /* check, which nodes must be kept */
3634 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3636 /* kill unreachable control flow */
3637 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3638 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3639 * and fixes assertion because dead cf to dead blocks is NOT removed by
3641 apply_end(get_irg_end(irg), &env);
3642 irg_walk_graph(irg, NULL, apply_result, &env);
3644 len = ARR_LEN(env.kept_memory);
3646 add_memory_keeps(env.kept_memory, len);
3649 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3653 /* control flow might changed */
3654 set_irg_outs_inconsistent(irg);
3655 set_irg_extblk_inconsistent(irg);
3656 set_irg_doms_inconsistent(irg);
3657 set_irg_loopinfo_inconsistent(irg);
3658 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
3661 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3663 /* remove the partition hook */
3664 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
3666 DEL_ARR_F(env.kept_memory);
3667 del_set(env.opcode2id_map);
3668 obstack_free(&env.obst, NULL);
3670 /* restore value_of() default behavior */
3671 set_value_of_func(NULL);
3672 current_ir_graph = rem;
3675 /* Creates an ir_graph pass for combo. */
3676 ir_graph_pass_t *combo_pass(const char *name)
3678 return def_graph_pass(name ? name : "combo", combo);