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 * Note further that we use the terminology from Click's work here, which is different
27 * in some cases from Firm terminology. Especially, Click's type is a
28 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
36 #include "iroptimize.h"
44 #include "irgraph_t.h"
59 /* define this to check that all type translations are monotone */
60 #define VERIFY_MONOTONE
64 typedef struct node_t node_t;
65 typedef struct partition_t partition_t;
66 typedef struct opcode_key_t opcode_key_t;
67 typedef struct listmap_entry_t listmap_entry_t;
69 /** The type of the compute function. */
70 typedef void (*compute_func)(node_t *node);
76 ir_opcode code; /**< The Firm opcode. */
77 ir_mode *mode; /**< The mode of all nodes in the partition. */
79 long proj; /**< For Proj nodes, its proj number */
80 ir_entity *ent; /**< For Sel Nodes, its entity */
85 * An entry in the list_map.
87 struct listmap_entry_t {
88 void *id; /**< The id. */
89 node_t *list; /**< The associated list for this id. */
90 listmap_entry_t *next; /**< Link to the next entry in the map. */
93 /** We must map id's to lists. */
94 typedef struct listmap_t {
95 set *map; /**< Map id's to listmap_entry_t's */
96 listmap_entry_t *values; /**< List of all values in the map. */
100 * A lattice element. Because we handle constants and symbolic constants different, we
101 * have to use this union.
112 ir_node *node; /**< The IR-node itself. */
113 list_head node_list; /**< Double-linked list of leader/follower entries. */
114 list_head cprop_list; /**< Double-linked partition.cprop list. */
115 partition_t *part; /**< points to the partition this node belongs to */
116 node_t *next; /**< Next node on local list (partition.touched, fallen). */
117 node_t *race_next; /**< Next node on race list. */
118 lattice_elem_t type; /**< The associated lattice element "type". */
119 int max_user_input; /**< Maximum input number of Def-Use edges. */
120 int next_edge; /**< Index of the next Def-Use edge to use. */
121 int n_followers; /**< Number of Follower in the outs set. */
122 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
123 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
124 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
125 unsigned is_follower:1; /**< Set, if this node is a follower. */
126 unsigned is_flagged:1; /**< Set, if this node is flagged by step(). */
127 unsigned by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
131 * A partition containing congruent nodes.
134 list_head Leader; /**< The head of partition Leader node list. */
135 list_head Follower; /**< The head of partition Follower node list. */
136 list_head cprop; /**< The head of partition.cprop list. */
137 partition_t *wl_next; /**< Next entry in the work list if any. */
138 partition_t *touched_next; /**< Points to the next partition in the touched set. */
139 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
140 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
141 node_t *touched; /**< The partition.touched set of this partition. */
142 unsigned n_leader; /**< Number of entries in this partition.Leader. */
143 unsigned n_touched; /**< Number of entries in the partition.touched. */
144 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
145 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
146 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
147 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
148 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
150 partition_t *dbg_next; /**< Link all partitions for debugging */
151 unsigned nr; /**< A unique number for (what-)mapping, >0. */
155 typedef struct environment_t {
156 struct obstack obst; /**< obstack to allocate data structures. */
157 partition_t *worklist; /**< The work list. */
158 partition_t *cprop; /**< The constant propagation list. */
159 partition_t *touched; /**< the touched set. */
160 partition_t *initial; /**< The initial partition. */
161 set *opcode2id_map; /**< The opcodeMode->id map. */
162 pmap *type2id_map; /**< The type->id map. */
163 int end_idx; /**< -1 for local and 0 for global congruences. */
164 int lambda_input; /**< Captured argument for lambda_partition(). */
165 int modified; /**< Set, if the graph was modified. */
167 partition_t *dbg_list; /**< List of all partitions. */
171 /** Type of the what function. */
172 typedef void *(*what_func)(const node_t *node, environment_t *env);
174 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
175 #define set_irn_node(follower, node) set_irn_link(follower, node)
177 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
178 #undef tarval_unreachable
179 #define tarval_unreachable tarval_top
182 /** The debug module handle. */
183 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
185 /** Next partition number. */
186 DEBUG_ONLY(static unsigned part_nr = 0);
189 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
192 * Dump partition to output.
194 static void dump_partition(const char *msg, const partition_t *part) {
197 lattice_elem_t type = get_partition_type(part);
199 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
200 msg, part->nr, part->type_is_T_or_C ? "*" : "",
201 part->n_leader, type));
202 list_for_each_entry(node_t, node, &part->Leader, node_list) {
203 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
206 if (! list_empty(&part->Follower)) {
207 DB((dbg, LEVEL_2, "\n---\n "));
209 list_for_each_entry(node_t, node, &part->Follower, node_list) {
210 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
214 DB((dbg, LEVEL_2, "\n}\n"));
215 } /* dump_partition */
220 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
224 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
226 DB((dbg, LEVEL_3, "%s = {\n ", msg));
227 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
228 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
231 DB((dbg, LEVEL_3, "\n}\n"));
239 static void dump_race_list(const char *msg, const node_t *list) {
240 do_dump_list(msg, list, offsetof(node_t, race_next));
244 * Dumps a local list.
246 static void dump_list(const char *msg, const node_t *list) {
247 do_dump_list(msg, list, offsetof(node_t, next));
251 * Dump all partitions.
253 static void dump_all_partitions(const environment_t *env) {
254 const partition_t *P;
256 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
257 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
258 dump_partition("", P);
262 #define dump_partition(msg, part)
263 #define dump_race_list(msg, list)
264 #define dump_list(msg, list)
265 #define dump_all_partitions(env)
268 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
270 * Verify that a type transition is monotone
272 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
273 if (old_type.tv == new_type.tv) {
277 if (old_type.tv == tarval_top) {
278 /* from Top down-to is always allowed */
281 if (old_type.tv == tarval_reachable) {
282 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
284 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
288 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
291 #define verify_type(old_type, new_type)
295 * Compare two pointer values of a listmap.
297 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
298 const listmap_entry_t *e1 = elt;
299 const listmap_entry_t *e2 = key;
302 return e1->id != e2->id;
303 } /* listmap_cmp_ptr */
306 * Initializes a listmap.
308 * @param map the listmap
310 static void listmap_init(listmap_t *map) {
311 map->map = new_set(listmap_cmp_ptr, 16);
316 * Terminates a listmap.
318 * @param map the listmap
320 static void listmap_term(listmap_t *map) {
325 * Return the associated listmap entry for a given id.
327 * @param map the listmap
328 * @param id the id to search for
330 * @return the asociated listmap entry for the given id
332 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
333 listmap_entry_t key, *entry;
338 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
340 if (entry->list == NULL) {
341 /* a new entry, put into the list */
342 entry->next = map->values;
349 * Calculate the hash value for an opcode map entry.
351 * @param entry an opcode map entry
353 * @return a hash value for the given opcode map entry
355 static unsigned opcode_hash(const opcode_key_t *entry) {
356 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
360 * Compare two entries in the opcode map.
362 static int cmp_opcode(const void *elt, const void *key, size_t size) {
363 const opcode_key_t *o1 = elt;
364 const opcode_key_t *o2 = key;
367 return o1->code != o2->code || o1->mode != o2->mode ||
368 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
372 * Compare two Def-Use edges for input position.
374 static int cmp_def_use_edge(const void *a, const void *b) {
375 const ir_def_use_edge *ea = a;
376 const ir_def_use_edge *eb = b;
378 /* no overrun, because range is [-1, MAXINT] */
379 return ea->pos - eb->pos;
380 } /* cmp_def_use_edge */
383 * We need the Def-Use edges sorted.
385 static void sort_irn_outs(node_t *node) {
386 ir_node *irn = node->node;
387 int n_outs = get_irn_n_outs(irn);
390 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
392 node->max_user_input = irn->out[n_outs].pos;
393 } /* sort_irn_outs */
396 * Return the type of a node.
398 * @param irn an IR-node
400 * @return the associated type of this node
402 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
403 return get_irn_node(irn)->type;
404 } /* get_node_type */
407 * Return the tarval of a node.
409 * @param irn an IR-node
411 * @return the associated type of this node
413 static INLINE tarval *get_node_tarval(const ir_node *irn) {
414 lattice_elem_t type = get_node_type(irn);
416 if (is_tarval(type.tv))
418 return tarval_bottom;
419 } /* get_node_type */
422 * Add a partition to the worklist.
424 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
425 assert(X->on_worklist == 0);
426 X->wl_next = env->worklist;
429 } /* add_to_worklist */
432 * Create a new empty partition.
434 * @param env the environment
436 * @return a newly allocated partition
438 static INLINE partition_t *new_partition(environment_t *env) {
439 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
441 INIT_LIST_HEAD(&part->Leader);
442 INIT_LIST_HEAD(&part->Follower);
443 INIT_LIST_HEAD(&part->cprop);
444 part->wl_next = NULL;
445 part->touched_next = NULL;
446 part->cprop_next = NULL;
447 part->split_next = NULL;
448 part->touched = NULL;
451 part->max_user_inputs = 0;
452 part->on_worklist = 0;
453 part->on_touched = 0;
455 part->type_is_T_or_C = 0;
457 part->dbg_next = env->dbg_list;
458 env->dbg_list = part;
459 part->nr = part_nr++;
463 } /* new_partition */
466 * Get the first node from a partition.
468 static INLINE node_t *get_first_node(const partition_t *X) {
469 return list_entry(X->Leader.next, node_t, node_list);
473 * Return the type of a partition (assuming partition is non-empty and
474 * all elements have the same type).
476 * @param X a partition
478 * @return the type of the first element of the partition
480 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
481 const node_t *first = get_first_node(X);
483 } /* get_partition_type */
486 * Creates a partition node for the given IR-node and place it
487 * into the given partition.
489 * @param irn an IR-node
490 * @param part a partition to place the node in
491 * @param env the environment
493 * @return the created node
495 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
496 /* create a partition node and place it in the partition */
497 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
499 INIT_LIST_HEAD(&node->node_list);
500 INIT_LIST_HEAD(&node->cprop_list);
504 node->race_next = NULL;
505 node->type.tv = tarval_top;
506 node->max_user_input = 0;
508 node->n_followers = 0;
509 node->on_touched = 0;
512 node->is_follower = 0;
513 node->is_flagged = 0;
514 node->by_all_const = 0;
515 set_irn_node(irn, node);
517 list_add_tail(&node->node_list, &part->Leader);
521 } /* create_partition_node */
524 * Pre-Walker, init all Block-Phi lists.
526 static void init_block_phis(ir_node *irn, void *env) {
530 set_Block_phis(irn, NULL);
535 * Post-Walker, initialize all Nodes' type to U or top and place
536 * all nodes into the TOP partition.
538 static void create_initial_partitions(ir_node *irn, void *ctx) {
539 environment_t *env = ctx;
540 partition_t *part = env->initial;
543 node = create_partition_node(irn, part, env);
545 if (node->max_user_input > part->max_user_inputs)
546 part->max_user_inputs = node->max_user_input;
549 add_Block_phi(get_nodes_block(irn), irn);
551 } /* create_initial_partitions */
554 * Add a partition to the touched set if not already there.
556 * @param part the partition
557 * @param env the environment
559 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
560 if (part->on_touched == 0) {
561 part->touched_next = env->touched;
563 part->on_touched = 1;
565 } /* add_to_touched */
568 * Add a node to the entry.partition.touched set if not already there.
572 static INLINE void add_to_partition_touched(node_t *y) {
573 if (y->on_touched == 0) {
574 partition_t *part = y->part;
576 y->next = part->touched;
581 } /* add_to_partition_touched */
584 * Update the worklist: If Z is on worklist then add Z' to worklist.
585 * Else add the smaller of Z and Z' to worklist.
587 * @param Z the Z partition
588 * @param Z_prime the Z' partition, a previous part of Z
589 * @param env the environment
591 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
592 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
593 add_to_worklist(Z_prime, env);
595 add_to_worklist(Z, env);
597 } /* update_worklist */
601 * Split a partition by a local list.
603 * @param Z the Z partition to split
604 * @param g a (non-empty) node list
605 * @param env the environment
607 * @return a new partition containing the nodes of g
609 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
610 partition_t *Z_prime;
615 dump_partition("Splitting ", Z);
619 /* Remove g from Z. */
620 for (node = g; node != NULL; node = node->next) {
621 list_del(&node->node_list);
624 assert(n < Z->n_leader);
627 /* Move g to a new partition, Z
\92. */
628 Z_prime = new_partition(env);
630 for (node = g; node != NULL; node = node->next) {
631 list_add(&node->node_list, &Z_prime->Leader);
632 node->part = Z_prime;
633 if (node->max_user_input > max_input)
634 max_input = node->max_user_input;
636 Z_prime->max_user_inputs = max_input;
637 Z_prime->n_leader = n;
639 /* for now, copy the type info tag. it will be adjusted
641 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
643 update_worklist(Z, Z_prime, env);
645 dump_partition("Now ", Z);
646 dump_partition("Created new ", Z_prime);
653 * The environment for one race step.
655 typedef struct step_env {
656 node_t *initial; /**< The initial node list. */
657 node_t *unwalked; /**< The unwalked node list. */
658 node_t *unwalked_last; /**< Points to the last element of the unwalked node list. */
659 node_t *walked; /**< The walked node list. */
660 int index; /**< Next index of Follower use_def edge. */
661 int n_leader; /**< number of Leader in initial. */
665 * Do one step in the race.
667 static int step(step_env *env) {
670 if (env->initial != NULL) {
671 /* Move node from initial to unwalked */
673 env->initial = n->race_next;
675 if (env->unwalked_last == NULL)
676 env->unwalked_last = n;
678 n->race_next = env->unwalked;
684 while (env->unwalked != NULL) {
685 /* let n be the first node in unwalked */
687 while (env->index < n->n_followers) {
688 /* let m be n.F.def_use[index] */
689 node_t *m = get_irn_node(n->node->out[1 + env->index].use);
691 assert(m->is_follower);
694 /* only followers from our partition */
695 if (m->part != n->part)
698 if (! m->is_flagged) {
701 /* add m to unwalked not as first node */
703 if (env->unwalked == NULL) {
706 env->unwalked_last->race_next = m;
708 env->unwalked_last = m;
712 /* move n to walked */
713 env->unwalked = n->race_next;
714 n->race_next = env->walked;
722 * Clear the flags from a list.
724 * @param list the list
726 static void clear_flags(node_t *list) {
729 for (n = list; n != NULL; n = n->race_next)
734 * Split a partition by a local list using the race.
736 * @param X the partition to split
737 * @param gg a (non-empty) node list
738 * @param env the environment
740 * @return a new partition containing the nodes of gg
742 static partition_t *split(partition_t *X, node_t *gg, environment_t *env) {
743 partition_t *X_prime;
745 step_env env1, env2, *winner;
746 node_t *g, *h, *node;
749 dump_partition("Splitting ", X);
750 dump_list("by list ", gg);
752 INIT_LIST_HEAD(&tmp);
754 /* Remove gg from X.Leader and put into g */
757 for (node = gg; node != NULL; node = node->next) {
758 list_del(&node->node_list);
759 list_add_tail(&node->node_list, &tmp);
767 list_for_each_entry(node_t, node, &X->Leader, node_list) {
772 /* restore X.Leader */
773 list_splice(&tmp, &X->Leader);
776 env1.unwalked = NULL;
777 env1.unwalked_last = NULL;
783 env2.unwalked = NULL;
784 env2.unwalked_last = NULL;
799 assert(winner->initial == NULL);
800 assert(winner->unwalked == NULL);
802 /* clear flags from walked/unwalked */
803 clear_flags(env1.unwalked);
804 clear_flags(env1.walked);
805 clear_flags(env2.unwalked);
806 clear_flags(env2.walked);
808 dump_race_list("winner ", winner->walked);
810 /* Move walked_{winner} to a new partition, X
\92. */
811 X_prime = new_partition(env);
813 for (node = winner->walked; node != NULL; node = node->race_next) {
814 list_del(&node->node_list);
815 if (node->is_follower) {
816 list_add(&node->node_list, &X_prime->Follower);
818 list_add(&node->node_list, &X_prime->Leader);
821 node->part = X_prime;
822 if (node->max_user_input > max_input)
823 max_input = node->max_user_input;
825 X_prime->max_user_inputs = max_input;
826 X->n_leader -= winner->n_leader;
828 /* for now, copy the type info tag. it will be adjusted
830 X_prime->type_is_T_or_C = X->type_is_T_or_C;
832 update_worklist(X, X_prime, env);
834 dump_partition("Now ", X);
835 dump_partition("Created new ", X_prime);
838 #endif /* NO_FOLLOWER */
841 * Returns non-zero if the i'th input of a Phi node is live.
843 * @param phi a Phi-node
844 * @param i an input number
846 * @return non-zero if the i'th input of the given Phi node is live
848 static int is_live_input(ir_node *phi, int i) {
850 ir_node *block = get_nodes_block(phi);
851 ir_node *pred = get_Block_cfgpred(block, i);
852 lattice_elem_t type = get_node_type(pred);
854 return type.tv != tarval_unreachable;
856 /* else it's the control input, always live */
858 } /* is_live_input */
861 * Return non-zero if a type is a constant.
863 static int is_constant_type(lattice_elem_t type) {
864 if (type.tv != tarval_bottom && type.tv != tarval_top)
867 } /* is_constant_type */
870 * Place a node on the cprop list.
873 * @param env the environment
875 static void add_node_to_cprop(node_t *y, environment_t *env) {
876 /* Add y to y.partition.cprop. */
877 if (y->on_cprop == 0) {
878 partition_t *Y = y->part;
880 list_add_tail(&y->cprop_list, &Y->cprop);
883 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
885 /* place its partition on the cprop list */
886 if (Y->on_cprop == 0) {
887 Y->cprop_next = env->cprop;
892 if (get_irn_mode(y->node) == mode_T) {
893 /* mode_T nodes always produce tarval_bottom, so we must explicitly
894 add it's Proj's to get constant evaluation to work */
897 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
898 node_t *proj = get_irn_node(get_irn_out(y->node, i));
900 add_node_to_cprop(proj, env);
904 if (is_Block(y->node)) {
905 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
906 * if someone placed the block. The Block is only placed if the reachability
907 * changes, and this must be re-evaluated in compute_Phi(). */
909 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
910 node_t *p = get_irn_node(phi);
911 add_node_to_cprop(p, env);
914 } /* add_node_to_cprop */
917 * Check whether a type is neither Top or a constant.
918 * Note: U is handled like Top here, R is a constant.
920 * @param type the type to check
922 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
923 if (is_tarval(type.tv)) {
924 if (type.tv == tarval_top)
926 if (tarval_is_constant(type.tv))
936 * Collect nodes to the touched list.
938 * @param list the list which contains the nodes that must be evaluated
939 * @param idx the index of the def_use edge to evaluate
940 * @param env the environment
942 static void collect_touched(list_head *list, int idx, environment_t *env) {
944 int end_idx = env->end_idx;
946 list_for_each_entry(node_t, x, list, node_list) {
950 /* leader edges start AFTER follower edges */
951 x->next_edge = 1 + x->n_followers;
953 num_edges = get_irn_n_outs(x->node);
955 /* for all edges in x.L.def_use_{idx} */
956 while (x->next_edge <= num_edges) {
957 ir_def_use_edge *edge = &x->node->out[x->next_edge];
960 /* check if we have necessary edges */
968 /* ignore the "control input" for non-pinned nodes
969 if we are running in GCSE mode */
970 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
973 y = get_irn_node(succ);
974 if (is_constant_type(y->type)) {
975 ir_opcode code = get_irn_opcode(succ);
976 if (code == iro_Sub || code == iro_Cmp)
977 add_node_to_cprop(y, env);
980 /* Partitions of constants should not be split simply because their Nodes have unequal
981 functions or incongruent inputs. */
982 if (type_is_neither_top_nor_const(y->type) &&
983 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
984 partition_t *Y = y->part;
985 add_to_touched(Y, env);
986 add_to_partition_touched(y);
992 * Split the partitions if caused by the first entry on the worklist.
994 * @param env the environment
996 static void cause_splits(environment_t *env) {
1001 /* remove the first partition from the worklist */
1003 env->worklist = X->wl_next;
1006 dump_partition("Cause_split: ", X);
1008 /* combine temporary leader and follower list */
1009 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1010 /* empty the touched set: already done, just clear the list */
1011 env->touched = NULL;
1013 collect_touched(&X->Leader, idx, env);
1014 collect_touched(&X->Follower, idx, env);
1016 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
1017 /* remove it from the touched set */
1020 if (Z->n_leader != Z->n_touched) {
1021 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1022 split(Z, Z->touched, env);
1024 /* Empty local Z.touched. */
1025 for (e = Z->touched; e != NULL; e = e->next) {
1032 } /* cause_splits */
1035 * Implements split_by_what(): Split a partition by characteristics given
1036 * by the what function.
1038 * @param X the partition to split
1039 * @param What a function returning an Id for every node of the partition X
1040 * @param P a list to store the result partitions
1041 * @param env the environment
1045 static partition_t *split_by_what(partition_t *X, what_func What,
1046 partition_t **P, environment_t *env) {
1049 listmap_entry_t *iter;
1052 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1054 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1055 void *id = What(x, env);
1056 listmap_entry_t *entry;
1059 /* input not allowed, ignore */
1062 /* Add x to map[What(x)]. */
1063 entry = listmap_find(&map, id);
1064 x->next = entry->list;
1067 /* Let P be a set of Partitions. */
1069 /* for all sets S except one in the range of map do */
1070 for (iter = map.values; iter != NULL; iter = iter->next) {
1071 if (iter->next == NULL) {
1072 /* this is the last entry, ignore */
1077 /* Add SPLIT( X, S ) to P. */
1078 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1079 R = split(X, S, env);
1089 } /* split_by_what */
1091 /** lambda n.(n.type) */
1092 static void *lambda_type(const node_t *node, environment_t *env) {
1094 return node->type.tv;
1097 /** lambda n.(n.opcode) */
1098 static void *lambda_opcode(const node_t *node, environment_t *env) {
1099 opcode_key_t key, *entry;
1100 ir_node *irn = node->node;
1102 key.code = get_irn_opcode(irn);
1103 key.mode = get_irn_mode(irn);
1107 switch (get_irn_opcode(irn)) {
1109 key.u.proj = get_Proj_proj(irn);
1112 key.u.ent = get_Sel_entity(irn);
1118 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1120 } /* lambda_opcode */
1122 /** lambda n.(n[i].partition) */
1123 static void *lambda_partition(const node_t *node, environment_t *env) {
1124 ir_node *skipped = skip_Proj(node->node);
1127 int i = env->lambda_input;
1129 if (i >= get_irn_arity(node->node)) {
1130 /* we are outside the allowed range */
1134 /* ignore the "control input" for non-pinned nodes
1135 if we are running in GCSE mode */
1136 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1139 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1140 p = get_irn_node(pred);
1143 } /* lambda_partition */
1146 * Returns true if a type is a constant.
1148 static int is_con(const lattice_elem_t type) {
1149 /* be conservative */
1150 if (is_tarval(type.tv))
1151 return tarval_is_constant(type.tv);
1152 return is_entity(type.sym.entity_p);
1156 * Implements split_by().
1158 * @param X the partition to split
1159 * @param env the environment
1161 static void split_by(partition_t *X, environment_t *env) {
1162 partition_t *I, *P = NULL;
1165 dump_partition("split_by", X);
1167 if (X->n_leader == 1) {
1168 /* we have only one leader, no need to split, just check it's type */
1169 node_t *x = get_first_node(X);
1170 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1174 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1175 P = split_by_what(X, lambda_type, &P, env);
1177 /* adjust the type tags, we have split partitions by type */
1178 for (I = P; I != NULL; I = I->split_next) {
1179 node_t *x = get_first_node(I);
1180 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1187 if (Y->n_leader > 1) {
1188 /* we do not want split the TOP or constant partitions */
1189 if (! Y->type_is_T_or_C) {
1190 partition_t *Q = NULL;
1192 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1193 Q = split_by_what(Y, lambda_opcode, &Q, env);
1199 if (Z->n_leader > 1) {
1200 const node_t *first = get_first_node(Z);
1201 int arity = get_irn_arity(first->node);
1205 * BEWARE: during splitting by input 2 for instance we might
1206 * create new partitions which are different by input 1, so collect
1207 * them and split further.
1209 Z->split_next = NULL;
1212 for (input = arity - 1; input >= -1; --input) {
1214 partition_t *Z_prime = R;
1217 if (Z_prime->n_leader > 1) {
1218 env->lambda_input = input;
1219 DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1220 S = split_by_what(Z_prime, lambda_partition, &S, env);
1222 Z_prime->split_next = S;
1225 } while (R != NULL);
1230 } while (Q != NULL);
1233 } while (P != NULL);
1237 * (Re-)compute the type for a given node.
1239 * @param node the node
1241 static void default_compute(node_t *node) {
1243 ir_node *irn = node->node;
1244 node_t *block = get_irn_node(get_nodes_block(irn));
1246 if (block->type.tv == tarval_unreachable) {
1247 node->type.tv = tarval_top;
1251 /* if any of the data inputs have type top, the result is type top */
1252 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1253 ir_node *pred = get_irn_n(irn, i);
1254 node_t *p = get_irn_node(pred);
1256 if (p->type.tv == tarval_top) {
1257 node->type.tv = tarval_top;
1262 if (get_irn_mode(node->node) == mode_X)
1263 node->type.tv = tarval_reachable;
1265 node->type.tv = computed_value(irn);
1266 } /* default_compute */
1269 * (Re-)compute the type for a Block node.
1271 * @param node the node
1273 static void compute_Block(node_t *node) {
1275 ir_node *block = node->node;
1277 if (block == get_irg_start_block(current_ir_graph)) {
1278 /* start block is always reachable */
1279 node->type.tv = tarval_reachable;
1283 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1284 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1286 if (pred->type.tv == tarval_reachable) {
1287 /* A block is reachable, if at least of predecessor is reachable. */
1288 node->type.tv = tarval_reachable;
1292 node->type.tv = tarval_top;
1293 } /* compute_Block */
1296 * (Re-)compute the type for a Bad node.
1298 * @param node the node
1300 static void compute_Bad(node_t *node) {
1301 /* Bad nodes ALWAYS compute Top */
1302 node->type.tv = tarval_top;
1306 * (Re-)compute the type for an Unknown node.
1308 * @param node the node
1310 static void compute_Unknown(node_t *node) {
1311 /* While Unknown nodes should compute Top this is dangerous:
1312 * a Top input to a Cond would lead to BOTH control flows unreachable.
1313 * While this is correct in the given semantics, it would destroy the Firm
1316 * It would be safe to compute Top IF it can be assured, that only Cmp
1317 * nodes are inputs to Conds. We check that first.
1318 * This is the way Frontends typically build Firm, but some optimizations
1319 * (cond_eval for instance) might replace them by Phib's...
1321 * For now, we compute bottom here.
1323 node->type.tv = tarval_bottom;
1324 } /* compute_Unknown */
1327 * (Re-)compute the type for a Jmp node.
1329 * @param node the node
1331 static void compute_Jmp(node_t *node) {
1332 node_t *block = get_irn_node(get_nodes_block(node->node));
1334 node->type = block->type;
1338 * (Re-)compute the type for the End node.
1340 * @param node the node
1342 static void compute_End(node_t *node) {
1343 /* the End node is NOT dead of course */
1344 node->type.tv = tarval_reachable;
1348 * (Re-)compute the type for a SymConst node.
1350 * @param node the node
1352 static void compute_SymConst(node_t *node) {
1353 ir_node *irn = node->node;
1354 node_t *block = get_irn_node(get_nodes_block(irn));
1356 if (block->type.tv == tarval_unreachable) {
1357 node->type.tv = tarval_top;
1360 switch (get_SymConst_kind(irn)) {
1361 case symconst_addr_ent:
1362 /* case symconst_addr_name: cannot handle this yet */
1363 node->type.sym = get_SymConst_symbol(irn);
1366 node->type.tv = computed_value(irn);
1368 } /* compute_SymConst */
1371 * (Re-)compute the type for a Phi node.
1373 * @param node the node
1375 static void compute_Phi(node_t *node) {
1377 ir_node *phi = node->node;
1378 lattice_elem_t type;
1380 /* if a Phi is in a unreachable block, its type is TOP */
1381 node_t *block = get_irn_node(get_nodes_block(phi));
1383 if (block->type.tv == tarval_unreachable) {
1384 node->type.tv = tarval_top;
1388 /* Phi implements the Meet operation */
1389 type.tv = tarval_top;
1390 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1391 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1392 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1394 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1395 /* ignore TOP inputs: We must check here for unreachable blocks,
1396 because Firm constants live in the Start Block are NEVER Top.
1397 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1398 comes from a unreachable input. */
1401 if (pred->type.tv == tarval_bottom) {
1402 node->type.tv = tarval_bottom;
1404 } else if (type.tv == tarval_top) {
1405 /* first constant found */
1407 } else if (type.tv != pred->type.tv) {
1408 /* different constants or tarval_bottom */
1409 node->type.tv = tarval_bottom;
1412 /* else nothing, constants are the same */
1418 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1420 * @param node the node
1422 static void compute_Add(node_t *node) {
1423 ir_node *sub = node->node;
1424 node_t *l = get_irn_node(get_Add_left(sub));
1425 node_t *r = get_irn_node(get_Add_right(sub));
1426 lattice_elem_t a = l->type;
1427 lattice_elem_t b = r->type;
1430 if (a.tv == tarval_top || b.tv == tarval_top) {
1431 node->type.tv = tarval_top;
1432 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1433 node->type.tv = tarval_bottom;
1435 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1436 must call tarval_add() first to handle this case! */
1437 if (is_tarval(a.tv)) {
1438 if (is_tarval(b.tv)) {
1439 node->type.tv = tarval_add(a.tv, b.tv);
1442 mode = get_tarval_mode(a.tv);
1443 if (a.tv == get_mode_null(mode)) {
1447 } else if (is_tarval(b.tv)) {
1448 mode = get_tarval_mode(b.tv);
1449 if (b.tv == get_mode_null(mode)) {
1454 node->type.tv = tarval_bottom;
1459 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1461 * @param node the node
1463 static void compute_Sub(node_t *node) {
1464 ir_node *sub = node->node;
1465 node_t *l = get_irn_node(get_Sub_left(sub));
1466 node_t *r = get_irn_node(get_Sub_right(sub));
1467 lattice_elem_t a = l->type;
1468 lattice_elem_t b = r->type;
1471 if (a.tv == tarval_top || b.tv == tarval_top) {
1472 node->type.tv = tarval_top;
1473 } else if (is_con(a) && is_con(b)) {
1474 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1475 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1476 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1478 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1481 node->type.tv = tarval_bottom;
1483 node->by_all_const = 1;
1484 } else if (r->part == l->part &&
1485 (!mode_is_float(get_irn_mode(l->node)))) {
1487 * BEWARE: a - a is NOT always 0 for floating Point values, as
1488 * NaN op NaN = NaN, so we must check this here.
1490 ir_mode *mode = get_irn_mode(sub);
1491 tv = get_mode_null(mode);
1493 /* if the node was ONCE evaluated by all constants, but now
1494 this breakes AND we cat by partition a different result, switch to bottom.
1495 This happens because initially all nodes are in the same partition ... */
1496 if (node->by_all_const && node->type.tv != tv)
1500 node->type.tv = tarval_bottom;
1505 * (Re-)compute the type for Cmp.
1507 * @param node the node
1509 static void compute_Cmp(node_t *node) {
1510 ir_node *cmp = node->node;
1511 node_t *l = get_irn_node(get_Cmp_left(cmp));
1512 node_t *r = get_irn_node(get_Cmp_right(cmp));
1513 lattice_elem_t a = l->type;
1514 lattice_elem_t b = r->type;
1516 if (a.tv == tarval_top || b.tv == tarval_top) {
1517 node->type.tv = tarval_top;
1518 } else if (is_con(a) && is_con(b)) {
1519 /* both nodes are constants, we can probably do something */
1520 node->type.tv = tarval_b_true;
1521 } else if (r->part == l->part) {
1522 /* both nodes congruent, we can probably do something */
1523 node->type.tv = tarval_b_true;
1525 node->type.tv = tarval_bottom;
1527 } /* compute_Proj_Cmp */
1530 * (Re-)compute the type for a Proj(Cmp).
1532 * @param node the node
1533 * @param cond the predecessor Cmp node
1535 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1536 ir_node *proj = node->node;
1537 node_t *l = get_irn_node(get_Cmp_left(cmp));
1538 node_t *r = get_irn_node(get_Cmp_right(cmp));
1539 lattice_elem_t a = l->type;
1540 lattice_elem_t b = r->type;
1541 pn_Cmp pnc = get_Proj_proj(proj);
1544 if (a.tv == tarval_top || b.tv == tarval_top) {
1545 node->type.tv = tarval_top;
1546 } else if (is_con(a) && is_con(b)) {
1547 default_compute(node);
1548 node->by_all_const = 1;
1549 } else if (r->part == l->part &&
1550 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1552 * BEWARE: a == a is NOT always True for floating Point values, as
1553 * NaN != NaN is defined, so we must check this here.
1555 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1557 /* if the node was ONCE evaluated by all constants, but now
1558 this breakes AND we cat by partition a different result, switch to bottom.
1559 This happens because initially all nodes are in the same partition ... */
1560 if (node->by_all_const && node->type.tv != tv)
1564 node->type.tv = tarval_bottom;
1566 } /* compute_Proj_Cmp */
1569 * (Re-)compute the type for a Proj(Cond).
1571 * @param node the node
1572 * @param cond the predecessor Cond node
1574 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1575 ir_node *proj = node->node;
1576 long pnc = get_Proj_proj(proj);
1577 ir_node *sel = get_Cond_selector(cond);
1578 node_t *selector = get_irn_node(sel);
1580 if (get_irn_mode(sel) == mode_b) {
1582 if (pnc == pn_Cond_true) {
1583 if (selector->type.tv == tarval_b_false) {
1584 node->type.tv = tarval_unreachable;
1585 } else if (selector->type.tv == tarval_b_true) {
1586 node->type.tv = tarval_reachable;
1587 } else if (selector->type.tv == tarval_bottom) {
1588 node->type.tv = tarval_reachable;
1590 assert(selector->type.tv == tarval_top);
1591 node->type.tv = tarval_unreachable;
1594 assert(pnc == pn_Cond_false);
1596 if (selector->type.tv == tarval_b_false) {
1597 node->type.tv = tarval_reachable;
1598 } else if (selector->type.tv == tarval_b_true) {
1599 node->type.tv = tarval_unreachable;
1600 } else if (selector->type.tv == tarval_bottom) {
1601 node->type.tv = tarval_reachable;
1603 assert(selector->type.tv == tarval_top);
1604 node->type.tv = tarval_unreachable;
1609 if (selector->type.tv == tarval_bottom) {
1610 node->type.tv = tarval_reachable;
1611 } else if (selector->type.tv == tarval_top) {
1612 node->type.tv = tarval_unreachable;
1614 long value = get_tarval_long(selector->type.tv);
1615 if (pnc == get_Cond_defaultProj(cond)) {
1616 /* default switch, have to check ALL other cases */
1619 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1620 ir_node *succ = get_irn_out(cond, i);
1624 if (value == get_Proj_proj(succ)) {
1625 /* we found a match, will NOT take the default case */
1626 node->type.tv = tarval_unreachable;
1630 /* all cases checked, no match, will take default case */
1631 node->type.tv = tarval_reachable;
1634 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1638 } /* compute_Proj_Cond */
1641 * (Re-)compute the type for a Proj-Node.
1643 * @param node the node
1645 static void compute_Proj(node_t *node) {
1646 ir_node *proj = node->node;
1647 ir_mode *mode = get_irn_mode(proj);
1648 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1649 ir_node *pred = get_Proj_pred(proj);
1651 if (block->type.tv == tarval_unreachable) {
1652 /* a Proj in a unreachable Block stay Top */
1653 node->type.tv = tarval_top;
1656 if (get_irn_node(pred)->type.tv == tarval_top) {
1657 /* if the predecessor is Top, its Proj follow */
1658 node->type.tv = tarval_top;
1662 if (mode == mode_M) {
1663 /* mode M is always bottom */
1664 node->type.tv = tarval_bottom;
1667 if (mode != mode_X) {
1669 compute_Proj_Cmp(node, pred);
1671 default_compute(node);
1674 /* handle mode_X nodes */
1676 switch (get_irn_opcode(pred)) {
1678 /* the Proj_X from the Start is always reachable.
1679 However this is already handled at the top. */
1680 node->type.tv = tarval_reachable;
1683 compute_Proj_Cond(node, pred);
1686 default_compute(node);
1688 } /* compute_Proj */
1691 * (Re-)compute the type for a Confirm.
1693 * @param node the node
1695 static void compute_Confirm(node_t *node) {
1696 ir_node *confirm = node->node;
1697 node_t *pred = get_irn_node(get_Confirm_value(confirm));
1699 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1700 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1702 if (is_con(bound->type)) {
1703 /* is equal to a constant */
1704 node->type = bound->type;
1708 /* a Confirm is a copy OR a Const */
1709 node->type = pred->type;
1710 } /* compute_Confirm */
1713 * (Re-)compute the type for a Max.
1715 * @param node the node
1717 static void compute_Max(node_t *node) {
1718 ir_node *op = node->node;
1719 node_t *l = get_irn_node(get_binop_left(op));
1720 node_t *r = get_irn_node(get_binop_right(op));
1721 lattice_elem_t a = l->type;
1722 lattice_elem_t b = r->type;
1724 if (a.tv == tarval_top || b.tv == tarval_top) {
1725 node->type.tv = tarval_top;
1726 } else if (is_con(a) && is_con(b)) {
1727 /* both nodes are constants, we can probably do something */
1729 /* this case handles symconsts as well */
1732 ir_mode *mode = get_irn_mode(op);
1733 tarval *tv_min = get_mode_min(mode);
1737 else if (b.tv == tv_min)
1739 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1740 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1741 node->type.tv = a.tv;
1743 node->type.tv = b.tv;
1745 node->type.tv = tarval_bad;
1748 } else if (r->part == l->part) {
1749 /* both nodes congruent, we can probably do something */
1752 node->type.tv = tarval_bottom;
1757 * (Re-)compute the type for a Min.
1759 * @param node the node
1761 static void compute_Min(node_t *node) {
1762 ir_node *op = node->node;
1763 node_t *l = get_irn_node(get_binop_left(op));
1764 node_t *r = get_irn_node(get_binop_right(op));
1765 lattice_elem_t a = l->type;
1766 lattice_elem_t b = r->type;
1768 if (a.tv == tarval_top || b.tv == tarval_top) {
1769 node->type.tv = tarval_top;
1770 } else if (is_con(a) && is_con(b)) {
1771 /* both nodes are constants, we can probably do something */
1773 /* this case handles symconsts as well */
1776 ir_mode *mode = get_irn_mode(op);
1777 tarval *tv_max = get_mode_max(mode);
1781 else if (b.tv == tv_max)
1783 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1784 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1785 node->type.tv = a.tv;
1787 node->type.tv = b.tv;
1789 node->type.tv = tarval_bad;
1792 } else if (r->part == l->part) {
1793 /* both nodes congruent, we can probably do something */
1796 node->type.tv = tarval_bottom;
1801 * (Re-)compute the type for a given node.
1803 * @param node the node
1805 static void compute(node_t *node) {
1808 if (is_no_Block(node->node)) {
1809 node_t *block = get_irn_node(get_nodes_block(node->node));
1811 if (block->type.tv == tarval_unreachable) {
1812 node->type.tv = tarval_top;
1817 func = (compute_func)node->node->op->ops.generic;
1823 * Identity functions: Note that one might thing that identity() is just a
1824 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
1825 * here, because it expects that the identity node is one of the inputs, which is NOT
1826 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
1827 * So, we have our own implementation, which copies some parts of equivalent_node()
1831 * Calculates the Identity for Phi nodes
1833 static node_t *identity_Phi(node_t *node) {
1834 ir_node *phi = node->node;
1835 ir_node *block = get_nodes_block(phi);
1836 node_t *n_part = NULL;
1839 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1840 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
1842 if (pred_X->type.tv == tarval_reachable) {
1843 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1847 else if (n_part->part != pred->part) {
1848 /* incongruent inputs, not a follower */
1853 /* if n_part is NULL here, all inputs path are dead, the Phi computes
1854 * tarval_top, is in the TOP partition and should NOT being split! */
1855 assert(n_part != NULL);
1857 } /* identity_Phi */
1860 * Calculates the Identity for commutative 0 neutral nodes.
1862 static node_t *identity_comm_zero_binop(node_t *node) {
1863 ir_node *op = node->node;
1864 node_t *a = get_irn_node(get_binop_left(op));
1865 node_t *b = get_irn_node(get_binop_right(op));
1866 ir_mode *mode = get_irn_mode(op);
1869 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1870 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1873 /* node: no input should be tarval_top, else the binop would be also
1874 * Top and not being split. */
1875 zero = get_mode_null(mode);
1876 if (a->type.tv == zero)
1878 if (b->type.tv == zero)
1881 } /* identity_comm_zero_binop */
1883 #define identity_Add identity_comm_zero_binop
1884 #define identity_Or identity_comm_zero_binop
1887 * Calculates the Identity for Mul nodes.
1889 static node_t *identity_Mul(node_t *node) {
1890 ir_node *op = node->node;
1891 node_t *a = get_irn_node(get_Mul_left(op));
1892 node_t *b = get_irn_node(get_Mul_right(op));
1893 ir_mode *mode = get_irn_mode(op);
1896 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1897 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1900 /* node: no input should be tarval_top, else the binop would be also
1901 * Top and not being split. */
1902 one = get_mode_one(mode);
1903 if (a->type.tv == one)
1905 if (b->type.tv == one)
1908 } /* identity_Mul */
1911 * Calculates the Identity for Sub nodes.
1913 static node_t *identity_Sub(node_t *node) {
1914 ir_node *sub = node->node;
1915 node_t *b = get_irn_node(get_Sub_right(sub));
1916 ir_mode *mode = get_irn_mode(sub);
1918 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1919 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1922 /* node: no input should be tarval_top, else the binop would be also
1923 * Top and not being split. */
1924 if (b->type.tv == get_mode_null(mode))
1925 return get_irn_node(get_Sub_left(sub));
1927 } /* identity_Mul */
1930 * Calculates the Identity for And nodes.
1932 static node_t *identity_And(node_t *node) {
1933 ir_node *and = node->node;
1934 node_t *a = get_irn_node(get_And_left(and));
1935 node_t *b = get_irn_node(get_And_right(and));
1936 tarval *neutral = get_mode_all_one(get_irn_mode(and));
1938 /* node: no input should be tarval_top, else the And would be also
1939 * Top and not being split. */
1940 if (a->type.tv == neutral)
1942 if (b->type.tv == neutral)
1945 } /* identity_And */
1948 * Calculates the Identity for Confirm nodes.
1950 static node_t *identity_Confirm(node_t *node) {
1951 ir_node *confirm = node->node;
1953 /* a Confirm is always a Copy */
1954 return get_irn_node(get_Confirm_value(confirm));
1955 } /* identity_Confirm */
1958 * Calculates the Identity for Mux nodes.
1960 static node_t *identity_Mux(node_t *node) {
1961 ir_node *mux = node->node;
1962 node_t *sel = get_irn_node(get_Mux_sel(mux));
1963 node_t *t = get_irn_node(get_Mux_true(mux));
1964 node_t *f = get_irn_node(get_Mux_false(mux));
1966 if (t->part == f->part)
1969 /* Mux sel input is mode_b, so it is always a tarval */
1970 if (sel->type.tv == tarval_b_true)
1972 if (sel->type.tv == tarval_b_false)
1975 } /* identity_Mux */
1978 * Calculates the Identity for Min nodes.
1980 static node_t *identity_Min(node_t *node) {
1981 ir_node *op = node->node;
1982 node_t *a = get_irn_node(get_binop_left(op));
1983 node_t *b = get_irn_node(get_binop_right(op));
1984 ir_mode *mode = get_irn_mode(op);
1987 if (a->part == b->part) {
1988 /* leader of multiple predecessors */
1992 /* works even with NaN */
1993 tv_max = get_mode_max(mode);
1994 if (a->type.tv == tv_max)
1996 if (b->type.tv == tv_max)
1999 } /* identity_Min */
2002 * Calculates the Identity for Max nodes.
2004 static node_t *identity_Max(node_t *node) {
2005 ir_node *op = node->node;
2006 node_t *a = get_irn_node(get_binop_left(op));
2007 node_t *b = get_irn_node(get_binop_right(op));
2008 ir_mode *mode = get_irn_mode(op);
2011 if (a->part == b->part) {
2012 /* leader of multiple predecessors */
2016 /* works even with NaN */
2017 tv_min = get_mode_min(mode);
2018 if (a->type.tv == tv_min)
2020 if (b->type.tv == tv_min)
2023 } /* identity_Max */
2026 * Calculates the Identity for nodes.
2028 static node_t *identity(node_t *node) {
2029 ir_node *irn = node->node;
2031 switch (get_irn_opcode(irn)) {
2033 return identity_Phi(node);
2035 return identity_Add(node);
2037 return identity_Or(node);
2039 return identity_Sub(node);
2041 return identity_Add(node);
2043 return identity_Confirm(node);
2045 return identity_Mux(node);
2047 return identity_Min(node);
2049 return identity_Max(node);
2056 * Node follower is a (new) follower of leader, segregate Leader
2059 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2060 ir_node *l = leader->node;
2061 int j, i, n = get_irn_n_outs(l);
2063 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2064 /* The leader edges must remain sorted, but follower edges can
2066 for (i = leader->n_followers + 1; i <= n; ++i) {
2067 if (l->out[i].use == follower) {
2068 ir_def_use_edge t = l->out[i];
2070 for (j = i - 1; j >= leader->n_followers + 1; --j)
2071 l->out[j + 1] = l->out[j];
2072 ++leader->n_followers;
2073 l->out[leader->n_followers] = t;
2075 /* note: a node might be a n-fold follower, for instance
2076 * if x = max(a,a), so no break here. */
2079 } /* segregate_def_use_chain_1 */
2082 * Node follower is a (new) follower of leader, segregate Leader
2083 * out edges. If follower is a n-congruent Input identity, all follower
2084 * inputs congruent to follower are also leader.
2086 static void segregate_def_use_chain(const ir_node *follower, node_t *leader) {
2087 ir_op *op = get_irn_op(follower);
2090 /* n-Congruent Input Identity for Phi's */
2092 ir_node *block = get_nodes_block(follower);
2094 DB((dbg, LEVEL_2, "n-Congruent follower %+F\n", follower));
2095 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2096 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2098 /* beware: we are NOT followers of dead inputs */
2099 if (pred_X->type.tv == tarval_reachable) {
2100 node_t *pred = get_irn_node(get_irn_n(follower, i));
2102 if (pred->part == leader->part)
2103 segregate_def_use_chain_1(follower, pred);
2106 } else if (op == op_Mux || op == op_Max || op == op_Min) {
2107 /* n-Congruent Input Identity */
2110 DB((dbg, LEVEL_2, "n-Congruent follower %+F\n", follower));
2111 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2112 node_t *pred = get_irn_node(get_irn_n(follower, i));
2114 if (pred->part == leader->part)
2115 segregate_def_use_chain_1(follower, pred);
2118 /* 1-Congruent Input Identity */
2119 segregate_def_use_chain_1(follower, leader);
2121 } /* segregate_def_use_chain */
2124 * Make all inputs to x from inside X no longer be F.def_use edges.
2126 static void move_edges_to_leader(node_t *x) {
2127 partition_t *X = x->part;
2128 ir_node *irn = x->node;
2131 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
2132 node_t *pred = get_irn_node(get_irn_n(irn, i));
2137 n = get_irn_n_outs(p);
2138 for (j = 1; j <= pred->n_followers; ++j) {
2139 if (p->out[j].pos == i && p->out[j].use == irn) {
2140 /* found a follower edge to x, move it to the Leader */
2141 ir_def_use_edge edge = p->out[j];
2143 /* remove this edge from the Follower set */
2144 p->out[j] = p->out[pred->n_followers];
2145 --pred->n_followers;
2147 /* sort it into the leader set */
2148 for (k = pred->n_followers + 2; k <= n; ++k) {
2149 if (p->out[k].pos >= edge.pos)
2151 p->out[k - 1] = p->out[k];
2153 /* place the new edge here */
2154 p->out[k - 1] = edge;
2156 /* edge found and moved */
2164 * Propagate constant evaluation.
2166 * @param env the environment
2168 static void propagate(environment_t *env) {
2171 lattice_elem_t old_type;
2173 unsigned n_fallen, old_type_was_T_or_C;
2176 while (env->cprop != NULL) {
2177 void *oldopcode = NULL;
2179 /* remove the first partition X from cprop */
2182 env->cprop = X->cprop_next;
2184 old_type_was_T_or_C = X->type_is_T_or_C;
2186 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2189 while (! list_empty(&X->cprop)) {
2190 /* remove the first Node x from X.cprop */
2191 x = list_entry(X->cprop.next, node_t, cprop_list);
2192 list_del(&x->cprop_list);
2195 if (x->is_follower && identity(x) == x) {
2196 /* x will make the follower -> leader transition */
2197 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", x->node));
2198 if (oldopcode == NULL) {
2199 oldopcode = lambda_opcode(get_first_node(X), env);
2201 if (oldopcode != lambda_opcode(x, env)) {
2202 /* different opcode -> x falls out of this partition */
2207 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2210 /* move x from X.Follower to X.Leader */
2211 list_del(&x->node_list);
2212 list_add_tail(&x->node_list, &X->Leader);
2216 /* Make all inputs to x from inside X no longer be F.def_use edges */
2217 move_edges_to_leader(x);
2220 /* compute a new type for x */
2222 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2224 if (x->type.tv != old_type.tv) {
2225 verify_type(old_type, x->type);
2226 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2228 if (x->on_fallen == 0) {
2229 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2230 not already on the list. */
2235 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2237 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2238 ir_node *succ = get_irn_out(x->node, i);
2239 node_t *y = get_irn_node(succ);
2241 /* Add y to y.partition.cprop. */
2242 add_node_to_cprop(y, env);
2247 if (n_fallen > 0 && n_fallen != X->n_leader) {
2248 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2249 Y = split(X, fallen, env);
2253 /* remove the flags from the fallen list */
2254 for (x = fallen; x != NULL; x = x->next)
2258 if (old_type_was_T_or_C) {
2261 /* check if some nodes will make the leader -> follower transition */
2262 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2263 if (! is_con(y->type)) {
2264 node_t *eq_node = identity(y);
2267 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2268 /* move to Follower */
2270 list_del(&y->node_list);
2273 list_add_tail(&y->node_list, &Y->Follower);
2274 segregate_def_use_chain(y->node, eq_node);
2285 * Get the leader for a given node from its congruence class.
2287 * @param irn the node
2289 static ir_node *get_leader(node_t *node) {
2290 partition_t *part = node->part;
2292 if (part->n_leader > 1 || node->is_follower) {
2293 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2295 return get_first_node(part)->node;
2301 * Return non-zero if the control flow predecessor node pred
2302 * is the only reachable control flow exit of its block.
2304 * @param pred the control flow exit
2306 static int can_exchange(ir_node *pred) {
2309 else if (is_Jmp(pred))
2311 else if (get_irn_mode(pred) == mode_T) {
2314 /* if the predecessor block has more than one
2315 reachable outputs we cannot remove the block */
2317 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2318 ir_node *proj = get_irn_out(pred, i);
2321 /* skip non-control flow Proj's */
2322 if (get_irn_mode(proj) != mode_X)
2325 node = get_irn_node(proj);
2326 if (node->type.tv == tarval_reachable) {
2337 * Block Post-Walker, apply the analysis results on control flow by
2338 * shortening Phi's and Block inputs.
2340 static void apply_cf(ir_node *block, void *ctx) {
2341 environment_t *env = ctx;
2342 node_t *node = get_irn_node(block);
2344 ir_node **ins, **in_X;
2345 ir_node *phi, *next;
2347 if (block == get_irg_end_block(current_ir_graph) ||
2348 block == get_irg_start_block(current_ir_graph)) {
2349 /* the EndBlock is always reachable even if the analysis
2350 finds out the opposite :-) */
2353 if (node->type.tv == tarval_unreachable) {
2354 /* mark dead blocks */
2355 set_Block_dead(block);
2359 n = get_Block_n_cfgpreds(block);
2362 /* only one predecessor combine */
2363 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2365 if (can_exchange(pred)) {
2366 exchange(block, get_nodes_block(pred));
2372 NEW_ARR_A(ir_node *, in_X, n);
2374 for (i = 0; i < n; ++i) {
2375 ir_node *pred = get_Block_cfgpred(block, i);
2376 node_t *node = get_irn_node(pred);
2378 if (node->type.tv == tarval_reachable) {
2385 NEW_ARR_A(ir_node *, ins, n);
2386 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2387 node_t *node = get_irn_node(phi);
2389 next = get_Phi_next(phi);
2390 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2391 /* this Phi is replaced by a constant */
2392 tarval *tv = node->type.tv;
2393 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2395 set_irn_node(c, node);
2397 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2402 for (i = 0; i < n; ++i) {
2403 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2405 if (pred->type.tv == tarval_reachable) {
2406 ins[j++] = get_Phi_pred(phi, i);
2410 /* this Phi is replaced by a single predecessor */
2411 ir_node *s = ins[0];
2414 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2418 set_irn_in(phi, j, ins);
2425 /* this Block has only one live predecessor */
2426 ir_node *pred = skip_Proj(in_X[0]);
2428 if (can_exchange(pred)) {
2429 exchange(block, get_nodes_block(pred));
2433 set_irn_in(block, k, in_X);
2439 * Post-Walker, apply the analysis results;
2441 static void apply_result(ir_node *irn, void *ctx) {
2442 environment_t *env = ctx;
2443 node_t *node = get_irn_node(irn);
2445 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2446 /* blocks already handled, do not touch the End node */
2448 node_t *block = get_irn_node(get_nodes_block(irn));
2450 if (block->type.tv == tarval_unreachable) {
2451 ir_node *bad = get_irg_bad(current_ir_graph);
2453 /* here, bad might already have a node, but this can be safely ignored
2454 as long as bad has at least ONE valid node */
2455 set_irn_node(bad, node);
2457 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2461 else if (node->type.tv == tarval_unreachable) {
2462 ir_node *bad = get_irg_bad(current_ir_graph);
2464 /* see comment above */
2465 set_irn_node(bad, node);
2467 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2471 else if (get_irn_mode(irn) == mode_X) {
2474 ir_node *cond = get_Proj_pred(irn);
2476 if (is_Cond(cond)) {
2477 node_t *sel = get_irn_node(get_Cond_selector(cond));
2479 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2480 /* Cond selector is a constant, make a Jmp */
2481 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2482 set_irn_node(jmp, node);
2484 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2491 /* normal data node */
2492 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2493 tarval *tv = node->type.tv;
2496 * Beware: never replace mode_T nodes by constants. Currently we must mark
2497 * mode_T nodes with constants, but do NOT replace them.
2499 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2500 /* can be replaced by a constant */
2501 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2502 set_irn_node(c, node);
2504 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2508 } else if (is_entity(node->type.sym.entity_p)) {
2509 if (! is_SymConst(irn)) {
2510 /* can be replaced by a Symconst */
2511 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2512 set_irn_node(symc, node);
2515 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2516 exchange(irn, symc);
2520 ir_node *leader = get_leader(node);
2522 if (leader != irn) {
2523 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2524 exchange(irn, leader);
2530 } /* apply_result */
2532 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2535 * sets the generic functions to compute.
2537 static void set_compute_functions(void) {
2540 /* set the default compute function */
2541 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2542 ir_op *op = get_irp_opcode(i);
2543 op->ops.generic = (op_func)default_compute;
2546 /* set specific functions */
2565 } /* set_compute_functions */
2567 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2568 ir_node *irn = local != NULL ? local : n;
2569 node_t *node = get_irn_node(irn);
2571 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2575 void combo(ir_graph *irg) {
2577 ir_node *initial_bl;
2579 ir_graph *rem = current_ir_graph;
2581 current_ir_graph = irg;
2583 /* register a debug mask */
2584 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2585 firm_dbg_set_mask(dbg, SET_LEVEL_3);
2587 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2589 obstack_init(&env.obst);
2590 env.worklist = NULL;
2594 #ifdef DEBUG_libfirm
2595 env.dbg_list = NULL;
2597 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
2598 env.type2id_map = pmap_create();
2599 env.end_idx = get_opt_global_cse() ? 0 : -1;
2600 env.lambda_input = 0;
2603 assure_irg_outs(irg);
2605 /* we have our own value_of function */
2606 set_value_of_func(get_node_tarval);
2608 set_compute_functions();
2609 DEBUG_ONLY(part_nr = 0);
2611 /* create the initial partition and place it on the work list */
2612 env.initial = new_partition(&env);
2613 add_to_worklist(env.initial, &env);
2614 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2616 /* all nodes on the initial partition have type Top */
2617 env.initial->type_is_T_or_C = 1;
2619 /* Place the START Node's partition on cprop.
2620 Place the START Node on its local worklist. */
2621 initial_bl = get_irg_start_block(irg);
2622 start = get_irn_node(initial_bl);
2623 add_node_to_cprop(start, &env);
2627 if (env.worklist != NULL)
2629 } while (env.cprop != NULL || env.worklist != NULL);
2631 dump_all_partitions(&env);
2634 set_dump_node_vcgattr_hook(dump_partition_hook);
2635 dump_ir_block_graph(irg, "-partition");
2636 set_dump_node_vcgattr_hook(NULL);
2638 (void)dump_partition_hook;
2641 /* apply the result */
2642 irg_block_walk_graph(irg, NULL, apply_cf, &env);
2643 irg_walk_graph(irg, NULL, apply_result, &env);
2646 /* control flow might changed */
2647 set_irg_outs_inconsistent(irg);
2648 set_irg_extblk_inconsistent(irg);
2649 set_irg_doms_inconsistent(irg);
2650 set_irg_loopinfo_inconsistent(irg);
2653 pmap_destroy(env.type2id_map);
2654 del_set(env.opcode2id_map);
2655 obstack_free(&env.obst, NULL);
2657 /* restore value_of() default behavior */
2658 set_value_of_func(NULL);
2659 current_ir_graph = rem;