2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Combining congruent blocks
11 * This phase find congruent blocks.
12 * Two block are congruent, if they contains only equal calculations.
16 #include "iroptimize.h"
19 #include "irgraph_t.h"
30 /* define this for general block shaping: congruent blocks
31 are found not only before the end block but anywhere in the graph */
34 typedef struct partition_t partition_t;
35 typedef struct block_t block_t;
36 typedef struct node_t node_t;
37 typedef struct pair_t pair_t;
38 typedef struct phi_t phi_t;
39 typedef struct opcode_key_t opcode_key_t;
40 typedef struct listmap_entry_t listmap_entry_t;
41 typedef struct environment_t environment_t;
42 typedef struct pred_t pred_t;
44 /** An opcode map key. */
46 unsigned code; /**< The Firm opcode. */
47 ir_mode *mode; /**< The mode of all nodes in the partition. */
48 int arity; /**< The arity of this opcode (needed for Phi etc. */
50 long proj; /**< For Proj nodes, its proj number */
51 ir_entity *ent; /**< For Sel nodes, its entity */
52 ir_tarval *tv; /**< For Const nodes, its tarval */
53 symconst_symbol sym; /**< For SymConst nodes, its symbol .*/
54 void *addr; /**< Alias all addresses. */
55 int intVal; /**< For Conv/Div nodes: strict/remainderless. */
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. */
64 ir_node *meet_block; /**< The control flow meet block of this partition. */
66 unsigned nr; /**< For debugging: number of this partition. */
72 list_head block_list; /**< Double linked list of block inside a partition. */
73 list_head nodes; /**< Wait-queue of nodes that must be checked for congruence. */
74 block_t *next; /**< Next block of a split list. */
75 ir_node *block; /**< Pointer to the associated IR-node block. */
76 ir_node **roots; /**< An array of all root nodes. */
77 node_t *cf_root; /**< The control flow root node of this block. */
78 pair_t *input_pairs; /**< The list of inputs to this block. */
79 phi_t *phis; /**< The list of Phis in this block. */
80 block_t *all_next; /**< Links all created blocks. */
81 int meet_input; /**< Input number of this block in the meet-block. */
86 list_head node_list; /**< Double linked list of block inside a partition. */
87 ir_node *node; /**< Pointer to the associated IR-node or NULL for block inputs. */
88 char is_input; /**< Set if this node is an input from other block. */
91 /** The environment. */
92 struct environment_t {
93 list_head partitions; /**< list of partitions. */
94 list_head ready; /**< list of ready partitions. */
95 set *opcode2id_map; /**< The opcodeMode->id map. */
96 ir_node **live_outs; /**< Live out only nodes. */
97 block_t *all_blocks; /**< List of all created blocks. */
98 struct obstack obst; /** obstack for temporary data */
101 /** A (node, input index) pair. */
103 pair_t *next; /**< Points to the next pair entry. */
104 ir_node *irn; /**< The IR-node. */
105 int index; /**< An input index. */
106 ir_node **ins; /**< A new in array once allocated. */
109 /** A Phi, inputs pair. */
111 phi_t *next; /**< Points to the next Phi pair entry. */
112 ir_node *phi; /**< The Phi node. */
113 ir_node **ins; /**< A new in array once allocated. */
116 /** Describes a predecessor input. */
118 ir_node *pred; /**< The predecessor. */
119 int index; /**< Its input index. */
123 * An entry in the list_map.
125 struct listmap_entry_t {
126 void *id; /**< The id. */
127 block_t *list; /**< The associated list for this id. */
128 listmap_entry_t *next; /**< Link to the next entry in the map. */
131 /** We must map id's to lists. */
132 typedef struct listmap_t {
133 set *map; /**< Map id's to listmap_entry_t's */
134 listmap_entry_t *values; /**< List of all values in the map. */
137 #define get_Block_entry(block) ((block_t *)get_irn_link(block))
139 /** The debug module handle. */
140 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
142 /** Next partition number. */
143 DEBUG_ONLY(static unsigned part_nr = 0;)
147 * Dump partition to output.
149 static void dump_partition(const char *msg, const partition_t *part)
153 DB((dbg, LEVEL_2, " %s part%u (%u blocks) {\n ", msg, part->nr, part->n_blocks));
154 list_for_each_entry(block_t, block, &part->blocks, block_list) {
155 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", block->block));
158 DB((dbg, LEVEL_2, "\n }\n"));
164 static void dump_list(const char *msg, const block_t *block)
169 DB((dbg, LEVEL_3, " %s = {\n ", msg));
170 for (p = block; p != NULL; p = p->next) {
171 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->block));
174 DB((dbg, LEVEL_3, "\n }\n"));
177 #define dump_partition(msg, part)
178 #define dump_list(msg, block)
182 * Compare two pointer values of a listmap.
184 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
186 const listmap_entry_t *e1 = (const listmap_entry_t*)elt;
187 const listmap_entry_t *e2 = (const listmap_entry_t*)key;
190 return e1->id != e2->id;
194 * Initializes a listmap.
196 * @param map the listmap
198 static void listmap_init(listmap_t *map)
200 map->map = new_set(listmap_cmp_ptr, 16);
205 * Terminates a listmap.
207 * @param map the listmap
209 static void listmap_term(listmap_t *map)
215 * Return the associated listmap entry for a given id.
217 * @param map the listmap
218 * @param id the id to search for
220 * @return the associated listmap entry for the given id
222 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
224 listmap_entry_t key, *entry;
229 entry = set_insert(listmap_entry_t, map->map, &key, sizeof(key), hash_ptr(id));
231 if (entry->list == NULL) {
232 /* a new entry, put into the list */
233 entry->next = map->values;
240 * Calculate the hash value for an opcode map entry.
242 * @param entry an opcode map entry
244 * @return a hash value for the given opcode map entry
246 static unsigned opcode_hash(const opcode_key_t *entry)
248 /* assume long >= int */
249 return (unsigned)(PTR_TO_INT(entry->mode) * 9 + entry->code + entry->u.proj * 3 + hash_ptr(entry->u.addr) + entry->arity);
253 * Compare two entries in the opcode map.
255 static int cmp_opcode(const void *elt, const void *key, size_t size)
257 const opcode_key_t *o1 = (opcode_key_t*)elt;
258 const opcode_key_t *o2 = (opcode_key_t*)key;
261 return o1->code != o2->code || o1->mode != o2->mode ||
262 o1->arity != o2->arity ||
263 o1->u.proj != o2->u.proj || o1->u.addr != o2->u.addr;
267 * Creates a new empty partition and put in on the
270 * @param meet_block the control flow meet block of this partition
271 * @param env the environment
273 static partition_t *create_partition(ir_node *meet_block, environment_t *env)
275 partition_t *part = OALLOC(&env->obst, partition_t);
277 INIT_LIST_HEAD(&part->blocks);
278 part->meet_block = meet_block;
280 DEBUG_ONLY(part->nr = part_nr++;)
281 list_add_tail(&part->part_list, &env->partitions);
286 * Allocate a new block in the given partition.
288 * @param block the IR-node
289 * @param meet_input Input number of this block in the meet-block
290 * @param partition the partition to add to
291 * @param env the environment
293 static block_t *create_block(ir_node *block, int meet_input, partition_t *partition, environment_t *env)
295 block_t *bl = OALLOC(&env->obst, block_t);
297 set_irn_link(block, bl);
299 INIT_LIST_HEAD(&bl->nodes);
302 bl->roots = NEW_ARR_F(ir_node *, 0);
304 bl->input_pairs = NULL;
306 bl->meet_input = meet_input;
308 /* put it into the list of partition blocks */
309 list_add_tail(&bl->block_list, &partition->blocks);
310 ++partition->n_blocks;
312 /* put in into the list of all blocks */
313 bl->all_next = env->all_blocks;
314 env->all_blocks = bl;
320 * Allocate a new node and add it to a blocks wait queue.
322 * @param irn the IR-node
323 * @param block the block to add to
324 * @param env the environment
326 static node_t *create_node(ir_node *irn, block_t *block, environment_t *env)
328 node_t *node = OALLOC(&env->obst, node_t);
333 list_add_tail(&node->node_list, &block->nodes);
339 * Add an input pair to a block.
341 * @param block the block
342 * @param irn the IR-node that has an block input
343 * @param idx the index of the block input in node's predecessors
344 * @param env the environment
346 static void add_pair(block_t *block, ir_node *irn, int idx, environment_t *env)
348 pair_t *pair = OALLOC(&env->obst, pair_t);
350 pair->next = block->input_pairs;
355 block->input_pairs = pair;
359 * Add a Phi to a block.
361 * @param block the block
362 * @param phi the Phi node
363 * @param env the environment
365 static void add_phi(block_t *block, ir_node *phi, environment_t *env)
367 phi_t *node = OALLOC(&env->obst, phi_t);
369 node->next = block->phis;
377 * Creates an opcode from a node.
379 static opcode_key_t *opcode(const node_t *node, environment_t *env)
381 opcode_key_t key, *entry;
382 ir_node *irn = node->node;
384 if (node->is_input) {
385 /* Node: as Block nodes are never propagated, it is safe to
386 use its code for "input" node */
387 key.code = iro_Block;
390 key.code = get_irn_opcode(irn);
391 key.arity = get_irn_arity(irn);
393 key.mode = get_irn_mode(node->node);
399 key.u.proj = get_Proj_proj(irn);
402 key.u.ent = get_Sel_entity(irn);
405 key.u.sym = get_SymConst_symbol(irn);
408 key.u.tv = get_Const_tarval(irn);
411 key.mode = get_Load_mode(irn);
414 key.u.intVal = get_Div_no_remainder(irn);
417 key.u.intVal = get_Builtin_kind(irn);
423 entry = set_insert(opcode_key_t, env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
428 * Split a partition by a local list.
430 * @param Z partition to split
431 * @param g a (non-empty) block list
432 * @param env the environment
434 * @return a new partition containing the nodes of g
436 static partition_t *split(partition_t *Z, block_t *g, environment_t *env)
438 partition_t *Z_prime;
442 dump_partition("Splitting ", Z);
443 dump_list("by list ", g);
447 /* Remove g from Z. */
448 for (block = g; block != NULL; block = block->next) {
449 list_del(&block->block_list);
452 assert(n < Z->n_blocks);
455 /* Move g to a new partition, Z'. */
456 Z_prime = create_partition(Z->meet_block, env);
457 for (block = g; block != NULL; block = block->next) {
458 list_add_tail(&block->block_list, &Z_prime->blocks);
460 Z_prime->n_blocks = n;
462 dump_partition("Now ", Z);
463 dump_partition("Created new ", Z_prime);
468 * Return non-zero if pred should be tread as a input node.
470 static int is_input_node(ir_node *pred, ir_node *irn, int index)
472 /* for now, do NOT turn direct calls into indirect one */
475 if (! is_SymConst_addr_ent(pred))
483 * Propagate nodes on all wait queues of the given partition.
485 * @param part the partition
486 * @param env the environment
488 static void propagate_blocks(partition_t *part, environment_t *env)
490 block_t *ready_blocks = NULL;
491 unsigned n_ready = 0;
493 listmap_entry_t *iter;
495 DB((dbg, LEVEL_2, " Propagate blocks on part%u\n", part->nr));
497 /* Let map be an empty mapping from the range of Opcodes to (local) list of blocks. */
499 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
501 listmap_entry_t *entry;
504 if (list_empty(&bl->nodes)) {
505 bl->next = ready_blocks;
508 DB((dbg, LEVEL_2, " Block %+F completely processed\n", bl->block));
512 /* get the first node from the wait queue */
513 node = list_entry(bl->nodes.next, node_t, node_list);
514 list_del(&node->node_list);
516 /* put all not-visited predecessors to the wait queue */
517 if (! node->is_input) {
518 ir_node *irn = node->node;
521 DB((dbg, LEVEL_3, " propagate %+F\n", irn));
522 ir_normalize_node(node->node);
523 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
524 ir_node *pred = get_irn_n(irn, i);
525 ir_node *block = get_nodes_block(skip_Proj(pred));
527 if (block != bl->block) {
528 node_t *p_node = create_node(pred, bl, env);
529 if (is_input_node(pred, irn, i)) {
530 /* is a block live input */
531 p_node->is_input = 1;
533 add_pair(bl, irn, i, env);
534 } else if (is_Phi(pred)) {
535 /* update the Phi list */
536 add_phi(bl, pred, env);
538 } else if (! irn_visited_else_mark(pred)) {
539 /* not yet visited, ok */
540 create_node(pred, bl, env);
543 /* update the Phi list */
544 add_phi(bl, pred, env);
549 DB((dbg, LEVEL_3, " propagate Input %+F\n", node->node));
552 /* Add bl to map[opcode(n)]. */
553 id = opcode(node, env);
554 entry = listmap_find(&map, id);
555 bl->next = entry->list;
559 /* split out ready blocks */
563 if (n_ready < part->n_blocks)
564 Z = split(part, ready_blocks, env);
567 list_del(&Z->part_list);
569 if (Z->n_blocks > 1) {
570 DB((dbg, LEVEL_2, " Partition %u is ready\n", Z->nr));
571 list_add(&Z->part_list, &env->ready);
573 DB((dbg, LEVEL_2, " Partition %u contains only one block, killed\n", Z->nr));
577 /* for all sets S except one in the range of map do */
578 for (iter = map.values; iter != NULL; iter = iter->next) {
581 if (iter->next == NULL) {
582 /* this is the last entry, ignore */
587 /* Add SPLIT( X, S ) to P. */
594 * Propagate nodes on all wait queues.
596 * @param env the environment
598 static void propagate(environment_t *env)
600 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
601 if (part->n_blocks < 2) {
602 /* zero or one block left, kill this partition */
603 list_del(&part->part_list);
604 DB((dbg, LEVEL_2, " Partition %u contains less than 2 blocks, killed\n", part->nr));
606 propagate_blocks(part, env);
611 * Map a block to the phi[block->input] live-trough.
613 static void *live_throughs(const block_t *bl, const ir_node *phi)
615 ir_node *input = get_Phi_pred(phi, bl->meet_input);
617 /* If this input is inside our block, this
618 is a live-out and not a live trough.
619 Live-outs are tested inside propagate, so map all of
620 them to the "general" value NULL */
621 if (get_nodes_block(input) == bl->block)
627 * Split partition by live-outs and live-troughs.
629 * @param part the partition
630 * @param env the environment
632 static void propagate_blocks_live_troughs(partition_t *part, environment_t *env)
634 const ir_node *meet_block = part->meet_block;
636 listmap_entry_t *iter;
639 DB((dbg, LEVEL_2, " Propagate live-troughs on part%u\n", part->nr));
641 for (phi = get_Block_phis(meet_block); phi != NULL; phi = get_Phi_next(phi)) {
642 /* propagate on all Phis of the meet-block */
644 if (part->n_blocks < 2) {
645 /* zero or one block left, kill this partition */
646 list_del(&part->part_list);
647 DB((dbg, LEVEL_2, " Partition %u contains less than 2 blocks, killed\n", part->nr));
651 /* Let map be an empty mapping from the range of live-troughs to (local) list of blocks. */
653 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
655 listmap_entry_t *entry;
657 /* Add bl to map[live_trough(bl)]. */
658 id = (opcode_key_t*)live_throughs(bl, phi);
659 entry = listmap_find(&map, id);
660 bl->next = entry->list;
664 /* for all sets S except one in the range of map do */
665 for (iter = map.values; iter != NULL; iter = iter->next) {
668 if (iter->next == NULL) {
669 /* this is the last entry, ignore */
674 /* Add SPLIT( X, S ) to P. */
682 * Propagate live-troughs on all partitions on the partition list.
684 * @param env the environment
686 static void propagate_live_troughs(environment_t *env)
688 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
689 propagate_blocks_live_troughs(part, env);
694 * Apply analysis results by replacing all blocks of a partition
695 * by one representative.
697 * Route all inputs from all block of the partition to the one
699 * Enhance all existing Phis by combining them.
700 * Create new Phis for all previous input nodes.
702 * @param part the partition to process
704 static void apply(ir_graph *irg, partition_t *part)
706 block_t *repr = list_entry(part->blocks.next, block_t, block_list);
707 ir_node *block, *end, *meet_block, *p, *next;
708 ir_node **ins, **phi_ins;
709 phi_t *repr_phi, *phi;
710 pair_t *repr_pair, *pair;
711 int i, j, k, n, n_phis;
713 list_del(&repr->block_list);
715 /* prepare new in arrays for the block ... */
717 n = get_Block_n_cfgpreds(block);
718 ins = NEW_ARR_F(ir_node *, n);
720 for (i = 0; i < n; ++i) {
721 ins[i] = get_Block_cfgpred(block, i);
724 /* ... for all existing Phis ... */
725 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
726 repr_phi->ins = NEW_ARR_F(ir_node *, n);
728 for (i = 0; i < n; ++i)
729 repr_phi->ins[i] = get_Phi_pred(repr_phi->phi, i);
732 /* ... and all newly created Phis */
733 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
734 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
736 repr_pair->ins = NEW_ARR_F(ir_node *, n);
737 for (i = 0; i < n; ++i)
738 repr_pair->ins[i] = input;
741 DB((dbg, LEVEL_1, "Replacing "));
743 /* collect new in arrays */
744 end = get_irg_end(irg);
745 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
748 DB((dbg, LEVEL_1, "%+F, ", block));
750 /* first step: kill any keep-alive from this block */
751 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
752 ir_node *ka = get_End_keepalive(end, i);
756 remove_End_keepalive(end, ka);
758 if (get_nodes_block(ka) == block)
759 remove_End_keepalive(end, ka);
763 /* second step: update control flow */
764 n = get_Block_n_cfgpreds(block);
765 for (i = 0; i < n; ++i) {
766 ir_node *pred = get_Block_cfgpred(block, i);
767 ARR_APP1(ir_node *, ins, pred);
770 /* third step: update Phis */
771 for (repr_phi = repr->phis, phi = bl->phis;
773 repr_phi = repr_phi->next, phi = phi->next) {
774 for (i = 0; i < n; ++i) {
775 ir_node *pred = get_Phi_pred(phi->phi, i);
776 ARR_APP1(ir_node *, repr_phi->ins, pred);
780 /* fourth step: update inputs for new Phis */
781 for (repr_pair = repr->input_pairs, pair = bl->input_pairs;
783 repr_pair = repr_pair->next, pair = pair->next) {
784 ir_node *input = get_irn_n(pair->irn, pair->index);
786 for (i = 0; i < n; ++i)
787 ARR_APP1(ir_node *, repr_pair->ins, input);
791 DB((dbg, LEVEL_1, "by %+F\n", repr->block));
793 /* rewire block input ... */
797 * Some problem here. For:
798 * if (x) y = 1; else y = 2;
800 * the following code is constructed:
802 * b0: if (x) goto b1; else goto b1;
805 * However, both predecessors of b1 are b0, making the Phi
808 * We solve this by fixing critical edges.
810 for (i = 0; i < n; ++i) {
811 ir_node *pred = ins[i];
817 cfop = get_irn_op(skip_Proj(pred));
818 if (is_op_fragile(cfop)) {
819 /* ignore exception flow */
822 if (is_op_forking(cfop)) {
823 /* a critical edge */
824 ir_node *block = new_r_Block(irg, 1, &ins[i]);
825 ir_node *jmp = new_r_Jmp(block);
831 set_irn_in(block, n, ins);
834 /* ... existing Phis ... */
835 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
836 set_irn_in(repr_phi->phi, n, repr_phi->ins);
837 DEL_ARR_F(repr_phi->ins);
840 /* ... and all inputs by creating new Phis ... */
841 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
842 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
843 ir_mode *mode = get_irn_mode(input);
844 ir_node *phi = new_r_Phi(block, n, repr_pair->ins, mode);
846 set_irn_n(repr_pair->irn, repr_pair->index, phi);
847 DEL_ARR_F(repr_pair->ins);
849 /* might be optimized away */
851 add_Block_phi(block, phi);
854 /* ... finally rewire the meet block and fix its Phi-nodes */
855 meet_block = part->meet_block;
856 n = get_Block_n_cfgpreds(meet_block);
858 ins = NEW_ARR_F(ir_node *, n);
861 for (p = get_Block_phis(meet_block); p != NULL; p = get_Phi_next(p)) {
865 phi_ins = NEW_ARR_F(ir_node *, n_phis * n);
867 for (i = j = 0; i < n; ++i) {
868 ir_node *pred = get_Block_cfgpred(meet_block, i);
870 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
871 if (bl->cf_root->node == pred)
876 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = get_Phi_next(p), ++k) {
877 phi_ins[k * n + j] = get_Phi_pred(p, i);
887 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = next, ++k) {
888 next = get_Phi_next(p);
890 exchange(p, phi_ins[k * n]);
892 /* all Phis killed */
893 set_Block_phis(meet_block, NULL);
895 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = next, ++k) {
896 next = get_Phi_next(p);
898 set_irn_in(p, j, &phi_ins[k * n]);
903 /* fix inputs of the meet block */
904 set_irn_in(meet_block, j, ins);
909 * Create a partition for a the end block.
911 * @param end_block the end block
912 * @param env the environment
914 static void partition_for_end_block(ir_node *end_block, environment_t *env)
916 partition_t *part = create_partition(end_block, env);
920 /* collect normal blocks */
921 for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
922 ir_node *pred = get_Block_cfgpred(end_block, i);
927 mark_irn_visited(pred);
929 block = get_nodes_block(pred);
930 bl = create_block(block, i, part, env);
931 node = create_node(pred, bl, env);
936 /* collect all no-return blocks */
937 end = get_irg_end(get_irn_irg(end_block));
938 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
939 ir_node *ka = get_End_keepalive(end, i);
946 mark_irn_visited(ka);
949 block = get_nodes_block(ka);
950 bl = create_block(block, -1, part, env);
951 node = create_node(ka, bl, env);
956 dump_partition("Created", part);
961 * Create a partition for a given meet block.
963 * @param block the meet block
964 * @param preds array of candidate predecessors
965 * @param n_preds number of elements in preds
966 * @param env the environment
968 static void partition_for_block(ir_node *block, pred_t preds[], int n_preds, environment_t *env)
970 partition_t *part = create_partition(block, env);
973 for (i = n_preds - 1; i >= 0; --i) {
974 ir_node *pred = preds[i].pred;
979 mark_irn_visited(pred);
981 block = get_nodes_block(pred);
982 bl = create_block(block, preds[i].index, part, env);
983 node = create_node(pred, bl, env);
988 dump_partition("Created", part);
992 * Walker: clear the links of all block phi lists and normal
995 static void clear_phi_links(ir_node *irn, void *env)
999 set_Block_phis(irn, NULL);
1000 set_irn_link(irn, NULL);
1005 * Walker, detect live-out nodes.
1007 static void find_liveouts(ir_node *irn, void *ctx)
1009 environment_t *env = (environment_t*)ctx;
1010 ir_node **live_outs = env->live_outs;
1011 ir_node *this_block;
1017 /* ignore Keep-alives */
1021 this_block = get_nodes_block(irn);
1024 /* update the Phi list */
1025 add_Block_phi(this_block, irn);
1028 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1029 ir_node *pred_block;
1030 ir_node *pred = get_irn_n(irn, i);
1031 int idx = get_irn_idx(pred);
1033 if (live_outs[idx] != NULL) {
1034 /* already marked as live-out */
1038 pred_block = get_nodes_block(pred);
1039 /* Phi nodes always refer to live-outs */
1040 if (is_Phi(irn) || this_block != pred_block) {
1041 /* pred is a live-out */
1042 live_outs[idx] = pred_block;
1048 * Check if the current block is the meet block of its predecessors.
1050 static void check_for_cf_meet(ir_node *block, void *ctx)
1052 environment_t *env = (environment_t*)ctx;
1056 if (block == get_irg_end_block(get_irn_irg(block))) {
1057 /* always create a partition for the end block */
1058 partition_for_end_block(block, env);
1062 n = get_Block_n_cfgpreds(block);
1064 /* Must have at least two predecessors */
1068 NEW_ARR_A(pred_t, preds, n);
1070 for (i = n - 1; i >= 0; --i) {
1071 ir_node *pred = get_Block_cfgpred(block, i);
1073 /* pred must be a direct jump to us */
1074 if (! is_Jmp(pred) && ! is_Raise(pred) && !is_Bad(pred))
1077 preds[k].pred = pred;
1083 partition_for_block(block, preds, k, env);
1087 * Compare two nodes for root ordering.
1089 static int cmp_nodes(const void *a, const void *b)
1091 const ir_node *const *pa = (const ir_node*const*)a;
1092 const ir_node *const *pb = (const ir_node*const*)b;
1093 const ir_node *irn_a = *pa;
1094 const ir_node *irn_b = *pb;
1095 unsigned code_a = get_irn_opcode(irn_a);
1096 unsigned code_b = get_irn_opcode(irn_b);
1097 ir_mode *mode_a, *mode_b;
1098 unsigned idx_a, idx_b;
1100 /* try opcode first */
1101 if (code_a != code_b)
1102 return code_a - code_b;
1105 mode_a = get_irn_mode(irn_a);
1106 mode_b = get_irn_mode(irn_b);
1108 if (mode_a != mode_b)
1109 return mode_a < mode_b ? -1 : +1;
1111 /* last resort: index */
1112 idx_a = get_irn_idx(irn_a);
1113 idx_b = get_irn_idx(irn_b);
1115 return (idx_a > idx_b) - (idx_a < idx_b);
1119 * Add the roots to all blocks.
1121 static void add_roots(ir_graph *irg, environment_t *env)
1123 unsigned idx, n = get_irg_last_idx(irg);
1124 ir_node **live_outs = env->live_outs;
1127 for (idx = 0; idx < n; ++idx) {
1128 ir_node *block = live_outs[idx];
1130 if (block != NULL && is_Block(block)) {
1131 block_t *bl = get_Block_entry(block);
1134 ir_node *irn = get_idx_irn(irg, idx);
1136 if (!irn_visited_else_mark(irn)) {
1137 ARR_APP1(ir_node *, bl->roots, irn);
1143 * Now sort the roots to normalize them as good as possible.
1144 * Else, we will split identical blocks if we start which different roots.
1146 for (bl = env->all_blocks; bl != NULL; bl = bl->all_next) {
1147 size_t i, n = ARR_LEN(bl->roots);
1149 /* TODO: is this really needed? The roots are already in
1150 idx-order by construction, which might be good enough. */
1151 qsort(bl->roots, n, sizeof(bl->roots[0]), cmp_nodes);
1153 DB((dbg, LEVEL_2, " Adding Roots for block %+F\n ", bl->block));
1154 /* ok, add them sorted */
1155 for (i = 0; i < n; ++i) {
1156 DB((dbg, LEVEL_2, "%+F, ", bl->roots[i]));
1157 create_node(bl->roots[i], bl, env);
1159 DB((dbg, LEVEL_2, "\n"));
1160 DEL_ARR_F(bl->roots);
1164 #endif /* GENERAL_SHAPE */
1166 /* Combines congruent end blocks into one. */
1167 void shape_blocks(ir_graph *irg)
1173 /* register a debug mask */
1174 FIRM_DBG_REGISTER(dbg, "firm.opt.blocks");
1176 DEBUG_ONLY(part_nr = 0;)
1177 DB((dbg, LEVEL_1, "Shaping blocks for %+F\n", irg));
1179 /* works better, when returns are placed at the end of the blocks */
1180 normalize_n_returns(irg);
1182 obstack_init(&env.obst);
1183 INIT_LIST_HEAD(&env.partitions);
1184 INIT_LIST_HEAD(&env.ready);
1185 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1187 n = get_irg_last_idx(irg);
1188 env.live_outs = NEW_ARR_FZ(ir_node*, n);
1190 env.all_blocks = NULL;
1192 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
1194 #ifdef GENERAL_SHAPE
1196 * Detect, which nodes are live-out only: these are the roots of our blocks.
1199 irg_walk_graph(irg, clear_phi_links, find_liveouts, &env);
1202 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1204 inc_irg_visited(irg);
1205 #ifdef GENERAL_SHAPE
1207 * Detect all control flow meets and create partitions.
1209 irg_block_walk_graph(irg, NULL, check_for_cf_meet, &env);
1211 /* add root nodes to the partition blocks */
1212 add_roots(irg, &env);
1214 partition_for_end_block(get_irg_end_block(irg), &env);
1217 propagate_live_troughs(&env);
1218 while (! list_empty(&env.partitions))
1221 res = !list_empty(&env.ready);
1222 //if (res) dump_ir_block_graph(irg, "-before");
1225 list_for_each_entry(partition_t, part, &env.ready, part_list) {
1226 dump_partition("Ready Partition", part);
1229 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
1232 /* control flow changed */
1233 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
1236 for (bl = env.all_blocks; bl != NULL; bl = bl->all_next) {
1237 DEL_ARR_F(bl->roots);
1240 DEL_ARR_F(env.live_outs);
1241 del_set(env.opcode2id_map);
1242 obstack_free(&env.obst, NULL);
1245 ir_graph_pass_t *shape_blocks_pass(const char *name)
1247 return def_graph_pass(name ? name : "shape_blocks", shape_blocks);