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"
34 #include "irgraph_t.h"
44 /* define this for general block shaping: congruent blocks
45 are found not only before the end block but anywhere in the graph */
48 typedef struct partition_t partition_t;
49 typedef struct block_t block_t;
50 typedef struct node_t node_t;
51 typedef struct pair_t pair_t;
52 typedef struct phi_t phi_t;
53 typedef struct opcode_key_t opcode_key_t;
54 typedef struct listmap_entry_t listmap_entry_t;
55 typedef struct environment_t environment_t;
56 typedef struct pred_t pred_t;
58 /** An opcode map key. */
60 ir_opcode code; /**< The Firm opcode. */
61 ir_mode *mode; /**< The mode of all nodes in the partition. */
62 int arity; /**< The arity of this opcode (needed for Phi etc. */
64 long proj; /**< For Proj nodes, its proj number */
65 ir_entity *ent; /**< For Sel nodes, its entity */
66 tarval *tv; /**< For Const nodes, its tarval */
67 symconst_symbol sym; /**< For SymConst nodes, its symbol .*/
68 void *addr; /**< Alias all addresses. */
69 int intVal; /**< For Conv/Div nodes: strict/remainderless. */
73 /** A partition contains all congruent blocks. */
75 list_head part_list; /**< Double linked list of partitions. */
76 list_head blocks; /**< List of blocks in this partition. */
77 unsigned n_blocks; /**< Number of block in this partition. */
78 ir_node *meet_block; /**< The control flow meet block of this partition. */
80 unsigned nr; /**< For debugging: number of this partition. */
86 list_head block_list; /**< Double linked list of block inside a partition. */
87 list_head nodes; /**< Wait-queue of nodes that must be checked for congruence. */
88 block_t *next; /**< Next block of a split list. */
89 ir_node *block; /**< Pointer to the associated IR-node block. */
90 ir_node **roots; /**< An array of all root nodes. */
91 node_t *cf_root; /**< The control flow root node of this block. */
92 pair_t *input_pairs; /**< The list of inputs to this block. */
93 phi_t *phis; /**< The list of Phis in this block. */
94 block_t *all_next; /**< Links all created blocks. */
95 int meet_input; /**< Input number of this block in the meet-block. */
100 list_head node_list; /**< Double linked list of block inside a partition. */
101 ir_node *node; /**< Pointer to the associated IR-node or NULL for block inputs. */
102 char is_input; /**< Set if this node is an input from other block. */
105 /** The environment. */
106 struct environment_t {
107 list_head partitions; /**< list of partitions. */
108 list_head ready; /**< list of ready partitions. */
109 set *opcode2id_map; /**< The opcodeMode->id map. */
110 ir_node **live_outs; /**< Live out only nodes. */
111 block_t *all_blocks; /**< List of all created blocks. */
112 struct obstack obst; /** obstack for temporary data */
115 /** A (node, input index) pair. */
117 pair_t *next; /**< Points to the next pair entry. */
118 ir_node *irn; /**< The IR-node. */
119 int index; /**< An input index. */
120 ir_node **ins; /**< A new in array once allocated. */
123 /** A Phi, inputs pair. */
125 phi_t *next; /**< Points to the next Phi pair entry. */
126 ir_node *phi; /**< The Phi node. */
127 ir_node **ins; /**< A new in array once allocated. */
130 /** Describes a predecessor input. */
132 ir_node *pred; /**< The predecessor. */
133 int index; /**< Its input index. */
137 * An entry in the list_map.
139 struct listmap_entry_t {
140 void *id; /**< The id. */
141 block_t *list; /**< The associated list for this id. */
142 listmap_entry_t *next; /**< Link to the next entry in the map. */
145 /** We must map id's to lists. */
146 typedef struct listmap_t {
147 set *map; /**< Map id's to listmap_entry_t's */
148 listmap_entry_t *values; /**< List of all values in the map. */
151 #define get_Block_entry(block) ((block_t *)get_irn_link(block))
153 /** The debug module handle. */
154 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
156 /** Next partition number. */
157 DEBUG_ONLY(static unsigned part_nr = 0);
161 * Dump partition to output.
163 static void dump_partition(const char *msg, const partition_t *part)
165 const block_t *block;
168 DB((dbg, LEVEL_2, " %s part%u (%u blocks) {\n ", msg, part->nr, part->n_blocks));
169 list_for_each_entry(block_t, block, &part->blocks, block_list) {
170 DB((dbg, LEVEL_2, "%s%+F", first ? "" : ", ", block->block));
173 DB((dbg, LEVEL_2, "\n }\n"));
174 } /* dump_partition */
179 static void dump_list(const char *msg, const block_t *block)
184 DB((dbg, LEVEL_3, " %s = {\n ", msg));
185 for (p = block; p != NULL; p = p->next) {
186 DB((dbg, LEVEL_3, "%s%+F", first ? "" : ", ", p->block));
189 DB((dbg, LEVEL_3, "\n }\n"));
192 #define dump_partition(msg, part)
193 #define dump_list(msg, block)
197 * Compare two pointer values of a listmap.
199 static int listmap_cmp_ptr(const void *elt, const void *key, size_t size)
201 const listmap_entry_t *e1 = elt;
202 const listmap_entry_t *e2 = key;
205 return e1->id != e2->id;
206 } /* listmap_cmp_ptr */
209 * Initializes a listmap.
211 * @param map the listmap
213 static void listmap_init(listmap_t *map)
215 map->map = new_set(listmap_cmp_ptr, 16);
220 * Terminates a listmap.
222 * @param map the listmap
224 static void listmap_term(listmap_t *map)
230 * Return the associated listmap entry for a given id.
232 * @param map the listmap
233 * @param id the id to search for
235 * @return the associated listmap entry for the given id
237 static listmap_entry_t *listmap_find(listmap_t *map, void *id)
239 listmap_entry_t key, *entry;
244 entry = set_insert(map->map, &key, sizeof(key), HASH_PTR(id));
246 if (entry->list == NULL) {
247 /* a new entry, put into the list */
248 entry->next = map->values;
255 * Calculate the hash value for an opcode map entry.
257 * @param entry an opcode map entry
259 * @return a hash value for the given opcode map entry
261 static unsigned opcode_hash(const opcode_key_t *entry)
263 /* assume long >= int */
264 return (entry->mode - (ir_mode *)0) * 9 + entry->code + entry->u.proj * 3 + HASH_PTR(entry->u.addr) + entry->arity;
268 * Compare two entries in the opcode map.
270 static int cmp_opcode(const void *elt, const void *key, size_t size)
272 const opcode_key_t *o1 = elt;
273 const opcode_key_t *o2 = key;
276 return o1->code != o2->code || o1->mode != o2->mode ||
277 o1->arity != o2->arity ||
278 o1->u.proj != o2->u.proj || o1->u.addr != o2->u.addr;
282 * Creates a new empty partition and put in on the
285 * @param meet_block the control flow meet block of this partition
286 * @param env the environment
288 static partition_t *create_partition(ir_node *meet_block, environment_t *env)
290 partition_t *part = OALLOC(&env->obst, partition_t);
292 INIT_LIST_HEAD(&part->blocks);
293 part->meet_block = meet_block;
295 DEBUG_ONLY(part->nr = part_nr++);
296 list_add_tail(&part->part_list, &env->partitions);
298 } /* create_partition */
301 * Allocate a new block in the given partition.
303 * @param block the IR-node
304 * @param meet_input Input number of this block in the meet-block
305 * @param partition the partition to add to
306 * @param env the environment
308 static block_t *create_block(ir_node *block, int meet_input, partition_t *partition, environment_t *env)
310 block_t *bl = OALLOC(&env->obst, block_t);
312 set_irn_link(block, bl);
314 INIT_LIST_HEAD(&bl->nodes);
317 bl->roots = NEW_ARR_F(ir_node *, 0);
319 bl->input_pairs = NULL;
321 bl->meet_input = meet_input;
323 /* put it into the list of partition blocks */
324 list_add_tail(&bl->block_list, &partition->blocks);
325 ++partition->n_blocks;
327 /* put in into the list of all blocks */
328 bl->all_next = env->all_blocks;
329 env->all_blocks = bl;
335 * Allocate a new node and add it to a blocks wait queue.
337 * @param irn the IR-node
338 * @param block the block to add to
339 * @param env the environment
341 static node_t *create_node(ir_node *irn, block_t *block, environment_t *env)
343 node_t *node = OALLOC(&env->obst, node_t);
348 list_add_tail(&node->node_list, &block->nodes);
354 * Add an input pair to a block.
356 * @param block the block
357 * @param irn the IR-node that has an block input
358 * @param idx the index of the block input in node's predecessors
359 * @param env the environment
361 static void add_pair(block_t *block, ir_node *irn, int idx, environment_t *env)
363 pair_t *pair = OALLOC(&env->obst, pair_t);
365 pair->next = block->input_pairs;
370 block->input_pairs = pair;
374 * Add a Phi to a block.
376 * @param block the block
377 * @param phi the Phi node
378 * @param env the environment
380 static void add_phi(block_t *block, ir_node *phi, environment_t *env)
382 phi_t *node = OALLOC(&env->obst, phi_t);
384 node->next = block->phis;
392 * Creates an opcode from a node.
394 static opcode_key_t *opcode(const node_t *node, environment_t *env)
396 opcode_key_t key, *entry;
397 ir_node *irn = node->node;
399 if (node->is_input) {
400 /* Node: as Block nodes are never propagated, it is safe to
401 use its code for "input" node */
402 key.code = iro_Block;
405 key.code = get_irn_opcode(irn);
406 key.arity = get_irn_arity(irn);
408 key.mode = get_irn_mode(node->node);
414 key.u.proj = get_Proj_proj(irn);
417 key.u.ent = get_Sel_entity(irn);
420 key.u.sym = get_SymConst_symbol(irn);
423 key.u.tv = get_Const_tarval(irn);
426 key.u.intVal = get_Conv_strict(irn);
429 key.mode = get_Load_mode(irn);
432 key.u.intVal = get_Div_no_remainder(irn);
435 key.u.intVal = get_Builtin_kind(irn);
441 entry = set_insert(env->opcode2id_map, &key, sizeof(key), opcode_hash(&key));
446 * Split a partition by a local list.
448 * @param Z partition to split
449 * @param g a (non-empty) block list
450 * @param env the environment
452 * @return a new partition containing the nodes of g
454 static partition_t *split(partition_t *Z, block_t *g, environment_t *env)
456 partition_t *Z_prime;
460 dump_partition("Splitting ", Z);
461 dump_list("by list ", g);
465 /* Remove g from Z. */
466 for (block = g; block != NULL; block = block->next) {
467 list_del(&block->block_list);
470 assert(n < Z->n_blocks);
473 /* Move g to a new partition, Z'. */
474 Z_prime = create_partition(Z->meet_block, env);
475 for (block = g; block != NULL; block = block->next) {
476 list_add_tail(&block->block_list, &Z_prime->blocks);
478 Z_prime->n_blocks = n;
480 dump_partition("Now ", Z);
481 dump_partition("Created new ", Z_prime);
486 * Return non-zero if pred should be tread as a input node.
488 static int is_input_node(ir_node *pred, ir_node *irn, int index)
490 /* for now, do NOT turn direct calls into indirect one */
493 if (! is_SymConst_addr_ent(pred))
498 } /* is_input_node */
501 * Propagate nodes on all wait queues of the given partition.
503 * @param part the partition
504 * @param env the environment
506 static void propagate_blocks(partition_t *part, environment_t *env)
508 block_t *ready_blocks = NULL;
509 unsigned n_ready = 0;
512 listmap_entry_t *iter;
514 DB((dbg, LEVEL_2, " Propagate blocks on part%u\n", part->nr));
516 /* Let map be an empty mapping from the range of Opcodes to (local) list of blocks. */
518 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
520 listmap_entry_t *entry;
523 if (list_empty(&bl->nodes)) {
524 bl->next = ready_blocks;
527 DB((dbg, LEVEL_2, " Block %+F completely processed\n", bl->block));
531 /* get the first node from the wait queue */
532 node = list_entry(bl->nodes.next, node_t, node_list);
533 list_del(&node->node_list);
535 /* put all not-visited predecessors to the wait queue */
536 if (! node->is_input) {
537 ir_node *irn = node->node;
540 DB((dbg, LEVEL_3, " propagate %+F\n", irn));
541 ir_normalize_node(node->node);
542 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
543 ir_node *pred = get_irn_n(irn, i);
544 ir_node *block = get_nodes_block(skip_Proj(pred));
547 if (block != bl->block) {
548 p_node = create_node(pred, bl, env);
549 if (is_input_node(pred, irn, i)) {
550 /* is a block live input */
551 p_node->is_input = 1;
553 add_pair(bl, irn, i, env);
554 } else if (is_Phi(pred)) {
555 /* update the Phi list */
556 add_phi(bl, pred, env);
558 } else if (! irn_visited_else_mark(pred)) {
559 /* not yet visited, ok */
560 p_node = create_node(pred, bl, env);
563 /* update the Phi list */
564 add_phi(bl, pred, env);
569 DB((dbg, LEVEL_3, " propagate Input %+F\n", node->node));
572 /* Add bl to map[opcode(n)]. */
573 id = opcode(node, env);
574 entry = listmap_find(&map, id);
575 bl->next = entry->list;
579 /* split out ready blocks */
583 if (n_ready < part->n_blocks)
584 Z = split(part, ready_blocks, env);
587 list_del(&Z->part_list);
589 if (Z->n_blocks > 1) {
590 DB((dbg, LEVEL_2, " Partition %u is ready\n", Z->nr));
591 list_add(&Z->part_list, &env->ready);
593 DB((dbg, LEVEL_2, " Partition %u contains only one block, killed\n", Z->nr));
597 /* for all sets S except one in the range of map do */
598 for (iter = map.values; iter != NULL; iter = iter->next) {
601 if (iter->next == NULL) {
602 /* this is the last entry, ignore */
607 /* Add SPLIT( X, S ) to P. */
611 } /* propagate_blocks */
614 * Propagate nodes on all wait queues.
616 * @param env the environment
618 static void propagate(environment_t *env)
620 partition_t *part, *next;
622 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
623 if (part->n_blocks < 2) {
624 /* zero or one block left, kill this partition */
625 list_del(&part->part_list);
626 DB((dbg, LEVEL_2, " Partition %u contains less than 2 blocks, killed\n", part->nr));
628 propagate_blocks(part, env);
633 * Map a block to the phi[block->input] live-trough.
635 static void *live_throughs(const block_t *bl, const ir_node *phi)
637 ir_node *input = get_Phi_pred(phi, bl->meet_input);
639 /* If this input is inside our block, this
640 is a live-out and not a live trough.
641 Live-outs are tested inside propagate, so map all of
642 them to the "general" value NULL */
643 if (get_nodes_block(input) == bl->block)
646 } /* live_throughs */
649 * Split partition by live-outs and live-troughs.
651 * @param part the partition
652 * @param env the environment
654 static void propagate_blocks_live_troughs(partition_t *part, environment_t *env)
656 const ir_node *meet_block = part->meet_block;
659 listmap_entry_t *iter;
662 DB((dbg, LEVEL_2, " Propagate live-troughs on part%u\n", part->nr));
664 for (phi = get_Block_phis(meet_block); phi != NULL; phi = get_Phi_next(phi)) {
665 /* propagate on all Phis of the meet-block */
667 if (part->n_blocks < 2) {
668 /* zero or one block left, kill this partition */
669 list_del(&part->part_list);
670 DB((dbg, LEVEL_2, " Partition %u contains less than 2 blocks, killed\n", part->nr));
674 /* Let map be an empty mapping from the range of live-troughs to (local) list of blocks. */
676 list_for_each_entry_safe(block_t, bl, next, &part->blocks, block_list) {
678 listmap_entry_t *entry;
680 /* Add bl to map[live_trough(bl)]. */
681 id = live_throughs(bl, phi);
682 entry = listmap_find(&map, id);
683 bl->next = entry->list;
687 /* for all sets S except one in the range of map do */
688 for (iter = map.values; iter != NULL; iter = iter->next) {
691 if (iter->next == NULL) {
692 /* this is the last entry, ignore */
697 /* Add SPLIT( X, S ) to P. */
702 } /* propagate_blocks_live_troughs */
705 * Propagate live-troughs on all partitions on the partition list.
707 * @param env the environment
709 static void propagate_live_troughs(environment_t *env)
711 partition_t *part, *next;
713 list_for_each_entry_safe(partition_t, part, next, &env->partitions, part_list) {
714 propagate_blocks_live_troughs(part, env);
716 } /* propagate_live_troughs */
719 * Apply analysis results by replacing all blocks of a partition
720 * by one representative.
722 * Route all inputs from all block of the partition to the one
724 * Enhance all existing Phis by combining them.
725 * Create new Phis for all previous input nodes.
727 * @param part the partition to process
729 static void apply(ir_graph *irg, partition_t *part)
731 block_t *repr = list_entry(part->blocks.next, block_t, block_list);
733 ir_node *block, *end, *meet_block, *p, *next;
734 ir_node **ins, **phi_ins;
735 phi_t *repr_phi, *phi;
736 pair_t *repr_pair, *pair;
737 int i, j, k, n, block_nr, n_phis;
739 list_del(&repr->block_list);
741 /* prepare new in arrays for the block ... */
743 n = get_Block_n_cfgpreds(block);
744 ins = NEW_ARR_F(ir_node *, n);
746 for (i = 0; i < n; ++i) {
747 ins[i] = get_Block_cfgpred(block, i);
750 /* ... for all existing Phis ... */
751 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
752 repr_phi->ins = NEW_ARR_F(ir_node *, n);
754 for (i = 0; i < n; ++i)
755 repr_phi->ins[i] = get_Phi_pred(repr_phi->phi, i);
758 /* ... and all newly created Phis */
759 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
760 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
762 repr_pair->ins = NEW_ARR_F(ir_node *, n);
763 for (i = 0; i < n; ++i)
764 repr_pair->ins[i] = input;
767 DB((dbg, LEVEL_1, "Replacing "));
769 /* collect new in arrays */
770 end = get_irg_end(irg);
772 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
776 DB((dbg, LEVEL_1, "%+F, ", block));
778 /* first step: kill any keep-alive from this block */
779 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
780 ir_node *ka = get_End_keepalive(end, i);
784 remove_End_keepalive(end, ka);
786 if (get_nodes_block(ka) == block)
787 remove_End_keepalive(end, ka);
791 /* second step: update control flow */
792 n = get_Block_n_cfgpreds(block);
793 for (i = 0; i < n; ++i) {
794 ir_node *pred = get_Block_cfgpred(block, i);
795 ARR_APP1(ir_node *, ins, pred);
798 /* third step: update Phis */
799 for (repr_phi = repr->phis, phi = bl->phis;
801 repr_phi = repr_phi->next, phi = phi->next) {
802 for (i = 0; i < n; ++i) {
803 ir_node *pred = get_Phi_pred(phi->phi, i);
804 ARR_APP1(ir_node *, repr_phi->ins, pred);
808 /* fourth step: update inputs for new Phis */
809 for (repr_pair = repr->input_pairs, pair = bl->input_pairs;
811 repr_pair = repr_pair->next, pair = pair->next) {
812 ir_node *input = get_irn_n(pair->irn, pair->index);
814 for (i = 0; i < n; ++i)
815 ARR_APP1(ir_node *, repr_pair->ins, input);
819 DB((dbg, LEVEL_1, "by %+F\n", repr->block));
821 /* rewire block input ... */
825 * Some problem here. For:
826 * if (x) y = 1; else y = 2;
828 * the following code is constructed:
830 * b0: if (x) goto b1; else goto b1;
833 * However, both predecessors of b1 are b0, making the Phi
836 * We solve this by fixing critical edges.
838 for (i = 0; i < n; ++i) {
839 ir_node *pred = ins[i];
845 cfop = get_irn_op(skip_Proj(pred));
846 if (is_op_fragile(cfop)) {
847 /* ignore exception flow */
850 if (is_op_forking(cfop)) {
851 /* a critical edge */
852 ir_node *block = new_r_Block(irg, 1, &ins[i]);
853 ir_node *jmp = new_r_Jmp(block);
859 set_irn_in(block, n, ins);
862 /* ... existing Phis ... */
863 for (repr_phi = repr->phis; repr_phi != NULL; repr_phi = repr_phi->next) {
864 set_irn_in(repr_phi->phi, n, repr_phi->ins);
865 DEL_ARR_F(repr_phi->ins);
868 /* ... and all inputs by creating new Phis ... */
869 for (repr_pair = repr->input_pairs; repr_pair != NULL; repr_pair = repr_pair->next) {
870 ir_node *input = get_irn_n(repr_pair->irn, repr_pair->index);
871 ir_mode *mode = get_irn_mode(input);
872 ir_node *phi = new_r_Phi(block, n, repr_pair->ins, mode);
874 set_irn_n(repr_pair->irn, repr_pair->index, phi);
875 DEL_ARR_F(repr_pair->ins);
877 /* might be optimized away */
879 add_Block_phi(block, phi);
882 /* ... finally rewire the meet block and fix its Phi-nodes */
883 meet_block = part->meet_block;
884 n = get_Block_n_cfgpreds(meet_block);
886 ins = NEW_ARR_F(ir_node *, n);
889 for (p = get_Block_phis(meet_block); p != NULL; p = get_Phi_next(p)) {
893 phi_ins = NEW_ARR_F(ir_node *, n_phis * n);
895 for (i = j = 0; i < n; ++i) {
896 ir_node *pred = get_Block_cfgpred(meet_block, i);
898 list_for_each_entry(block_t, bl, &part->blocks, block_list) {
899 if (bl->cf_root->node == pred)
904 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = get_Phi_next(p), ++k) {
905 phi_ins[k * n + j] = get_Phi_pred(p, i);
915 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = next, ++k) {
916 next = get_Phi_next(p);
918 exchange(p, phi_ins[k * n]);
920 /* all Phis killed */
921 set_Block_phis(meet_block, NULL);
923 for (k = 0, p = get_Block_phis(meet_block); p != NULL; p = next, ++k) {
924 next = get_Phi_next(p);
926 set_irn_in(p, j, &phi_ins[k * n]);
931 /* fix inputs of the meet block */
932 set_irn_in(meet_block, j, ins);
937 * Create a partition for a the end block.
939 * @param end_block the end block
940 * @param env the environment
942 static void partition_for_end_block(ir_node *end_block, environment_t *env)
944 partition_t *part = create_partition(end_block, env);
948 /* collect normal blocks */
949 for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
950 ir_node *pred = get_Block_cfgpred(end_block, i);
955 mark_irn_visited(pred);
957 block = get_nodes_block(pred);
958 bl = create_block(block, i, part, env);
959 node = create_node(pred, bl, env);
964 /* collect all no-return blocks */
965 end = get_irg_end(get_irn_irg(end_block));
966 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
967 ir_node *ka = get_End_keepalive(end, i);
974 mark_irn_visited(ka);
977 block = get_nodes_block(ka);
978 bl = create_block(block, -1, part, env);
979 node = create_node(ka, bl, env);
984 dump_partition("Created", part);
985 } /* partition_for_end_block */
989 * Create a partition for a given meet block.
991 * @param block the meet block
992 * @param preds array of candidate predecessors
993 * @param n_preds number of elements in preds
994 * @param env the environment
996 static void partition_for_block(ir_node *block, pred_t preds[], int n_preds, environment_t *env)
998 partition_t *part = create_partition(block, env);
1001 for (i = n_preds - 1; i >= 0; --i) {
1002 ir_node *pred = preds[i].pred;
1007 mark_irn_visited(pred);
1009 block = get_nodes_block(pred);
1010 bl = create_block(block, preds[i].index, part, env);
1011 node = create_node(pred, bl, env);
1016 dump_partition("Created", part);
1017 } /* partition_for_block */
1020 * Walker: clear the links of all block phi lists and normal
1023 static void clear_phi_links(ir_node *irn, void *env)
1026 if (is_Block(irn)) {
1027 set_Block_phis(irn, NULL);
1028 set_irn_link(irn, NULL);
1030 } /* clear_phi_links */
1033 * Walker, detect live-out nodes.
1035 static void find_liveouts(ir_node *irn, void *ctx)
1037 environment_t *env = ctx;
1038 ir_node **live_outs = env->live_outs;
1039 ir_node *this_block;
1045 /* ignore Keep-alives */
1049 this_block = get_nodes_block(irn);
1052 /* update the Phi list */
1053 add_Block_phi(this_block, irn);
1056 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
1057 ir_node *pred_block;
1058 ir_node *pred = get_irn_n(irn, i);
1059 int idx = get_irn_idx(pred);
1061 if (live_outs[idx] != NULL) {
1062 /* already marked as live-out */
1066 pred_block = get_nodes_block(pred);
1067 /* Phi nodes always refer to live-outs */
1068 if (is_Phi(irn) || this_block != pred_block) {
1069 /* pred is a live-out */
1070 live_outs[idx] = pred_block;
1073 } /* find_liveouts */
1076 * Check if the current block is the meet block of a its predecessors.
1078 static void check_for_cf_meet(ir_node *block, void *ctx)
1080 environment_t *env = ctx;
1084 if (block == get_irg_end_block(get_irn_irg(block))) {
1085 /* always create a partition for the end block */
1086 partition_for_end_block(block, env);
1090 n = get_Block_n_cfgpreds(block);
1092 /* Must have at least two predecessors */
1096 NEW_ARR_A(pred_t, preds, n);
1098 for (i = n - 1; i >= 0; --i) {
1099 ir_node *pred = get_Block_cfgpred(block, i);
1100 ir_node *pred_block;
1102 /* pred must be a direct jump to us */
1103 if (! is_Jmp(pred) && ! is_Raise(pred) && !is_Bad(pred))
1106 pred_block = get_nodes_block(skip_Proj(pred));
1108 preds[k].pred = pred;
1113 partition_for_block(block, preds, k, env);
1114 } /* check_for_cf_meet */
1117 * Compare two nodes for root ordering.
1119 static int cmp_nodes(const void *a, const void *b)
1121 const ir_node *const *pa = a;
1122 const ir_node *const *pb = b;
1123 const ir_node *irn_a = *pa;
1124 const ir_node *irn_b = *pb;
1125 ir_opcode code_a = get_irn_opcode(irn_a);
1126 ir_opcode code_b = get_irn_opcode(irn_b);
1127 ir_mode *mode_a, *mode_b;
1128 unsigned idx_a, idx_b;
1130 /* try opcode first */
1131 if (code_a != code_b)
1132 return code_a - code_b;
1135 mode_a = get_irn_mode(irn_a);
1136 mode_b = get_irn_mode(irn_b);
1138 if (mode_a != mode_b)
1139 return mode_a < mode_b ? -1 : +1;
1141 /* last resort: index */
1142 idx_a = get_irn_idx(irn_a);
1143 idx_b = get_irn_idx(irn_b);
1145 return (idx_a > idx_b) - (idx_a < idx_b);
1149 * Add the roots to all blocks.
1151 static void add_roots(ir_graph *irg, environment_t *env)
1153 unsigned idx, n = get_irg_last_idx(irg);
1154 ir_node **live_outs = env->live_outs;
1157 for (idx = 0; idx < n; ++idx) {
1158 ir_node *block = live_outs[idx];
1160 if (block != NULL && is_Block(block)) {
1161 block_t *bl = get_Block_entry(block);
1164 ir_node *irn = get_idx_irn(irg, idx);
1166 if (!irn_visited_else_mark(irn)) {
1167 ARR_APP1(ir_node *, bl->roots, irn);
1173 * Now sort the roots to normalize them as good as possible.
1174 * Else, we will split identical blocks if we start which different roots.
1176 for (bl = env->all_blocks; bl != NULL; bl = bl->all_next) {
1177 int i, n = ARR_LEN(bl->roots);
1180 /* TODO: is this really needed? The roots are already in
1181 idx-order by construction, which might be good enough. */
1182 qsort(bl->roots, n, sizeof(bl->roots[0]), cmp_nodes);
1185 DB((dbg, LEVEL_2, " Adding Roots for block %+F\n ", bl->block));
1186 /* ok, add them sorted */
1187 for (i = 0; i < n; ++i) {
1188 DB((dbg, LEVEL_2, "%+F, ", bl->roots[i]));
1189 create_node(bl->roots[i], bl, env);
1191 DB((dbg, LEVEL_2, "\n"));
1192 DEL_ARR_F(bl->roots);
1196 #endif /* GENERAL_SHAPE */
1198 /* Combines congruent end blocks into one. */
1199 int shape_blocks(ir_graph *irg)
1206 /* register a debug mask */
1207 FIRM_DBG_REGISTER(dbg, "firm.opt.blocks");
1209 DEBUG_ONLY(part_nr = 0);
1210 DB((dbg, LEVEL_1, "Shaping blocks for %+F\n", irg));
1212 /* works better, when returns are placed at the end of the blocks */
1213 normalize_n_returns(irg);
1215 obstack_init(&env.obst);
1216 INIT_LIST_HEAD(&env.partitions);
1217 INIT_LIST_HEAD(&env.ready);
1218 env.opcode2id_map = new_set(cmp_opcode, iro_Last * 4);
1220 n = get_irg_last_idx(irg);
1221 env.live_outs = NEW_ARR_F(ir_node *, n);
1222 memset(env.live_outs, 0, sizeof(*env.live_outs) * n);
1224 env.all_blocks = NULL;
1226 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
1228 #ifdef GENERAL_SHAPE
1230 * Detect, which nodes are live-out only: these are the roots of our blocks.
1233 irg_walk_graph(irg, clear_phi_links, find_liveouts, &env);
1236 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1238 inc_irg_visited(irg);
1239 #ifdef GENERAL_SHAPE
1241 * Detect all control flow meets and create partitions.
1243 irg_block_walk_graph(irg, NULL, check_for_cf_meet, &env);
1245 /* add root nodes to the partition blocks */
1246 add_roots(irg, &env);
1248 partition_for_end_block(get_irg_end_block(irg), &env);
1251 propagate_live_troughs(&env);
1252 while (! list_empty(&env.partitions))
1255 res = !list_empty(&env.ready);
1256 //if (res) dump_ir_block_graph(irg, "-before");
1259 list_for_each_entry(partition_t, part, &env.ready, part_list) {
1260 dump_partition("Ready Partition", part);
1263 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
1266 /* control flow changed */
1267 set_irg_outs_inconsistent(irg);
1268 set_irg_extblk_inconsistent(irg);
1269 set_irg_doms_inconsistent(irg);
1270 set_irg_loopinfo_inconsistent(irg);
1272 /* Calls might be removed. */
1273 set_trouts_inconsistent();
1276 for (bl = env.all_blocks; bl != NULL; bl = bl->all_next) {
1277 DEL_ARR_F(bl->roots);
1280 DEL_ARR_F(env.live_outs);
1281 del_set(env.opcode2id_map);
1282 obstack_free(&env.obst, NULL);
1285 } /* shape_blocks */
1287 ir_graph_pass_t *shape_blocks_pass(const char *name)
1289 return def_graph_pass_ret(name ? name : "shape_blocks", shape_blocks);
1290 } /* shape_blocks_pass */