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 opcode_entry_t opcode_entry_t;
62 typedef struct listmap_entry_t listmap_entry_t;
64 /** The type of the compute function. */
65 typedef void (*compute_func)(node_t *node);
71 ir_opcode code; /**< The Firm opcode. */
72 ir_mode *mode; /**< The mode of all nodes in the partition. */
76 * An entry in the opcode map.
78 struct opcode_entry_t {
79 opcode_key_t key; /**< The key. */
80 partition_t *part; /**< The associated partition. */
84 * An entry in the list_map.
86 struct listmap_entry_t {
87 void *id; /**< The id. */
88 node_t *list; /**< The associated list for this id. */
89 listmap_entry_t *next; /**< Link to the next entry in the map. */
92 /** We must map id's to lists. */
93 typedef struct listmap_t {
94 set *map; /**< Map id's to listmap_entry_t's */
95 listmap_entry_t *values; /**< List of all values in the map. */
99 * A lattice element. Because we handle constants and symbolic constants different, we
100 * have to use this union.
111 ir_node *node; /**< The IR-node itself. */
112 list_head node_list; /**< Double-linked list of entries. */
113 partition_t *part; /**< points to the partition this node belongs to */
114 node_t *cprop_next; /**< Next node on partition.cprop list. */
115 node_t *next; /**< Next node on local list (partition.touched, fallen). */
116 lattice_elem_t type; /**< The associated lattice element "type". */
117 int max_user_input; /**< Maximum input number of Def-Use edges. */
118 int next_edge; /**< Index of the next Def-Use edge to use. */
119 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
120 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
124 * A partition containing congruent nodes.
127 list_head entries; /**< The head of partition node list. */
128 node_t *cprop; /**< The 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. */
153 partition_t *dbg_list; /**< List of all partitions. */
155 set *opcode2id_map; /**< The opcodeMode->id map. */
156 pmap *type2id_map; /**< The type->id map. */
157 int end_idx; /**< -1 for local and 0 for global congruences. */
158 int lambda_input; /**< Captured argument for lambda_partition(). */
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 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)
209 * Compare two pointer values of a listmap.
211 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
212 const listmap_entry_t *e1 = elt;
213 const listmap_entry_t *e2 = key;
215 return e1->id != e2->id;
216 } /* listmap_cmp_ptr */
219 * Initializes a listmap.
221 * @param map the listmap
223 static void listmap_init(listmap_t *map) {
224 map->map = new_set(listmap_cmp_ptr, 16);
229 * Terminates a listmap.
231 * @param map the listmap
233 static void listmap_term(listmap_t *map) {
238 * Return the associated listmap entry for a given id.
240 * @param map the listmap
241 * @param id the id to search for
243 * @return the asociated listmap entry for the given id
245 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
246 listmap_entry_t key, *entry;
251 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
253 if (entry->list == NULL) {
254 /* a new entry, put into the list */
255 entry->next = map->values;
262 * Calculate the hash value for an opcode map entry.
264 * @param entry an opcode map entry
266 * @return a hash value for the given opcode map entry
268 static unsigned opcode_hash(const opcode_key_t *entry) {
269 return (entry->mode - (ir_mode *)0) * 9 + entry->code;
273 * Compare two entries in the opcode map.
275 static int cmp_opcode(const void *elt, const void *key, size_t size) {
276 const opcode_key_t *o1 = elt;
277 const opcode_key_t *o2 = key;
279 return o1->code != o2->code || o1->mode != o2->mode;
283 * Compare two Def-Use edges for input position.
285 static int cmp_def_use_edge(const void *a, const void *b) {
286 const ir_def_use_edge *ea = a;
287 const ir_def_use_edge *eb = b;
289 /* no overrun, because range is [-1, MAXINT] */
290 return ea->pos - eb->pos;
291 } /* cmp_def_use_edge */
294 * We need the Def-Use edges sorted.
296 static void sort_irn_outs(node_t *node) {
297 ir_node *irn = node->node;
298 int n_outs = get_irn_n_outs(irn);
301 qsort(&irn->out[1], n_outs, sizeof(irn->out[0]), cmp_def_use_edge);
303 node->max_user_input = irn->out[n_outs + 1].pos;
304 } /* sort_irn_outs */
307 * Return the type of a node.
309 * @param irn an IR-node
311 * @return the associated type of this node
313 static INLINE lattice_elem_t get_node_type(const ir_node *irn) {
314 return get_irn_node(irn)->type;
315 } /* get_node_type */
318 * Return the tarval of a node.
320 * @param irn an IR-node
322 * @return the associated type of this node
324 static INLINE tarval *get_node_tarval(const ir_node *irn) {
325 lattice_elem_t type = get_node_type(irn);
327 if (is_tarval(type.tv))
329 return tarval_bottom;
330 } /* get_node_type */
333 * Add a partition to the worklist.
335 static INLINE void add_to_worklist(partition_t *X, environment_t *env) {
336 assert(X->on_worklist == 0);
337 X->wl_next = env->worklist;
343 * Create a new empty partition.
345 * @param env the environment
347 * @return a newly allocated partition
349 static INLINE partition_t *new_partition(environment_t *env) {
350 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
352 INIT_LIST_HEAD(&part->entries);
354 part->wl_next = NULL;
355 part->touched_next = NULL;
356 part->cprop_next = NULL;
357 part->touched = NULL;
361 part->max_user_inputs = 0;
362 part->on_worklist = 0;
363 part->on_touched = 0;
366 part->dbg_next = env->dbg_list;
367 env->dbg_list = part;
368 part->nr = part_nr++;
372 } /* new_partition */
375 * Get the first node from a partition.
377 static INLINE node_t *get_first_node(const partition_t *X) {
378 return list_entry(X->entries.next, node_t, node_list);
382 * Return the type of a partition (assuming partition is non-empty and
383 * all elements have the same type).
385 * @param X a partition
387 * @return the type of the first element of the partition
389 static INLINE lattice_elem_t get_partition_type(const partition_t *X) {
390 const node_t *first = get_first_node(X);
392 } /* get_partition_type */
395 * Creates a partition node for the given IR-node and place it
396 * into the given partition.
398 * @param irn an IR-node
399 * @param part a partition to place the node in
400 * @param env the environment
402 * @return the created node
404 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
405 /* create a partition node and place it in the partition */
406 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
407 ir_mode *mode = get_irn_mode(irn);
409 INIT_LIST_HEAD(&node->node_list);
412 node->cprop_next = NULL;
414 node->type.tv = (mode == mode_X || mode == mode_BB) ? tarval_unreachable : tarval_top;
415 node->max_user_input = 0;
417 node->on_touched = 0;
419 set_irn_node(irn, node);
421 list_add_tail(&node->node_list, &part->entries);
425 } /* create_partition_node */
428 * Walker, initialize all Nodes' type to U or top and place
429 * all nodes into the TOP partition.
431 static void create_initial_partitions(ir_node *irn, void *ctx) {
432 environment_t *env = ctx;
433 partition_t *part = env->initial;
437 node = create_partition_node(irn, part, env);
439 arity = get_irn_arity(irn);
440 if (arity > part->max_arity)
441 part->max_arity = arity;
442 if (node->max_user_input > part->max_user_inputs)
443 part->max_user_inputs = node->max_user_input;
444 } /* create_initial_partitions */
447 * Add a partition to the touched set if not already there.
449 * @param part the partition
450 * @param env the environment
452 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
453 if (part->on_touched == 0) {
454 part->touched_next = env->touched;
456 part->on_touched = 1;
458 } /* add_to_touched */
461 * Add a node to the entry.partition.touched set if not already there.
465 static INLINE void add_to_partition_touched(node_t *y) {
466 if (y->on_touched == 0) {
467 partition_t *part = y->part;
469 y->next = part->touched;
474 } /* add_to_partition_touched */
477 * Update the worklist: If Z is on worklist then add Z' to worklist.
478 * Else add the smaller of Z and Z' to worklist.
480 * @param Z the Z partition
481 * @param Z_prime the Z' partition, a previous part of Z
482 * @param env the environment
484 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
485 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
486 add_to_worklist(Z_prime, env);
488 add_to_worklist(Z, env);
490 } /* update_worklist */
493 * Split a partition by a local list.
495 * @param Z the Z partition to split
496 * @param g a (non-empty) node list
497 * @param env the environment
499 * @return a new partition containing the nodes of g
501 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
502 partition_t *Z_prime;
505 int max_input, max_arity, arity;
507 dump_partition("Splitting ", Z);
511 /* Remove g from Z. */
512 for (node = g; node != NULL; node = node->next) {
513 list_del(&node->node_list);
516 assert(n < Z->n_nodes);
519 /* Move g to a new partition, Z
\92. */
520 Z_prime = new_partition(env);
521 max_arity = max_input = 0;
522 for (node = g; node != NULL; node = node->next) {
523 list_add(&node->node_list, &Z_prime->entries);
524 node->part = Z_prime;
525 arity = get_irn_arity(node->node);
526 if (arity > max_arity)
528 if (node->max_user_input > max_input)
529 max_input = node->max_user_input;
531 Z_prime->max_arity = max_arity;
532 Z_prime->max_user_inputs = max_input;
533 Z_prime->n_nodes = n;
535 update_worklist(Z, Z_prime, env);
537 dump_partition("Now ", Z);
538 dump_partition("Created new ", Z_prime);
543 * Returns non-zero if the i'th input of a Phi node is live.
545 * @param phi a Phi-node
546 * @param i an input number
548 * @return non-zero if the i'th input of the given Phi node is live
550 static int is_live_input(ir_node *phi, int i) {
552 ir_node *block = get_nodes_block(phi);
553 ir_node *pred = get_Block_cfgpred(block, i);
554 lattice_elem_t type = get_node_type(pred);
556 return type.tv != tarval_unreachable;
558 /* else it's the control input, always live */
560 } /* is_live_input */
563 * Return non-zero if a type is a constant.
565 static int is_constant_type(lattice_elem_t type) {
566 if (type.tv != tarval_bottom && type.tv != tarval_top)
569 } /* is_constant_type */
572 * Place a node on the cprop list.
575 * @param env the environment
577 static void add_node_to_cprop(node_t *y, environment_t *env) {
578 /* Add y to y.partition.cprop. */
579 if (y->on_cprop == 0) {
580 partition_t *Y = y->part;
582 y->cprop_next = Y->cprop;
586 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
588 /* place its partition on the cprop list */
589 if (Y->on_cprop == 0) {
590 Y->cprop_next = env->cprop;
595 if (get_irn_mode(y->node) == mode_T) {
596 /* mode_T nodes always produce tarval_bottom, so we must explicitly
597 add it's Proj's to get constant evaluation to work */
600 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
601 node_t *proj = get_irn_node(get_irn_out(y->node, i));
603 add_node_to_cprop(proj, env);
606 } /* add_node_to_cprop */
609 * Split the partitions if caused by the first entry on the worklist.
611 * @param env the environment
613 static void cause_splits(environment_t *env) {
614 partition_t *X, *Y, *Z;
620 /* remove the first partition from the worklist */
622 env->worklist = X->wl_next;
625 dump_partition("Cause_split: ", X);
626 end_idx = env->end_idx;
627 for (i = -1; i <= X->max_user_inputs; ++i) {
628 /* empty the touched set: already done, just clear the list */
631 list_for_each_entry(node_t, x, &X->entries, node_list) {
637 num_edges = get_irn_n_outs(x->node);
639 while (x->next_edge <= num_edges) {
640 ir_def_use_edge *edge = &x->node->out[x->next_edge];
642 /* check if we have necessary edges */
650 /* ignore the "control input" for non-pinned nodes
651 if we are running in GCSE mode */
652 if (i < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
655 y = get_irn_node(succ);
656 if (is_constant_type(y->type)) {
657 code = get_irn_opcode(succ);
658 if (code == iro_Sub || (code == iro_Proj && is_Cmp(get_Proj_pred(succ))))
659 add_node_to_cprop(y, env);
662 /* Partitions of constants should not be split simply because their Nodes have unequal
663 functions or incongruent inputs. */
664 if (y->type.tv == tarval_bottom &&
665 (! is_Phi(x->node) || is_live_input(x->node, i))) {
667 add_to_touched(Y, env);
668 add_to_partition_touched(y);
673 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
674 /* remove it from the touched set */
677 if (Z->n_nodes != Z->n_touched) {
678 split(Z, Z->touched, env);
680 /* Empty local Z.touched. */
681 for (e = Z->touched; e != NULL; e = e->next) {
691 * Implements split_by_what(): Split a partition by characteristics given
692 * by the what function.
694 * @param X the partition to split
695 * @param What a function returning an Id for every node of the partition X
696 * @param P an flexible array to store the result partitions or NULL
697 * @param env the environment
699 * @return if P != NULL P will be filled with the resulting partitions and returned
701 static partition_t **split_by_what(partition_t *X, what_func What,
702 partition_t **P, environment_t *env) {
705 listmap_entry_t *iter;
708 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
710 list_for_each_entry(node_t, x, &X->entries, node_list) {
711 void *id = What(x, env);
712 listmap_entry_t *entry;
715 /* input not allowed, ignore */
718 /* Add x to map[What(x)]. */
719 entry = listmap_find(&map, id);
720 x->next = entry->list;
723 /* Let P be a set of Partitions. */
725 /* for all sets S except one in the range of map do */
726 for (iter = map.values; iter != NULL; iter = iter->next) {
727 if (iter->next == NULL) {
728 /* this is the last entry, ignore */
733 /* Add SPLIT( X, S ) to P. */
734 R = split(X, S, env);
736 ARR_APP1(partition_t *, P, R);
741 ARR_APP1(partition_t *, P, X);
746 } /* split_by_what */
748 /** lambda n.(n.type) */
749 static void *lambda_type(const node_t *node, environment_t *env) {
751 return node->type.tv;
754 /** lambda n.(n.opcode) */
755 static void *lambda_opcode(const node_t *node, environment_t *env) {
756 opcode_key_t key, *entry;
758 key.code = get_irn_opcode(node->node);
759 key.mode = get_irn_mode(node->node);
760 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
762 } /* lambda_opcode */
764 /** lambda n.(n[i].partition) */
765 static void *lambda_partition(const node_t *node, environment_t *env) {
768 int i = env->lambda_input;
770 if (i >= get_irn_arity(node->node)) {
771 /* we are outside the allowed range */
775 /* ignore the "control input" for non-pinned nodes
776 if we are running in GCSE mode */
777 if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
780 pred = get_irn_n(node->node, i);
781 p = get_irn_node(pred);
784 } /* lambda_partition */
787 * Checks whether a type is a constant.
789 static int is_type_constant(lattice_elem_t type) {
790 if (is_tarval(type.tv))
791 return tarval_is_constant(type.tv);
792 /* else it is a symconst */
797 * Implements split_by().
799 * @param X the partition to split
800 * @param env the environment
802 static void split_by(partition_t *X, environment_t *env) {
803 partition_t **P = NEW_ARR_F(partition_t *, 0);
806 P = split_by_what(X, lambda_type, P, env);
807 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
808 partition_t *Y = P[i];
810 if (Y->n_nodes > 1) {
811 lattice_elem_t type = get_partition_type(Y);
813 /* we do not want split the TOP, unreachable or constant partitions */
814 if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
815 partition_t **Q = NEW_ARR_F(partition_t *, 0);
817 Q = split_by_what(Y, lambda_opcode, Q, env);
819 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
820 partition_t *Z = Q[j];
822 for (k = Z->max_arity - 1; k >= -1; --k) {
823 if (Z->n_nodes > 1) {
824 env->lambda_input = k;
825 split_by_what(Z, lambda_partition, NULL, env);
837 * (Re-)compute the type for a given node.
839 * @param node the node
841 static void default_compute(node_t *node) {
843 ir_node *irn = node->node;
844 tarval *top = tarval_top;
846 if (get_irn_mode(node->node) == mode_X)
847 top = tarval_unreachable;
849 if (get_irn_pinned(irn) == op_pin_state_pinned) {
850 node_t *block = get_irn_node(get_nodes_block(irn));
852 if (block->type.tv == tarval_unreachable) {
858 /* if any of the data inputs have type top, the result is type top */
859 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
860 ir_node *pred = get_irn_n(irn, i);
861 node_t *p = get_irn_node(pred);
863 if (p->type.tv == tarval_top) {
869 if (get_irn_mode(node->node) == mode_X)
870 node->type.tv = tarval_reachable;
872 node->type.tv = computed_value(irn);
873 } /* default_compute */
876 * (Re-)compute the type for a Block node.
878 * @param node the node
880 static void compute_Block(node_t *node) {
882 ir_node *block = node->node;
884 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
885 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
887 if (pred->type.tv == tarval_reachable) {
888 /* A block is reachable, if at least of predecessor is reachable. */
889 node->type.tv = tarval_reachable;
893 node->type.tv = tarval_unreachable;
894 } /* compute_Block */
897 * (Re-)compute the type for a Jmp node.
899 * @param node the node
901 static void compute_Jmp(node_t *node) {
902 node_t *block = get_irn_node(get_nodes_block(node->node));
904 node->type = block->type;
908 * (Re-)compute the type for the End node.
910 * @param node the node
912 static void compute_End(node_t *node) {
913 /* the End node is NOT dead of course */
914 node->type.tv = tarval_reachable;
918 * (Re-)compute the type for a SymConst node.
920 * @param node the node
922 static void compute_SymConst(node_t *node) {
923 ir_node *irn = node->node;
924 node_t *block = get_irn_node(get_nodes_block(irn));
926 if (block->type.tv == tarval_unreachable) {
927 node->type.tv = tarval_top;
930 switch (get_SymConst_kind(irn)) {
931 case symconst_addr_ent:
932 /* case symconst_addr_name: cannot handle this yet */
933 node->type.sym = get_SymConst_symbol(irn);
936 node->type.tv = computed_value(irn);
938 } /* compute_SymConst */
941 * (Re-)compute the type for a Phi node.
943 * @param node the node
945 static void compute_Phi(node_t *node) {
947 ir_node *phi = node->node;
950 /* if a Phi is in a unreachable block, its type is TOP */
951 node_t *block = get_irn_node(get_nodes_block(phi));
953 if (block->type.tv == tarval_unreachable) {
954 node->type.tv = tarval_top;
958 /* Phi implements the Meet operation */
959 type.tv = tarval_top;
960 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
961 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
962 node_t *pred_X = get_irn_node(get_Block_cfgpred(block->node, i));
964 if (pred_X->type.tv == tarval_unreachable || pred->type.tv == tarval_top) {
965 /* ignore TOP inputs: We must check here for unreachable blocks,
966 because Firm constants live in the Start Block are NEVER Top.
967 Else, a Phi (1,2) will produce Bottom, even if the 2 for instance
968 comes from a unreachable input. */
971 if (pred->type.tv == tarval_bottom) {
972 node->type.tv = tarval_bottom;
974 } else if (type.tv == tarval_top) {
975 /* first constant found */
977 } else if (type.tv != pred->type.tv) {
978 /* different constants or tarval_bottom */
979 node->type.tv = tarval_bottom;
982 /* else nothing, constants are the same */
988 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
990 * @param node the node
992 static void compute_Add(node_t *node) {
993 ir_node *sub = node->node;
994 node_t *l = get_irn_node(get_Add_left(sub));
995 node_t *r = get_irn_node(get_Add_right(sub));
996 lattice_elem_t a = l->type;
997 lattice_elem_t b = r->type;
998 node_t *block = get_irn_node(get_nodes_block(sub));
1001 if (block->type.tv == tarval_unreachable) {
1002 node->type.tv = tarval_top;
1006 if (a.tv == tarval_top || b.tv == tarval_top) {
1007 node->type.tv = tarval_top;
1008 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1009 node->type.tv = tarval_bottom;
1011 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1012 must call tarval_add() first to handle this case! */
1013 if (is_tarval(a.tv)) {
1014 if (is_tarval(b.tv)) {
1015 node->type.tv = tarval_add(a.tv, b.tv);
1018 mode = get_tarval_mode(a.tv);
1019 if (a.tv == get_mode_null(mode)) {
1023 } else if (is_tarval(b.tv)) {
1024 mode = get_tarval_mode(b.tv);
1025 if (b.tv == get_mode_null(mode)) {
1030 node->type.tv = tarval_bottom;
1035 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1037 * @param node the node
1039 static void compute_Sub(node_t *node) {
1040 ir_node *sub = node->node;
1041 node_t *l = get_irn_node(get_Sub_left(sub));
1042 node_t *r = get_irn_node(get_Sub_right(sub));
1043 lattice_elem_t a = l->type;
1044 lattice_elem_t b = r->type;
1045 node_t *block = get_irn_node(get_nodes_block(sub));
1047 if (block->type.tv == tarval_unreachable) {
1048 node->type.tv = tarval_top;
1052 if (a.tv == tarval_top || b.tv == tarval_top) {
1053 node->type.tv = tarval_top;
1054 } else if (r->part == l->part) {
1055 ir_mode *mode = get_irn_mode(sub);
1056 node->type.tv = get_mode_null(mode);
1057 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1058 node->type.tv = tarval_bottom;
1060 if (is_tarval(a.tv) && is_tarval(b.tv))
1061 node->type.tv = tarval_sub(a.tv, b.tv);
1063 node->type.tv = tarval_bottom;
1068 * (Re-)compute the type for a Proj(Cmp).
1070 * @param node the node
1071 * @param cond the predecessor Cmp node
1073 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1074 ir_node *proj = node->node;
1075 node_t *l = get_irn_node(get_Cmp_left(cmp));
1076 node_t *r = get_irn_node(get_Cmp_right(cmp));
1077 lattice_elem_t a = l->type;
1078 lattice_elem_t b = r->type;
1079 pn_Cmp pnc = get_Proj_proj(proj);
1082 * BEWARE: a == a is NOT always True for floating Point values, as
1083 * NaN != NaN is defined, so we must check this here.
1085 if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt) {
1086 if (a.tv == tarval_top || b.tv == tarval_top) {
1087 node->type.tv = tarval_top;
1088 } else if (r->part == l->part) {
1089 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1090 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1091 node->type.tv = tarval_bottom;
1093 default_compute(node);
1096 default_compute(node);
1098 } /* compute_Proj_Cmp */
1101 * (Re-)compute the type for a Proj(Cond).
1103 * @param node the node
1104 * @param cond the predecessor Cond node
1106 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1107 ir_node *proj = node->node;
1108 long pnc = get_Proj_proj(proj);
1109 ir_node *sel = get_Cond_selector(cond);
1110 node_t *selector = get_irn_node(sel);
1112 if (get_irn_mode(sel) == mode_b) {
1114 if (pnc == pn_Cond_true) {
1115 if (selector->type.tv == tarval_b_false) {
1116 node->type.tv = tarval_unreachable;
1117 } else if (selector->type.tv == tarval_b_true) {
1118 node->type.tv = tarval_reachable;
1119 } else if (selector->type.tv == tarval_bottom) {
1120 node->type.tv = tarval_reachable;
1122 assert(selector->type.tv == tarval_top);
1123 node->type.tv = tarval_unreachable;
1126 assert(pnc == pn_Cond_false);
1128 if (selector->type.tv == tarval_b_false) {
1129 node->type.tv = tarval_reachable;
1130 } else if (selector->type.tv == tarval_b_true) {
1131 node->type.tv = tarval_unreachable;
1132 } else if (selector->type.tv == tarval_bottom) {
1133 node->type.tv = tarval_reachable;
1135 assert(selector->type.tv == tarval_top);
1136 node->type.tv = tarval_unreachable;
1141 if (selector->type.tv == tarval_bottom) {
1142 node->type.tv = tarval_reachable;
1143 } else if (selector->type.tv == tarval_top) {
1144 node->type.tv = tarval_unreachable;
1146 long value = get_tarval_long(selector->type.tv);
1147 if (pnc == get_Cond_defaultProj(cond)) {
1148 /* default switch, have to check ALL other cases */
1151 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1152 ir_node *succ = get_irn_out(cond, i);
1156 if (value == get_Proj_proj(succ)) {
1157 /* we found a match, will NOT take the default case */
1158 node->type.tv = tarval_unreachable;
1162 /* all cases checked, no match, will take default case */
1163 node->type.tv = tarval_reachable;
1166 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1170 } /* compute_Proj_Cond */
1173 * (Re-)compute the type for a Proj-Nodes.
1175 * @param node the node
1177 static void compute_Proj(node_t *node) {
1178 ir_node *proj = node->node;
1179 ir_mode *mode = get_irn_mode(proj);
1182 if (mode == mode_M) {
1183 /* mode M is always bottom */
1184 node->type.tv = tarval_bottom;
1187 if (mode != mode_X) {
1188 ir_node *cmp = get_Proj_pred(proj);
1190 compute_Proj_Cmp(node, cmp);
1192 default_compute(node);
1195 /* handle mode_X nodes */
1196 pred = get_Proj_pred(proj);
1198 switch (get_irn_opcode(pred)) {
1200 /* the Proj_X from the Start is always reachable */
1201 node->type.tv = tarval_reachable;
1204 compute_Proj_Cond(node, pred);
1207 default_compute(node);
1209 } /* compute_Proj */
1212 * (Re-)compute the type for a given node.
1214 * @param node the node
1216 static void compute(node_t *node) {
1217 compute_func func = (compute_func)node->node->op->ops.generic;
1224 * Propagate constant evaluation.
1226 * @param env the environment
1228 static void propagate(environment_t *env) {
1231 lattice_elem_t old_type;
1236 while (env->cprop != NULL) {
1237 /* remove the first partition X from cprop */
1240 env->cprop = X->cprop_next;
1244 while (X->cprop != NULL) {
1245 /* remove the first Node x from X.cprop */
1248 X->cprop = x->cprop_next;
1250 /* compute a new type for x */
1252 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1254 if (x->type.tv != old_type.tv) {
1255 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1257 /* Add x to fallen. */
1262 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1263 ir_node *succ = get_irn_out(x->node, i);
1264 node_t *y = get_irn_node(succ);
1266 /* Add y to y.partition.cprop. */
1267 add_node_to_cprop(y, env);
1272 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1273 Y = split(X, fallen, env);
1283 * Get the leader for a given node from its congruence class.
1285 * @param irn the node
1287 static ir_node *get_leader(node_t *node) {
1288 partition_t *part = node->part;
1290 if (part->n_nodes > 1) {
1291 DB((dbg, LEVEL_2, "Found congruence class for %+F\n", node->node));
1293 return get_first_node(part)->node;
1299 * Post-Walker, apply the analysis results;
1301 static void apply_result(ir_node *irn, void *ctx) {
1302 environment_t *env = ctx;
1303 node_t *node = get_irn_node(irn);
1305 if (is_Block(irn)) {
1306 if (irn == get_irg_end_block(current_ir_graph)) {
1307 /* the EndBlock is always reachable even if the analysis
1308 finds out the opposite :-) */
1312 if (node->type.tv == tarval_unreachable) {
1313 /* mark dead blocks */
1314 set_Block_dead(irn);
1316 } else if (is_End(irn)) {
1317 /* do not touch the End node */
1319 node_t *block = get_irn_node(get_nodes_block(irn));
1321 if (block->type.tv == tarval_unreachable) {
1322 ir_node *bad = get_irg_bad(current_ir_graph);
1324 /* here, bad might already have a node, but this can be safely ignored
1325 as long as bad has at least ONE valid node */
1326 set_irn_node(bad, node);
1328 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1331 else if (get_irn_mode(irn) == mode_X) {
1332 if (node->type.tv == tarval_unreachable) {
1333 ir_node *bad = get_irg_bad(current_ir_graph);
1335 /* see comment above */
1336 set_irn_node(bad, node);
1338 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1339 exchange(irn, get_irg_bad(current_ir_graph));
1341 else if (is_Proj(irn)) {
1343 ir_node *cond = get_Proj_pred(irn);
1345 if (is_Cond(cond)) {
1346 node_t *sel = get_irn_node(get_Cond_selector(cond));
1348 if (is_tarval(sel->type.tv) && tarval_is_constant(sel->type.tv)) {
1349 /* Cond selector is a constant, make a Jmp */
1350 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1351 set_irn_node(jmp, node);
1353 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1359 /* normal data node */
1360 if (is_tarval(node->type.tv) && tarval_is_constant(node->type.tv)) {
1361 tarval *tv = node->type.tv;
1363 if (! is_Const(irn)) {
1364 /* can be replaced by a constant */
1365 ir_node *c = new_r_Const(current_ir_graph, block->node, get_tarval_mode(tv), tv);
1366 set_irn_node(c, node);
1368 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, c));
1371 } else if (is_entity(node->type.sym.entity_p)) {
1372 if (! is_SymConst(irn)) {
1373 /* can be replaced by a Symconst */
1374 ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
1375 set_irn_node(symc, node);
1378 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, symc));
1379 exchange(irn, symc);
1382 ir_node *leader = get_leader(node);
1384 if (leader != irn) {
1385 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, leader));
1386 exchange(irn, leader);
1391 } /* static void apply_result(ir_node *irn, void *ctx) {
1394 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1397 * sets the generic functions to compute.
1399 static void set_compute_functions(void) {
1402 /* set the default compute function */
1403 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1404 ir_op *op = get_irp_opcode(i);
1405 op->ops.generic = (op_func)default_compute;
1408 /* set specific functions */
1417 } /* set_compute_functions */
1419 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1420 ir_node *irn = local != NULL ? local : n;
1421 node_t *node = get_irn_node(n);
1423 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1427 void combo(ir_graph *irg) {
1431 ir_graph *rem = current_ir_graph;
1433 current_ir_graph = irg;
1435 /* register a debug mask */
1436 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1437 firm_dbg_set_mask(dbg, SET_LEVEL_1);
1439 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1441 obstack_init(&env.obst);
1442 env.worklist = NULL;
1446 #ifdef DEBUG_libfirm
1447 env.dbg_list = NULL;
1449 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1450 env.type2id_map = pmap_create();
1451 env.end_idx = get_opt_global_cse() ? 0 : -1;
1452 env.lambda_input = 0;
1454 assure_irg_outs(irg);
1456 /* we have our own value_of function */
1457 set_value_of_func(get_node_tarval);
1459 set_compute_functions();
1461 /* create the initial partition and place it on the work list */
1462 env.initial = new_partition(&env);
1463 add_to_worklist(env.initial, &env);
1464 irg_walk_graph(irg, NULL, create_initial_partitions, &env);
1466 /* Place the START Node's partition on cprop.
1467 Place the START Node on its local worklist. */
1468 initial_X = get_irg_initial_exec(irg);
1469 start = get_irn_node(initial_X);
1470 add_node_to_cprop(start, &env);
1474 if (env.worklist != NULL)
1476 } while (env.cprop != NULL || env.worklist != NULL);
1478 dump_all_partitions(&env);
1480 set_dump_node_vcgattr_hook(dump_partition_hook);
1481 dump_ir_block_graph(irg, "-partition");
1482 set_dump_node_vcgattr_hook(NULL);
1485 /* apply the result */
1486 irg_walk_graph(irg, NULL, apply_result, &env);
1488 pmap_destroy(env.type2id_map);
1489 del_set(env.opcode2id_map);
1490 obstack_free(&env.obst, NULL);
1492 /* restore value_of() default behavior */
1493 set_value_of_func(NULL);
1494 current_ir_graph = rem;