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
62 /* define this to check the consistency of partitions */
63 #define CHECK_PARTITIONS
65 /* define this to disable followers (may be buggy) */
68 typedef struct node_t node_t;
69 typedef struct partition_t partition_t;
70 typedef struct opcode_key_t opcode_key_t;
71 typedef struct listmap_entry_t listmap_entry_t;
73 /** The type of the compute function. */
74 typedef void (*compute_func)(node_t *node);
80 ir_opcode code; /**< The Firm opcode. */
81 ir_mode *mode; /**< The mode of all nodes in the partition. */
82 int arity; /**< The arity of this opcode (needed for Phi etc. */
84 long proj; /**< For Proj nodes, its proj number */
85 ir_entity *ent; /**< For Sel Nodes, its entity */
90 * An entry in the list_map.
92 struct listmap_entry_t {
93 void *id; /**< The id. */
94 node_t *list; /**< The associated list for this id. */
95 listmap_entry_t *next; /**< Link to the next entry in the map. */
98 /** We must map id's to lists. */
99 typedef struct listmap_t {
100 set *map; /**< Map id's to listmap_entry_t's */
101 listmap_entry_t *values; /**< List of all values in the map. */
105 * A lattice element. Because we handle constants and symbolic constants different, we
106 * have to use this union.
117 ir_node *node; /**< The IR-node itself. */
118 list_head node_list; /**< Double-linked list of leader/follower entries. */
119 list_head cprop_list; /**< Double-linked partition.cprop list. */
120 partition_t *part; /**< points to the partition this node belongs to */
121 node_t *next; /**< Next node on local list (partition.touched, fallen). */
122 node_t *race_next; /**< Next node on race list. */
123 lattice_elem_t type; /**< The associated lattice element "type". */
124 int max_user_input; /**< Maximum input number of Def-Use edges. */
125 int next_edge; /**< Index of the next Def-Use edge to use. */
126 int n_followers; /**< Number of Follower in the outs set. */
127 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
128 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
129 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
130 unsigned is_follower:1; /**< Set, if this node is a follower. */
131 unsigned is_flagged:1; /**< Set, if this node is flagged by step(). */
132 unsigned by_all_const:1; /**< Set, if this node was once evaluated by all constants. */
136 * A partition containing congruent nodes.
139 list_head Leader; /**< The head of partition Leader node list. */
140 list_head Follower; /**< The head of partition Follower node list. */
141 list_head cprop; /**< The head of partition.cprop list. */
142 partition_t *wl_next; /**< Next entry in the work list if any. */
143 partition_t *touched_next; /**< Points to the next partition in the touched set. */
144 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
145 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
146 node_t *touched; /**< The partition.touched set of this partition. */
147 unsigned n_leader; /**< Number of entries in this partition.Leader. */
148 unsigned n_touched; /**< Number of entries in the partition.touched. */
149 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
150 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
151 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
152 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
153 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
155 partition_t *dbg_next; /**< Link all partitions for debugging */
156 unsigned nr; /**< A unique number for (what-)mapping, >0. */
160 typedef struct environment_t {
161 struct obstack obst; /**< obstack to allocate data structures. */
162 partition_t *worklist; /**< The work list. */
163 partition_t *cprop; /**< The constant propagation list. */
164 partition_t *touched; /**< the touched set. */
165 partition_t *initial; /**< The initial partition. */
166 set *opcode2id_map; /**< The opcodeMode->id map. */
167 pmap *type2id_map; /**< The type->id map. */
168 int end_idx; /**< -1 for local and 0 for global congruences. */
169 int lambda_input; /**< Captured argument for lambda_partition(). */
170 int modified; /**< Set, if the graph was modified. */
172 partition_t *dbg_list; /**< List of all partitions. */
176 /** Type of the what function. */
177 typedef void *(*what_func)(const node_t *node, environment_t *env);
179 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
180 #define set_irn_node(follower, node) set_irn_link(follower, node)
182 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
183 #undef tarval_unreachable
184 #define tarval_unreachable tarval_top
187 /** The debug module handle. */
188 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
190 /** Next partition number. */
191 DEBUG_ONLY(static unsigned part_nr = 0);
194 static node_t *identity(node_t *node);
196 #ifdef CHECK_PARTITIONS
200 static void check_partition(const partition_t *T) {
204 list_for_each_entry(node_t, node, &T->Leader, node_list) {
205 assert(node->is_follower == 0);
206 assert(node->part == T);
209 assert(n == T->n_leader);
211 list_for_each_entry(node_t, node, &T->Follower, node_list) {
212 assert(node->is_follower == 1);
213 assert(node->part == T);
215 } /* check_partition */
217 #define check_partition(T)
218 #endif /* CHECK_PARTITIONS */
221 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
224 * Dump partition to output.
226 static void dump_partition(const char *msg, const partition_t *part) {
229 lattice_elem_t type = get_partition_type(part);
231 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
232 msg, part->nr, part->type_is_T_or_C ? "*" : "",
233 part->n_leader, type));
234 list_for_each_entry(node_t, node, &part->Leader, node_list) {
235 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
238 if (! list_empty(&part->Follower)) {
239 DB((dbg, LEVEL_2, "\n---\n "));
241 list_for_each_entry(node_t, node, &part->Follower, node_list) {
242 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
246 DB((dbg, LEVEL_2, "\n}\n"));
247 } /* dump_partition */
252 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
256 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
258 DB((dbg, LEVEL_3, "%s = {\n ", msg));
259 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
260 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
263 DB((dbg, LEVEL_3, "\n}\n"));
271 static void dump_race_list(const char *msg, const node_t *list) {
272 do_dump_list(msg, list, offsetof(node_t, race_next));
276 * Dumps a local list.
278 static void dump_list(const char *msg, const node_t *list) {
279 do_dump_list(msg, list, offsetof(node_t, next));
283 * Dump all partitions.
285 static void dump_all_partitions(const environment_t *env) {
286 const partition_t *P;
288 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
289 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
290 dump_partition("", P);
294 #define dump_partition(msg, part)
295 #define dump_race_list(msg, list)
296 #define dump_list(msg, list)
297 #define dump_all_partitions(env)
300 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
302 * Verify that a type transition is monotone
304 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
305 if (old_type.tv == new_type.tv) {
309 if (old_type.tv == tarval_top) {
310 /* from Top down-to is always allowed */
313 if (old_type.tv == tarval_reachable) {
314 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
316 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
320 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
323 #define verify_type(old_type, new_type)
327 * Compare two pointer values of a listmap.
329 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
330 const listmap_entry_t *e1 = elt;
331 const listmap_entry_t *e2 = key;
334 return e1->id != e2->id;
335 } /* listmap_cmp_ptr */
338 * Initializes a listmap.
340 * @param map the listmap
342 static void listmap_init(listmap_t *map) {
343 map->map = new_set(listmap_cmp_ptr, 16);
348 * Terminates a listmap.
350 * @param map the listmap
352 static void listmap_term(listmap_t *map) {
357 * Return the associated listmap entry for a given id.
359 * @param map the listmap
360 * @param id the id to search for
362 * @return the asociated listmap entry for the given id
364 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
365 listmap_entry_t key, *entry;
370 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
372 if (entry->list == NULL) {
373 /* a new entry, put into the list */
374 entry->next = map->values;
381 * Calculate the hash value for an opcode map entry.
383 * @param entry an opcode map entry
385 * @return a hash value for the given opcode map entry
387 static unsigned opcode_hash(const opcode_key_t *entry) {
388 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
392 * Compare two entries in the opcode map.
394 static int cmp_opcode(const void *elt, const void *key, size_t size) {
395 const opcode_key_t *o1 = elt;
396 const opcode_key_t *o2 = key;
399 return o1->code != o2->code || o1->mode != o2->mode ||
400 o1->arity != o2->arity ||
401 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
405 * Compare two Def-Use edges for input position.
407 static int cmp_def_use_edge(const void *a, const void *b) {
408 const ir_def_use_edge *ea = a;
409 const ir_def_use_edge *eb = b;
411 /* no overrun, because range is [-1, MAXINT] */
412 return ea->pos - eb->pos;
413 } /* cmp_def_use_edge */
416 * We need the Def-Use edges sorted.
418 static void sort_irn_outs(node_t *node) {
419 ir_node *irn = node->node;
420 int n_outs = get_irn_n_outs(irn);
423 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
425 node->max_user_input = irn->out[n_outs].pos;
426 } /* sort_irn_outs */
429 * Return the type of a node.
431 * @param irn an IR-node
433 * @return the associated type of this node
435 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
436 return get_irn_node(irn)->type;
437 } /* get_node_type */
440 * Return the tarval of a node.
442 * @param irn an IR-node
444 * @return the associated type of this node
446 static INLINE tarval *get_node_tarval(const ir_node *irn) {
447 lattice_elem_t type = get_node_type(irn);
449 if (is_tarval(type.tv))
451 return tarval_bottom;
452 } /* get_node_type */
455 * Add a partition to the worklist.
457 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
458 assert(X->on_worklist == 0);
459 X->wl_next = env->worklist;
462 } /* add_to_worklist */
465 * Create a new empty partition.
467 * @param env the environment
469 * @return a newly allocated partition
471 static INLINE partition_t *new_partition(environment_t *env) {
472 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
474 INIT_LIST_HEAD(&part->Leader);
475 INIT_LIST_HEAD(&part->Follower);
476 INIT_LIST_HEAD(&part->cprop);
477 part->wl_next = NULL;
478 part->touched_next = NULL;
479 part->cprop_next = NULL;
480 part->split_next = NULL;
481 part->touched = NULL;
484 part->max_user_inputs = 0;
485 part->on_worklist = 0;
486 part->on_touched = 0;
488 part->type_is_T_or_C = 0;
490 part->dbg_next = env->dbg_list;
491 env->dbg_list = part;
492 part->nr = part_nr++;
496 } /* new_partition */
499 * Get the first node from a partition.
501 static INLINE node_t *get_first_node(const partition_t *X) {
502 return list_entry(X->Leader.next, node_t, node_list);
503 } /* get_first_node */
506 * Return the type of a partition (assuming partition is non-empty and
507 * all elements have the same type).
509 * @param X a partition
511 * @return the type of the first element of the partition
513 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
514 const node_t *first = get_first_node(X);
516 } /* get_partition_type */
519 * Creates a partition node for the given IR-node and place it
520 * into the given partition.
522 * @param irn an IR-node
523 * @param part a partition to place the node in
524 * @param env the environment
526 * @return the created node
528 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
529 /* create a partition node and place it in the partition */
530 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
532 INIT_LIST_HEAD(&node->node_list);
533 INIT_LIST_HEAD(&node->cprop_list);
537 node->race_next = NULL;
538 node->type.tv = tarval_top;
539 node->max_user_input = 0;
541 node->n_followers = 0;
542 node->on_touched = 0;
545 node->is_follower = 0;
546 node->is_flagged = 0;
547 node->by_all_const = 0;
548 set_irn_node(irn, node);
550 list_add_tail(&node->node_list, &part->Leader);
554 } /* create_partition_node */
557 * Pre-Walker, init all Block-Phi lists.
559 static void init_block_phis(ir_node *irn, void *env) {
563 set_Block_phis(irn, NULL);
565 } /* init_block_phis */
568 * Post-Walker, initialize all Nodes' type to U or top and place
569 * all nodes into the TOP partition.
571 static void create_initial_partitions(ir_node *irn, void *ctx) {
572 environment_t *env = ctx;
573 partition_t *part = env->initial;
576 node = create_partition_node(irn, part, env);
578 if (node->max_user_input > part->max_user_inputs)
579 part->max_user_inputs = node->max_user_input;
582 add_Block_phi(get_nodes_block(irn), irn);
584 } /* create_initial_partitions */
587 * Add a node to the entry.partition.touched set and
588 * node->partition to the touched set if not already there.
591 * @param env the environment
593 static INLINE void add_to_touched(node_t *y, environment_t *env) {
594 if (y->on_touched == 0) {
595 partition_t *part = y->part;
597 y->next = part->touched;
602 if (part->on_touched == 0) {
603 part->touched_next = env->touched;
605 part->on_touched = 1;
608 } /* add_to_touched */
611 * Place a node on the cprop list.
614 * @param env the environment
616 static void add_to_cprop(node_t *y, environment_t *env) {
617 /* Add y to y.partition.cprop. */
618 if (y->on_cprop == 0) {
619 partition_t *Y = y->part;
621 list_add_tail(&y->cprop_list, &Y->cprop);
624 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
626 /* place its partition on the cprop list */
627 if (Y->on_cprop == 0) {
628 Y->cprop_next = env->cprop;
633 if (get_irn_mode(y->node) == mode_T) {
634 /* mode_T nodes always produce tarval_bottom, so we must explicitly
635 add it's Proj's to get constant evaluation to work */
638 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
639 node_t *proj = get_irn_node(get_irn_out(y->node, i));
641 add_to_cprop(proj, env);
643 } else if (is_Block(y->node)) {
644 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
645 * if someone placed the block. The Block is only placed if the reachability
646 * changes, and this must be re-evaluated in compute_Phi(). */
648 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
649 node_t *p = get_irn_node(phi);
650 add_to_cprop(p, env);
656 * Update the worklist: If Z is on worklist then add Z' to worklist.
657 * Else add the smaller of Z and Z' to worklist.
659 * @param Z the Z partition
660 * @param Z_prime the Z' partition, a previous part of Z
661 * @param env the environment
663 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
664 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
665 add_to_worklist(Z_prime, env);
667 add_to_worklist(Z, env);
669 } /* update_worklist */
672 * Make all inputs to x no longer be F.def_use edges.
676 static void move_edges_to_leader(node_t *x) {
677 ir_node *irn = x->node;
680 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
681 node_t *pred = get_irn_node(get_irn_n(irn, i));
686 n = get_irn_n_outs(p);
687 for (j = 1; j <= pred->n_followers; ++j) {
688 if (p->out[j].pos == i && p->out[j].use == irn) {
689 /* found a follower edge to x, move it to the Leader */
690 ir_def_use_edge edge = p->out[j];
692 /* remove this edge from the Follower set */
693 p->out[j] = p->out[pred->n_followers];
696 /* sort it into the leader set */
697 for (k = pred->n_followers + 2; k <= n; ++k) {
698 if (p->out[k].pos >= edge.pos)
700 p->out[k - 1] = p->out[k];
702 /* place the new edge here */
703 p->out[k - 1] = edge;
705 /* edge found and moved */
710 } /* move_edges_to_leader */
713 * Split a partition that has NO followers by a local list.
715 * @param Z partition to split
716 * @param g a (non-empty) node list
717 * @param env the environment
719 * @return a new partition containing the nodes of g
721 static partition_t *split_no_followers(partition_t *Z, node_t *g, environment_t *env) {
722 partition_t *Z_prime;
727 dump_partition("Splitting ", Z);
728 dump_list("by list ", g);
732 /* Remove g from Z. */
733 for (node = g; node != NULL; node = node->next) {
734 assert(node->part == Z);
735 list_del(&node->node_list);
738 assert(n < Z->n_leader);
741 /* Move g to a new partition, Z'. */
742 Z_prime = new_partition(env);
744 for (node = g; node != NULL; node = node->next) {
745 list_add(&node->node_list, &Z_prime->Leader);
746 node->part = Z_prime;
747 if (node->max_user_input > max_input)
748 max_input = node->max_user_input;
750 Z_prime->max_user_inputs = max_input;
751 Z_prime->n_leader = n;
754 check_partition(Z_prime);
756 /* for now, copy the type info tag, it will be adjusted in split_by(). */
757 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
759 update_worklist(Z, Z_prime, env);
761 dump_partition("Now ", Z);
762 dump_partition("Created new ", Z_prime);
764 } /* split_no_followers */
768 #define split(Z, g, env) split_no_followers(*(Z), g, env)
773 * The environment for one race step.
775 typedef struct step_env {
776 node_t *initial; /**< The initial node list. */
777 node_t *unwalked; /**< The unwalked node list. */
778 node_t *walked; /**< The walked node list. */
779 int index; /**< Next index of Follower use_def edge. */
780 unsigned n_leader; /**< number of Leader in initial. */
784 * Do one step in the race.
786 static int step(step_env *env) {
789 if (env->initial != NULL) {
790 /* Move node from initial to unwalked */
792 env->initial = n->race_next;
794 n->race_next = env->unwalked;
800 while (env->unwalked != NULL) {
801 /* let n be the first node in unwalked */
803 while (env->index < n->n_followers) {
804 /* let m be n.F.def_use[index] */
805 node_t *m = get_irn_node(n->node->out[1 + env->index].use);
807 assert(m->is_follower);
810 /* only followers from our partition */
811 if (m->part != n->part)
814 if (!m->is_flagged) {
817 /* add m to unwalked not as first node (we might still need to
818 check for more follower node */
819 m->race_next = n->race_next;
824 /* move n to walked */
825 env->unwalked = n->race_next;
826 n->race_next = env->walked;
834 * Clear the flags from a list.
836 * @param list the list
838 static void clear_flags(node_t *list) {
841 for (n = list; n != NULL; n = n->race_next)
846 * Split a partition by a local list using the race.
848 * @param pX pointer to the partition to split, might be changed!
849 * @param gg a (non-empty) node list
850 * @param env the environment
852 * @return a new partition containing the nodes of gg
854 static partition_t *split(partition_t **pX, node_t *gg, environment_t *env) {
855 partition_t *X = *pX;
856 partition_t *X_prime;
858 step_env env1, env2, *winner;
859 node_t *g, *h, *node, *t;
862 DEBUG_ONLY(static int run = 0;)
864 DB((dbg, LEVEL_2, "Run %d ", run++));
865 if (list_empty(&X->Follower)) {
866 /* if the partition has NO follower, we can use the fast
867 splitting algorithm. */
868 return split_no_followers(X, gg, env);
870 /* else do the race */
872 dump_partition("Splitting ", X);
873 dump_list("by list ", gg);
875 INIT_LIST_HEAD(&tmp);
877 /* Remove gg from X.Leader and put into g */
880 for (node = gg; node != NULL; node = node->next) {
881 assert(node->part == X);
882 assert(node->is_follower == 0);
884 list_del(&node->node_list);
885 list_add_tail(&node->node_list, &tmp);
893 list_for_each_entry(node_t, node, &X->Leader, node_list) {
898 /* restore X.Leader */
899 list_splice(&tmp, &X->Leader);
902 env1.unwalked = NULL;
908 env2.unwalked = NULL;
923 assert(winner->initial == NULL);
924 assert(winner->unwalked == NULL);
926 /* clear flags from walked/unwalked */
927 clear_flags(env1.unwalked);
928 clear_flags(env1.walked);
929 clear_flags(env2.unwalked);
930 clear_flags(env2.walked);
932 dump_race_list("winner ", winner->walked);
934 /* Move walked_{winner} to a new partition, X'. */
935 X_prime = new_partition(env);
937 for (node = winner->walked; node != NULL; node = node->race_next) {
938 list_del(&node->node_list);
939 node->part = X_prime;
940 if (node->is_follower) {
941 list_add(&node->node_list, &X_prime->Follower);
943 list_add(&node->node_list, &X_prime->Leader);
946 if (node->max_user_input > max_input)
947 max_input = node->max_user_input;
949 X_prime->max_user_inputs = max_input;
950 X->n_leader -= X_prime->n_leader;
952 /* for now, copy the type info tag, it will be adjusted in split_by(). */
953 X_prime->type_is_T_or_C = X->type_is_T_or_C;
955 /* do the Follower -> Leader transition for nodes that loose congruent inputs */
956 list_for_each_entry_safe(node_t, node, t, &X_prime->Follower, node_list) {
957 if (identity(node) == node) {
958 /* we reach a follower from both sides, this will split congruent
959 * inputs and make it a leader. */
960 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", node->node));
961 node->is_follower = 0;
962 move_edges_to_leader(node);
963 list_del(&node->node_list);
964 list_add(&node->node_list, &X_prime->Leader);
969 check_partition(X_prime);
971 /* X' is the smaller part */
972 add_to_worklist(X_prime, env);
974 dump_partition("Now ", X);
975 dump_partition("Created new ", X_prime);
977 /* we have to ensure that the partition containing g is returned */
978 if (winner == &env2) {
985 #endif /* NO_FOLLOWER */
988 * Returns non-zero if the i'th input of a Phi node is live.
990 * @param phi a Phi-node
991 * @param i an input number
993 * @return non-zero if the i'th input of the given Phi node is live
995 static int is_live_input(ir_node *phi, int i) {
997 ir_node *block = get_nodes_block(phi);
998 ir_node *pred = get_Block_cfgpred(block, i);
999 lattice_elem_t type = get_node_type(pred);
1001 return type.tv != tarval_unreachable;
1003 /* else it's the control input, always live */
1005 } /* is_live_input */
1008 * Return non-zero if a type is a constant.
1010 static int is_constant_type(lattice_elem_t type) {
1011 if (type.tv != tarval_bottom && type.tv != tarval_top)
1014 } /* is_constant_type */
1017 * Check whether a type is neither Top or a constant.
1018 * Note: U is handled like Top here, R is a constant.
1020 * @param type the type to check
1022 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
1023 if (is_tarval(type.tv)) {
1024 if (type.tv == tarval_top)
1026 if (tarval_is_constant(type.tv))
1036 * Collect nodes to the touched list.
1038 * @param list the list which contains the nodes that must be evaluated
1039 * @param idx the index of the def_use edge to evaluate
1040 * @param env the environment
1042 static void collect_touched(list_head *list, int idx, environment_t *env) {
1044 int end_idx = env->end_idx;
1046 list_for_each_entry(node_t, x, list, node_list) {
1050 /* leader edges start AFTER follower edges */
1051 x->next_edge = 1 + x->n_followers;
1053 num_edges = get_irn_n_outs(x->node);
1055 /* for all edges in x.L.def_use_{idx} */
1056 while (x->next_edge <= num_edges) {
1057 ir_def_use_edge *edge = &x->node->out[x->next_edge];
1060 /* check if we have necessary edges */
1061 if (edge->pos > idx)
1068 /* ignore the "control input" for non-pinned nodes
1069 if we are running in GCSE mode */
1070 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
1073 y = get_irn_node(succ);
1075 /* ignore block edges touching followers */
1076 if (idx == -1 && y->is_follower)
1079 if (is_constant_type(y->type)) {
1080 ir_opcode code = get_irn_opcode(succ);
1081 if (code == iro_Sub || code == iro_Cmp)
1082 add_to_cprop(y, env);
1085 /* Partitions of constants should not be split simply because their Nodes have unequal
1086 functions or incongruent inputs. */
1087 if (type_is_neither_top_nor_const(y->type) &&
1088 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
1089 add_to_touched(y, env);
1093 } /* collect_touched */
1096 * Split the partitions if caused by the first entry on the worklist.
1098 * @param env the environment
1100 static void cause_splits(environment_t *env) {
1101 partition_t *X, *Z, *N;
1104 /* remove the first partition from the worklist */
1106 env->worklist = X->wl_next;
1109 dump_partition("Cause_split: ", X);
1111 /* combine temporary leader and follower list */
1112 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1113 /* empty the touched set: already done, just clear the list */
1114 env->touched = NULL;
1116 collect_touched(&X->Leader, idx, env);
1117 collect_touched(&X->Follower, idx, env);
1119 for (Z = env->touched; Z != NULL; Z = N) {
1121 node_t *touched = Z->touched;
1122 unsigned n_touched = Z->n_touched;
1124 assert(Z->touched != NULL);
1126 /* beware, split might change Z */
1127 N = Z->touched_next;
1129 /* remove it from the touched set */
1132 /* Empty local Z.touched. */
1133 for (e = touched; e != NULL; e = e->next) {
1134 assert(e->is_follower == 0);
1140 if (n_touched > 0 && Z->n_leader != n_touched) {
1141 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1142 split(&Z, touched, env);
1146 } /* cause_splits */
1149 * Implements split_by_what(): Split a partition by characteristics given
1150 * by the what function.
1152 * @param X the partition to split
1153 * @param What a function returning an Id for every node of the partition X
1154 * @param P a list to store the result partitions
1155 * @param env the environment
1159 static partition_t *split_by_what(partition_t *X, what_func What,
1160 partition_t **P, environment_t *env) {
1163 listmap_entry_t *iter;
1166 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1168 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1169 void *id = What(x, env);
1170 listmap_entry_t *entry;
1173 /* input not allowed, ignore */
1176 /* Add x to map[What(x)]. */
1177 entry = listmap_find(&map, id);
1178 x->next = entry->list;
1181 /* Let P be a set of Partitions. */
1183 /* for all sets S except one in the range of map do */
1184 for (iter = map.values; iter != NULL; iter = iter->next) {
1185 if (iter->next == NULL) {
1186 /* this is the last entry, ignore */
1191 /* Add SPLIT( X, S ) to P. */
1192 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1193 R = split(&X, S, env);
1203 } /* split_by_what */
1205 /** lambda n.(n.type) */
1206 static void *lambda_type(const node_t *node, environment_t *env) {
1208 return node->type.tv;
1211 /** lambda n.(n.opcode) */
1212 static void *lambda_opcode(const node_t *node, environment_t *env) {
1213 opcode_key_t key, *entry;
1214 ir_node *irn = node->node;
1216 key.code = get_irn_opcode(irn);
1217 key.mode = get_irn_mode(irn);
1218 key.arity = get_irn_arity(irn);
1222 switch (get_irn_opcode(irn)) {
1224 key.u.proj = get_Proj_proj(irn);
1227 key.u.ent = get_Sel_entity(irn);
1233 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1235 } /* lambda_opcode */
1237 /** lambda n.(n[i].partition) */
1238 static void *lambda_partition(const node_t *node, environment_t *env) {
1239 ir_node *skipped = skip_Proj(node->node);
1242 int i = env->lambda_input;
1244 if (i >= get_irn_arity(node->node)) {
1245 /* we are outside the allowed range */
1249 /* ignore the "control input" for non-pinned nodes
1250 if we are running in GCSE mode */
1251 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1254 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1255 p = get_irn_node(pred);
1258 } /* lambda_partition */
1261 * Returns true if a type is a constant.
1263 static int is_con(const lattice_elem_t type) {
1264 /* be conservative */
1265 if (is_tarval(type.tv))
1266 return tarval_is_constant(type.tv);
1267 return is_entity(type.sym.entity_p);
1271 * Implements split_by().
1273 * @param X the partition to split
1274 * @param env the environment
1276 static void split_by(partition_t *X, environment_t *env) {
1277 partition_t *I, *P = NULL;
1280 dump_partition("split_by", X);
1282 if (X->n_leader == 1) {
1283 /* we have only one leader, no need to split, just check it's type */
1284 node_t *x = get_first_node(X);
1285 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1289 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1290 P = split_by_what(X, lambda_type, &P, env);
1292 /* adjust the type tags, we have split partitions by type */
1293 for (I = P; I != NULL; I = I->split_next) {
1294 node_t *x = get_first_node(I);
1295 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1302 if (Y->n_leader > 1) {
1303 /* we do not want split the TOP or constant partitions */
1304 if (! Y->type_is_T_or_C) {
1305 partition_t *Q = NULL;
1307 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1308 Q = split_by_what(Y, lambda_opcode, &Q, env);
1314 if (Z->n_leader > 1) {
1315 const node_t *first = get_first_node(Z);
1316 int arity = get_irn_arity(first->node);
1320 * BEWARE: during splitting by input 2 for instance we might
1321 * create new partitions which are different by input 1, so collect
1322 * them and split further.
1324 Z->split_next = NULL;
1327 for (input = arity - 1; input >= -1; --input) {
1329 partition_t *Z_prime = R;
1332 if (Z_prime->n_leader > 1) {
1333 env->lambda_input = input;
1334 DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1335 S = split_by_what(Z_prime, lambda_partition, &S, env);
1337 Z_prime->split_next = S;
1340 } while (R != NULL);
1345 } while (Q != NULL);
1348 } while (P != NULL);
1352 * (Re-)compute the type for a given node.
1354 * @param node the node
1356 static void default_compute(node_t *node) {
1358 ir_node *irn = node->node;
1359 node_t *block = get_irn_node(get_nodes_block(irn));
1361 if (block->type.tv == tarval_unreachable) {
1362 node->type.tv = tarval_top;
1366 /* if any of the data inputs have type top, the result is type top */
1367 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1368 ir_node *pred = get_irn_n(irn, i);
1369 node_t *p = get_irn_node(pred);
1371 if (p->type.tv == tarval_top) {
1372 node->type.tv = tarval_top;
1377 if (get_irn_mode(node->node) == mode_X)
1378 node->type.tv = tarval_reachable;
1380 node->type.tv = computed_value(irn);
1381 } /* default_compute */
1384 * (Re-)compute the type for a Block node.
1386 * @param node the node
1388 static void compute_Block(node_t *node) {
1390 ir_node *block = node->node;
1392 if (block == get_irg_start_block(current_ir_graph)) {
1393 /* start block is always reachable */
1394 node->type.tv = tarval_reachable;
1398 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1399 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1401 if (pred->type.tv == tarval_reachable) {
1402 /* A block is reachable, if at least of predecessor is reachable. */
1403 node->type.tv = tarval_reachable;
1407 node->type.tv = tarval_top;
1408 } /* compute_Block */
1411 * (Re-)compute the type for a Bad node.
1413 * @param node the node
1415 static void compute_Bad(node_t *node) {
1416 /* Bad nodes ALWAYS compute Top */
1417 node->type.tv = tarval_top;
1421 * (Re-)compute the type for an Unknown node.
1423 * @param node the node
1425 static void compute_Unknown(node_t *node) {
1426 /* While Unknown nodes should compute Top this is dangerous:
1427 * a Top input to a Cond would lead to BOTH control flows unreachable.
1428 * While this is correct in the given semantics, it would destroy the Firm
1431 * It would be safe to compute Top IF it can be assured, that only Cmp
1432 * nodes are inputs to Conds. We check that first.
1433 * This is the way Frontends typically build Firm, but some optimizations
1434 * (cond_eval for instance) might replace them by Phib's...
1436 * For now, we compute bottom here.
1438 node->type.tv = tarval_bottom;
1439 } /* compute_Unknown */
1442 * (Re-)compute the type for a Jmp node.
1444 * @param node the node
1446 static void compute_Jmp(node_t *node) {
1447 node_t *block = get_irn_node(get_nodes_block(node->node));
1449 node->type = block->type;
1453 * (Re-)compute the type for the End node.
1455 * @param node the node
1457 static void compute_End(node_t *node) {
1458 /* the End node is NOT dead of course */
1459 node->type.tv = tarval_reachable;
1463 * (Re-)compute the type for a SymConst node.
1465 * @param node the node
1467 static void compute_SymConst(node_t *node) {
1468 ir_node *irn = node->node;
1469 node_t *block = get_irn_node(get_nodes_block(irn));
1471 if (block->type.tv == tarval_unreachable) {
1472 node->type.tv = tarval_top;
1475 switch (get_SymConst_kind(irn)) {
1476 case symconst_addr_ent:
1477 /* case symconst_addr_name: cannot handle this yet */
1478 node->type.sym = get_SymConst_symbol(irn);
1481 node->type.tv = computed_value(irn);
1483 } /* compute_SymConst */
1486 * (Re-)compute the type for a Phi node.
1488 * @param node the node
1490 static void compute_Phi(node_t *node) {
1492 ir_node *phi = node->node;
1493 lattice_elem_t type;
1495 /* if a Phi is in a unreachable block, its type is TOP */
1496 node_t *block = get_irn_node(get_nodes_block(phi));
1498 if (block->type.tv == tarval_unreachable) {
1499 node->type.tv = tarval_top;
1503 /* Phi implements the Meet operation */
1504 type.tv = tarval_top;
1505 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1506 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1507 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1509 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1510 /* ignore TOP inputs: We must check here for unreachable blocks,
1511 because Firm constants live in the Start Block are NEVER Top.
1512 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1513 comes from a unreachable input. */
1516 if (pred->type.tv == tarval_bottom) {
1517 node->type.tv = tarval_bottom;
1519 } else if (type.tv == tarval_top) {
1520 /* first constant found */
1522 } else if (type.tv != pred->type.tv) {
1523 /* different constants or tarval_bottom */
1524 node->type.tv = tarval_bottom;
1527 /* else nothing, constants are the same */
1533 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1535 * @param node the node
1537 static void compute_Add(node_t *node) {
1538 ir_node *sub = node->node;
1539 node_t *l = get_irn_node(get_Add_left(sub));
1540 node_t *r = get_irn_node(get_Add_right(sub));
1541 lattice_elem_t a = l->type;
1542 lattice_elem_t b = r->type;
1545 if (a.tv == tarval_top || b.tv == tarval_top) {
1546 node->type.tv = tarval_top;
1547 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1548 node->type.tv = tarval_bottom;
1550 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1551 must call tarval_add() first to handle this case! */
1552 if (is_tarval(a.tv)) {
1553 if (is_tarval(b.tv)) {
1554 node->type.tv = tarval_add(a.tv, b.tv);
1557 mode = get_tarval_mode(a.tv);
1558 if (a.tv == get_mode_null(mode)) {
1562 } else if (is_tarval(b.tv)) {
1563 mode = get_tarval_mode(b.tv);
1564 if (b.tv == get_mode_null(mode)) {
1569 node->type.tv = tarval_bottom;
1574 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1576 * @param node the node
1578 static void compute_Sub(node_t *node) {
1579 ir_node *sub = node->node;
1580 node_t *l = get_irn_node(get_Sub_left(sub));
1581 node_t *r = get_irn_node(get_Sub_right(sub));
1582 lattice_elem_t a = l->type;
1583 lattice_elem_t b = r->type;
1586 if (a.tv == tarval_top || b.tv == tarval_top) {
1587 node->type.tv = tarval_top;
1588 } else if (is_con(a) && is_con(b)) {
1589 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1590 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1591 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1593 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1596 node->type.tv = tarval_bottom;
1598 node->by_all_const = 1;
1599 } else if (r->part == l->part &&
1600 (!mode_is_float(get_irn_mode(l->node)))) {
1602 * BEWARE: a - a is NOT always 0 for floating Point values, as
1603 * NaN op NaN = NaN, so we must check this here.
1605 ir_mode *mode = get_irn_mode(sub);
1606 tv = get_mode_null(mode);
1608 /* if the node was ONCE evaluated by all constants, but now
1609 this breakes AND we cat by partition a different result, switch to bottom.
1610 This happens because initially all nodes are in the same partition ... */
1611 if (node->by_all_const && node->type.tv != tv)
1615 node->type.tv = tarval_bottom;
1620 * (Re-)compute the type for Cmp.
1622 * @param node the node
1624 static void compute_Cmp(node_t *node) {
1625 ir_node *cmp = node->node;
1626 node_t *l = get_irn_node(get_Cmp_left(cmp));
1627 node_t *r = get_irn_node(get_Cmp_right(cmp));
1628 lattice_elem_t a = l->type;
1629 lattice_elem_t b = r->type;
1631 if (a.tv == tarval_top || b.tv == tarval_top) {
1632 node->type.tv = tarval_top;
1633 } else if (is_con(a) && is_con(b)) {
1634 /* both nodes are constants, we can probably do something */
1635 node->type.tv = tarval_b_true;
1636 } else if (r->part == l->part) {
1637 /* both nodes congruent, we can probably do something */
1638 node->type.tv = tarval_b_true;
1640 node->type.tv = tarval_bottom;
1642 } /* compute_Proj_Cmp */
1645 * (Re-)compute the type for a Proj(Cmp).
1647 * @param node the node
1648 * @param cond the predecessor Cmp node
1650 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1651 ir_node *proj = node->node;
1652 node_t *l = get_irn_node(get_Cmp_left(cmp));
1653 node_t *r = get_irn_node(get_Cmp_right(cmp));
1654 lattice_elem_t a = l->type;
1655 lattice_elem_t b = r->type;
1656 pn_Cmp pnc = get_Proj_proj(proj);
1659 if (a.tv == tarval_top || b.tv == tarval_top) {
1660 node->type.tv = tarval_top;
1661 } else if (is_con(a) && is_con(b)) {
1662 default_compute(node);
1663 node->by_all_const = 1;
1664 } else if (r->part == l->part &&
1665 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1667 * BEWARE: a == a is NOT always True for floating Point values, as
1668 * NaN != NaN is defined, so we must check this here.
1670 tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1672 /* if the node was ONCE evaluated by all constants, but now
1673 this breakes AND we cat by partition a different result, switch to bottom.
1674 This happens because initially all nodes are in the same partition ... */
1675 if (node->by_all_const && node->type.tv != tv)
1679 node->type.tv = tarval_bottom;
1681 } /* compute_Proj_Cmp */
1684 * (Re-)compute the type for a Proj(Cond).
1686 * @param node the node
1687 * @param cond the predecessor Cond node
1689 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1690 ir_node *proj = node->node;
1691 long pnc = get_Proj_proj(proj);
1692 ir_node *sel = get_Cond_selector(cond);
1693 node_t *selector = get_irn_node(sel);
1695 if (get_irn_mode(sel) == mode_b) {
1697 if (pnc == pn_Cond_true) {
1698 if (selector->type.tv == tarval_b_false) {
1699 node->type.tv = tarval_unreachable;
1700 } else if (selector->type.tv == tarval_b_true) {
1701 node->type.tv = tarval_reachable;
1702 } else if (selector->type.tv == tarval_bottom) {
1703 node->type.tv = tarval_reachable;
1705 assert(selector->type.tv == tarval_top);
1706 node->type.tv = tarval_unreachable;
1709 assert(pnc == pn_Cond_false);
1711 if (selector->type.tv == tarval_b_false) {
1712 node->type.tv = tarval_reachable;
1713 } else if (selector->type.tv == tarval_b_true) {
1714 node->type.tv = tarval_unreachable;
1715 } else if (selector->type.tv == tarval_bottom) {
1716 node->type.tv = tarval_reachable;
1718 assert(selector->type.tv == tarval_top);
1719 node->type.tv = tarval_unreachable;
1724 if (selector->type.tv == tarval_bottom) {
1725 node->type.tv = tarval_reachable;
1726 } else if (selector->type.tv == tarval_top) {
1727 node->type.tv = tarval_unreachable;
1729 long value = get_tarval_long(selector->type.tv);
1730 if (pnc == get_Cond_defaultProj(cond)) {
1731 /* default switch, have to check ALL other cases */
1734 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1735 ir_node *succ = get_irn_out(cond, i);
1739 if (value == get_Proj_proj(succ)) {
1740 /* we found a match, will NOT take the default case */
1741 node->type.tv = tarval_unreachable;
1745 /* all cases checked, no match, will take default case */
1746 node->type.tv = tarval_reachable;
1749 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1753 } /* compute_Proj_Cond */
1756 * (Re-)compute the type for a Proj-Node.
1758 * @param node the node
1760 static void compute_Proj(node_t *node) {
1761 ir_node *proj = node->node;
1762 ir_mode *mode = get_irn_mode(proj);
1763 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1764 ir_node *pred = get_Proj_pred(proj);
1766 if (block->type.tv == tarval_unreachable) {
1767 /* a Proj in a unreachable Block stay Top */
1768 node->type.tv = tarval_top;
1771 if (get_irn_node(pred)->type.tv == tarval_top) {
1772 /* if the predecessor is Top, its Proj follow */
1773 node->type.tv = tarval_top;
1777 if (mode == mode_M) {
1778 /* mode M is always bottom */
1779 node->type.tv = tarval_bottom;
1782 if (mode != mode_X) {
1784 compute_Proj_Cmp(node, pred);
1786 default_compute(node);
1789 /* handle mode_X nodes */
1791 switch (get_irn_opcode(pred)) {
1793 /* the Proj_X from the Start is always reachable.
1794 However this is already handled at the top. */
1795 node->type.tv = tarval_reachable;
1798 compute_Proj_Cond(node, pred);
1801 default_compute(node);
1803 } /* compute_Proj */
1806 * (Re-)compute the type for a Confirm.
1808 * @param node the node
1810 static void compute_Confirm(node_t *node) {
1811 ir_node *confirm = node->node;
1812 node_t *pred = get_irn_node(get_Confirm_value(confirm));
1814 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1815 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1817 if (is_con(bound->type)) {
1818 /* is equal to a constant */
1819 node->type = bound->type;
1823 /* a Confirm is a copy OR a Const */
1824 node->type = pred->type;
1825 } /* compute_Confirm */
1828 * (Re-)compute the type for a Max.
1830 * @param node the node
1832 static void compute_Max(node_t *node) {
1833 ir_node *op = node->node;
1834 node_t *l = get_irn_node(get_binop_left(op));
1835 node_t *r = get_irn_node(get_binop_right(op));
1836 lattice_elem_t a = l->type;
1837 lattice_elem_t b = r->type;
1839 if (a.tv == tarval_top || b.tv == tarval_top) {
1840 node->type.tv = tarval_top;
1841 } else if (is_con(a) && is_con(b)) {
1842 /* both nodes are constants, we can probably do something */
1844 /* this case handles symconsts as well */
1847 ir_mode *mode = get_irn_mode(op);
1848 tarval *tv_min = get_mode_min(mode);
1852 else if (b.tv == tv_min)
1854 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1855 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1856 node->type.tv = a.tv;
1858 node->type.tv = b.tv;
1860 node->type.tv = tarval_bad;
1863 } else if (r->part == l->part) {
1864 /* both nodes congruent, we can probably do something */
1867 node->type.tv = tarval_bottom;
1872 * (Re-)compute the type for a Min.
1874 * @param node the node
1876 static void compute_Min(node_t *node) {
1877 ir_node *op = node->node;
1878 node_t *l = get_irn_node(get_binop_left(op));
1879 node_t *r = get_irn_node(get_binop_right(op));
1880 lattice_elem_t a = l->type;
1881 lattice_elem_t b = r->type;
1883 if (a.tv == tarval_top || b.tv == tarval_top) {
1884 node->type.tv = tarval_top;
1885 } else if (is_con(a) && is_con(b)) {
1886 /* both nodes are constants, we can probably do something */
1888 /* this case handles symconsts as well */
1891 ir_mode *mode = get_irn_mode(op);
1892 tarval *tv_max = get_mode_max(mode);
1896 else if (b.tv == tv_max)
1898 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1899 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1900 node->type.tv = a.tv;
1902 node->type.tv = b.tv;
1904 node->type.tv = tarval_bad;
1907 } else if (r->part == l->part) {
1908 /* both nodes congruent, we can probably do something */
1911 node->type.tv = tarval_bottom;
1916 * (Re-)compute the type for a given node.
1918 * @param node the node
1920 static void compute(node_t *node) {
1923 if (is_no_Block(node->node)) {
1924 node_t *block = get_irn_node(get_nodes_block(node->node));
1926 if (block->type.tv == tarval_unreachable) {
1927 node->type.tv = tarval_top;
1932 func = (compute_func)node->node->op->ops.generic;
1938 * Identity functions: Note that one might thing that identity() is just a
1939 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
1940 * here, because it expects that the identity node is one of the inputs, which is NOT
1941 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
1942 * So, we have our own implementation, which copies some parts of equivalent_node()
1946 * Calculates the Identity for Phi nodes
1948 static node_t *identity_Phi(node_t *node) {
1949 ir_node *phi = node->node;
1950 ir_node *block = get_nodes_block(phi);
1951 node_t *n_part = NULL;
1954 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1955 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
1957 if (pred_X->type.tv == tarval_reachable) {
1958 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1962 else if (n_part->part != pred->part) {
1963 /* incongruent inputs, not a follower */
1968 /* if n_part is NULL here, all inputs path are dead, the Phi computes
1969 * tarval_top, is in the TOP partition and should NOT being split! */
1970 assert(n_part != NULL);
1972 } /* identity_Phi */
1975 * Calculates the Identity for commutative 0 neutral nodes.
1977 static node_t *identity_comm_zero_binop(node_t *node) {
1978 ir_node *op = node->node;
1979 node_t *a = get_irn_node(get_binop_left(op));
1980 node_t *b = get_irn_node(get_binop_right(op));
1981 ir_mode *mode = get_irn_mode(op);
1984 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1985 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1988 /* node: no input should be tarval_top, else the binop would be also
1989 * Top and not being split. */
1990 zero = get_mode_null(mode);
1991 if (a->type.tv == zero)
1993 if (b->type.tv == zero)
1996 } /* identity_comm_zero_binop */
1998 #define identity_Add identity_comm_zero_binop
1999 #define identity_Or identity_comm_zero_binop
2002 * Calculates the Identity for Mul nodes.
2004 static node_t *identity_Mul(node_t *node) {
2005 ir_node *op = node->node;
2006 node_t *a = get_irn_node(get_Mul_left(op));
2007 node_t *b = get_irn_node(get_Mul_right(op));
2008 ir_mode *mode = get_irn_mode(op);
2011 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2012 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2015 /* node: no input should be tarval_top, else the binop would be also
2016 * Top and not being split. */
2017 one = get_mode_one(mode);
2018 if (a->type.tv == one)
2020 if (b->type.tv == one)
2023 } /* identity_Mul */
2026 * Calculates the Identity for Sub nodes.
2028 static node_t *identity_Sub(node_t *node) {
2029 ir_node *sub = node->node;
2030 node_t *b = get_irn_node(get_Sub_right(sub));
2031 ir_mode *mode = get_irn_mode(sub);
2033 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2034 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2037 /* node: no input should be tarval_top, else the binop would be also
2038 * Top and not being split. */
2039 if (b->type.tv == get_mode_null(mode))
2040 return get_irn_node(get_Sub_left(sub));
2042 } /* identity_Mul */
2045 * Calculates the Identity for And nodes.
2047 static node_t *identity_And(node_t *node) {
2048 ir_node *and = node->node;
2049 node_t *a = get_irn_node(get_And_left(and));
2050 node_t *b = get_irn_node(get_And_right(and));
2051 tarval *neutral = get_mode_all_one(get_irn_mode(and));
2053 /* node: no input should be tarval_top, else the And would be also
2054 * Top and not being split. */
2055 if (a->type.tv == neutral)
2057 if (b->type.tv == neutral)
2060 } /* identity_And */
2063 * Calculates the Identity for Confirm nodes.
2065 static node_t *identity_Confirm(node_t *node) {
2066 ir_node *confirm = node->node;
2068 /* a Confirm is always a Copy */
2069 return get_irn_node(get_Confirm_value(confirm));
2070 } /* identity_Confirm */
2073 * Calculates the Identity for Mux nodes.
2075 static node_t *identity_Mux(node_t *node) {
2076 ir_node *mux = node->node;
2077 node_t *sel = get_irn_node(get_Mux_sel(mux));
2078 node_t *t = get_irn_node(get_Mux_true(mux));
2079 node_t *f = get_irn_node(get_Mux_false(mux));
2081 if (t->part == f->part)
2084 /* Mux sel input is mode_b, so it is always a tarval */
2085 if (sel->type.tv == tarval_b_true)
2087 if (sel->type.tv == tarval_b_false)
2090 } /* identity_Mux */
2093 * Calculates the Identity for Min nodes.
2095 static node_t *identity_Min(node_t *node) {
2096 ir_node *op = node->node;
2097 node_t *a = get_irn_node(get_binop_left(op));
2098 node_t *b = get_irn_node(get_binop_right(op));
2099 ir_mode *mode = get_irn_mode(op);
2102 if (a->part == b->part) {
2103 /* leader of multiple predecessors */
2107 /* works even with NaN */
2108 tv_max = get_mode_max(mode);
2109 if (a->type.tv == tv_max)
2111 if (b->type.tv == tv_max)
2114 } /* identity_Min */
2117 * Calculates the Identity for Max nodes.
2119 static node_t *identity_Max(node_t *node) {
2120 ir_node *op = node->node;
2121 node_t *a = get_irn_node(get_binop_left(op));
2122 node_t *b = get_irn_node(get_binop_right(op));
2123 ir_mode *mode = get_irn_mode(op);
2126 if (a->part == b->part) {
2127 /* leader of multiple predecessors */
2131 /* works even with NaN */
2132 tv_min = get_mode_min(mode);
2133 if (a->type.tv == tv_min)
2135 if (b->type.tv == tv_min)
2138 } /* identity_Max */
2141 * Calculates the Identity for nodes.
2143 static node_t *identity(node_t *node) {
2144 ir_node *irn = node->node;
2146 switch (get_irn_opcode(irn)) {
2148 return identity_Phi(node);
2150 return identity_Add(node);
2152 return identity_Mul(node);
2154 return identity_Or(node);
2156 return identity_And(node);
2158 return identity_Sub(node);
2160 return identity_Confirm(node);
2162 return identity_Mux(node);
2164 return identity_Min(node);
2166 return identity_Max(node);
2173 * Node follower is a (new) follower of leader, segregate Leader
2176 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2177 ir_node *l = leader->node;
2178 int j, i, n = get_irn_n_outs(l);
2180 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2181 /* The leader edges must remain sorted, but follower edges can
2183 for (i = leader->n_followers + 1; i <= n; ++i) {
2184 if (l->out[i].use == follower) {
2185 ir_def_use_edge t = l->out[i];
2187 for (j = i - 1; j >= leader->n_followers + 1; --j)
2188 l->out[j + 1] = l->out[j];
2189 ++leader->n_followers;
2190 l->out[leader->n_followers] = t;
2194 } /* segregate_def_use_chain_1 */
2197 * Node follower is a (new) follower of leader, segregate Leader
2198 * out edges. If follower is a n-congruent Input identity, all follower
2199 * inputs congruent to follower are also leader.
2201 * @param follower the follower IR node
2203 static void segregate_def_use_chain(const ir_node *follower) {
2206 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2207 node_t *pred = get_irn_node(get_irn_n(follower, i));
2209 segregate_def_use_chain_1(follower, pred);
2211 } /* segregate_def_use_chain */
2214 * Propagate constant evaluation.
2216 * @param env the environment
2218 static void propagate(environment_t *env) {
2221 lattice_elem_t old_type;
2223 unsigned n_fallen, old_type_was_T_or_C;
2226 while (env->cprop != NULL) {
2227 void *oldopcode = NULL;
2229 /* remove the first partition X from cprop */
2232 env->cprop = X->cprop_next;
2234 old_type_was_T_or_C = X->type_is_T_or_C;
2236 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2239 while (! list_empty(&X->cprop)) {
2240 /* remove the first Node x from X.cprop */
2241 x = list_entry(X->cprop.next, node_t, cprop_list);
2242 assert(x->part == X);
2243 list_del(&x->cprop_list);
2246 if (x->is_follower && identity(x) == x) {
2247 /* x will make the follower -> leader transition */
2248 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", x->node));
2250 if (oldopcode == NULL) {
2251 oldopcode = lambda_opcode(get_first_node(X), env);
2253 if (oldopcode != lambda_opcode(x, env)) {
2254 if (x->on_fallen == 0) {
2255 /* different opcode -> x falls out of this partition */
2260 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2264 /* move x from X.Follower to X.Leader */
2265 list_del(&x->node_list);
2266 list_add_tail(&x->node_list, &X->Leader);
2270 /* Make all inputs to x from inside X no longer be F.def_use edges */
2271 move_edges_to_leader(x);
2274 /* compute a new type for x */
2276 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2278 if (x->type.tv != old_type.tv) {
2279 verify_type(old_type, x->type);
2280 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2282 if (x->on_fallen == 0) {
2283 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2284 not already on the list. */
2289 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2291 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2292 ir_node *succ = get_irn_out(x->node, i);
2293 node_t *y = get_irn_node(succ);
2295 /* Add y to y.partition.cprop. */
2296 add_to_cprop(y, env);
2301 if (n_fallen > 0 && n_fallen != X->n_leader) {
2302 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2303 Y = split(&X, fallen, env);
2307 /* remove the flags from the fallen list */
2308 for (x = fallen; x != NULL; x = x->next)
2312 if (old_type_was_T_or_C) {
2315 if (Y->on_worklist == 0)
2316 add_to_worklist(Y, env);
2318 /* check if some nodes will make the leader -> follower transition */
2319 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2320 if (y->type.tv != tarval_top && ! is_con(y->type)) {
2321 node_t *eq_node = identity(y);
2323 if (eq_node != y && eq_node->part == y->part) {
2324 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2325 /* move to Follower */
2327 list_del(&y->node_list);
2328 list_add_tail(&y->node_list, &Y->Follower);
2331 segregate_def_use_chain(y->node);
2342 * Get the leader for a given node from its congruence class.
2344 * @param irn the node
2346 static ir_node *get_leader(node_t *node) {
2347 partition_t *part = node->part;
2349 if (part->n_leader > 1 || node->is_follower) {
2350 if (node->is_follower)
2351 DB((dbg, LEVEL_2, "Replacing follower %+F\n", node->node));
2353 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2355 return get_first_node(part)->node;
2361 * Return non-zero if the control flow predecessor node pred
2362 * is the only reachable control flow exit of its block.
2364 * @param pred the control flow exit
2366 static int can_exchange(ir_node *pred) {
2369 else if (is_Jmp(pred))
2371 else if (get_irn_mode(pred) == mode_T) {
2374 /* if the predecessor block has more than one
2375 reachable outputs we cannot remove the block */
2377 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2378 ir_node *proj = get_irn_out(pred, i);
2381 /* skip non-control flow Proj's */
2382 if (get_irn_mode(proj) != mode_X)
2385 node = get_irn_node(proj);
2386 if (node->type.tv == tarval_reachable) {
2397 * Block Post-Walker, apply the analysis results on control flow by
2398 * shortening Phi's and Block inputs.
2400 static void apply_cf(ir_node *block, void *ctx) {
2401 environment_t *env = ctx;
2402 node_t *node = get_irn_node(block);
2404 ir_node **ins, **in_X;
2405 ir_node *phi, *next;
2407 if (block == get_irg_end_block(current_ir_graph) ||
2408 block == get_irg_start_block(current_ir_graph)) {
2409 /* the EndBlock is always reachable even if the analysis
2410 finds out the opposite :-) */
2413 if (node->type.tv == tarval_unreachable) {
2414 /* mark dead blocks */
2415 set_Block_dead(block);
2419 n = get_Block_n_cfgpreds(block);
2422 /* only one predecessor combine */
2423 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2425 if (can_exchange(pred)) {
2426 exchange(block, get_nodes_block(pred));
2432 NEW_ARR_A(ir_node *, in_X, n);
2434 for (i = 0; i < n; ++i) {
2435 ir_node *pred = get_Block_cfgpred(block, i);
2436 node_t *node = get_irn_node(pred);
2438 if (node->type.tv == tarval_reachable) {
2445 NEW_ARR_A(ir_node *, ins, n);
2446 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2447 node_t *node = get_irn_node(phi);
2449 next = get_Phi_next(phi);
2450 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2451 /* this Phi is replaced by a constant */
2452 tarval *tv = node->type.tv;
2453 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2455 set_irn_node(c, node);
2457 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2462 for (i = 0; i < n; ++i) {
2463 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2465 if (pred->type.tv == tarval_reachable) {
2466 ins[j++] = get_Phi_pred(phi, i);
2470 /* this Phi is replaced by a single predecessor */
2471 ir_node *s = ins[0];
2474 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2478 set_irn_in(phi, j, ins);
2485 /* this Block has only one live predecessor */
2486 ir_node *pred = skip_Proj(in_X[0]);
2488 if (can_exchange(pred)) {
2489 exchange(block, get_nodes_block(pred));
2493 set_irn_in(block, k, in_X);
2499 * Post-Walker, apply the analysis results;
2501 static void apply_result(ir_node *irn, void *ctx) {
2502 environment_t *env = ctx;
2503 node_t *node = get_irn_node(irn);
2505 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2506 /* blocks already handled, do not touch the End node */
2508 node_t *block = get_irn_node(get_nodes_block(irn));
2510 if (block->type.tv == tarval_unreachable) {
2511 ir_node *bad = get_irg_bad(current_ir_graph);
2513 /* here, bad might already have a node, but this can be safely ignored
2514 as long as bad has at least ONE valid node */
2515 set_irn_node(bad, node);
2517 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2521 else if (node->type.tv == tarval_unreachable) {
2522 ir_node *bad = get_irg_bad(current_ir_graph);
2524 /* see comment above */
2525 set_irn_node(bad, node);
2527 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2531 else if (get_irn_mode(irn) == mode_X) {
2534 ir_node *cond = get_Proj_pred(irn);
2536 if (is_Cond(cond)) {
2537 node_t *sel = get_irn_node(get_Cond_selector(cond));
2539 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2540 /* Cond selector is a constant, make a Jmp */
2541 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2542 set_irn_node(jmp, node);
2544 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2551 /* normal data node */
2552 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2553 tarval *tv = node->type.tv;
2556 * Beware: never replace mode_T nodes by constants. Currently we must mark
2557 * mode_T nodes with constants, but do NOT replace them.
2559 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2560 /* can be replaced by a constant */
2561 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2562 set_irn_node(c, node);
2564 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2568 } else if (is_entity(node->type.sym.entity_p)) {
2569 if (! is_SymConst(irn)) {
2570 /* can be replaced by a Symconst */
2571 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2572 set_irn_node(symc, node);
2575 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2576 exchange(irn, symc);
2579 } else if (is_Confirm(irn)) {
2580 /* Confirms are always follower, but do not kill them here */
2582 ir_node *leader = get_leader(node);
2584 if (leader != irn) {
2585 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2586 exchange(irn, leader);
2592 } /* apply_result */
2595 * Fix the keep-alives by deleting unreachable ones.
2597 static void apply_end(ir_node *end, environment_t *env) {
2598 int i, j, n = get_End_n_keepalives(end);
2602 NEW_ARR_A(ir_node *, in, n);
2604 /* fix the keep alive */
2605 for (i = j = 0; i < n; i++) {
2606 ir_node *ka = get_End_keepalive(end, i);
2607 node_t *node = get_irn_node(ka);
2609 /* Use the is_flagged bit to mark already visited nodes.
2610 * This should not be ready but better safe than sorry. */
2611 if (node->is_flagged == 0) {
2612 node->is_flagged = 1;
2615 node = get_irn_node(get_nodes_block(ka));
2617 if (node->type.tv != tarval_unreachable)
2622 set_End_keepalives(end, j, in);
2627 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2630 * sets the generic functions to compute.
2632 static void set_compute_functions(void) {
2635 /* set the default compute function */
2636 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2637 ir_op *op = get_irp_opcode(i);
2638 op->ops.generic = (op_func)default_compute;
2641 /* set specific functions */
2660 } /* set_compute_functions */
2662 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2663 ir_node *irn = local != NULL ? local : n;
2664 node_t *node = get_irn_node(irn);
2666 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2670 void combo(ir_graph *irg) {
2672 ir_node *initial_bl;
2674 ir_graph *rem = current_ir_graph;
2676 current_ir_graph = irg;
2678 /* register a debug mask */
2679 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2680 //firm_dbg_set_mask(dbg, SET_LEVEL_3);
2682 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2684 obstack_init(&env.obst);
2685 env.worklist = NULL;
2689 #ifdef DEBUG_libfirm
2690 env.dbg_list = NULL;
2692 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
2693 env.type2id_map = pmap_create();
2694 env.end_idx = get_opt_global_cse() ? 0 : -1;
2695 env.lambda_input = 0;
2698 assure_irg_outs(irg);
2700 /* we have our own value_of function */
2701 set_value_of_func(get_node_tarval);
2703 set_compute_functions();
2704 DEBUG_ONLY(part_nr = 0);
2706 /* create the initial partition and place it on the work list */
2707 env.initial = new_partition(&env);
2708 add_to_worklist(env.initial, &env);
2709 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2711 /* all nodes on the initial partition have type Top */
2712 env.initial->type_is_T_or_C = 1;
2714 /* Place the START Node's partition on cprop.
2715 Place the START Node on its local worklist. */
2716 initial_bl = get_irg_start_block(irg);
2717 start = get_irn_node(initial_bl);
2718 add_to_cprop(start, &env);
2722 if (env.worklist != NULL)
2724 } while (env.cprop != NULL || env.worklist != NULL);
2726 dump_all_partitions(&env);
2729 set_dump_node_vcgattr_hook(dump_partition_hook);
2730 dump_ir_block_graph(irg, "-partition");
2731 set_dump_node_vcgattr_hook(NULL);
2733 (void)dump_partition_hook;
2736 /* apply the result */
2737 irg_block_walk_graph(irg, NULL, apply_cf, &env);
2738 irg_walk_graph(irg, NULL, apply_result, &env);
2739 apply_end(get_irg_end(irg), &env);
2742 /* control flow might changed */
2743 set_irg_outs_inconsistent(irg);
2744 set_irg_extblk_inconsistent(irg);
2745 set_irg_doms_inconsistent(irg);
2746 set_irg_loopinfo_inconsistent(irg);
2749 pmap_destroy(env.type2id_map);
2750 del_set(env.opcode2id_map);
2751 obstack_free(&env.obst, NULL);
2753 /* restore value_of() default behavior */
2754 set_value_of_func(NULL);
2755 current_ir_graph = rem;