handle Block_entity like other node attributes
[libfirm] / ir / be / bedomfront.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Algorithms for computing dominance frontiers.
23  * @author      Sebastian Hack, Daniel Grund
24  * @date        04.05.2005
25  */
26 #include "config.h"
27
28 #include "obst.h"
29 #include "pmap.h"
30 #include "pdeq.h"
31 #include "irdom.h"
32 #include "array.h"
33 #include "irgraph.h"
34 #include "iredges_t.h"
35 #include "irnodeset.h"
36
37 #include "bedomfront.h"
38
39 /**
40  * The dominance frontier for a graph.
41  */
42 struct be_dom_front_info_t {
43         pmap *df_map;         /**< A map, mapping every block to a list of its dominance frontier blocks. */
44         struct obstack obst;  /**< An obstack holding all the frontier data. */
45 };
46
47 /**
48  * A wrapper for get_Block_idom.
49  * This function returns the block itself, if the block is the start
50  * block. Returning NULL would make any != comparison true which
51  * suggests, that the start block is dominated by some other node.
52  * @param bl The block.
53  * @return The immediate dominator of the block.
54  */
55 static inline ir_node *get_idom(ir_node *bl)
56 {
57         ir_node *idom = get_Block_idom(bl);
58         return idom == NULL ? bl : idom;
59 }
60
61 /**
62  * Compute the dominance frontier for a given block.
63  *
64  * @param blk   the block where the calculation starts
65  *
66  * @return the list of all blocks in the dominance frontier of blk
67  */
68 static ir_node **compute_df(ir_node *blk, be_dom_front_info_t *info)
69 {
70         ir_node *c;
71         const ir_edge_t *edge;
72         ir_node **df_list = NEW_ARR_F(ir_node *, 0);
73         ir_node **df;
74         size_t len;
75
76         /* Add local dominance frontiers */
77         foreach_block_succ(blk, edge) {
78                 ir_node *y = get_edge_src_irn(edge);
79
80                 if (get_idom(y) != blk) {
81                         ARR_APP1(ir_node *, df_list, y);
82                 }
83         }
84
85         /*
86          * Go recursively down the dominance tree and add all blocks
87          * into the dominance frontiers of the children, which are not
88          * dominated by the given block.
89          */
90         for (c = get_Block_dominated_first(blk); c; c = get_Block_dominated_next(c)) {
91                 size_t i;
92                 ir_node **df_c_list = compute_df(c, info);
93
94                 for (i = ARR_LEN(df_c_list); i > 0;) {
95                         ir_node *w = df_c_list[--i];
96                         if (get_idom(w) != blk)
97                                 ARR_APP1(ir_node *, df_list, w);
98                 }
99         }
100
101         /* now copy the flexible array to the obstack */
102         len = ARR_LEN(df_list);
103         df = NEW_ARR_D(ir_node *, &info->obst, len);
104         memcpy(df, df_list, len * sizeof(df[0]));
105         DEL_ARR_F(df_list);
106
107         pmap_insert(info->df_map, blk, df);
108         return df;
109 }
110
111 be_dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
112 {
113         be_dom_front_info_t *info = XMALLOC(be_dom_front_info_t);
114
115         edges_assure(irg);
116         obstack_init(&info->obst);
117         info->df_map = pmap_create();
118         assure_doms(irg);
119         (void)compute_df(get_irg_start_block(irg), info);
120
121         return info;
122 }
123
124 void be_free_dominance_frontiers(be_dom_front_info_t *info)
125 {
126         obstack_free(&info->obst, NULL);
127         pmap_destroy(info->df_map);
128         free(info);
129 }
130
131 /* Get the dominance frontier of a block. */
132 ir_node **be_get_dominance_frontier(const be_dom_front_info_t *info,
133                                     ir_node *block)
134 {
135         return (ir_node**)pmap_get(info->df_map, block);
136 }