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.
33 #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", node->node));
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 p_node->is_input = 1;
471 add_node(bl, p_node);
473 add_pair(bl, node->node, i, env);
474 } else if (! irn_visited_else_mark(pred)) {
475 /* not yet visited, ok */
476 p_node = create_node(pred, env);
477 add_node(bl, p_node);
480 /* update the Phi list */
481 add_phi(bl, pred, env);
486 DB((dbg, LEVEL_3, " propagate Input %+F\n", node->node));
489 /* Add bl to map[opcode(bl)]. */
490 id = opcode(node, env);
491 entry = listmap_find(&map, id);
492 bl->next = entry->list;
496 /* split out ready blocks */
500 if (n_ready < part->n_blocks)
501 Z = split(part, ready_blocks, env);
504 list_del(&Z->part_list);
506 if (Z->n_blocks > 1) {
507 DB((dbg, LEVEL_1, "Partition %u is ready\n", Z->nr));
508 list_add(&Z->part_list, &env->ready);
510 DB((dbg, LEVEL_1, "Partition %u contains only one block, killed\n", Z->nr));
514 /* for all sets S except one in the range of map do */
515 for (iter = map.values; iter != NULL; iter = iter->next) {
518 if (iter->next == NULL) {
519 /* this is the last entry, ignore */
524 /* Add SPLIT( X, S ) to P. */
528 } /* propagate_blocks */
531 * Propagate nodes on all wait queues.
533 * @param env the environment
535 void propagate(environment_t *env) {
536 partition_t *part, *next;
538 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
539 if (part->n_blocks == 1) {
540 /* only one block left, move to ready */
541 list_del(&part->part_list);
542 DB((dbg, LEVEL_1, "Partition %u contains only one block, killed\n", part->nr));
544 propagate_blocks(part, env);
549 * Apply analysis results by replacing all blocks of a partition
550 * by one representative.
552 * Route all inputs from all block of the partition to the one
554 * Enhance all existing Phis by combining them.
555 * Create new Phis for all previous input nodes.
557 * @param part the partition to process
559 static void apply(ir_graph *irg, partition_t *part) {
560 block_t *repr = list_entry(part->blocks.next, block_t, block_list);
562 ir_node *block, *end, *end_block;
564 phi_t *repr_phi, *phi;
565 pair_t *repr_pair, *pair;
566 int i, j, n, block_nr;
568 list_del(&repr->block_list);
570 /* prepare new in arrays for the block ... */
572 n = get_Block_n_cfgpreds(block);
573 ins = NEW_ARR_F(ir_node *, n);
575 for (i = 0; i < n; ++i)
576 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 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 repr_pair->ins = NEW_ARR_F(ir_node *, part->n_blocks);
589 repr_pair->ins[0] = get_irn_n(repr_pair->irn, repr_pair->index);
592 /* collect new in arrays */
593 end = get_irg_end(irg);
595 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
599 /* first step: kill any keep-alive from this block */
600 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
601 ir_node *ka = get_End_keepalive(end, i);
605 remove_End_keepalive(end, ka);
607 if (get_nodes_block(ka) == block)
608 remove_End_keepalive(end, ka);
612 /* second step: update control flow */
613 n = get_Block_n_cfgpreds(block);
614 for (i = 0; i < n; ++i) {
615 ARR_APP1(ir_node *, ins, get_Block_cfgpred(block, i));
618 /* third step: update Phis */
619 for (repr_phi = repr->phis, phi = bl->phis;
621 repr_phi = repr_phi->next, phi = phi->next) {
622 for (i = 0; i < n; ++i)
623 ARR_APP1(ir_node *, repr_phi->ins, get_Phi_pred(phi->phi, i));
626 /* fourth step: update inputs for new Phis */
627 for (repr_pair = repr->input_pairs, pair = bl->input_pairs;
629 repr_pair = repr_pair->next, pair = pair->next) {
630 repr_pair->ins[block_nr] = get_irn_n(pair->irn, pair->index);
635 /* rewire block input ... */
638 set_irn_in(block, n, ins);
641 /* ... existing Phis ... */
642 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
643 set_irn_in(repr_phi->phi, n, repr_phi->ins);
644 DEL_ARR_F(repr_phi->ins);
647 /* ... and all inputs by creating new Phis ... */
649 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
650 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
651 ir_mode *mode = get_irn_mode(input);
652 ir_node *phi = new_r_Phi(current_ir_graph, block, n, repr_pair->ins, mode);
654 set_irn_n(repr_pair->irn, repr_pair->index, phi);
655 DEL_ARR_F(repr_pair->ins);
658 /* ... finally rewire the end block */
659 end_block = get_irg_end_block(irg);
660 n = get_Block_n_cfgpreds(end_block);
662 ins = NEW_ARR_F(ir_node *, n);
664 for (i = j = 0; i < n; ++i) {
665 ir_node *out = get_Block_cfgpred(end_block, i);
667 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
670 for (root = bl->roots; root != NULL; root = root->next) {
671 if (root->node == out)
679 set_irn_in(end_block, j, ins);
682 /* control flow changed */
683 set_irg_outs_inconsistent(irg);
684 set_irg_extblk_inconsistent(irg);
685 set_irg_doms_inconsistent(irg);
686 /* Hmm, only the root loop is inconsistent */
687 set_irg_loopinfo_inconsistent(irg);
690 /* Combines congruent end blocks into one. */
691 int melt_end_blocks(ir_graph *irg) {
698 rem = current_ir_graph;
699 current_ir_graph = irg;
701 /* register a debug mask */
702 FIRM_DBG_REGISTER(dbg, "firm.opt.blocks");
704 DEBUG_ONLY(part_nr = 0);
705 DB((dbg, LEVEL_1, "Melting end blocks for %+F\n", irg));
707 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
708 inc_irg_visited(irg);
710 obstack_init(&env.obst);
711 INIT_LIST_HEAD(&env.partitions);
712 INIT_LIST_HEAD(&env.ready);
713 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
715 part = create_partition(&env);
717 /* collect all no-return blocks */
718 end = get_irg_end(irg);
719 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
720 ir_node *ka = get_End_keepalive(end, i);
727 mark_irn_visited(ka);
730 block = get_nodes_block(ka);
731 bl = create_block(block, &env);
733 node = create_node(ka, &env);
736 node->next = bl->roots;
740 /* collect normal blocks */
741 end = get_irg_end_block(irg);
742 for (i = get_Block_n_cfgpreds(end) - 1; i >= 0; --i) {
743 ir_node *pred = get_Block_cfgpred(end, i);
748 mark_irn_visited(pred);
750 block = get_nodes_block(pred);
751 bl = create_block(block, &env);
753 node = create_node(pred, &env);
756 node->next = bl->roots;
760 while (! list_empty(&env.partitions))
763 res = !list_empty(&env.ready);
765 list_for_each_entry(partition_t, part, &env.ready, part_list) {
766 dump_partition("Ready Partition", part);
769 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
770 del_set(env.opcode2id_map);
771 obstack_free(&env.obst, NULL);
772 current_ir_graph = rem;
775 } /* melt_end_blocks */