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 opcode2id_entry_t opcode2id_entry_t;
61 typedef struct listmap_entry_t listmap_entry_t;
63 /** The type of the compute function. */
64 typedef void (*compute_func)(node_t *node);
70 ir_opcode code; /**< The Firm opcode. */
71 ir_mode *mode; /**< The mode of all nodes in the partition. */
75 * An entry in the opcode map.
77 struct opcode_entry_t {
78 opcode_key_t key; /**< The key. */
79 partition_t *part; /**< The associated partition. */
83 * An entry in the opcode map2id.
85 struct opcode2id_entry_t {
86 opcode_key_t key; /**< The key. */
87 unsigned id; /**< The associated id. */
91 * An entry in the list_map.
93 struct listmap_entry_t {
94 unsigned id; /**< The id. */
95 node_t *list; /**< The associated list for this id. */
96 listmap_entry_t *next; /**< Link to the next entry in the map. */
99 /** We must map id's to lists. */
100 typedef struct listmap_t {
101 set *map; /**< Map id's to listmap_entry_t's */
102 listmap_entry_t *values; /**< List of all values in the map. */
110 ir_node *node; /**< The IR-node itself. */
111 list_head node_list; /**< Double-linked list of entries. */
112 partition_t *part; /**< points to the partition this node belongs to */
113 node_t *cprop_next; /**< Next node on partition.cprop list. */
114 node_t *next; /**< Next node on local list (partition.touched, fallen). */
115 tarval *type; /**< The associated lattice element "type". */
116 unsigned on_touched:1; /**< Set, if this node is on the partition.touched set. */
117 unsigned on_cprop:1; /**< Set, if this node is on the partition.cprop list. */
121 * A partition containing congruent nodes.
124 list_head entries; /**< The head of partition node list. */
125 node_t *cprop; /**< The partition.cprop list. */
126 partition_t *wl_next; /**< Next entry in the work list if any. */
127 partition_t *touched_next; /**< Points to the next partition in the touched set. */
128 partition_t *cprop_next; /**< Points to the next partition in the cprop list. */
129 node_t *touched; /**< The partition.touched set of this partition. */
130 unsigned n_nodes; /**< Number of entries in this partition. */
131 unsigned n_touched; /**< Number of entries in the partition.touched. */
132 int n_inputs; /**< Maximum number of inputs of all entries. */
133 unsigned on_worklist:1; /**< Set, if this partition is in the work list. */
134 unsigned on_touched:1; /**< Set, if this partition is on the touched set. */
135 unsigned nr; /**< A unique number for (what-)mapping, >0. */
138 typedef struct environment_t {
139 struct obstack obst; /**< obstack to allocate data structures. */
140 partition_t *worklist; /**< The work list. */
141 partition_t *cprop; /**< The constant propagation list. */
142 partition_t *touched; /**< the touched set. */
143 partition_t *TOP; /**< The TOP partition. */
144 set *opcode_map; /**< The initial opcode->partition map. */
145 set *opcode2id_map; /**< The opcodeMode->id map. */
146 pmap *type2id_map; /**< The type->id map. */
147 int end_idx; /**< -1 for local and 0 for global congruences. */
148 int lambda_input; /**< Captured argument for lambda_partition(). */
149 int next_opcode_id; /**< Next ID for the opcode2id map. */
150 int next_type_id; /**< Next ID for the type2id map. */
153 /** Type of the what function. */
154 typedef unsigned (*what_func)(const node_t *node, environment_t *env);
156 #define get_irn_node(irn) ((node_t *)get_irn_link(irn))
157 #define set_irn_node(irn, node) set_irn_link(irn, node)
159 /** The debug module handle. */
160 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
162 /** Next partition number, 0 has special meaning. */
163 static unsigned part_nr = 1;
167 * Dump partition to output.
169 static void dump_partition(const char *msg, partition_t *part) {
173 DB((dbg, LEVEL_2, "%s part%u (%u) {\n ", msg, part->nr, part->n_nodes));
174 list_for_each_entry(node_t, node, &part->entries, node_list) {
175 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", node->node));
178 DB((dbg, LEVEL_2, "}\n"));
181 #define dump_partition(msg, part)
185 * compare two pointer values.
187 static int cmp_ptr(const void *elt, const void *key, size_t size) {
188 const listmap_entry_t *e1 = elt;
189 const listmap_entry_t *e2 = key;
191 return e1->id != e2->id;
195 * Creates a new listmap.
197 static void new_listmap(listmap_t *map) {
198 map->map = new_set(cmp_ptr, 16);
205 static void del_listmap(listmap_t *map) {
210 * Return the associated listmap entry for a given id.
212 static listmap_entry_t *listmap_find(listmap_t *map, unsigned id) {
213 listmap_entry_t key, *entry;
218 entry = set_insert(map->map, &key, sizeof(key), id);
220 if (entry->list == NULL) {
221 /* a new entry, put into the list */
222 entry->next = map->values;
229 * calculate the hash value for an opcode map entry.
231 static unsigned opcode_hash(const opcode_key_t *entry) {
232 return (entry->mode - (ir_mode *)0) * 9 + entry->code;
236 * Compare two entries in the opcode map.
238 static int cmp_opcode(const void *elt, const void *key, size_t size) {
239 const opcode_key_t *o1 = elt;
240 const opcode_key_t *o2 = key;
242 return o1->code != o2->code || o1->mode != o2->mode;
245 /** Return the type of a node. */
246 static INLINE tarval *get_node_type(const ir_node *irn) {
247 return get_irn_node(irn)->type;
251 * Create a new empty partition.
253 static INLINE partition_t *new_partition(environment_t *env) {
254 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
256 INIT_LIST_HEAD(&part->entries);
258 part->wl_next = env->worklist;
259 part->touched_next = NULL;
260 part->cprop_next = NULL;
261 part->touched = NULL;
265 part->on_worklist = 0;
266 part->on_touched = 0;
267 part->nr = part_nr++;
273 * Get the partition for a given opcode.
275 static INLINE partition_t *get_partition_for_irn(const ir_node *irn, environment_t *env) {
276 opcode_entry_t key, *entry;
279 key.key.code = get_irn_opcode(irn);
280 key.key.mode = get_irn_mode(irn);
281 hash = opcode_hash(&key.key);
283 entry = set_find(env->opcode_map, &key, sizeof(key), hash);
285 /* create a new partition and place it on the wait queue */
286 partition_t *part = new_partition(env);
288 part->on_worklist = 1;
289 env->worklist = part;
292 set_insert(env->opcode_map, &key, sizeof(key), hash);
299 * Creates a partition node for the given IR-node and place it
300 * into the given partition.
302 static void create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
303 /* create a partition node and place it in the partition */
304 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
306 INIT_LIST_HEAD(&node->node_list);
309 node->cprop_next = NULL;
311 node->type = tarval_top; /* == tarval_U */
312 node->on_touched = 0;
314 set_irn_node(irn, node);
316 list_add_tail(&node->node_list, &part->entries);
319 DB((dbg, LEVEL_2, "Placing %+F in partition %u\n", irn, part->nr));
323 * Walker, initialize all Nodes' type to U or top and place
324 * all nodes into the TOP partition.
326 static void create_initial_partitions(ir_node *irn, void *ctx) {
327 environment_t *env = ctx;
328 partition_t *part = env->TOP;
331 create_partition_node(irn, part, env);
332 arity = get_irn_arity(irn);
333 if (arity > part->n_inputs)
334 part->n_inputs = arity;
338 * Add a partition to the touched set if not already there.
340 static INLINE void add_to_touched(partition_t *part, environment_t *env) {
341 if (part->on_touched == 0) {
342 part->touched_next = env->touched;
344 part->on_touched = 1;
349 * Add a node to the entry.partition.touched set if not already there..
351 static INLINE void add_to_partition_touched(node_t *y) {
352 if (y->on_touched == 0) {
353 partition_t *part = y->part;
355 y->next = part->touched;
363 * update the worklist
365 static void update_worklist(partition_t *Z, partition_t *Z_prime, environment_t *env) {
366 /* If Z is on worklist then add Z' to worklist.
367 Else add the smaller of Z and Z' to worklist. */
368 if (Z->on_worklist || Z_prime->n_nodes < Z->n_nodes) {
369 Z_prime->on_worklist = 1;
370 Z_prime->wl_next = env->worklist;
371 env->worklist = Z_prime;
374 Z->wl_next = env->worklist;
380 * Split a partition by a local list.
382 static partition_t *split(partition_t *Z, node_t *g, environment_t *env) {
383 partition_t *Z_prime;
388 dump_partition("Splitting ", Z);
390 /* Remove g from Z. */
391 for (node = g; node != NULL; node = node->next) {
392 list_del(&node->node_list);
397 /* Move g to a new partition, Z
\92. */
398 Z_prime = new_partition(env);
400 for (node = g; node != NULL; node = node->next) {
401 int arity = get_irn_arity(node->node);
402 list_add(&node->node_list, &Z_prime->entries);
403 node->part = Z_prime;
404 if (arity > n_inputs)
407 Z_prime->n_inputs = n_inputs;
408 Z_prime->n_nodes = n;
410 update_worklist(Z, Z_prime, env);
412 dump_partition("Now ", Z);
413 dump_partition("Created new ", Z_prime);
418 * Returns non-zero if the i'th input of a Phi node is live.
420 static int is_live_input(ir_node *phi, int i) {
421 ir_node *block = get_nodes_block(phi);
422 ir_node *pred = get_Block_cfgpred(block, i);
423 tarval *type = get_node_type(pred);
425 return type != tarval_U;
429 * Split the partitions if caused by the first entry on the worklist.
431 static void cause_splits(environment_t *env) {
432 partition_t *X, *Y, *Z;
436 /* remove the first partition from the worklist */
438 env->worklist = X->wl_next;
441 dump_partition("Cause_split: ", X);
442 end_idx = env->end_idx;
443 for (i = X->n_inputs - 1; i >= -1; --i) {
444 /* empty the touched set: already done, just clear the list */
447 list_for_each_entry(node_t, x, &X->entries, node_list) {
448 /* ignore the "control input" for non-pinned nodes
449 if we are running in GCSE mode */
450 if (i < end_idx && get_irn_pinned(x->node) != op_pin_state_pinned)
453 /* non-existing input */
454 if (i >= get_irn_arity(x->node))
457 y = get_irn_node(get_irn_n(x->node, i));
459 if (Y != env->TOP && (! is_Phi(x->node) || is_live_input(x->node, i))) {
460 add_to_touched(Y, env);
461 add_to_partition_touched(y);
465 for (Z = env->touched; Z != NULL; Z = Z->touched_next) {
466 /* remove it from the touched set */
469 if (Z->n_nodes != Z->n_touched) {
470 split(Z, Z->touched, env);
472 /* Empty local Z.touched. */
473 for (e = Z->touched; e != NULL; e = e->next) {
483 * Implements split_by_what(): Split a partition by characteristics given
484 * by the what function.
486 * @return list of partitions
488 static partition_t **split_by_what(partition_t *X, what_func What,
489 partition_t**P, environment_t *env) {
492 listmap_entry_t *iter;
495 /* Let map be an empty mapping from the range of What to (local) list of Nodes. */
497 list_for_each_entry(node_t, x, &X->entries, node_list) {
498 unsigned id = What(x, env);
499 listmap_entry_t *entry;
502 /* input not allowed, ignore */
505 /* Add x to map[What(x)]. */
506 entry = listmap_find(&map, id);
507 x->next = entry->list;
510 /* Let P be a set of Partitions. */
512 /* for all sets S except one in the range of map do */
513 for (iter = map.values; iter != NULL; iter = iter->next) {
514 if (iter->next == NULL) {
515 /* this is the last entry, ignore */
520 /* Add SPLIT( X, S ) to P. */
521 R = split(X, S, env);
523 ARR_APP1(partition_t *, P, R);
528 ARR_APP1(partition_t *, P, X);
535 /** lambda n.(n.type) */
536 static unsigned lambda_type(const node_t *node, environment_t *env) {
538 /* ensure that it is NOT null */
539 return (((unsigned)((char *)node->type - (char *)0)) << 1) | 1;
542 /** lambda n.(n.opcode) */
543 static unsigned lambda_opcode(const node_t *node, environment_t *env) {
544 opcode2id_entry_t key, *entry;
546 key.key.code = get_irn_opcode(node->node);
547 key.key.mode = get_irn_mode(node->node);
549 entry = set_insert(env->opcode2id_map, &key, sizeof(&key), opcode_hash(&key.key));
551 entry->id = ++env->next_opcode_id;
555 /** lambda n.(n[i].partition) */
556 static unsigned lambda_partition(const node_t *node, environment_t *env) {
559 int i = env->lambda_input;
561 if (i >= get_irn_arity(node->node)) {
562 /* we are outside the allowed range */
566 /* ignore the "control input" for non-pinned nodes
567 if we are running in GCSE mode */
568 if (i < env->end_idx && get_irn_pinned(node->node) != op_pin_state_pinned)
571 pred = get_irn_n(node->node, i);
572 p = get_irn_node(pred);
578 * Implements split_by().
580 static void split_by(partition_t *X, environment_t *env) {
581 partition_t **P = NEW_ARR_F(partition_t *, 0);
584 P = split_by_what(X, lambda_type, P, env);
585 for (i = ARR_LEN(P) - 1; i >= 0; --i) {
586 partition_t *Y = P[i];
589 partition_t **Q = NEW_ARR_F(partition_t *, 0);
591 Q = split_by_what(Y, lambda_opcode, Q, env);
593 for (j = ARR_LEN(Q) - 1; j >= 0; --j) {
594 partition_t *Z = Q[i];
596 for (k = Z->n_inputs - 1; k >= -1; --k) {
597 env->lambda_input = k;
598 split_by_what(Z, lambda_partition, NULL, env);
608 * (Re-)compute the type for a given node.
610 static void default_compute(node_t *node) {
612 ir_node *irn = node->node;
615 if (get_irn_pinned(irn) == op_pin_state_pinned) {
616 node_t *block = get_irn_node(get_nodes_block(irn));
618 if (block->type == tarval_U) {
619 node->type = tarval_top;
623 mode = get_irn_mode(irn);
624 if (mode == mode_M) {
625 /* mode M is always bottom for now */
626 node->type = tarval_bottom;
629 if (! mode_is_data(mode))
632 /* if any of the data inputs have type top, the result is type top */
633 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
634 ir_node *pred = get_irn_n(irn, i);
635 node_t *p = get_irn_node(pred);
637 if (p->type == tarval_top) {
638 node->type = tarval_top;
642 node->type = computed_value(irn);
646 * (Re-)compute the type for a Block node.
648 static void compute_Block(node_t *node) {
650 ir_node *block = node->node;
652 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
653 node_t *pred = get_irn_node(get_Block_cfgpred(block, i));
655 if (pred->type == tarval_R) {
656 /* A block is reachable, if at least of predecessor is reachable. */
657 node->type = tarval_R;
661 node->type = tarval_U;
665 * (Re-)compute the type for a Jmp node.
667 static void compute_Jmp(node_t *node) {
668 node_t *block = get_irn_node(get_nodes_block(node->node));
670 node->type = block->type;
674 * (Re-)compute the type for a Phi node.
676 static void compute_Phi(node_t *node) {
678 ir_node *phi = node->node;
679 tarval *type = tarval_top;
681 /* if a Phi is in a unreachable block, its type is TOP */
682 node_t *block = get_irn_node(get_nodes_block(phi));
684 if (block->type == tarval_U) {
685 node->type = tarval_top;
689 /* if any of the data inputs have type top, the result is type top */
690 for (i = get_Phi_n_preds(phi) - 1; i >= 0; --i) {
691 node_t *pred = get_irn_node(get_Phi_pred(phi, i));
693 if (pred->type == tarval_top) {
694 /* ignore TOP inputs */
697 if (pred->type == tarval_bottom) {
698 node->type = tarval_bottom;
700 } else if (type == tarval_top) {
701 /* first constant found */
703 } else if (type == pred->type) {
704 /* same constant, continue */
707 /* different constants or tarval_bottom */
708 node->type = tarval_bottom;
716 * (Re-)compute the type for a Proj-Nodes.
718 static void compute_Proj(node_t *node) {
719 ir_node *proj = node->node;
720 ir_mode *mode = get_irn_mode(proj);
723 if (mode == mode_M) {
724 /* mode M is always bottom */
725 node->type = tarval_bottom;
728 if (mode != mode_X) {
729 default_compute(node);
732 /* handle mode_X nodes */
733 pred = get_Proj_pred(proj);
735 switch (get_irn_opcode(pred)) {
737 /* the Proj_X from the Start is always reachable */
738 node->type = tarval_R;
741 default_compute(node);
746 * (Re-)compute the type for a given node.
748 static void compute(node_t *node) {
749 compute_func func = (compute_func)node->node->op->ops.generic;
755 * Propagate constant evaluation.
757 static void propagate(environment_t *env) {
761 node_t *fallen = NULL;
762 unsigned n_fallen = 0;
765 while (env->cprop != NULL) {
766 /* remove a partition X from cprop */
768 env->cprop = X->cprop_next;
770 while (X->cprop != NULL) {
771 /* remove a Node x from X.cprop */
774 X->cprop = x->cprop_next;
776 /* compute a new type for x */
779 if (x->type != old_type) {
780 DB((dbg, LEVEL_2, "node %+F has changed type from %T to %T\n", x->node, old_type, x->type));
781 /* Add x to fallen. */
786 for (i = get_irn_n_outs(x->node) - 1; i >= 0; --i) {
787 ir_node *succ = get_irn_out(x->node, i);
788 node_t *y = get_irn_node(succ);
790 /* Add y to y.partition.cprop. */
791 if (y->on_cprop == 0) {
792 y->cprop_next = y->part->cprop;
799 if (n_fallen != X->n_nodes) {
800 Y = split(X, fallen, env);
809 * Get the leader for a given node from its congruence class.
811 * @param irn the node
813 static ir_node *get_leader(ir_node *irn) {
814 partition_t *part = get_irn_node(irn)->part;
816 if (part->n_nodes > 1) {
817 DB((dbg, LEVEL_2, "Found congruence class for %+F ", irn));
818 dump_partition("", part);
824 * Post-Walker, apply the analysis results;
826 static void apply_result(ir_node *irn, void *ctx) {
827 environment_t *env = ctx;
829 if (is_no_Block(irn)) {
830 ir_node *leader = get_leader(irn);
833 exchange(irn, leader);
838 #define SET(code) op_##code->ops.generic = (op_func)compute_##code
841 * sets the generic functions to compute.
843 static void set_compute_functions(void) {
846 /* set the default compute function */
847 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
848 ir_op *op = get_irp_opcode(i);
849 op->ops.generic = (op_func)default_compute;
852 /* set specific functions */
859 void combo(ir_graph *irg) {
861 ir_node *start_bl, *initial_X;
863 ir_graph *rem = current_ir_graph;
865 current_ir_graph = irg;
867 /* register a debug mask */
868 FIRM_DBG_REGISTER(dbg, "firm.opt.combo");
869 firm_dbg_set_mask(dbg, SET_LEVEL_2);
871 obstack_init(&env.obst);
876 env.opcode_map = new_set(cmp_opcode, iro_Last * 4);
877 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
878 env.type2id_map = pmap_create();
879 env.end_idx = get_opt_global_cse() ? 0 : -1;
880 env.lambda_input = 0;
881 env.next_opcode_id = 0;
883 assure_irg_outs(irg);
885 /* we have our own value_of function */
886 set_value_of_func(get_node_type);
888 set_compute_functions();
890 /* create the initial TOP partition and place it on the work list */
891 env.TOP = new_partition(&env);
892 env.TOP->wl_next = env.worklist;
893 env.worklist = env.TOP;
895 irg_walk_graph(irg, NULL, create_initial_partitions, &env);
897 /* Place the START Node's partition on cprop.
898 Place the START Node on its local worklist. */
899 start_bl = get_irg_start_block(irg);
900 start = get_irn_node(start_bl);
901 start->part->cprop_next = env.cprop;
902 env.cprop = start->part;
904 start->cprop_next = start->part->cprop;
905 start->part->cprop = start;
907 /* set the initial exec to R */
908 initial_X = get_irg_initial_exec(irg);
909 get_irn_node(initial_X)->type = tarval_R;
911 while (env.cprop != NULL && env.worklist != NULL) {
913 if (env.worklist != NULL)
917 /* apply the result */
918 irg_walk_graph(irg, NULL, apply_result, &env);
920 pmap_destroy(env.type2id_map);
921 del_set(env.opcode_map);
922 del_set(env.opcode2id_map);
923 obstack_free(&env.obst, NULL);
925 /* restore value_of() default behavior */
926 set_value_of_func(NULL);
927 current_ir_graph = rem;