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 Structure Analysis
23 * @author Michael Beck
29 #include "firm_common.h"
31 #include "structure.h"
41 typedef union ir_reg_or_blk ir_reg_or_blk;
43 /* The structure tree. */
45 struct obstack obst; /**< The obstack where the data is allocated. */
46 ir_region *top; /**< The top region. */
47 ir_graph *irg; /**< Associated graph. */
52 firm_kind kind; /**< Must be k_ir_region. */
53 ir_region_kind type; /**< The type of this region. */
54 ir_region *parent; /**< points to the parent. */
55 ir_reg_or_blk *parts; /**< The list of all region parts. */
56 ir_region **pred; /**< The predecessor (control flow) regions of this region. */
57 ir_region **succ; /**< The successor (control flow) regions of this region. */
58 unsigned prenum; /**< DFS pre-oder number */
59 unsigned postnum; /**< DFS post-oder number */
60 void *link; /**< A link field. */
61 unsigned long nr; /**< for debugging */
62 unsigned visited:1; /**< The visited flag. */
63 unsigned exit:1; /**< If set, the parent region can be left by this node. */
64 unsigned enter:1; /**< If set, the parent region can be entered by this node. */
67 /* A helper type for unioning blocks and regions. */
69 firm_kind *kind; /**< For easier check. */
70 ir_node *blk; /**< A node */
71 ir_region *region; /**< A region. */
74 /* The debug handle. */
75 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
78 * Returns the link of a region.
80 void *get_region_link(const ir_region *reg) {
85 * Sets the link of a region.
87 void set_region_link(ir_region *reg, void *data) {
92 * Get the immediate region of a block.
94 ir_region *get_block_region(const ir_node *block) {
95 assert(is_Block(block));
96 return block->attr.block.region;
100 * Sets the immediate region of a block.
102 void set_block_region(ir_node *block, ir_region *reg) {
103 assert(is_Block(block));
104 block->attr.block.region = reg;
108 * Get the immediate region of a node.
110 ir_region *get_irn_region(ir_node *n) {
112 n = get_nodes_block(n);
113 return get_block_region(n);
117 * Return non-zero if a given firm thing is a region.
119 int is_region(const void *thing) {
120 const firm_kind *kind = thing;
121 return *kind == k_ir_region;
125 * Return the number of predecessors of a region.
127 int get_region_n_preds(const ir_region *reg) {
128 return ARR_LEN(reg->pred);
132 * Return the predecessor region at position pos.
134 ir_region *get_region_pred(const ir_region *reg, int pos) {
135 assert(0 <= pos && pos <= get_region_n_preds(reg));
136 return reg->pred[pos];
140 * Set the predecessor region at position pos.
142 void set_region_pred(ir_region *reg, int pos, ir_region *n) {
143 assert(0 <= pos && pos <= get_region_n_preds(reg));
148 * Return the number of successors in a region.
150 int get_region_n_succs(const ir_region *reg) {
151 return ARR_LEN(reg->succ);
155 * Return the successor region at position pos.
157 ir_region *get_region_succ(const ir_region *reg, int pos) {
158 assert(0 <= pos && pos <= get_region_n_succs(reg));
159 return reg->succ[pos];
163 * Set the successor region at position pos.
165 void set_region_succ(ir_region *reg, int pos, ir_region *n) {
166 assert(0 <= pos && pos <= get_region_n_succs(reg));
170 /* ----------------------- construction -------------------------- */
172 /** Walker environment. */
173 typedef struct walk_env {
174 struct obstack *obst; /**< An obstack to allocate from. */
175 ir_region **post; /**< The list of all currently existent top regions. */
176 unsigned l_post; /**< The length of the allocated regions array. */
177 unsigned premax; /**< maximum pre counter */
178 unsigned postmax; /**< maximum post counter */
179 ir_node *start_block; /**< The start block of the graph. */
180 ir_node *end_block; /**< The end block of the graph. */
184 * Do a DFS search on the initial regions, assign a prenum and a postnum to every
185 * node and store the region nodes into the post array.
187 static void dfs_walk2(ir_region *reg, walk_env *env) {
190 if (reg->visited == 0) {
193 reg->prenum = env->premax++;
194 for (i = 0, n = get_region_n_succs(reg); i < n; ++i) {
196 ir_region *succ = get_region_succ(reg, i);
197 dfs_walk2(succ, env);
200 env->post[env->postmax] = reg;
201 reg->postnum = env->postmax++;
206 * Do a DFS search on the initial regions, assign a prenum and a postnum to every
207 * node and store the region nodes into the post array.
209 static void dfs_walk(ir_graph *irg, walk_env *env) {
210 ir_graph *rem = current_ir_graph;
213 current_ir_graph = irg;
214 reg = get_irn_link(get_irg_start_block(irg));
219 current_ir_graph = rem;
223 * Post-walker: wrap all blocks with a BasicBlock region
226 static void wrap_BasicBlocks(ir_node *block, void *ctx) {
230 /* Allocate a Block wrapper */
231 reg = OALLOC(env->obst, ir_region);
232 reg->kind = k_ir_region;
233 reg->type = ir_rk_BasicBlock;
241 reg->nr = get_irn_node_nr(block);
242 reg->parts = NEW_ARR_D(ir_reg_or_blk, env->obst, 1);
244 reg->parts[0].blk = block;
245 set_irn_link(block, reg);
248 } /* wrap_BasicBlocks */
251 * Post-walker: Create the pred and succ edges for Block wrapper.
252 * Kill edges to the Start and End blocks.
254 static void update_BasicBlock_regions(ir_node *blk, void *ctx) {
256 ir_region *reg = get_irn_link(blk);
259 if (blk == env->start_block) {
260 /* handle Firm's self loop: Start block has no predecessors */
261 reg->pred = NEW_ARR_D(ir_region *, env->obst, 0);
263 len = get_Block_n_cfgpreds(blk);
264 reg->pred = NEW_ARR_D(ir_region *, env->obst, len);
265 for (i = j = 0; i < len; ++i) {
266 ir_node *pred = get_Block_cfgpred_block(blk, i);
267 reg->pred[j++] = get_irn_link(pred);
269 ARR_SHRINKLEN(reg->pred, j);
272 len = get_Block_n_cfg_outs(blk);
273 reg->succ = NEW_ARR_D(ir_region *, env->obst, len);
274 for (i = j = 0; i < len; ++i) {
275 ir_node *succ = get_Block_cfg_out(blk, i);
276 reg->succ[j++] = get_irn_link(succ);
278 ARR_SHRINKLEN(reg->succ, j);
279 } /* update_BasicBlock_regions */
281 /** Allocate a new region on an obstack */
282 #define ALLOC_REG(obst, reg, tp) \
284 (reg) = OALLOC((obst), ir_region); \
285 (reg)->kind = k_ir_region; \
287 (reg)->parent = NULL; \
289 (reg)->postnum = 0; \
290 (reg)->visited = 0; \
293 (reg)->link = NULL; \
297 * Creates a new Sequence region.
299 static ir_region *new_Sequence(struct obstack *obst, ir_region *nset, int nset_len) {
300 ir_region *reg, *next;
303 ALLOC_REG(obst, reg, ir_rk_Sequence);
305 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, nset_len);
307 /* beware: list is in reverse order, reverse */
309 for (i = nset_len - 1; i >= 0; --i) {
311 reg->parts[i].region = nset;
317 reg->nr = reg->parts[0].region->nr;
318 reg->pred = DUP_ARR_D(ir_region *, obst, reg->parts[0].region->pred);
319 reg->succ = DUP_ARR_D(ir_region *, obst, reg->parts[nset_len - 1].region->succ);
322 DB((dbg, LEVEL_2, " Created Sequence "));
323 for (i = 0; i < nset_len; ++i) {
324 DB((dbg, LEVEL_2, "(%u)", reg->parts[i].region->nr));
326 DB((dbg, LEVEL_2, "\n"));
332 * Create a new IfThenElse region.
334 static ir_region *new_IfThenElse(struct obstack *obst, ir_region *if_b, ir_region *then_b, ir_region *else_b) {
337 ALLOC_REG(obst, reg, ir_rk_IfThenElse);
340 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 3);
342 reg->parts[0].region = if_b; if_b->parent = reg;
343 reg->parts[1].region = then_b; then_b->parent = reg;
344 reg->parts[2].region = else_b; else_b->parent = reg;
346 reg->pred = DUP_ARR_D(ir_region *, obst, if_b->pred);
347 reg->succ = DUP_ARR_D(ir_region *, obst, then_b->succ);
349 DB((dbg, LEVEL_2, " Created If(%u)Then(%u)Else(%u)\n", reg->nr, then_b->nr, else_b->nr));
352 } /* new_IfThenElse */
355 * Create a new IfThen region.
357 static ir_region *new_IfThen(struct obstack *obst, ir_region *if_b, ir_region *then_b) {
360 ALLOC_REG(obst, reg, ir_rk_IfThen);
363 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 2);
365 reg->parts[0].region = if_b; if_b->parent = reg;
366 reg->parts[1].region = then_b; then_b->parent = reg;
368 reg->pred = DUP_ARR_D(ir_region *, obst, if_b->pred);
369 reg->succ = DUP_ARR_D(ir_region *, obst, then_b->succ);
371 DB((dbg, LEVEL_2, " Created If(%u)Then(%u)\n", reg->nr, then_b->nr));
374 } /* new_IfThenElse */
377 * Create a new Switch/case region.
379 static ir_region *new_SwitchCase(struct obstack *obst, ir_region_kind type, ir_region *head, ir_region *exit,
380 ir_region *cases, int cases_len) {
381 ir_region *reg, *c, *n;
385 /* check, if the exit block is in the list */
386 for (c = cases; c != NULL; c = c->link) {
393 ALLOC_REG(obst, reg, type);
396 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, cases_len + add);
398 reg->parts[0].region = head; head->parent = reg;
400 for (c = cases; c != NULL; c = n) {
403 reg->parts[i++].region = c;
409 reg->pred = DUP_ARR_D(ir_region *, obst, head->pred);
410 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
414 DB((dbg, LEVEL_2, " Created %s(%u)\n", reg->type == ir_rk_Switch ? "Switch" : "Case", reg->nr));
415 for (i = 1; i < ARR_LEN(reg->parts); ++i) {
416 DB((dbg, LEVEL_2, " Case(%u)\n", reg->parts[i].region->nr));
418 DB((dbg, LEVEL_2, " Exit(%u)\n", exit->nr));
421 } /* new_SwitchCase */
424 * Create a new SelfLoop region.
426 static ir_region *new_SelfLoop(struct obstack *obst, ir_region *head) {
427 ir_region *reg, *succ;
430 ALLOC_REG(obst, reg, ir_rk_SelfLoop);
433 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 1);
435 reg->parts[0].region = head; head->parent = reg;
437 len = ARR_LEN(head->pred);
438 reg->pred = NEW_ARR_D(ir_region *, obst, len - 1);
439 for (i = j = 0; i < len; ++i) {
440 ir_region *pred = get_region_pred(head, i);
442 reg->pred[j++] = pred;
444 assert(j == len - 1);
446 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
447 assert(ARR_LEN(head->succ) == 2);
449 succ = get_region_succ(head, 0);
453 reg->succ[0] = get_region_succ(head, 1);
455 DB((dbg, LEVEL_2, " Created SelfLoop(%u)\n", reg->nr));
461 * Create a new RepeatLoop region.
463 static ir_region *new_RepeatLoop(struct obstack *obst, ir_region *head, ir_region *body) {
464 ir_region *reg, *succ;
466 ALLOC_REG(obst, reg, ir_rk_RepeatLoop);
469 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 2);
471 reg->parts[0].region = head; head->parent = reg;
472 reg->parts[1].region = body; body->parent = reg;
474 reg->pred = DUP_ARR_D(ir_region *, obst, head->pred);
475 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
476 assert(ARR_LEN(body->succ) == 2);
478 succ = get_region_succ(body, 0);
482 reg->succ[0] = get_region_succ(body, 1);
484 DB((dbg, LEVEL_2, " Created RepeatLoop(%u)Body(%u)\n", reg->nr, body->nr));
487 } /* new_RepeatLoop */
490 * Create a new WhileLoop region.
492 static ir_region *new_WhileLoop(struct obstack *obst, ir_region *head) {
493 ir_region *reg, *succ;
494 ir_region *body = head->link;
499 ALLOC_REG(obst, reg, ir_rk_WhileLoop);
502 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 2);
504 reg->parts[0].region = head; head->parent = reg;
505 reg->parts[1].region = body; body->parent = reg;
507 len = ARR_LEN(head->pred);
508 reg->pred = NEW_ARR_D(ir_region *, obst, len - 1);
509 for (i = j = 0; i < len; ++i) {
510 ir_region *pred = get_region_pred(head, i);
512 reg->pred[j++] = pred;
514 assert(j == len - 1);
516 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
517 assert(ARR_LEN(head->succ) == 2);
519 succ = get_region_succ(head, 0);
523 reg->succ[0] = get_region_succ(head, 1);
525 DB((dbg, LEVEL_2, " Created WhileLoop(%u)Body(%u)\n", reg->nr, body->nr));
528 } /* new_WhileLoop */
531 * Create a new new_NaturalLoop region.
533 static ir_region *new_NaturalLoop(struct obstack *obst, ir_region *head) {
534 ir_region *reg, *c, *n;
535 int i, j, k, len, n_pred, n_succ;
537 /* count number of parts */
538 for (len = 0, c = head; c != NULL; c = c->link)
541 ALLOC_REG(obst, reg, ir_rk_WhileLoop);
544 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, len);
546 /* enter all parts */
547 for (i = 0, c = head; c != NULL; c = n) {
548 reg->parts[i++].region = c;
554 /* count number of preds */
556 for (i = get_region_n_preds(head) - 1; i >= 0; --i) {
557 ir_region *pred = get_region_pred(head, i);
558 if (pred->parent != reg)
561 reg->pred = NEW_ARR_D(ir_region *, obst, n_pred);
562 for (j = 0, i = get_region_n_preds(head) - 1; i >= 0; --i) {
563 ir_region *pred = get_region_pred(head, i);
564 if (pred->parent != reg)
565 reg->pred[j++] = pred;
568 /* count number of succs */
570 for (j = 0; j < len; ++j) {
571 ir_region *c = reg->parts[j].region;
572 for (i = get_region_n_succs(c) - 1; i >= 0; --i) {
573 ir_region *succ = get_region_succ(c, i);
574 if (succ->parent != reg)
578 reg->succ = NEW_ARR_D(ir_region *, obst, n_succ);
580 for (j = 0; j < len; ++j) {
581 ir_region *c = reg->parts[j].region;
582 for (i = get_region_n_succs(c) - 1; i >= 0; --i) {
583 ir_region *succ = get_region_succ(c, i);
584 if (succ->parent != reg)
585 reg->succ[k++] = succ;
590 DB((dbg, LEVEL_2, " Created NaturalLoop(%u)Head(%u)\n", reg->nr, head->nr));
591 for (i = 1; i < len; ++i) {
592 ir_region *p = reg->parts[i].region;
593 DB((dbg, LEVEL_2, " Body(%u)\n", p->nr));
597 } /* new_NaturalLoop */
600 * Return true if region a is an ancestor of region b in DFS search.
602 static int is_ancestor(const ir_region *a, const ir_region *b) {
603 return (a->prenum <= b->prenum && a->postnum > b->postnum);
607 * Return true if region pred is a predecessor of region n.
609 static int pred_of(const ir_region *pred, const ir_region *n) {
611 for (i = get_region_n_preds(n) - 1; i >= 0; --i) {
612 if (get_region_pred(n, i) == pred)
619 * Return true if region succ is a successor of region n.
621 static int succ_of(const ir_region *succ, const ir_region *n) {
623 for (i = get_region_n_succs(n) - 1; i >= 0; --i) {
624 if (get_region_succ(n, i) == succ)
631 * Reverse a linked list of regions.
633 static struct ir_region *reverse_list(ir_region *n) {
634 ir_region *prev = NULL, *next;
636 for (; n; n = next) {
645 * Find the cyclic region in the subgraph entered by node.
647 static ir_region *find_cyclic_region(ir_region *node) {
649 ir_region *last = node;
652 for (i = get_region_n_preds(node) - 1; i >= 0; --i) {
653 ir_region *pred = get_region_pred(node, i);
655 /* search backedges */
656 if (!pred->link && pred != last && is_ancestor(node, pred)) {
657 ir_region *rem = last;
662 for (j = get_region_n_preds(pred) - 1; j >= 0; --j) {
663 ir_region *p = get_region_pred(pred, j);
665 /* Search regions we didn't visited yet and
666 link them into the list. */
667 if (!p->link && p != last) {
668 if (is_ancestor(node, p)) {
676 /* reverse the list. */
678 rem->link = reverse_list(rem->link);
682 if (node->link && improper) {
683 /* found an improper region, do minimization */
689 #define LINK(list) ((ir_region *)list->link)
692 * Detect a cyclic region.
694 static ir_region *cyclic_region_type(struct obstack *obst, ir_region *node) {
697 /* simple cases first */
698 if (succ_of(node, node)) {
699 return new_SelfLoop(obst, node);
701 if (get_region_n_succs(node) == 1) {
702 ir_region *succ = get_region_succ(node, 0);
703 if (get_region_n_preds(succ) == 1 && succ_of(node, succ)) {
704 return new_RepeatLoop(obst, node, succ);
707 list = find_cyclic_region(node);
710 if (!LINK(list)->link && get_region_n_succs(list->link) == 1) {
711 /* only one body block with only one successor (the head) */
712 return new_WhileLoop(obst, list);
714 /* A Loop with one head */
715 return new_NaturalLoop(obst, list);
722 * Clear all links on a list. Needed, because we expect cleared links.
724 static void clear_list(ir_region *list) {
727 for (next = list; next; list = next) {
733 #define ADD_LIST(list, n) do { n->link = list; list = n; ++list##_len; } while(0)
736 * Detect an acyclic region.
738 static ir_region *acyclic_region_type(struct obstack *obst, ir_region *node) {
741 ir_region *nset = NULL;
745 /* check for a block containing node */
747 p = get_region_n_preds(n) == 1;
750 n = get_region_pred(n, 0);
751 p = get_region_n_preds(n) == 1;
752 s = get_region_n_succs(n) == 1;
755 s = get_region_n_succs(n) == 1;
758 n = get_region_succ(n, 0);
759 p = get_region_n_preds(n) == 1;
760 s = get_region_n_succs(n) == 1;
766 /* node --> .. --> .. */
767 res = new_Sequence(obst, nset, nset_len);
772 /* check for IfThenElse */
773 k = get_region_n_succs(node);
775 int n_succs, m_succs, n_preds, m_preds;
777 n = get_region_succ(node, 0);
778 m = get_region_succ(node, 1);
780 n_succs = get_region_n_succs(n);
781 m_succs = get_region_n_succs(m);
782 n_preds = get_region_n_preds(n);
783 m_preds = get_region_n_preds(m);
784 if (n_succs == 1 && n_succs == m_succs && n_preds == m_preds &&
785 get_region_succ(n, 0) == get_region_succ(m, 0)) {
791 return new_IfThenElse(obst, node, n, m);
794 get_region_succ(n, 0) == m &&
800 return new_IfThen(obst, node, n);
803 get_region_succ(m, 0) == m &&
809 return new_IfThen(obst, node, m);
812 /* check for Switch, case */
814 ir_region *exit = NULL;
815 nset = NULL; nset_len = 0;
817 for (i = k - 1; i >= 0; --i) {
818 n = get_region_succ(node, i);
820 if (get_region_n_succs(n) != 1) {
821 /* must be the exit */
829 ir_region_kind kind = ir_rk_Case;
830 ir_region *pos_exit_1 = NULL;
831 ir_region *pos_exit_2 = NULL;
834 for (m = nset; m != NULL; m = m->link) {
835 if (get_region_n_succs(m) != 1) {
836 /* must be the exit block */
839 } else if (exit != m) {
845 ir_region *succ = get_region_succ(m, 0);
847 if (succ->link == NULL) {
849 if (succ == pos_exit_1)
851 else if (succ == pos_exit_2)
853 else if (pos_exit_1 == NULL)
855 else if (pos_exit_2 == NULL)
858 /* more than two possible exits */
861 } else if (exit != succ) {
871 for (n = nset; n != NULL; n = n->link) {
874 /* good, default fall through */
877 succ = get_region_succ(n, 0);
879 /* good, switch to exit */
882 if (succ->link == NULL) {
893 return new_SwitchCase(obst, kind, node, exit, nset, nset_len);
903 * replace all pred edges from region pred that points to any of the set set
904 * to ONE edge to reg.
906 static void replace_pred(ir_region *succ, ir_region *reg) {
907 int i, len = get_region_n_preds(succ);
910 for (i = 0; i < len; ++i) {
911 ir_region *pred = get_region_pred(succ, i);
913 if (pred->parent == reg) {
918 r = get_region_pred(succ, --len);
924 set_region_pred(succ, i, r);
926 /* the current region can be entered by this node */
930 ARR_SHRINKLEN(succ->pred, len);
934 * replace all succ edges from region pred that points to any of the set set
935 * to ONE edge to reg.
937 static void replace_succ(ir_region *pred, ir_region *reg) {
938 int i, len = get_region_n_succs(pred);
941 for (i = 0; i < len; ++i) {
942 ir_region *succ = get_region_succ(pred, i);
944 if (succ->parent == reg) {
949 r = get_region_succ(pred, --len);
955 set_region_succ(pred, i, r);
957 /* current region can be left by this node */
961 ARR_SHRINKLEN(pred->succ, len);
965 * Reduce the graph by the node reg.
967 static void reduce(walk_env *env, ir_region *reg) {
969 ir_region *head = reg->parts[0].region;
970 unsigned maxorder = head->postnum;
971 unsigned minorder = head->prenum;
973 /* second step: replace all preds in successors */
974 for (i = get_region_n_succs(reg) - 1; i >= 0; --i) {
975 ir_region *succ = get_region_succ(reg, i);
977 replace_pred(succ, reg);
980 /* third step: replace all succs in predessors */
981 for (i = get_region_n_preds(reg) - 1; i >= 0; --i) {
982 ir_region *pred = get_region_pred(reg, i);
984 replace_succ(pred, reg);
987 reg->prenum = minorder;
988 reg->postnum = maxorder;
989 env->post[maxorder] = reg;
993 * Construct the region tree of a graph by doing
994 * structural analysis.
996 * Uses link fields of nodes.
998 * @param irg the graph
1000 ir_reg_tree *construct_region_tree(ir_graph *irg) {
1002 ir_graph *rem = current_ir_graph;
1003 ir_reg_tree *res = XMALLOC(ir_reg_tree);
1005 obstack_init(&res->obst);
1007 current_ir_graph = irg;
1009 FIRM_DBG_REGISTER(dbg, "firm.ana.structure");
1010 firm_dbg_set_mask(dbg, SET_LEVEL_5);
1012 DB((dbg, LEVEL_1, "Structural analysis on %+F starts...\n", irg));
1014 dump_ir_block_graph(irg, "-structure_start");
1016 /* we need dominance info */
1019 assure_irg_outs(irg);
1021 env.start_block = get_irg_start_block(irg);
1022 env.end_block = get_irg_end_block(irg);
1024 /* create the Block wrapper and count them */
1026 env.obst = &res->obst;
1027 irg_block_walk_graph(irg, NULL, wrap_BasicBlocks, &env);
1028 irg_block_walk_graph(irg, NULL, update_BasicBlock_regions, &env);
1030 env.post = NEW_ARR_F(ir_region *, env.l_post);
1032 /* do the DFS walk */
1033 dfs_walk(irg, &env);
1035 DB((dbg, LEVEL_1, "%d regions left\n", env.postmax));
1036 if (env.postmax > 1) {
1037 unsigned postctr = 0;
1039 ir_region *reg, *n = env.post[postctr];
1041 if (n->parent != NULL) {
1042 /* already folded */
1045 /* locate an acyclic region if present */
1046 reg = acyclic_region_type(env.obst, n);
1048 /* locate a cyclic region */
1049 reg = cyclic_region_type(env.obst, n);
1052 /* found a new region */
1056 } while (reg != NULL);
1058 } while (postctr < env.postmax);
1060 DB((dbg, LEVEL_1, "Structural analysis finished.\n"));
1062 DEL_ARR_F(env.post);
1063 current_ir_graph = rem;
1065 res->top = env.post[0];
1071 * Walk over the region tree.
1073 * @param reg a region node
1074 * @param pre walker function, executed before the children of a tree node are visited
1075 * @param post walker function, executed after the children of a tree node are visited
1076 * @param env environment, passed to pre and post
1078 static void region_tree_walk2(ir_region *reg, irg_reg_walk_func *pre, irg_reg_walk_func *post, void *env) {
1083 if (reg->type != ir_rk_BasicBlock) {
1084 for (i = 0, n = ARR_LEN(reg->parts); i < n; ++i)
1085 region_tree_walk2(reg->parts[i].region, pre, post, env);
1092 * Walk over the region tree.
1094 * @param tree the tree
1095 * @param pre walker function, executed before the children of a tree node are visited
1096 * @param post walker function, executed after the children of a tree node are visited
1097 * @param env environment, passed to pre and post
1099 void region_tree_walk(ir_reg_tree *tree, irg_reg_walk_func *pre, irg_reg_walk_func *post, void *env) {
1100 region_tree_walk2(tree->top, pre, post, env);