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"
58 typedef struct node_t node_t;
59 typedef struct partition_t partition_t;
60 typedef struct opcode_key_t opcode_key_t;
61 typedef struct listmap_entry_t listmap_entry_t;
63 /** The type of the compute function. */
64 typedef void (*compute_func)(node_t *node);
70 ir_opcode code; /**< The Firm opcode. */
71 ir_mode *mode; /**< The mode of all nodes in the partition. */
73 long proj; /**< For Proj nodes, its proj number */
74 ir_entity *ent; /**< For Sel Nodes, its entity */
79 * An entry in the list_map.
81 struct listmap_entry_t {
82 void *id; /**< The id. */
83 node_t *list; /**< The associated list for this id. */
84 listmap_entry_t *next; /**< Link to the next entry in the map. */
87 /** We must map id's to lists. */
88 typedef struct listmap_t {
89 set *map; /**< Map id's to listmap_entry_t's */
90 listmap_entry_t *values; /**< List of all values in the map. */
94 * A lattice element. Because we handle constants and symbolic constants different, we
95 * have to use this union.
106 ir_node *node; /**< The IR-node itself. */
107 list_head node_list; /**< Double-linked list of entries. */
108 list_head cprop_list; /**< Double-linked partition.cprop list. */
109 partition_t *part; /**< points to the partition this node belongs to */
110 node_t *next; /**< Next node on local list (partition.touched, fallen). */
111 lattice_elem_t type; /**< The associated lattice element "type". */
112 int max_user_input; /**< Maximum input number of Def-Use edges. */
113 int next_edge; /**< Index of the next Def-Use edge to use. */
114 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
115 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
116 unsigned on_fallen:1; /**< Set, if this node is on the fallen list. */
120 * A partition containing congruent nodes.
123 list_head entries; /**< The head of partition node list. */
124 list_head cprop; /**< The head of partition.cprop list. */
125 partition_t *wl_next; /**< Next entry in the work list if any. */
126 partition_t *touched_next; /**< Points to the next partition in the touched set. */
127 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
128 node_t *touched; /**< The partition.touched set of this partition. */
129 unsigned n_nodes; /**< Number of entries in this partition. */
130 unsigned n_touched; /**< Number of entries in the partition.touched. */
131 int max_arity; /**< Maximum arity of all entries. */
132 int max_user_inputs; /**< Maximum number of user inputs of all entries. */
133 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
134 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
135 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
137 partition_t *dbg_next; /**< Link all partitions for debugging */
138 unsigned nr; /**< A unique number for (what-)mapping, >0. */
142 typedef struct environment_t {
143 struct obstack obst; /**< obstack to allocate data structures. */
144 partition_t *worklist; /**< The work list. */
145 partition_t *cprop; /**< The constant propagation list. */
146 partition_t *touched; /**< the touched set. */
147 partition_t *initial; /**< The initial partition. */
149 partition_t *dbg_list; /**< List of all partitions. */
151 set *opcode2id_map; /**< The opcodeMode->id map. */
152 pmap *type2id_map; /**< The type->id map. */
153 int end_idx; /**< -1 for local and 0 for global congruences. */
154 int lambda_input; /**< Captured argument for lambda_partition(). */
157 /** Type of the what function. */
158 typedef void *(*what_func)(const node_t *node, environment_t *env);
160 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
161 #define set_irn_node(irn, node) set_irn_link(irn, node)
163 /** The debug module handle. */
164 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
166 /** Next partition number. */
167 DEBUG_ONLY(static unsigned part_nr = 0);
170 static INLINE lattice_elem_t get_partition_type(const partition_t *X);
173 * Dump partition to output.
175 static void dump_partition(const char *msg, const partition_t *part) {
178 lattice_elem_t type = get_partition_type(part);
180 DB((dbg, LEVEL_2, "%s part%u (%u, %+F) {\n ", msg, part->nr, part->n_nodes, type));
181 list_for_each_entry(node_t, node, &part->entries, node_list) {
182 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
185 DB((dbg, LEVEL_2, "\n}\n"));
189 * Dump all partitions.
191 static void dump_all_partitions(const environment_t *env) {
192 const partition_t *P;
194 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
195 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
196 dump_partition("", P);
200 #define dump_partition(msg, part)
201 #define dump_all_partitions(env)
205 * Compare two pointer values of a listmap.
207 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
208 const listmap_entry_t *e1 = elt;
209 const listmap_entry_t *e2 = key;
212 return e1->id != e2->id;
213 } /* listmap_cmp_ptr */
216 * Initializes a listmap.
218 * @param map the listmap
220 static void listmap_init(listmap_t *map) {
221 map->map = new_set(listmap_cmp_ptr, 16);
226 * Terminates a listmap.
228 * @param map the listmap
230 static void listmap_term(listmap_t *map) {
235 * Return the associated listmap entry for a given id.
237 * @param map the listmap
238 * @param id the id to search for
240 * @return the asociated listmap entry for the given id
242 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
243 listmap_entry_t key, *entry;
248 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
250 if (entry->list == NULL) {
251 /* a new entry, put into the list */
252 entry->next = map->values;
259 * Calculate the hash value for an opcode map entry.
261 * @param entry an opcode map entry
263 * @return a hash value for the given opcode map entry
265 static unsigned opcode_hash(const opcode_key_t *entry) {
266 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent);
270 * Compare two entries in the opcode map.
272 static int cmp_opcode(const void *elt, const void *key, size_t size) {
273 const opcode_key_t *o1 = elt;
274 const opcode_key_t *o2 = key;
277 return o1->code != o2->code || o1->mode != o2->mode ||
278 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
282 * Compare two Def-Use edges for input position.
284 static int cmp_def_use_edge(const void *a, const void *b) {
285 const ir_def_use_edge *ea = a;
286 const ir_def_use_edge *eb = b;
288 /* no overrun, because range is [-1, MAXINT] */
289 return ea->pos - eb->pos;
290 } /* cmp_def_use_edge */
293 * We need the Def-Use edges sorted.
295 static void sort_irn_outs(node_t *node) {
296 ir_node *irn = node->node;
297 int n_outs = get_irn_n_outs(irn);
300 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
302 node->max_user_input = irn->out[n_outs + 1].pos;
303 } /* sort_irn_outs */
306 * Return the type of a node.
308 * @param irn an IR-node
310 * @return the associated type of this node
312 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
313 return get_irn_node(irn)->type;
314 } /* get_node_type */
317 * Return the tarval of a node.
319 * @param irn an IR-node
321 * @return the associated type of this node
323 static INLINE tarval *get_node_tarval(const ir_node *irn) {
324 lattice_elem_t type = get_node_type(irn);
326 if (is_tarval(type.tv))
328 return tarval_bottom;
329 } /* get_node_type */
332 * Add a partition to the worklist.
334 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
335 assert(X->on_worklist == 0);
336 X->wl_next = env->worklist;
342 * Create a new empty partition.
344 * @param env the environment
346 * @return a newly allocated partition
348 static INLINE partition_t *new_partition(environment_t *env) {
349 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
351 INIT_LIST_HEAD(&part->entries);
352 INIT_LIST_HEAD(&part->cprop);
353 part->wl_next = NULL;
354 part->touched_next = NULL;
355 part->cprop_next = NULL;
356 part->touched = NULL;
360 part->max_user_inputs = 0;
361 part->on_worklist = 0;
362 part->on_touched = 0;
365 part->dbg_next = env->dbg_list;
366 env->dbg_list = part;
367 part->nr = part_nr++;
371 } /* new_partition */
374 * Get the first node from a partition.
376 static INLINE node_t *get_first_node(const partition_t *X) {
377 return list_entry(X->entries.next, node_t, node_list);
381 * Return the type of a partition (assuming partition is non-empty and
382 * all elements have the same type).
384 * @param X a partition
386 * @return the type of the first element of the partition
388 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
389 const node_t *first = get_first_node(X);
391 } /* get_partition_type */
394 * Creates a partition node for the given IR-node and place it
395 * into the given partition.
397 * @param irn an IR-node
398 * @param part a partition to place the node in
399 * @param env the environment
401 * @return the created node
403 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
404 /* create a partition node and place it in the partition */
405 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
406 ir_mode *mode = get_irn_mode(irn);
408 INIT_LIST_HEAD(&node->node_list);
409 INIT_LIST_HEAD(&node->cprop_list);
413 node->type.tv = (mode == mode_X || mode == mode_BB) ? tarval_unreachable : tarval_top;
414 node->max_user_input = 0;
416 node->on_touched = 0;
419 set_irn_node(irn, node);
421 list_add_tail(&node->node_list, &part->entries);
425 } /* create_partition_node */
428 * Pre-Walker, init all Block-Phi lists.
430 static void init_block_phis(ir_node *irn, void *env) {
434 set_Block_phis(irn, NULL);
439 * Post-Walker, initialize all Nodes' type to U or top and place
440 * all nodes into the TOP partition.
442 static void create_initial_partitions(ir_node *irn, void *ctx) {
443 environment_t *env = ctx;
444 partition_t *part = env->initial;
448 node = create_partition_node(irn, part, env);
450 arity = get_irn_arity(irn);
451 if (arity > part->max_arity)
452 part->max_arity = arity;
453 if (node->max_user_input > part->max_user_inputs)
454 part->max_user_inputs = node->max_user_input;
457 add_Block_phi(get_nodes_block(irn), irn);
459 } /* create_initial_partitions */
462 * Add a partition to the touched set if not already there.
464 * @param part the partition
465 * @param env the environment
467 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
468 if (part->on_touched == 0) {
469 part->touched_next = env->touched;
471 part->on_touched = 1;
473 } /* add_to_touched */
476 * Add a node to the entry.partition.touched set if not already there.
480 static INLINE void add_to_partition_touched(node_t *y) {
481 if (y->on_touched == 0) {
482 partition_t *part = y->part;
484 y->next = part->touched;
489 } /* add_to_partition_touched */
492 * Update the worklist: If Z is on worklist then add Z' to worklist.
493 * Else add the smaller of Z and Z' to worklist.
495 * @param Z the Z partition
496 * @param Z_prime the Z' partition, a previous part of Z
497 * @param env the environment
499 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
500 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
501 add_to_worklist(Z_prime, env);
503 add_to_worklist(Z, env);
505 } /* update_worklist */
508 * Split a partition by a local list.
510 * @param Z the Z partition to split
511 * @param g a (non-empty) node list
512 * @param env the environment
514 * @return a new partition containing the nodes of g
516 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
517 partition_t *Z_prime;
520 int max_input, max_arity, arity;
522 dump_partition("Splitting ", Z);
526 /* Remove g from Z. */
527 for (node = g; node != NULL; node = node->next) {
528 list_del(&node->node_list);
531 assert(n < Z->n_nodes);
534 /* Move g to a new partition, Z
\92. */
535 Z_prime = new_partition(env);
536 max_arity = max_input = 0;
537 for (node = g; node != NULL; node = node->next) {
538 list_add(&node->node_list, &Z_prime->entries);
539 node->part = Z_prime;
540 arity = get_irn_arity(node->node);
541 if (arity > max_arity)
543 if (node->max_user_input > max_input)
544 max_input = node->max_user_input;
546 Z_prime->max_arity = max_arity;
547 Z_prime->max_user_inputs = max_input;
548 Z_prime->n_nodes = n;
550 update_worklist(Z, Z_prime, env);
552 dump_partition("Now ", Z);
553 dump_partition("Created new ", Z_prime);
558 * Returns non-zero if the i'th input of a Phi node is live.
560 * @param phi a Phi-node
561 * @param i an input number
563 * @return non-zero if the i'th input of the given Phi node is live
565 static int is_live_input(ir_node *phi, int i) {
567 ir_node *block = get_nodes_block(phi);
568 ir_node *pred = get_Block_cfgpred(block, i);
569 lattice_elem_t type = get_node_type(pred);
571 return type.tv != tarval_unreachable;
573 /* else it's the control input, always live */
575 } /* is_live_input */
578 * Return non-zero if a type is a constant.
580 static int is_constant_type(lattice_elem_t type) {
581 if (type.tv != tarval_bottom && type.tv != tarval_top)
584 } /* is_constant_type */
587 * Place a node on the cprop list.
590 * @param env the environment
592 static void add_node_to_cprop(node_t *y, environment_t *env) {
593 /* Add y to y.partition.cprop. */
594 if (y->on_cprop == 0) {
595 partition_t *Y = y->part;
597 list_add_tail(&y->cprop_list, &Y->cprop);
600 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
602 /* place its partition on the cprop list */
603 if (Y->on_cprop == 0) {
604 Y->cprop_next = env->cprop;
609 if (get_irn_mode(y->node) == mode_T) {
610 /* mode_T nodes always produce tarval_bottom, so we must explicitly
611 add it's Proj's to get constant evaluation to work */
614 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
615 node_t *proj = get_irn_node(get_irn_out(y->node, i));
617 add_node_to_cprop(proj, env);
620 if (is_Block(y->node)) {
621 /* Due to the way we handle Phi's, we must place all Phis of a block on the list
622 * if someone placeis the block. The Block is only placed if the reachability
623 * changes, and this must be re-evaluated in compute_Phi(). */
625 for (phi = get_Block_phis(y->node); phi != NULL; phi = get_Phi_next(phi)) {
626 node_t *p = get_irn_node(phi);
627 add_node_to_cprop(p, env);
630 } /* add_node_to_cprop */
633 * Split the partitions if caused by the first entry on the worklist.
635 * @param env the environment
637 static void cause_splits(environment_t *env) {
638 partition_t *X, *Y, *Z;
644 /* remove the first partition from the worklist */
646 env->worklist = X->wl_next;
649 dump_partition("Cause_split: ", X);
650 end_idx = env->end_idx;
651 for (i = -1; i <= X->max_user_inputs; ++i) {
652 /* empty the touched set: already done, just clear the list */
655 list_for_each_entry(node_t, x, &X->entries, node_list) {
661 num_edges = get_irn_n_outs(x->node);
663 while (x->next_edge <= num_edges) {
664 ir_def_use_edge *edge = &x->node->out[x->next_edge];
666 /* check if we have necessary edges */
674 /* ignore the "control input" for non-pinned nodes
675 if we are running in GCSE mode */
676 if (i < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
679 y = get_irn_node(succ);
680 if (is_constant_type(y->type)) {
681 code = get_irn_opcode(succ);
682 if (code == iro_Sub || (code == iro_Proj && is_Cmp(get_Proj_pred(succ))))
683 add_node_to_cprop(y, env);
686 /* Partitions of constants should not be split simply because their Nodes have unequal
687 functions or incongruent inputs. */
688 if (y->type.tv == tarval_bottom &&
689 (! is_Phi(y->node) || is_live_input(y->node, i))) {
691 add_to_touched(Y, env);
692 add_to_partition_touched(y);
697 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
698 /* remove it from the touched set */
701 if (Z->n_nodes != Z->n_touched) {
702 split(Z, Z->touched, env);
704 /* Empty local Z.touched. */
705 for (e = Z->touched; e != NULL; e = e->next) {
715 * Implements split_by_what(): Split a partition by characteristics given
716 * by the what function.
718 * @param X the partition to split
719 * @param What a function returning an Id for every node of the partition X
720 * @param P an flexible array to store the result partitions or NULL
721 * @param env the environment
723 * @return if P != NULL P will be filled with the resulting partitions and returned
725 static partition_t **split_by_what(partition_t *X, what_func What,
726 partition_t **P, environment_t *env) {
729 listmap_entry_t *iter;
732 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
734 list_for_each_entry(node_t, x, &X->entries, node_list) {
735 void *id = What(x, env);
736 listmap_entry_t *entry;
739 /* input not allowed, ignore */
742 /* Add x to map[What(x)]. */
743 entry = listmap_find(&map, id);
744 x->next = entry->list;
747 /* Let P be a set of Partitions. */
749 /* for all sets S except one in the range of map do */
750 for (iter = map.values; iter != NULL; iter = iter->next) {
751 if (iter->next == NULL) {
752 /* this is the last entry, ignore */
757 /* Add SPLIT( X, S ) to P. */
758 R = split(X, S, env);
760 ARR_APP1(partition_t *, P, R);
765 ARR_APP1(partition_t *, P, X);
770 } /* split_by_what */
772 /** lambda n.(n.type) */
773 static void *lambda_type(const node_t *node, environment_t *env) {
775 return node->type.tv;
778 /** lambda n.(n.opcode) */
779 static void *lambda_opcode(const node_t *node, environment_t *env) {
780 opcode_key_t key, *entry;
781 ir_node *irn = node->node;
783 key.code = get_irn_opcode(irn);
784 key.mode = get_irn_mode(irn);
788 switch (get_irn_opcode(irn)) {
790 key.u.proj = get_Proj_proj(irn);
793 key.u.ent = get_Sel_entity(irn);
799 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
801 } /* lambda_opcode */
803 /** lambda n.(n[i].partition) */
804 static void *lambda_partition(const node_t *node, environment_t *env) {
807 int i = env->lambda_input;
809 if (i >= get_irn_arity(node->node)) {
810 /* we are outside the allowed range */
814 /* ignore the "control input" for non-pinned nodes
815 if we are running in GCSE mode */
816 if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
819 pred = get_irn_n(node->node, i);
820 p = get_irn_node(pred);
823 } /* lambda_partition */
826 * Checks whether a type is a constant.
828 static int is_type_constant(lattice_elem_t type) {
829 if (is_tarval(type.tv))
830 return tarval_is_constant(type.tv);
831 /* else it is a symconst */
836 * Implements split_by().
838 * @param X the partition to split
839 * @param env the environment
841 static void split_by(partition_t *X, environment_t *env) {
842 partition_t **P = NEW_ARR_F(partition_t *, 0);
845 P = split_by_what(X, lambda_type, P, env);
846 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
847 partition_t *Y = P[i];
849 if (Y->n_nodes > 1) {
850 lattice_elem_t type = get_partition_type(Y);
852 /* we do not want split the TOP, unreachable or constant partitions */
853 if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
854 partition_t **Q = NEW_ARR_F(partition_t *, 0);
856 Q = split_by_what(Y, lambda_opcode, Q, env);
858 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
859 partition_t *Z = Q[j];
861 for (k = Z->max_arity - 1; k >= -1; --k) {
862 if (Z->n_nodes > 1) {
863 env->lambda_input = k;
864 split_by_what(Z, lambda_partition, NULL, env);
876 * (Re-)compute the type for a given node.
878 * @param node the node
880 static void default_compute(node_t *node) {
882 ir_node *irn = node->node;
883 tarval *top = tarval_top;
885 if (get_irn_mode(node->node) == mode_X)
886 top = tarval_unreachable;
888 if (get_irn_pinned(irn) == op_pin_state_pinned) {
889 node_t *block = get_irn_node(get_nodes_block(irn));
891 if (block->type.tv == tarval_unreachable) {
897 /* if any of the data inputs have type top, the result is type top */
898 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
899 ir_node *pred = get_irn_n(irn, i);
900 node_t *p = get_irn_node(pred);
902 if (p->type.tv == tarval_top) {
908 if (get_irn_mode(node->node) == mode_X)
909 node->type.tv = tarval_reachable;
911 node->type.tv = computed_value(irn);
912 } /* default_compute */
915 * (Re-)compute the type for a Block node.
917 * @param node the node
919 static void compute_Block(node_t *node) {
921 ir_node *block = node->node;
923 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
924 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
926 if (pred->type.tv == tarval_reachable) {
927 /* A block is reachable, if at least of predecessor is reachable. */
928 node->type.tv = tarval_reachable;
932 node->type.tv = tarval_unreachable;
933 } /* compute_Block */
936 * (Re-)compute the type for a Jmp node.
938 * @param node the node
940 static void compute_Jmp(node_t *node) {
941 node_t *block = get_irn_node(get_nodes_block(node->node));
943 node->type = block->type;
947 * (Re-)compute the type for the End node.
949 * @param node the node
951 static void compute_End(node_t *node) {
952 /* the End node is NOT dead of course */
953 node->type.tv = tarval_reachable;
957 * (Re-)compute the type for a SymConst node.
959 * @param node the node
961 static void compute_SymConst(node_t *node) {
962 ir_node *irn = node->node;
963 node_t *block = get_irn_node(get_nodes_block(irn));
965 if (block->type.tv == tarval_unreachable) {
966 node->type.tv = tarval_top;
969 switch (get_SymConst_kind(irn)) {
970 case symconst_addr_ent:
971 /* case symconst_addr_name: cannot handle this yet */
972 node->type.sym = get_SymConst_symbol(irn);
975 node->type.tv = computed_value(irn);
977 } /* compute_SymConst */
980 * (Re-)compute the type for a Phi node.
982 * @param node the node
984 static void compute_Phi(node_t *node) {
986 ir_node *phi = node->node;
989 /* if a Phi is in a unreachable block, its type is TOP */
990 node_t *block = get_irn_node(get_nodes_block(phi));
992 if (block->type.tv == tarval_unreachable) {
993 node->type.tv = tarval_top;
997 /* Phi implements the Meet operation */
998 type.tv = tarval_top;
999 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
1000 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
1001 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
1003 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
1004 /* ignore TOP inputs: We must check here for unreachable blocks,
1005 because Firm constants live in the Start Block are NEVER Top.
1006 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
1007 comes from a unreachable input. */
1010 if (pred->type.tv == tarval_bottom) {
1011 node->type.tv = tarval_bottom;
1013 } else if (type.tv == tarval_top) {
1014 /* first constant found */
1016 } else if (type.tv != pred->type.tv) {
1017 /* different constants or tarval_bottom */
1018 node->type.tv = tarval_bottom;
1021 /* else nothing, constants are the same */
1027 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
1029 * @param node the node
1031 static void compute_Add(node_t *node) {
1032 ir_node *sub = node->node;
1033 node_t *l = get_irn_node(get_Add_left(sub));
1034 node_t *r = get_irn_node(get_Add_right(sub));
1035 lattice_elem_t a = l->type;
1036 lattice_elem_t b = r->type;
1037 node_t *block = get_irn_node(get_nodes_block(sub));
1040 if (block->type.tv == tarval_unreachable) {
1041 node->type.tv = tarval_top;
1045 if (a.tv == tarval_top || b.tv == tarval_top) {
1046 node->type.tv = tarval_top;
1047 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1048 node->type.tv = tarval_bottom;
1050 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1051 must call tarval_add() first to handle this case! */
1052 if (is_tarval(a.tv)) {
1053 if (is_tarval(b.tv)) {
1054 node->type.tv = tarval_add(a.tv, b.tv);
1057 mode = get_tarval_mode(a.tv);
1058 if (a.tv == get_mode_null(mode)) {
1062 } else if (is_tarval(b.tv)) {
1063 mode = get_tarval_mode(b.tv);
1064 if (b.tv == get_mode_null(mode)) {
1069 node->type.tv = tarval_bottom;
1074 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1076 * @param node the node
1078 static void compute_Sub(node_t *node) {
1079 ir_node *sub = node->node;
1080 node_t *l = get_irn_node(get_Sub_left(sub));
1081 node_t *r = get_irn_node(get_Sub_right(sub));
1082 lattice_elem_t a = l->type;
1083 lattice_elem_t b = r->type;
1084 node_t *block = get_irn_node(get_nodes_block(sub));
1086 if (block->type.tv == tarval_unreachable) {
1087 node->type.tv = tarval_top;
1091 if (a.tv == tarval_top || b.tv == tarval_top) {
1092 node->type.tv = tarval_top;
1093 } else if (r->part == l->part) {
1094 ir_mode *mode = get_irn_mode(sub);
1095 node->type.tv = get_mode_null(mode);
1096 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1097 node->type.tv = tarval_bottom;
1099 if (is_tarval(a.tv) && is_tarval(b.tv))
1100 node->type.tv = tarval_sub(a.tv, b.tv);
1102 node->type.tv = tarval_bottom;
1107 * (Re-)compute the type for a Proj(Cmp).
1109 * @param node the node
1110 * @param cond the predecessor Cmp node
1112 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1113 ir_node *proj = node->node;
1114 node_t *l = get_irn_node(get_Cmp_left(cmp));
1115 node_t *r = get_irn_node(get_Cmp_right(cmp));
1116 lattice_elem_t a = l->type;
1117 lattice_elem_t b = r->type;
1118 pn_Cmp pnc = get_Proj_proj(proj);
1121 * BEWARE: a == a is NOT always True for floating Point values, as
1122 * NaN != NaN is defined, so we must check this here.
1124 if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt) {
1125 if (a.tv == tarval_top || b.tv == tarval_top) {
1126 node->type.tv = tarval_top;
1127 } else if (r->part == l->part) {
1128 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1129 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1130 node->type.tv = tarval_bottom;
1132 default_compute(node);
1135 default_compute(node);
1137 } /* compute_Proj_Cmp */
1140 * (Re-)compute the type for a Proj(Cond).
1142 * @param node the node
1143 * @param cond the predecessor Cond node
1145 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1146 ir_node *proj = node->node;
1147 long pnc = get_Proj_proj(proj);
1148 ir_node *sel = get_Cond_selector(cond);
1149 node_t *selector = get_irn_node(sel);
1151 if (get_irn_mode(sel) == mode_b) {
1153 if (pnc == pn_Cond_true) {
1154 if (selector->type.tv == tarval_b_false) {
1155 node->type.tv = tarval_unreachable;
1156 } else if (selector->type.tv == tarval_b_true) {
1157 node->type.tv = tarval_reachable;
1158 } else if (selector->type.tv == tarval_bottom) {
1159 node->type.tv = tarval_reachable;
1161 assert(selector->type.tv == tarval_top);
1162 node->type.tv = tarval_unreachable;
1165 assert(pnc == pn_Cond_false);
1167 if (selector->type.tv == tarval_b_false) {
1168 node->type.tv = tarval_reachable;
1169 } else if (selector->type.tv == tarval_b_true) {
1170 node->type.tv = tarval_unreachable;
1171 } else if (selector->type.tv == tarval_bottom) {
1172 node->type.tv = tarval_reachable;
1174 assert(selector->type.tv == tarval_top);
1175 node->type.tv = tarval_unreachable;
1180 if (selector->type.tv == tarval_bottom) {
1181 node->type.tv = tarval_reachable;
1182 } else if (selector->type.tv == tarval_top) {
1183 node->type.tv = tarval_unreachable;
1185 long value = get_tarval_long(selector->type.tv);
1186 if (pnc == get_Cond_defaultProj(cond)) {
1187 /* default switch, have to check ALL other cases */
1190 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1191 ir_node *succ = get_irn_out(cond, i);
1195 if (value == get_Proj_proj(succ)) {
1196 /* we found a match, will NOT take the default case */
1197 node->type.tv = tarval_unreachable;
1201 /* all cases checked, no match, will take default case */
1202 node->type.tv = tarval_reachable;
1205 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1209 } /* compute_Proj_Cond */
1212 * (Re-)compute the type for a Proj-Nodes.
1214 * @param node the node
1216 static void compute_Proj(node_t *node) {
1217 ir_node *proj = node->node;
1218 ir_mode *mode = get_irn_mode(proj);
1221 if (mode == mode_M) {
1222 /* mode M is always bottom */
1223 node->type.tv = tarval_bottom;
1226 if (mode != mode_X) {
1227 ir_node *cmp = get_Proj_pred(proj);
1229 compute_Proj_Cmp(node, cmp);
1231 default_compute(node);
1234 /* handle mode_X nodes */
1235 pred = get_Proj_pred(proj);
1237 switch (get_irn_opcode(pred)) {
1239 /* the Proj_X from the Start is always reachable */
1240 node->type.tv = tarval_reachable;
1243 compute_Proj_Cond(node, pred);
1246 default_compute(node);
1248 } /* compute_Proj */
1251 * (Re-)compute the type for a given node.
1253 * @param node the node
1255 static void compute(node_t *node) {
1256 compute_func func = (compute_func)node->node->op->ops.generic;
1263 * Propagate constant evaluation.
1265 * @param env the environment
1267 static void propagate(environment_t *env) {
1270 lattice_elem_t old_type;
1275 while (env->cprop != NULL) {
1276 /* remove the first partition X from cprop */
1279 env->cprop = X->cprop_next;
1283 while (! list_empty(&X->cprop)) {
1284 /* remove the first Node x from X.cprop */
1285 x = list_entry(X->cprop.next, node_t, cprop_list);
1286 list_del(&x->cprop_list);
1289 /* compute a new type for x */
1291 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1293 if (x->type.tv != old_type.tv) {
1294 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1296 if (x->on_fallen == 0) {
1297 /* Add x to fallen. Nodes might fall from T -> const -> _|_, so check that they are
1298 not already on the list. */
1304 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1305 ir_node *succ = get_irn_out(x->node, i);
1306 node_t *y = get_irn_node(succ);
1308 /* Add y to y.partition.cprop. */
1309 add_node_to_cprop(y, env);
1314 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1315 Y = split(X, fallen, env);
1319 /* remove the nodes from the fallen list */
1320 for (x = fallen; x != NULL; x = x->next)
1329 * Get the leader for a given node from its congruence class.
1331 * @param irn the node
1333 static ir_node *get_leader(node_t *node) {
1334 partition_t *part = node->part;
1336 if (part->n_nodes > 1) {
1337 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1339 return get_first_node(part)->node;
1345 * Post-Walker, apply the analysis results;
1347 static void apply_result(ir_node *irn, void *ctx) {
1348 node_t *node = get_irn_node(irn);
1351 if (is_Block(irn)) {
1352 if (irn == get_irg_end_block(current_ir_graph)) {
1353 /* the EndBlock is always reachable even if the analysis
1354 finds out the opposite :-) */
1358 if (node->type.tv == tarval_unreachable) {
1359 /* mark dead blocks */
1360 set_Block_dead(irn);
1362 } else if (is_End(irn)) {
1363 /* do not touch the End node */
1365 node_t *block = get_irn_node(get_nodes_block(irn));
1367 if (block->type.tv == tarval_unreachable) {
1368 if (! is_Bad(irn)) {
1369 ir_node *bad = get_irg_bad(current_ir_graph);
1371 /* here, bad might already have a node, but this can be safely ignored
1372 as long as bad has at least ONE valid node */
1373 set_irn_node(bad, node);
1375 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1379 else if (get_irn_mode(irn) == mode_X) {
1380 if (node->type.tv == tarval_unreachable) {
1381 ir_node *bad = get_irg_bad(current_ir_graph);
1383 /* see comment above */
1384 set_irn_node(bad, node);
1386 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1389 else if (is_Proj(irn)) {
1391 ir_node *cond = get_Proj_pred(irn);
1393 if (is_Cond(cond)) {
1394 node_t *sel = get_irn_node(get_Cond_selector(cond));
1396 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1397 /* Cond selector is a constant, make a Jmp */
1398 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1399 set_irn_node(jmp, node);
1401 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1407 /* normal data node */
1408 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1409 tarval *tv = node->type.tv;
1411 if (! is_Const(irn)) {
1412 /* can be replaced by a constant */
1413 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1414 set_irn_node(c, node);
1416 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1419 } else if (is_entity(node->type.sym.entity_p)) {
1420 if (! is_SymConst(irn)) {
1421 /* can be replaced by a Symconst */
1422 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1423 set_irn_node(symc, node);
1426 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1427 exchange(irn, symc);
1430 ir_node *leader = get_leader(node);
1432 if (leader != irn) {
1433 DB((dbg, LEVEL_1, "%+F from part%d is replaced by %+F\n", irn, node->part->nr, leader));
1434 exchange(irn, leader);
1439 } /* static void apply_result(ir_node *irn, void *ctx) {
1442 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1445 * sets the generic functions to compute.
1447 static void set_compute_functions(void) {
1450 /* set the default compute function */
1451 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1452 ir_op *op = get_irp_opcode(i);
1453 op->ops.generic = (op_func)default_compute;
1456 /* set specific functions */
1465 } /* set_compute_functions */
1467 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1468 ir_node *irn = local != NULL ? local : n;
1469 node_t *node = get_irn_node(irn);
1471 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1475 void combo(ir_graph *irg) {
1479 ir_graph *rem = current_ir_graph;
1481 current_ir_graph = irg;
1483 /* register a debug mask */
1484 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1486 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1488 obstack_init(&env.obst);
1489 env.worklist = NULL;
1493 #ifdef DEBUG_libfirm
1494 env.dbg_list = NULL;
1496 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1497 env.type2id_map = pmap_create();
1498 env.end_idx = get_opt_global_cse() ? 0 : -1;
1499 env.lambda_input = 0;
1501 assure_irg_outs(irg);
1503 /* we have our own value_of function */
1504 set_value_of_func(get_node_tarval);
1506 set_compute_functions();
1507 DEBUG_ONLY(part_nr = 0);
1509 /* create the initial partition and place it on the work list */
1510 env.initial = new_partition(&env);
1511 add_to_worklist(env.initial, &env);
1512 irg_walk_graph(irg, init_block_phis, create_initial_partitions, &env);
1514 /* Place the START Node's partition on cprop.
1515 Place the START Node on its local worklist. */
1516 initial_X = get_irg_initial_exec(irg);
1517 start = get_irn_node(initial_X);
1518 add_node_to_cprop(start, &env);
1522 if (env.worklist != NULL)
1524 } while (env.cprop != NULL || env.worklist != NULL);
1526 dump_all_partitions(&env);
1528 set_dump_node_vcgattr_hook(dump_partition_hook);
1529 dump_ir_block_graph(irg, "-partition");
1530 set_dump_node_vcgattr_hook(NULL);
1533 /* apply the result */
1534 irg_walk_graph(irg, NULL, apply_result, &env);
1536 pmap_destroy(env.type2id_map);
1537 del_set(env.opcode2id_map);
1538 obstack_free(&env.obst, NULL);
1540 /* restore value_of() default behavior */
1541 set_value_of_func(NULL);
1542 current_ir_graph = rem;