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 that the current implementation lack the leaders/followers
29 * Note further that we use the terminology from Click's work here, which is different
30 * in some cases from Firm terminology. Especially, Click's type is a
31 * Firm tarval/entity, nevertheless we call it type here for "maximum compatibility".
39 #include "iroptimize.h"
47 #include "irgraph_t.h"
62 /* define this to check that all type translations are monotone */
63 #define VERIFY_MONOTONE
65 typedef struct node_t node_t;
66 typedef struct partition_t partition_t;
67 typedef struct opcode_key_t opcode_key_t;
68 typedef struct listmap_entry_t listmap_entry_t;
70 /** The type of the compute function. */
71 typedef void (*compute_func)(node_t *node);
77 ir_opcode code; /**< The Firm opcode. */
78 ir_mode *mode; /**< The mode of all nodes in the partition. */
80 long proj; /**< For Proj nodes, its proj number */
81 ir_entity *ent; /**< For Sel Nodes, its entity */
86 * An entry in the list_map.
88 struct listmap_entry_t {
89 void *id; /**< The id. */
90 node_t *list; /**< The associated list for this id. */
91 listmap_entry_t *next; /**< Link to the next entry in the map. */
94 /** We must map id's to lists. */
95 typedef struct listmap_t {
96 set *map; /**< Map id's to listmap_entry_t's */
97 listmap_entry_t *values; /**< List of all values in the map. */
101 * A lattice element. Because we handle constants and symbolic constants different, we
102 * have to use this union.
113 ir_node *node; /**< The IR-node itself. */
114 list_head node_list; /**< Double-linked list of leader/follower entries. */
115 list_head cprop_list; /**< Double-linked partition.cprop list. */
116 partition_t *part; /**< points to the partition this node belongs to */
117 node_t *next; /**< Next node on local list (partition.touched, fallen). */
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 followers 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. */
129 * A partition containing congruent nodes.
132 list_head leaders; /**< The head of partition leader node list. */
133 list_head followers; /**< The head of partition followers node list. */
134 list_head cprop; /**< The head of partition.cprop list. */
135 partition_t *wl_next; /**< Next entry in the work list if any. */
136 partition_t *touched_next; /**< Points to the next partition in the touched set. */
137 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
138 partition_t *split_next; /**< Points to the next partition in the list that must be split by split_by(). */
139 node_t *touched; /**< The partition.touched set of this partition. */
140 unsigned n_leaders; /**< Number of entries in this partition.leaders. */
141 unsigned n_touched; /**< Number of entries in the partition.touched. */
142 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
143 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
144 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
145 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
146 unsigned type_is_T_or_C:1;/**< Set, if all nodes in this partition have type Top or Constant. */
148 partition_t *dbg_next; /**< Link all partitions for debugging */
149 unsigned nr; /**< A unique number for (what-)mapping, >0. */
153 typedef struct environment_t {
154 struct obstack obst; /**< obstack to allocate data structures. */
155 partition_t *worklist; /**< The work list. */
156 partition_t *cprop; /**< The constant propagation list. */
157 partition_t *touched; /**< the touched set. */
158 partition_t *initial; /**< The initial partition. */
159 set *opcode2id_map; /**< The opcodeMode->id map. */
160 pmap *type2id_map; /**< The type->id map. */
161 int end_idx; /**< -1 for local and 0 for global congruences. */
162 int lambda_input; /**< Captured argument for lambda_partition(). */
163 int modified; /**< Set, if the graph was modified. */
165 partition_t *dbg_list; /**< List of all partitions. */
169 /** Type of the what function. */
170 typedef void *(*what_func)(const node_t *node, environment_t *env);
172 #define get_irn_node(follower) ((node_t *)get_irn_link(follower))
173 #define set_irn_node(follower, node) set_irn_link(follower, node)
175 /* we do NOT use tarval_unreachable here, instead we use Top for this purpose */
176 #undef tarval_unreachable
177 #define tarval_unreachable tarval_top
180 /** The debug module handle. */
181 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
183 /** Next partition number. */
184 DEBUG_ONLY(static unsigned part_nr = 0);
187 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
190 * Dump partition to output.
192 static void dump_partition(const char *msg, const partition_t *part) {
195 lattice_elem_t type = get_partition_type(part);
197 DB((dbg, LEVEL_2, "%s part%u%s (%u, %+F) {\n ",
198 msg, part->nr, part->type_is_T_or_C ? "*" : "",
199 part->n_leaders, type));
200 list_for_each_entry(node_t, node, &part->leaders, node_list) {
201 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
204 if (! list_empty(&part->followers)) {
205 DB((dbg, LEVEL_2, "\n---\n"));
207 list_for_each_entry(node_t, node, &part->followers, node_list) {
208 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
212 DB((dbg, LEVEL_2, "\n}\n"));
213 } /* dump_partition */
216 * Dump all partitions.
218 static void dump_all_partitions(const environment_t *env) {
219 const partition_t *P;
221 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
222 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
223 dump_partition("", P);
227 #define dump_partition(msg, part)
228 #define dump_all_partitions(env)
231 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
233 * Verify that a type transition is monotone
235 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
236 if (old_type.tv == new_type.tv) {
240 if (old_type.tv == tarval_top) {
241 /* from Top down-to is always allowed */
244 if (old_type.tv == tarval_reachable) {
245 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
247 if (new_type.tv == tarval_bottom || new_type.tv == tarval_reachable) {
251 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
254 #define verify_type(old_type, new_type)
258 * Compare two pointer values of a listmap.
260 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
261 const listmap_entry_t *e1 = elt;
262 const listmap_entry_t *e2 = key;
265 return e1->id != e2->id;
266 } /* listmap_cmp_ptr */
269 * Initializes a listmap.
271 * @param map the listmap
273 static void listmap_init(listmap_t *map) {
274 map->map = new_set(listmap_cmp_ptr, 16);
279 * Terminates a listmap.
281 * @param map the listmap
283 static void listmap_term(listmap_t *map) {
288 * Return the associated listmap entry for a given id.
290 * @param map the listmap
291 * @param id the id to search for
293 * @return the asociated listmap entry for the given id
295 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
296 listmap_entry_t key, *entry;
301 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
303 if (entry->list == NULL) {
304 /* a new entry, put into the list */
305 entry->next = map->values;
312 * Calculate the hash value for an opcode map entry.
314 * @param entry an opcode map entry
316 * @return a hash value for the given opcode map entry
318 static unsigned opcode_hash(const opcode_key_t *entry) {
319 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
323 * Compare two entries in the opcode map.
325 static int cmp_opcode(const void *elt, const void *key, size_t size) {
326 const opcode_key_t *o1 = elt;
327 const opcode_key_t *o2 = key;
330 return o1->code != o2->code || o1->mode != o2->mode ||
331 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
335 * Compare two Def-Use edges for input position.
337 static int cmp_def_use_edge(const void *a, const void *b) {
338 const ir_def_use_edge *ea = a;
339 const ir_def_use_edge *eb = b;
341 /* no overrun, because range is [-1, MAXINT] */
342 return ea->pos - eb->pos;
343 } /* cmp_def_use_edge */
346 * We need the Def-Use edges sorted.
348 static void sort_irn_outs(node_t *node) {
349 ir_node *irn = node->node;
350 int n_outs = get_irn_n_outs(irn);
353 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
355 node->max_user_input = irn->out[n_outs].pos;
356 } /* sort_irn_outs */
359 * Return the type of a node.
361 * @param irn an IR-node
363 * @return the associated type of this node
365 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
366 return get_irn_node(irn)->type;
367 } /* get_node_type */
370 * Return the tarval of a node.
372 * @param irn an IR-node
374 * @return the associated type of this node
376 static INLINE tarval *get_node_tarval(const ir_node *irn) {
377 lattice_elem_t type = get_node_type(irn);
379 if (is_tarval(type.tv))
381 return tarval_bottom;
382 } /* get_node_type */
385 * Add a partition to the worklist.
387 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
388 assert(X->on_worklist == 0);
389 X->wl_next = env->worklist;
395 * Create a new empty partition.
397 * @param env the environment
399 * @return a newly allocated partition
401 static INLINE partition_t *new_partition(environment_t *env) {
402 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
404 INIT_LIST_HEAD(&part->leaders);
405 INIT_LIST_HEAD(&part->followers);
406 INIT_LIST_HEAD(&part->cprop);
407 part->wl_next = NULL;
408 part->touched_next = NULL;
409 part->cprop_next = NULL;
410 part->split_next = NULL;
411 part->touched = NULL;
414 part->max_user_inputs = 0;
415 part->on_worklist = 0;
416 part->on_touched = 0;
418 part->type_is_T_or_C = 0;
420 part->dbg_next = env->dbg_list;
421 env->dbg_list = part;
422 part->nr = part_nr++;
426 } /* new_partition */
429 * Get the first node from a partition.
431 static INLINE node_t *get_first_node(const partition_t *X) {
432 return list_entry(X->leaders.next, node_t, node_list);
436 * Return the type of a partition (assuming partition is non-empty and
437 * all elements have the same type).
439 * @param X a partition
441 * @return the type of the first element of the partition
443 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
444 const node_t *first = get_first_node(X);
446 } /* get_partition_type */
449 * Creates a partition node for the given IR-node and place it
450 * into the given partition.
452 * @param irn an IR-node
453 * @param part a partition to place the node in
454 * @param env the environment
456 * @return the created node
458 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
459 /* create a partition node and place it in the partition */
460 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
462 INIT_LIST_HEAD(&node->node_list);
463 INIT_LIST_HEAD(&node->cprop_list);
467 node->type.tv = tarval_top;
468 node->max_user_input = 0;
470 node->n_followers = 0;
471 node->on_touched = 0;
474 node->is_follower = 0;
475 set_irn_node(irn, node);
477 list_add_tail(&node->node_list, &part->leaders);
481 } /* create_partition_node */
484 * Pre-Walker, init all Block-Phi lists.
486 static void init_block_phis(ir_node *irn, void *env) {
490 set_Block_phis(irn, NULL);
495 * Post-Walker, initialize all Nodes' type to U or top and place
496 * all nodes into the TOP partition.
498 static void create_initial_partitions(ir_node *irn, void *ctx) {
499 environment_t *env = ctx;
500 partition_t *part = env->initial;
503 node = create_partition_node(irn, part, env);
505 if (node->max_user_input > part->max_user_inputs)
506 part->max_user_inputs = node->max_user_input;
509 add_Block_phi(get_nodes_block(irn), irn);
511 } /* create_initial_partitions */
514 * Add a partition to the touched set if not already there.
516 * @param part the partition
517 * @param env the environment
519 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
520 if (part->on_touched == 0) {
521 part->touched_next = env->touched;
523 part->on_touched = 1;
525 } /* add_to_touched */
528 * Add a node to the entry.partition.touched set if not already there.
532 static INLINE void add_to_partition_touched(node_t *y) {
533 if (y->on_touched == 0) {
534 partition_t *part = y->part;
536 y->next = part->touched;
541 } /* add_to_partition_touched */
544 * Update the worklist: If Z is on worklist then add Z' to worklist.
545 * Else add the smaller of Z and Z' to worklist.
547 * @param Z the Z partition
548 * @param Z_prime the Z' partition, a previous part of Z
549 * @param env the environment
551 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
552 if (Z->on_worklist || Z_prime->n_leaders < Z->n_leaders) {
553 add_to_worklist(Z_prime, env);
555 add_to_worklist(Z, env);
557 } /* update_worklist */
560 * Split a partition by a local list.
562 * @param Z the Z partition to split
563 * @param g a (non-empty) node list
564 * @param env the environment
566 * @return a new partition containing the nodes of g
568 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
569 partition_t *Z_prime;
572 int max_input, max_arity, arity;
574 dump_partition("Splitting ", Z);
578 /* Remove g from Z. */
579 for (node = g; node != NULL; node = node->next) {
580 list_del(&node->node_list);
583 assert(n < Z->n_leaders);
586 /* Move g to a new partition, Z
\92. */
587 Z_prime = new_partition(env);
588 max_arity = max_input = 0;
589 for (node = g; node != NULL; node = node->next) {
590 list_add(&node->node_list, &Z_prime->leaders);
591 node->part = Z_prime;
592 arity = get_irn_arity(node->node);
593 if (arity > max_arity)
595 if (node->max_user_input > max_input)
596 max_input = node->max_user_input;
598 Z_prime->max_user_inputs = max_input;
599 Z_prime->n_leaders = n;
601 /* for now, copy the type info tag. it will be adjusted
603 Z_prime->type_is_T_or_C = Z->type_is_T_or_C;
605 update_worklist(Z, Z_prime, env);
607 dump_partition("Now ", Z);
608 dump_partition("Created new ", Z_prime);
613 * Returns non-zero if the i'th input of a Phi node is live.
615 * @param phi a Phi-node
616 * @param i an input number
618 * @return non-zero if the i'th input of the given Phi node is live
620 static int is_live_input(ir_node *phi, int i) {
622 ir_node *block = get_nodes_block(phi);
623 ir_node *pred = get_Block_cfgpred(block, i);
624 lattice_elem_t type = get_node_type(pred);
626 return type.tv != tarval_unreachable;
628 /* else it's the control input, always live */
630 } /* is_live_input */
633 * Return non-zero if a type is a constant.
635 static int is_constant_type(lattice_elem_t type) {
636 if (type.tv != tarval_bottom && type.tv != tarval_top)
639 } /* is_constant_type */
642 * Place a node on the cprop list.
645 * @param env the environment
647 static void add_node_to_cprop(node_t *y, environment_t *env) {
648 /* Add y to y.partition.cprop. */
649 if (y->on_cprop == 0) {
650 partition_t *Y = y->part;
652 list_add_tail(&y->cprop_list, &Y->cprop);
655 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
657 /* place its partition on the cprop list */
658 if (Y->on_cprop == 0) {
659 Y->cprop_next = env->cprop;
664 if (get_irn_mode(y->node) == mode_T) {
665 /* mode_T nodes always produce tarval_bottom, so we must explicitly
666 add it's Proj's to get constant evaluation to work */
669 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
670 node_t *proj = get_irn_node(get_irn_out(y->node, i));
672 add_node_to_cprop(proj, env);
676 if (is_Block(y->node)) {
677 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
678 * if someone placed the block. The Block is only placed if the reachability
679 * changes, and this must be re-evaluated in compute_Phi(). */
681 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
682 node_t *p = get_irn_node(phi);
683 add_node_to_cprop(p, env);
686 } /* add_node_to_cprop */
689 * Check whether a type is neither Top or a constant.
690 * Note: U is handled like Top here, R is a constant.
692 * @param type the type to check
694 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
695 if (is_tarval(type.tv)) {
696 if (type.tv == tarval_top)
698 if (tarval_is_constant(type.tv))
708 * Collect nodes to the touched list.
710 * @param list the list which contains the nodes that must be evaluated
711 * @param idx the index of the def_use edge to evaluate
712 * @param env the environment
714 static void collect_touched(list_head *list, int idx, environment_t *env) {
716 int end_idx = env->end_idx;
718 list_for_each_entry(node_t, x, list, node_list) {
722 /* leader edges start AFTER follower edges */
723 x->next_edge = 1 + x->n_followers;
725 num_edges = get_irn_n_outs(x->node);
727 /* for all edges in x.L.def_use_{idx} */
728 while (x->next_edge <= num_edges) {
729 ir_def_use_edge *edge = &x->node->out[x->next_edge];
732 /* check if we have necessary edges */
740 /* ignore the "control input" for non-pinned nodes
741 if we are running in GCSE mode */
742 if (idx < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
745 y = get_irn_node(succ);
746 if (is_constant_type(y->type)) {
747 ir_opcode code = get_irn_opcode(succ);
748 if (code == iro_Sub || code == iro_Cmp)
749 add_node_to_cprop(y, env);
752 /* Partitions of constants should not be split simply because their Nodes have unequal
753 functions or incongruent inputs. */
754 if (type_is_neither_top_nor_const(y->type) &&
755 (! is_Phi(y->node) || is_live_input(y->node, idx))) {
756 partition_t *Y = y->part;
757 add_to_touched(Y, env);
758 add_to_partition_touched(y);
764 * Split the partitions if caused by the first entry on the worklist.
766 * @param env the environment
768 static void cause_splits(environment_t *env) {
773 /* remove the first partition from the worklist */
775 env->worklist = X->wl_next;
778 dump_partition("Cause_split: ", X);
780 /* combine temporary leader and follower list */
781 for (idx = -1; idx <= X->max_user_inputs; ++idx) {
782 /* empty the touched set: already done, just clear the list */
785 collect_touched(&X->leaders, idx, env);
786 collect_touched(&X->followers, idx, env);
788 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
789 /* remove it from the touched set */
792 if (Z->n_leaders != Z->n_touched) {
793 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
794 split(Z, Z->touched, env);
796 /* Empty local Z.touched. */
797 for (e = Z->touched; e != NULL; e = e->next) {
807 * Implements split_by_what(): Split a partition by characteristics given
808 * by the what function.
810 * @param X the partition to split
811 * @param What a function returning an Id for every node of the partition X
812 * @param P a list to store the result partitions
813 * @param env the environment
817 static partition_t *split_by_what(partition_t *X, what_func What,
818 partition_t **P, environment_t *env) {
821 listmap_entry_t *iter;
824 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
826 list_for_each_entry(node_t, x, &X->leaders, node_list) {
827 void *id = What(x, env);
828 listmap_entry_t *entry;
831 /* input not allowed, ignore */
834 /* Add x to map[What(x)]. */
835 entry = listmap_find(&map, id);
836 x->next = entry->list;
839 /* Let P be a set of Partitions. */
841 /* for all sets S except one in the range of map do */
842 for (iter = map.values; iter != NULL; iter = iter->next) {
843 if (iter->next == NULL) {
844 /* this is the last entry, ignore */
849 /* Add SPLIT( X, S ) to P. */
850 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
851 R = split(X, S, env);
861 } /* split_by_what */
863 /** lambda n.(n.type) */
864 static void *lambda_type(const node_t *node, environment_t *env) {
866 return node->type.tv;
869 /** lambda n.(n.opcode) */
870 static void *lambda_opcode(const node_t *node, environment_t *env) {
871 opcode_key_t key, *entry;
872 ir_node *irn = node->node;
874 key.code = get_irn_opcode(irn);
875 key.mode = get_irn_mode(irn);
879 switch (get_irn_opcode(irn)) {
881 key.u.proj = get_Proj_proj(irn);
884 key.u.ent = get_Sel_entity(irn);
890 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
892 } /* lambda_opcode */
894 /** lambda n.(n[i].partition) */
895 static void *lambda_partition(const node_t *node, environment_t *env) {
896 ir_node *skipped = skip_Proj(node->node);
899 int i = env->lambda_input;
901 if (i >= get_irn_arity(node->node)) {
902 /* we are outside the allowed range */
906 /* ignore the "control input" for non-pinned nodes
907 if we are running in GCSE mode */
908 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
911 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
912 p = get_irn_node(pred);
915 } /* lambda_partition */
918 * Returns true if a type is a constant.
920 static int is_con(const lattice_elem_t type) {
921 /* be conservative */
922 if (is_tarval(type.tv))
923 return tarval_is_constant(type.tv);
924 return is_entity(type.sym.entity_p);
928 * Implements split_by().
930 * @param X the partition to split
931 * @param env the environment
933 static void split_by(partition_t *X, environment_t *env) {
934 partition_t *I, *P = NULL;
937 if (X->n_leaders == 1) {
938 /* we have only one leader, no need to split, just check it's type */
939 node_t *x = get_first_node(X);
940 X->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
944 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
945 P = split_by_what(X, lambda_type, &P, env);
947 /* adjust the type tags, we have split partitions by type */
948 for (I = P; I != NULL; I = I->split_next) {
949 node_t *x = get_first_node(I);
950 I->type_is_T_or_C = x->type.tv == tarval_top || is_con(x->type);
957 if (Y->n_leaders > 1) {
958 lattice_elem_t type = get_partition_type(Y);
960 /* we do not want split the TOP or constant partitions */
961 if (type.tv != tarval_top && !is_con(type)) {
962 partition_t *Q = NULL;
964 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
965 Q = split_by_what(Y, lambda_opcode, &Q, env);
971 if (Z->n_leaders > 1) {
972 const node_t *first = get_first_node(Z);
973 int arity = get_irn_arity(first->node);
977 * BEWARE: during splitting by input 2 for instance we might
978 * create new partitions which are different by input 1, so collect
979 * them and split further.
981 Z->split_next = NULL;
984 for (input = arity - 1; input >= -1; --input) {
986 partition_t *Z_prime = R;
989 if (Z_prime->n_leaders > 1) {
990 env->lambda_input = input;
991 DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", input, Z_prime->nr));
992 S = split_by_what(Z_prime, lambda_partition, &S, env);
994 Z_prime->split_next = S;
1002 } while (Q != NULL);
1005 } while (P != NULL);
1009 * (Re-)compute the type for a given node.
1011 * @param node the node
1013 static void default_compute(node_t *node) {
1015 ir_node *irn = node->node;
1016 node_t *block = get_irn_node(get_nodes_block(irn));
1018 if (block->type.tv == tarval_unreachable) {
1019 node->type.tv = tarval_top;
1023 /* if any of the data inputs have type top, the result is type top */
1024 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1025 ir_node *pred = get_irn_n(irn, i);
1026 node_t *p = get_irn_node(pred);
1028 if (p->type.tv == tarval_top) {
1029 node->type.tv = tarval_top;
1034 if (get_irn_mode(node->node) == mode_X)
1035 node->type.tv = tarval_reachable;
1037 node->type.tv = computed_value(irn);
1038 } /* default_compute */
1041 * (Re-)compute the type for a Block node.
1043 * @param node the node
1045 static void compute_Block(node_t *node) {
1047 ir_node *block = node->node;
1049 if (block == get_irg_start_block(current_ir_graph)) {
1050 /* start block is always reachable */
1051 node->type.tv = tarval_reachable;
1055 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
1056 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
1058 if (pred->type.tv == tarval_reachable) {
1059 /* A block is reachable, if at least of predecessor is reachable. */
1060 node->type.tv = tarval_reachable;
1064 node->type.tv = tarval_top;
1065 } /* compute_Block */
1068 * (Re-)compute the type for a Bad node.
1070 * @param node the node
1072 static void compute_Bad(node_t *node) {
1073 /* Bad nodes ALWAYS compute Top */
1074 node->type.tv = tarval_top;
1078 * (Re-)compute the type for an Unknown node.
1080 * @param node the node
1082 static void compute_Unknown(node_t *node) {
1083 /* While Unknown nodes should compute Top this is dangerous:
1084 * a Top input to a Cond would lead to BOTH control flows unreachable.
1085 * While this is correct in the given semantics, it would destroy the Firm
1088 * It would be safe to compute Top IF it can be assured, that only Cmp
1089 * nodes are inputs to Conds. We check that first.
1090 * This is the way Frontends typically build Firm, but some optimizations
1091 * (cond_eval for instance) might replace them by Phib's...
1093 * For now, we compute bottom here.
1095 node->type.tv = tarval_bottom;
1096 } /* compute_Unknown */
1099 * (Re-)compute the type for a Jmp node.
1101 * @param node the node
1103 static void compute_Jmp(node_t *node) {
1104 node_t *block = get_irn_node(get_nodes_block(node->node));
1106 node->type = block->type;
1110 * (Re-)compute the type for the End node.
1112 * @param node the node
1114 static void compute_End(node_t *node) {
1115 /* the End node is NOT dead of course */
1116 node->type.tv = tarval_reachable;
1120 * (Re-)compute the type for a SymConst node.
1122 * @param node the node
1124 static void compute_SymConst(node_t *node) {
1125 ir_node *irn = node->node;
1126 node_t *block = get_irn_node(get_nodes_block(irn));
1128 if (block->type.tv == tarval_unreachable) {
1129 node->type.tv = tarval_top;
1132 switch (get_SymConst_kind(irn)) {
1133 case symconst_addr_ent:
1134 /* case symconst_addr_name: cannot handle this yet */
1135 node->type.sym = get_SymConst_symbol(irn);
1138 node->type.tv = computed_value(irn);
1140 } /* compute_SymConst */
1143 * (Re-)compute the type for a Phi node.
1145 * @param node the node
1147 static void compute_Phi(node_t *node) {
1149 ir_node *phi = node->node;
1150 lattice_elem_t type;
1152 /* if a Phi is in a unreachable block, its type is TOP */
1153 node_t *block = get_irn_node(get_nodes_block(phi));
1155 if (block->type.tv == tarval_unreachable) {
1156 node->type.tv = tarval_top;
1160 /* Phi implements the Meet operation */
1161 type.tv = tarval_top;
1162 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1163 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1164 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1166 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1167 /* ignore TOP inputs: We must check here for unreachable blocks,
1168 because Firm constants live in the Start Block are NEVER Top.
1169 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1170 comes from a unreachable input. */
1173 if (pred->type.tv == tarval_bottom) {
1174 node->type.tv = tarval_bottom;
1176 } else if (type.tv == tarval_top) {
1177 /* first constant found */
1179 } else if (type.tv != pred->type.tv) {
1180 /* different constants or tarval_bottom */
1181 node->type.tv = tarval_bottom;
1184 /* else nothing, constants are the same */
1190 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1192 * @param node the node
1194 static void compute_Add(node_t *node) {
1195 ir_node *sub = node->node;
1196 node_t *l = get_irn_node(get_Add_left(sub));
1197 node_t *r = get_irn_node(get_Add_right(sub));
1198 lattice_elem_t a = l->type;
1199 lattice_elem_t b = r->type;
1202 if (a.tv == tarval_top || b.tv == tarval_top) {
1203 node->type.tv = tarval_top;
1204 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1205 node->type.tv = tarval_bottom;
1207 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1208 must call tarval_add() first to handle this case! */
1209 if (is_tarval(a.tv)) {
1210 if (is_tarval(b.tv)) {
1211 node->type.tv = tarval_add(a.tv, b.tv);
1214 mode = get_tarval_mode(a.tv);
1215 if (a.tv == get_mode_null(mode)) {
1219 } else if (is_tarval(b.tv)) {
1220 mode = get_tarval_mode(b.tv);
1221 if (b.tv == get_mode_null(mode)) {
1226 node->type.tv = tarval_bottom;
1231 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1233 * @param node the node
1235 static void compute_Sub(node_t *node) {
1236 ir_node *sub = node->node;
1237 node_t *l = get_irn_node(get_Sub_left(sub));
1238 node_t *r = get_irn_node(get_Sub_right(sub));
1239 lattice_elem_t a = l->type;
1240 lattice_elem_t b = r->type;
1242 if (a.tv == tarval_top || b.tv == tarval_top) {
1243 node->type.tv = tarval_top;
1244 } else if (is_con(a) && is_con(b)) {
1245 if (is_tarval(a.tv) && is_tarval(b.tv)) {
1246 node->type.tv = tarval_sub(a.tv, b.tv, get_irn_mode(sub));
1247 } else if (is_tarval(a.tv) && tarval_is_null(a.tv)) {
1249 } else if (is_tarval(b.tv) && tarval_is_null(b.tv)) {
1252 node->type.tv = tarval_bottom;
1254 } else if (r->part == l->part &&
1255 (!mode_is_float(get_irn_mode(l->node)))) {
1256 if (node->type.tv == tarval_top) {
1258 * BEWARE: a - a is NOT always 0 for floating Point values, as
1259 * NaN op NaN = NaN, so we must check this here.
1261 ir_mode *mode = get_irn_mode(sub);
1262 node->type.tv = get_mode_null(mode);
1264 node->type.tv = tarval_bottom;
1267 node->type.tv = tarval_bottom;
1272 * (Re-)compute the type for Cmp.
1274 * @param node the node
1276 static void compute_Cmp(node_t *node) {
1277 ir_node *cmp = node->node;
1278 node_t *l = get_irn_node(get_Cmp_left(cmp));
1279 node_t *r = get_irn_node(get_Cmp_right(cmp));
1280 lattice_elem_t a = l->type;
1281 lattice_elem_t b = r->type;
1283 if (a.tv == tarval_top || b.tv == tarval_top) {
1284 node->type.tv = tarval_top;
1285 } else if (is_con(a) && is_con(b)) {
1286 /* both nodes are constants, we can probably do something */
1287 node->type.tv = tarval_b_true;
1288 } else if (r->part == l->part) {
1289 /* both nodes congruent, we can probably do something */
1290 node->type.tv = tarval_b_true;
1292 node->type.tv = tarval_bottom;
1294 } /* compute_Proj_Cmp */
1297 * (Re-)compute the type for a Proj(Cmp).
1299 * @param node the node
1300 * @param cond the predecessor Cmp node
1302 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1303 ir_node *proj = node->node;
1304 node_t *l = get_irn_node(get_Cmp_left(cmp));
1305 node_t *r = get_irn_node(get_Cmp_right(cmp));
1306 lattice_elem_t a = l->type;
1307 lattice_elem_t b = r->type;
1308 pn_Cmp pnc = get_Proj_proj(proj);
1310 if (a.tv == tarval_top || b.tv == tarval_top) {
1311 node->type.tv = tarval_top;
1312 } else if (is_con(a) && is_con(b)) {
1313 default_compute(node);
1314 } else if (r->part == l->part &&
1315 (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt)) {
1316 if (node->type.tv == tarval_top) {
1318 * BEWARE: a == a is NOT always True for floating Point values, as
1319 * NaN != NaN is defined, so we must check this here.
1321 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1323 node->type.tv = tarval_bottom;
1326 node->type.tv = tarval_bottom;
1328 } /* compute_Proj_Cmp */
1331 * (Re-)compute the type for a Proj(Cond).
1333 * @param node the node
1334 * @param cond the predecessor Cond node
1336 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1337 ir_node *proj = node->node;
1338 long pnc = get_Proj_proj(proj);
1339 ir_node *sel = get_Cond_selector(cond);
1340 node_t *selector = get_irn_node(sel);
1342 if (get_irn_mode(sel) == mode_b) {
1344 if (pnc == pn_Cond_true) {
1345 if (selector->type.tv == tarval_b_false) {
1346 node->type.tv = tarval_unreachable;
1347 } else if (selector->type.tv == tarval_b_true) {
1348 node->type.tv = tarval_reachable;
1349 } else if (selector->type.tv == tarval_bottom) {
1350 node->type.tv = tarval_reachable;
1352 assert(selector->type.tv == tarval_top);
1353 node->type.tv = tarval_unreachable;
1356 assert(pnc == pn_Cond_false);
1358 if (selector->type.tv == tarval_b_false) {
1359 node->type.tv = tarval_reachable;
1360 } else if (selector->type.tv == tarval_b_true) {
1361 node->type.tv = tarval_unreachable;
1362 } else if (selector->type.tv == tarval_bottom) {
1363 node->type.tv = tarval_reachable;
1365 assert(selector->type.tv == tarval_top);
1366 node->type.tv = tarval_unreachable;
1371 if (selector->type.tv == tarval_bottom) {
1372 node->type.tv = tarval_reachable;
1373 } else if (selector->type.tv == tarval_top) {
1374 node->type.tv = tarval_unreachable;
1376 long value = get_tarval_long(selector->type.tv);
1377 if (pnc == get_Cond_defaultProj(cond)) {
1378 /* default switch, have to check ALL other cases */
1381 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1382 ir_node *succ = get_irn_out(cond, i);
1386 if (value == get_Proj_proj(succ)) {
1387 /* we found a match, will NOT take the default case */
1388 node->type.tv = tarval_unreachable;
1392 /* all cases checked, no match, will take default case */
1393 node->type.tv = tarval_reachable;
1396 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1400 } /* compute_Proj_Cond */
1403 * (Re-)compute the type for a Proj-Node.
1405 * @param node the node
1407 static void compute_Proj(node_t *node) {
1408 ir_node *proj = node->node;
1409 ir_mode *mode = get_irn_mode(proj);
1410 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1411 ir_node *pred = get_Proj_pred(proj);
1413 if (block->type.tv == tarval_unreachable) {
1414 /* a Proj in a unreachable Block stay Top */
1415 node->type.tv = tarval_top;
1418 if (get_irn_node(pred)->type.tv == tarval_top) {
1419 /* if the predecessor is Top, its Proj follow */
1420 node->type.tv = tarval_top;
1424 if (mode == mode_M) {
1425 /* mode M is always bottom */
1426 node->type.tv = tarval_bottom;
1429 if (mode != mode_X) {
1431 compute_Proj_Cmp(node, pred);
1433 default_compute(node);
1436 /* handle mode_X nodes */
1438 switch (get_irn_opcode(pred)) {
1440 /* the Proj_X from the Start is always reachable.
1441 However this is already handled at the top. */
1442 node->type.tv = tarval_reachable;
1445 compute_Proj_Cond(node, pred);
1448 default_compute(node);
1450 } /* compute_Proj */
1453 * (Re-)compute the type for a Confirm.
1455 * @param node the node
1457 static void compute_Confirm(node_t *node) {
1458 ir_node *confirm = node->node;
1459 node_t *pred = get_irn_node(get_Confirm_value(confirm));
1461 if (get_Confirm_cmp(confirm) == pn_Cmp_Eq) {
1462 node_t *bound = get_irn_node(get_Confirm_bound(confirm));
1464 if (is_con(bound->type)) {
1465 /* is equal to a constant */
1466 node->type = bound->type;
1470 /* a Confirm is a copy OR a Const */
1471 node->type = pred->type;
1472 } /* compute_Confirm */
1475 * (Re-)compute the type for a Max.
1477 * @param node the node
1479 static void compute_Max(node_t *node) {
1480 ir_node *op = node->node;
1481 node_t *l = get_irn_node(get_binop_left(op));
1482 node_t *r = get_irn_node(get_binop_right(op));
1483 lattice_elem_t a = l->type;
1484 lattice_elem_t b = r->type;
1486 if (a.tv == tarval_top || b.tv == tarval_top) {
1487 node->type.tv = tarval_top;
1488 } else if (is_con(a) && is_con(b)) {
1489 /* both nodes are constants, we can probably do something */
1491 /* this case handles symconsts as well */
1494 ir_mode *mode = get_irn_mode(op);
1495 tarval *tv_min = get_mode_min(mode);
1499 else if (b.tv == tv_min)
1501 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1502 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1503 node->type.tv = a.tv;
1505 node->type.tv = b.tv;
1507 node->type.tv = tarval_bad;
1510 } else if (r->part == l->part) {
1511 /* both nodes congruent, we can probably do something */
1514 node->type.tv = tarval_bottom;
1519 * (Re-)compute the type for a Min.
1521 * @param node the node
1523 static void compute_Min(node_t *node) {
1524 ir_node *op = node->node;
1525 node_t *l = get_irn_node(get_binop_left(op));
1526 node_t *r = get_irn_node(get_binop_right(op));
1527 lattice_elem_t a = l->type;
1528 lattice_elem_t b = r->type;
1530 if (a.tv == tarval_top || b.tv == tarval_top) {
1531 node->type.tv = tarval_top;
1532 } else if (is_con(a) && is_con(b)) {
1533 /* both nodes are constants, we can probably do something */
1535 /* this case handles symconsts as well */
1538 ir_mode *mode = get_irn_mode(op);
1539 tarval *tv_max = get_mode_max(mode);
1543 else if (b.tv == tv_max)
1545 else if (is_tarval(a.tv) && is_tarval(b.tv)) {
1546 if (tarval_cmp(a.tv, b.tv) & pn_Cmp_Gt)
1547 node->type.tv = a.tv;
1549 node->type.tv = b.tv;
1551 node->type.tv = tarval_bad;
1554 } else if (r->part == l->part) {
1555 /* both nodes congruent, we can probably do something */
1558 node->type.tv = tarval_bottom;
1563 * (Re-)compute the type for a given node.
1565 * @param node the node
1567 static void compute(node_t *node) {
1570 if (is_no_Block(node->node)) {
1571 node_t *block = get_irn_node(get_nodes_block(node->node));
1573 if (block->type.tv == tarval_unreachable) {
1574 node->type.tv = tarval_top;
1579 func = (compute_func)node->node->op->ops.generic;
1585 * Identity functions: Note that one might thing that identity() is just a
1586 * synonym for equivalent_node(). While this is true, we cannot use it for the algorithm
1587 * here, because it expects that the identity node is one of the inputs, which is NOT
1588 * always true for equivalent_node() which can handle (and does sometimes) DAGs.
1589 * So, we have our own implementation, which copies some parts of equivalent_node()
1593 * Calculates the Identity for Phi nodes
1595 static node_t *identity_Phi(node_t *node) {
1596 ir_node *phi = node->node;
1597 ir_node *block = get_nodes_block(phi);
1598 node_t *n_part = NULL;
1601 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1602 node_t *pred_X = get_irn_node(get_Block_cfgpred(block, i));
1604 if (pred_X->type.tv == tarval_reachable) {
1605 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1609 else if (n_part->part != pred->part) {
1610 /* incongruent inputs, not a follower */
1615 /* if n_part is NULL here, all inputs path are dead, the Phi computes
1616 * tarval_top, is in the TOP partition and should NOT being split! */
1617 assert(n_part != NULL);
1619 } /* identity_Phi */
1622 * Calculates the Identity for commutative 0 neutral nodes.
1624 static node_t *identity_comm_zero_binop(node_t *node) {
1625 ir_node *op = node->node;
1626 node_t *a = get_irn_node(get_binop_left(op));
1627 node_t *b = get_irn_node(get_binop_right(op));
1628 ir_mode *mode = get_irn_mode(op);
1631 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1632 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1635 /* node: no input should be tarval_top, else the binop would be also
1636 * Top and not being split. */
1637 zero = get_mode_null(mode);
1638 if (a->type.tv == zero)
1640 if (b->type.tv == zero)
1643 } /* identity_comm_zero_binop */
1645 #define identity_Add identity_comm_zero_binop
1646 #define identity_Or identity_comm_zero_binop
1649 * Calculates the Identity for Mul nodes.
1651 static node_t *identity_Mul(node_t *node) {
1652 ir_node *op = node->node;
1653 node_t *a = get_irn_node(get_Mul_left(op));
1654 node_t *b = get_irn_node(get_Mul_right(op));
1655 ir_mode *mode = get_irn_mode(op);
1658 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1659 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1662 /* node: no input should be tarval_top, else the binop would be also
1663 * Top and not being split. */
1664 one = get_mode_one(mode);
1665 if (a->type.tv == one)
1667 if (b->type.tv == one)
1670 } /* identity_Mul */
1673 * Calculates the Identity for Sub nodes.
1675 static node_t *identity_Sub(node_t *node) {
1676 ir_node *sub = node->node;
1677 node_t *b = get_irn_node(get_Sub_right(sub));
1678 ir_mode *mode = get_irn_mode(sub);
1680 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1681 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1684 /* node: no input should be tarval_top, else the binop would be also
1685 * Top and not being split. */
1686 if (b->type.tv == get_mode_null(mode))
1687 return get_irn_node(get_Sub_left(sub));
1689 } /* identity_Mul */
1692 * Calculates the Identity for And nodes.
1694 static node_t *identity_And(node_t *node) {
1695 ir_node *and = node->node;
1696 node_t *a = get_irn_node(get_And_left(and));
1697 node_t *b = get_irn_node(get_And_right(and));
1698 tarval *neutral = get_mode_all_one(get_irn_mode(and));
1700 /* node: no input should be tarval_top, else the And would be also
1701 * Top and not being split. */
1702 if (a->type.tv == neutral)
1704 if (b->type.tv == neutral)
1707 } /* identity_And */
1710 * Calculates the Identity for Confirm nodes.
1712 static node_t *identity_Confirm(node_t *node) {
1713 ir_node *confirm = node->node;
1715 /* a Confirm is always a Copy */
1716 return get_irn_node(get_Confirm_value(confirm));
1717 } /* identity_Confirm */
1720 * Calculates the Identity for Mux nodes.
1722 static node_t *identity_Mux(node_t *node) {
1723 ir_node *mux = node->node;
1724 node_t *sel = get_irn_node(get_Mux_sel(mux));
1725 node_t *t = get_irn_node(get_Mux_true(mux));
1726 node_t *f = get_irn_node(get_Mux_false(mux));
1728 if (t->part == f->part)
1731 /* Mux sel input is mode_b, so it is always a tarval */
1732 if (sel->type.tv == tarval_b_true)
1734 if (sel->type.tv == tarval_b_false)
1737 } /* identity_Mux */
1740 * Calculates the Identity for Min nodes.
1742 static node_t *identity_Min(node_t *node) {
1743 ir_node *op = node->node;
1744 node_t *a = get_irn_node(get_binop_left(op));
1745 node_t *b = get_irn_node(get_binop_right(op));
1746 ir_mode *mode = get_irn_mode(op);
1749 if (a->part == b->part) {
1750 /* leader of multiple predecessors */
1754 /* works even with NaN */
1755 tv_max = get_mode_max(mode);
1756 if (a->type.tv == tv_max)
1758 if (b->type.tv == tv_max)
1761 } /* identity_Min */
1764 * Calculates the Identity for Max nodes.
1766 static node_t *identity_Max(node_t *node) {
1767 ir_node *op = node->node;
1768 node_t *a = get_irn_node(get_binop_left(op));
1769 node_t *b = get_irn_node(get_binop_right(op));
1770 ir_mode *mode = get_irn_mode(op);
1773 if (a->part == b->part) {
1774 /* leader of multiple predecessors */
1778 /* works even with NaN */
1779 tv_min = get_mode_min(mode);
1780 if (a->type.tv == tv_min)
1782 if (b->type.tv == tv_min)
1785 } /* identity_Max */
1788 * Calculates the Identity for nodes.
1790 static node_t *identity(node_t *node) {
1791 ir_node *irn = node->node;
1793 switch (get_irn_opcode(irn)) {
1795 return identity_Phi(node);
1797 return identity_Add(node);
1799 return identity_Or(node);
1801 return identity_Sub(node);
1803 return identity_Add(node);
1805 return identity_Confirm(node);
1807 return identity_Mux(node);
1809 return identity_Min(node);
1811 return identity_Max(node);
1818 * Node follower is a (new) follower of leader, segregate leaders
1821 static void segregate_def_use_chain_1(const ir_node *follower, node_t *leader) {
1822 ir_node *l = leader->node;
1823 int j, i, n = get_irn_n_outs(l);
1825 /* The leader edges must remain sorted, but follower edges can
1827 for (i = leader->n_followers + 1; i <= n; ++i) {
1828 if (l->out[i].use == follower) {
1829 ir_def_use_edge t = l->out[i];
1831 for (j = i - 1; j >= leader->n_followers + 1; --j)
1832 l->out[j + 1] = l->out[j];
1833 ++leader->n_followers;
1834 l->out[leader->n_followers] = t;
1836 /* note: a node might be a n-fold follower, for instance
1837 * if x = max(a,a), so no break here. */
1840 } /* segregate_def_use_chain_1 */
1843 * Node follower is a (new) follower of leader, segregate leaders
1844 * out edges. If follower is a n-congruent Input identity, all follower
1845 * inputs congruent to follower are also leader.
1847 static void segregate_def_use_chain(const ir_node *follower, node_t *leader) {
1848 ir_op *op = get_irn_op(follower);
1850 if (op == op_Phi || op == op_Mux || op == op_Max || op == op_Min) {
1851 /* n-Congruent Input Identity */
1854 DB((dbg, LEVEL_2, "n-Congruent follower %+F\n", follower));
1855 for (i = get_irn_arity(follower) - 1; i >= 0; --i) {
1856 node_t *pred = get_irn_node(get_irn_n(follower, i));
1858 if (pred->part == leader->part)
1859 segregate_def_use_chain_1(follower, pred);
1862 /* 1-Congruent Input Identity */
1863 segregate_def_use_chain_1(follower, leader);
1865 } /* segregate_def_use_chain */
1868 * Propagate constant evaluation.
1870 * @param env the environment
1872 static void propagate(environment_t *env) {
1875 lattice_elem_t old_type;
1877 unsigned n_fallen, old_type_was_T_or_C;
1880 while (env->cprop != NULL) {
1881 /* remove the first partition X from cprop */
1884 env->cprop = X->cprop_next;
1886 old_type_was_T_or_C = X->type_is_T_or_C;
1888 DB((dbg, LEVEL_2, "Propagate type on part%d\n", X->nr));
1891 while (! list_empty(&X->cprop)) {
1892 /* remove the first Node x from X.cprop */
1893 x = list_entry(X->cprop.next, node_t, cprop_list);
1894 list_del(&x->cprop_list);
1897 /* compute a new type for x */
1899 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1901 if (x->type.tv != old_type.tv) {
1902 verify_type(old_type, x->type);
1903 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1905 if (x->on_fallen == 0) {
1906 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
1907 not already on the list. */
1912 DB((dbg, LEVEL_2, "Add node %+F to fallen\n", x->node));
1914 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1915 ir_node *succ = get_irn_out(x->node, i);
1916 node_t *y = get_irn_node(succ);
1918 /* Add y to y.partition.cprop. */
1919 add_node_to_cprop(y, env);
1924 if (n_fallen > 0 && n_fallen != X->n_leaders) {
1925 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
1926 Y = split(X, fallen, env);
1930 /* remove the nodes from the fallen list */
1931 for (x = fallen; x != NULL; x = x->next)
1934 if (0 && old_type_was_T_or_C) {
1937 list_for_each_entry_safe(node_t, y, tmp, &Y->leaders, node_list) {
1938 node_t *eq_node = y;
1939 if (! is_con(y->type))
1940 eq_node = identity(y);
1943 /* move to followers */
1944 list_del(&y->node_list);
1947 list_add_tail(&y->node_list, &Y->followers);
1948 segregate_def_use_chain(y->node, eq_node);
1957 * Get the leader for a given node from its congruence class.
1959 * @param irn the node
1961 static ir_node *get_leader(node_t *node) {
1962 partition_t *part = node->part;
1964 if (part->n_leaders > 1) {
1965 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1967 return get_first_node(part)->node;
1973 * Return non-zero if the control flow predecessor node pred
1974 * is the only reachable control flow exit of its block.
1976 * @param pred the control flow exit
1978 static int can_exchange(ir_node *pred) {
1981 else if (is_Jmp(pred))
1983 else if (get_irn_mode(pred) == mode_T) {
1986 /* if the predecessor block has more than one
1987 reachable outputs we cannot remove the block */
1989 for (i = get_irn_n_outs(pred) - 1; i >= 0; --i) {
1990 ir_node *proj = get_irn_out(pred, i);
1993 /* skip non-control flow Proj's */
1994 if (get_irn_mode(proj) != mode_X)
1997 node = get_irn_node(proj);
1998 if (node->type.tv == tarval_reachable) {
2009 * Block Post-Walker, apply the analysis results on control flow by
2010 * shortening Phi's and Block inputs.
2012 static void apply_cf(ir_node *block, void *ctx) {
2013 environment_t *env = ctx;
2014 node_t *node = get_irn_node(block);
2016 ir_node **ins, **in_X;
2017 ir_node *phi, *next;
2019 if (block == get_irg_end_block(current_ir_graph) ||
2020 block == get_irg_start_block(current_ir_graph)) {
2021 /* the EndBlock is always reachable even if the analysis
2022 finds out the opposite :-) */
2025 if (node->type.tv == tarval_unreachable) {
2026 /* mark dead blocks */
2027 set_Block_dead(block);
2031 n = get_Block_n_cfgpreds(block);
2034 /* only one predecessor combine */
2035 ir_node *pred = skip_Proj(get_Block_cfgpred(block, 0));
2037 if (can_exchange(pred)) {
2038 exchange(block, get_nodes_block(pred));
2044 NEW_ARR_A(ir_node *, in_X, n);
2046 for (i = 0; i < n; ++i) {
2047 ir_node *pred = get_Block_cfgpred(block, i);
2048 node_t *node = get_irn_node(pred);
2050 if (node->type.tv == tarval_reachable) {
2057 NEW_ARR_A(ir_node *, ins, n);
2058 for (phi = get_Block_phis(block); phi != NULL; phi = next) {
2059 node_t *node = get_irn_node(phi);
2061 next = get_Phi_next(phi);
2062 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2063 /* this Phi is replaced by a constant */
2064 tarval *tv = node->type.tv;
2065 ir_node *c = new_r_Const(current_ir_graph, block, get_tarval_mode(tv), tv);
2067 set_irn_node(c, node);
2069 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, c));
2074 for (i = 0; i < n; ++i) {
2075 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
2077 if (pred->type.tv == tarval_reachable) {
2078 ins[j++] = get_Phi_pred(phi, i);
2082 /* this Phi is replaced by a single predecessor */
2083 ir_node *s = ins[0];
2086 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", phi, s));
2090 set_irn_in(phi, j, ins);
2097 /* this Block has only one live predecessor */
2098 ir_node *pred = skip_Proj(in_X[0]);
2100 if (can_exchange(pred)) {
2101 exchange(block, get_nodes_block(pred));
2105 set_irn_in(block, k, in_X);
2111 * Post-Walker, apply the analysis results;
2113 static void apply_result(ir_node *irn, void *ctx) {
2114 environment_t *env = ctx;
2115 node_t *node = get_irn_node(irn);
2117 if (is_Block(irn) || is_End(irn) || is_Bad(irn)) {
2118 /* blocks already handled, do not touch the End node */
2120 node_t *block = get_irn_node(get_nodes_block(irn));
2122 if (block->type.tv == tarval_unreachable) {
2123 ir_node *bad = get_irg_bad(current_ir_graph);
2125 /* here, bad might already have a node, but this can be safely ignored
2126 as long as bad has at least ONE valid node */
2127 set_irn_node(bad, node);
2129 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2133 else if (node->type.tv == tarval_unreachable) {
2134 ir_node *bad = get_irg_bad(current_ir_graph);
2136 /* see comment above */
2137 set_irn_node(bad, node);
2139 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
2143 else if (get_irn_mode(irn) == mode_X) {
2146 ir_node *cond = get_Proj_pred(irn);
2148 if (is_Cond(cond)) {
2149 node_t *sel = get_irn_node(get_Cond_selector(cond));
2151 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
2152 /* Cond selector is a constant, make a Jmp */
2153 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
2154 set_irn_node(jmp, node);
2156 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
2163 /* normal data node */
2164 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
2165 tarval *tv = node->type.tv;
2167 if (! is_Const(irn)) {
2168 /* can be replaced by a constant */
2169 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
2170 set_irn_node(c, node);
2172 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
2176 } else if (is_entity(node->type.sym.entity_p)) {
2177 if (! is_SymConst(irn)) {
2178 /* can be replaced by a Symconst */
2179 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
2180 set_irn_node(symc, node);
2183 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
2184 exchange(irn, symc);
2188 ir_node *leader = get_leader(node);
2190 if (leader != irn) {
2191 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
2192 exchange(irn, leader);
2198 } /* apply_result */
2200 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
2203 * sets the generic functions to compute.
2205 static void set_compute_functions(void) {
2208 /* set the default compute function */
2209 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
2210 ir_op *op = get_irp_opcode(i);
2211 op->ops.generic = (op_func)default_compute;
2214 /* set specific functions */
2233 } /* set_compute_functions */
2235 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
2236 ir_node *irn = local != NULL ? local : n;
2237 node_t *node = get_irn_node(irn);
2239 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
2243 void combo(ir_graph *irg) {
2245 ir_node *initial_bl;
2247 ir_graph *rem = current_ir_graph;
2249 current_ir_graph = irg;
2251 /* register a debug mask */
2252 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
2253 firm_dbg_set_mask(dbg, SET_LEVEL_3);
2255 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
2257 obstack_init(&env.obst);
2258 env.worklist = NULL;
2262 #ifdef DEBUG_libfirm
2263 env.dbg_list = NULL;
2265 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
2266 env.type2id_map = pmap_create();
2267 env.end_idx = get_opt_global_cse() ? 0 : -1;
2268 env.lambda_input = 0;
2271 assure_irg_outs(irg);
2273 /* we have our own value_of function */
2274 set_value_of_func(get_node_tarval);
2276 set_compute_functions();
2277 DEBUG_ONLY(part_nr = 0);
2279 /* create the initial partition and place it on the work list */
2280 env.initial = new_partition(&env);
2281 add_to_worklist(env.initial, &env);
2282 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
2284 /* all nodes on the initial partition have type Top */
2285 env.initial->type_is_T_or_C = 1;
2287 /* Place the START Node's partition on cprop.
2288 Place the START Node on its local worklist. */
2289 initial_bl = get_irg_start_block(irg);
2290 start = get_irn_node(initial_bl);
2291 add_node_to_cprop(start, &env);
2295 if (env.worklist != NULL)
2297 } while (env.cprop != NULL || env.worklist != NULL);
2299 dump_all_partitions(&env);
2302 set_dump_node_vcgattr_hook(dump_partition_hook);
2303 dump_ir_block_graph(irg, "-partition");
2304 set_dump_node_vcgattr_hook(NULL);
2306 (void)dump_partition_hook;
2309 /* apply the result */
2310 irg_block_walk_graph(irg, NULL, apply_cf, &env);
2311 irg_walk_graph(irg, NULL, apply_result, &env);
2314 /* control flow might changed */
2315 set_irg_outs_inconsistent(irg);
2316 set_irg_extblk_inconsistent(irg);
2317 set_irg_doms_inconsistent(irg);
2318 set_irg_loopinfo_inconsistent(irg);
2321 pmap_destroy(env.type2id_map);
2322 del_set(env.opcode2id_map);
2323 obstack_free(&env.obst, NULL);
2325 /* restore value_of() default behavior */
2326 set_value_of_func(NULL);
2327 current_ir_graph = rem;