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.
27 * Two block are congruent, if they contains only equal calculations.
31 #include "iroptimize.h"
33 #include "irgraph_t.h"
43 /* define this for general block shaping: congruent blocks
44 are found not only before the end block but anywhere in the graph */
47 typedef struct partition_t partition_t;
48 typedef struct block_t block_t;
49 typedef struct node_t node_t;
50 typedef struct pair_t pair_t;
51 typedef struct phi_t phi_t;
52 typedef struct opcode_key_t opcode_key_t;
53 typedef struct listmap_entry_t listmap_entry_t;
54 typedef struct environment_t environment_t;
55 typedef struct pred_t pred_t;
57 /** An opcode map key. */
59 ir_opcode code; /**< The Firm opcode. */
60 ir_mode *mode; /**< The mode of all nodes in the partition. */
61 int arity; /**< The arity of this opcode (needed for Phi etc. */
63 long proj; /**< For Proj nodes, its proj number */
64 ir_entity *ent; /**< For Sel nodes, its entity */
65 tarval *tv; /**< For Const nodes, its tarval */
66 symconst_symbol sym; /**< For SymConst nodes, its symbol .*/
67 void *addr; /**< Alias all addresses. */
68 int intVal; /**< For Conv/Div nodes: strict/remainderless. */
72 /** A partition contains all congruent blocks. */
74 list_head part_list; /**< Double linked list of partitions. */
75 list_head blocks; /**< List of blocks in this partition. */
76 unsigned n_blocks; /**< Number of block in this partition. */
77 ir_node *meet_block; /**< The control flow meet block of this partition. */
79 unsigned nr; /**< For debugging: number of this partition. */
85 list_head block_list; /**< Double linked list of block inside a partition. */
86 list_head nodes; /**< Wait-queue of nodes that must be checked for congruence. */
87 block_t *next; /**< Next block of a split list. */
88 ir_node *block; /**< Pointer to the associated IR-node block. */
89 ir_node **roots; /**< An array of all root nodes. */
90 node_t *cf_root; /**< The control flow root node of this block. */
91 pair_t *input_pairs; /**< The list of inputs to this block. */
92 phi_t *phis; /**< The list of Phis in this block. */
93 block_t *all_next; /**< Links all created blocks. */
94 int meet_input; /**< Input number of this block in the meet-block. */
99 list_head node_list; /**< Double linked list of block inside a partition. */
100 ir_node *node; /**< Pointer to the associated IR-node or NULL for block inputs. */
101 char is_input; /**< Set if this node is an input from other block. */
104 /** The environment. */
105 struct environment_t {
106 list_head partitions; /**< list of partitions. */
107 list_head ready; /**< list of ready partitions. */
108 set *opcode2id_map; /**< The opcodeMode->id map. */
109 ir_node **live_outs; /**< Live out only nodes. */
110 block_t *all_blocks; /**< List of all created blocks. */
111 struct obstack obst; /** obstack for temporary data */
114 /** A (node, input index) pair. */
116 pair_t *next; /**< Points to the next pair entry. */
117 ir_node *irn; /**< The IR-node. */
118 int index; /**< An input index. */
119 ir_node **ins; /**< A new in array once allocated. */
122 /** A Phi, inputs pair. */
124 phi_t *next; /**< Points to the next Phi pair entry. */
125 ir_node *phi; /**< The Phi node. */
126 ir_node **ins; /**< A new in array once allocated. */
129 /** Describes a predecessor input. */
131 ir_node *pred; /**< The predecessor. */
132 int index; /**< Its input index. */
136 * An entry in the list_map.
138 struct listmap_entry_t {
139 void *id; /**< The id. */
140 block_t *list; /**< The associated list for this id. */
141 listmap_entry_t *next; /**< Link to the next entry in the map. */
144 /** We must map id's to lists. */
145 typedef struct listmap_t {
146 set *map; /**< Map id's to listmap_entry_t's */
147 listmap_entry_t *values; /**< List of all values in the map. */
150 #define get_Block_entry(block) ((block_t *)get_irn_link(block))
152 /** The debug module handle. */
153 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
155 /** Next partition number. */
156 DEBUG_ONLY(static unsigned part_nr = 0);
160 * Dump partition to output.
162 static void dump_partition(const char *msg, const partition_t *part) {
163 const block_t *block;
166 DB((dbg, LEVEL_2, " %s part%u (%u blocks) {\n ", msg, part->nr, part->n_blocks));
167 list_for_each_entry(block_t, block, &part->blocks, block_list) {
168 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", block->block));
171 DB((dbg, LEVEL_2, "\n }\n"));
172 } /* dump_partition */
177 static void dump_list(const char *msg, const block_t *block) {
181 DB((dbg, LEVEL_3, " %s = {\n ", msg));
182 for (p = block; p != NULL; p = p->next) {
183 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->block));
186 DB((dbg, LEVEL_3, "\n }\n"));
189 #define dump_partition(msg, part)
190 #define dump_list(msg, block)
194 * Compare two pointer values of a listmap.
196 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size) {
197 const listmap_entry_t *e1 = elt;
198 const listmap_entry_t *e2 = key;
201 return e1->id != e2->id;
202 } /* listmap_cmp_ptr */
205 * Initializes a listmap.
207 * @param map the listmap
209 static void listmap_init(listmap_t *map) {
210 map->map = new_set(listmap_cmp_ptr, 16);
215 * Terminates a listmap.
217 * @param map the listmap
219 static void listmap_term(listmap_t *map) {
224 * Return the associated listmap entry for a given id.
226 * @param map the listmap
227 * @param id the id to search for
229 * @return the associated listmap entry for the given id
231 static listmap_entry_t *listmap_find(listmap_t *map, void *id) {
232 listmap_entry_t key, *entry;
237 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
239 if (entry->list == NULL) {
240 /* a new entry, put into the list */
241 entry->next = map->values;
248 * Calculate the hash value for an opcode map entry.
250 * @param entry an opcode map entry
252 * @return a hash value for the given opcode map entry
254 static unsigned opcode_hash(const opcode_key_t *entry) {
255 /* assume long >= int */
256 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.addr) + entry->arity;
260 * Compare two entries in the opcode map.
262 static int cmp_opcode(const void *elt, const void *key, size_t size) {
263 const opcode_key_t *o1 = elt;
264 const opcode_key_t *o2 = key;
267 return o1->code != o2->code || o1->mode != o2->mode ||
268 o1->arity != o2->arity ||
269 o1->u.proj != o2->u.proj || o1->u.addr != o2->u.addr;
273 * Creates a new empty partition and put in on the
276 * @param meet_block the control flow meet block of this partition
277 * @param env the environment
279 static partition_t *create_partition(ir_node *meet_block, environment_t *env) {
280 partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
282 INIT_LIST_HEAD(&part->blocks);
283 part->meet_block = meet_block;
285 DEBUG_ONLY(part->nr = part_nr++);
286 list_add_tail(&part->part_list, &env->partitions);
288 } /* create_partition */
291 * Allocate a new block in the given partition.
293 * @param block the IR-node
294 * @param meet_input Input number of this block in the meet-block
295 * @param partition the partition to add to
296 * @param env the environment
298 static block_t *create_block(ir_node *block, int meet_input, partition_t *partition, environment_t *env) {
299 block_t *bl = obstack_alloc(&env->obst, sizeof(*bl));
301 set_irn_link(block, bl);
303 INIT_LIST_HEAD(&bl->nodes);
306 bl->roots = NEW_ARR_F(ir_node *, 0);
308 bl->input_pairs = NULL;
310 bl->meet_input = meet_input;
312 /* put it into the list of partition blocks */
313 list_add_tail(&bl->block_list, &partition->blocks);
314 ++partition->n_blocks;
316 /* put in into the list of all blocks */
317 bl->all_next = env->all_blocks;
318 env->all_blocks = bl;
324 * Allocate a new node and add it to a blocks wait queue.
326 * @param irn the IR-node
327 * @param block the block to add to
328 * @param env the environment
330 static node_t *create_node(ir_node *irn, block_t *block, environment_t *env) {
331 node_t *node = obstack_alloc(&env->obst, sizeof(*node));
336 list_add_tail(&node->node_list, &block->nodes);
342 * Add an input pair to a block.
344 * @param block the block
345 * @param irn the IR-node that has an block input
346 * @param idx the index of the block input in node's predecessors
347 * @param env the environment
349 static void add_pair(block_t *block, ir_node *irn, int idx, environment_t *env) {
350 pair_t *pair = obstack_alloc(&env->obst, sizeof(*pair));
352 pair->next = block->input_pairs;
357 block->input_pairs = pair;
361 * Add a Phi to a block.
363 * @param block the block
364 * @param phi the Phi node
365 * @param env the environment
367 static void add_phi(block_t *block, ir_node *phi, environment_t *env) {
368 phi_t *node = obstack_alloc(&env->obst, sizeof(*node));
370 node->next = block->phis;
378 * Creates an opcode from a node.
380 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.u.intVal = get_Conv_strict(irn);
414 key.mode = get_Load_mode(irn);
417 key.u.intVal = get_Div_no_remainder(irn);
420 key.u.intVal = get_Builtin_kind(irn);
426 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
431 * Split a partition by a local list.
433 * @param Z partition to split
434 * @param g a (non-empty) block list
435 * @param env the environment
437 * @return a new partition containing the nodes of g
439 static partition_t *split(partition_t *Z, block_t *g, environment_t *env) {
440 partition_t *Z_prime;
444 dump_partition("Splitting ", Z);
445 dump_list("by list ", g);
449 /* Remove g from Z. */
450 for (block = g; block != NULL; block = block->next) {
451 list_del(&block->block_list);
454 assert(n < Z->n_blocks);
457 /* Move g to a new partition, Z'. */
458 Z_prime = create_partition(Z->meet_block, env);
459 for (block = g; block != NULL; block = block->next) {
460 list_add_tail(&block->block_list, &Z_prime->blocks);
462 Z_prime->n_blocks = n;
464 dump_partition("Now ", Z);
465 dump_partition("Created new ", Z_prime);
470 * Return non-zero if pred should be tread as a input node.
472 static int is_input_node(ir_node *pred, ir_node *irn, int index) {
473 /* for now, do NOT turn direct calls into indirect one */
476 if (! is_SymConst_addr_ent(pred))
481 } /* is_input_node */
484 * Propagate nodes on all wait queues of the given partition.
486 * @param part the partition
487 * @param env the environment
489 static void propagate_blocks(partition_t *part, environment_t *env) {
490 block_t *ready_blocks = NULL;
491 unsigned n_ready = 0;
494 listmap_entry_t *iter;
496 DB((dbg, LEVEL_2, " Propagate blocks on part%u\n", part->nr));
498 /* Let map be an empty mapping from the range of Opcodes to (local) list of blocks. */
500 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
502 listmap_entry_t *entry;
505 if (list_empty(&bl->nodes)) {
506 bl->next = ready_blocks;
509 DB((dbg, LEVEL_2, " Block %+F completely processed\n", bl->block));
513 /* get the first node from the wait queue */
514 node = list_entry(bl->nodes.next, node_t, node_list);
515 list_del(&node->node_list);
517 /* put all not-visited predecessors to the wait queue */
518 if (! node->is_input) {
519 ir_node *irn = node->node;
522 DB((dbg, LEVEL_3, " propagate %+F\n", irn));
523 ir_normalize_node(node->node);
524 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
525 ir_node *pred = get_irn_n(irn, i);
526 ir_node *block = get_nodes_block(skip_Proj(pred));
529 if (block != bl->block) {
530 p_node = create_node(pred, bl, env);
531 if (is_input_node(pred, irn, i)) {
532 /* is a block live input */
533 p_node->is_input = 1;
535 add_pair(bl, irn, i, env);
536 } else if (is_Phi(pred)) {
537 /* update the Phi list */
538 add_phi(bl, pred, env);
540 } else if (! irn_visited_else_mark(pred)) {
541 /* not yet visited, ok */
542 p_node = create_node(pred, bl, env);
545 /* update the Phi list */
546 add_phi(bl, pred, env);
551 DB((dbg, LEVEL_3, " propagate Input %+F\n", node->node));
554 /* Add bl to map[opcode(n)]. */
555 id = opcode(node, env);
556 entry = listmap_find(&map, id);
557 bl->next = entry->list;
561 /* split out ready blocks */
565 if (n_ready < part->n_blocks)
566 Z = split(part, ready_blocks, env);
569 list_del(&Z->part_list);
571 if (Z->n_blocks > 1) {
572 DB((dbg, LEVEL_2, " Partition %u is ready\n", Z->nr));
573 list_add(&Z->part_list, &env->ready);
575 DB((dbg, LEVEL_2, " Partition %u contains only one block, killed\n", Z->nr));
579 /* for all sets S except one in the range of map do */
580 for (iter = map.values; iter != NULL; iter = iter->next) {
583 if (iter->next == NULL) {
584 /* this is the last entry, ignore */
589 /* Add SPLIT( X, S ) to P. */
593 } /* propagate_blocks */
596 * Propagate nodes on all wait queues.
598 * @param env the environment
600 static void propagate(environment_t *env) {
601 partition_t *part, *next;
603 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
604 if (part->n_blocks < 2) {
605 /* zero or one block left, kill this partition */
606 list_del(&part->part_list);
607 DB((dbg, LEVEL_2, " Partition %u contains less than 2 blocks, killed\n", part->nr));
609 propagate_blocks(part, env);
614 * Map a block to the phi[block->input] live-trough.
616 static void *live_throughs(const block_t *bl, const ir_node *phi) {
617 ir_node *input = get_Phi_pred(phi, bl->meet_input);
619 /* If this input is inside our block, this
620 is a live-out and not a live trough.
621 Live-outs are tested inside propagate, so map all of
622 them to the "general" value NULL */
623 if (get_nodes_block(input) == bl->block)
626 } /* live_throughs */
629 * Split partition by live-outs and live-troughs.
631 * @param part the partition
632 * @param env the environment
634 void propagate_blocks_live_troughs(partition_t *part, environment_t *env) {
635 const ir_node *meet_block = part->meet_block;
638 listmap_entry_t *iter;
641 DB((dbg, LEVEL_2, " Propagate live-troughs on part%u\n", part->nr));
643 for (phi = get_Block_phis(meet_block); phi != NULL; phi = get_Phi_next(phi)) {
644 /* propagate on all Phis of the meet-block */
646 if (part->n_blocks < 2) {
647 /* zero or one block left, kill this partition */
648 list_del(&part->part_list);
649 DB((dbg, LEVEL_2, " Partition %u contains less than 2 blocks, killed\n", part->nr));
653 /* Let map be an empty mapping from the range of live-troughs to (local) list of blocks. */
655 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
657 listmap_entry_t *entry;
659 /* Add bl to map[live_trough(bl)]. */
660 id = live_throughs(bl, phi);
661 entry = listmap_find(&map, id);
662 bl->next = entry->list;
666 /* for all sets S except one in the range of map do */
667 for (iter = map.values; iter != NULL; iter = iter->next) {
670 if (iter->next == NULL) {
671 /* this is the last entry, ignore */
676 /* Add SPLIT( X, S ) to P. */
681 } /* propagate_blocks_live_troughs */
684 * Propagate live-troughs on all partitions on the partition list.
686 * @param env the environment
688 void propagate_live_troughs(environment_t *env) {
689 partition_t *part, *next;
691 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
692 propagate_blocks_live_troughs(part, env);
694 } /* propagate_live_troughs */
697 * Apply analysis results by replacing all blocks of a partition
698 * by one representative.
700 * Route all inputs from all block of the partition to the one
702 * Enhance all existing Phis by combining them.
703 * Create new Phis for all previous input nodes.
705 * @param part the partition to process
707 static void apply(ir_graph *irg, partition_t *part) {
708 block_t *repr = list_entry(part->blocks.next, block_t, block_list);
710 ir_node *block, *end, *meet_block, *p, *next;
711 ir_node **ins, **phi_ins;
712 phi_t *repr_phi, *phi;
713 pair_t *repr_pair, *pair;
714 int i, j, k, n, block_nr, n_phis;
716 list_del(&repr->block_list);
718 /* prepare new in arrays for the block ... */
720 n = get_Block_n_cfgpreds(block);
721 ins = NEW_ARR_F(ir_node *, n);
723 for (i = 0; i < n; ++i) {
724 ins[i] = get_Block_cfgpred(block, i);
727 /* ... for all existing Phis ... */
728 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
729 repr_phi->ins = NEW_ARR_F(ir_node *, n);
731 for (i = 0; i < n; ++i)
732 repr_phi->ins[i] = get_Phi_pred(repr_phi->phi, i);
735 /* ... and all newly created Phis */
736 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
737 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
739 repr_pair->ins = NEW_ARR_F(ir_node *, n);
740 for (i = 0; i < n; ++i)
741 repr_pair->ins[i] = input;
744 DB((dbg, LEVEL_1, "Replacing "));
746 /* collect new in arrays */
747 end = get_irg_end(irg);
749 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
753 DB((dbg, LEVEL_1, "%+F, ", block));
755 /* first step: kill any keep-alive from this block */
756 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
757 ir_node *ka = get_End_keepalive(end, i);
761 remove_End_keepalive(end, ka);
763 if (get_nodes_block(ka) == block)
764 remove_End_keepalive(end, ka);
768 /* second step: update control flow */
769 n = get_Block_n_cfgpreds(block);
770 for (i = 0; i < n; ++i) {
771 ir_node *pred = get_Block_cfgpred(block, i);
772 ARR_APP1(ir_node *, ins, pred);
775 /* third step: update Phis */
776 for (repr_phi = repr->phis, phi = bl->phis;
778 repr_phi = repr_phi->next, phi = phi->next) {
779 for (i = 0; i < n; ++i) {
780 ir_node *pred = get_Phi_pred(phi->phi, i);
781 ARR_APP1(ir_node *, repr_phi->ins, pred);
785 /* fourth step: update inputs for new Phis */
786 for (repr_pair = repr->input_pairs, pair = bl->input_pairs;
788 repr_pair = repr_pair->next, pair = pair->next) {
789 ir_node *input = get_irn_n(pair->irn, pair->index);
791 for (i = 0; i < n; ++i)
792 ARR_APP1(ir_node *, repr_pair->ins, input);
796 DB((dbg, LEVEL_1, "by %+F\n", repr->block));
798 /* rewire block input ... */
802 * Some problem here. For:
803 * if (x) y = 1; else y = 2;
805 * the following code is constructed:
807 * b0: if (x) goto b1; else goto b1;
810 * However, both predecessors of b1 are b0, making the Phi
813 * We solve this by fixing critical edges.
815 for (i = 0; i < n; ++i) {
816 ir_node *pred = ins[i];
822 cfop = get_irn_op(skip_Proj(pred));
823 if (is_op_fragile(cfop)) {
824 /* ignore exception flow */
827 if (is_op_forking(cfop)) {
828 /* a critical edge */
829 ir_node *block = new_r_Block(irg, 1, &ins[i]);
830 ir_node *jmp = new_r_Jmp(block);
836 set_irn_in(block, n, ins);
839 /* ... existing Phis ... */
840 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
841 set_irn_in(repr_phi->phi, n, repr_phi->ins);
842 DEL_ARR_F(repr_phi->ins);
845 /* ... and all inputs by creating new Phis ... */
846 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
847 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
848 ir_mode *mode = get_irn_mode(input);
849 ir_node *phi = new_r_Phi(block, n, repr_pair->ins, mode);
851 set_irn_n(repr_pair->irn, repr_pair->index, phi);
852 DEL_ARR_F(repr_pair->ins);
854 /* might be optimized away */
856 add_Block_phi(block, phi);
859 /* ... finally rewire the meet block and fix its Phi-nodes */
860 meet_block = part->meet_block;
861 n = get_Block_n_cfgpreds(meet_block);
863 ins = NEW_ARR_F(ir_node *, n);
866 for (p = get_Block_phis(meet_block); p != NULL; p = get_Phi_next(p)) {
870 phi_ins = NEW_ARR_F(ir_node *, n_phis * n);
872 for (i = j = 0; i < n; ++i) {
873 ir_node *pred = get_Block_cfgpred(meet_block, i);
875 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
876 if (bl->cf_root->node == pred)
881 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = get_Phi_next(p), ++k) {
882 phi_ins[k * n + j] = get_Phi_pred(p, i);
892 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = next, ++k) {
893 next = get_Phi_next(p);
895 exchange(p, phi_ins[k * n]);
897 /* all Phis killed */
898 set_Block_phis(meet_block, NULL);
900 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = next, ++k) {
901 next = get_Phi_next(p);
903 set_irn_in(p, j, &phi_ins[k * n]);
908 /* fix inputs of the meet block */
909 set_irn_in(meet_block, j, ins);
914 * Create a partition for a the end block.
916 * @param end_block the end block
917 * @param env the environment
919 static void partition_for_end_block(ir_node *end_block, environment_t *env) {
920 partition_t *part = create_partition(end_block, env);
924 /* collect normal blocks */
925 for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
926 ir_node *pred = get_Block_cfgpred(end_block, i);
931 mark_irn_visited(pred);
933 block = get_nodes_block(pred);
934 bl = create_block(block, i, part, env);
935 node = create_node(pred, bl, env);
940 /* collect all no-return blocks */
941 end = get_irg_end(current_ir_graph);
942 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
943 ir_node *ka = get_End_keepalive(end, i);
950 mark_irn_visited(ka);
953 block = get_nodes_block(ka);
954 bl = create_block(block, -1, part, env);
955 node = create_node(ka, bl, env);
960 dump_partition("Created", part);
961 } /* partition_for_end_block */
965 * Create a partition for a given meet block.
967 * @param block the meet block
968 * @param preds array of candidate predecessors
969 * @param n_preds number of elements in preds
970 * @param env the environment
972 static void partition_for_block(ir_node *block, pred_t preds[], int n_preds, environment_t *env) {
973 partition_t *part = create_partition(block, env);
976 for (i = n_preds - 1; i >= 0; --i) {
977 ir_node *pred = preds[i].pred;
982 mark_irn_visited(pred);
984 block = get_nodes_block(pred);
985 bl = create_block(block, preds[i].index, part, env);
986 node = create_node(pred, bl, env);
991 dump_partition("Created", part);
992 } /* partition_for_block */
995 * Walker: clear the links of all block phi lists and normal
998 static void clear_phi_links(ir_node *irn, void *env) {
1000 if (is_Block(irn)) {
1001 set_Block_phis(irn, NULL);
1002 set_irn_link(irn, NULL);
1004 } /* clear_phi_links */
1007 * Walker, detect live-out nodes.
1009 static void find_liveouts(ir_node *irn, void *ctx) {
1010 environment_t *env = ctx;
1011 ir_node **live_outs = env->live_outs;
1012 ir_node *this_block;
1018 /* ignore Keep-alives */
1022 this_block = get_nodes_block(irn);
1025 /* update the Phi list */
1026 add_Block_phi(this_block, irn);
1029 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1030 ir_node *pred_block;
1031 ir_node *pred = get_irn_n(irn, i);
1032 int idx = get_irn_idx(pred);
1034 if (live_outs[idx] != NULL) {
1035 /* already marked as live-out */
1039 pred_block = get_nodes_block(pred);
1040 /* Phi nodes always refer to live-outs */
1041 if (is_Phi(irn) || this_block != pred_block) {
1042 /* pred is a live-out */
1043 live_outs[idx] = pred_block;
1046 } /* find_liveouts */
1049 * Check if the current block is the meet block of a its predecessors.
1051 static void check_for_cf_meet(ir_node *block, void *ctx) {
1052 environment_t *env = ctx;
1056 if (block == get_irg_end_block(current_ir_graph)) {
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);
1072 ir_node *pred_block;
1074 /* pred must be a direct jump to us */
1075 if (! is_Jmp(pred) && ! is_Raise(pred) && !is_Bad(pred))
1078 pred_block = get_nodes_block(skip_Proj(pred));
1080 preds[k].pred = pred;
1085 partition_for_block(block, preds, k, env);
1086 } /* check_for_cf_meet */
1089 * Compare two nodes for root ordering.
1091 static int cmp_nodes(const void *a, const void *b) {
1092 const ir_node *const *pa = a;
1093 const ir_node *const *pb = b;
1094 const ir_node *irn_a = *pa;
1095 const ir_node *irn_b = *pb;
1096 ir_opcode code_a = get_irn_opcode(irn_a);
1097 ir_opcode code_b = get_irn_opcode(irn_b);
1098 ir_mode *mode_a, *mode_b;
1099 unsigned idx_a, idx_b;
1101 /* try opcode first */
1102 if (code_a != code_b)
1103 return code_a - code_b;
1106 mode_a = get_irn_mode(irn_a);
1107 mode_b = get_irn_mode(irn_b);
1109 if (mode_a != mode_b)
1110 return mode_a < mode_b ? -1 : +1;
1112 /* last resort: index */
1113 idx_a = get_irn_idx(irn_a);
1114 idx_b = get_irn_idx(irn_b);
1116 return (idx_a > idx_b) - (idx_a < idx_b);
1120 * Add the roots to all blocks.
1122 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 int i, n = ARR_LEN(bl->roots);
1150 /* TODO: is this really needed? The roots are already in
1151 idx-order by construction, which might be good enough. */
1152 qsort(bl->roots, n, sizeof(bl->roots[0]), cmp_nodes);
1155 DB((dbg, LEVEL_2, " Adding Roots for block %+F\n ", bl->block));
1156 /* ok, add them sorted */
1157 for (i = 0; i < n; ++i) {
1158 DB((dbg, LEVEL_2, "%+F, ", bl->roots[i]));
1159 create_node(bl->roots[i], bl, env);
1161 DB((dbg, LEVEL_2, "\n"));
1162 DEL_ARR_F(bl->roots);
1166 #endif /* GENERAL_SHAPE */
1168 /* Combines congruent end blocks into one. */
1169 int shape_blocks(ir_graph *irg) {
1176 rem = current_ir_graph;
1177 current_ir_graph = irg;
1179 /* register a debug mask */
1180 FIRM_DBG_REGISTER(dbg, "firm.opt.blocks");
1182 DEBUG_ONLY(part_nr = 0);
1183 DB((dbg, LEVEL_1, "Shaping blocks for %+F\n", irg));
1185 /* works better, when returns are placed at the end of the blocks */
1186 normalize_n_returns(irg);
1188 obstack_init(&env.obst);
1189 INIT_LIST_HEAD(&env.partitions);
1190 INIT_LIST_HEAD(&env.ready);
1191 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1193 n = get_irg_last_idx(irg);
1194 env.live_outs = NEW_ARR_F(ir_node *, n);
1195 memset(env.live_outs, 0, sizeof(*env.live_outs) * n);
1197 env.all_blocks = NULL;
1199 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
1201 #ifdef GENERAL_SHAPE
1203 * Detect, which nodes are live-out only: these are the roots of our blocks.
1206 irg_walk_graph(irg, clear_phi_links, find_liveouts, &env);
1209 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1211 inc_irg_visited(irg);
1212 #ifdef GENERAL_SHAPE
1214 * Detect all control flow meets and create partitions.
1216 irg_block_walk_graph(irg, NULL, check_for_cf_meet, &env);
1218 /* add root nodes to the partition blocks */
1219 add_roots(irg, &env);
1221 partition_for_end_block(get_irg_end_block(irg), &env);
1224 propagate_live_troughs(&env);
1225 while (! list_empty(&env.partitions))
1228 res = !list_empty(&env.ready);
1229 //if (res) dump_ir_block_graph(irg, "-before");
1232 list_for_each_entry(partition_t, part, &env.ready, part_list) {
1233 dump_partition("Ready Partition", part);
1236 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
1239 /* control flow changed */
1240 set_irg_outs_inconsistent(irg);
1241 set_irg_extblk_inconsistent(irg);
1242 set_irg_doms_inconsistent(irg);
1243 set_irg_loopinfo_inconsistent(irg);
1245 /* Calls might be removed. */
1246 set_trouts_inconsistent();
1248 // dump_ir_block_graph(irg, "-after");
1251 for (bl = env.all_blocks; bl != NULL; bl = bl->all_next) {
1252 DEL_ARR_F(bl->roots);
1255 DEL_ARR_F(env.live_outs);
1256 del_set(env.opcode2id_map);
1257 obstack_free(&env.obst, NULL);
1258 current_ir_graph = rem;
1261 } /* shape_blocks */
1263 ir_graph_pass_t *shape_blocks_pass(const char *name) {
1264 return def_graph_pass_ret(name ? name : "shape_blocks", shape_blocks);
1265 } /* shape_blocks_pass */