2 * Copyright (C) 1995-2011 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)
86 * Sets the link of a region.
88 void set_region_link(ir_region *reg, void *data)
94 * Get the immediate region of a block.
96 ir_region *get_block_region(const ir_node *block)
98 assert(is_Block(block));
99 return block->attr.block.region;
103 * Sets the immediate region of a block.
105 void set_block_region(ir_node *block, ir_region *reg)
107 assert(is_Block(block));
108 block->attr.block.region = reg;
112 * Get the immediate region of a node.
114 ir_region *get_irn_region(ir_node *n)
117 n = get_nodes_block(n);
118 return get_block_region(n);
122 * Return non-zero if a given firm thing is a region.
124 int is_region(const void *thing)
126 const firm_kind *kind = (const firm_kind*) thing;
127 return *kind == k_ir_region;
131 * Return the number of predecessors of a region.
133 int get_region_n_preds(const ir_region *reg)
135 return ARR_LEN(reg->pred);
139 * Return the predecessor region at position pos.
141 ir_region *get_region_pred(const ir_region *reg, int pos)
143 assert(0 <= pos && pos <= get_region_n_preds(reg));
144 return reg->pred[pos];
148 * Set the predecessor region at position pos.
150 void set_region_pred(ir_region *reg, int pos, ir_region *n)
152 assert(0 <= pos && pos <= get_region_n_preds(reg));
157 * Return the number of successors in a region.
159 int get_region_n_succs(const ir_region *reg)
161 return ARR_LEN(reg->succ);
165 * Return the successor region at position pos.
167 ir_region *get_region_succ(const ir_region *reg, int pos)
169 assert(0 <= pos && pos <= get_region_n_succs(reg));
170 return reg->succ[pos];
174 * Set the successor region at position pos.
176 void set_region_succ(ir_region *reg, int pos, ir_region *n)
178 assert(0 <= pos && pos <= get_region_n_succs(reg));
182 /* ----------------------- construction -------------------------- */
184 /** Walker environment. */
185 typedef struct walk_env {
186 struct obstack *obst; /**< An obstack to allocate from. */
187 ir_region **post; /**< The list of all currently existent top regions. */
188 unsigned l_post; /**< The length of the allocated regions array. */
189 unsigned premax; /**< maximum pre counter */
190 unsigned postmax; /**< maximum post counter */
191 ir_node *start_block; /**< The start block of the graph. */
192 ir_node *end_block; /**< The end block of the graph. */
196 * Do a DFS search on the initial regions, assign a prenum and a postnum to every
197 * node and store the region nodes into the post array.
199 static void dfs_walk2(ir_region *reg, walk_env *env)
203 if (reg->visited == 0) {
206 reg->prenum = env->premax++;
207 for (i = 0, n = get_region_n_succs(reg); i < n; ++i) {
209 ir_region *succ = get_region_succ(reg, i);
210 dfs_walk2(succ, env);
213 env->post[env->postmax] = reg;
214 reg->postnum = env->postmax++;
219 * Do a DFS search on the initial regions, assign a prenum and a postnum to every
220 * node and store the region nodes into the post array.
222 static void dfs_walk(ir_graph *irg, walk_env *env)
224 ir_graph *rem = current_ir_graph;
227 current_ir_graph = irg;
228 reg = (ir_region*) get_irn_link(get_irg_start_block(irg));
233 current_ir_graph = rem;
237 * Post-walker: wrap all blocks with a BasicBlock region
240 static void wrap_BasicBlocks(ir_node *block, void *ctx)
242 walk_env *env = (walk_env*) ctx;
245 /* Allocate a Block wrapper */
246 reg = OALLOC(env->obst, ir_region);
247 reg->kind = k_ir_region;
248 reg->type = ir_rk_BasicBlock;
256 reg->nr = get_irn_node_nr(block);
257 reg->parts = NEW_ARR_D(ir_reg_or_blk, env->obst, 1);
259 reg->parts[0].blk = block;
260 set_irn_link(block, reg);
263 } /* wrap_BasicBlocks */
266 * Post-walker: Create the pred and succ edges for Block wrapper.
267 * Kill edges to the Start and End blocks.
269 static void update_BasicBlock_regions(ir_node *blk, void *ctx)
271 walk_env *env = (walk_env*) ctx;
272 ir_region *reg = (ir_region*) get_irn_link(blk);
275 if (blk == env->start_block) {
276 /* handle Firm's self loop: Start block has no predecessors */
277 reg->pred = NEW_ARR_D(ir_region *, env->obst, 0);
279 len = get_Block_n_cfgpreds(blk);
280 reg->pred = NEW_ARR_D(ir_region *, env->obst, len);
281 for (i = j = 0; i < len; ++i) {
282 ir_node *pred = get_Block_cfgpred_block(blk, i);
283 reg->pred[j++] = (ir_region*) get_irn_link(pred);
285 ARR_SHRINKLEN(reg->pred, j);
288 len = get_Block_n_cfg_outs(blk);
289 reg->succ = NEW_ARR_D(ir_region *, env->obst, len);
290 for (i = j = 0; i < len; ++i) {
291 ir_node *succ = get_Block_cfg_out(blk, i);
292 reg->succ[j++] = (ir_region*) get_irn_link(succ);
294 ARR_SHRINKLEN(reg->succ, j);
295 } /* update_BasicBlock_regions */
297 /** Allocate a new region on an obstack */
298 #define ALLOC_REG(obst, reg, tp) \
300 (reg) = OALLOC((obst), ir_region); \
301 (reg)->kind = k_ir_region; \
303 (reg)->parent = NULL; \
305 (reg)->postnum = 0; \
306 (reg)->visited = 0; \
309 (reg)->link = NULL; \
313 * Creates a new Sequence region.
315 static ir_region *new_Sequence(struct obstack *obst, ir_region *nset, int nset_len)
317 ir_region *reg, *next;
320 ALLOC_REG(obst, reg, ir_rk_Sequence);
322 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, nset_len);
324 /* beware: list is in reverse order, reverse */
326 for (i = nset_len - 1; i >= 0; --i) {
328 reg->parts[i].region = nset;
330 next = (ir_region*) nset->link;
334 reg->nr = reg->parts[0].region->nr;
335 reg->pred = DUP_ARR_D(ir_region *, obst, reg->parts[0].region->pred);
336 reg->succ = DUP_ARR_D(ir_region *, obst, reg->parts[nset_len - 1].region->succ);
339 DB((dbg, LEVEL_2, " Created Sequence "));
340 for (i = 0; i < nset_len; ++i) {
341 DB((dbg, LEVEL_2, "(%u)", reg->parts[i].region->nr));
343 DB((dbg, LEVEL_2, "\n"));
349 * Create a new IfThenElse region.
351 static ir_region *new_IfThenElse(struct obstack *obst, ir_region *if_b, ir_region *then_b, ir_region *else_b)
355 ALLOC_REG(obst, reg, ir_rk_IfThenElse);
358 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 3);
360 reg->parts[0].region = if_b; if_b->parent = reg;
361 reg->parts[1].region = then_b; then_b->parent = reg;
362 reg->parts[2].region = else_b; else_b->parent = reg;
364 reg->pred = DUP_ARR_D(ir_region *, obst, if_b->pred);
365 reg->succ = DUP_ARR_D(ir_region *, obst, then_b->succ);
367 DB((dbg, LEVEL_2, " Created If(%u)Then(%u)Else(%u)\n", reg->nr, then_b->nr, else_b->nr));
370 } /* new_IfThenElse */
373 * Create a new IfThen region.
375 static ir_region *new_IfThen(struct obstack *obst, ir_region *if_b, ir_region *then_b)
379 ALLOC_REG(obst, reg, ir_rk_IfThen);
382 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 2);
384 reg->parts[0].region = if_b; if_b->parent = reg;
385 reg->parts[1].region = then_b; then_b->parent = reg;
387 reg->pred = DUP_ARR_D(ir_region *, obst, if_b->pred);
388 reg->succ = DUP_ARR_D(ir_region *, obst, then_b->succ);
390 DB((dbg, LEVEL_2, " Created If(%u)Then(%u)\n", reg->nr, then_b->nr));
393 } /* new_IfThenElse */
396 * Create a new Switch/case region.
398 static ir_region *new_SwitchCase(struct obstack *obst, ir_region_kind type, ir_region *head, ir_region *exit,
399 ir_region *cases, int cases_len)
401 ir_region *reg, *c, *n;
405 /* check, if the exit block is in the list */
406 for (c = cases; c != NULL; c = (ir_region*) c->link) {
413 ALLOC_REG(obst, reg, type);
416 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, cases_len + add);
418 reg->parts[0].region = head; head->parent = reg;
420 for (c = cases; c != NULL; c = n) {
421 n = (ir_region*) c->link;
423 reg->parts[i++].region = c;
429 reg->pred = DUP_ARR_D(ir_region *, obst, head->pred);
430 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
435 DB((dbg, LEVEL_2, " Created %s(%u)\n", reg->type == ir_rk_Switch ? "Switch" : "Case", reg->nr));
436 for (i = 1; i < ARR_LEN(reg->parts); ++i) {
437 DB((dbg, LEVEL_2, " Case(%u)\n", reg->parts[i].region->nr));
439 DB((dbg, LEVEL_2, " Exit(%u)\n", exit->nr));
442 } /* new_SwitchCase */
445 * Create a new SelfLoop region.
447 static ir_region *new_SelfLoop(struct obstack *obst, ir_region *head)
449 ir_region *reg, *succ;
452 ALLOC_REG(obst, reg, ir_rk_SelfLoop);
455 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 1);
457 reg->parts[0].region = head; head->parent = reg;
459 len = ARR_LEN(head->pred);
460 reg->pred = NEW_ARR_D(ir_region *, obst, len - 1);
461 for (i = j = 0; i < len; ++i) {
462 ir_region *pred = get_region_pred(head, i);
464 reg->pred[j++] = pred;
466 assert(j == len - 1);
468 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
469 assert(ARR_LEN(head->succ) == 2);
471 succ = get_region_succ(head, 0);
475 reg->succ[0] = get_region_succ(head, 1);
477 DB((dbg, LEVEL_2, " Created SelfLoop(%u)\n", reg->nr));
483 * Create a new RepeatLoop region.
485 static ir_region *new_RepeatLoop(struct obstack *obst, ir_region *head, ir_region *body)
487 ir_region *reg, *succ;
489 ALLOC_REG(obst, reg, ir_rk_RepeatLoop);
492 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 2);
494 reg->parts[0].region = head; head->parent = reg;
495 reg->parts[1].region = body; body->parent = reg;
497 reg->pred = DUP_ARR_D(ir_region *, obst, head->pred);
498 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
499 assert(ARR_LEN(body->succ) == 2);
501 succ = get_region_succ(body, 0);
505 reg->succ[0] = get_region_succ(body, 1);
507 DB((dbg, LEVEL_2, " Created RepeatLoop(%u)Body(%u)\n", reg->nr, body->nr));
510 } /* new_RepeatLoop */
513 * Create a new WhileLoop region.
515 static ir_region *new_WhileLoop(struct obstack *obst, ir_region *head)
517 ir_region *reg, *succ;
518 ir_region *body = (ir_region*) head->link;
523 ALLOC_REG(obst, reg, ir_rk_WhileLoop);
526 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, 2);
528 reg->parts[0].region = head; head->parent = reg;
529 reg->parts[1].region = body; body->parent = reg;
531 len = ARR_LEN(head->pred);
532 reg->pred = NEW_ARR_D(ir_region *, obst, len - 1);
533 for (i = j = 0; i < len; ++i) {
534 ir_region *pred = get_region_pred(head, i);
536 reg->pred[j++] = pred;
538 assert(j == len - 1);
540 reg->succ = NEW_ARR_D(ir_region *, obst, 1);
541 assert(ARR_LEN(head->succ) == 2);
543 succ = get_region_succ(head, 0);
547 reg->succ[0] = get_region_succ(head, 1);
549 DB((dbg, LEVEL_2, " Created WhileLoop(%u)Body(%u)\n", reg->nr, body->nr));
552 } /* new_WhileLoop */
555 * Create a new new_NaturalLoop region.
557 static ir_region *new_NaturalLoop(struct obstack *obst, ir_region *head)
559 ir_region *reg, *c, *n;
560 int i, j, k, len, n_pred, n_succ;
562 /* count number of parts */
563 for (len = 0, c = head; c != NULL; c = (ir_region*) c->link)
566 ALLOC_REG(obst, reg, ir_rk_WhileLoop);
569 reg->parts = NEW_ARR_D(ir_reg_or_blk, obst, len);
571 /* enter all parts */
572 for (i = 0, c = head; c != NULL; c = n) {
573 reg->parts[i++].region = c;
575 n = (ir_region*) c->link;
579 /* count number of preds */
581 for (i = get_region_n_preds(head) - 1; i >= 0; --i) {
582 ir_region *pred = get_region_pred(head, i);
583 if (pred->parent != reg)
586 reg->pred = NEW_ARR_D(ir_region *, obst, n_pred);
587 for (j = 0, i = get_region_n_preds(head) - 1; i >= 0; --i) {
588 ir_region *pred = get_region_pred(head, i);
589 if (pred->parent != reg)
590 reg->pred[j++] = pred;
593 /* count number of succs */
595 for (j = 0; j < len; ++j) {
596 ir_region *pc = reg->parts[j].region;
597 for (i = get_region_n_succs(pc) - 1; i >= 0; --i) {
598 ir_region *succ = get_region_succ(pc, i);
599 if (succ->parent != reg)
603 reg->succ = NEW_ARR_D(ir_region *, obst, n_succ);
605 for (j = 0; j < len; ++j) {
606 ir_region *pc = reg->parts[j].region;
607 for (i = get_region_n_succs(pc) - 1; i >= 0; --i) {
608 ir_region *succ = get_region_succ(pc, i);
609 if (succ->parent != reg)
610 reg->succ[k++] = succ;
615 DB((dbg, LEVEL_2, " Created NaturalLoop(%u)Head(%u)\n", reg->nr, head->nr));
616 for (i = 1; i < len; ++i) {
617 ir_region *p = reg->parts[i].region;
618 DB((dbg, LEVEL_2, " Body(%u)\n", p->nr));
622 } /* new_NaturalLoop */
625 * Return true if region a is an ancestor of region b in DFS search.
627 static int is_ancestor(const ir_region *a, const ir_region *b)
629 return (a->prenum <= b->prenum && a->postnum > b->postnum);
633 * Return true if region pred is a predecessor of region n.
635 static int pred_of(const ir_region *pred, const ir_region *n)
638 for (i = get_region_n_preds(n) - 1; i >= 0; --i) {
639 if (get_region_pred(n, i) == pred)
646 * Return true if region succ is a successor of region n.
648 static int succ_of(const ir_region *succ, const ir_region *n)
651 for (i = get_region_n_succs(n) - 1; i >= 0; --i) {
652 if (get_region_succ(n, i) == succ)
659 * Reverse a linked list of regions.
661 static struct ir_region *reverse_list(ir_region *n)
663 ir_region *prev = NULL, *next;
665 for (; n; n = next) {
666 next = (ir_region*) n->link;
674 * Find the cyclic region in the subgraph entered by node.
676 static ir_region *find_cyclic_region(ir_region *node)
679 ir_region *last = node;
682 for (i = get_region_n_preds(node) - 1; i >= 0; --i) {
683 ir_region *pred = get_region_pred(node, i);
685 /* search backedges */
686 if (!pred->link && pred != last && is_ancestor(node, pred)) {
687 ir_region *rem = last;
692 for (j = get_region_n_preds(pred) - 1; j >= 0; --j) {
693 ir_region *p = get_region_pred(pred, j);
695 /* Search regions we didn't visited yet and
696 link them into the list. */
697 if (!p->link && p != last) {
698 if (is_ancestor(node, p)) {
706 /* reverse the list. */
707 last = (ir_region*) rem->link;
708 rem->link = reverse_list((ir_region*) rem->link);
712 if (node->link && improper) {
713 /* found an improper region, do minimization */
719 #define LINK(list) ((ir_region *)list->link)
722 * Detect a cyclic region.
724 static ir_region *cyclic_region_type(struct obstack *obst, ir_region *node)
728 /* simple cases first */
729 if (succ_of(node, node)) {
730 return new_SelfLoop(obst, node);
732 if (get_region_n_succs(node) == 1) {
733 ir_region *succ = get_region_succ(node, 0);
734 if (get_region_n_preds(succ) == 1 && succ_of(node, succ)) {
735 return new_RepeatLoop(obst, node, succ);
738 list = find_cyclic_region(node);
741 if (!LINK(list)->link && get_region_n_succs((ir_region*) list->link) == 1) {
742 /* only one body block with only one successor (the head) */
743 return new_WhileLoop(obst, list);
745 /* A Loop with one head */
746 return new_NaturalLoop(obst, list);
753 * Clear all links on a list. Needed, because we expect cleared links.
755 static void clear_list(ir_region *list)
759 for (next = list; next; list = next) {
760 next = (ir_region*) list->link;
765 #define ADD_LIST(list, n) do { n->link = list; list = n; ++list##_len; } while (0)
768 * Detect an acyclic region.
770 static ir_region *acyclic_region_type(struct obstack *obst, ir_region *node)
774 ir_region *nset = NULL;
778 /* check for a block containing node */
780 p = get_region_n_preds(n) == 1;
783 n = get_region_pred(n, 0);
784 p = get_region_n_preds(n) == 1;
785 s = get_region_n_succs(n) == 1;
788 s = get_region_n_succs(n) == 1;
791 n = get_region_succ(n, 0);
792 p = get_region_n_preds(n) == 1;
793 s = get_region_n_succs(n) == 1;
799 /* node --> .. --> .. */
800 res = new_Sequence(obst, nset, nset_len);
805 /* check for IfThenElse */
806 k = get_region_n_succs(node);
808 int n_succs, m_succs, n_preds, m_preds;
810 n = get_region_succ(node, 0);
811 m = get_region_succ(node, 1);
813 n_succs = get_region_n_succs(n);
814 m_succs = get_region_n_succs(m);
815 n_preds = get_region_n_preds(n);
816 m_preds = get_region_n_preds(m);
817 if (n_succs == 1 && n_succs == m_succs && n_preds == m_preds &&
818 get_region_succ(n, 0) == get_region_succ(m, 0)) {
824 return new_IfThenElse(obst, node, n, m);
827 get_region_succ(n, 0) == m &&
833 return new_IfThen(obst, node, n);
836 get_region_succ(m, 0) == m &&
842 return new_IfThen(obst, node, m);
845 /* check for Switch, case */
847 ir_region *rexit = NULL;
848 nset = NULL; nset_len = 0;
850 for (i = k - 1; i >= 0; --i) {
851 n = get_region_succ(node, i);
853 if (get_region_n_succs(n) != 1) {
854 /* must be the exit */
862 ir_region_kind kind = ir_rk_Case;
863 ir_region *pos_exit_1 = NULL;
864 ir_region *pos_exit_2 = NULL;
867 for (m = (ir_region*) nset; m != NULL; m = (ir_region*) m->link) {
868 if (get_region_n_succs(m) != 1) {
869 /* must be the exit block */
872 } else if (rexit != m) {
878 ir_region *succ = get_region_succ(m, 0);
880 if (succ->link == NULL) {
882 if (succ == pos_exit_1)
884 else if (succ == pos_exit_2)
886 else if (pos_exit_1 == NULL)
888 else if (pos_exit_2 == NULL)
891 /* more than two possible exits */
894 } else if (rexit != succ) {
904 for (n = (ir_region*) nset; n != NULL; n = (ir_region*) n->link) {
907 /* good, default fall through */
910 succ = get_region_succ(n, 0);
912 /* good, switch to exit */
915 if (succ->link == NULL) {
926 return new_SwitchCase(obst, kind, node, rexit, nset, nset_len);
936 * replace all pred edges from region pred that points to any of the set set
937 * to ONE edge to reg.
939 static void replace_pred(ir_region *succ, ir_region *reg)
941 int i, len = get_region_n_preds(succ);
944 for (i = 0; i < len; ++i) {
945 ir_region *pred = get_region_pred(succ, i);
947 if (pred->parent == reg) {
952 r = get_region_pred(succ, --len);
958 set_region_pred(succ, i, r);
960 /* the current region can be entered by this node */
964 ARR_SHRINKLEN(succ->pred, len);
968 * replace all succ edges from region pred that points to any of the set set
969 * to ONE edge to reg.
971 static void replace_succ(ir_region *pred, ir_region *reg)
973 int i, len = get_region_n_succs(pred);
976 for (i = 0; i < len; ++i) {
977 ir_region *succ = get_region_succ(pred, i);
979 if (succ->parent == reg) {
984 r = get_region_succ(pred, --len);
990 set_region_succ(pred, i, r);
992 /* current region can be left by this node */
996 ARR_SHRINKLEN(pred->succ, len);
1000 * Reduce the graph by the node reg.
1002 static void reduce(walk_env *env, ir_region *reg)
1005 ir_region *head = reg->parts[0].region;
1006 unsigned maxorder = head->postnum;
1007 unsigned minorder = head->prenum;
1009 /* second step: replace all preds in successors */
1010 for (i = get_region_n_succs(reg) - 1; i >= 0; --i) {
1011 ir_region *succ = get_region_succ(reg, i);
1013 replace_pred(succ, reg);
1016 /* third step: replace all succs in predessors */
1017 for (i = get_region_n_preds(reg) - 1; i >= 0; --i) {
1018 ir_region *pred = get_region_pred(reg, i);
1020 replace_succ(pred, reg);
1023 reg->prenum = minorder;
1024 reg->postnum = maxorder;
1025 env->post[maxorder] = reg;
1029 * Construct the region tree of a graph by doing
1030 * structural analysis.
1032 * Uses link fields of nodes.
1034 * @param irg the graph
1036 ir_reg_tree *construct_region_tree(ir_graph *irg)
1039 ir_graph *rem = current_ir_graph;
1040 ir_reg_tree *res = XMALLOC(ir_reg_tree);
1042 obstack_init(&res->obst);
1044 current_ir_graph = irg;
1046 FIRM_DBG_REGISTER(dbg, "firm.ana.structure");
1047 firm_dbg_set_mask(dbg, SET_LEVEL_5);
1049 DB((dbg, LEVEL_1, "Structural analysis on %+F starts...\n", irg));
1051 /* we need dominance info */
1054 assure_irg_outs(irg);
1056 env.start_block = get_irg_start_block(irg);
1057 env.end_block = get_irg_end_block(irg);
1059 /* create the Block wrapper and count them */
1061 env.obst = &res->obst;
1062 irg_block_walk_graph(irg, NULL, wrap_BasicBlocks, &env);
1063 irg_block_walk_graph(irg, NULL, update_BasicBlock_regions, &env);
1065 env.post = NEW_ARR_F(ir_region *, env.l_post);
1067 /* do the DFS walk */
1068 dfs_walk(irg, &env);
1070 DB((dbg, LEVEL_1, "%d regions left\n", env.postmax));
1071 if (env.postmax > 1) {
1072 unsigned postctr = 0;
1074 ir_region *reg, *n = env.post[postctr];
1076 if (n->parent != NULL) {
1077 /* already folded */
1080 /* locate an acyclic region if present */
1081 reg = acyclic_region_type(env.obst, n);
1083 /* locate a cyclic region */
1084 reg = cyclic_region_type(env.obst, n);
1087 /* found a new region */
1091 } while (reg != NULL);
1093 } while (postctr < env.postmax);
1095 DB((dbg, LEVEL_1, "Structural analysis finished.\n"));
1097 DEL_ARR_F(env.post);
1098 current_ir_graph = rem;
1100 res->top = env.post[0];
1106 * Walk over the region tree.
1108 * @param reg a region node
1109 * @param pre walker function, executed before the children of a tree node are visited
1110 * @param post walker function, executed after the children of a tree node are visited
1111 * @param env environment, passed to pre and post
1113 static void region_tree_walk2(ir_region *reg, irg_reg_walk_func *pre, irg_reg_walk_func *post, void *env)
1119 if (reg->type != ir_rk_BasicBlock) {
1120 for (i = 0, n = ARR_LEN(reg->parts); i < n; ++i)
1121 region_tree_walk2(reg->parts[i].region, pre, post, env);
1128 * Walk over the region tree.
1130 * @param tree the tree
1131 * @param pre walker function, executed before the children of a tree node are visited
1132 * @param post walker function, executed after the children of a tree node are visited
1133 * @param env environment, passed to pre and post
1135 void region_tree_walk(ir_reg_tree *tree, irg_reg_walk_func *pre, irg_reg_walk_func *post, void *env)
1137 region_tree_walk2(tree->top, pre, post, env);