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 add_node(bl, p_node);
470 /* do not threat Constants like live-ins */
471 if (! is_irn_constlike(irn)) {
472 p_node->is_input = 1;
474 add_pair(bl, irn, i, env);
476 } else if (! irn_visited_else_mark(pred)) {
477 /* not yet visited, ok */
478 p_node = create_node(pred, env);
479 add_node(bl, p_node);
482 /* update the Phi list */
483 add_phi(bl, pred, env);
488 DB((dbg, LEVEL_3, " propagate Input %+F\n", node->node));
491 /* Add bl to map[opcode(bl)]. */
492 id = opcode(node, env);
493 entry = listmap_find(&map, id);
494 bl->next = entry->list;
498 /* split out ready blocks */
502 if (n_ready < part->n_blocks)
503 Z = split(part, ready_blocks, env);
506 list_del(&Z->part_list);
508 if (Z->n_blocks > 1) {
509 DB((dbg, LEVEL_1, "Partition %u is ready\n", Z->nr));
510 list_add(&Z->part_list, &env->ready);
512 DB((dbg, LEVEL_1, "Partition %u contains only one block, killed\n", Z->nr));
516 /* for all sets S except one in the range of map do */
517 for (iter = map.values; iter != NULL; iter = iter->next) {
520 if (iter->next == NULL) {
521 /* this is the last entry, ignore */
526 /* Add SPLIT( X, S ) to P. */
530 } /* propagate_blocks */
533 * Propagate nodes on all wait queues.
535 * @param env the environment
537 void propagate(environment_t *env) {
538 partition_t *part, *next;
540 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
541 if (part->n_blocks < 2) {
542 /* zero or one block left, kill this partition */
543 list_del(&part->part_list);
544 DB((dbg, LEVEL_1, "Partition %u contains less than 2 blocks, killed\n", part->nr));
546 propagate_blocks(part, env);
551 * Apply analysis results by replacing all blocks of a partition
552 * by one representative.
554 * Route all inputs from all block of the partition to the one
556 * Enhance all existing Phis by combining them.
557 * Create new Phis for all previous input nodes.
559 * @param part the partition to process
561 static void apply(ir_graph *irg, partition_t *part) {
562 block_t *repr = list_entry(part->blocks.next, block_t, block_list);
564 ir_node *block, *end, *end_block;
566 phi_t *repr_phi, *phi;
567 pair_t *repr_pair, *pair;
568 int i, j, n, block_nr;
570 list_del(&repr->block_list);
572 /* prepare new in arrays for the block ... */
574 n = get_Block_n_cfgpreds(block);
575 ins = NEW_ARR_F(ir_node *, n);
577 for (i = 0; i < n; ++i) {
578 ins[i] = get_Block_cfgpred(block, i);
581 /* ... for all existing Phis ... */
582 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
583 repr_phi->ins = NEW_ARR_F(ir_node *, n);
585 for (i = 0; i < n; ++i)
586 repr_phi->ins[i] = get_Phi_pred(repr_phi->phi, i);
589 /* ... and all newly created Phis */
590 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
591 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
593 repr_pair->ins = NEW_ARR_F(ir_node *, n);
594 for (i = 0; i < n; ++i)
595 repr_pair->ins[i] = input;
598 /* collect new in arrays */
599 end = get_irg_end(irg);
601 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
605 /* first step: kill any keep-alive from this block */
606 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
607 ir_node *ka = get_End_keepalive(end, i);
611 remove_End_keepalive(end, ka);
613 if (get_nodes_block(ka) == block)
614 remove_End_keepalive(end, ka);
618 /* second step: update control flow */
619 n = get_Block_n_cfgpreds(block);
620 for (i = 0; i < n; ++i) {
621 ir_node *pred = get_Block_cfgpred(block, i);
622 ARR_APP1(ir_node *, ins, pred);
625 /* third step: update Phis */
626 for (repr_phi = repr->phis, phi = bl->phis;
628 repr_phi = repr_phi->next, phi = phi->next) {
629 for (i = 0; i < n; ++i) {
630 ir_node *pred = get_Phi_pred(phi->phi, i);
631 ARR_APP1(ir_node *, repr_phi->ins, pred);
635 /* fourth step: update inputs for new Phis */
636 for (repr_pair = repr->input_pairs, pair = bl->input_pairs;
638 repr_pair = repr_pair->next, pair = pair->next) {
639 ir_node *input = get_irn_n(pair->irn, pair->index);
641 for (i = 0; i < n; ++i)
642 ARR_APP1(ir_node *, repr_pair->ins, input);
647 /* rewire block input ... */
650 set_irn_in(block, n, ins);
653 /* ... existing Phis ... */
654 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
655 set_irn_in(repr_phi->phi, n, repr_phi->ins);
656 DEL_ARR_F(repr_phi->ins);
659 /* ... and all inputs by creating new Phis ... */
660 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
661 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
662 ir_mode *mode = get_irn_mode(input);
663 ir_node *phi = new_r_Phi(current_ir_graph, block, n, repr_pair->ins, mode);
665 set_irn_n(repr_pair->irn, repr_pair->index, phi);
666 DEL_ARR_F(repr_pair->ins);
669 /* ... finally rewire the end block */
670 end_block = get_irg_end_block(irg);
671 n = get_Block_n_cfgpreds(end_block);
673 ins = NEW_ARR_F(ir_node *, n);
675 for (i = j = 0; i < n; ++i) {
676 ir_node *out = get_Block_cfgpred(end_block, i);
678 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
681 for (root = bl->roots; root != NULL; root = root->next) {
682 if (root->node == out)
690 set_irn_in(end_block, j, ins);
693 /* control flow changed */
694 set_irg_outs_inconsistent(irg);
695 set_irg_extblk_inconsistent(irg);
696 set_irg_doms_inconsistent(irg);
697 /* Hmm, only the root loop is inconsistent */
698 set_irg_loopinfo_inconsistent(irg);
701 /* Combines congruent end blocks into one. */
702 int melt_end_blocks(ir_graph *irg) {
709 rem = current_ir_graph;
710 current_ir_graph = irg;
712 /* register a debug mask */
713 FIRM_DBG_REGISTER(dbg, "firm.opt.blocks");
715 DEBUG_ONLY(part_nr = 0);
716 DB((dbg, LEVEL_1, "Melting end blocks for %+F\n", irg));
718 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
719 inc_irg_visited(irg);
721 obstack_init(&env.obst);
722 INIT_LIST_HEAD(&env.partitions);
723 INIT_LIST_HEAD(&env.ready);
724 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
726 part = create_partition(&env);
728 /* collect all no-return blocks */
729 end = get_irg_end(irg);
730 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
731 ir_node *ka = get_End_keepalive(end, i);
738 mark_irn_visited(ka);
741 block = get_nodes_block(ka);
742 bl = create_block(block, &env);
744 node = create_node(ka, &env);
747 node->next = bl->roots;
751 /* collect normal blocks */
752 end = get_irg_end_block(irg);
753 for (i = get_Block_n_cfgpreds(end) - 1; i >= 0; --i) {
754 ir_node *pred = get_Block_cfgpred(end, i);
759 mark_irn_visited(pred);
761 block = get_nodes_block(pred);
762 bl = create_block(block, &env);
764 node = create_node(pred, &env);
767 node->next = bl->roots;
771 while (! list_empty(&env.partitions))
774 res = !list_empty(&env.ready);
776 list_for_each_entry(partition_t, part, &env.ready, part_list) {
777 dump_partition("Ready Partition", part);
780 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
781 del_set(env.opcode2id_map);
782 obstack_free(&env.obst, NULL);
783 current_ir_graph = rem;
786 } /* melt_end_blocks */