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_bottom : tarval_unreachable;
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);
424 DB((dbg, LEVEL_3, "Placing %+F in partition %u\n", irn, part->nr));
427 } /* create_partition_node */
430 * Walker, initialize all Nodes' type to U or top and place
431 * all nodes into the TOP partition.
433 static void create_initial_partitions(ir_node *irn, void *ctx) {
434 environment_t *env = ctx;
435 partition_t *part = env->initial;
439 node = create_partition_node(irn, part, env);
441 arity = get_irn_arity(irn);
442 if (arity > part->max_arity)
443 part->max_arity = arity;
444 if (node->max_user_input > part->max_user_inputs)
445 part->max_user_inputs = node->max_user_input;
446 } /* create_initial_partitions */
449 * Add a partition to the touched set if not already there.
451 * @param part the partition
452 * @param env the environment
454 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
455 if (part->on_touched == 0) {
456 part->touched_next = env->touched;
458 part->on_touched = 1;
460 } /* add_to_touched */
463 * Add a node to the entry.partition.touched set if not already there.
467 static INLINE void add_to_partition_touched(node_t *y) {
468 if (y->on_touched == 0) {
469 partition_t *part = y->part;
471 y->next = part->touched;
476 } /* add_to_partition_touched */
479 * Update the worklist: If Z is on worklist then add Z' to worklist.
480 * Else add the smaller of Z and Z' to worklist.
482 * @param Z the Z partition
483 * @param Z_prime the Z' partition, a previous part of Z
484 * @param env the environment
486 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
487 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
488 add_to_worklist(Z_prime, env);
490 add_to_worklist(Z, env);
492 } /* update_worklist */
495 * Split a partition by a local list.
497 * @param Z the Z partition to split
498 * @param g a (non-empty) node list
499 * @param env the environment
501 * @return a new partition containing the nodes of g
503 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
504 partition_t *Z_prime;
507 int max_input, max_arity, arity;
509 dump_partition("Splitting ", Z);
513 /* Remove g from Z. */
514 for (node = g; node != NULL; node = node->next) {
515 list_del(&node->node_list);
518 assert(n < Z->n_nodes);
521 /* Move g to a new partition, Z
\92. */
522 Z_prime = new_partition(env);
523 max_arity = max_input = 0;
524 for (node = g; node != NULL; node = node->next) {
525 list_add(&node->node_list, &Z_prime->entries);
526 node->part = Z_prime;
527 arity = get_irn_arity(node->node);
528 if (arity > max_arity)
530 if (node->max_user_input > max_input)
531 max_input = node->max_user_input;
533 Z_prime->max_arity = max_arity;
534 Z_prime->max_user_inputs = max_input;
535 Z_prime->n_nodes = n;
537 update_worklist(Z, Z_prime, env);
539 dump_partition("Now ", Z);
540 dump_partition("Created new ", Z_prime);
545 * Returns non-zero if the i'th input of a Phi node is live.
547 * @param phi a Phi-node
548 * @param i an input number
550 * @return non-zero if the i'th input of the given Phi node is live
552 static int is_live_input(ir_node *phi, int i) {
554 ir_node *block = get_nodes_block(phi);
555 ir_node *pred = get_Block_cfgpred(block, i);
556 lattice_elem_t type = get_node_type(pred);
558 return type.tv != tarval_unreachable;
560 /* else it's the control input, always live */
562 } /* is_live_input */
565 * Return non-zero if a type is a constant.
567 static int is_constant_type(lattice_elem_t type) {
568 if (type.tv != tarval_bottom && type.tv != tarval_top)
571 } /* is_constant_type */
574 * Place a node on the cprop list.
577 * @param env the environment
579 static void add_node_to_cprop(node_t *y, environment_t *env) {
580 /* Add y to y.partition.cprop. */
581 if (y->on_cprop == 0) {
582 partition_t *Y = y->part;
584 y->cprop_next = Y->cprop;
588 DB((dbg, LEVEL_3, "Add %+F to part%u.cprop\n", y->node, Y->nr));
590 /* place its partition on the cprop list */
591 if (Y->on_cprop == 0) {
592 Y->cprop_next = env->cprop;
597 if (get_irn_mode(y->node) == mode_T) {
598 /* mode_T nodes always produce tarval_bottom, so we must explicitly
599 add it's Proj's to get constant evaluation to work */
602 for (i = get_irn_n_outs(y->node) - 1; i >= 0; --i) {
603 node_t *proj = get_irn_node(get_irn_out(y->node, i));
605 add_node_to_cprop(proj, env);
608 } /* add_node_to_cprop */
611 * Split the partitions if caused by the first entry on the worklist.
613 * @param env the environment
615 static void cause_splits(environment_t *env) {
616 partition_t *X, *Y, *Z;
622 /* remove the first partition from the worklist */
624 env->worklist = X->wl_next;
627 dump_partition("Cause_split: ", X);
628 end_idx = env->end_idx;
629 for (i = -1; i <= X->max_user_inputs; ++i) {
630 /* empty the touched set: already done, just clear the list */
633 list_for_each_entry(node_t, x, &X->entries, node_list) {
639 num_edges = get_irn_n_outs(x->node);
641 while (x->next_edge <= num_edges) {
642 ir_def_use_edge *edge = &x->node->out[x->next_edge];
644 /* check if we have necessary edges */
652 /* ignore the "control input" for non-pinned nodes
653 if we are running in GCSE mode */
654 if (i < end_idx && get_irn_pinned(succ) != op_pin_state_pinned)
657 y = get_irn_node(succ);
658 if (is_constant_type(y->type)) {
659 code = get_irn_opcode(succ);
660 if (code == iro_Sub || (code == iro_Proj && is_Cmp(get_Proj_pred(succ))))
661 add_node_to_cprop(y, env);
664 /* Partitions of constants should not be split simply because their Nodes have unequal
665 functions or incongruent inputs. */
666 if (y->type.tv == tarval_bottom &&
667 (! is_Phi(x->node) || is_live_input(x->node, i))) {
669 add_to_touched(Y, env);
670 add_to_partition_touched(y);
675 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
676 /* remove it from the touched set */
679 if (Z->n_nodes != Z->n_touched) {
680 split(Z, Z->touched, env);
682 /* Empty local Z.touched. */
683 for (e = Z->touched; e != NULL; e = e->next) {
693 * Implements split_by_what(): Split a partition by characteristics given
694 * by the what function.
696 * @param X the partition to split
697 * @param What a function returning an Id for every node of the partition X
698 * @param P an flexible array to store the result partitions or NULL
699 * @param env the environment
701 * @return if P != NULL P will be filled with the resulting partitions and returned
703 static partition_t **split_by_what(partition_t *X, what_func What,
704 partition_t **P, environment_t *env) {
707 listmap_entry_t *iter;
710 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
712 list_for_each_entry(node_t, x, &X->entries, node_list) {
713 void *id = What(x, env);
714 listmap_entry_t *entry;
717 /* input not allowed, ignore */
720 /* Add x to map[What(x)]. */
721 entry = listmap_find(&map, id);
722 x->next = entry->list;
725 /* Let P be a set of Partitions. */
727 /* for all sets S except one in the range of map do */
728 for (iter = map.values; iter != NULL; iter = iter->next) {
729 if (iter->next == NULL) {
730 /* this is the last entry, ignore */
735 /* Add SPLIT( X, S ) to P. */
736 R = split(X, S, env);
738 ARR_APP1(partition_t *, P, R);
743 ARR_APP1(partition_t *, P, X);
748 } /* split_by_what */
750 /** lambda n.(n.type) */
751 static void *lambda_type(const node_t *node, environment_t *env) {
753 return node->type.tv;
756 /** lambda n.(n.opcode) */
757 static void *lambda_opcode(const node_t *node, environment_t *env) {
758 opcode_key_t key, *entry;
760 key.code = get_irn_opcode(node->node);
761 key.mode = get_irn_mode(node->node);
762 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
764 } /* lambda_opcode */
766 /** lambda n.(n[i].partition) */
767 static void *lambda_partition(const node_t *node, environment_t *env) {
770 int i = env->lambda_input;
772 if (i >= get_irn_arity(node->node)) {
773 /* we are outside the allowed range */
777 /* ignore the "control input" for non-pinned nodes
778 if we are running in GCSE mode */
779 if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
782 pred = get_irn_n(node->node, i);
783 p = get_irn_node(pred);
786 } /* lambda_partition */
789 * Checks whether a type is a constant.
791 static int is_type_constant(lattice_elem_t type) {
792 if (is_tarval(type.tv))
793 return tarval_is_constant(type.tv);
794 /* else it is a symconst */
799 * Implements split_by().
801 * @param X the partition to split
802 * @param env the environment
804 static void split_by(partition_t *X, environment_t *env) {
805 partition_t **P = NEW_ARR_F(partition_t *, 0);
808 P = split_by_what(X, lambda_type, P, env);
809 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
810 partition_t *Y = P[i];
812 if (Y->n_nodes > 1) {
813 lattice_elem_t type = get_partition_type(Y);
815 /* we do not want split the TOP, unreachable or constant partitions */
816 if (type.tv != tarval_top && type.tv != tarval_unreachable && !is_type_constant(type)) {
817 partition_t **Q = NEW_ARR_F(partition_t *, 0);
819 Q = split_by_what(Y, lambda_opcode, Q, env);
821 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
822 partition_t *Z = Q[j];
824 for (k = Z->max_arity - 1; k >= -1; --k) {
825 if (Z->n_nodes > 1) {
826 env->lambda_input = k;
827 split_by_what(Z, lambda_partition, NULL, env);
839 * (Re-)compute the type for a given node.
841 * @param node the node
843 static void default_compute(node_t *node) {
845 ir_node *irn = node->node;
847 if (get_irn_pinned(irn) == op_pin_state_pinned) {
848 node_t *block = get_irn_node(get_nodes_block(irn));
850 if (block->type.tv == tarval_unreachable) {
851 node->type.tv = tarval_top;
856 /* if any of the data inputs have type top, the result is type top */
857 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
858 ir_node *pred = get_irn_n(irn, i);
859 node_t *p = get_irn_node(pred);
861 if (p->type.tv == tarval_top) {
862 node->type.tv = tarval_top;
867 node->type.tv = computed_value(irn);
868 } /* default_compute */
871 * (Re-)compute the type for a Block node.
873 * @param node the node
875 static void compute_Block(node_t *node) {
877 ir_node *block = node->node;
879 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
880 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
882 if (pred->type.tv == tarval_reachable) {
883 /* A block is reachable, if at least of predecessor is reachable. */
884 node->type.tv = tarval_reachable;
888 node->type.tv = tarval_unreachable;
889 } /* compute_Block */
892 * (Re-)compute the type for a Jmp node.
894 * @param node the node
896 static void compute_Jmp(node_t *node) {
897 node_t *block = get_irn_node(get_nodes_block(node->node));
899 node->type = block->type;
903 * (Re-)compute the type for the End node.
905 * @param node the node
907 static void compute_End(node_t *node) {
908 /* the End node is NOT dead of course */
909 node->type.tv = tarval_reachable;
913 * (Re-)compute the type for a SymConst node.
915 * @param node the node
917 static void compute_SymConst(node_t *node) {
918 ir_node *irn = node->node;
919 node_t *block = get_irn_node(get_nodes_block(irn));
921 if (block->type.tv == tarval_unreachable) {
922 node->type.tv = tarval_top;
925 switch (get_SymConst_kind(irn)) {
926 case symconst_addr_ent:
927 case symconst_addr_name:
928 node->type.sym = get_SymConst_symbol(irn);
931 node->type.tv = computed_value(irn);
933 } /* compute_SymConst */
936 * (Re-)compute the type for a Phi node.
938 * @param node the node
940 static void compute_Phi(node_t *node) {
942 ir_node *phi = node->node;
945 /* if a Phi is in a unreachable block, its type is TOP */
946 node_t *block = get_irn_node(get_nodes_block(phi));
948 if (block->type.tv == tarval_unreachable) {
949 node->type.tv = tarval_top;
953 /* Phi implements the Meet operation */
954 type.tv = tarval_top;
955 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
956 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
958 if (pred->type.tv == tarval_top) {
959 /* ignore TOP inputs */
962 if (pred->type.tv == tarval_bottom) {
963 node->type.tv = tarval_bottom;
965 } else if (type.tv == tarval_top) {
966 /* first constant found */
968 } else if (type.tv != pred->type.tv) {
969 /* different constants or tarval_bottom */
970 node->type.tv = tarval_bottom;
973 /* else nothing, constants are the same */
979 * (Re-)compute the type for an Add. Special case: one nodes is a Zero Const.
981 * @param node the node
983 static void compute_Add(node_t *node) {
984 ir_node *sub = node->node;
985 node_t *l = get_irn_node(get_Add_left(sub));
986 node_t *r = get_irn_node(get_Add_right(sub));
987 lattice_elem_t a = l->type;
988 lattice_elem_t b = r->type;
989 node_t *block = get_irn_node(get_nodes_block(sub));
992 if (block->type.tv == tarval_unreachable) {
993 node->type.tv = tarval_top;
997 if (a.tv == tarval_top || b.tv == tarval_top) {
998 node->type.tv = tarval_top;
999 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1000 node->type.tv = tarval_bottom;
1002 /* x + 0 = 0 + x = x, but beware of floating point +0 + -0, so we
1003 must call tarval_add() first to handle this case! */
1004 if (is_tarval(a.tv)) {
1005 if (is_tarval(b.tv)) {
1006 node->type.tv = tarval_add(a.tv, b.tv);
1009 mode = get_tarval_mode(a.tv);
1010 if (a.tv == get_mode_null(mode)) {
1014 } else if (is_tarval(b.tv)) {
1015 mode = get_tarval_mode(b.tv);
1016 if (b.tv == get_mode_null(mode)) {
1021 node->type.tv = tarval_bottom;
1026 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
1028 * @param node the node
1030 static void compute_Sub(node_t *node) {
1031 ir_node *sub = node->node;
1032 node_t *l = get_irn_node(get_Sub_left(sub));
1033 node_t *r = get_irn_node(get_Sub_right(sub));
1034 lattice_elem_t a = l->type;
1035 lattice_elem_t b = r->type;
1036 node_t *block = get_irn_node(get_nodes_block(sub));
1038 if (block->type.tv == tarval_unreachable) {
1039 node->type.tv = tarval_top;
1043 if (a.tv == tarval_top || b.tv == tarval_top) {
1044 node->type.tv = tarval_top;
1045 } else if (r->part == l->part) {
1046 ir_mode *mode = get_irn_mode(sub);
1047 node->type.tv = get_mode_null(mode);
1048 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1049 node->type.tv = tarval_bottom;
1051 if (is_tarval(a.tv) && is_tarval(b.tv))
1052 node->type.tv = tarval_sub(a.tv, b.tv);
1054 node->type.tv = tarval_bottom;
1059 * (Re-)compute the type for a Proj(Cmp).
1061 * @param node the node
1062 * @param cond the predecessor Cmp node
1064 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
1065 ir_node *proj = node->node;
1066 node_t *l = get_irn_node(get_Cmp_left(cmp));
1067 node_t *r = get_irn_node(get_Cmp_right(cmp));
1068 lattice_elem_t a = l->type;
1069 lattice_elem_t b = r->type;
1070 pn_Cmp pnc = get_Proj_proj(proj);
1073 * BEWARE: a == a is NOT always True for floating Point values, as
1074 * NaN != NaN is defined, so we must check this here.
1076 if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt) {
1077 if (a.tv == tarval_top || b.tv == tarval_top) {
1078 node->type.tv = tarval_top;
1079 } else if (r->part == l->part) {
1080 node->type.tv = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
1081 } else if (a.tv == tarval_bottom || b.tv == tarval_bottom) {
1082 node->type.tv = tarval_bottom;
1084 default_compute(node);
1087 default_compute(node);
1089 } /* compute_Proj_Cmp */
1092 * (Re-)compute the type for a Proj(Cond).
1094 * @param node the node
1095 * @param cond the predecessor Cond node
1097 static void compute_Proj_Cond(node_t *node, ir_node *cond) {
1098 ir_node *proj = node->node;
1099 long pnc = get_Proj_proj(proj);
1100 ir_node *sel = get_Cond_selector(cond);
1101 node_t *selector = get_irn_node(sel);
1103 if (get_irn_mode(sel) == mode_b) {
1105 if (pnc == pn_Cond_true) {
1106 if (selector->type.tv == tarval_b_false) {
1107 node->type.tv = tarval_unreachable;
1108 } else if (selector->type.tv == tarval_b_true) {
1109 node->type.tv = tarval_reachable;
1110 } else if (selector->type.tv == tarval_bottom) {
1111 node->type.tv = tarval_reachable;
1113 assert(selector->type.tv == tarval_top);
1114 node->type.tv = tarval_unreachable;
1117 assert(pnc == pn_Cond_false);
1119 if (selector->type.tv == tarval_b_false) {
1120 node->type.tv = tarval_reachable;
1121 } else if (selector->type.tv == tarval_b_true) {
1122 node->type.tv = tarval_unreachable;
1123 } else if (selector->type.tv == tarval_bottom) {
1124 node->type.tv = tarval_reachable;
1126 assert(selector->type.tv == tarval_top);
1127 node->type.tv = tarval_unreachable;
1132 if (selector->type.tv == tarval_bottom) {
1133 node->type.tv = tarval_reachable;
1134 } else if (selector->type.tv == tarval_top) {
1135 node->type.tv = tarval_unreachable;
1137 long value = get_tarval_long(selector->type.tv);
1138 if (pnc == get_Cond_defaultProj(cond)) {
1139 /* default switch, have to check ALL other cases */
1142 for (i = get_irn_n_outs(cond) - 1; i >= 0; --i) {
1143 ir_node *succ = get_irn_out(cond, i);
1147 if (value == get_Proj_proj(succ)) {
1148 /* we found a match, will NOT take the default case */
1149 node->type.tv = tarval_unreachable;
1153 /* all cases checked, no match, will take default case */
1154 node->type.tv = tarval_reachable;
1157 node->type.tv = value == pnc ? tarval_reachable : tarval_unreachable;
1161 } /* compute_Proj_Cond */
1164 * (Re-)compute the type for a Proj-Nodes.
1166 * @param node the node
1168 static void compute_Proj(node_t *node) {
1169 ir_node *proj = node->node;
1170 ir_mode *mode = get_irn_mode(proj);
1173 if (mode == mode_M) {
1174 /* mode M is always bottom */
1175 node->type.tv = tarval_bottom;
1178 if (mode != mode_X) {
1179 ir_node *cmp = get_Proj_pred(proj);
1181 compute_Proj_Cmp(node, cmp);
1183 default_compute(node);
1186 /* handle mode_X nodes */
1187 pred = get_Proj_pred(proj);
1189 switch (get_irn_opcode(pred)) {
1191 /* the Proj_X from the Start is always reachable */
1192 node->type.tv = tarval_reachable;
1195 compute_Proj_Cond(node, pred);
1198 default_compute(node);
1200 } /* compute_Proj */
1203 * (Re-)compute the type for a given node.
1205 * @param node the node
1207 static void compute(node_t *node) {
1208 compute_func func = (compute_func)node->node->op->ops.generic;
1215 * Propagate constant evaluation.
1217 * @param env the environment
1219 static void propagate(environment_t *env) {
1222 lattice_elem_t old_type;
1227 while (env->cprop != NULL) {
1228 /* remove the first partition X from cprop but do not set the bit here */
1230 env->cprop = X->cprop_next;
1235 /* remove the first Node x from X.cprop but do NOT set the bit here */
1237 X->cprop = x->cprop_next;
1239 /* compute a new type for x */
1241 DB((dbg, LEVEL_3, "computing type of %+F\n", x->node));
1243 if (x->type.tv != old_type.tv) {
1244 DB((dbg, LEVEL_2, "node %+F has changed type from %+F to %+F\n", x->node, old_type, x->type));
1245 /* Add x to fallen. */
1250 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
1251 ir_node *succ = get_irn_out(x->node, i);
1252 node_t *y = get_irn_node(succ);
1254 /* Add y to y.partition.cprop. */
1255 add_node_to_cprop(y, env);
1258 /* now remove x from X.cprop: this ensures that a node is not placed on the list again
1259 if is its user by itself (happens for Phi nodes and dead code) */
1261 } while (X->cprop != NULL);
1263 /* now remove X from cprop, we have emptied it's local list */
1266 if (n_fallen > 0 && n_fallen != X->n_nodes) {
1267 Y = split(X, fallen, env);
1277 * Get the leader for a given node from its congruence class.
1279 * @param irn the node
1281 static ir_node *get_leader(node_t *node) {
1282 partition_t *part = node->part;
1284 if (part->n_nodes > 1) {
1285 DB((dbg, LEVEL_2, "Found congruence class for %+F ", node->node));
1287 return get_first_node(part)->node;
1293 * Post-Walker, apply the analysis results;
1295 static void apply_result(ir_node *irn, void *ctx) {
1296 environment_t *env = ctx;
1297 node_t *node = get_irn_node(irn);
1299 if (is_Block(irn)) {
1300 if (node->type.tv == tarval_unreachable) {
1301 set_Block_dead(irn);
1304 node_t *block = get_irn_node(get_nodes_block(irn));
1306 if (block->type.tv == tarval_unreachable) {
1307 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1308 exchange(irn, get_irg_bad(current_ir_graph));
1310 else if (get_irn_mode(irn) == mode_X) {
1311 if (node->type.tv == tarval_unreachable) {
1312 DB((dbg, LEVEL_1, "%+F is unreachable\n", irn));
1313 exchange(irn, get_irg_bad(current_ir_graph));
1315 else if (is_Proj(irn)) {
1317 ir_node *cond = get_Proj_pred(irn);
1319 if (is_Cond(cond)) {
1320 ir_node *sel = get_Cond_selector(cond);
1322 if (is_Const(sel)) {
1323 /* Cond selector was replaced by a constant, make a Jmp */
1324 ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
1326 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
1332 /* normal data node */
1333 ir_node *leader = get_leader(node);
1335 if (leader != irn) {
1336 DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, leader));
1337 exchange(irn, leader);
1341 } /* static void apply_result(ir_node *irn, void *ctx) {
1344 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
1347 * sets the generic functions to compute.
1349 static void set_compute_functions(void) {
1352 /* set the default compute function */
1353 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
1354 ir_op *op = get_irp_opcode(i);
1355 op->ops.generic = (op_func)default_compute;
1358 /* set specific functions */
1367 } /* set_compute_functions */
1369 static int dump_partition_hook(FILE *F, ir_node *n, ir_node *local) {
1370 ir_node *irn = local != NULL ? local : n;
1371 node_t *node = get_irn_node(n);
1373 ir_fprintf(F, "info2 : \"partition %u type %+F\"\n", node->part->nr, node->type);
1377 void combo(ir_graph *irg) {
1381 ir_graph *rem = current_ir_graph;
1383 current_ir_graph = irg;
1385 /* register a debug mask */
1386 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
1387 firm_dbg_set_mask(dbg, SET_LEVEL_1);
1389 DB((dbg, LEVEL_1, "Doing COMBO for %+F\n", irg));
1391 obstack_init(&env.obst);
1392 env.worklist = NULL;
1396 #ifdef DEBUG_libfirm
1397 env.dbg_list = NULL;
1399 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1400 env.type2id_map = pmap_create();
1401 env.end_idx = get_opt_global_cse() ? 0 : -1;
1402 env.lambda_input = 0;
1404 assure_irg_outs(irg);
1406 /* we have our own value_of function */
1407 set_value_of_func(get_node_tarval);
1409 set_compute_functions();
1411 /* create the initial partition and place it on the work list */
1412 env.initial = new_partition(&env);
1413 add_to_worklist(env.initial, &env);
1414 irg_walk_graph(irg, NULL, create_initial_partitions, &env);
1416 /* Place the START Node's partition on cprop.
1417 Place the START Node on its local worklist. */
1418 initial_X = get_irg_initial_exec(irg);
1419 start = get_irn_node(initial_X);
1420 add_node_to_cprop(start, &env);
1424 if (env.worklist != NULL)
1426 } while (env.cprop != NULL || env.worklist != NULL);
1428 dump_all_partitions(&env);
1430 set_dump_node_vcgattr_hook(dump_partition_hook);
1431 dump_ir_block_graph(irg, "-partition");
1434 /* apply the result */
1435 irg_walk_graph(irg, NULL, apply_result, &env);
1437 pmap_destroy(env.type2id_map);
1438 del_set(env.opcode2id_map);
1439 obstack_free(&env.obst, NULL);
1441 /* restore value_of() default behavior */
1442 set_value_of_func(NULL);
1443 current_ir_graph = rem;