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. */
127 partition_t *dbg_next; /**< Link all partitions for debugging */
128 unsigned nr; /**< A unique number for (what-)mapping, >0. */
132 typedef struct environment_t {
133 struct obstack obst; /**< obstack to allocate data structures. */
134 partition_t *worklist; /**< The work list. */
135 partition_t *cprop; /**< The constant propagation list. */
136 partition_t *touched; /**< the touched set. */
137 partition_t *TOP; /**< The TOP partition. */
139 partition_t *dbg_list; /**< List of all partitions. */
141 set *opcode_map; /**< The initial opcode->partition map. */
142 set *opcode2id_map; /**< The opcodeMode->id map. */
143 pmap *type2id_map; /**< The type->id map. */
144 int end_idx; /**< -1 for local and 0 for global congruences. */
145 int lambda_input; /**< Captured argument for lambda_partition(). */
148 /** Type of the what function. */
149 typedef void *(*what_func)(const node_t *node, environment_t *env);
151 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
152 #define set_irn_node(irn, node) set_irn_link(irn, node)
154 /** The debug module handle. */
155 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
157 /** Next partition number. */
158 DEBUG_ONLY(static unsigned part_nr = 0);
162 * Dump partition to output.
164 static void dump_partition(const char *msg, partition_t *part) {
168 DB((dbg, LEVEL_2, "%s part%u (%u) {\n ", msg, part->nr, part->n_nodes));
169 list_for_each_entry(node_t, node, &part->entries, node_list) {
170 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
173 DB((dbg, LEVEL_2, "\n}\n"));
177 * Dump all partitions.
179 static void dump_all_partitions(environment_t *env) {
182 DB((dbg, LEVEL_2, "All partitions\n===============\n"));
183 for (P = env->dbg_list; P != NULL; P = P->dbg_next)
184 dump_partition("", P);
188 #define dump_partition(msg, part)
189 #define dump_all_partitions(env)
193 * compare two pointer values.
195 static int cmp_ptr(const void *elt, const void *key, size_t size) {
196 const listmap_entry_t *e1 = elt;
197 const listmap_entry_t *e2 = key;
199 return e1->id != e2->id;
203 * Creates a new listmap.
205 static void new_listmap(listmap_t *map) {
206 map->map = new_set(cmp_ptr, 16);
213 static void del_listmap(listmap_t *map) {
218 * Return the associated listmap entry for a given id.
220 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
221 listmap_entry_t key, *entry;
226 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
228 if (entry->list == NULL) {
229 /* a new entry, put into the list */
230 entry->next = map->values;
237 * calculate the hash value for an opcode map entry.
239 static unsigned opcode_hash(const opcode_key_t *entry) {
240 return (entry->mode - (ir_mode *)0) * 9 + entry->code;
244 * Compare two entries in the opcode map.
246 static int cmp_opcode(const void *elt, const void *key, size_t size) {
247 const opcode_key_t *o1 = elt;
248 const opcode_key_t *o2 = key;
250 return o1->code != o2->code || o1->mode != o2->mode;
253 /** Return the type of a node. */
254 static INLINE tarval *get_node_type(const ir_node *irn) {
255 return get_irn_node(irn)->type;
259 * Create a new empty partition.
261 static INLINE partition_t *new_partition(environment_t *env) {
262 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
264 INIT_LIST_HEAD(&part->entries);
266 part->wl_next = env->worklist;
267 part->touched_next = NULL;
268 part->cprop_next = NULL;
269 part->touched = NULL;
273 part->on_worklist = 0;
274 part->on_touched = 0;
276 part->dbg_next = env->dbg_list;
277 env->dbg_list = part;
278 part->nr = part_nr++;
285 * Get the partition for a given opcode.
287 static INLINE partition_t *get_partition_for_irn(const ir_node *irn, environment_t *env) {
288 opcode_entry_t key, *entry;
291 key.key.code = get_irn_opcode(irn);
292 key.key.mode = get_irn_mode(irn);
293 hash = opcode_hash(&key.key);
295 entry = set_find(env->opcode_map, &key, sizeof(key), hash);
297 /* create a new partition and place it on the wait queue */
298 partition_t *part = new_partition(env);
300 part->on_worklist = 1;
301 env->worklist = part;
304 set_insert(env->opcode_map, &key, sizeof(key), hash);
311 * Creates a partition node for the given IR-node and place it
312 * into the given partition.
314 static void create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
315 /* create a partition node and place it in the partition */
316 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
318 INIT_LIST_HEAD(&node->node_list);
321 node->cprop_next = NULL;
323 node->type = tarval_top; /* == tarval_U */
324 node->on_touched = 0;
326 set_irn_node(irn, node);
328 list_add_tail(&node->node_list, &part->entries);
331 DB((dbg, LEVEL_2, "Placing %+F in partition %u\n", irn, part->nr));
335 * Walker, initialize all Nodes' type to U or top and place
336 * all nodes into the TOP partition.
338 static void create_initial_partitions(ir_node *irn, void *ctx) {
339 environment_t *env = ctx;
340 partition_t *part = env->TOP;
343 create_partition_node(irn, part, env);
344 arity = get_irn_arity(irn);
345 if (arity > part->n_inputs)
346 part->n_inputs = arity;
350 * Add a partition to the touched set if not already there.
352 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
353 if (part->on_touched == 0) {
354 part->touched_next = env->touched;
356 part->on_touched = 1;
361 * Add a node to the entry.partition.touched set if not already there..
363 static INLINE void add_to_partition_touched(node_t *y) {
364 if (y->on_touched == 0) {
365 partition_t *part = y->part;
367 y->next = part->touched;
375 * update the worklist
377 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
378 /* If Z is on worklist then add Z' to worklist.
379 Else add the smaller of Z and Z' to worklist. */
380 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
381 Z_prime->on_worklist = 1;
382 Z_prime->wl_next = env->worklist;
383 env->worklist = Z_prime;
386 Z->wl_next = env->worklist;
392 * Split a partition by a local list.
394 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
395 partition_t *Z_prime;
400 dump_partition("Splitting ", Z);
402 /* Remove g from Z. */
403 for (node = g; node != NULL; node = node->next) {
404 list_del(&node->node_list);
409 /* Move g to a new partition, Z
\92. */
410 Z_prime = new_partition(env);
412 for (node = g; node != NULL; node = node->next) {
413 int arity = get_irn_arity(node->node);
414 list_add(&node->node_list, &Z_prime->entries);
415 node->part = Z_prime;
416 if (arity > n_inputs)
419 Z_prime->n_inputs = n_inputs;
420 Z_prime->n_nodes = n;
422 update_worklist(Z, Z_prime, env);
424 dump_partition("Now ", Z);
425 dump_partition("Created new ", Z_prime);
430 * Returns non-zero if the i'th input of a Phi node is live.
432 static int is_live_input(ir_node *phi, int i) {
433 ir_node *block = get_nodes_block(phi);
434 ir_node *pred = get_Block_cfgpred(block, i);
435 tarval *type = get_node_type(pred);
437 return type != tarval_U;
441 * Split the partitions if caused by the first entry on the worklist.
443 static void cause_splits(environment_t *env) {
444 partition_t *X, *Y, *Z;
448 /* remove the first partition from the worklist */
450 env->worklist = X->wl_next;
453 dump_partition("Cause_split: ", X);
454 end_idx = env->end_idx;
455 for (i = X->n_inputs - 1; i >= -1; --i) {
456 /* empty the touched set: already done, just clear the list */
459 list_for_each_entry(node_t, x, &X->entries, node_list) {
460 /* ignore the "control input" for non-pinned nodes
461 if we are running in GCSE mode */
462 if (i < end_idx && get_irn_pinned(x->node) != op_pin_state_pinned)
465 /* non-existing input */
466 if (i >= get_irn_arity(x->node))
469 y = get_irn_node(get_irn_n(x->node, i));
471 if (Y != env->TOP && (! is_Phi(x->node) || is_live_input(x->node, i))) {
472 add_to_touched(Y, env);
473 add_to_partition_touched(y);
477 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
478 /* remove it from the touched set */
481 if (Z->n_nodes != Z->n_touched) {
482 split(Z, Z->touched, env);
484 /* Empty local Z.touched. */
485 for (e = Z->touched; e != NULL; e = e->next) {
495 * Implements split_by_what(): Split a partition by characteristics given
496 * by the what function.
498 * @return list of partitions
500 static partition_t **split_by_what(partition_t *X, what_func What,
501 partition_t**P, environment_t *env) {
504 listmap_entry_t *iter;
507 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
509 list_for_each_entry(node_t, x, &X->entries, node_list) {
510 void *id = What(x, env);
511 listmap_entry_t *entry;
514 /* input not allowed, ignore */
517 /* Add x to map[What(x)]. */
518 entry = listmap_find(&map, id);
519 x->next = entry->list;
522 /* Let P be a set of Partitions. */
524 /* for all sets S except one in the range of map do */
525 for (iter = map.values; iter != NULL; iter = iter->next) {
526 if (iter->next == NULL) {
527 /* this is the last entry, ignore */
532 /* Add SPLIT( X, S ) to P. */
533 R = split(X, S, env);
535 ARR_APP1(partition_t *, P, R);
540 ARR_APP1(partition_t *, P, X);
547 /** lambda n.(n.type) */
548 static void *lambda_type(const node_t *node, environment_t *env) {
553 /** lambda n.(n.opcode) */
554 static void *lambda_opcode(const node_t *node, environment_t *env) {
555 opcode_key_t key, *entry;
557 key.code = get_irn_opcode(node->node);
558 key.mode = get_irn_mode(node->node);
559 entry = set_insert(env->opcode2id_map, &key, sizeof(&key), opcode_hash(&key));
563 /** lambda n.(n[i].partition) */
564 static void *lambda_partition(const node_t *node, environment_t *env) {
567 int i = env->lambda_input;
569 if (i >= get_irn_arity(node->node)) {
570 /* we are outside the allowed range */
574 /* ignore the "control input" for non-pinned nodes
575 if we are running in GCSE mode */
576 if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
579 pred = get_irn_n(node->node, i);
580 p = get_irn_node(pred);
586 * Implements split_by().
588 static void split_by(partition_t *X, environment_t *env) {
589 partition_t **P = NEW_ARR_F(partition_t *, 0);
592 P = split_by_what(X, lambda_type, P, env);
593 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
594 partition_t *Y = P[i];
597 partition_t **Q = NEW_ARR_F(partition_t *, 0);
599 Q = split_by_what(Y, lambda_opcode, Q, env);
601 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
602 partition_t *Z = Q[j];
604 for (k = Z->n_inputs - 1; k >= -1; --k) {
605 env->lambda_input = k;
606 split_by_what(Z, lambda_partition, NULL, env);
616 * (Re-)compute the type for a given node.
618 static void default_compute(node_t *node) {
620 ir_node *irn = node->node;
623 if (get_irn_pinned(irn) == op_pin_state_pinned) {
624 node_t *block = get_irn_node(get_nodes_block(irn));
626 if (block->type == tarval_U) {
627 node->type = tarval_top;
631 mode = get_irn_mode(irn);
632 if (mode == mode_M) {
633 /* mode M is always bottom for now */
634 node->type = tarval_bottom;
637 if (! mode_is_data(mode))
640 /* if any of the data inputs have type top, the result is type top */
641 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
642 ir_node *pred = get_irn_n(irn, i);
643 node_t *p = get_irn_node(pred);
645 if (p->type == tarval_top) {
646 node->type = tarval_top;
650 node->type = computed_value(irn);
654 * (Re-)compute the type for a Block node.
656 static void compute_Block(node_t *node) {
658 ir_node *block = node->node;
660 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
661 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
663 if (pred->type == tarval_R) {
664 /* A block is reachable, if at least of predecessor is reachable. */
665 node->type = tarval_R;
669 node->type = tarval_U;
673 * (Re-)compute the type for a Jmp node.
675 static void compute_Jmp(node_t *node) {
676 node_t *block = get_irn_node(get_nodes_block(node->node));
678 node->type = block->type;
682 * (Re-)compute the type for a Phi node.
684 static void compute_Phi(node_t *node) {
686 ir_node *phi = node->node;
687 tarval *type = tarval_top;
689 /* if a Phi is in a unreachable block, its type is TOP */
690 node_t *block = get_irn_node(get_nodes_block(phi));
692 if (block->type == tarval_U) {
693 node->type = tarval_top;
697 /* if any of the data inputs have type top, the result is type top */
698 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
699 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
701 if (pred->type == tarval_top) {
702 /* ignore TOP inputs */
705 if (pred->type == tarval_bottom) {
706 node->type = tarval_bottom;
708 } else if (type == tarval_top) {
709 /* first constant found */
711 } else if (type == pred->type) {
712 /* same constant, continue */
715 /* different constants or tarval_bottom */
716 node->type = tarval_bottom;
724 * (Re-)compute the type for a Sub. Special case: both nodes are congruent.
726 static void compute_Sub(node_t *node) {
727 ir_node *sub = node->node;
728 node_t *l = get_irn_node(get_Sub_left(sub));
729 node_t *r = get_irn_node(get_Sub_right(sub));
733 if (a == tarval_top || b == tarval_top) {
734 node->type = tarval_top;
735 } else if (r->part == l->part) {
736 ir_mode *mode = get_irn_mode(sub);
737 node->type = get_mode_null(mode);
738 } else if (a == tarval_bottom || b == tarval_bottom) {
739 node->type = tarval_bottom;
741 node->type = tarval_sub(a, b);
746 * (Re-)compute the type for a Proj(Cmp).
748 static void compute_Proj_Cmp(node_t *node, ir_node *cmp) {
749 ir_node *proj = node->node;
750 node_t *l = get_irn_node(get_Cmp_left(cmp));
751 node_t *r = get_irn_node(get_Cmp_right(cmp));
754 pn_Cmp pnc = get_Proj_proj(proj);
757 * BEWARE: a == a is NOT always True for floating Point values, as
758 * NaN != NaN is defined, so we must check this here.
760 if (!mode_is_float(get_irn_mode(l->node)) || pnc == pn_Cmp_Lt || pnc == pn_Cmp_Gt) {
761 if (a == tarval_top || b == tarval_top) {
762 node->type = tarval_top;
763 } else if (r->part == l->part) {
764 node->type = new_tarval_from_long(pnc & pn_Cmp_Eq, mode_b);
765 } else if (a == tarval_bottom || b == tarval_bottom) {
766 node->type = tarval_bottom;
768 default_compute(node);
771 default_compute(node);
776 * (Re-)compute the type for a Proj-Nodes.
778 static void compute_Proj(node_t *node) {
779 ir_node *proj = node->node;
780 ir_mode *mode = get_irn_mode(proj);
783 if (mode == mode_M) {
784 /* mode M is always bottom */
785 node->type = tarval_bottom;
788 if (mode != mode_X) {
789 ir_node *cmp = get_Proj_pred(proj);
791 compute_Proj_Cmp(node, cmp);
793 default_compute(node);
796 /* handle mode_X nodes */
797 pred = get_Proj_pred(proj);
799 switch (get_irn_opcode(pred)) {
801 /* the Proj_X from the Start is always reachable */
802 node->type = tarval_R;
805 default_compute(node);
810 * (Re-)compute the type for a given node.
812 static void compute(node_t *node) {
813 compute_func func = (compute_func)node->node->op->ops.generic;
819 * Propagate constant evaluation.
821 static void propagate(environment_t *env) {
825 node_t *fallen = NULL;
826 unsigned n_fallen = 0;
829 while (env->cprop != NULL) {
830 /* remove a partition X from cprop */
832 env->cprop = X->cprop_next;
834 while (X->cprop != NULL) {
835 /* remove a Node x from X.cprop */
838 X->cprop = x->cprop_next;
840 /* compute a new type for x */
843 if (x->type != old_type) {
844 DB((dbg, LEVEL_2, "node %+F has changed type from %T to %T\n", x->node, old_type, x->type));
845 /* Add x to fallen. */
850 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
851 ir_node *succ = get_irn_out(x->node, i);
852 node_t *y = get_irn_node(succ);
854 /* Add y to y.partition.cprop. */
855 if (y->on_cprop == 0) {
856 y->cprop_next = y->part->cprop;
863 if (n_fallen != X->n_nodes) {
864 Y = split(X, fallen, env);
873 * Get the leader for a given node from its congruence class.
875 * @param irn the node
877 static ir_node *get_leader(ir_node *irn) {
878 partition_t *part = get_irn_node(irn)->part;
880 if (part->n_nodes > 1) {
881 DB((dbg, LEVEL_2, "Found congruence class for %+F ", irn));
882 dump_partition("", part);
888 * Post-Walker, apply the analysis results;
890 static void apply_result(ir_node *irn, void *ctx) {
891 environment_t *env = ctx;
893 if (is_no_Block(irn)) {
894 ir_node *leader = get_leader(irn);
897 exchange(irn, leader);
902 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
905 * sets the generic functions to compute.
907 static void set_compute_functions(void) {
910 /* set the default compute function */
911 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
912 ir_op *op = get_irp_opcode(i);
913 op->ops.generic = (op_func)default_compute;
916 /* set specific functions */
923 void combo(ir_graph *irg) {
925 ir_node *start_bl, *initial_X;
927 ir_graph *rem = current_ir_graph;
929 current_ir_graph = irg;
931 /* register a debug mask */
932 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
933 firm_dbg_set_mask(dbg, SET_LEVEL_2);
935 obstack_init(&env.obst);
943 env.opcode_map = new_set(cmp_opcode, iro_Last * 4);
944 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
945 env.type2id_map = pmap_create();
946 env.end_idx = get_opt_global_cse() ? 0 : -1;
947 env.lambda_input = 0;
949 assure_irg_outs(irg);
951 /* we have our own value_of function */
952 set_value_of_func(get_node_type);
954 set_compute_functions();
956 /* create the initial TOP partition and place it on the work list */
957 env.TOP = new_partition(&env);
958 env.TOP->wl_next = env.worklist;
959 env.worklist = env.TOP;
960 irg_walk_graph(irg, NULL, create_initial_partitions, &env);
962 /* Place the START Node's partition on cprop.
963 Place the START Node on its local worklist. */
964 start_bl = get_irg_start_block(irg);
965 start = get_irn_node(start_bl);
966 start->part->cprop_next = env.cprop;
967 env.cprop = start->part;
969 start->cprop_next = start->part->cprop;
970 start->part->cprop = start;
972 /* set the initial exec to R */
973 initial_X = get_irg_initial_exec(irg);
974 get_irn_node(initial_X)->type = tarval_R;
976 while (env.cprop != NULL && env.worklist != NULL) {
978 if (env.worklist != NULL)
982 dump_all_partitions(&env);
984 /* apply the result */
985 irg_walk_graph(irg, NULL, apply_result, &env);
987 pmap_destroy(env.type2id_map);
988 del_set(env.opcode_map);
989 del_set(env.opcode2id_map);
990 obstack_free(&env.obst, NULL);
992 /* restore value_of() default behavior */
993 set_value_of_func(NULL);
994 current_ir_graph = rem;