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"
43 #include "irgraph_t.h"
52 /* we need the tarval_R and tarval_U */
53 #define tarval_U tarval_undefined
54 #define tarval_R tarval_bad
56 typedef struct node_t node_t;
57 typedef struct partition_t partition_t;
58 typedef struct opcode_key_t opcode_key_t;
59 typedef struct opcode_entry_t opcode_entry_t;
60 typedef struct listmap_entry_t listmap_entry_t;
62 /** The type of the compute function. */
63 typedef void (*compute_func)(node_t *node);
69 ir_opcode code; /**< The Firm opcode. */
70 ir_mode *mode; /**< The mode of all nodes in the partition. */
74 * An entry in the opcode map.
76 struct opcode_entry_t {
77 opcode_key_t key; /**< The key. */
78 partition_t *part; /**< The associated partition. */
82 * An entry in the list_map.
84 struct listmap_entry_t {
85 void *id; /**< The id. */
86 node_t *list; /**< The associated list for this id. */
87 listmap_entry_t *next; /**< Link to the next entry in the map. */
90 /** We must map id's to lists. */
91 typedef struct listmap_t {
92 set *map; /**< Map id's to listmap_entry_t's */
93 listmap_entry_t *values; /**< List of all values in the map. */
101 ir_node *node; /**< The IR-node itself. */
102 list_head node_list; /**< Double-linked list of entries. */
103 partition_t *part; /**< points to the partition this node belongs to */
104 node_t *cprop_next; /**< Next node on partition.cprop list. */
105 node_t *next; /**< Next node on local list (partition.touched, fallen). */
106 tarval *type; /**< The associated lattice element "type". */
107 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
108 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
112 * A partition containing congruent nodes.
115 list_head entries; /**< The head of partition node list. */
116 node_t *cprop; /**< The partition.cprop list. */
117 partition_t *wl_next; /**< Next entry in the work list if any. */
118 partition_t *touched_next; /**< Points to the next partition in the touched set. */
119 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
120 node_t *touched; /**< The partition.touched set of this partition. */
121 unsigned n_nodes; /**< Number of entries in this partition. */
122 unsigned n_touched; /**< Number of entries in the partition.touched. */
123 int n_inputs; /**< Maximum number of inputs of all entries. */
124 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
125 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
126 unsigned on_cprop:1; /**< Set, if this partition is on the cprop list. */
128 partition_t *dbg_next; /**< Link all partitions for debugging */
129 unsigned nr; /**< A unique number for (what-)mapping, >0. */
133 typedef struct environment_t {
134 struct obstack obst; /**< obstack to allocate data structures. */
135 partition_t *worklist; /**< The work list. */
136 partition_t *cprop; /**< The constant propagation list. */
137 partition_t *touched; /**< the touched set. */
138 partition_t *TOP; /**< The TOP partition. */
140 partition_t *dbg_list; /**< List of all partitions. */
142 set *opcode_map; /**< The initial opcode->partition map. */
143 set *opcode2id_map; /**< The opcodeMode->id map. */
144 pmap *type2id_map; /**< The type->id map. */
145 int end_idx; /**< -1 for local and 0 for global congruences. */
146 int lambda_input; /**< Captured argument for lambda_partition(). */
149 /** Type of the what function. */
150 typedef void *(*what_func)(const node_t *node, environment_t *env);
152 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
153 #define set_irn_node(irn, node) set_irn_link(irn, node)
155 /** The debug module handle. */
156 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
158 /** Next partition number. */
159 DEBUG_ONLY(static unsigned part_nr = 0);
163 * Dump partition to output.
165 static void dump_partition(const char *msg, partition_t *part) {
169 DB((dbg, LEVEL_2, "%s part%u (%u) {\n ", msg, part->nr, part->n_nodes));
170 list_for_each_entry(node_t, node, &part->entries, node_list) {
171 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
174 DB((dbg, LEVEL_2, "\n}\n"));
178 * Dump all partitions.
180 static void dump_all_partitions(environment_t *env) {
183 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
184 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
185 dump_partition("", P);
189 #define dump_partition(msg, part)
190 #define dump_all_partitions(env)
194 * compare two pointer values.
196 static int cmp_ptr(const void *elt, const void *key, size_t size) {
197 const listmap_entry_t *e1 = elt;
198 const listmap_entry_t *e2 = key;
200 return e1->id != e2->id;
204 * Creates a new listmap.
206 static void new_listmap(listmap_t *map) {
207 map->map = new_set(cmp_ptr, 16);
214 static void del_listmap(listmap_t *map) {
219 * Return the associated listmap entry for a given id.
221 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
222 listmap_entry_t key, *entry;
227 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
229 if (entry->list == NULL) {
230 /* a new entry, put into the list */
231 entry->next = map->values;
238 * calculate the hash value for an opcode map entry.
240 static unsigned opcode_hash(const opcode_key_t *entry) {
241 return (entry->mode - (ir_mode *)0) * 9 + entry->code;
245 * Compare two entries in the opcode map.
247 static int cmp_opcode(const void *elt, const void *key, size_t size) {
248 const opcode_key_t *o1 = elt;
249 const opcode_key_t *o2 = key;
251 return o1->code != o2->code || o1->mode != o2->mode;
254 /** Return the type of a node. */
255 static INLINE tarval *get_node_type(const ir_node *irn) {
256 return get_irn_node(irn)->type;
260 * Create a new empty partition.
262 static INLINE partition_t *new_partition(environment_t *env) {
263 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
265 INIT_LIST_HEAD(&part->entries);
267 part->wl_next = env->worklist;
268 part->touched_next = NULL;
269 part->cprop_next = NULL;
270 part->touched = NULL;
274 part->on_worklist = 0;
275 part->on_touched = 0;
278 part->dbg_next = env->dbg_list;
279 env->dbg_list = part;
280 part->nr = part_nr++;
287 * Get the partition for a given opcode.
289 static INLINE partition_t *get_partition_for_irn(const ir_node *irn, environment_t *env) {
290 opcode_entry_t key, *entry;
293 key.key.code = get_irn_opcode(irn);
294 key.key.mode = get_irn_mode(irn);
295 hash = opcode_hash(&key.key);
297 entry = set_find(env->opcode_map, &key, sizeof(key), hash);
299 /* create a new partition and place it on the wait queue */
300 partition_t *part = new_partition(env);
302 part->on_worklist = 1;
303 env->worklist = part;
306 set_insert(env->opcode_map, &key, sizeof(key), hash);
313 * Creates a partition node for the given IR-node and place it
314 * into the given partition.
316 static void create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
317 /* create a partition node and place it in the partition */
318 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
320 INIT_LIST_HEAD(&node->node_list);
323 node->cprop_next = NULL;
325 node->type = tarval_top; /* == tarval_U */
326 node->on_touched = 0;
328 set_irn_node(irn, node);
330 list_add_tail(&node->node_list, &part->entries);
333 DB((dbg, LEVEL_2, "Placing %+F in partition %u\n", irn, part->nr));
337 * Walker, initialize all Nodes' type to U or top and place
338 * all nodes into the TOP partition.
340 static void create_initial_partitions(ir_node *irn, void *ctx) {
341 environment_t *env = ctx;
342 partition_t *part = env->TOP;
345 create_partition_node(irn, part, env);
346 arity = get_irn_arity(irn);
347 if (arity > part->n_inputs)
348 part->n_inputs = arity;
352 * Add a partition to the touched set if not already there.
354 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
355 if (part->on_touched == 0) {
356 part->touched_next = env->touched;
358 part->on_touched = 1;
363 * Add a node to the entry.partition.touched set if not already there..
365 static INLINE void add_to_partition_touched(node_t *y) {
366 if (y->on_touched == 0) {
367 partition_t *part = y->part;
369 y->next = part->touched;
377 * update the worklist
379 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
380 /* If Z is on worklist then add Z' to worklist.
381 Else add the smaller of Z and Z' to worklist. */
382 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
383 Z_prime->on_worklist = 1;
384 Z_prime->wl_next = env->worklist;
385 env->worklist = Z_prime;
388 Z->wl_next = env->worklist;
394 * Split a partition by a local list.
396 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
397 partition_t *Z_prime;
402 dump_partition("Splitting ", Z);
404 /* Remove g from Z. */
405 for (node = g; node != NULL; node = node->next) {
406 list_del(&node->node_list);
411 /* Move g to a new partition, Z
\92. */
412 Z_prime = new_partition(env);
414 for (node = g; node != NULL; node = node->next) {
415 int arity = get_irn_arity(node->node);
416 list_add(&node->node_list, &Z_prime->entries);
417 node->part = Z_prime;
418 if (arity > n_inputs)
421 Z_prime->n_inputs = n_inputs;
422 Z_prime->n_nodes = n;
424 update_worklist(Z, Z_prime, env);
426 dump_partition("Now ", Z);
427 dump_partition("Created new ", Z_prime);
432 * Returns non-zero if the i'th input of a Phi node is live.
434 static int is_live_input(ir_node *phi, int i) {
435 ir_node *block = get_nodes_block(phi);
436 ir_node *pred = get_Block_cfgpred(block, i);
437 tarval *type = get_node_type(pred);
439 return type != tarval_U;
443 * Split the partitions if caused by the first entry on the worklist.
445 static void cause_splits(environment_t *env) {
446 partition_t *X, *Y, *Z;
450 /* remove the first partition from the worklist */
452 env->worklist = X->wl_next;
455 dump_partition("Cause_split: ", X);
456 end_idx = env->end_idx;
457 for (i = X->n_inputs - 1; i >= -1; --i) {
458 /* empty the touched set: already done, just clear the list */
461 list_for_each_entry(node_t, x, &X->entries, node_list) {
462 /* ignore the "control input" for non-pinned nodes
463 if we are running in GCSE mode */
464 if (i < end_idx && get_irn_pinned(x->node) != op_pin_state_pinned)
467 /* non-existing input */
468 if (i >= get_irn_arity(x->node))
471 y = get_irn_node(get_irn_n(x->node, i));
473 if (Y != env->TOP && (! is_Phi(x->node) || is_live_input(x->node, i))) {
474 add_to_touched(Y, env);
475 add_to_partition_touched(y);
479 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
480 /* remove it from the touched set */
483 if (Z->n_nodes != Z->n_touched) {
484 split(Z, Z->touched, env);
486 /* Empty local Z.touched. */
487 for (e = Z->touched; e != NULL; e = e->next) {
497 * Implements split_by_what(): Split a partition by characteristics given
498 * by the what function.
500 * @return list of partitions
502 static partition_t **split_by_what(partition_t *X, what_func What,
503 partition_t**P, environment_t *env) {
506 listmap_entry_t *iter;
509 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
511 list_for_each_entry(node_t, x, &X->entries, node_list) {
512 void *id = What(x, env);
513 listmap_entry_t *entry;
516 /* input not allowed, ignore */
519 /* Add x to map[What(x)]. */
520 entry = listmap_find(&map, id);
521 x->next = entry->list;
524 /* Let P be a set of Partitions. */
526 /* for all sets S except one in the range of map do */
527 for (iter = map.values; iter != NULL; iter = iter->next) {
528 if (iter->next == NULL) {
529 /* this is the last entry, ignore */
534 /* Add SPLIT( X, S ) to P. */
535 R = split(X, S, env);
537 ARR_APP1(partition_t *, P, R);
542 ARR_APP1(partition_t *, P, X);
549 /** lambda n.(n.type) */
550 static void *lambda_type(const node_t *node, environment_t *env) {
555 /** lambda n.(n.opcode) */
556 static void *lambda_opcode(const node_t *node, environment_t *env) {
557 opcode_key_t key, *entry;
559 key.code = get_irn_opcode(node->node);
560 key.mode = get_irn_mode(node->node);
561 entry = set_insert(env->opcode2id_map, &key, sizeof(&key), opcode_hash(&key));
565 /** lambda n.(n[i].partition) */
566 static void *lambda_partition(const node_t *node, environment_t *env) {
569 int i = env->lambda_input;
571 if (i >= get_irn_arity(node->node)) {
572 /* we are outside the allowed range */
576 /* ignore the "control input" for non-pinned nodes
577 if we are running in GCSE mode */
578 if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
581 pred = get_irn_n(node->node, i);
582 p = get_irn_node(pred);
588 * Implements split_by().
590 static void split_by(partition_t *X, environment_t *env) {
591 partition_t **P = NEW_ARR_F(partition_t *, 0);
594 P = split_by_what(X, lambda_type, P, env);
595 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
596 partition_t *Y = P[i];
599 partition_t **Q = NEW_ARR_F(partition_t *, 0);
601 Q = split_by_what(Y, lambda_opcode, Q, env);
603 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
604 partition_t *Z = Q[j];
606 for (k = Z->n_inputs - 1; k >= -1; --k) {
607 env->lambda_input = k;
608 split_by_what(Z, lambda_partition, NULL, env);
618 * (Re-)compute the type for a given node.
620 static void default_compute(node_t *node) {
622 ir_node *irn = node->node;
625 if (get_irn_pinned(irn) == op_pin_state_pinned) {
626 node_t *block = get_irn_node(get_nodes_block(irn));
628 if (block->type == tarval_U) {
629 node->type = tarval_top;
633 mode = get_irn_mode(irn);
634 if (mode == mode_M) {
635 /* mode M is always bottom for now */
636 node->type = tarval_bottom;
639 if (! mode_is_data(mode))
642 /* if any of the data inputs have type top, the result is type top */
643 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
644 ir_node *pred = get_irn_n(irn, i);
645 node_t *p = get_irn_node(pred);
647 if (p->type == tarval_top) {
648 node->type = tarval_top;
652 node->type = computed_value(irn);
656 * (Re-)compute the type for a Block node.
658 static void compute_Block(node_t *node) {
660 ir_node *block = node->node;
662 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
663 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
665 if (pred->type == tarval_R) {
666 /* A block is reachable, if at least of predecessor is reachable. */
667 node->type = tarval_R;
671 node->type = tarval_U;
675 * (Re-)compute the type for a Jmp node.
677 static void compute_Jmp(node_t *node) {
678 node_t *block = get_irn_node(get_nodes_block(node->node));
680 node->type = block->type;
684 * (Re-)compute the type for a Phi node.
686 static void compute_Phi(node_t *node) {
688 ir_node *phi = node->node;
689 tarval *type = tarval_top;
691 /* if a Phi is in a unreachable block, its type is TOP */
692 node_t *block = get_irn_node(get_nodes_block(phi));
694 if (block->type == tarval_U) {
695 node->type = tarval_top;
699 /* if any of the data inputs have type top, the result is type top */
700 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
701 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
703 if (pred->type == tarval_top) {
704 /* ignore TOP inputs */
707 if (pred->type == tarval_bottom) {
708 node->type = tarval_bottom;
710 } else if (type == tarval_top) {
711 /* first constant found */
713 } else if (type == pred->type) {
714 /* same constant, continue */
717 /* different constants or tarval_bottom */
718 node->type = tarval_bottom;
726 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
728 static void compute_Sub(node_t *node) {
729 ir_node *sub = node->node;
730 node_t *l = get_irn_node(get_Sub_left(sub));
731 node_t *r = get_irn_node(get_Sub_right(sub));
735 if (a == tarval_top || b == tarval_top) {
736 node->type = tarval_top;
737 } else if (r->part == l->part) {
738 ir_mode *mode = get_irn_mode(sub);
739 node->type = get_mode_null(mode);
740 } else if (a == tarval_bottom || b == tarval_bottom) {
741 node->type = tarval_bottom;
743 node->type = tarval_sub(a, b);
748 * (Re-)compute the type for a Proj(Cmp).
750 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
751 ir_node *proj = node->node;
752 node_t *l = get_irn_node(get_Cmp_left(cmp));
753 node_t *r = get_irn_node(get_Cmp_right(cmp));
756 pn_Cmp pnc = get_Proj_proj(proj);
759 * BEWARE: a == a is NOT always True for floating Point values, as
760 * NaN != NaN is defined, so we must check this here.
762 if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt) {
763 if (a == tarval_top || b == tarval_top) {
764 node->type = tarval_top;
765 } else if (r->part == l->part) {
766 node->type = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
767 } else if (a == tarval_bottom || b == tarval_bottom) {
768 node->type = tarval_bottom;
770 default_compute(node);
773 default_compute(node);
778 * (Re-)compute the type for a Proj-Nodes.
780 static void compute_Proj(node_t *node) {
781 ir_node *proj = node->node;
782 ir_mode *mode = get_irn_mode(proj);
785 if (mode == mode_M) {
786 /* mode M is always bottom */
787 node->type = tarval_bottom;
790 if (mode != mode_X) {
791 ir_node *cmp = get_Proj_pred(proj);
793 compute_Proj_Cmp(node, cmp);
795 default_compute(node);
798 /* handle mode_X nodes */
799 pred = get_Proj_pred(proj);
801 switch (get_irn_opcode(pred)) {
803 /* the Proj_X from the Start is always reachable */
804 node->type = tarval_R;
807 default_compute(node);
812 * (Re-)compute the type for a given node.
814 static void compute(node_t *node) {
815 compute_func func = (compute_func)node->node->op->ops.generic;
822 * place a node on the cprop list.
824 static void add_node_to_cprop(node_t *y, environment_t *env) {
825 /* Add y to y.partition.cprop. */
826 if (y->on_cprop == 0) {
827 partition_t *Y = y->part;
829 y->cprop_next = Y->cprop;
833 /* place its partition on the cprop list */
834 if (Y->on_cprop == 0) {
835 Y->cprop_next = env->cprop;
843 * Propagate constant evaluation.
845 static void propagate(environment_t *env) {
849 node_t *fallen = NULL;
850 unsigned n_fallen = 0;
853 while (env->cprop != NULL) {
854 /* remove a partition X from cprop */
857 env->cprop = X->cprop_next;
859 while (X->cprop != NULL) {
860 /* remove a Node x from X.cprop */
863 X->cprop = x->cprop_next;
865 /* compute a new type for x */
868 if (x->type != old_type) {
869 DB((dbg, LEVEL_2, "node %+F has changed type from %T to %T\n", x->node, old_type, x->type));
870 /* Add x to fallen. */
875 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
876 ir_node *succ = get_irn_out(x->node, i);
877 node_t *y = get_irn_node(succ);
879 /* Add y to y.partition.cprop. */
880 add_node_to_cprop(y, env);
884 if (n_fallen != X->n_nodes) {
885 Y = split(X, fallen, env);
894 * Get the leader for a given node from its congruence class.
896 * @param irn the node
898 static ir_node *get_leader(ir_node *irn) {
899 partition_t *part = get_irn_node(irn)->part;
901 if (part->n_nodes > 1) {
902 DB((dbg, LEVEL_2, "Found congruence class for %+F ", irn));
903 dump_partition("", part);
909 * Post-Walker, apply the analysis results;
911 static void apply_result(ir_node *irn, void *ctx) {
912 environment_t *env = ctx;
914 if (is_no_Block(irn)) {
915 ir_node *leader = get_leader(irn);
918 exchange(irn, leader);
923 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
926 * sets the generic functions to compute.
928 static void set_compute_functions(void) {
931 /* set the default compute function */
932 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
933 ir_op *op = get_irp_opcode(i);
934 op->ops.generic = (op_func)default_compute;
937 /* set specific functions */
944 void combo(ir_graph *irg) {
946 ir_node *start_bl, *initial_X;
948 ir_graph *rem = current_ir_graph;
950 current_ir_graph = irg;
952 /* register a debug mask */
953 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
954 firm_dbg_set_mask(dbg, SET_LEVEL_2);
956 obstack_init(&env.obst);
964 env.opcode_map = new_set(cmp_opcode, iro_Last * 4);
965 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
966 env.type2id_map = pmap_create();
967 env.end_idx = get_opt_global_cse() ? 0 : -1;
968 env.lambda_input = 0;
970 assure_irg_outs(irg);
972 /* we have our own value_of function */
973 set_value_of_func(get_node_type);
975 set_compute_functions();
977 /* create the initial TOP partition and place it on the work list */
978 env.TOP = new_partition(&env);
979 env.TOP->wl_next = env.worklist;
980 env.worklist = env.TOP;
981 irg_walk_graph(irg, NULL, create_initial_partitions, &env);
983 /* Place the START Node's partition on cprop.
984 Place the START Node on its local worklist. */
985 start_bl = get_irg_start_block(irg);
986 start = get_irn_node(start_bl);
987 add_node_to_cprop(start, &env);
989 /* set the initial exec to R */
990 initial_X = get_irg_initial_exec(irg);
991 get_irn_node(initial_X)->type = tarval_R;
993 while (env.cprop != NULL || env.worklist != NULL) {
995 if (env.worklist != NULL)
999 dump_all_partitions(&env);
1001 /* apply the result */
1002 irg_walk_graph(irg, NULL, apply_result, &env);
1004 pmap_destroy(env.type2id_map);
1005 del_set(env.opcode_map);
1006 del_set(env.opcode2id_map);
1007 obstack_free(&env.obst, NULL);
1009 /* restore value_of() default behavior */
1010 set_value_of_func(NULL);
1011 current_ir_graph = rem;