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 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, nevertheless we call it type here for "maximum compatibility".
36 #include "iroptimize.h"
44 #include "irgraph_t.h"
59 /* define this to check that all type translations are monotone */
60 #define VERIFY_MONOTONE
62 typedef struct node_t node_t;
63 typedef struct partition_t partition_t;
64 typedef struct opcode_key_t opcode_key_t;
65 typedef struct listmap_entry_t listmap_entry_t;
67 /** The type of the compute function. */
68 typedef void (*compute_func)(node_t *node);
74 ir_opcode code; /**< The Firm opcode. */
75 ir_mode *mode; /**< The mode of all nodes in the partition. */
77 long proj; /**< For Proj nodes, its proj number */
78 ir_entity *ent; /**< For Sel Nodes, its entity */
83 * An entry in the list_map.
85 struct listmap_entry_t {
86 void *id; /**< The id. */
87 node_t *list; /**< The associated list for this id. */
88 listmap_entry_t *next; /**< Link to the next entry in the map. */
91 /** We must map id's to lists. */
92 typedef struct listmap_t {
93 set *map; /**< Map id's to listmap_entry_t's */
94 listmap_entry_t *values; /**< List of all values in the map. */
98 * A lattice element. Because we handle constants and symbolic constants different, we
99 * have to use this union.
110 ir_node *node; /**< The IR-node itself. */
111 list_head node_list; /**< Double-linked list of entries. */
112 list_head cprop_list; /**< Double-linked partition.cprop list. */
113 partition_t *part; /**< points to the partition this node belongs to */
114 node_t *next; /**< Next node on local list (partition.touched, fallen). */
115 lattice_elem_t type; /**< The associated lattice element "type". */
116 int max_user_input; /**< Maximum input number of Def-Use edges. */
117 int next_edge; /**< Index of the next Def-Use edge to use. */
118 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
119 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
120 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
124 * A partition containing congruent nodes.
127 list_head entries; /**< The head of partition node list. */
128 list_head cprop; /**< The head of partition.cprop list. */
129 partition_t *wl_next; /**< Next entry in the work list if any. */
130 partition_t *touched_next; /**< Points to the next partition in the touched set. */
131 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
132 node_t *touched; /**< The partition.touched set of this partition. */
133 unsigned n_nodes; /**< Number of entries in this partition. */
134 unsigned n_touched; /**< Number of entries in the partition.touched. */
135 int max_arity; /**< Maximum arity of all entries. */
136 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
137 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
138 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
139 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
141 partition_t *dbg_next; /**< Link all partitions for debugging */
142 unsigned nr; /**< A unique number for (what-)mapping, >0. */
146 typedef struct environment_t {
147 struct obstack obst; /**< obstack to allocate data structures. */
148 partition_t *worklist; /**< The work list. */
149 partition_t *cprop; /**< The constant propagation list. */
150 partition_t *touched; /**< the touched set. */
151 partition_t *initial; /**< The initial partition. */
152 set *opcode2id_map; /**< The opcodeMode->id map. */
153 pmap *type2id_map; /**< The type->id map. */
154 int end_idx; /**< -1 for local and 0 for global congruences. */
155 int lambda_input; /**< Captured argument for lambda_partition(). */
157 partition_t *dbg_list; /**< List of all partitions. */
161 /** Type of the what function. */
162 typedef void *(*what_func)(const node_t *node, environment_t *env);
164 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
165 #define set_irn_node(irn, node) set_irn_link(irn, node)
167 /** The debug module handle. */
168 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
170 /** Next partition number. */
171 DEBUG_ONLY(static unsigned part_nr = 0);
174 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
177 * Dump partition to output.
179 static void dump_partition(const char *msg, const partition_t *part) {
182 lattice_elem_t type = get_partition_type(part);
184 DB((dbg, LEVEL_2, "%s part%u (%u, %+F) {\n ", msg, part->nr, part->n_nodes, type));
185 list_for_each_entry(node_t, node, &part->entries, node_list) {
186 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
189 DB((dbg, LEVEL_2, "\n}\n"));
193 * Dump all partitions.
195 static void dump_all_partitions(const environment_t *env) {
196 const partition_t *P;
198 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
199 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
200 dump_partition("", P);
204 #define dump_partition(msg, part)
205 #define dump_all_partitions(env)
208 #if defined(VERIFY_MONOTONE) && defined (DEBUG_libfirm)
210 * Verify that a type transition is monotone
212 static void verify_type(const lattice_elem_t old_type, const lattice_elem_t new_type) {
213 if (old_type.tv == new_type.tv) {
217 if (old_type.tv == tarval_top) {
218 /* from Top down-to is always allowed */
221 if (old_type.tv == tarval_unreachable) {
222 if (new_type.tv == tarval_reachable) {
226 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
228 if (new_type.tv == tarval_bottom) {
232 panic("verify_type(): wrong translation from %+F to %+F", old_type, new_type);
235 #define verify_type(old_type, new_type)
239 * Return the "top" value depending on the mode
241 static tarval *get_top_value(const ir_mode *mode) {
242 return (mode == mode_X || mode == mode_BB) ? tarval_unreachable : tarval_top;
246 * Compare two pointer values of a listmap.
248 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
249 const listmap_entry_t *e1 = elt;
250 const listmap_entry_t *e2 = key;
253 return e1->id != e2->id;
254 } /* listmap_cmp_ptr */
257 * Initializes a listmap.
259 * @param map the listmap
261 static void listmap_init(listmap_t *map) {
262 map->map = new_set(listmap_cmp_ptr, 16);
267 * Terminates a listmap.
269 * @param map the listmap
271 static void listmap_term(listmap_t *map) {
276 * Return the associated listmap entry for a given id.
278 * @param map the listmap
279 * @param id the id to search for
281 * @return the asociated listmap entry for the given id
283 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
284 listmap_entry_t key, *entry;
289 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
291 if (entry->list == NULL) {
292 /* a new entry, put into the list */
293 entry->next = map->values;
300 * Calculate the hash value for an opcode map entry.
302 * @param entry an opcode map entry
304 * @return a hash value for the given opcode map entry
306 static unsigned opcode_hash(const opcode_key_t *entry) {
307 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
311 * Compare two entries in the opcode map.
313 static int cmp_opcode(const void *elt, const void *key, size_t size) {
314 const opcode_key_t *o1 = elt;
315 const opcode_key_t *o2 = key;
318 return o1->code != o2->code || o1->mode != o2->mode ||
319 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
323 * Compare two Def-Use edges for input position.
325 static int cmp_def_use_edge(const void *a, const void *b) {
326 const ir_def_use_edge *ea = a;
327 const ir_def_use_edge *eb = b;
329 /* no overrun, because range is [-1, MAXINT] */
330 return ea->pos - eb->pos;
331 } /* cmp_def_use_edge */
334 * We need the Def-Use edges sorted.
336 static void sort_irn_outs(node_t *node) {
337 ir_node *irn = node->node;
338 int n_outs = get_irn_n_outs(irn);
341 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
343 node->max_user_input = irn->out[n_outs + 1].pos;
344 } /* sort_irn_outs */
347 * Return the type of a node.
349 * @param irn an IR-node
351 * @return the associated type of this node
353 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
354 return get_irn_node(irn)->type;
355 } /* get_node_type */
358 * Return the tarval of a node.
360 * @param irn an IR-node
362 * @return the associated type of this node
364 static INLINE tarval *get_node_tarval(const ir_node *irn) {
365 lattice_elem_t type = get_node_type(irn);
367 if (is_tarval(type.tv))
369 return tarval_bottom;
370 } /* get_node_type */
373 * Add a partition to the worklist.
375 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
376 assert(X->on_worklist == 0);
377 X->wl_next = env->worklist;
383 * Create a new empty partition.
385 * @param env the environment
387 * @return a newly allocated partition
389 static INLINE partition_t *new_partition(environment_t *env) {
390 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
392 INIT_LIST_HEAD(&part->entries);
393 INIT_LIST_HEAD(&part->cprop);
394 part->wl_next = NULL;
395 part->touched_next = NULL;
396 part->cprop_next = NULL;
397 part->touched = NULL;
401 part->max_user_inputs = 0;
402 part->on_worklist = 0;
403 part->on_touched = 0;
406 part->dbg_next = env->dbg_list;
407 env->dbg_list = part;
408 part->nr = part_nr++;
412 } /* new_partition */
415 * Get the first node from a partition.
417 static INLINE node_t *get_first_node(const partition_t *X) {
418 return list_entry(X->entries.next, node_t, node_list);
422 * Return the type of a partition (assuming partition is non-empty and
423 * all elements have the same type).
425 * @param X a partition
427 * @return the type of the first element of the partition
429 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
430 const node_t *first = get_first_node(X);
432 } /* get_partition_type */
435 * Creates a partition node for the given IR-node and place it
436 * into the given partition.
438 * @param irn an IR-node
439 * @param part a partition to place the node in
440 * @param env the environment
442 * @return the created node
444 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
445 /* create a partition node and place it in the partition */
446 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
447 ir_mode *mode = get_irn_mode(irn);
449 INIT_LIST_HEAD(&node->node_list);
450 INIT_LIST_HEAD(&node->cprop_list);
454 node->type.tv = get_top_value(mode);
455 node->max_user_input = 0;
457 node->on_touched = 0;
460 set_irn_node(irn, node);
462 list_add_tail(&node->node_list, &part->entries);
466 } /* create_partition_node */
469 * Pre-Walker, init all Block-Phi lists.
471 static void init_block_phis(ir_node *irn, void *env) {
475 set_Block_phis(irn, NULL);
480 * Post-Walker, initialize all Nodes' type to U or top and place
481 * all nodes into the TOP partition.
483 static void create_initial_partitions(ir_node *irn, void *ctx) {
484 environment_t *env = ctx;
485 partition_t *part = env->initial;
489 node = create_partition_node(irn, part, env);
491 arity = get_irn_arity(irn);
492 if (arity > part->max_arity)
493 part->max_arity = arity;
494 if (node->max_user_input > part->max_user_inputs)
495 part->max_user_inputs = node->max_user_input;
498 add_Block_phi(get_nodes_block(irn), irn);
500 } /* create_initial_partitions */
503 * Add a partition to the touched set if not already there.
505 * @param part the partition
506 * @param env the environment
508 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
509 if (part->on_touched == 0) {
510 part->touched_next = env->touched;
512 part->on_touched = 1;
514 } /* add_to_touched */
517 * Add a node to the entry.partition.touched set if not already there.
521 static INLINE void add_to_partition_touched(node_t *y) {
522 if (y->on_touched == 0) {
523 partition_t *part = y->part;
525 y->next = part->touched;
530 } /* add_to_partition_touched */
533 * Update the worklist: If Z is on worklist then add Z' to worklist.
534 * Else add the smaller of Z and Z' to worklist.
536 * @param Z the Z partition
537 * @param Z_prime the Z' partition, a previous part of Z
538 * @param env the environment
540 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
541 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
542 add_to_worklist(Z_prime, env);
544 add_to_worklist(Z, env);
546 } /* update_worklist */
549 * Split a partition by a local list.
551 * @param Z the Z partition to split
552 * @param g a (non-empty) node list
553 * @param env the environment
555 * @return a new partition containing the nodes of g
557 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
558 partition_t *Z_prime;
561 int max_input, max_arity, arity;
563 dump_partition("Splitting ", Z);
567 /* Remove g from Z. */
568 for (node = g; node != NULL; node = node->next) {
569 list_del(&node->node_list);
572 assert(n < Z->n_nodes);
575 /* Move g to a new partition, Z
\92. */
576 Z_prime = new_partition(env);
577 max_arity = max_input = 0;
578 for (node = g; node != NULL; node = node->next) {
579 list_add(&node->node_list, &Z_prime->entries);
580 node->part = Z_prime;
581 arity = get_irn_arity(node->node);
582 if (arity > max_arity)
584 if (node->max_user_input > max_input)
585 max_input = node->max_user_input;
587 Z_prime->max_arity = max_arity;
588 Z_prime->max_user_inputs = max_input;
589 Z_prime->n_nodes = n;
591 update_worklist(Z, Z_prime, env);
593 dump_partition("Now ", Z);
594 dump_partition("Created new ", Z_prime);
599 * Returns non-zero if the i'th input of a Phi node is live.
601 * @param phi a Phi-node
602 * @param i an input number
604 * @return non-zero if the i'th input of the given Phi node is live
606 static int is_live_input(ir_node *phi, int i) {
608 ir_node *block = get_nodes_block(phi);
609 ir_node *pred = get_Block_cfgpred(block, i);
610 lattice_elem_t type = get_node_type(pred);
612 return type.tv != tarval_unreachable;
614 /* else it's the control input, always live */
616 } /* is_live_input */
619 * Return non-zero if a type is a constant.
621 static int is_constant_type(lattice_elem_t type) {
622 if (type.tv != tarval_bottom && type.tv != tarval_top)
625 } /* is_constant_type */
628 * Place a node on the cprop list.
631 * @param env the environment
633 static void add_node_to_cprop(node_t *y, environment_t *env) {
634 /* Add y to y.partition.cprop. */
635 if (y->on_cprop == 0) {
636 partition_t *Y = y->part;
638 list_add_tail(&y->cprop_list, &Y->cprop);
641 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
643 /* place its partition on the cprop list */
644 if (Y->on_cprop == 0) {
645 Y->cprop_next = env->cprop;
650 if (get_irn_mode(y->node) == mode_T) {
651 /* mode_T nodes always produce tarval_bottom, so we must explicitly
652 add it's Proj's to get constant evaluation to work */
655 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
656 node_t *proj = get_irn_node(get_irn_out(y->node, i));
658 add_node_to_cprop(proj, env);
661 if (is_Block(y->node)) {
662 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
663 * if someone placed the block. The Block is only placed if the reachability
664 * changes, and this must be re-evaluated in compute_Phi(). */
666 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
667 node_t *p = get_irn_node(phi);
668 add_node_to_cprop(p, env);
671 } /* add_node_to_cprop */
674 * Check whether a type is neither Top or a constant.
675 * Note: U, R are NOT constants!
677 * @param type the type to check
679 static int type_is_neither_top_nor_const(const lattice_elem_t type) {
680 if (is_tarval(type.tv)) {
681 if (type.tv == tarval_top)
683 if (tarval_is_constant(type.tv))
693 * Split the partitions if caused by the first entry on the worklist.
695 * @param env the environment
697 static void cause_splits(environment_t *env) {
698 partition_t *X, *Y, *Z;
704 /* remove the first partition from the worklist */
706 env->worklist = X->wl_next;
709 dump_partition("Cause_split: ", X);
710 end_idx = env->end_idx;
711 for (i = -1; i <= X->max_user_inputs; ++i) {
712 /* empty the touched set: already done, just clear the list */
715 list_for_each_entry(node_t, x, &X->entries, node_list) {
721 num_edges = get_irn_n_outs(x->node);
723 while (x->next_edge <= num_edges) {
724 ir_def_use_edge *edge = &x->node->out[x->next_edge];
726 /* check if we have necessary edges */
734 /* ignore the "control input" for non-pinned nodes
735 if we are running in GCSE mode */
736 if (i < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
739 y = get_irn_node(succ);
740 if (is_constant_type(y->type)) {
741 code = get_irn_opcode(succ);
742 if (code == iro_Sub || (code == iro_Proj && is_Cmp(get_Proj_pred(succ))))
743 add_node_to_cprop(y, env);
746 /* Partitions of constants should not be split simply because their Nodes have unequal
747 functions or incongruent inputs. */
748 if (type_is_neither_top_nor_const(y->type) &&
749 (! is_Phi(y->node) || is_live_input(y->node, i))) {
751 add_to_touched(Y, env);
752 add_to_partition_touched(y);
757 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
758 /* remove it from the touched set */
761 if (Z->n_nodes != Z->n_touched) {
762 DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
763 split(Z, Z->touched, env);
765 /* Empty local Z.touched. */
766 for (e = Z->touched; e != NULL; e = e->next) {
776 * Implements split_by_what(): Split a partition by characteristics given
777 * by the what function.
779 * @param X the partition to split
780 * @param What a function returning an Id for every node of the partition X
781 * @param P an flexible array to store the result partitions or NULL
782 * @param env the environment
784 * @return if P != NULL P will be filled with the resulting partitions and returned
786 static partition_t **split_by_what(partition_t *X, what_func What,
787 partition_t **P, environment_t *env) {
790 listmap_entry_t *iter;
793 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
795 list_for_each_entry(node_t, x, &X->entries, node_list) {
796 void *id = What(x, env);
797 listmap_entry_t *entry;
800 /* input not allowed, ignore */
803 /* Add x to map[What(x)]. */
804 entry = listmap_find(&map, id);
805 x->next = entry->list;
808 /* Let P be a set of Partitions. */
810 /* for all sets S except one in the range of map do */
811 for (iter = map.values; iter != NULL; iter = iter->next) {
812 if (iter->next == NULL) {
813 /* this is the last entry, ignore */
818 /* Add SPLIT( X, S ) to P. */
819 DB((dbg, LEVEL_2, "Split part%d by what\n", X->nr));
820 R = split(X, S, env);
822 ARR_APP1(partition_t *, P, R);
827 ARR_APP1(partition_t *, P, X);
832 } /* split_by_what */
834 /** lambda n.(n.type) */
835 static void *lambda_type(const node_t *node, environment_t *env) {
837 return node->type.tv;
840 /** lambda n.(n.opcode) */
841 static void *lambda_opcode(const node_t *node, environment_t *env) {
842 opcode_key_t key, *entry;
843 ir_node *irn = node->node;
845 key.code = get_irn_opcode(irn);
846 key.mode = get_irn_mode(irn);
850 switch (get_irn_opcode(irn)) {
852 key.u.proj = get_Proj_proj(irn);
855 key.u.ent = get_Sel_entity(irn);
861 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
863 } /* lambda_opcode */
865 /** lambda n.(n[i].partition) */
866 static void *lambda_partition(const node_t *node, environment_t *env) {
867 ir_node *skipped = skip_Proj(node->node);
870 int i = env->lambda_input;
872 if (i >= get_irn_arity(node->node)) {
873 /* we are outside the allowed range */
877 /* ignore the "control input" for non-pinned nodes
878 if we are running in GCSE mode */
879 if (i < env->end_idx && get_irn_pinned(skipped) != op_pin_state_pinned)
882 pred = i == -1 ? get_irn_n(skipped, i) : get_irn_n(node->node, i);
883 p = get_irn_node(pred);
886 } /* lambda_partition */
889 * Checks whether a type is a constant.
891 static int is_type_constant(lattice_elem_t type) {
892 if (is_tarval(type.tv))
893 return tarval_is_constant(type.tv);
894 /* else it is a symconst */
899 * Implements split_by().
901 * @param X the partition to split
902 * @param env the environment
904 static void split_by(partition_t *X, environment_t *env) {
905 partition_t **P = NEW_ARR_F(partition_t *, 0);
908 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.type) on part%d\n", X->nr));
909 P = split_by_what(X, lambda_type, P, env);
910 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
911 partition_t *Y = P[i];
913 if (Y->n_nodes > 1) {
914 lattice_elem_t type = get_partition_type(Y);
916 /* we do not want split the TOP, unreachable or constant partitions */
917 if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
918 partition_t **Q = NEW_ARR_F(partition_t *, 0);
920 DB((dbg, LEVEL_2, "WHAT = lambda n.(n.opcode) on part%d\n", Y->nr));
921 Q = split_by_what(Y, lambda_opcode, Q, env);
923 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
924 partition_t *Z = Q[j];
926 for (k = Z->max_arity - 1; k >= -1; --k) {
927 if (Z->n_nodes > 1) {
928 env->lambda_input = k;
929 DB((dbg, LEVEL_2, "WHAT = lambda n.(n[%d].partition) on part%d\n", k, Z->nr));
930 split_by_what(Z, lambda_partition, NULL, env);
942 * (Re-)compute the type for a given node.
944 * @param node the node
946 static void default_compute(node_t *node) {
948 ir_node *irn = node->node;
949 tarval *top = tarval_top;
951 if (get_irn_mode(node->node) == mode_X)
952 top = tarval_unreachable;
954 if (get_irn_pinned(irn) == op_pin_state_pinned) {
955 node_t *block = get_irn_node(get_nodes_block(irn));
957 if (block->type.tv == tarval_unreachable) {
963 /* if any of the data inputs have type top, the result is type top */
964 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
965 ir_node *pred = get_irn_n(irn, i);
966 node_t *p = get_irn_node(pred);
968 if (p->type.tv == tarval_top) {
974 if (get_irn_mode(node->node) == mode_X)
975 node->type.tv = tarval_reachable;
977 node->type.tv = computed_value(irn);
978 } /* default_compute */
981 * (Re-)compute the type for a Block node.
983 * @param node the node
985 static void compute_Block(node_t *node) {
987 ir_node *block = node->node;
989 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
990 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
992 if (pred->type.tv == tarval_reachable) {
993 /* A block is reachable, if at least of predecessor is reachable. */
994 node->type.tv = tarval_reachable;
998 node->type.tv = tarval_unreachable;
999 } /* compute_Block */
1002 * (Re-)compute the type for a Jmp node.
1004 * @param node the node
1006 static void compute_Jmp(node_t *node) {
1007 node_t *block = get_irn_node(get_nodes_block(node->node));
1009 node->type = block->type;
1013 * (Re-)compute the type for the End node.
1015 * @param node the node
1017 static void compute_End(node_t *node) {
1018 /* the End node is NOT dead of course */
1019 node->type.tv = tarval_reachable;
1023 * (Re-)compute the type for a SymConst node.
1025 * @param node the node
1027 static void compute_SymConst(node_t *node) {
1028 ir_node *irn = node->node;
1029 node_t *block = get_irn_node(get_nodes_block(irn));
1031 if (block->type.tv == tarval_unreachable) {
1032 node->type.tv = tarval_top;
1035 switch (get_SymConst_kind(irn)) {
1036 case symconst_addr_ent:
1037 /* case symconst_addr_name: cannot handle this yet */
1038 node->type.sym = get_SymConst_symbol(irn);
1041 node->type.tv = computed_value(irn);
1043 } /* compute_SymConst */
1046 * (Re-)compute the type for a Phi node.
1048 * @param node the node
1050 static void compute_Phi(node_t *node) {
1052 ir_node *phi = node->node;
1053 lattice_elem_t type;
1055 /* if a Phi is in a unreachable block, its type is TOP */
1056 node_t *block = get_irn_node(get_nodes_block(phi));
1058 if (block->type.tv == tarval_unreachable) {
1059 node->type.tv = tarval_top;
1063 /* Phi implements the Meet operation */
1064 type.tv = tarval_top;
1065 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1066 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1067 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1069 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1070 /* ignore TOP inputs: We must check here for unreachable blocks,
1071 because Firm constants live in the Start Block are NEVER Top.
1072 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1073 comes from a unreachable input. */
1076 if (pred->type.tv == tarval_bottom) {
1077 node->type.tv = tarval_bottom;
1079 } else if (type.tv == tarval_top) {
1080 /* first constant found */
1082 } else if (type.tv != pred->type.tv) {
1083 /* different constants or tarval_bottom */
1084 node->type.tv = tarval_bottom;
1087 /* else nothing, constants are the same */
1093 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1095 * @param node the node
1097 static void compute_Add(node_t *node) {
1098 ir_node *sub = node->node;
1099 node_t *l = get_irn_node(get_Add_left(sub));
1100 node_t *r = get_irn_node(get_Add_right(sub));
1101 lattice_elem_t a = l->type;
1102 lattice_elem_t b = r->type;
1103 node_t *block = get_irn_node(get_nodes_block(sub));
1106 if (block->type.tv == tarval_unreachable) {
1107 node->type.tv = tarval_top;
1111 if (a.tv == tarval_top || b.tv == tarval_top) {
1112 node->type.tv = tarval_top;
1113 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1114 node->type.tv = tarval_bottom;
1116 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1117 must call tarval_add() first to handle this case! */
1118 if (is_tarval(a.tv)) {
1119 if (is_tarval(b.tv)) {
1120 node->type.tv = tarval_add(a.tv, b.tv);
1123 mode = get_tarval_mode(a.tv);
1124 if (a.tv == get_mode_null(mode)) {
1128 } else if (is_tarval(b.tv)) {
1129 mode = get_tarval_mode(b.tv);
1130 if (b.tv == get_mode_null(mode)) {
1135 node->type.tv = tarval_bottom;
1140 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1142 * @param node the node
1144 static void compute_Sub(node_t *node) {
1145 ir_node *sub = node->node;
1146 node_t *l = get_irn_node(get_Sub_left(sub));
1147 node_t *r = get_irn_node(get_Sub_right(sub));
1148 lattice_elem_t a = l->type;
1149 lattice_elem_t b = r->type;
1150 node_t *block = get_irn_node(get_nodes_block(sub));
1152 if (block->type.tv == tarval_unreachable) {
1153 node->type.tv = tarval_top;
1157 if (a.tv == tarval_top || b.tv == tarval_top) {
1158 node->type.tv = tarval_top;
1159 } else if (r->part == l->part) {
1160 ir_mode *mode = get_irn_mode(sub);
1161 node->type.tv = get_mode_null(mode);
1162 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1163 node->type.tv = tarval_bottom;
1165 if (is_tarval(a.tv) && is_tarval(b.tv))
1166 node->type.tv = tarval_sub(a.tv, b.tv);
1168 node->type.tv = tarval_bottom;
1173 * (Re-)compute the type for a Proj(Cmp).
1175 * @param node the node
1176 * @param cond the predecessor Cmp node
1178 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1179 ir_node *proj = node->node;
1180 node_t *l = get_irn_node(get_Cmp_left(cmp));
1181 node_t *r = get_irn_node(get_Cmp_right(cmp));
1182 lattice_elem_t a = l->type;
1183 lattice_elem_t b = r->type;
1184 pn_Cmp pnc = get_Proj_proj(proj);
1187 * BEWARE: a == a is NOT always True for floating Point values, as
1188 * NaN != NaN is defined, so we must check this here.
1190 if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt) {
1191 if (a.tv == tarval_top || b.tv == tarval_top) {
1192 node->type.tv = tarval_top;
1193 } else if (r->part == l->part) {
1194 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1195 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1196 node->type.tv = tarval_bottom;
1198 default_compute(node);
1201 default_compute(node);
1203 } /* compute_Proj_Cmp */
1206 * (Re-)compute the type for a Proj(Cond).
1208 * @param node the node
1209 * @param cond the predecessor Cond node
1211 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1212 ir_node *proj = node->node;
1213 long pnc = get_Proj_proj(proj);
1214 ir_node *sel = get_Cond_selector(cond);
1215 node_t *selector = get_irn_node(sel);
1217 if (get_irn_mode(sel) == mode_b) {
1219 if (pnc == pn_Cond_true) {
1220 if (selector->type.tv == tarval_b_false) {
1221 node->type.tv = tarval_unreachable;
1222 } else if (selector->type.tv == tarval_b_true) {
1223 node->type.tv = tarval_reachable;
1224 } else if (selector->type.tv == tarval_bottom) {
1225 node->type.tv = tarval_reachable;
1227 assert(selector->type.tv == tarval_top);
1228 node->type.tv = tarval_unreachable;
1231 assert(pnc == pn_Cond_false);
1233 if (selector->type.tv == tarval_b_false) {
1234 node->type.tv = tarval_reachable;
1235 } else if (selector->type.tv == tarval_b_true) {
1236 node->type.tv = tarval_unreachable;
1237 } else if (selector->type.tv == tarval_bottom) {
1238 node->type.tv = tarval_reachable;
1240 assert(selector->type.tv == tarval_top);
1241 node->type.tv = tarval_unreachable;
1246 if (selector->type.tv == tarval_bottom) {
1247 node->type.tv = tarval_reachable;
1248 } else if (selector->type.tv == tarval_top) {
1249 node->type.tv = tarval_unreachable;
1251 long value = get_tarval_long(selector->type.tv);
1252 if (pnc == get_Cond_defaultProj(cond)) {
1253 /* default switch, have to check ALL other cases */
1256 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1257 ir_node *succ = get_irn_out(cond, i);
1261 if (value == get_Proj_proj(succ)) {
1262 /* we found a match, will NOT take the default case */
1263 node->type.tv = tarval_unreachable;
1267 /* all cases checked, no match, will take default case */
1268 node->type.tv = tarval_reachable;
1271 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1275 } /* compute_Proj_Cond */
1278 * (Re-)compute the type for a Proj-Nodes.
1280 * @param node the node
1282 static void compute_Proj(node_t *node) {
1283 ir_node *proj = node->node;
1284 ir_mode *mode = get_irn_mode(proj);
1285 node_t *block = get_irn_node(get_nodes_block(skip_Proj(proj)));
1286 ir_node *pred = get_Proj_pred(proj);
1288 if (block->type.tv == tarval_unreachable) {
1289 /* a Proj node in an unreachable block computes Top
1290 except if it's the initial_exec node. */
1291 if (get_Proj_proj(proj) != pn_Start_X_initial_exec ||
1293 node->type.tv = get_top_value(mode);
1298 if (mode == mode_M) {
1299 /* mode M is always bottom */
1300 node->type.tv = tarval_bottom;
1303 if (mode != mode_X) {
1305 compute_Proj_Cmp(node, pred);
1307 default_compute(node);
1310 /* handle mode_X nodes */
1312 switch (get_irn_opcode(pred)) {
1314 /* the Proj_X from the Start is always reachable */
1315 node->type.tv = tarval_reachable;
1318 compute_Proj_Cond(node, pred);
1321 default_compute(node);
1323 } /* compute_Proj */
1326 * (Re-)compute the type for a given node.
1328 * @param node the node
1330 static void compute(node_t *node) {
1331 compute_func func = (compute_func)node->node->op->ops.generic;
1338 * Propagate constant evaluation.
1340 * @param env the environment
1342 static void propagate(environment_t *env) {
1345 lattice_elem_t old_type;
1350 while (env->cprop != NULL) {
1351 /* remove the first partition X from cprop */
1354 env->cprop = X->cprop_next;
1358 while (! list_empty(&X->cprop)) {
1359 /* remove the first Node x from X.cprop */
1360 x = list_entry(X->cprop.next, node_t, cprop_list);
1361 list_del(&x->cprop_list);
1364 /* compute a new type for x */
1366 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1368 if (x->type.tv != old_type.tv) {
1369 verify_type(old_type, x->type);
1370 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1372 if (x->on_fallen == 0) {
1373 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
1374 not already on the list. */
1380 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1381 ir_node *succ = get_irn_out(x->node, i);
1382 node_t *y = get_irn_node(succ);
1384 /* Add y to y.partition.cprop. */
1385 add_node_to_cprop(y, env);
1390 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1391 DB((dbg, LEVEL_2, "Splitting part%d by fallen\n", X->nr));
1392 Y = split(X, fallen, env);
1396 /* remove the nodes from the fallen list */
1397 for (x = fallen; x != NULL; x = x->next)
1406 * Get the leader for a given node from its congruence class.
1408 * @param irn the node
1410 static ir_node *get_leader(node_t *node) {
1411 partition_t *part = node->part;
1413 if (part->n_nodes > 1) {
1414 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1416 return get_first_node(part)->node;
1422 * Post-Walker, apply the analysis results;
1424 static void apply_result(ir_node *irn, void *ctx) {
1425 node_t *node = get_irn_node(irn);
1428 if (is_Block(irn)) {
1429 if (irn == get_irg_end_block(current_ir_graph)) {
1430 /* the EndBlock is always reachable even if the analysis
1431 finds out the opposite :-) */
1435 if (node->type.tv == tarval_unreachable) {
1436 /* mark dead blocks */
1437 set_Block_dead(irn);
1439 } else if (is_End(irn)) {
1440 /* do not touch the End node */
1442 node_t *block = get_irn_node(get_nodes_block(irn));
1444 if (block->type.tv == tarval_unreachable) {
1445 if (! is_Bad(irn)) {
1446 ir_node *bad = get_irg_bad(current_ir_graph);
1448 /* here, bad might already have a node, but this can be safely ignored
1449 as long as bad has at least ONE valid node */
1450 set_irn_node(bad, node);
1452 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1456 else if (get_irn_mode(irn) == mode_X) {
1457 if (node->type.tv == tarval_unreachable) {
1458 ir_node *bad = get_irg_bad(current_ir_graph);
1460 /* see comment above */
1461 set_irn_node(bad, node);
1463 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1466 else if (is_Proj(irn)) {
1468 ir_node *cond = get_Proj_pred(irn);
1470 if (is_Cond(cond)) {
1471 node_t *sel = get_irn_node(get_Cond_selector(cond));
1473 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1474 /* Cond selector is a constant, make a Jmp */
1475 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1476 set_irn_node(jmp, node);
1478 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1484 /* normal data node */
1485 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1486 tarval *tv = node->type.tv;
1488 if (! is_Const(irn)) {
1489 /* can be replaced by a constant */
1490 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1491 set_irn_node(c, node);
1493 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1496 } else if (is_entity(node->type.sym.entity_p)) {
1497 if (! is_SymConst(irn)) {
1498 /* can be replaced by a Symconst */
1499 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1500 set_irn_node(symc, node);
1503 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1504 exchange(irn, symc);
1507 ir_node *leader = get_leader(node);
1509 if (leader != irn) {
1510 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
1511 exchange(irn, leader);
1516 } /* static void apply_result(ir_node *irn, void *ctx) {
1519 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1522 * sets the generic functions to compute.
1524 static void set_compute_functions(void) {
1527 /* set the default compute function */
1528 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1529 ir_op *op = get_irp_opcode(i);
1530 op->ops.generic = (op_func)default_compute;
1533 /* set specific functions */
1542 } /* set_compute_functions */
1544 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1545 ir_node *irn = local != NULL ? local : n;
1546 node_t *node = get_irn_node(irn);
1548 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1552 void combo(ir_graph *irg) {
1556 ir_graph *rem = current_ir_graph;
1558 current_ir_graph = irg;
1560 /* register a debug mask */
1561 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1562 firm_dbg_set_mask(dbg, SET_LEVEL_3);
1564 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1566 obstack_init(&env.obst);
1567 env.worklist = NULL;
1571 #ifdef DEBUG_libfirm
1572 env.dbg_list = NULL;
1574 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1575 env.type2id_map = pmap_create();
1576 env.end_idx = get_opt_global_cse() ? 0 : -1;
1577 env.lambda_input = 0;
1579 assure_irg_outs(irg);
1581 /* we have our own value_of function */
1582 set_value_of_func(get_node_tarval);
1584 set_compute_functions();
1585 DEBUG_ONLY(part_nr = 0);
1587 /* create the initial partition and place it on the work list */
1588 env.initial = new_partition(&env);
1589 add_to_worklist(env.initial, &env);
1590 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
1592 /* Place the START Node's partition on cprop.
1593 Place the START Node on its local worklist. */
1594 initial_X = get_irg_initial_exec(irg);
1595 start = get_irn_node(initial_X);
1596 add_node_to_cprop(start, &env);
1600 if (env.worklist != NULL)
1602 } while (env.cprop != NULL || env.worklist != NULL);
1604 dump_all_partitions(&env);
1606 set_dump_node_vcgattr_hook(dump_partition_hook);
1607 dump_ir_block_graph(irg, "-partition");
1608 set_dump_node_vcgattr_hook(NULL);
1611 /* apply the result */
1612 irg_walk_graph(irg, NULL, apply_result, &env);
1614 pmap_destroy(env.type2id_map);
1615 del_set(env.opcode2id_map);
1616 obstack_free(&env.obst, NULL);
1618 /* restore value_of() default behavior */
1619 set_value_of_func(NULL);
1620 current_ir_graph = rem;