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 Combining congruent blocks
23 * @author Michael Beck
26 * This phase find congruent blocks. Works currently for
27 * predecessors of the end block only.
28 * Two block are congruent, if they contains only equal calculations.
32 #include "irgraph_t.h"
38 typedef struct partition_t partition_t;
39 typedef struct block_t block_t;
40 typedef struct node_t node_t;
41 typedef struct pair_t pair_t;
42 typedef struct phi_t phi_t;
43 typedef struct opcode_key_t opcode_key_t;
44 typedef struct listmap_entry_t listmap_entry_t;
45 typedef struct environment_t environment_t;
47 /** An opcode map key. */
49 ir_opcode code; /**< The Firm opcode. */
50 ir_mode *mode; /**< The mode of all nodes in the partition. */
51 int arity; /**< The arity of this opcode (needed for Phi etc. */
53 long proj; /**< For Proj nodes, its proj number */
54 ir_entity *ent; /**< For Sel Nodes, its entity */
58 /** A partition contains all congruent blocks. */
60 list_head part_list; /**< Double linked list of partitions. */
61 list_head blocks; /**< List of blocks in this partition. */
62 unsigned n_blocks; /**< Number of block in this partition. */
64 unsigned nr; /**< For debugging: number of this partition. */
70 list_head block_list; /**< Double linked list of block inside a partition. */
71 list_head nodes; /**< Wait-queue of nodes that must be checked for congruence. */
72 block_t *next; /**< Next block of a split list. */
73 ir_node *block; /**< Pointer to the associated IR-node block. */
74 node_t *roots; /**< The list of all root nodes. */
75 pair_t *input_pairs; /**< The list of inputs to this block. */
76 phi_t *phis; /**< The list of Phis in this block. */
81 list_head node_list; /**< Double linked list of block inside a partition. */
82 ir_node *node; /**< Pointer to the associated IR-node or NULL for block inputs. */
83 node_t *next; /**< Link to the next node in the root set. */
84 char is_input; /**< Set if this node is an input from other block. */
87 /** The environment. */
88 struct environment_t {
89 list_head partitions; /**< list of partitions. */
90 list_head ready; /**< list of ready partitions. */
91 set *opcode2id_map; /**< The opcodeMode->id map. */
92 struct obstack obst; /** obstack for temporary data */
95 /** A node, input index pair. */
97 pair_t *next; /**< Points to the next pair entry. */
98 ir_node *irn; /**< The IR-node. */
99 int index; /**< An input index. */
100 ir_node **ins; /**< A new in array once allocated. */
103 /** A Phi, inputs pair. */
105 phi_t *next; /**< Points to the next Phi pair entry. */
106 ir_node *phi; /**< The Phi node. */
107 ir_node **ins; /**< A new in array once allocated. */
111 * An entry in the list_map.
113 struct listmap_entry_t {
114 void *id; /**< The id. */
115 block_t *list; /**< The associated list for this id. */
116 listmap_entry_t *next; /**< Link to the next entry in the map. */
119 /** We must map id's to lists. */
120 typedef struct listmap_t {
121 set *map; /**< Map id's to listmap_entry_t's */
122 listmap_entry_t *values; /**< List of all values in the map. */
125 /** The debug module handle. */
126 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
128 /** Next partition number. */
129 DEBUG_ONLY(static unsigned part_nr = 0);
133 * Dump partition to output.
135 static void dump_partition(const char *msg, const partition_t *part) {
136 const block_t *block;
139 DB((dbg, LEVEL_2, "%s part%u (%u) {\n ", msg, part->nr, part->n_blocks));
140 list_for_each_entry(block_t, block, &part->blocks, block_list) {
141 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", block->block));
144 DB((dbg, LEVEL_2, "\n}\n"));
145 } /* dump_partition */
150 static void dump_list(const char *msg, const block_t *block) {
154 DB((dbg, LEVEL_3, "%s = {\n ", msg));
155 for (p = block; p != NULL; p = p->next) {
156 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->block));
159 DB((dbg, LEVEL_3, "\n}\n"));
162 #define dump_partition(msg, part)
163 #define dump_list(msg, block)
167 * Compare two pointer values of a listmap.
169 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
170 const listmap_entry_t *e1 = elt;
171 const listmap_entry_t *e2 = key;
174 return e1->id != e2->id;
175 } /* listmap_cmp_ptr */
178 * Initializes a listmap.
180 * @param map the listmap
182 static void listmap_init(listmap_t *map) {
183 map->map = new_set(listmap_cmp_ptr, 16);
188 * Terminates a listmap.
190 * @param map the listmap
192 static void listmap_term(listmap_t *map) {
197 * Return the associated listmap entry for a given id.
199 * @param map the listmap
200 * @param id the id to search for
202 * @return the associated listmap entry for the given id
204 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
205 listmap_entry_t key, *entry;
210 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
212 if (entry->list == NULL) {
213 /* a new entry, put into the list */
214 entry->next = map->values;
221 * Calculate the hash value for an opcode map entry.
223 * @param entry an opcode map entry
225 * @return a hash value for the given opcode map entry
227 static unsigned opcode_hash(const opcode_key_t *entry) {
228 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent) + entry->arity;
232 * Compare two entries in the opcode map.
234 static int cmp_opcode(const void *elt, const void *key, size_t size) {
235 const opcode_key_t *o1 = elt;
236 const opcode_key_t *o2 = key;
239 return o1->code != o2->code || o1->mode != o2->mode ||
240 o1->arity != o2->arity ||
241 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
245 * Creates a new empty partition.
247 static partition_t *create_partition(environment_t *env) {
248 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
250 INIT_LIST_HEAD(&part->blocks);
252 DEBUG_ONLY(part->nr = part_nr++);
253 list_add_tail(&part->part_list, &env->partitions);
255 } /* create_partition */
258 * Allocate a new block.
260 * @param block the IR-node
261 * @param env the environment
263 static block_t *create_block(ir_node *block, environment_t *env) {
264 block_t *bl = obstack_alloc(&env->obst, sizeof(*bl));
266 INIT_LIST_HEAD(&bl->nodes);
270 bl->input_pairs = NULL;
276 * Adds a block to a partition.
278 * @param partition the partition to add to
279 * @param block the block to add
281 static void add_block(partition_t *partition, block_t *block) {
282 list_add_tail(&block->block_list, &partition->blocks);
283 ++partition->n_blocks;
287 * Allocate a new node.
289 * @param irn the IR-node
290 * @param env the environment
292 static node_t *create_node(ir_node *irn, environment_t *env) {
293 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
302 * Adds a node to a block wait queue.
304 * @param block the block to add to
305 * @param node the node to add
307 static void add_node(block_t *block, node_t *node) {
308 list_add_tail(&node->node_list, &block->nodes);
312 * Add an input pair to a block.
314 * @param block the block
315 * @param irn the IR-node that has an block input
316 * @param idx the index of the block input in node's predecessors
317 * @param env the environment
319 static void add_pair(block_t *block, ir_node *irn, int idx, environment_t *env) {
320 pair_t *pair = obstack_alloc(&env->obst, sizeof(*pair));
322 pair->next = block->input_pairs;
327 block->input_pairs = pair;
331 * Add a Phi to a block.
333 * @param block the block
334 * @param phi the Phi node
335 * @param env the environment
337 static void add_phi(block_t *block, ir_node *phi, environment_t *env) {
338 phi_t *node = obstack_alloc(&env->obst, sizeof(*node));
340 node->next = block->phis;
348 * Creates an opcode from a node.
350 static opcode_key_t *opcode(const node_t *node, environment_t *env) {
351 opcode_key_t key, *entry;
352 ir_node *irn = node->node;
354 if (node->is_input) {
355 /* Node: as Block nodes are never propagated, it is safe to
356 use its code for "input" node */
357 key.code = iro_Block;
360 key.code = get_irn_opcode(irn);
361 key.arity = get_irn_arity(irn);
363 key.mode = get_irn_mode(node->node);
369 key.u.proj = get_Proj_proj(irn);
372 key.u.ent = get_Sel_entity(irn);
378 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
383 * Split a partition by a local list.
385 * @param Z partition to split
386 * @param g a (non-empty) block list
387 * @param env the environment
389 * @return a new partition containing the nodes of g
391 static partition_t *split(partition_t *Z, block_t *g, environment_t *env) {
392 partition_t *Z_prime;
396 dump_partition("Splitting ", Z);
397 dump_list("by list ", g);
401 /* Remove g from Z. */
402 for (block = g; block != NULL; block = block->next) {
403 list_del(&block->block_list);
406 assert(n < Z->n_blocks);
409 /* Move g to a new partition, Z'. */
410 Z_prime = create_partition(env);
411 for (block = g; block != NULL; block = block->next) {
412 list_add_tail(&block->block_list, &Z_prime->blocks);
414 Z_prime->n_blocks = n;
416 dump_partition("Now ", Z);
417 dump_partition("Created new ", Z_prime);
422 * Propagate nodes on all wait queues of the given partition.
424 * @param part the partition
425 * @param env the environment
427 void propagate_blocks(partition_t *part, environment_t *env) {
428 block_t *ready_blocks = NULL;
429 unsigned n_ready = 0;
432 listmap_entry_t *iter;
434 DB((dbg, LEVEL_2, "Propagate blocks on part%u\n", part->nr));
436 /* Let map be an empty mapping from the range of Opcodes to (local) list of Nodes. */
438 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
440 listmap_entry_t *entry;
443 if (list_empty(&bl->nodes)) {
444 bl->next = ready_blocks;
447 DB((dbg, LEVEL_1, "Block %+F completely processed\n", bl->block));
451 /* get the first node from the wait queue */
452 node = list_entry(bl->nodes.next, node_t, node_list);
453 list_del(&node->node_list);
455 /* put all not-visited predecessors to the wait queue */
456 if (! node->is_input) {
457 ir_node *irn = node->node;
460 DB((dbg, LEVEL_3, " propagate %+F\n", irn));
461 ir_normalize_node(node->node);
462 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
463 ir_node *pred = get_irn_n(irn, i);
464 ir_node *block = get_nodes_block(skip_Proj(pred));
467 if (block != bl->block) {
468 p_node = create_node(pred, env);
469 p_node->is_input = 1;
470 add_node(bl, p_node);
472 add_pair(bl, irn, i, env);
473 } else if (! irn_visited_else_mark(pred)) {
474 /* not yet visited, ok */
475 p_node = create_node(pred, env);
476 add_node(bl, p_node);
479 /* update the Phi list */
480 add_phi(bl, pred, env);
485 DB((dbg, LEVEL_3, " propagate Input %+F\n", node->node));
488 /* Add bl to map[opcode(bl)]. */
489 id = opcode(node, env);
490 entry = listmap_find(&map, id);
491 bl->next = entry->list;
495 /* split out ready blocks */
499 if (n_ready < part->n_blocks)
500 Z = split(part, ready_blocks, env);
503 list_del(&Z->part_list);
505 if (Z->n_blocks > 1) {
506 DB((dbg, LEVEL_1, "Partition %u is ready\n", Z->nr));
507 list_add(&Z->part_list, &env->ready);
509 DB((dbg, LEVEL_1, "Partition %u contains only one block, killed\n", Z->nr));
513 /* for all sets S except one in the range of map do */
514 for (iter = map.values; iter != NULL; iter = iter->next) {
517 if (iter->next == NULL) {
518 /* this is the last entry, ignore */
523 /* Add SPLIT( X, S ) to P. */
527 } /* propagate_blocks */
530 * Propagate nodes on all wait queues.
532 * @param env the environment
534 void propagate(environment_t *env) {
535 partition_t *part, *next;
537 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
538 if (part->n_blocks < 2) {
539 /* zero or one block left, kill this partition */
540 list_del(&part->part_list);
541 DB((dbg, LEVEL_1, "Partition %u contains less than 2 blocks, killed\n", part->nr));
543 propagate_blocks(part, env);
548 * Apply analysis results by replacing all blocks of a partition
549 * by one representative.
551 * Route all inputs from all block of the partition to the one
553 * Enhance all existing Phis by combining them.
554 * Create new Phis for all previous input nodes.
556 * @param part the partition to process
558 static void apply(ir_graph *irg, partition_t *part) {
559 block_t *repr = list_entry(part->blocks.next, block_t, block_list);
561 ir_node *block, *end, *end_block;
563 phi_t *repr_phi, *phi;
564 pair_t *repr_pair, *pair;
565 int i, j, n, block_nr;
567 list_del(&repr->block_list);
569 /* prepare new in arrays for the block ... */
571 n = get_Block_n_cfgpreds(block);
572 ins = NEW_ARR_F(ir_node *, n);
574 for (i = 0; i < n; ++i) {
575 ins[i] = get_Block_cfgpred(block, i);
578 /* ... for all existing Phis ... */
579 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
580 repr_phi->ins = NEW_ARR_F(ir_node *, n);
582 for (i = 0; i < n; ++i)
583 repr_phi->ins[i] = get_Phi_pred(repr_phi->phi, i);
586 /* ... and all newly created Phis */
587 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
588 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
590 repr_pair->ins = NEW_ARR_F(ir_node *, n);
591 for (i = 0; i < n; ++i)
592 repr_pair->ins[i] = input;
595 /* collect new in arrays */
596 end = get_irg_end(irg);
598 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
602 /* first step: kill any keep-alive from this block */
603 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
604 ir_node *ka = get_End_keepalive(end, i);
608 remove_End_keepalive(end, ka);
610 if (get_nodes_block(ka) == block)
611 remove_End_keepalive(end, ka);
615 /* second step: update control flow */
616 n = get_Block_n_cfgpreds(block);
617 for (i = 0; i < n; ++i) {
618 ir_node *pred = get_Block_cfgpred(block, i);
619 ARR_APP1(ir_node *, ins, pred);
622 /* third step: update Phis */
623 for (repr_phi = repr->phis, phi = bl->phis;
625 repr_phi = repr_phi->next, phi = phi->next) {
626 for (i = 0; i < n; ++i) {
627 ir_node *pred = get_Phi_pred(phi->phi, i);
628 ARR_APP1(ir_node *, repr_phi->ins, pred);
632 /* fourth step: update inputs for new Phis */
633 for (repr_pair = repr->input_pairs, pair = bl->input_pairs;
635 repr_pair = repr_pair->next, pair = pair->next) {
636 ir_node *input = get_irn_n(pair->irn, pair->index);
638 for (i = 0; i < n; ++i)
639 ARR_APP1(ir_node *, repr_pair->ins, input);
644 /* rewire block input ... */
647 set_irn_in(block, n, ins);
650 /* ... existing Phis ... */
651 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
652 set_irn_in(repr_phi->phi, n, repr_phi->ins);
653 DEL_ARR_F(repr_phi->ins);
656 /* ... and all inputs by creating new Phis ... */
657 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
658 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
659 ir_mode *mode = get_irn_mode(input);
660 ir_node *phi = new_r_Phi(current_ir_graph, block, n, repr_pair->ins, mode);
662 set_irn_n(repr_pair->irn, repr_pair->index, phi);
663 DEL_ARR_F(repr_pair->ins);
666 /* ... finally rewire the end block */
667 end_block = get_irg_end_block(irg);
668 n = get_Block_n_cfgpreds(end_block);
670 ins = NEW_ARR_F(ir_node *, n);
672 for (i = j = 0; i < n; ++i) {
673 ir_node *out = get_Block_cfgpred(end_block, i);
675 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
678 for (root = bl->roots; root != NULL; root = root->next) {
679 if (root->node == out)
687 set_irn_in(end_block, j, ins);
690 /* control flow changed */
691 set_irg_outs_inconsistent(irg);
692 set_irg_extblk_inconsistent(irg);
693 set_irg_doms_inconsistent(irg);
694 /* Hmm, only the root loop is inconsistent */
695 set_irg_loopinfo_inconsistent(irg);
698 /* Combines congruent end blocks into one. */
699 int melt_end_blocks(ir_graph *irg) {
706 rem = current_ir_graph;
707 current_ir_graph = irg;
709 /* register a debug mask */
710 FIRM_DBG_REGISTER(dbg, "firm.opt.blocks");
712 DEBUG_ONLY(part_nr = 0);
713 DB((dbg, LEVEL_1, "Melting end blocks for %+F\n", irg));
715 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
716 inc_irg_visited(irg);
718 obstack_init(&env.obst);
719 INIT_LIST_HEAD(&env.partitions);
720 INIT_LIST_HEAD(&env.ready);
721 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
723 part = create_partition(&env);
725 /* collect all no-return blocks */
726 end = get_irg_end(irg);
727 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
728 ir_node *ka = get_End_keepalive(end, i);
735 mark_irn_visited(ka);
738 block = get_nodes_block(ka);
739 bl = create_block(block, &env);
741 node = create_node(ka, &env);
744 node->next = bl->roots;
748 /* collect normal blocks */
749 end = get_irg_end_block(irg);
750 for (i = get_Block_n_cfgpreds(end) - 1; i >= 0; --i) {
751 ir_node *pred = get_Block_cfgpred(end, i);
756 mark_irn_visited(pred);
758 block = get_nodes_block(pred);
759 bl = create_block(block, &env);
761 node = create_node(pred, &env);
764 node->next = bl->roots;
768 while (! list_empty(&env.partitions))
771 res = !list_empty(&env.ready);
773 list_for_each_entry(partition_t, part, &env.ready, part_list) {
774 dump_partition("Ready Partition", part);
777 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
778 del_set(env.opcode2id_map);
779 obstack_free(&env.obst, NULL);
780 current_ir_graph = rem;
783 } /* melt_end_blocks */