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"
39 typedef struct partition_t partition_t;
40 typedef struct block_t block_t;
41 typedef struct node_t node_t;
42 typedef struct pair_t pair_t;
43 typedef struct phi_t phi_t;
44 typedef struct opcode_key_t opcode_key_t;
45 typedef struct listmap_entry_t listmap_entry_t;
46 typedef struct environment_t environment_t;
48 /** An opcode map key. */
50 ir_opcode code; /**< The Firm opcode. */
51 ir_mode *mode; /**< The mode of all nodes in the partition. */
52 int arity; /**< The arity of this opcode (needed for Phi etc. */
54 long proj; /**< For Proj nodes, its proj number */
55 ir_entity *ent; /**< For Sel Nodes, its entity */
59 /** A partition contains all congruent blocks. */
61 list_head part_list; /**< Double linked list of partitions. */
62 list_head blocks; /**< List of blocks in this partition. */
63 unsigned n_blocks; /**< Number of block in this partition. */
65 unsigned nr; /**< For debugging: number of this partition. */
71 list_head block_list; /**< Double linked list of block inside a partition. */
72 list_head nodes; /**< Wait-queue of nodes that must be checked for congruence. */
73 block_t *next; /**< Next block of a split list. */
74 ir_node *block; /**< Pointer to the associated IR-node block. */
75 node_t *roots; /**< The list of all root nodes. */
76 pair_t *input_pairs; /**< The list of inputs to this block. */
77 phi_t *phis; /**< The list of Phis in this block. */
82 list_head node_list; /**< Double linked list of block inside a partition. */
83 ir_node *node; /**< Pointer to the associated IR-node or NULL for block inputs. */
84 node_t *next; /**< Link to the next node in the root set. */
85 char is_input; /**< Set if this node is an input from other block. */
88 /** The environment. */
89 struct environment_t {
90 list_head partitions; /**< list of partitions. */
91 list_head ready; /**< list of ready partitions. */
92 set *opcode2id_map; /**< The opcodeMode->id map. */
93 struct obstack obst; /** obstack for temporary data */
96 /** A node, input index pair. */
98 pair_t *next; /**< Points to the next pair entry. */
99 ir_node *irn; /**< The IR-node. */
100 int index; /**< An input index. */
101 ir_node **ins; /**< A new in array once allocated. */
104 /** A Phi, inputs pair. */
106 phi_t *next; /**< Points to the next Phi pair entry. */
107 ir_node *phi; /**< The Phi node. */
108 ir_node **ins; /**< A new in array once allocated. */
112 * An entry in the list_map.
114 struct listmap_entry_t {
115 void *id; /**< The id. */
116 block_t *list; /**< The associated list for this id. */
117 listmap_entry_t *next; /**< Link to the next entry in the map. */
120 /** We must map id's to lists. */
121 typedef struct listmap_t {
122 set *map; /**< Map id's to listmap_entry_t's */
123 listmap_entry_t *values; /**< List of all values in the map. */
126 /** The debug module handle. */
127 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
129 /** Next partition number. */
130 DEBUG_ONLY(static unsigned part_nr = 0);
134 * Dump partition to output.
136 static void dump_partition(const char *msg, const partition_t *part) {
137 const block_t *block;
140 DB((dbg, LEVEL_2, "%s part%u (%u) {\n ", msg, part->nr, part->n_blocks));
141 list_for_each_entry(block_t, block, &part->blocks, block_list) {
142 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", block->block));
145 DB((dbg, LEVEL_2, "\n}\n"));
146 } /* dump_partition */
151 static void dump_list(const char *msg, const block_t *block) {
155 DB((dbg, LEVEL_3, "%s = {\n ", msg));
156 for (p = block; p != NULL; p = p->next) {
157 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->block));
160 DB((dbg, LEVEL_3, "\n}\n"));
163 #define dump_partition(msg, part)
164 #define dump_list(msg, block)
168 * Compare two pointer values of a listmap.
170 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
171 const listmap_entry_t *e1 = elt;
172 const listmap_entry_t *e2 = key;
175 return e1->id != e2->id;
176 } /* listmap_cmp_ptr */
179 * Initializes a listmap.
181 * @param map the listmap
183 static void listmap_init(listmap_t *map) {
184 map->map = new_set(listmap_cmp_ptr, 16);
189 * Terminates a listmap.
191 * @param map the listmap
193 static void listmap_term(listmap_t *map) {
198 * Return the associated listmap entry for a given id.
200 * @param map the listmap
201 * @param id the id to search for
203 * @return the associated listmap entry for the given id
205 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
206 listmap_entry_t key, *entry;
211 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
213 if (entry->list == NULL) {
214 /* a new entry, put into the list */
215 entry->next = map->values;
222 * Calculate the hash value for an opcode map entry.
224 * @param entry an opcode map entry
226 * @return a hash value for the given opcode map entry
228 static unsigned opcode_hash(const opcode_key_t *entry) {
229 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.ent) + entry->arity;
233 * Compare two entries in the opcode map.
235 static int cmp_opcode(const void *elt, const void *key, size_t size) {
236 const opcode_key_t *o1 = elt;
237 const opcode_key_t *o2 = key;
240 return o1->code != o2->code || o1->mode != o2->mode ||
241 o1->arity != o2->arity ||
242 o1->u.proj != o2->u.proj || o1->u.ent != o2->u.ent;
246 * Creates a new empty partition.
248 static partition_t *create_partition(environment_t *env) {
249 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
251 INIT_LIST_HEAD(&part->blocks);
253 DEBUG_ONLY(part->nr = part_nr++);
254 list_add_tail(&part->part_list, &env->partitions);
256 } /* create_partition */
259 * Allocate a new block.
261 * @param block the IR-node
262 * @param env the environment
264 static block_t *create_block(ir_node *block, environment_t *env) {
265 block_t *bl = obstack_alloc(&env->obst, sizeof(*bl));
267 INIT_LIST_HEAD(&bl->nodes);
271 bl->input_pairs = NULL;
277 * Adds a block to a partition.
279 * @param partition the partition to add to
280 * @param block the block to add
282 static void add_block(partition_t *partition, block_t *block) {
283 list_add_tail(&block->block_list, &partition->blocks);
284 ++partition->n_blocks;
288 * Allocate a new node.
290 * @param irn the IR-node
291 * @param env the environment
293 static node_t *create_node(ir_node *irn, environment_t *env) {
294 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
303 * Adds a node to a block wait queue.
305 * @param block the block to add to
306 * @param node the node to add
308 static void add_node(block_t *block, node_t *node) {
309 list_add_tail(&node->node_list, &block->nodes);
313 * Add an input pair to a block.
315 * @param block the block
316 * @param irn the IR-node that has an block input
317 * @param idx the index of the block input in node's predecessors
318 * @param env the environment
320 static void add_pair(block_t *block, ir_node *irn, int idx, environment_t *env) {
321 pair_t *pair = obstack_alloc(&env->obst, sizeof(*pair));
323 pair->next = block->input_pairs;
328 block->input_pairs = pair;
332 * Add a Phi to a block.
334 * @param block the block
335 * @param phi the Phi node
336 * @param env the environment
338 static void add_phi(block_t *block, ir_node *phi, environment_t *env) {
339 phi_t *node = obstack_alloc(&env->obst, sizeof(*node));
341 node->next = block->phis;
349 * Creates an opcode from a node.
351 static opcode_key_t *opcode(const node_t *node, environment_t *env) {
352 opcode_key_t key, *entry;
353 ir_node *irn = node->node;
355 if (node->is_input) {
356 /* Node: as Block nodes are never propagated, it is safe to
357 use its code for "input" node */
358 key.code = iro_Block;
361 key.code = get_irn_opcode(irn);
362 key.arity = get_irn_arity(irn);
364 key.mode = get_irn_mode(node->node);
370 key.u.proj = get_Proj_proj(irn);
373 key.u.ent = get_Sel_entity(irn);
379 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
384 * Split a partition by a local list.
386 * @param Z partition to split
387 * @param g a (non-empty) block list
388 * @param env the environment
390 * @return a new partition containing the nodes of g
392 static partition_t *split(partition_t *Z, block_t *g, environment_t *env) {
393 partition_t *Z_prime;
397 dump_partition("Splitting ", Z);
398 dump_list("by list ", g);
402 /* Remove g from Z. */
403 for (block = g; block != NULL; block = block->next) {
404 list_del(&block->block_list);
407 assert(n < Z->n_blocks);
410 /* Move g to a new partition, Z'. */
411 Z_prime = create_partition(env);
412 for (block = g; block != NULL; block = block->next) {
413 list_add_tail(&block->block_list, &Z_prime->blocks);
415 Z_prime->n_blocks = n;
417 dump_partition("Now ", Z);
418 dump_partition("Created new ", Z_prime);
423 * Propagate nodes on all wait queues of the given partition.
425 * @param part the partition
426 * @param env the environment
428 void propagate_blocks(partition_t *part, environment_t *env) {
429 block_t *ready_blocks = NULL;
430 unsigned n_ready = 0;
433 listmap_entry_t *iter;
435 DB((dbg, LEVEL_2, "Propagate blocks on part%u\n", part->nr));
437 /* Let map be an empty mapping from the range of Opcodes to (local) list of Nodes. */
439 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
441 listmap_entry_t *entry;
444 if (list_empty(&bl->nodes)) {
445 bl->next = ready_blocks;
448 DB((dbg, LEVEL_1, "Block %+F completely processed\n", bl->block));
452 /* get the first node from the wait queue */
453 node = list_entry(bl->nodes.next, node_t, node_list);
454 list_del(&node->node_list);
456 /* put all not-visited predecessors to the wait queue */
457 if (! node->is_input) {
458 ir_node *irn = node->node;
461 DB((dbg, LEVEL_3, " propagate %+F\n", irn));
462 ir_normalize_node(node->node);
463 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
464 ir_node *pred = get_irn_n(irn, i);
465 ir_node *block = get_nodes_block(skip_Proj(pred));
468 if (block != bl->block) {
469 p_node = create_node(pred, env);
470 add_node(bl, p_node);
471 /* do not threat Constants like live-ins */
472 if (! is_irn_constlike(irn)) {
473 p_node->is_input = 1;
475 add_pair(bl, irn, i, env);
477 } else if (! irn_visited_else_mark(pred)) {
478 /* not yet visited, ok */
479 p_node = create_node(pred, env);
480 add_node(bl, p_node);
483 /* update the Phi list */
484 add_phi(bl, pred, env);
489 DB((dbg, LEVEL_3, " propagate Input %+F\n", node->node));
492 /* Add bl to map[opcode(bl)]. */
493 id = opcode(node, env);
494 entry = listmap_find(&map, id);
495 bl->next = entry->list;
499 /* split out ready blocks */
503 if (n_ready < part->n_blocks)
504 Z = split(part, ready_blocks, env);
507 list_del(&Z->part_list);
509 if (Z->n_blocks > 1) {
510 DB((dbg, LEVEL_1, "Partition %u is ready\n", Z->nr));
511 list_add(&Z->part_list, &env->ready);
513 DB((dbg, LEVEL_1, "Partition %u contains only one block, killed\n", Z->nr));
517 /* for all sets S except one in the range of map do */
518 for (iter = map.values; iter != NULL; iter = iter->next) {
521 if (iter->next == NULL) {
522 /* this is the last entry, ignore */
527 /* Add SPLIT( X, S ) to P. */
531 } /* propagate_blocks */
534 * Propagate nodes on all wait queues.
536 * @param env the environment
538 void propagate(environment_t *env) {
539 partition_t *part, *next;
541 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
542 if (part->n_blocks < 2) {
543 /* zero or one block left, kill this partition */
544 list_del(&part->part_list);
545 DB((dbg, LEVEL_1, "Partition %u contains less than 2 blocks, killed\n", part->nr));
547 propagate_blocks(part, env);
552 * Apply analysis results by replacing all blocks of a partition
553 * by one representative.
555 * Route all inputs from all block of the partition to the one
557 * Enhance all existing Phis by combining them.
558 * Create new Phis for all previous input nodes.
560 * @param part the partition to process
562 static void apply(ir_graph *irg, partition_t *part) {
563 block_t *repr = list_entry(part->blocks.next, block_t, block_list);
565 ir_node *block, *end, *end_block;
567 phi_t *repr_phi, *phi;
568 pair_t *repr_pair, *pair;
569 int i, j, n, block_nr;
571 list_del(&repr->block_list);
573 /* prepare new in arrays for the block ... */
575 n = get_Block_n_cfgpreds(block);
576 ins = NEW_ARR_F(ir_node *, n);
578 for (i = 0; i < n; ++i) {
579 ins[i] = get_Block_cfgpred(block, i);
582 /* ... for all existing Phis ... */
583 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
584 repr_phi->ins = NEW_ARR_F(ir_node *, n);
586 for (i = 0; i < n; ++i)
587 repr_phi->ins[i] = get_Phi_pred(repr_phi->phi, i);
590 /* ... and all newly created Phis */
591 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
592 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
594 repr_pair->ins = NEW_ARR_F(ir_node *, n);
595 for (i = 0; i < n; ++i)
596 repr_pair->ins[i] = input;
599 /* collect new in arrays */
600 end = get_irg_end(irg);
602 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
606 /* first step: kill any keep-alive from this block */
607 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
608 ir_node *ka = get_End_keepalive(end, i);
612 remove_End_keepalive(end, ka);
614 if (get_nodes_block(ka) == block)
615 remove_End_keepalive(end, ka);
619 /* second step: update control flow */
620 n = get_Block_n_cfgpreds(block);
621 for (i = 0; i < n; ++i) {
622 ir_node *pred = get_Block_cfgpred(block, i);
623 ARR_APP1(ir_node *, ins, pred);
626 /* third step: update Phis */
627 for (repr_phi = repr->phis, phi = bl->phis;
629 repr_phi = repr_phi->next, phi = phi->next) {
630 for (i = 0; i < n; ++i) {
631 ir_node *pred = get_Phi_pred(phi->phi, i);
632 ARR_APP1(ir_node *, repr_phi->ins, pred);
636 /* fourth step: update inputs for new Phis */
637 for (repr_pair = repr->input_pairs, pair = bl->input_pairs;
639 repr_pair = repr_pair->next, pair = pair->next) {
640 ir_node *input = get_irn_n(pair->irn, pair->index);
642 for (i = 0; i < n; ++i)
643 ARR_APP1(ir_node *, repr_pair->ins, input);
648 /* rewire block input ... */
651 set_irn_in(block, n, ins);
654 /* ... existing Phis ... */
655 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
656 set_irn_in(repr_phi->phi, n, repr_phi->ins);
657 DEL_ARR_F(repr_phi->ins);
660 /* ... and all inputs by creating new Phis ... */
661 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
662 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
663 ir_mode *mode = get_irn_mode(input);
664 ir_node *phi = new_r_Phi(current_ir_graph, block, n, repr_pair->ins, mode);
666 set_irn_n(repr_pair->irn, repr_pair->index, phi);
667 DEL_ARR_F(repr_pair->ins);
670 /* ... finally rewire the end block */
671 end_block = get_irg_end_block(irg);
672 n = get_Block_n_cfgpreds(end_block);
674 ins = NEW_ARR_F(ir_node *, n);
676 for (i = j = 0; i < n; ++i) {
677 ir_node *out = get_Block_cfgpred(end_block, i);
679 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
682 for (root = bl->roots; root != NULL; root = root->next) {
683 if (root->node == out)
691 set_irn_in(end_block, j, ins);
694 /* control flow changed */
695 set_irg_outs_inconsistent(irg);
696 set_irg_extblk_inconsistent(irg);
697 set_irg_doms_inconsistent(irg);
698 /* Hmm, only the root loop is inconsistent */
699 set_irg_loopinfo_inconsistent(irg);
701 /* Calls might be removed. */
702 set_trouts_inconsistent();
705 /* Combines congruent end blocks into one. */
706 int melt_end_blocks(ir_graph *irg) {
713 rem = current_ir_graph;
714 current_ir_graph = irg;
716 /* register a debug mask */
717 FIRM_DBG_REGISTER(dbg, "firm.opt.blocks");
719 DEBUG_ONLY(part_nr = 0);
720 DB((dbg, LEVEL_1, "Melting end blocks for %+F\n", irg));
722 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
723 inc_irg_visited(irg);
725 obstack_init(&env.obst);
726 INIT_LIST_HEAD(&env.partitions);
727 INIT_LIST_HEAD(&env.ready);
728 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
730 part = create_partition(&env);
732 /* collect all no-return blocks */
733 end = get_irg_end(irg);
734 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
735 ir_node *ka = get_End_keepalive(end, i);
742 mark_irn_visited(ka);
745 block = get_nodes_block(ka);
746 bl = create_block(block, &env);
748 node = create_node(ka, &env);
751 node->next = bl->roots;
755 /* collect normal blocks */
756 end = get_irg_end_block(irg);
757 for (i = get_Block_n_cfgpreds(end) - 1; i >= 0; --i) {
758 ir_node *pred = get_Block_cfgpred(end, i);
763 mark_irn_visited(pred);
765 block = get_nodes_block(pred);
766 bl = create_block(block, &env);
768 node = create_node(pred, &env);
771 node->next = bl->roots;
775 while (! list_empty(&env.partitions))
778 res = !list_empty(&env.ready);
780 list_for_each_entry(partition_t, part, &env.ready, part_list) {
781 dump_partition("Ready Partition", part);
784 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
785 del_set(env.opcode2id_map);
786 obstack_free(&env.obst, NULL);
787 current_ir_graph = rem;
790 } /* melt_end_blocks */