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(). */
130 * A partition containing congruent nodes.
133 list_head Leader; /**< The head of partition Leader node list. */
134 list_head Follower; /**< The head of partition Follower node list. */
135 list_head cprop; /**< The head of partition.cprop list. */
136 partition_t *wl_next; /**< Next entry in the work list if any. */
137 partition_t *touched_next; /**< Points to the next partition in the touched set. */
138 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
139 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
140 node_t *touched; /**< The partition.touched set of this partition. */
141 unsigned n_leader; /**< Number of entries in this partition.Leader. */
142 unsigned n_touched; /**< Number of entries in the partition.touched. */
143 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
144 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
145 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
146 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
147 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
149 partition_t *dbg_next; /**< Link all partitions for debugging */
150 unsigned nr; /**< A unique number for (what-)mapping, >0. */
154 typedef struct environment_t {
155 struct obstack obst; /**< obstack to allocate data structures. */
156 partition_t *worklist; /**< The work list. */
157 partition_t *cprop; /**< The constant propagation list. */
158 partition_t *touched; /**< the touched set. */
159 partition_t *initial; /**< The initial partition. */
160 set *opcode2id_map; /**< The opcodeMode->id map. */
161 pmap *type2id_map; /**< The type->id map. */
162 int end_idx; /**< -1 for local and 0 for global congruences. */
163 int lambda_input; /**< Captured argument for lambda_partition(). */
164 int modified; /**< Set, if the graph was modified. */
166 partition_t *dbg_list; /**< List of all partitions. */
170 /** Type of the what function. */
171 typedef void *(*what_func)(const node_t *node, environment_t *env);
173 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
174 #define set_irn_node(follower, node) set_irn_link(follower, node)
176 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
177 #undef tarval_unreachable
178 #define tarval_unreachable tarval_top
181 /** The debug module handle. */
182 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
184 /** Next partition number. */
185 DEBUG_ONLY(static unsigned part_nr = 0);
188 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
191 * Dump partition to output.
193 static void dump_partition(const char *msg, const partition_t *part) {
196 lattice_elem_t type = get_partition_type(part);
198 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
199 msg, part->nr, part->type_is_T_or_C ? "*" : "",
200 part->n_leader, type));
201 list_for_each_entry(node_t, node, &part->Leader, node_list) {
202 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
205 if (! list_empty(&part->Follower)) {
206 DB((dbg, LEVEL_2, "\n---\n "));
208 list_for_each_entry(node_t, node, &part->Follower, node_list) {
209 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
213 DB((dbg, LEVEL_2, "\n}\n"));
214 } /* dump_partition */
219 static void do_dump_list(const char *msg, const node_t *node, int ofs) {
223 #define GET_LINK(p, ofs) *((const node_t **)((char *)(p) + (ofs)))
225 DB((dbg, LEVEL_3, "%s = {\n ", msg));
226 for (p = node; p != NULL; p = GET_LINK(p, ofs)) {
227 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->node));
230 DB((dbg, LEVEL_3, "\n}\n"));
238 static void dump_race_list(const char *msg, const node_t *list) {
239 do_dump_list(msg, list, offsetof(node_t, race_next));
243 * Dumps a local list.
245 static void dump_list(const char *msg, const node_t *list) {
246 do_dump_list(msg, list, offsetof(node_t, next));
250 * Dump all partitions.
252 static void dump_all_partitions(const environment_t *env) {
253 const partition_t *P;
255 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
256 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
257 dump_partition("", P);
261 #define dump_partition(msg, part)
262 #define dump_race_list(msg, list)
263 #define dump_list(msg, list)
264 #define dump_all_partitions(env)
267 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
269 * Verify that a type transition is monotone
271 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
272 if (old_type.tv == new_type.tv) {
276 if (old_type.tv == tarval_top) {
277 /* from Top down-to is always allowed */
280 if (old_type.tv == tarval_reachable) {
281 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
283 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
287 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
290 #define verify_type(old_type, new_type)
294 * Compare two pointer values of a listmap.
296 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
297 const listmap_entry_t *e1 = elt;
298 const listmap_entry_t *e2 = key;
301 return e1->id != e2->id;
302 } /* listmap_cmp_ptr */
305 * Initializes a listmap.
307 * @param map the listmap
309 static void listmap_init(listmap_t *map) {
310 map->map = new_set(listmap_cmp_ptr, 16);
315 * Terminates a listmap.
317 * @param map the listmap
319 static void listmap_term(listmap_t *map) {
324 * Return the associated listmap entry for a given id.
326 * @param map the listmap
327 * @param id the id to search for
329 * @return the asociated listmap entry for the given id
331 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
332 listmap_entry_t key, *entry;
337 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
339 if (entry->list == NULL) {
340 /* a new entry, put into the list */
341 entry->next = map->values;
348 * Calculate the hash value for an opcode map entry.
350 * @param entry an opcode map entry
352 * @return a hash value for the given opcode map entry
354 static unsigned opcode_hash(const opcode_key_t *entry) {
355 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
359 * Compare two entries in the opcode map.
361 static int cmp_opcode(const void *elt, const void *key, size_t size) {
362 const opcode_key_t *o1 = elt;
363 const opcode_key_t *o2 = key;
366 return o1->code != o2->code || o1->mode != o2->mode ||
367 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
371 * Compare two Def-Use edges for input position.
373 static int cmp_def_use_edge(const void *a, const void *b) {
374 const ir_def_use_edge *ea = a;
375 const ir_def_use_edge *eb = b;
377 /* no overrun, because range is [-1, MAXINT] */
378 return ea->pos - eb->pos;
379 } /* cmp_def_use_edge */
382 * We need the Def-Use edges sorted.
384 static void sort_irn_outs(node_t *node) {
385 ir_node *irn = node->node;
386 int n_outs = get_irn_n_outs(irn);
389 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
391 node->max_user_input = irn->out[n_outs].pos;
392 } /* sort_irn_outs */
395 * Return the type of a node.
397 * @param irn an IR-node
399 * @return the associated type of this node
401 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
402 return get_irn_node(irn)->type;
403 } /* get_node_type */
406 * Return the tarval of a node.
408 * @param irn an IR-node
410 * @return the associated type of this node
412 static INLINE tarval *get_node_tarval(const ir_node *irn) {
413 lattice_elem_t type = get_node_type(irn);
415 if (is_tarval(type.tv))
417 return tarval_bottom;
418 } /* get_node_type */
421 * Add a partition to the worklist.
423 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
424 assert(X->on_worklist == 0);
425 X->wl_next = env->worklist;
428 } /* add_to_worklist */
431 * Create a new empty partition.
433 * @param env the environment
435 * @return a newly allocated partition
437 static INLINE partition_t *new_partition(environment_t *env) {
438 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
440 INIT_LIST_HEAD(&part->Leader);
441 INIT_LIST_HEAD(&part->Follower);
442 INIT_LIST_HEAD(&part->cprop);
443 part->wl_next = NULL;
444 part->touched_next = NULL;
445 part->cprop_next = NULL;
446 part->split_next = NULL;
447 part->touched = NULL;
450 part->max_user_inputs = 0;
451 part->on_worklist = 0;
452 part->on_touched = 0;
454 part->type_is_T_or_C = 0;
456 part->dbg_next = env->dbg_list;
457 env->dbg_list = part;
458 part->nr = part_nr++;
462 } /* new_partition */
465 * Get the first node from a partition.
467 static INLINE node_t *get_first_node(const partition_t *X) {
468 return list_entry(X->Leader.next, node_t, node_list);
472 * Return the type of a partition (assuming partition is non-empty and
473 * all elements have the same type).
475 * @param X a partition
477 * @return the type of the first element of the partition
479 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
480 const node_t *first = get_first_node(X);
482 } /* get_partition_type */
485 * Creates a partition node for the given IR-node and place it
486 * into the given partition.
488 * @param irn an IR-node
489 * @param part a partition to place the node in
490 * @param env the environment
492 * @return the created node
494 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
495 /* create a partition node and place it in the partition */
496 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
498 INIT_LIST_HEAD(&node->node_list);
499 INIT_LIST_HEAD(&node->cprop_list);
503 node->race_next = NULL;
504 node->type.tv = tarval_top;
505 node->max_user_input = 0;
507 node->n_followers = 0;
508 node->on_touched = 0;
511 node->is_follower = 0;
512 node->is_flagged = 0;
513 set_irn_node(irn, node);
515 list_add_tail(&node->node_list, &part->Leader);
519 } /* create_partition_node */
522 * Pre-Walker, init all Block-Phi lists.
524 static void init_block_phis(ir_node *irn, void *env) {
528 set_Block_phis(irn, NULL);
533 * Post-Walker, initialize all Nodes' type to U or top and place
534 * all nodes into the TOP partition.
536 static void create_initial_partitions(ir_node *irn, void *ctx) {
537 environment_t *env = ctx;
538 partition_t *part = env->initial;
541 node = create_partition_node(irn, part, env);
543 if (node->max_user_input > part->max_user_inputs)
544 part->max_user_inputs = node->max_user_input;
547 add_Block_phi(get_nodes_block(irn), irn);
549 } /* create_initial_partitions */
552 * Add a partition to the touched set if not already there.
554 * @param part the partition
555 * @param env the environment
557 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
558 if (part->on_touched == 0) {
559 part->touched_next = env->touched;
561 part->on_touched = 1;
563 } /* add_to_touched */
566 * Add a node to the entry.partition.touched set if not already there.
570 static INLINE void add_to_partition_touched(node_t *y) {
571 if (y->on_touched == 0) {
572 partition_t *part = y->part;
574 y->next = part->touched;
579 } /* add_to_partition_touched */
582 * Update the worklist: If Z is on worklist then add Z' to worklist.
583 * Else add the smaller of Z and Z' to worklist.
585 * @param Z the Z partition
586 * @param Z_prime the Z' partition, a previous part of Z
587 * @param env the environment
589 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
590 if (Z->on_worklist || Z_prime->n_leader < Z->n_leader) {
591 add_to_worklist(Z_prime, env);
593 add_to_worklist(Z, env);
595 } /* update_worklist */
599 * Split a partition by a local list.
601 * @param Z the Z partition to split
602 * @param g a (non-empty) node list
603 * @param env the environment
605 * @return a new partition containing the nodes of g
607 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
608 partition_t *Z_prime;
613 dump_partition("Splitting ", Z);
617 /* Remove g from Z. */
618 for (node = g; node != NULL; node = node->next) {
619 list_del(&node->node_list);
622 assert(n < Z->n_leader);
625 /* Move g to a new partition, Z
\92. */
626 Z_prime = new_partition(env);
628 for (node = g; node != NULL; node = node->next) {
629 list_add(&node->node_list, &Z_prime->Leader);
630 node->part = Z_prime;
631 if (node->max_user_input > max_input)
632 max_input = node->max_user_input;
634 Z_prime->max_user_inputs = max_input;
635 Z_prime->n_leader = n;
637 /* for now, copy the type info tag. it will be adjusted
639 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
641 update_worklist(Z, Z_prime, env);
643 dump_partition("Now ", Z);
644 dump_partition("Created new ", Z_prime);
651 * The environment for one race step.
653 typedef struct step_env {
654 node_t *initial; /**< The initial node list. */
655 node_t *unwalked; /**< The unwalked node list. */
656 node_t *unwalked_last; /**< Points to the last element of the unwalked node list. */
657 node_t *walked; /**< The walked node list. */
658 int index; /**< Next index of Follower use_def edge. */
659 int n_leader; /**< number of Leader in initial. */
663 * Do one step in the race.
665 static int step(step_env *env) {
668 if (env->initial != NULL) {
669 /* Move node from initial to unwalked */
671 env->initial = n->race_next;
673 if (env->unwalked_last == NULL)
674 env->unwalked_last = n;
676 n->race_next = env->unwalked;
682 while (env->unwalked != NULL) {
683 /* let n be the first node in unwalked */
685 while (env->index < n->n_followers) {
686 /* let m be n.F.def_use[index] */
687 node_t *m = get_irn_node(n->node->out[1 + env->index].use);
689 assert(m->is_follower);
692 /* only followers from our partition */
693 if (m->part != n->part)
696 if (! m->is_flagged) {
699 /* add m to unwalked not as first node */
701 if (env->unwalked == NULL) {
704 env->unwalked_last->race_next = m;
706 env->unwalked_last = m;
710 /* move n to walked */
711 env->unwalked = n->race_next;
712 n->race_next = env->walked;
720 * Clear the flags from a list.
722 * @param list the list
724 static void clear_flags(node_t *list) {
727 for (n = list; n != NULL; n = n->race_next)
732 * Split a partition by a local list using the race.
734 * @param X the partition to split
735 * @param gg a (non-empty) node list
736 * @param env the environment
738 * @return a new partition containing the nodes of gg
740 static partition_t *split(partition_t *X, node_t *gg, environment_t *env) {
741 partition_t *X_prime;
743 step_env env1, env2, *winner;
744 node_t *g, *h, *node;
747 dump_partition("Splitting ", X);
748 dump_list("by list ", gg);
750 INIT_LIST_HEAD(&tmp);
752 /* Remove gg from X.Leader and put into g */
755 for (node = gg; node != NULL; node = node->next) {
756 list_del(&node->node_list);
757 list_add_tail(&node->node_list, &tmp);
765 list_for_each_entry(node_t, node, &X->Leader, node_list) {
770 /* restore X.Leader */
771 list_splice(&tmp, &X->Leader);
774 env1.unwalked = NULL;
775 env1.unwalked_last = NULL;
781 env2.unwalked = NULL;
782 env2.unwalked_last = NULL;
797 assert(winner->initial == NULL);
798 assert(winner->unwalked == NULL);
800 /* clear flags from walked/unwalked */
801 clear_flags(env1.unwalked);
802 clear_flags(env1.walked);
803 clear_flags(env2.unwalked);
804 clear_flags(env2.walked);
806 dump_race_list("winner ", winner->walked);
808 /* Move walked_{winner} to a new partition, X
\92. */
809 X_prime = new_partition(env);
811 for (node = winner->walked; node != NULL; node = node->race_next) {
812 list_del(&node->node_list);
813 if (node->is_follower) {
814 list_add(&node->node_list, &X_prime->Follower);
816 list_add(&node->node_list, &X_prime->Leader);
819 node->part = X_prime;
820 if (node->max_user_input > max_input)
821 max_input = node->max_user_input;
823 X_prime->max_user_inputs = max_input;
824 X->n_leader -= winner->n_leader;
826 /* for now, copy the type info tag. it will be adjusted
828 X_prime->type_is_T_or_C = X->type_is_T_or_C;
830 update_worklist(X, X_prime, env);
832 dump_partition("Now ", X);
833 dump_partition("Created new ", X_prime);
836 #endif /* NO_FOLLOWER */
839 * Returns non-zero if the i'th input of a Phi node is live.
841 * @param phi a Phi-node
842 * @param i an input number
844 * @return non-zero if the i'th input of the given Phi node is live
846 static int is_live_input(ir_node *phi, int i) {
848 ir_node *block = get_nodes_block(phi);
849 ir_node *pred = get_Block_cfgpred(block, i);
850 lattice_elem_t type = get_node_type(pred);
852 return type.tv != tarval_unreachable;
854 /* else it's the control input, always live */
856 } /* is_live_input */
859 * Return non-zero if a type is a constant.
861 static int is_constant_type(lattice_elem_t type) {
862 if (type.tv != tarval_bottom && type.tv != tarval_top)
865 } /* is_constant_type */
868 * Place a node on the cprop list.
871 * @param env the environment
873 static void add_node_to_cprop(node_t *y, environment_t *env) {
874 /* Add y to y.partition.cprop. */
875 if (y->on_cprop == 0) {
876 partition_t *Y = y->part;
878 list_add_tail(&y->cprop_list, &Y->cprop);
881 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
883 /* place its partition on the cprop list */
884 if (Y->on_cprop == 0) {
885 Y->cprop_next = env->cprop;
890 if (get_irn_mode(y->node) == mode_T) {
891 /* mode_T nodes always produce tarval_bottom, so we must explicitly
892 add it's Proj's to get constant evaluation to work */
895 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
896 node_t *proj = get_irn_node(get_irn_out(y->node, i));
898 add_node_to_cprop(proj, env);
902 if (is_Block(y->node)) {
903 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
904 * if someone placed the block. The Block is only placed if the reachability
905 * changes, and this must be re-evaluated in compute_Phi(). */
907 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
908 node_t *p = get_irn_node(phi);
909 add_node_to_cprop(p, env);
912 } /* add_node_to_cprop */
915 * Check whether a type is neither Top or a constant.
916 * Note: U is handled like Top here, R is a constant.
918 * @param type the type to check
920 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
921 if (is_tarval(type.tv)) {
922 if (type.tv == tarval_top)
924 if (tarval_is_constant(type.tv))
934 * Collect nodes to the touched list.
936 * @param list the list which contains the nodes that must be evaluated
937 * @param idx the index of the def_use edge to evaluate
938 * @param env the environment
940 static void collect_touched(list_head *list, int idx, environment_t *env) {
942 int end_idx = env->end_idx;
944 list_for_each_entry(node_t, x, list, node_list) {
948 /* leader edges start AFTER follower edges */
949 x->next_edge = 1 + x->n_followers;
951 num_edges = get_irn_n_outs(x->node);
953 /* for all edges in x.L.def_use_{idx} */
954 while (x->next_edge <= num_edges) {
955 ir_def_use_edge *edge = &x->node->out[x->next_edge];
958 /* check if we have necessary edges */
966 /* ignore the "control input" for non-pinned nodes
967 if we are running in GCSE mode */
968 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
971 y = get_irn_node(succ);
972 if (is_constant_type(y->type)) {
973 ir_opcode code = get_irn_opcode(succ);
974 if (code == iro_Sub || code == iro_Cmp)
975 add_node_to_cprop(y, env);
978 /* Partitions of constants should not be split simply because their Nodes have unequal
979 functions or incongruent inputs. */
980 if (type_is_neither_top_nor_const(y->type) &&
981 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
982 partition_t *Y = y->part;
983 add_to_touched(Y, env);
984 add_to_partition_touched(y);
990 * Split the partitions if caused by the first entry on the worklist.
992 * @param env the environment
994 static void cause_splits(environment_t *env) {
999 /* remove the first partition from the worklist */
1001 env->worklist = X->wl_next;
1004 dump_partition("Cause_split: ", X);
1006 /* combine temporary leader and follower list */
1007 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
1008 /* empty the touched set: already done, just clear the list */
1009 env->touched = NULL;
1011 collect_touched(&X->Leader, idx, env);
1012 collect_touched(&X->Follower, idx, env);
1014 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
1015 /* remove it from the touched set */
1018 if (Z->n_leader != Z->n_touched) {
1019 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
1020 split(Z, Z->touched, env);
1022 /* Empty local Z.touched. */
1023 for (e = Z->touched; e != NULL; e = e->next) {
1030 } /* cause_splits */
1033 * Implements split_by_what(): Split a partition by characteristics given
1034 * by the what function.
1036 * @param X the partition to split
1037 * @param What a function returning an Id for every node of the partition X
1038 * @param P a list to store the result partitions
1039 * @param env the environment
1043 static partition_t *split_by_what(partition_t *X, what_func What,
1044 partition_t **P, environment_t *env) {
1047 listmap_entry_t *iter;
1050 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
1052 list_for_each_entry(node_t, x, &X->Leader, node_list) {
1053 void *id = What(x, env);
1054 listmap_entry_t *entry;
1057 /* input not allowed, ignore */
1060 /* Add x to map[What(x)]. */
1061 entry = listmap_find(&map, id);
1062 x->next = entry->list;
1065 /* Let P be a set of Partitions. */
1067 /* for all sets S except one in the range of map do */
1068 for (iter = map.values; iter != NULL; iter = iter->next) {
1069 if (iter->next == NULL) {
1070 /* this is the last entry, ignore */
1075 /* Add SPLIT( X, S ) to P. */
1076 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
1077 R = split(X, S, env);
1087 } /* split_by_what */
1089 /** lambda n.(n.type) */
1090 static void *lambda_type(const node_t *node, environment_t *env) {
1092 return node->type.tv;
1095 /** lambda n.(n.opcode) */
1096 static void *lambda_opcode(const node_t *node, environment_t *env) {
1097 opcode_key_t key, *entry;
1098 ir_node *irn = node->node;
1100 key.code = get_irn_opcode(irn);
1101 key.mode = get_irn_mode(irn);
1105 switch (get_irn_opcode(irn)) {
1107 key.u.proj = get_Proj_proj(irn);
1110 key.u.ent = get_Sel_entity(irn);
1116 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
1118 } /* lambda_opcode */
1120 /** lambda n.(n[i].partition) */
1121 static void *lambda_partition(const node_t *node, environment_t *env) {
1122 ir_node *skipped = skip_Proj(node->node);
1125 int i = env->lambda_input;
1127 if (i >= get_irn_arity(node->node)) {
1128 /* we are outside the allowed range */
1132 /* ignore the "control input" for non-pinned nodes
1133 if we are running in GCSE mode */
1134 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
1137 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
1138 p = get_irn_node(pred);
1141 } /* lambda_partition */
1144 * Returns true if a type is a constant.
1146 static int is_con(const lattice_elem_t type) {
1147 /* be conservative */
1148 if (is_tarval(type.tv))
1149 return tarval_is_constant(type.tv);
1150 return is_entity(type.sym.entity_p);
1154 * Implements split_by().
1156 * @param X the partition to split
1157 * @param env the environment
1159 static void split_by(partition_t *X, environment_t *env) {
1160 partition_t *I, *P = NULL;
1163 dump_partition("split_by", X);
1165 if (X->n_leader == 1) {
1166 /* we have only one leader, no need to split, just check it's type */
1167 node_t *x = get_first_node(X);
1168 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1172 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
1173 P = split_by_what(X, lambda_type, &P, env);
1175 /* adjust the type tags, we have split partitions by type */
1176 for (I = P; I != NULL; I = I->split_next) {
1177 node_t *x = get_first_node(I);
1178 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
1185 if (Y->n_leader > 1) {
1186 /* we do not want split the TOP or constant partitions */
1187 if (! Y->type_is_T_or_C) {
1188 partition_t *Q = NULL;
1190 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
1191 Q = split_by_what(Y, lambda_opcode, &Q, env);
1197 if (Z->n_leader > 1) {
1198 const node_t *first = get_first_node(Z);
1199 int arity = get_irn_arity(first->node);
1203 * BEWARE: during splitting by input 2 for instance we might
1204 * create new partitions which are different by input 1, so collect
1205 * them and split further.
1207 Z->split_next = NULL;
1210 for (input = arity - 1; input >= -1; --input) {
1212 partition_t *Z_prime = R;
1215 if (Z_prime->n_leader > 1) {
1216 env->lambda_input = input;
1217 DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
1218 S = split_by_what(Z_prime, lambda_partition, &S, env);
1220 Z_prime->split_next = S;
1223 } while (R != NULL);
1228 } while (Q != NULL);
1231 } while (P != NULL);
1235 * (Re-)compute the type for a given node.
1237 * @param node the node
1239 static void default_compute(node_t *node) {
1241 ir_node *irn = node->node;
1242 node_t *block = get_irn_node(get_nodes_block(irn));
1244 if (block->type.tv == tarval_unreachable) {
1245 node->type.tv = tarval_top;
1249 /* if any of the data inputs have type top, the result is type top */
1250 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1251 ir_node *pred = get_irn_n(irn, i);
1252 node_t *p = get_irn_node(pred);
1254 if (p->type.tv == tarval_top) {
1255 node->type.tv = tarval_top;
1260 if (get_irn_mode(node->node) == mode_X)
1261 node->type.tv = tarval_reachable;
1263 node->type.tv = computed_value(irn);
1264 } /* default_compute */
1267 * (Re-)compute the type for a Block node.
1269 * @param node the node
1271 static void compute_Block(node_t *node) {
1273 ir_node *block = node->node;
1275 if (block == get_irg_start_block(current_ir_graph)) {
1276 /* start block is always reachable */
1277 node->type.tv = tarval_reachable;
1281 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1282 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1284 if (pred->type.tv == tarval_reachable) {
1285 /* A block is reachable, if at least of predecessor is reachable. */
1286 node->type.tv = tarval_reachable;
1290 node->type.tv = tarval_top;
1291 } /* compute_Block */
1294 * (Re-)compute the type for a Bad node.
1296 * @param node the node
1298 static void compute_Bad(node_t *node) {
1299 /* Bad nodes ALWAYS compute Top */
1300 node->type.tv = tarval_top;
1304 * (Re-)compute the type for an Unknown node.
1306 * @param node the node
1308 static void compute_Unknown(node_t *node) {
1309 /* While Unknown nodes should compute Top this is dangerous:
1310 * a Top input to a Cond would lead to BOTH control flows unreachable.
1311 * While this is correct in the given semantics, it would destroy the Firm
1314 * It would be safe to compute Top IF it can be assured, that only Cmp
1315 * nodes are inputs to Conds. We check that first.
1316 * This is the way Frontends typically build Firm, but some optimizations
1317 * (cond_eval for instance) might replace them by Phib's...
1319 * For now, we compute bottom here.
1321 node->type.tv = tarval_bottom;
1322 } /* compute_Unknown */
1325 * (Re-)compute the type for a Jmp node.
1327 * @param node the node
1329 static void compute_Jmp(node_t *node) {
1330 node_t *block = get_irn_node(get_nodes_block(node->node));
1332 node->type = block->type;
1336 * (Re-)compute the type for the End node.
1338 * @param node the node
1340 static void compute_End(node_t *node) {
1341 /* the End node is NOT dead of course */
1342 node->type.tv = tarval_reachable;
1346 * (Re-)compute the type for a SymConst node.
1348 * @param node the node
1350 static void compute_SymConst(node_t *node) {
1351 ir_node *irn = node->node;
1352 node_t *block = get_irn_node(get_nodes_block(irn));
1354 if (block->type.tv == tarval_unreachable) {
1355 node->type.tv = tarval_top;
1358 switch (get_SymConst_kind(irn)) {
1359 case symconst_addr_ent:
1360 /* case symconst_addr_name: cannot handle this yet */
1361 node->type.sym = get_SymConst_symbol(irn);
1364 node->type.tv = computed_value(irn);
1366 } /* compute_SymConst */
1369 * (Re-)compute the type for a Phi node.
1371 * @param node the node
1373 static void compute_Phi(node_t *node) {
1375 ir_node *phi = node->node;
1376 lattice_elem_t type;
1378 /* if a Phi is in a unreachable block, its type is TOP */
1379 node_t *block = get_irn_node(get_nodes_block(phi));
1381 if (block->type.tv == tarval_unreachable) {
1382 node->type.tv = tarval_top;
1386 /* Phi implements the Meet operation */
1387 type.tv = tarval_top;
1388 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1389 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1390 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1392 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1393 /* ignore TOP inputs: We must check here for unreachable blocks,
1394 because Firm constants live in the Start Block are NEVER Top.
1395 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1396 comes from a unreachable input. */
1399 if (pred->type.tv == tarval_bottom) {
1400 node->type.tv = tarval_bottom;
1402 } else if (type.tv == tarval_top) {
1403 /* first constant found */
1405 } else if (type.tv != pred->type.tv) {
1406 /* different constants or tarval_bottom */
1407 node->type.tv = tarval_bottom;
1410 /* else nothing, constants are the same */
1416 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1418 * @param node the node
1420 static void compute_Add(node_t *node) {
1421 ir_node *sub = node->node;
1422 node_t *l = get_irn_node(get_Add_left(sub));
1423 node_t *r = get_irn_node(get_Add_right(sub));
1424 lattice_elem_t a = l->type;
1425 lattice_elem_t b = r->type;
1428 if (a.tv == tarval_top || b.tv == tarval_top) {
1429 node->type.tv = tarval_top;
1430 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1431 node->type.tv = tarval_bottom;
1433 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1434 must call tarval_add() first to handle this case! */
1435 if (is_tarval(a.tv)) {
1436 if (is_tarval(b.tv)) {
1437 node->type.tv = tarval_add(a.tv, b.tv);
1440 mode = get_tarval_mode(a.tv);
1441 if (a.tv == get_mode_null(mode)) {
1445 } else if (is_tarval(b.tv)) {
1446 mode = get_tarval_mode(b.tv);
1447 if (b.tv == get_mode_null(mode)) {
1452 node->type.tv = tarval_bottom;
1457 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1459 * @param node the node
1461 static void compute_Sub(node_t *node) {
1462 ir_node *sub = node->node;
1463 node_t *l = get_irn_node(get_Sub_left(sub));
1464 node_t *r = get_irn_node(get_Sub_right(sub));
1465 lattice_elem_t a = l->type;
1466 lattice_elem_t b = r->type;
1468 if (a.tv == tarval_top || b.tv == tarval_top) {
1469 node->type.tv = tarval_top;
1470 } else if (is_con(a) && is_con(b)) {
1471 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1472 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1473 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1475 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1478 node->type.tv = tarval_bottom;
1480 } else if (r->part == l->part &&
1481 (!mode_is_float(get_irn_mode(l->node)))) {
1483 * BEWARE: a - a is NOT always 0 for floating Point values, as
1484 * NaN op NaN = NaN, so we must check this here.
1486 ir_mode *mode = get_irn_mode(sub);
1487 node->type.tv = get_mode_null(mode);
1489 node->type.tv = tarval_bottom;
1494 * (Re-)compute the type for Cmp.
1496 * @param node the node
1498 static void compute_Cmp(node_t *node) {
1499 ir_node *cmp = node->node;
1500 node_t *l = get_irn_node(get_Cmp_left(cmp));
1501 node_t *r = get_irn_node(get_Cmp_right(cmp));
1502 lattice_elem_t a = l->type;
1503 lattice_elem_t b = r->type;
1505 if (a.tv == tarval_top || b.tv == tarval_top) {
1506 node->type.tv = tarval_top;
1507 } else if (is_con(a) && is_con(b)) {
1508 /* both nodes are constants, we can probably do something */
1509 node->type.tv = tarval_b_true;
1510 } else if (r->part == l->part) {
1511 /* both nodes congruent, we can probably do something */
1512 node->type.tv = tarval_b_true;
1514 node->type.tv = tarval_bottom;
1516 } /* compute_Proj_Cmp */
1519 * (Re-)compute the type for a Proj(Cmp).
1521 * @param node the node
1522 * @param cond the predecessor Cmp node
1524 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1525 ir_node *proj = node->node;
1526 node_t *l = get_irn_node(get_Cmp_left(cmp));
1527 node_t *r = get_irn_node(get_Cmp_right(cmp));
1528 lattice_elem_t a = l->type;
1529 lattice_elem_t b = r->type;
1530 pn_Cmp pnc = get_Proj_proj(proj);
1532 if (a.tv == tarval_top || b.tv == tarval_top) {
1533 node->type.tv = tarval_top;
1534 } else if (is_con(a) && is_con(b)) {
1535 default_compute(node);
1536 } else if (r->part == l->part &&
1537 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1539 * BEWARE: a == a is NOT always True for floating Point values, as
1540 * NaN != NaN is defined, so we must check this here.
1542 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1544 node->type.tv = tarval_bottom;
1546 } /* compute_Proj_Cmp */
1549 * (Re-)compute the type for a Proj(Cond).
1551 * @param node the node
1552 * @param cond the predecessor Cond node
1554 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1555 ir_node *proj = node->node;
1556 long pnc = get_Proj_proj(proj);
1557 ir_node *sel = get_Cond_selector(cond);
1558 node_t *selector = get_irn_node(sel);
1560 if (get_irn_mode(sel) == mode_b) {
1562 if (pnc == pn_Cond_true) {
1563 if (selector->type.tv == tarval_b_false) {
1564 node->type.tv = tarval_unreachable;
1565 } else if (selector->type.tv == tarval_b_true) {
1566 node->type.tv = tarval_reachable;
1567 } else if (selector->type.tv == tarval_bottom) {
1568 node->type.tv = tarval_reachable;
1570 assert(selector->type.tv == tarval_top);
1571 node->type.tv = tarval_unreachable;
1574 assert(pnc == pn_Cond_false);
1576 if (selector->type.tv == tarval_b_false) {
1577 node->type.tv = tarval_reachable;
1578 } else if (selector->type.tv == tarval_b_true) {
1579 node->type.tv = tarval_unreachable;
1580 } else if (selector->type.tv == tarval_bottom) {
1581 node->type.tv = tarval_reachable;
1583 assert(selector->type.tv == tarval_top);
1584 node->type.tv = tarval_unreachable;
1589 if (selector->type.tv == tarval_bottom) {
1590 node->type.tv = tarval_reachable;
1591 } else if (selector->type.tv == tarval_top) {
1592 node->type.tv = tarval_unreachable;
1594 long value = get_tarval_long(selector->type.tv);
1595 if (pnc == get_Cond_defaultProj(cond)) {
1596 /* default switch, have to check ALL other cases */
1599 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1600 ir_node *succ = get_irn_out(cond, i);
1604 if (value == get_Proj_proj(succ)) {
1605 /* we found a match, will NOT take the default case */
1606 node->type.tv = tarval_unreachable;
1610 /* all cases checked, no match, will take default case */
1611 node->type.tv = tarval_reachable;
1614 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1618 } /* compute_Proj_Cond */
1621 * (Re-)compute the type for a Proj-Node.
1623 * @param node the node
1625 static void compute_Proj(node_t *node) {
1626 ir_node *proj = node->node;
1627 ir_mode *mode = get_irn_mode(proj);
1628 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1629 ir_node *pred = get_Proj_pred(proj);
1631 if (block->type.tv == tarval_unreachable) {
1632 /* a Proj in a unreachable Block stay Top */
1633 node->type.tv = tarval_top;
1636 if (get_irn_node(pred)->type.tv == tarval_top) {
1637 /* if the predecessor is Top, its Proj follow */
1638 node->type.tv = tarval_top;
1642 if (mode == mode_M) {
1643 /* mode M is always bottom */
1644 node->type.tv = tarval_bottom;
1647 if (mode != mode_X) {
1649 compute_Proj_Cmp(node, pred);
1651 default_compute(node);
1654 /* handle mode_X nodes */
1656 switch (get_irn_opcode(pred)) {
1658 /* the Proj_X from the Start is always reachable.
1659 However this is already handled at the top. */
1660 node->type.tv = tarval_reachable;
1663 compute_Proj_Cond(node, pred);
1666 default_compute(node);
1668 } /* compute_Proj */
1671 * (Re-)compute the type for a Confirm.
1673 * @param node the node
1675 static void compute_Confirm(node_t *node) {
1676 ir_node *confirm = node->node;
1677 node_t *pred = get_irn_node(get_Confirm_value(confirm));
1679 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1680 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1682 if (is_con(bound->type)) {
1683 /* is equal to a constant */
1684 node->type = bound->type;
1688 /* a Confirm is a copy OR a Const */
1689 node->type = pred->type;
1690 } /* compute_Confirm */
1693 * (Re-)compute the type for a Max.
1695 * @param node the node
1697 static void compute_Max(node_t *node) {
1698 ir_node *op = node->node;
1699 node_t *l = get_irn_node(get_binop_left(op));
1700 node_t *r = get_irn_node(get_binop_right(op));
1701 lattice_elem_t a = l->type;
1702 lattice_elem_t b = r->type;
1704 if (a.tv == tarval_top || b.tv == tarval_top) {
1705 node->type.tv = tarval_top;
1706 } else if (is_con(a) && is_con(b)) {
1707 /* both nodes are constants, we can probably do something */
1709 /* this case handles symconsts as well */
1712 ir_mode *mode = get_irn_mode(op);
1713 tarval *tv_min = get_mode_min(mode);
1717 else if (b.tv == tv_min)
1719 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1720 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1721 node->type.tv = a.tv;
1723 node->type.tv = b.tv;
1725 node->type.tv = tarval_bad;
1728 } else if (r->part == l->part) {
1729 /* both nodes congruent, we can probably do something */
1732 node->type.tv = tarval_bottom;
1737 * (Re-)compute the type for a Min.
1739 * @param node the node
1741 static void compute_Min(node_t *node) {
1742 ir_node *op = node->node;
1743 node_t *l = get_irn_node(get_binop_left(op));
1744 node_t *r = get_irn_node(get_binop_right(op));
1745 lattice_elem_t a = l->type;
1746 lattice_elem_t b = r->type;
1748 if (a.tv == tarval_top || b.tv == tarval_top) {
1749 node->type.tv = tarval_top;
1750 } else if (is_con(a) && is_con(b)) {
1751 /* both nodes are constants, we can probably do something */
1753 /* this case handles symconsts as well */
1756 ir_mode *mode = get_irn_mode(op);
1757 tarval *tv_max = get_mode_max(mode);
1761 else if (b.tv == tv_max)
1763 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1764 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1765 node->type.tv = a.tv;
1767 node->type.tv = b.tv;
1769 node->type.tv = tarval_bad;
1772 } else if (r->part == l->part) {
1773 /* both nodes congruent, we can probably do something */
1776 node->type.tv = tarval_bottom;
1781 * (Re-)compute the type for a given node.
1783 * @param node the node
1785 static void compute(node_t *node) {
1788 if (is_no_Block(node->node)) {
1789 node_t *block = get_irn_node(get_nodes_block(node->node));
1791 if (block->type.tv == tarval_unreachable) {
1792 node->type.tv = tarval_top;
1797 func = (compute_func)node->node->op->ops.generic;
1803 * Identity functions: Note that one might thing that identity() is just a
1804 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
1805 * here, because it expects that the identity node is one of the inputs, which is NOT
1806 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
1807 * So, we have our own implementation, which copies some parts of equivalent_node()
1811 * Calculates the Identity for Phi nodes
1813 static node_t *identity_Phi(node_t *node) {
1814 ir_node *phi = node->node;
1815 ir_node *block = get_nodes_block(phi);
1816 node_t *n_part = NULL;
1819 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1820 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
1822 if (pred_X->type.tv == tarval_reachable) {
1823 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1827 else if (n_part->part != pred->part) {
1828 /* incongruent inputs, not a follower */
1833 /* if n_part is NULL here, all inputs path are dead, the Phi computes
1834 * tarval_top, is in the TOP partition and should NOT being split! */
1835 assert(n_part != NULL);
1837 } /* identity_Phi */
1840 * Calculates the Identity for commutative 0 neutral nodes.
1842 static node_t *identity_comm_zero_binop(node_t *node) {
1843 ir_node *op = node->node;
1844 node_t *a = get_irn_node(get_binop_left(op));
1845 node_t *b = get_irn_node(get_binop_right(op));
1846 ir_mode *mode = get_irn_mode(op);
1849 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1850 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1853 /* node: no input should be tarval_top, else the binop would be also
1854 * Top and not being split. */
1855 zero = get_mode_null(mode);
1856 if (a->type.tv == zero)
1858 if (b->type.tv == zero)
1861 } /* identity_comm_zero_binop */
1863 #define identity_Add identity_comm_zero_binop
1864 #define identity_Or identity_comm_zero_binop
1867 * Calculates the Identity for Mul nodes.
1869 static node_t *identity_Mul(node_t *node) {
1870 ir_node *op = node->node;
1871 node_t *a = get_irn_node(get_Mul_left(op));
1872 node_t *b = get_irn_node(get_Mul_right(op));
1873 ir_mode *mode = get_irn_mode(op);
1876 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1877 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1880 /* node: no input should be tarval_top, else the binop would be also
1881 * Top and not being split. */
1882 one = get_mode_one(mode);
1883 if (a->type.tv == one)
1885 if (b->type.tv == one)
1888 } /* identity_Mul */
1891 * Calculates the Identity for Sub nodes.
1893 static node_t *identity_Sub(node_t *node) {
1894 ir_node *sub = node->node;
1895 node_t *b = get_irn_node(get_Sub_right(sub));
1896 ir_mode *mode = get_irn_mode(sub);
1898 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1899 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1902 /* node: no input should be tarval_top, else the binop would be also
1903 * Top and not being split. */
1904 if (b->type.tv == get_mode_null(mode))
1905 return get_irn_node(get_Sub_left(sub));
1907 } /* identity_Mul */
1910 * Calculates the Identity for And nodes.
1912 static node_t *identity_And(node_t *node) {
1913 ir_node *and = node->node;
1914 node_t *a = get_irn_node(get_And_left(and));
1915 node_t *b = get_irn_node(get_And_right(and));
1916 tarval *neutral = get_mode_all_one(get_irn_mode(and));
1918 /* node: no input should be tarval_top, else the And would be also
1919 * Top and not being split. */
1920 if (a->type.tv == neutral)
1922 if (b->type.tv == neutral)
1925 } /* identity_And */
1928 * Calculates the Identity for Confirm nodes.
1930 static node_t *identity_Confirm(node_t *node) {
1931 ir_node *confirm = node->node;
1933 /* a Confirm is always a Copy */
1934 return get_irn_node(get_Confirm_value(confirm));
1935 } /* identity_Confirm */
1938 * Calculates the Identity for Mux nodes.
1940 static node_t *identity_Mux(node_t *node) {
1941 ir_node *mux = node->node;
1942 node_t *sel = get_irn_node(get_Mux_sel(mux));
1943 node_t *t = get_irn_node(get_Mux_true(mux));
1944 node_t *f = get_irn_node(get_Mux_false(mux));
1946 if (t->part == f->part)
1949 /* Mux sel input is mode_b, so it is always a tarval */
1950 if (sel->type.tv == tarval_b_true)
1952 if (sel->type.tv == tarval_b_false)
1955 } /* identity_Mux */
1958 * Calculates the Identity for Min nodes.
1960 static node_t *identity_Min(node_t *node) {
1961 ir_node *op = node->node;
1962 node_t *a = get_irn_node(get_binop_left(op));
1963 node_t *b = get_irn_node(get_binop_right(op));
1964 ir_mode *mode = get_irn_mode(op);
1967 if (a->part == b->part) {
1968 /* leader of multiple predecessors */
1972 /* works even with NaN */
1973 tv_max = get_mode_max(mode);
1974 if (a->type.tv == tv_max)
1976 if (b->type.tv == tv_max)
1979 } /* identity_Min */
1982 * Calculates the Identity for Max nodes.
1984 static node_t *identity_Max(node_t *node) {
1985 ir_node *op = node->node;
1986 node_t *a = get_irn_node(get_binop_left(op));
1987 node_t *b = get_irn_node(get_binop_right(op));
1988 ir_mode *mode = get_irn_mode(op);
1991 if (a->part == b->part) {
1992 /* leader of multiple predecessors */
1996 /* works even with NaN */
1997 tv_min = get_mode_min(mode);
1998 if (a->type.tv == tv_min)
2000 if (b->type.tv == tv_min)
2003 } /* identity_Max */
2006 * Calculates the Identity for nodes.
2008 static node_t *identity(node_t *node) {
2009 ir_node *irn = node->node;
2011 switch (get_irn_opcode(irn)) {
2013 return identity_Phi(node);
2015 return identity_Add(node);
2017 return identity_Or(node);
2019 return identity_Sub(node);
2021 return identity_Add(node);
2023 return identity_Confirm(node);
2025 return identity_Mux(node);
2027 return identity_Min(node);
2029 return identity_Max(node);
2036 * Node follower is a (new) follower of leader, segregate Leader
2039 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
2040 ir_node *l = leader->node;
2041 int j, i, n = get_irn_n_outs(l);
2043 DB((dbg, LEVEL_2, "%+F is a follower of %+F\n", follower, leader->node));
2044 /* The leader edges must remain sorted, but follower edges can
2046 for (i = leader->n_followers + 1; i <= n; ++i) {
2047 if (l->out[i].use == follower) {
2048 ir_def_use_edge t = l->out[i];
2050 for (j = i - 1; j >= leader->n_followers + 1; --j)
2051 l->out[j + 1] = l->out[j];
2052 ++leader->n_followers;
2053 l->out[leader->n_followers] = t;
2055 /* note: a node might be a n-fold follower, for instance
2056 * if x = max(a,a), so no break here. */
2059 } /* segregate_def_use_chain_1 */
2062 * Node follower is a (new) follower of leader, segregate Leader
2063 * out edges. If follower is a n-congruent Input identity, all follower
2064 * inputs congruent to follower are also leader.
2066 static void segregate_def_use_chain(const ir_node *follower, node_t *leader) {
2067 ir_op *op = get_irn_op(follower);
2070 /* n-Congruent Input Identity for Phi's */
2072 ir_node *block = get_nodes_block(follower);
2074 DB((dbg, LEVEL_2, "n-Congruent follower %+F\n", follower));
2075 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2076 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
2078 /* beware: we are NOT followers of dead inputs */
2079 if (pred_X->type.tv == tarval_reachable) {
2080 node_t *pred = get_irn_node(get_irn_n(follower, i));
2082 if (pred->part == leader->part)
2083 segregate_def_use_chain_1(follower, pred);
2086 } else if (op == op_Mux || op == op_Max || op == op_Min) {
2087 /* n-Congruent Input Identity */
2090 DB((dbg, LEVEL_2, "n-Congruent follower %+F\n", follower));
2091 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
2092 node_t *pred = get_irn_node(get_irn_n(follower, i));
2094 if (pred->part == leader->part)
2095 segregate_def_use_chain_1(follower, pred);
2098 /* 1-Congruent Input Identity */
2099 segregate_def_use_chain_1(follower, leader);
2101 } /* segregate_def_use_chain */
2104 * Make all inputs to x from inside X no longer be F.def_use edges.
2106 static void move_edges_to_leader(node_t *x) {
2107 partition_t *X = x->part;
2108 ir_node *irn = x->node;
2111 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
2112 node_t *pred = get_irn_node(get_irn_n(irn, i));
2117 n = get_irn_n_outs(p);
2118 for (j = 1; j <= pred->n_followers; ++j) {
2119 if (p->out[j].pos == i && p->out[j].use == irn) {
2120 /* found a follower edge to x, move it to the Leader */
2121 ir_def_use_edge edge = p->out[j];
2123 /* remove this edge from the Follower set */
2124 p->out[j] = p->out[pred->n_followers];
2125 --pred->n_followers;
2127 /* sort it into the leader set */
2128 for (k = pred->n_followers + 2; k <= n; ++k) {
2129 if (p->out[k].pos >= edge.pos)
2131 p->out[k - 1] = p->out[k];
2133 /* place the new edge here */
2134 p->out[k - 1] = edge;
2136 /* edge found and moved */
2144 * Propagate constant evaluation.
2146 * @param env the environment
2148 static void propagate(environment_t *env) {
2151 lattice_elem_t old_type;
2153 unsigned n_fallen, old_type_was_T_or_C;
2156 while (env->cprop != NULL) {
2157 void *oldopcode = NULL;
2159 /* remove the first partition X from cprop */
2162 env->cprop = X->cprop_next;
2164 old_type_was_T_or_C = X->type_is_T_or_C;
2166 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
2169 while (! list_empty(&X->cprop)) {
2170 /* remove the first Node x from X.cprop */
2171 x = list_entry(X->cprop.next, node_t, cprop_list);
2172 list_del(&x->cprop_list);
2175 if (x->is_follower && identity(x) == x) {
2176 /* x will make the follower -> leader transition */
2177 DB((dbg, LEVEL_2, "%+F make the follower -> leader transition\n", x->node));
2178 if (oldopcode == NULL) {
2179 oldopcode = lambda_opcode(get_first_node(X), env);
2181 if (oldopcode != lambda_opcode(x, env)) {
2182 /* different opcode -> x falls out of this partition */
2187 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2190 /* move x from X.Follower to X.Leader */
2191 list_del(&x->node_list);
2192 list_add_tail(&x->node_list, &X->Leader);
2196 /* Make all inputs to x from inside X no longer be F.def_use edges */
2197 move_edges_to_leader(x);
2200 /* compute a new type for x */
2202 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
2204 if (x->type.tv != old_type.tv) {
2205 verify_type(old_type, x->type);
2206 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
2208 if (x->on_fallen == 0) {
2209 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
2210 not already on the list. */
2215 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
2217 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
2218 ir_node *succ = get_irn_out(x->node, i);
2219 node_t *y = get_irn_node(succ);
2221 /* Add y to y.partition.cprop. */
2222 add_node_to_cprop(y, env);
2227 if (n_fallen > 0 && n_fallen != X->n_leader) {
2228 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
2229 Y = split(X, fallen, env);
2233 /* remove the flags from the fallen list */
2234 for (x = fallen; x != NULL; x = x->next)
2238 if (old_type_was_T_or_C) {
2241 /* check if some nodes will make the leader -> follower transition */
2242 list_for_each_entry_safe(node_t, y, tmp, &Y->Leader, node_list) {
2243 if (! is_con(y->type)) {
2244 node_t *eq_node = identity(y);
2247 DB((dbg, LEVEL_2, "Node %+F is a follower of %+F\n", y->node, eq_node->node));
2248 /* move to Follower */
2250 list_del(&y->node_list);
2253 list_add_tail(&y->node_list, &Y->Follower);
2254 segregate_def_use_chain(y->node, eq_node);
2265 * Get the leader for a given node from its congruence class.
2267 * @param irn the node
2269 static ir_node *get_leader(node_t *node) {
2270 partition_t *part = node->part;
2272 if (part->n_leader > 1 || node->is_follower) {
2273 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
2275 return get_first_node(part)->node;
2281 * Return non-zero if the control flow predecessor node pred
2282 * is the only reachable control flow exit of its block.
2284 * @param pred the control flow exit
2286 static int can_exchange(ir_node *pred) {
2289 else if (is_Jmp(pred))
2291 else if (get_irn_mode(pred) == mode_T) {
2294 /* if the predecessor block has more than one
2295 reachable outputs we cannot remove the block */
2297 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
2298 ir_node *proj = get_irn_out(pred, i);
2301 /* skip non-control flow Proj's */
2302 if (get_irn_mode(proj) != mode_X)
2305 node = get_irn_node(proj);
2306 if (node->type.tv == tarval_reachable) {
2317 * Block Post-Walker, apply the analysis results on control flow by
2318 * shortening Phi's and Block inputs.
2320 static void apply_cf(ir_node *block, void *ctx) {
2321 environment_t *env = ctx;
2322 node_t *node = get_irn_node(block);
2324 ir_node **ins, **in_X;
2325 ir_node *phi, *next;
2327 if (block == get_irg_end_block(current_ir_graph) ||
2328 block == get_irg_start_block(current_ir_graph)) {
2329 /* the EndBlock is always reachable even if the analysis
2330 finds out the opposite :-) */
2333 if (node->type.tv == tarval_unreachable) {
2334 /* mark dead blocks */
2335 set_Block_dead(block);
2339 n = get_Block_n_cfgpreds(block);
2342 /* only one predecessor combine */
2343 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2345 if (can_exchange(pred)) {
2346 exchange(block, get_nodes_block(pred));
2352 NEW_ARR_A(ir_node *, in_X, n);
2354 for (i = 0; i < n; ++i) {
2355 ir_node *pred = get_Block_cfgpred(block, i);
2356 node_t *node = get_irn_node(pred);
2358 if (node->type.tv == tarval_reachable) {
2365 NEW_ARR_A(ir_node *, ins, n);
2366 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2367 node_t *node = get_irn_node(phi);
2369 next = get_Phi_next(phi);
2370 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2371 /* this Phi is replaced by a constant */
2372 tarval *tv = node->type.tv;
2373 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2375 set_irn_node(c, node);
2377 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2382 for (i = 0; i < n; ++i) {
2383 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2385 if (pred->type.tv == tarval_reachable) {
2386 ins[j++] = get_Phi_pred(phi, i);
2390 /* this Phi is replaced by a single predecessor */
2391 ir_node *s = ins[0];
2394 DB((dbg, LEVEL_1, "%+F is replaced by %+F because of cf change\n", phi, s));
2398 set_irn_in(phi, j, ins);
2405 /* this Block has only one live predecessor */
2406 ir_node *pred = skip_Proj(in_X[0]);
2408 if (can_exchange(pred)) {
2409 exchange(block, get_nodes_block(pred));
2413 set_irn_in(block, k, in_X);
2419 * Post-Walker, apply the analysis results;
2421 static void apply_result(ir_node *irn, void *ctx) {
2422 environment_t *env = ctx;
2423 node_t *node = get_irn_node(irn);
2425 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2426 /* blocks already handled, do not touch the End node */
2428 node_t *block = get_irn_node(get_nodes_block(irn));
2430 if (block->type.tv == tarval_unreachable) {
2431 ir_node *bad = get_irg_bad(current_ir_graph);
2433 /* here, bad might already have a node, but this can be safely ignored
2434 as long as bad has at least ONE valid node */
2435 set_irn_node(bad, node);
2437 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2441 else if (node->type.tv == tarval_unreachable) {
2442 ir_node *bad = get_irg_bad(current_ir_graph);
2444 /* see comment above */
2445 set_irn_node(bad, node);
2447 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2451 else if (get_irn_mode(irn) == mode_X) {
2454 ir_node *cond = get_Proj_pred(irn);
2456 if (is_Cond(cond)) {
2457 node_t *sel = get_irn_node(get_Cond_selector(cond));
2459 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2460 /* Cond selector is a constant, make a Jmp */
2461 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2462 set_irn_node(jmp, node);
2464 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2471 /* normal data node */
2472 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2473 tarval *tv = node->type.tv;
2476 * Beware: never replace mode_T nodes by constants. Currently we must mark
2477 * mode_T nodes with constants, but do NOT replace them.
2479 if (! is_Const(irn) && get_irn_mode(irn) != mode_T) {
2480 /* can be replaced by a constant */
2481 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2482 set_irn_node(c, node);
2484 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2488 } else if (is_entity(node->type.sym.entity_p)) {
2489 if (! is_SymConst(irn)) {
2490 /* can be replaced by a Symconst */
2491 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2492 set_irn_node(symc, node);
2495 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2496 exchange(irn, symc);
2500 ir_node *leader = get_leader(node);
2502 if (leader != irn) {
2503 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2504 exchange(irn, leader);
2510 } /* apply_result */
2512 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2515 * sets the generic functions to compute.
2517 static void set_compute_functions(void) {
2520 /* set the default compute function */
2521 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2522 ir_op *op = get_irp_opcode(i);
2523 op->ops.generic = (op_func)default_compute;
2526 /* set specific functions */
2545 } /* set_compute_functions */
2547 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2548 ir_node *irn = local != NULL ? local : n;
2549 node_t *node = get_irn_node(irn);
2551 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2555 void combo(ir_graph *irg) {
2557 ir_node *initial_bl;
2559 ir_graph *rem = current_ir_graph;
2561 current_ir_graph = irg;
2563 /* register a debug mask */
2564 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2565 //firm_dbg_set_mask(dbg, SET_LEVEL_3);
2567 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2569 obstack_init(&env.obst);
2570 env.worklist = NULL;
2574 #ifdef DEBUG_libfirm
2575 env.dbg_list = NULL;
2577 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
2578 env.type2id_map = pmap_create();
2579 env.end_idx = get_opt_global_cse() ? 0 : -1;
2580 env.lambda_input = 0;
2583 assure_irg_outs(irg);
2585 /* we have our own value_of function */
2586 set_value_of_func(get_node_tarval);
2588 set_compute_functions();
2589 DEBUG_ONLY(part_nr = 0);
2591 /* create the initial partition and place it on the work list */
2592 env.initial = new_partition(&env);
2593 add_to_worklist(env.initial, &env);
2594 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2596 /* all nodes on the initial partition have type Top */
2597 env.initial->type_is_T_or_C = 1;
2599 /* Place the START Node's partition on cprop.
2600 Place the START Node on its local worklist. */
2601 initial_bl = get_irg_start_block(irg);
2602 start = get_irn_node(initial_bl);
2603 add_node_to_cprop(start, &env);
2607 if (env.worklist != NULL)
2609 } while (env.cprop != NULL || env.worklist != NULL);
2611 dump_all_partitions(&env);
2614 set_dump_node_vcgattr_hook(dump_partition_hook);
2615 dump_ir_block_graph(irg, "-partition");
2616 set_dump_node_vcgattr_hook(NULL);
2618 (void)dump_partition_hook;
2621 /* apply the result */
2622 irg_block_walk_graph(irg, NULL, apply_cf, &env);
2623 irg_walk_graph(irg, NULL, apply_result, &env);
2626 /* control flow might changed */
2627 set_irg_outs_inconsistent(irg);
2628 set_irg_extblk_inconsistent(irg);
2629 set_irg_doms_inconsistent(irg);
2630 set_irg_loopinfo_inconsistent(irg);
2633 pmap_destroy(env.type2id_map);
2634 del_set(env.opcode2id_map);
2635 obstack_free(&env.obst, NULL);
2637 /* restore value_of() default behavior */
2638 set_value_of_func(NULL);
2639 current_ir_graph = rem;