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) {
246 list_for_each_entry(node_t, node, &T->Leader, node_list) {
247 assert(node->is_follower == 0);
248 assert(node->flagged == 0);
249 assert(node->part == T);
252 assert(n == T->n_leader);
254 list_for_each_entry(node_t, node, &T->Follower, node_list) {
255 assert(node->is_follower == 1);
256 assert(node->flagged == 0);
257 assert(node->part == T);
259 } /* check_partition */
262 * check that all leader nodes in the partition have the same opcode.
264 static void check_opcode(const partition_t *Z) {
269 list_for_each_entry(node_t, node, &Z->Leader, node_list) {
270 ir_node *irn = node->node;
273 key.code = get_irn_opcode(irn);
274 key.mode = get_irn_mode(irn);
275 key.arity = get_irn_arity(irn);
279 switch (get_irn_opcode(irn)) {
281 key.u.proj = get_Proj_proj(irn);
284 key.u.ent = get_Sel_entity(irn);
287 key.u.intVal = get_Conv_strict(irn);
290 key.u.intVal = get_Div_no_remainder(irn);
296 key.mode = get_Load_mode(irn);
299 key.u.intVal = get_Builtin_kind(irn);
306 assert((unsigned)key.code == get_irn_opcode(irn));
307 assert(key.mode == get_irn_mode(irn));
308 assert(key.arity == get_irn_arity(irn));
310 switch (get_irn_opcode(irn)) {
312 assert(key.u.proj == get_Proj_proj(irn));
315 assert(key.u.ent == get_Sel_entity(irn));
318 assert(key.u.intVal == get_Conv_strict(irn));
321 assert(key.u.intVal == get_Div_no_remainder(irn));
324 assert(key.u.block == irn);
327 assert(key.mode == get_Load_mode(irn));
330 assert(key.u.intVal == (int) get_Builtin_kind(irn));
339 static void check_all_partitions(environment_t *env) {
344 for (P = env->dbg_list; P != NULL; P = P->dbg_next) {
346 if (! P->type_is_T_or_C)
348 list_for_each_entry(node_t, node, &P->Follower, node_list) {
349 node_t *leader = identity(node);
351 assert(leader != node && leader->part == node->part);
362 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
366 #define NEXT(e) *((const node_t **)((char *)(e) + (ofs)))
367 for (e = list; e != NULL; e = NEXT(e)) {
368 assert(e->part == Z);
376 } /* ido_check_list */
379 * Check a local list.
381 static void check_list(const node_t *list, const partition_t *Z) {
382 do_check_list(list, offsetof(node_t, next), Z);
386 #define check_partition(T)
387 #define check_list(list, Z)
388 #define check_all_partitions(env)
389 #endif /* CHECK_PARTITIONS */
392 static inline lattice_elem_t get_partition_type(const partition_t *X);
395 * Dump partition to output.
397 static void dump_partition(const char *msg, const partition_t *part) {
400 lattice_elem_t type = get_partition_type(part);
402 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
403 msg, part->nr, part->type_is_T_or_C ? "*" : "",
404 part->n_leader, type));
405 list_for_each_entry(node_t, node, &part->Leader, node_list) {
406 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
409 if (! list_empty(&part->Follower)) {
410 DB((dbg, LEVEL_2, "\n---\n "));
412 list_for_each_entry(node_t, node, &part->Follower, node_list) {
413 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
417 DB((dbg, LEVEL_2, "\n}\n"));
418 } /* dump_partition */
423 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
427 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
429 DB((dbg, LEVEL_3, "%s = {\n ", msg));
430 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
431 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
434 DB((dbg, LEVEL_3, "\n}\n"));
442 static void dump_race_list(const char *msg, const node_t *list) {
443 do_dump_list(msg, list, offsetof(node_t, race_next));
444 } /* dump_race_list */
447 * Dumps a local list.
449 static void dump_list(const char *msg, const node_t *list) {
450 do_dump_list(msg, list, offsetof(node_t, next));
454 * Dump all partitions.
456 static void dump_all_partitions(const environment_t *env) {
457 const partition_t *P;
459 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
460 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
461 dump_partition("", P);
462 } /* dump_all_partitions */
467 static void dump_split_list(const partition_t *list) {
468 const partition_t *p;
470 DB((dbg, LEVEL_2, "Split by %s produced = {\n", what_reason));
471 for (p = list; p != NULL; p = p->split_next)
472 DB((dbg, LEVEL_2, "part%u, ", p->nr));
473 DB((dbg, LEVEL_2, "\n}\n"));
474 } /* dump_split_list */
477 * Dump partition and type for a node.
479 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
480 ir_node *irn = local != NULL ? local : n;
481 node_t *node = get_irn_node(irn);
483 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
485 } /* dump_partition_hook */
488 #define dump_partition(msg, part)
489 #define dump_race_list(msg, list)
490 #define dump_list(msg, list)
491 #define dump_all_partitions(env)
492 #define dump_split_list(list)
495 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
497 * Verify that a type transition is monotone
499 static void verify_type(const lattice_elem_t old_type, node_t *node) {
500 if (old_type.tv == node->type.tv) {
504 if (old_type.tv == tarval_top) {
505 /* from Top down-to is always allowed */
508 if (node->type.tv == tarval_bottom || node->type.tv == tarval_reachable) {
512 panic("combo: wrong translation from %+F to %+F on node %+F", old_type, node->type, node->node);
516 #define verify_type(old_type, node)
520 * Compare two pointer values of a listmap.
522 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
523 const listmap_entry_t *e1 = elt;
524 const listmap_entry_t *e2 = key;
527 return e1->id != e2->id;
528 } /* listmap_cmp_ptr */
531 * Initializes a listmap.
533 * @param map the listmap
535 static void listmap_init(listmap_t *map) {
536 map->map = new_set(listmap_cmp_ptr, 16);
541 * Terminates a listmap.
543 * @param map the listmap
545 static void listmap_term(listmap_t *map) {
550 * Return the associated listmap entry for a given id.
552 * @param map the listmap
553 * @param id the id to search for
555 * @return the associated listmap entry for the given id
557 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
558 listmap_entry_t key, *entry;
563 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
565 if (entry->list == NULL) {
566 /* a new entry, put into the list */
567 entry->next = map->values;
574 * Calculate the hash value for an opcode map entry.
576 * @param entry an opcode map entry
578 * @return a hash value for the given opcode map entry
580 static unsigned opcode_hash(const opcode_key_t *entry) {
581 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ptr) + entry->arity;
585 * Compare two entries in the opcode map.
587 static int cmp_opcode(const void *elt, const void *key, size_t size) {
588 const opcode_key_t *o1 = elt;
589 const opcode_key_t *o2 = key;
592 return o1->code != o2->code || o1->mode != o2->mode ||
593 o1->arity != o2->arity ||
594 o1->u.proj != o2->u.proj ||
595 o1->u.intVal != o2->u.intVal || /* this already checks uIntVal */
596 o1->u.ptr != o2->u.ptr;
600 * Compare two Def-Use edges for input position.
602 static int cmp_def_use_edge(const void *a, const void *b) {
603 const ir_def_use_edge *ea = a;
604 const ir_def_use_edge *eb = b;
606 /* no overrun, because range is [-1, MAXINT] */
607 return ea->pos - eb->pos;
608 } /* cmp_def_use_edge */
611 * We need the Def-Use edges sorted.
613 static void sort_irn_outs(node_t *node) {
614 ir_node *irn = node->node;
615 int n_outs = get_irn_n_outs(irn);
618 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
620 node->max_user_input = irn->out[n_outs].pos;
621 } /* sort_irn_outs */
624 * Return the type of a node.
626 * @param irn an IR-node
628 * @return the associated type of this node
630 static inline lattice_elem_t get_node_type(const ir_node *irn) {
631 return get_irn_node(irn)->type;
632 } /* get_node_type */
635 * Return the tarval of a node.
637 * @param irn an IR-node
639 * @return the associated type of this node
641 static inline tarval *get_node_tarval(const ir_node *irn) {
642 lattice_elem_t type = get_node_type(irn);
644 if (is_tarval(type.tv))
646 return tarval_bottom;
647 } /* get_node_type */
650 * Add a partition to the worklist.
652 static inline void add_to_worklist(partition_t *X, environment_t *env) {
653 assert(X->on_worklist == 0);
654 DB((dbg, LEVEL_2, "Adding part%d to worklist\n", X->nr));
655 X->wl_next = env->worklist;
658 } /* add_to_worklist */
661 * Create a new empty partition.
663 * @param env the environment
665 * @return a newly allocated partition
667 static inline partition_t *new_partition(environment_t *env) {
668 partition_t *part = OALLOC(&env->obst, partition_t);
670 INIT_LIST_HEAD(&part->Leader);
671 INIT_LIST_HEAD(&part->Follower);
672 INIT_LIST_HEAD(&part->cprop);
673 INIT_LIST_HEAD(&part->cprop_X);
674 part->wl_next = NULL;
675 part->touched_next = NULL;
676 part->cprop_next = NULL;
677 part->split_next = NULL;
678 part->touched = NULL;
681 part->max_user_inputs = 0;
682 part->on_worklist = 0;
683 part->on_touched = 0;
685 part->type_is_T_or_C = 0;
687 part->dbg_next = env->dbg_list;
688 env->dbg_list = part;
689 part->nr = part_nr++;
693 } /* new_partition */
696 * Get the first node from a partition.
698 static inline node_t *get_first_node(const partition_t *X) {
699 return list_entry(X->Leader.next, node_t, node_list);
700 } /* get_first_node */
703 * Return the type of a partition (assuming partition is non-empty and
704 * all elements have the same type).
706 * @param X a partition
708 * @return the type of the first element of the partition
710 static inline lattice_elem_t get_partition_type(const partition_t *X) {
711 const node_t *first = get_first_node(X);
713 } /* get_partition_type */
716 * Creates a partition node for the given IR-node and place it
717 * into the given partition.
719 * @param irn an IR-node
720 * @param part a partition to place the node in
721 * @param env the environment
723 * @return the created node
725 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
726 /* create a partition node and place it in the partition */
727 node_t *node = OALLOC(&env->obst, node_t);
729 INIT_LIST_HEAD(&node->node_list);
730 INIT_LIST_HEAD(&node->cprop_list);
734 node->race_next = NULL;
735 node->type.tv = tarval_top;
736 node->max_user_input = 0;
738 node->n_followers = 0;
739 node->on_touched = 0;
742 node->is_follower = 0;
744 set_irn_node(irn, node);
746 list_add_tail(&node->node_list, &part->Leader);
750 } /* create_partition_node */
753 * Pre-Walker, initialize all Nodes' type to U or top and place
754 * all nodes into the TOP partition.
756 static void create_initial_partitions(ir_node *irn, void *ctx) {
757 environment_t *env = ctx;
758 partition_t *part = env->initial;
761 node = create_partition_node(irn, part, env);
763 if (node->max_user_input > part->max_user_inputs)
764 part->max_user_inputs = node->max_user_input;
767 set_Block_phis(irn, NULL);
769 } /* create_initial_partitions */
772 * Post-Walker, collect all Block-Phi lists, set Cond.
774 static void init_block_phis(ir_node *irn, void *ctx) {
778 add_Block_phi(get_nodes_block(irn), irn);
780 } /* init_block_phis */
783 * Add a node to the entry.partition.touched set and
784 * node->partition to the touched set if not already there.
787 * @param env the environment
789 static inline void add_to_touched(node_t *y, environment_t *env) {
790 if (y->on_touched == 0) {
791 partition_t *part = y->part;
793 y->next = part->touched;
798 if (part->on_touched == 0) {
799 part->touched_next = env->touched;
801 part->on_touched = 1;
804 check_list(part->touched, part);
806 } /* add_to_touched */
809 * Place a node on the cprop list.
812 * @param env the environment
814 static void add_to_cprop(node_t *y, environment_t *env) {
817 /* Add y to y.partition.cprop. */
818 if (y->on_cprop == 0) {
819 partition_t *Y = y->part;
820 ir_node *irn = y->node;
822 /* place Conds and all its Projs on the cprop_X list */
823 if (is_Cond(skip_Proj(irn)))
824 list_add_tail(&y->cprop_list, &Y->cprop_X);
826 list_add_tail(&y->cprop_list, &Y->cprop);
829 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
831 /* place its partition on the cprop list */
832 if (Y->on_cprop == 0) {
833 Y->cprop_next = env->cprop;
839 if (get_irn_mode(irn) == mode_T) {
840 /* mode_T nodes always produce tarval_bottom, so we must explicitly
841 add it's Proj's to get constant evaluation to work */
844 for (i = get_irn_n_outs(irn) - 1; i >= 0; --i) {
845 node_t *proj = get_irn_node(get_irn_out(irn, i));
847 add_to_cprop(proj, env);
849 } else if (is_Block(irn)) {
850 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
851 * if someone placed the block. The Block is only placed if the reachability
852 * changes, and this must be re-evaluated in compute_Phi(). */
854 for (phi = get_Block_phis(irn); phi != NULL; phi = get_Phi_next(phi)) {
855 node_t *p = get_irn_node(phi);
856 add_to_cprop(p, env);
862 * Update the worklist: If Z is on worklist then add Z' to worklist.
863 * Else add the smaller of Z and Z' to worklist.
865 * @param Z the Z partition
866 * @param Z_prime the Z' partition, a previous part of Z
867 * @param env the environment
869 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
870 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
871 add_to_worklist(Z_prime, env);
873 add_to_worklist(Z, env);
875 } /* update_worklist */
878 * Make all inputs to x no longer be F.def_use edges.
882 static void move_edges_to_leader(node_t *x) {
883 ir_node *irn = x->node;
886 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
887 node_t *pred = get_irn_node(get_irn_n(irn, i));
892 n = get_irn_n_outs(p);
893 for (j = 1; j <= pred->n_followers; ++j) {
894 if (p->out[j].pos == i && p->out[j].use == irn) {
895 /* found a follower edge to x, move it to the Leader */
896 ir_def_use_edge edge = p->out[j];
898 /* remove this edge from the Follower set */
899 p->out[j] = p->out[pred->n_followers];
902 /* sort it into the leader set */
903 for (k = pred->n_followers + 2; k <= n; ++k) {
904 if (p->out[k].pos >= edge.pos)
906 p->out[k - 1] = p->out[k];
908 /* place the new edge here */
909 p->out[k - 1] = edge;
911 /* edge found and moved */
916 } /* move_edges_to_leader */
919 * Split a partition that has NO followers by a local list.
921 * @param Z partition to split
922 * @param g a (non-empty) node list
923 * @param env the environment
925 * @return a new partition containing the nodes of g
927 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
928 partition_t *Z_prime;
933 dump_partition("Splitting ", Z);
934 dump_list("by list ", g);
938 /* Remove g from Z. */
939 for (node = g; node != NULL; node = node->next) {
940 assert(node->part == Z);
941 list_del(&node->node_list);
944 assert(n < Z->n_leader);
947 /* Move g to a new partition, Z'. */
948 Z_prime = new_partition(env);
950 for (node = g; node != NULL; node = node->next) {
951 list_add_tail(&node->node_list, &Z_prime->Leader);
952 node->part = Z_prime;
953 if (node->max_user_input > max_input)
954 max_input = node->max_user_input;
956 Z_prime->max_user_inputs = max_input;
957 Z_prime->n_leader = n;
960 check_partition(Z_prime);
962 /* for now, copy the type info tag, it will be adjusted in split_by(). */
963 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
965 update_worklist(Z, Z_prime, env);
967 dump_partition("Now ", Z);
968 dump_partition("Created new ", Z_prime);
970 } /* split_no_followers */
973 * Make the Follower -> Leader transition for a node.
977 static void follower_to_leader(node_t *n) {
978 assert(n->is_follower == 1);
980 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", n->node));
982 move_edges_to_leader(n);
983 list_del(&n->node_list);
984 list_add_tail(&n->node_list, &n->part->Leader);
986 } /* follower_to_leader */
989 * The environment for one race step.
991 typedef struct step_env {
992 node_t *initial; /**< The initial node list. */
993 node_t *unwalked; /**< The unwalked node list. */
994 node_t *walked; /**< The walked node list. */
995 int index; /**< Next index of Follower use_def edge. */
996 unsigned side; /**< side number. */
1000 * Return non-zero, if a input is a real follower
1002 * @param irn the node to check
1003 * @param input number of the input
1005 static int is_real_follower(const ir_node *irn, int input) {
1008 switch (get_irn_opcode(irn)) {
1011 /* ignore the Confirm bound input */
1017 /* ignore the Mux sel input */
1022 /* dead inputs are not follower edges */
1023 ir_node *block = get_nodes_block(irn);
1024 node_t *pred = get_irn_node(get_Block_cfgpred(block, input));
1026 if (pred->type.tv == tarval_unreachable)
1036 /* only a Sub x,0 / Shift x,0 might be a follower */
1043 pred = get_irn_node(get_irn_n(irn, input));
1044 if (is_tarval(pred->type.tv) && tarval_is_null(pred->type.tv))
1048 pred = get_irn_node(get_irn_n(irn, input));
1049 if (is_tarval(pred->type.tv) && tarval_is_one(pred->type.tv))
1053 pred = get_irn_node(get_irn_n(irn, input));
1054 if (is_tarval(pred->type.tv) && tarval_is_all_one(pred->type.tv))
1058 assert(!"opcode not implemented yet");
1062 } /* is_real_follower */
1065 * Do one step in the race.
1067 static int step(step_env *env) {
1070 if (env->initial != NULL) {
1071 /* Move node from initial to unwalked */
1073 env->initial = n->race_next;
1075 n->race_next = env->unwalked;
1081 while (env->unwalked != NULL) {
1082 /* let n be the first node in unwalked */
1084 while (env->index < n->n_followers) {
1085 const ir_def_use_edge *edge = &n->node->out[1 + env->index];
1087 /* let m be n.F.def_use[index] */
1088 node_t *m = get_irn_node(edge->use);
1090 assert(m->is_follower);
1092 * Some inputs, like the get_Confirm_bound are NOT
1093 * real followers, sort them out.
1095 if (! is_real_follower(m->node, edge->pos)) {
1101 /* only followers from our partition */
1102 if (m->part != n->part)
1105 if ((m->flagged & env->side) == 0) {
1106 m->flagged |= env->side;
1108 if (m->flagged != 3) {
1109 /* visited the first time */
1110 /* add m to unwalked not as first node (we might still need to
1111 check for more follower node */
1112 m->race_next = n->race_next;
1116 /* else already visited by the other side and on the other list */
1119 /* move n to walked */
1120 env->unwalked = n->race_next;
1121 n->race_next = env->walked;
1129 * Clear the flags from a list and check for
1130 * nodes that where touched from both sides.
1132 * @param list the list
1134 static int clear_flags(node_t *list) {
1138 for (n = list; n != NULL; n = n->race_next) {
1139 if (n->flagged == 3) {
1140 /* we reach a follower from both sides, this will split congruent
1141 * inputs and make it a leader. */
1142 follower_to_leader(n);
1151 * Split a partition by a local list using the race.
1153 * @param pX pointer to the partition to split, might be changed!
1154 * @param gg a (non-empty) node list
1155 * @param env the environment
1157 * @return a new partition containing the nodes of gg
1159 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
1160 partition_t *X = *pX;
1161 partition_t *X_prime;
1164 node_t *g, *h, *node, *t;
1165 int max_input, transitions, winner, shf;
1167 DEBUG_ONLY(static int run = 0;)
1169 DB((dbg, LEVEL_2, "Run %d ", run++));
1170 if (list_empty(&X->Follower)) {
1171 /* if the partition has NO follower, we can use the fast
1172 splitting algorithm. */
1173 return split_no_followers(X, gg, env);
1175 /* else do the race */
1177 dump_partition("Splitting ", X);
1178 dump_list("by list ", gg);
1180 INIT_LIST_HEAD(&tmp);
1182 /* Remove gg from X.Leader and put into g */
1184 for (node = gg; node != NULL; node = node->next) {
1185 assert(node->part == X);
1186 assert(node->is_follower == 0);
1188 list_del(&node->node_list);
1189 list_add_tail(&node->node_list, &tmp);
1190 node->race_next = g;
1195 list_for_each_entry(node_t, node, &X->Leader, node_list) {
1196 node->race_next = h;
1199 /* restore X.Leader */
1200 list_splice(&tmp, &X->Leader);
1202 senv[0].initial = g;
1203 senv[0].unwalked = NULL;
1204 senv[0].walked = NULL;
1208 senv[1].initial = h;
1209 senv[1].unwalked = NULL;
1210 senv[1].walked = NULL;
1215 * Some informations on the race that are not stated clearly in Click's
1217 * 1) A follower stays on the side that reach him first.
1218 * 2) If the other side reches a follower, if will be converted to
1219 * a leader. /This must be done after the race is over, else the
1220 * edges we are iterating on are renumbered./
1221 * 3) /New leader might end up on both sides./
1222 * 4) /If one side ends up with new Leaders, we must ensure that
1223 * they can split out by opcode, hence we have to put _every_
1224 * partition with new Leader nodes on the cprop list, as
1225 * opcode splitting is done by split_by() at the end of
1226 * constant propagation./
1229 if (step(&senv[0])) {
1233 if (step(&senv[1])) {
1238 assert(senv[winner].initial == NULL);
1239 assert(senv[winner].unwalked == NULL);
1241 /* clear flags from walked/unwalked */
1243 transitions = clear_flags(senv[0].unwalked) << shf;
1244 transitions |= clear_flags(senv[0].walked) << shf;
1246 transitions |= clear_flags(senv[1].unwalked) << shf;
1247 transitions |= clear_flags(senv[1].walked) << shf;
1249 dump_race_list("winner ", senv[winner].walked);
1251 /* Move walked_{winner} to a new partition, X'. */
1252 X_prime = new_partition(env);
1255 for (node = senv[winner].walked; node != NULL; node = node->race_next) {
1256 list_del(&node->node_list);
1257 node->part = X_prime;
1258 if (node->is_follower) {
1259 list_add_tail(&node->node_list, &X_prime->Follower);
1261 list_add_tail(&node->node_list, &X_prime->Leader);
1264 if (node->max_user_input > max_input)
1265 max_input = node->max_user_input;
1267 X_prime->n_leader = n;
1268 X_prime->max_user_inputs = max_input;
1269 X->n_leader -= X_prime->n_leader;
1271 /* for now, copy the type info tag, it will be adjusted in split_by(). */
1272 X_prime->type_is_T_or_C = X->type_is_T_or_C;
1275 * Even if a follower was not checked by both sides, it might have
1276 * loose its congruence, so we need to check this case for all follower.
1278 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
1279 if (identity(node) == node) {
1280 follower_to_leader(node);
1286 check_partition(X_prime);
1288 /* X' is the smaller part */
1289 add_to_worklist(X_prime, env);
1292 * If there where follower to leader transitions, ensure that the nodes
1293 * can be split out if necessary.
1295 if (transitions & 1) {
1296 /* place winner partition on the cprop list */
1297 if (X_prime->on_cprop == 0) {
1298 X_prime->cprop_next = env->cprop;
1299 env->cprop = X_prime;
1300 X_prime->on_cprop = 1;
1303 if (transitions & 2) {
1304 /* place other partition on the cprop list */
1305 if (X->on_cprop == 0) {
1306 X->cprop_next = env->cprop;
1312 dump_partition("Now ", X);
1313 dump_partition("Created new ", X_prime);
1315 /* we have to ensure that the partition containing g is returned */
1325 * Returns non-zero if the i'th input of a Phi node is live.
1327 * @param phi a Phi-node
1328 * @param i an input number
1330 * @return non-zero if the i'th input of the given Phi node is live
1332 static int is_live_input(ir_node *phi, int i) {
1334 ir_node *block = get_nodes_block(phi);
1335 ir_node *pred = get_Block_cfgpred(block, i);
1336 lattice_elem_t type = get_node_type(pred);
1338 return type.tv != tarval_unreachable;
1340 /* else it's the control input, always live */
1342 } /* is_live_input */
1345 * Return non-zero if a type is a constant.
1347 static int is_constant_type(lattice_elem_t type) {
1348 if (type.tv != tarval_bottom && type.tv != tarval_top)
1351 } /* is_constant_type */
1354 * Check whether a type is neither Top or a constant.
1355 * Note: U is handled like Top here, R is a constant.
1357 * @param type the type to check
1359 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1360 if (is_tarval(type.tv)) {
1361 if (type.tv == tarval_top)
1363 if (tarval_is_constant(type.tv))
1370 } /* type_is_neither_top_nor_const */
1373 * Collect nodes to the touched list.
1375 * @param list the list which contains the nodes that must be evaluated
1376 * @param idx the index of the def_use edge to evaluate
1377 * @param env the environment
1379 static void collect_touched(list_head *list, int idx, environment_t *env) {
1381 int end_idx = env->end_idx;
1383 list_for_each_entry(node_t, x, list, node_list) {
1387 /* leader edges start AFTER follower edges */
1388 x->next_edge = x->n_followers + 1;
1390 num_edges = get_irn_n_outs(x->node);
1392 /* for all edges in x.L.def_use_{idx} */
1393 while (x->next_edge <= num_edges) {
1394 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1397 /* check if we have necessary edges */
1398 if (edge->pos > idx)
1405 /* only non-commutative nodes */
1406 if (env->commutative &&
1407 (idx == 0 || idx == 1) && is_op_commutative(get_irn_op(succ)))
1410 /* ignore the "control input" for non-pinned nodes
1411 if we are running in GCSE mode */
1412 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1415 y = get_irn_node(succ);
1416 assert(get_irn_n(succ, idx) == x->node);
1418 /* ignore block edges touching followers */
1419 if (idx == -1 && y->is_follower)
1422 if (is_constant_type(y->type)) {
1423 ir_opcode code = get_irn_opcode(succ);
1424 if (code == iro_Sub || code == iro_Cmp)
1425 add_to_cprop(y, env);
1428 /* Partitions of constants should not be split simply because their Nodes have unequal
1429 functions or incongruent inputs. */
1430 if (type_is_neither_top_nor_const(y->type) &&
1431 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1432 add_to_touched(y, env);
1436 } /* collect_touched */
1439 * Collect commutative nodes to the touched list.
1441 * @param list the list which contains the nodes that must be evaluated
1442 * @param env the environment
1444 static void collect_commutative_touched(list_head *list, environment_t *env) {
1447 list_for_each_entry(node_t, x, list, node_list) {
1450 num_edges = get_irn_n_outs(x->node);
1452 x->next_edge = x->n_followers + 1;
1454 /* for all edges in x.L.def_use_{idx} */
1455 while (x->next_edge <= num_edges) {
1456 const ir_def_use_edge *edge = &x->node->out[x->next_edge];
1459 /* check if we have necessary edges */
1469 /* only commutative nodes */
1470 if (!is_op_commutative(get_irn_op(succ)))
1473 y = get_irn_node(succ);
1474 if (is_constant_type(y->type)) {
1475 ir_opcode code = get_irn_opcode(succ);
1476 if (code == iro_Eor)
1477 add_to_cprop(y, env);
1480 /* Partitions of constants should not be split simply because their Nodes have unequal
1481 functions or incongruent inputs. */
1482 if (type_is_neither_top_nor_const(y->type)) {
1483 add_to_touched(y, env);
1487 } /* collect_commutative_touched */
1490 * Split the partitions if caused by the first entry on the worklist.
1492 * @param env the environment
1494 static void cause_splits(environment_t *env) {
1495 partition_t *X, *Z, *N;
1498 /* remove the first partition from the worklist */
1500 env->worklist = X->wl_next;
1503 dump_partition("Cause_split: ", X);
1505 if (env->commutative) {
1506 /* handle commutative nodes first */
1508 /* empty the touched set: already done, just clear the list */
1509 env->touched = NULL;
1511 collect_commutative_touched(&X->Leader, env);
1512 collect_commutative_touched(&X->Follower, env);
1514 for (Z = env->touched; Z != NULL; Z = N) {
1516 node_t *touched = Z->touched;
1517 node_t *touched_aa = NULL;
1518 node_t *touched_ab = NULL;
1519 unsigned n_touched_aa = 0;
1520 unsigned n_touched_ab = 0;
1522 assert(Z->touched != NULL);
1524 /* beware, split might change Z */
1525 N = Z->touched_next;
1527 /* remove it from the touched set */
1530 /* Empty local Z.touched. */
1531 for (e = touched; e != NULL; e = n) {
1532 node_t *left = get_irn_node(get_irn_n(e->node, 0));
1533 node_t *right = get_irn_node(get_irn_n(e->node, 1));
1535 assert(e->is_follower == 0);
1540 * Note: op(a, a) is NOT congruent to op(a, b).
1541 * So, we must split the touched list.
1543 if (left->part == right->part) {
1544 e->next = touched_aa;
1548 e->next = touched_ab;
1553 assert(n_touched_aa + n_touched_ab == Z->n_touched);
1557 if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
1558 partition_t *Z_prime = Z;
1559 DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
1560 split(&Z_prime, touched_aa, env);
1562 assert(n_touched_aa <= Z->n_leader);
1564 if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
1565 partition_t *Z_prime = Z;
1566 DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
1567 split(&Z_prime, touched_ab, env);
1569 assert(n_touched_ab <= Z->n_leader);
1573 /* combine temporary leader and follower list */
1574 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1575 /* empty the touched set: already done, just clear the list */
1576 env->touched = NULL;
1578 collect_touched(&X->Leader, idx, env);
1579 collect_touched(&X->Follower, idx, env);
1581 for (Z = env->touched; Z != NULL; Z = N) {
1583 node_t *touched = Z->touched;
1584 unsigned n_touched = Z->n_touched;
1586 assert(Z->touched != NULL);
1588 /* beware, split might change Z */
1589 N = Z->touched_next;
1591 /* remove it from the touched set */
1594 /* Empty local Z.touched. */
1595 for (e = touched; e != NULL; e = e->next) {
1596 assert(e->is_follower == 0);
1602 if (0 < n_touched && n_touched < Z->n_leader) {
1603 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1604 split(&Z, touched, env);
1606 assert(n_touched <= Z->n_leader);
1609 } /* cause_splits */
1612 * Implements split_by_what(): Split a partition by characteristics given
1613 * by the what function.
1615 * @param X the partition to split
1616 * @param What a function returning an Id for every node of the partition X
1617 * @param P a list to store the result partitions
1618 * @param env the environment
1622 static partition_t *split_by_what(partition_t *X, what_func What,
1623 partition_t **P, environment_t *env) {
1626 listmap_entry_t *iter;
1629 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1631 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1632 void *id = What(x, env);
1633 listmap_entry_t *entry;
1636 /* input not allowed, ignore */
1639 /* Add x to map[What(x)]. */
1640 entry = listmap_find(&map, id);
1641 x->next = entry->list;
1644 /* Let P be a set of Partitions. */
1646 /* for all sets S except one in the range of map do */
1647 for (iter = map.values; iter != NULL; iter = iter->next) {
1648 if (iter->next == NULL) {
1649 /* this is the last entry, ignore */
1654 /* Add SPLIT( X, S ) to P. */
1655 DB((dbg, LEVEL_2, "Split part%d by WHAT = %s\n", X->nr, what_reason));
1656 R = split(&X, S, env);
1666 } /* split_by_what */
1668 /** lambda n.(n.type) */
1669 static void *lambda_type(const node_t *node, environment_t *env) {
1671 return node->type.tv;
1674 /** lambda n.(n.opcode) */
1675 static void *lambda_opcode(const node_t *node, environment_t *env) {
1676 opcode_key_t key, *entry;
1677 ir_node *irn = node->node;
1679 key.code = get_irn_opcode(irn);
1680 key.mode = get_irn_mode(irn);
1681 key.arity = get_irn_arity(irn);
1685 switch (get_irn_opcode(irn)) {
1687 key.u.proj = get_Proj_proj(irn);
1690 key.u.ent = get_Sel_entity(irn);
1693 key.u.intVal = get_Conv_strict(irn);
1696 key.u.intVal = get_Div_no_remainder(irn);
1700 * Some ugliness here: Two Blocks having the same
1701 * IJmp predecessor would be congruent, which of course is wrong.
1702 * We fix it by never letting blocks be congruent
1703 * which cannot be detected by combo either.
1708 key.mode = get_Load_mode(irn);
1711 key.u.intVal = get_Builtin_kind(irn);
1717 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1719 } /* lambda_opcode */
1721 /** lambda n.(n[i].partition) */
1722 static void *lambda_partition(const node_t *node, environment_t *env) {
1723 ir_node *skipped = skip_Proj(node->node);
1726 int i = env->lambda_input;
1728 if (i >= get_irn_arity(node->node)) {
1730 * We are outside the allowed range: This can happen even
1731 * if we have split by opcode first: doing so might move Followers
1732 * to Leaders and those will have a different opcode!
1733 * Note that in this case the partition is on the cprop list and will be
1739 /* ignore the "control input" for non-pinned nodes
1740 if we are running in GCSE mode */
1741 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1744 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1745 p = get_irn_node(pred);
1747 } /* lambda_partition */
1749 /** lambda n.(n[i].partition) for commutative nodes */
1750 static void *lambda_commutative_partition(const node_t *node, environment_t *env) {
1751 ir_node *irn = node->node;
1752 ir_node *skipped = skip_Proj(irn);
1753 ir_node *pred, *left, *right;
1755 partition_t *pl, *pr;
1756 int i = env->lambda_input;
1758 if (i >= get_irn_arity(node->node)) {
1760 * We are outside the allowed range: This can happen even
1761 * if we have split by opcode first: doing so might move Followers
1762 * to Leaders and those will have a different opcode!
1763 * Note that in this case the partition is on the cprop list and will be
1769 /* ignore the "control input" for non-pinned nodes
1770 if we are running in GCSE mode */
1771 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1775 pred = get_irn_n(skipped, i);
1776 p = get_irn_node(pred);
1780 if (is_op_commutative(get_irn_op(irn))) {
1781 /* normalize partition order by returning the "smaller" on input 0,
1782 the "bigger" on input 1. */
1783 left = get_binop_left(irn);
1784 pl = get_irn_node(left)->part;
1785 right = get_binop_right(irn);
1786 pr = get_irn_node(right)->part;
1789 return pl < pr ? pl : pr;
1791 return pl > pr ? pl : pr;
1793 /* a not split out Follower */
1794 pred = get_irn_n(irn, i);
1795 p = get_irn_node(pred);
1799 } /* lambda_commutative_partition */
1802 * Returns true if a type is a constant (and NOT Top
1805 static int is_con(const lattice_elem_t type) {
1806 /* be conservative */
1807 if (is_tarval(type.tv))
1808 return tarval_is_constant(type.tv);
1809 return is_entity(type.sym.entity_p);
1813 * Implements split_by().
1815 * @param X the partition to split
1816 * @param env the environment
1818 static void split_by(partition_t *X, environment_t *env) {
1819 partition_t *I, *P = NULL;
1822 dump_partition("split_by", X);
1824 if (X->n_leader == 1) {
1825 /* we have only one leader, no need to split, just check it's type */
1826 node_t *x = get_first_node(X);
1827 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1831 DEBUG_ONLY(what_reason = "lambda n.(n.type)";)
1832 P = split_by_what(X, lambda_type, &P, env);
1835 /* adjust the type tags, we have split partitions by type */
1836 for (I = P; I != NULL; I = I->split_next) {
1837 node_t *x = get_first_node(I);
1838 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1845 if (Y->n_leader > 1) {
1846 /* we do not want split the TOP or constant partitions */
1847 if (! Y->type_is_T_or_C) {
1848 partition_t *Q = NULL;
1850 DEBUG_ONLY(what_reason = "lambda n.(n.opcode)";)
1851 Q = split_by_what(Y, lambda_opcode, &Q, env);
1858 if (Z->n_leader > 1) {
1859 const node_t *first = get_first_node(Z);
1860 int arity = get_irn_arity(first->node);
1862 what_func what = lambda_partition;
1863 DEBUG_ONLY(char buf[64];)
1865 if (env->commutative && is_op_commutative(get_irn_op(first->node)))
1866 what = lambda_commutative_partition;
1869 * BEWARE: during splitting by input 2 for instance we might
1870 * create new partitions which are different by input 1, so collect
1871 * them and split further.
1873 Z->split_next = NULL;
1876 for (input = arity - 1; input >= -1; --input) {
1878 partition_t *Z_prime = R;
1881 if (Z_prime->n_leader > 1) {
1882 env->lambda_input = input;
1883 DEBUG_ONLY(snprintf(buf, sizeof(buf), "lambda n.(n[%d].partition)", input);)
1884 DEBUG_ONLY(what_reason = buf;)
1885 S = split_by_what(Z_prime, what, &S, env);
1888 Z_prime->split_next = S;
1891 } while (R != NULL);
1896 } while (Q != NULL);
1899 } while (P != NULL);
1903 * (Re-)compute the type for a given node.
1905 * @param node the node
1907 static void default_compute(node_t *node) {
1909 ir_node *irn = node->node;
1911 /* if any of the data inputs have type top, the result is type top */
1912 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1913 ir_node *pred = get_irn_n(irn, i);
1914 node_t *p = get_irn_node(pred);
1916 if (p->type.tv == tarval_top) {
1917 node->type.tv = tarval_top;
1922 if (get_irn_mode(node->node) == mode_X)
1923 node->type.tv = tarval_reachable;
1925 node->type.tv = computed_value(irn);
1926 } /* default_compute */
1929 * (Re-)compute the type for a Block node.
1931 * @param node the node
1933 static void compute_Block(node_t *node) {
1935 ir_node *block = node->node;
1937 if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
1938 /* start block and labelled blocks are always reachable */
1939 node->type.tv = tarval_reachable;
1943 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1944 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1946 if (pred->type.tv == tarval_reachable) {
1947 /* A block is reachable, if at least of predecessor is reachable. */
1948 node->type.tv = tarval_reachable;
1952 node->type.tv = tarval_top;
1953 } /* compute_Block */
1956 * (Re-)compute the type for a Bad node.
1958 * @param node the node
1960 static void compute_Bad(node_t *node) {
1961 /* Bad nodes ALWAYS compute Top */
1962 node->type.tv = tarval_top;
1966 * (Re-)compute the type for an Unknown node.
1968 * @param node the node
1970 static void compute_Unknown(node_t *node) {
1971 /* While Unknown nodes should compute Top this is dangerous:
1972 * a Top input to a Cond would lead to BOTH control flows unreachable.
1973 * While this is correct in the given semantics, it would destroy the Firm
1976 * It would be safe to compute Top IF it can be assured, that only Cmp
1977 * nodes are inputs to Conds. We check that first.
1978 * This is the way Frontends typically build Firm, but some optimizations
1979 * (jump threading for instance) might replace them by Phib's...
1981 node->type.tv = tarval_UNKNOWN;
1982 } /* compute_Unknown */
1985 * (Re-)compute the type for a Jmp node.
1987 * @param node the node
1989 static void compute_Jmp(node_t *node) {
1990 node_t *block = get_irn_node(get_nodes_block(node->node));
1992 node->type = block->type;
1996 * (Re-)compute the type for the Return node.
1998 * @param node the node
2000 static void compute_Return(node_t *node) {
2001 /* The Return node is NOT dead if it is in a reachable block.
2002 * This is already checked in compute(). so we can return
2003 * Reachable here. */
2004 node->type.tv = tarval_reachable;
2005 } /* compute_Return */
2008 * (Re-)compute the type for the End node.
2010 * @param node the node
2012 static void compute_End(node_t *node) {
2013 /* the End node is NOT dead of course */
2014 node->type.tv = tarval_reachable;
2018 * (Re-)compute the type for a Call.
2020 * @param node the node
2022 static void compute_Call(node_t *node) {
2024 * A Call computes always bottom, even if it has Unknown
2027 node->type.tv = tarval_bottom;
2028 } /* compute_Call */
2031 * (Re-)compute the type for a SymConst node.
2033 * @param node the node
2035 static void compute_SymConst(node_t *node) {
2036 ir_node *irn = node->node;
2037 node_t *block = get_irn_node(get_nodes_block(irn));
2039 if (block->type.tv == tarval_unreachable) {
2040 node->type.tv = tarval_top;
2043 switch (get_SymConst_kind(irn)) {
2044 case symconst_addr_ent:
2045 /* case symconst_addr_name: cannot handle this yet */
2046 node->type.sym = get_SymConst_symbol(irn);
2049 node->type.tv = computed_value(irn);
2051 } /* compute_SymConst */
2054 * (Re-)compute the type for a Phi node.
2056 * @param node the node
2058 static void compute_Phi(node_t *node) {
2060 ir_node *phi = node->node;
2061 lattice_elem_t type;
2063 /* if a Phi is in a unreachable block, its type is TOP */
2064 node_t *block = get_irn_node(get_nodes_block(phi));
2066 if (block->type.tv == tarval_unreachable) {
2067 node->type.tv = tarval_top;
2071 /* Phi implements the Meet operation */
2072 type.tv = tarval_top;
2073 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2074 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2075 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
2077 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
2078 /* ignore TOP inputs: We must check here for unreachable blocks,
2079 because Firm constants live in the Start Block are NEVER Top.
2080 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
2081 comes from a unreachable input. */
2084 if (pred->type.tv == tarval_bottom) {
2085 node->type.tv = tarval_bottom;
2087 } else if (type.tv == tarval_top) {
2088 /* first constant found */
2090 } else if (type.tv != pred->type.tv) {
2091 /* different constants or tarval_bottom */
2092 node->type.tv = tarval_bottom;
2095 /* else nothing, constants are the same */
2101 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
2103 * @param node the node
2105 static void compute_Add(node_t *node) {
2106 ir_node *sub = node->node;
2107 node_t *l = get_irn_node(get_Add_left(sub));
2108 node_t *r = get_irn_node(get_Add_right(sub));
2109 lattice_elem_t a = l->type;
2110 lattice_elem_t b = r->type;
2113 if (a.tv == tarval_top || b.tv == tarval_top) {
2114 node->type.tv = tarval_top;
2115 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
2116 node->type.tv = tarval_bottom;
2118 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
2119 must call tarval_add() first to handle this case! */
2120 if (is_tarval(a.tv)) {
2121 if (is_tarval(b.tv)) {
2122 node->type.tv = tarval_add(a.tv, b.tv);
2125 mode = get_tarval_mode(a.tv);
2126 if (a.tv == get_mode_null(mode)) {
2130 } else if (is_tarval(b.tv)) {
2131 mode = get_tarval_mode(b.tv);
2132 if (b.tv == get_mode_null(mode)) {
2137 node->type.tv = tarval_bottom;
2142 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
2144 * @param node the node
2146 static void compute_Sub(node_t *node) {
2147 ir_node *sub = node->node;
2148 node_t *l = get_irn_node(get_Sub_left(sub));
2149 node_t *r = get_irn_node(get_Sub_right(sub));
2150 lattice_elem_t a = l->type;
2151 lattice_elem_t b = r->type;
2154 if (a.tv == tarval_top || b.tv == tarval_top) {
2155 node->type.tv = tarval_top;
2156 } else if (is_con(a) && is_con(b)) {
2157 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2158 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
2159 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2161 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2164 node->type.tv = tarval_bottom;
2166 } else if (r->part == l->part &&
2167 (!mode_is_float(get_irn_mode(l->node)))) {
2169 * BEWARE: a - a is NOT always 0 for floating Point values, as
2170 * NaN op NaN = NaN, so we must check this here.
2172 ir_mode *mode = get_irn_mode(sub);
2173 tv = get_mode_null(mode);
2175 /* if the node was ONCE evaluated by all constants, but now
2176 this breaks AND we get from the argument partitions a different
2177 result, switch to bottom.
2178 This happens because initially all nodes are in the same partition ... */
2179 if (node->type.tv != tv)
2183 node->type.tv = tarval_bottom;
2188 * (Re-)compute the type for an Eor. Special case: both nodes are congruent.
2190 * @param node the node
2192 static void compute_Eor(node_t *node) {
2193 ir_node *eor = node->node;
2194 node_t *l = get_irn_node(get_Eor_left(eor));
2195 node_t *r = get_irn_node(get_Eor_right(eor));
2196 lattice_elem_t a = l->type;
2197 lattice_elem_t b = r->type;
2200 if (a.tv == tarval_top || b.tv == tarval_top) {
2201 node->type.tv = tarval_top;
2202 } else if (is_con(a) && is_con(b)) {
2203 if (is_tarval(a.tv) && is_tarval(b.tv)) {
2204 node->type.tv = tarval_eor(a.tv, b.tv);
2205 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
2207 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
2210 node->type.tv = tarval_bottom;
2212 } else if (r->part == l->part) {
2213 ir_mode *mode = get_irn_mode(eor);
2214 tv = get_mode_null(mode);
2216 /* if the node was ONCE evaluated by all constants, but now
2217 this breaks AND we get from the argument partitions a different
2218 result, switch to bottom.
2219 This happens because initially all nodes are in the same partition ... */
2220 if (node->type.tv != tv)
2224 node->type.tv = tarval_bottom;
2229 * (Re-)compute the type for Cmp.
2231 * @param node the node
2233 static void compute_Cmp(node_t *node) {
2234 ir_node *cmp = node->node;
2235 node_t *l = get_irn_node(get_Cmp_left(cmp));
2236 node_t *r = get_irn_node(get_Cmp_right(cmp));
2237 lattice_elem_t a = l->type;
2238 lattice_elem_t b = r->type;
2240 if (a.tv == tarval_top || b.tv == tarval_top) {
2241 node->type.tv = tarval_top;
2242 } else if (r->part == l->part) {
2243 /* both nodes congruent, we can probably do something */
2244 node->type.tv = tarval_b_true;
2245 } else if (is_con(a) && is_con(b)) {
2246 /* both nodes are constants, we can probably do something */
2247 node->type.tv = tarval_b_true;
2249 node->type.tv = tarval_bottom;
2254 * (Re-)compute the type for a Proj(Cmp).
2256 * @param node the node
2257 * @param cond the predecessor Cmp node
2259 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
2260 ir_node *proj = node->node;
2261 node_t *l = get_irn_node(get_Cmp_left(cmp));
2262 node_t *r = get_irn_node(get_Cmp_right(cmp));
2263 lattice_elem_t a = l->type;
2264 lattice_elem_t b = r->type;
2265 pn_Cmp pnc = get_Proj_proj(proj);
2268 if (a.tv == tarval_top || b.tv == tarval_top) {
2269 node->type.tv = tarval_undefined;
2270 } else if (is_con(a) && is_con(b)) {
2271 default_compute(node);
2272 } else if (r->part == l->part &&
2273 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
2275 * BEWARE: a == a is NOT always True for floating Point values, as
2276 * NaN != NaN is defined, so we must check this here.
2278 tv = pnc & pn_Cmp_Eq ? tarval_b_true: tarval_b_false;
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;
2290 } /* compute_Proj_Cmp */
2293 * (Re-)compute the type for a Proj(Cond).
2295 * @param node the node
2296 * @param cond the predecessor Cond node
2298 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
2299 ir_node *proj = node->node;
2300 long pnc = get_Proj_proj(proj);
2301 ir_node *sel = get_Cond_selector(cond);
2302 node_t *selector = get_irn_node(sel);
2305 * Note: it is crucial for the monotony that the Proj(Cond)
2306 * are evaluates after all predecessors of the Cond selector are
2312 * Due to the fact that 0 is a const, the Cmp gets immediately
2313 * on the cprop list. It will be evaluated before x is evaluated,
2314 * might leaving x as Top. When later x is evaluated, the Cmp
2315 * might change its value.
2316 * BUT if the Cond is evaluated before this happens, Proj(Cond, FALSE)
2317 * gets R, and later changed to F if Cmp is evaluated to True!
2319 * We prevent this by putting Conds in an extra cprop_X queue, which
2320 * gets evaluated after the cprop queue is empty.
2322 * Note that this even happens with Click's original algorithm, if
2323 * Cmp(x, 0) is evaluated to True first and later changed to False
2324 * if x was Top first and later changed to a Const ...
2325 * It is unclear how Click solved that problem ...
2327 * However, in rare cases even this does not help, if a Top reaches
2328 * a compare through a Phi, than Proj(Cond) is evaluated changing
2329 * the type of the Phi to something other.
2330 * So, we take the last resort and bind the type to R once
2333 * (This might be even the way Click works around the whole problem).
2335 * Finally, we may miss some optimization possibilities due to this:
2340 * If Top reaches the if first, than we decide for != here.
2341 * If y later is evaluated to 0, we cannot revert this decision
2342 * and must live with both outputs enabled. If this happens,
2343 * we get an unresolved if (true) in the code ...
2345 * In Click's version where this decision is done at the Cmp,
2346 * the Cmp is NOT optimized away than (if y evaluated to 1
2347 * for instance) and we get a if (1 == 0) here ...
2349 * Both solutions are suboptimal.
2350 * At least, we could easily detect this problem and run
2351 * cf_opt() (or even combo) again :-(
2353 if (node->type.tv == tarval_reachable)
2356 if (get_irn_mode(sel) == mode_b) {
2358 if (pnc == pn_Cond_true) {
2359 if (selector->type.tv == tarval_b_false) {
2360 node->type.tv = tarval_unreachable;
2361 } else if (selector->type.tv == tarval_b_true) {
2362 node->type.tv = tarval_reachable;
2363 } else if (selector->type.tv == tarval_bottom) {
2364 node->type.tv = tarval_reachable;
2366 assert(selector->type.tv == tarval_top);
2367 if (tarval_UNKNOWN == tarval_top) {
2368 /* any condition based on Top is "!=" */
2369 node->type.tv = tarval_unreachable;
2371 node->type.tv = tarval_unreachable;
2375 assert(pnc == pn_Cond_false);
2377 if (selector->type.tv == tarval_b_false) {
2378 node->type.tv = tarval_reachable;
2379 } else if (selector->type.tv == tarval_b_true) {
2380 node->type.tv = tarval_unreachable;
2381 } else if (selector->type.tv == tarval_bottom) {
2382 node->type.tv = tarval_reachable;
2384 assert(selector->type.tv == tarval_top);
2385 if (tarval_UNKNOWN == tarval_top) {
2386 /* any condition based on Top is "!=" */
2387 node->type.tv = tarval_reachable;
2389 node->type.tv = tarval_unreachable;
2395 if (selector->type.tv == tarval_bottom) {
2396 node->type.tv = tarval_reachable;
2397 } else if (selector->type.tv == tarval_top) {
2398 if (tarval_UNKNOWN == tarval_top &&
2399 pnc == get_Cond_default_proj(cond)) {
2400 /* a switch based of Top is always "default" */
2401 node->type.tv = tarval_reachable;
2403 node->type.tv = tarval_unreachable;
2406 long value = get_tarval_long(selector->type.tv);
2407 if (pnc == get_Cond_default_proj(cond)) {
2408 /* default switch, have to check ALL other cases */
2411 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
2412 ir_node *succ = get_irn_out(cond, i);
2416 if (value == get_Proj_proj(succ)) {
2417 /* we found a match, will NOT take the default case */
2418 node->type.tv = tarval_unreachable;
2422 /* all cases checked, no match, will take default case */
2423 node->type.tv = tarval_reachable;
2426 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
2430 } /* compute_Proj_Cond */
2433 * (Re-)compute the type for a Proj-Node.
2435 * @param node the node
2437 static void compute_Proj(node_t *node) {
2438 ir_node *proj = node->node;
2439 ir_mode *mode = get_irn_mode(proj);
2440 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
2441 ir_node *pred = get_Proj_pred(proj);
2443 if (block->type.tv == tarval_unreachable) {
2444 /* a Proj in a unreachable Block stay Top */
2445 node->type.tv = tarval_top;
2448 if (get_irn_node(pred)->type.tv == tarval_top && !is_Cond(pred)) {
2449 /* if the predecessor is Top, its Proj follow */
2450 node->type.tv = tarval_top;
2454 if (mode == mode_M) {
2455 /* mode M is always bottom */
2456 node->type.tv = tarval_bottom;
2459 if (mode != mode_X) {
2461 compute_Proj_Cmp(node, pred);
2463 default_compute(node);
2466 /* handle mode_X nodes */
2468 switch (get_irn_opcode(pred)) {
2470 /* the Proj_X from the Start is always reachable.
2471 However this is already handled at the top. */
2472 node->type.tv = tarval_reachable;
2475 compute_Proj_Cond(node, pred);
2478 default_compute(node);
2480 } /* compute_Proj */
2483 * (Re-)compute the type for a Confirm.
2485 * @param node the node
2487 static void compute_Confirm(node_t *node) {
2488 ir_node *confirm = node->node;
2489 node_t *pred = get_irn_node(get_Confirm_value(confirm));
2491 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
2492 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
2494 if (is_con(bound->type)) {
2495 /* is equal to a constant */
2496 node->type = bound->type;
2500 /* a Confirm is a copy OR a Const */
2501 node->type = pred->type;
2502 } /* compute_Confirm */
2505 * (Re-)compute the type for a given node.
2507 * @param node the node
2509 static void compute(node_t *node) {
2510 ir_node *irn = node->node;
2513 #ifndef VERIFY_MONOTONE
2515 * Once a node reaches bottom, the type cannot fall further
2516 * in the lattice and we can stop computation.
2517 * Do not take this exit if the monotony verifier is
2518 * enabled to catch errors.
2520 if (node->type.tv == tarval_bottom)
2524 if (is_no_Block(irn)) {
2525 /* for pinned nodes, check its control input */
2526 if (get_irn_pinned(skip_Proj(irn)) == op_pin_state_pinned) {
2527 node_t *block = get_irn_node(get_nodes_block(irn));
2529 if (block->type.tv == tarval_unreachable) {
2530 node->type.tv = tarval_top;
2536 func = (compute_func)node->node->op->ops.generic;
2542 * Identity functions: Note that one might thing that identity() is just a
2543 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
2544 * here, because it expects that the identity node is one of the inputs, which is NOT
2545 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
2546 * So, we have our own implementation, which copies some parts of equivalent_node()
2550 * Calculates the Identity for Phi nodes
2552 static node_t *identity_Phi(node_t *node) {
2553 ir_node *phi = node->node;
2554 ir_node *block = get_nodes_block(phi);
2555 node_t *n_part = NULL;
2558 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
2559 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2561 if (pred_X->type.tv == tarval_reachable) {
2562 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
2566 else if (n_part->part != pred->part) {
2567 /* incongruent inputs, not a follower */
2572 /* if n_part is NULL here, all inputs path are dead, the Phi computes
2573 * tarval_top, is in the TOP partition and should NOT being split! */
2574 assert(n_part != NULL);
2576 } /* identity_Phi */
2579 * Calculates the Identity for commutative 0 neutral nodes.
2581 static node_t *identity_comm_zero_binop(node_t *node) {
2582 ir_node *op = node->node;
2583 node_t *a = get_irn_node(get_binop_left(op));
2584 node_t *b = get_irn_node(get_binop_right(op));
2585 ir_mode *mode = get_irn_mode(op);
2588 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2589 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2592 /* node: no input should be tarval_top, else the binop would be also
2593 * Top and not being split. */
2594 zero = get_mode_null(mode);
2595 if (a->type.tv == zero)
2597 if (b->type.tv == zero)
2600 } /* identity_comm_zero_binop */
2603 * Calculates the Identity for Shift nodes.
2605 static node_t *identity_shift(node_t *node) {
2606 ir_node *op = node->node;
2607 node_t *b = get_irn_node(get_binop_right(op));
2608 ir_mode *mode = get_irn_mode(b->node);
2611 /* node: no input should be tarval_top, else the binop would be also
2612 * Top and not being split. */
2613 zero = get_mode_null(mode);
2614 if (b->type.tv == zero)
2615 return get_irn_node(get_binop_left(op));
2617 } /* identity_shift */
2620 * Calculates the Identity for Mul nodes.
2622 static node_t *identity_Mul(node_t *node) {
2623 ir_node *op = node->node;
2624 node_t *a = get_irn_node(get_Mul_left(op));
2625 node_t *b = get_irn_node(get_Mul_right(op));
2626 ir_mode *mode = get_irn_mode(op);
2629 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2630 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2633 /* node: no input should be tarval_top, else the binop would be also
2634 * Top and not being split. */
2635 one = get_mode_one(mode);
2636 if (a->type.tv == one)
2638 if (b->type.tv == one)
2641 } /* identity_Mul */
2644 * Calculates the Identity for Sub nodes.
2646 static node_t *identity_Sub(node_t *node) {
2647 ir_node *sub = node->node;
2648 node_t *b = get_irn_node(get_Sub_right(sub));
2649 ir_mode *mode = get_irn_mode(sub);
2651 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2652 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2655 /* node: no input should be tarval_top, else the binop would be also
2656 * Top and not being split. */
2657 if (b->type.tv == get_mode_null(mode))
2658 return get_irn_node(get_Sub_left(sub));
2660 } /* identity_Sub */
2663 * Calculates the Identity for And nodes.
2665 static node_t *identity_And(node_t *node) {
2666 ir_node *and = node->node;
2667 node_t *a = get_irn_node(get_And_left(and));
2668 node_t *b = get_irn_node(get_And_right(and));
2669 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2671 /* node: no input should be tarval_top, else the And would be also
2672 * Top and not being split. */
2673 if (a->type.tv == neutral)
2675 if (b->type.tv == neutral)
2678 } /* identity_And */
2681 * Calculates the Identity for Confirm nodes.
2683 static node_t *identity_Confirm(node_t *node) {
2684 ir_node *confirm = node->node;
2686 /* a Confirm is always a Copy */
2687 return get_irn_node(get_Confirm_value(confirm));
2688 } /* identity_Confirm */
2691 * Calculates the Identity for Mux nodes.
2693 static node_t *identity_Mux(node_t *node) {
2694 ir_node *mux = node->node;
2695 node_t *t = get_irn_node(get_Mux_true(mux));
2696 node_t *f = get_irn_node(get_Mux_false(mux));
2699 if (t->part == f->part)
2702 /* for now, the 1-input identity is not supported */
2704 sel = get_irn_node(get_Mux_sel(mux));
2706 /* Mux sel input is mode_b, so it is always a tarval */
2707 if (sel->type.tv == tarval_b_true)
2709 if (sel->type.tv == tarval_b_false)
2713 } /* identity_Mux */
2716 * Calculates the Identity for nodes.
2718 static node_t *identity(node_t *node) {
2719 ir_node *irn = node->node;
2721 switch (get_irn_opcode(irn)) {
2723 return identity_Phi(node);
2725 return identity_Mul(node);
2729 return identity_comm_zero_binop(node);
2734 return identity_shift(node);
2736 return identity_And(node);
2738 return identity_Sub(node);
2740 return identity_Confirm(node);
2742 return identity_Mux(node);
2749 * Node follower is a (new) follower of leader, segregate Leader
2752 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2753 ir_node *l = leader->node;
2754 int j, i, n = get_irn_n_outs(l);
2756 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2757 /* The leader edges must remain sorted, but follower edges can
2759 for (i = leader->n_followers + 1; i <= n; ++i) {
2760 if (l->out[i].use == follower) {
2761 ir_def_use_edge t = l->out[i];
2763 for (j = i - 1; j >= leader->n_followers + 1; --j)
2764 l->out[j + 1] = l->out[j];
2765 ++leader->n_followers;
2766 l->out[leader->n_followers] = t;
2770 } /* segregate_def_use_chain_1 */
2773 * Node follower is a (new) follower segregate its Leader
2776 * @param follower the follower IR node
2778 static void segregate_def_use_chain(const ir_node *follower) {
2781 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2782 node_t *pred = get_irn_node(get_irn_n(follower, i));
2784 segregate_def_use_chain_1(follower, pred);
2786 } /* segregate_def_use_chain */
2789 * Propagate constant evaluation.
2791 * @param env the environment
2793 static void propagate(environment_t *env) {
2796 lattice_elem_t old_type;
2798 unsigned n_fallen, old_type_was_T_or_C;
2801 while (env->cprop != NULL) {
2802 void *oldopcode = NULL;
2804 /* remove the first partition X from cprop */
2807 env->cprop = X->cprop_next;
2809 old_type_was_T_or_C = X->type_is_T_or_C;
2811 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2815 int cprop_empty = list_empty(&X->cprop);
2816 int cprop_X_empty = list_empty(&X->cprop_X);
2818 if (cprop_empty && cprop_X_empty) {
2819 /* both cprop lists are empty */
2823 /* remove the first Node x from X.cprop */
2825 /* Get a node from the cprop_X list only if
2826 * all data nodes are processed.
2827 * This ensures, that all inputs of the Cond
2828 * predecessor are processed if its type is still Top.
2830 x = list_entry(X->cprop_X.next, node_t, cprop_list);
2832 x = list_entry(X->cprop.next, node_t, cprop_list);
2835 //assert(x->part == X);
2836 list_del(&x->cprop_list);
2839 if (x->is_follower && identity(x) == x) {
2840 /* check the opcode first */
2841 if (oldopcode == NULL) {
2842 oldopcode = lambda_opcode(get_first_node(X), env);
2844 if (oldopcode != lambda_opcode(x, env)) {
2845 if (x->on_fallen == 0) {
2846 /* different opcode -> x falls out of this partition */
2851 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2855 /* x will make the follower -> leader transition */
2856 follower_to_leader(x);
2859 /* compute a new type for x */
2861 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2863 if (x->type.tv != old_type.tv) {
2864 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2865 verify_type(old_type, x);
2867 if (x->on_fallen == 0) {
2868 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2869 not already on the list. */
2874 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2876 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2877 ir_node *succ = get_irn_out(x->node, i);
2878 node_t *y = get_irn_node(succ);
2880 /* Add y to y.partition.cprop. */
2881 add_to_cprop(y, env);
2886 if (n_fallen > 0 && n_fallen != X->n_leader) {
2887 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2888 Y = split(&X, fallen, env);
2890 * We have split out fallen node. The type of the result
2891 * partition is NOT set yet.
2893 Y->type_is_T_or_C = 0;
2897 /* remove the flags from the fallen list */
2898 for (x = fallen; x != NULL; x = x->next)
2901 if (old_type_was_T_or_C) {
2904 /* check if some nodes will make the leader -> follower transition */
2905 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2906 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2907 node_t *eq_node = identity(y);
2909 if (eq_node != y && eq_node->part == y->part) {
2910 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2911 /* move to Follower */
2913 list_del(&y->node_list);
2914 list_add_tail(&y->node_list, &Y->Follower);
2917 segregate_def_use_chain(y->node);
2927 * Get the leader for a given node from its congruence class.
2929 * @param irn the node
2931 static ir_node *get_leader(node_t *node) {
2932 partition_t *part = node->part;
2934 if (part->n_leader > 1 || node->is_follower) {
2935 if (node->is_follower) {
2936 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2939 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2941 return get_first_node(part)->node;
2947 * Returns non-zero if a mode_T node has only one reachable output.
2949 static int only_one_reachable_proj(ir_node *n) {
2952 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2953 ir_node *proj = get_irn_out(n, i);
2956 /* skip non-control flow Proj's */
2957 if (get_irn_mode(proj) != mode_X)
2960 node = get_irn_node(proj);
2961 if (node->type.tv == tarval_reachable) {
2967 } /* only_one_reachable_proj */
2970 * Return non-zero if the control flow predecessor node pred
2971 * is the only reachable control flow exit of its block.
2973 * @param pred the control flow exit
2974 * @param block the destination block
2976 static int can_exchange(ir_node *pred, ir_node *block) {
2977 if (is_Start(pred) || has_Block_entity(block))
2979 else if (is_Jmp(pred))
2981 else if (get_irn_mode(pred) == mode_T) {
2982 /* if the predecessor block has more than one
2983 reachable outputs we cannot remove the block */
2984 return only_one_reachable_proj(pred);
2987 } /* can_exchange */
2990 * Block Post-Walker, apply the analysis results on control flow by
2991 * shortening Phi's and Block inputs.
2993 static void apply_cf(ir_node *block, void *ctx) {
2994 environment_t *env = ctx;
2995 node_t *node = get_irn_node(block);
2997 ir_node **ins, **in_X;
2998 ir_node *phi, *next;
3000 n = get_Block_n_cfgpreds(block);
3002 if (node->type.tv == tarval_unreachable) {
3005 for (i = n - 1; i >= 0; --i) {
3006 ir_node *pred = get_Block_cfgpred(block, i);
3008 if (! is_Bad(pred)) {
3009 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3011 if (pred_bl->flagged == 0) {
3012 pred_bl->flagged = 3;
3014 if (pred_bl->type.tv == tarval_reachable) {
3016 * We will remove an edge from block to its pred.
3017 * This might leave the pred block as an endless loop
3019 if (! is_backedge(block, i))
3020 keep_alive(pred_bl->node);
3026 /* the EndBlock is always reachable even if the analysis
3027 finds out the opposite :-) */
3028 if (block != get_irg_end_block(current_ir_graph)) {
3029 /* mark dead blocks */
3030 set_Block_dead(block);
3031 DB((dbg, LEVEL_1, "Removing dead %+F\n", block));
3033 /* the endblock is unreachable */
3034 set_irn_in(block, 0, NULL);
3040 /* only one predecessor combine */
3041 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
3043 if (can_exchange(pred, block)) {
3044 ir_node *new_block = get_nodes_block(pred);
3045 DB((dbg, LEVEL_1, "Fuse %+F with %+F\n", block, new_block));
3046 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3047 exchange(block, new_block);
3048 node->node = new_block;
3054 NEW_ARR_A(ir_node *, in_X, n);
3056 for (i = 0; i < n; ++i) {
3057 ir_node *pred = get_Block_cfgpred(block, i);
3058 node_t *node = get_irn_node(pred);
3060 if (node->type.tv == tarval_reachable) {
3063 DB((dbg, LEVEL_1, "Removing dead input %d from %+F (%+F)\n", i, block, pred));
3064 if (! is_Bad(pred)) {
3065 node_t *pred_bl = get_irn_node(get_nodes_block(skip_Proj(pred)));
3067 if (pred_bl->flagged == 0) {
3068 pred_bl->flagged = 3;
3070 if (pred_bl->type.tv == tarval_reachable) {
3072 * We will remove an edge from block to its pred.
3073 * This might leave the pred block as an endless loop
3075 if (! is_backedge(block, i))
3076 keep_alive(pred_bl->node);
3086 NEW_ARR_A(ir_node *, ins, n);
3087 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
3088 node_t *node = get_irn_node(phi);
3090 next = get_Phi_next(phi);
3091 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3092 /* this Phi is replaced by a constant */
3093 tarval *tv = node->type.tv;
3094 ir_node *c = new_Const(tv);
3096 set_irn_node(c, node);
3098 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
3099 DBG_OPT_COMBO(phi, c, FS_OPT_COMBO_CONST);
3104 for (i = 0; i < n; ++i) {
3105 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
3107 if (pred->type.tv == tarval_reachable) {
3108 ins[j++] = get_Phi_pred(phi, i);
3112 /* this Phi is replaced by a single predecessor */
3113 ir_node *s = ins[0];
3114 node_t *phi_node = get_irn_node(phi);
3117 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
3118 DBG_OPT_COMBO(phi, s, FS_OPT_COMBO_FOLLOWER);
3123 set_irn_in(phi, j, ins);
3131 /* this Block has only one live predecessor */
3132 ir_node *pred = skip_Proj(in_X[0]);
3134 if (can_exchange(pred, block)) {
3135 ir_node *new_block = get_nodes_block(pred);
3136 DBG_OPT_COMBO(block, new_block, FS_OPT_COMBO_CF);
3137 exchange(block, new_block);
3138 node->node = new_block;
3143 set_irn_in(block, k, in_X);
3148 * Exchange a node by its leader.
3149 * Beware: in rare cases the mode might be wrong here, for instance
3150 * AddP(x, NULL) is a follower of x, but with different mode.
3153 static void exchange_leader(ir_node *irn, ir_node *leader) {
3154 ir_mode *mode = get_irn_mode(irn);
3155 if (mode != get_irn_mode(leader)) {
3156 /* The conv is a no-op, so we are free to place it
3157 * either in the block of the leader OR in irn's block.
3158 * Probably placing it into leaders block might reduce
3159 * the number of Conv due to CSE. */
3160 ir_node *block = get_nodes_block(leader);
3161 dbg_info *dbg = get_irn_dbg_info(irn);
3163 leader = new_rd_Conv(dbg, block, leader, mode);
3165 exchange(irn, leader);
3166 } /* exchange_leader */
3169 * Check, if all users of a mode_M node are dead. Use
3170 * the Def-Use edges for this purpose, as they still
3171 * reflect the situation.
3173 static int all_users_are_dead(const ir_node *irn) {
3174 int i, n = get_irn_n_outs(irn);
3176 for (i = 1; i <= n; ++i) {
3177 const ir_node *succ = irn->out[i].use;
3178 const node_t *block = get_irn_node(get_nodes_block(succ));
3181 if (block->type.tv == tarval_unreachable) {
3182 /* block is unreachable */
3185 node = get_irn_node(succ);
3186 if (node->type.tv != tarval_top) {
3187 /* found a reachable user */
3191 /* all users are unreachable */
3193 } /* all_user_are_dead */
3196 * Walker: Find reachable mode_M nodes that have only
3197 * unreachable users. These nodes must be kept later.
3199 static void find_kept_memory(ir_node *irn, void *ctx) {
3200 environment_t *env = ctx;
3201 node_t *node, *block;
3203 if (get_irn_mode(irn) != mode_M)
3206 block = get_irn_node(get_nodes_block(irn));
3207 if (block->type.tv == tarval_unreachable)
3210 node = get_irn_node(irn);
3211 if (node->type.tv == tarval_top)
3214 /* ok, we found a live memory node. */
3215 if (all_users_are_dead(irn)) {
3216 DB((dbg, LEVEL_1, "%+F must be kept\n", irn));
3217 ARR_APP1(ir_node *, env->kept_memory, irn);
3219 } /* find_kept_memory */
3222 * Post-Walker, apply the analysis results;
3224 static void apply_result(ir_node *irn, void *ctx) {
3225 environment_t *env = ctx;
3226 node_t *node = get_irn_node(irn);
3228 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
3229 /* blocks already handled, do not touch the End node */
3231 node_t *block = get_irn_node(get_nodes_block(irn));
3233 if (block->type.tv == tarval_unreachable) {
3234 ir_node *bad = get_irg_bad(current_ir_graph);
3236 /* here, bad might already have a node, but this can be safely ignored
3237 as long as bad has at least ONE valid node */
3238 set_irn_node(bad, node);
3240 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
3243 } else if (node->type.tv == tarval_top) {
3244 ir_mode *mode = get_irn_mode(irn);
3246 if (mode == mode_M) {
3247 /* never kill a mode_M node */
3249 ir_node *pred = get_Proj_pred(irn);
3250 node_t *pnode = get_irn_node(pred);
3252 if (pnode->type.tv == tarval_top) {
3253 /* skip the predecessor */
3254 ir_node *mem = get_memop_mem(pred);
3256 DB((dbg, LEVEL_1, "%+F computes Top, replaced by %+F\n", irn, mem));
3261 /* leave other nodes, especially PhiM */
3262 } else if (mode == mode_T) {
3263 /* Do not kill mode_T nodes, kill their Projs */
3264 } else if (! is_Unknown(irn)) {
3265 /* don't kick away Unknown's, they might be still needed */
3266 ir_node *unk = new_r_Unknown(current_ir_graph, mode);
3268 /* control flow should already be handled at apply_cf() */
3269 assert(mode != mode_X);
3271 /* see comment above */
3272 set_irn_node(unk, node);
3274 DB((dbg, LEVEL_1, "%+F computes Top\n", irn));
3279 else if (get_irn_mode(irn) == mode_X) {
3282 ir_node *cond = get_Proj_pred(irn);
3284 if (is_Cond(cond)) {
3285 if (only_one_reachable_proj(cond)) {
3286 ir_node *jmp = new_r_Jmp(block->node);
3287 set_irn_node(jmp, node);
3289 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
3290 DBG_OPT_COMBO(irn, jmp, FS_OPT_COMBO_CF);
3294 node_t *sel = get_irn_node(get_Cond_selector(cond));
3295 tarval *tv = sel->type.tv;
3297 if (is_tarval(tv) && tarval_is_constant(tv)) {
3298 /* The selector is a constant, but more
3299 * than one output is active: An unoptimized
3307 /* normal data node */
3308 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
3309 tarval *tv = node->type.tv;
3312 * Beware: never replace mode_T nodes by constants. Currently we must mark
3313 * mode_T nodes with constants, but do NOT replace them.
3315 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
3316 /* can be replaced by a constant */
3317 ir_node *c = new_Const(tv);
3318 set_irn_node(c, node);
3320 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
3321 DBG_OPT_COMBO(irn, c, FS_OPT_COMBO_CONST);
3322 exchange_leader(irn, c);
3325 } else if (is_entity(node->type.sym.entity_p)) {
3326 if (! is_SymConst(irn)) {
3327 /* can be replaced by a SymConst */
3328 ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
3329 set_irn_node(symc, node);
3332 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
3333 DBG_OPT_COMBO(irn, symc, FS_OPT_COMBO_CONST);
3334 exchange_leader(irn, symc);
3337 } else if (is_Confirm(irn)) {
3338 /* Confirms are always follower, but do not kill them here */
3340 ir_node *leader = get_leader(node);
3342 if (leader != irn) {
3343 int non_strict_phi = 0;
3346 * Beware: Do not remove Phi(Unknown, ..., x, ..., Unknown)
3347 * as this might create non-strict programs.
3349 if (node->is_follower && is_Phi(irn) && !is_Unknown(leader)) {
3352 for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
3353 ir_node *pred = get_Phi_pred(irn, i);
3355 if (is_Unknown(pred)) {
3361 if (! non_strict_phi) {
3362 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
3363 if (node->is_follower)
3364 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_FOLLOWER);
3366 DBG_OPT_COMBO(irn, leader, FS_OPT_COMBO_CONGRUENT);
3367 exchange_leader(irn, leader);
3374 } /* apply_result */
3377 * Fix the keep-alives by deleting unreachable ones.
3379 static void apply_end(ir_node *end, environment_t *env) {
3380 int i, j, n = get_End_n_keepalives(end);
3384 NEW_ARR_A(ir_node *, in, n);
3386 /* fix the keep alive */
3387 for (i = j = 0; i < n; i++) {
3388 ir_node *ka = get_End_keepalive(end, i);
3389 node_t *node = get_irn_node(ka);
3392 node = get_irn_node(get_nodes_block(ka));
3394 if (node->type.tv != tarval_unreachable && !is_Bad(ka))
3398 set_End_keepalives(end, j, in);
3403 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
3406 * sets the generic functions to compute.
3408 static void set_compute_functions(void) {
3411 /* set the default compute function */
3412 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
3413 ir_op *op = get_irp_opcode(i);
3414 op->ops.generic = (op_func)default_compute;
3417 /* set specific functions */
3433 } /* set_compute_functions */
3438 static void add_memory_keeps(ir_node **kept_memory, int len) {
3439 ir_node *end = get_irg_end(current_ir_graph);
3443 ir_nodeset_init(&set);
3445 /* check, if those nodes are already kept */
3446 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i)
3447 ir_nodeset_insert(&set, get_End_keepalive(end, i));
3449 for (i = len - 1; i >= 0; --i) {
3450 ir_node *ka = kept_memory[i];
3452 if (! ir_nodeset_contains(&set, ka)) {
3453 add_End_keepalive(end, ka);
3456 ir_nodeset_destroy(&set);
3457 } /* add_memory_keeps */
3459 void combo(ir_graph *irg) {
3461 ir_node *initial_bl;
3463 ir_graph *rem = current_ir_graph;
3466 current_ir_graph = irg;
3468 /* register a debug mask */
3469 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
3471 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
3473 obstack_init(&env.obst);
3474 env.worklist = NULL;
3478 #ifdef DEBUG_libfirm
3479 env.dbg_list = NULL;
3481 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
3482 env.kept_memory = NEW_ARR_F(ir_node *, 0);
3483 env.end_idx = get_opt_global_cse() ? 0 : -1;
3484 env.lambda_input = 0;
3487 /* options driving the optimization */
3488 env.commutative = 1;
3489 env.opt_unknown = 1;
3491 assure_irg_outs(irg);
3492 assure_cf_loop(irg);
3494 /* we have our own value_of function */
3495 set_value_of_func(get_node_tarval);
3497 set_compute_functions();
3498 DEBUG_ONLY(part_nr = 0);
3500 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3502 if (env.opt_unknown)
3503 tarval_UNKNOWN = tarval_top;
3505 tarval_UNKNOWN = tarval_bad;
3507 /* create the initial partition and place it on the work list */
3508 env.initial = new_partition(&env);
3509 add_to_worklist(env.initial, &env);
3510 irg_walk_graph(irg, create_initial_partitions, init_block_phis, &env);
3512 /* set the hook: from now, every node has a partition and a type */
3513 DEBUG_ONLY(set_dump_node_vcgattr_hook(dump_partition_hook));
3515 /* all nodes on the initial partition have type Top */
3516 env.initial->type_is_T_or_C = 1;
3518 /* Place the START Node's partition on cprop.
3519 Place the START Node on its local worklist. */
3520 initial_bl = get_irg_start_block(irg);
3521 start = get_irn_node(initial_bl);
3522 add_to_cprop(start, &env);
3526 if (env.worklist != NULL)
3528 } while (env.cprop != NULL || env.worklist != NULL);
3530 dump_all_partitions(&env);
3531 check_all_partitions(&env);
3534 dump_ir_block_graph(irg, "-partition");
3537 /* apply the result */
3539 /* check, which nodes must be kept */
3540 irg_walk_graph(irg, NULL, find_kept_memory, &env);
3542 /* kill unreachable control flow */
3543 irg_block_walk_graph(irg, NULL, apply_cf, &env);
3544 /* Kill keep-alives of dead blocks: this speeds up apply_result()
3545 * and fixes assertion because dead cf to dead blocks is NOT removed by
3547 apply_end(get_irg_end(irg), &env);
3548 irg_walk_graph(irg, NULL, apply_result, &env);
3550 len = ARR_LEN(env.kept_memory);
3552 add_memory_keeps(env.kept_memory, len);
3555 DB((dbg, LEVEL_1, "Unoptimized Control Flow left"));
3559 /* control flow might changed */
3560 set_irg_outs_inconsistent(irg);
3561 set_irg_extblk_inconsistent(irg);
3562 set_irg_doms_inconsistent(irg);
3563 set_irg_loopinfo_inconsistent(irg);
3564 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
3567 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
3569 /* remove the partition hook */
3570 DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
3572 DEL_ARR_F(env.kept_memory);
3573 del_set(env.opcode2id_map);
3574 obstack_free(&env.obst, NULL);
3576 /* restore value_of() default behavior */
3577 set_value_of_func(NULL);
3578 current_ir_graph = rem;
3581 /* Creates an ir_graph pass for combo. */
3582 ir_graph_pass_t *combo_pass(const char *name)
3584 return def_graph_pass(name ? name : "combo", combo);