Implement binary emitter for LdTls.
[libfirm] / ir / be / bedomfront.c
1 /*
2  * Copyright (C) 1995-2008 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 normal and iterated dominance frontiers.
23  * @author      Sebastian Hack, Daniel Grund
24  * @date        04.05.2005
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include "obst.h"
30 #include "pmap.h"
31 #include "pdeq.h"
32 #include "irdom.h"
33 #include "array.h"
34 #include "irgraph.h"
35 #include "iredges_t.h"
36 #include "irnodeset.h"
37
38 #include "bedomfront.h"
39
40 /**
41  * The dominance frontier for a graph.
42  */
43 struct _be_dom_front_info_t {
44         pmap *df_map;         /**< A map, mapping every block to a list of its dominance frontier blocks. */
45         struct obstack obst;  /**< An obstack holding all the frontier data. */
46 };
47
48 /**
49  * A wrapper for get_Block_idom.
50  * This function returns the block itself, if the block is the start
51  * block. Returning NULL would make any != comparison true which
52  * suggests, that the start block is dominated by some other node.
53  * @param bl The block.
54  * @return The immediate dominator of the block.
55  */
56 static inline
57 ir_node *get_idom(ir_node *bl)
58 {
59         ir_node *idom = get_Block_idom(bl);
60         return idom == NULL ? bl : idom;
61 }
62
63 /**
64  * Compute the dominance frontier for a given block.
65  *
66  * @param blk   the block where the calculation starts
67  *
68  * @return the list of all blocks in the dominance frontier of blk
69  */
70 static
71 ir_node **compute_df(ir_node *blk, be_dom_front_info_t *info)
72 {
73         ir_node *c;
74         const ir_edge_t *edge;
75         ir_node **df_list = NEW_ARR_F(ir_node *, 0);
76         ir_node **df;
77         int len;
78
79         /* Add local dominance frontiers */
80         foreach_block_succ(blk, edge) {
81                 ir_node *y = get_edge_src_irn(edge);
82
83                 if (get_idom(y) != blk) {
84                         ARR_APP1(ir_node *, df_list, y);
85                 }
86         }
87
88         /*
89          * Go recursively down the dominance tree and add all blocks
90          * into the dominance frontiers of the children, which are not
91          * dominated by the given block.
92          */
93         for (c = get_Block_dominated_first(blk); c; c = get_Block_dominated_next(c)) {
94                 int i;
95                 ir_node **df_c_list = compute_df(c, info);
96
97                 for (i = ARR_LEN(df_c_list) - 1; i >= 0; --i) {
98                         ir_node *w = df_c_list[i];
99                         if (get_idom(w) != blk)
100                                 ARR_APP1(ir_node *, df_list, w);
101                 }
102         }
103
104         /* now copy the flexible array to the obstack */
105         len = ARR_LEN(df_list);
106         df = NEW_ARR_D(ir_node *, &info->obst, len);
107         memcpy(df, df_list, len * sizeof(df[0]));
108         DEL_ARR_F(df_list);
109
110         pmap_insert(info->df_map, blk, df);
111         return df;
112 }
113
114 be_dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
115 {
116         be_dom_front_info_t *info = XMALLOC(be_dom_front_info_t);
117
118         edges_assure(irg);
119         obstack_init(&info->obst);
120         info->df_map = pmap_create();
121         assure_doms(irg);
122         (void)compute_df(get_irg_start_block(irg), info);
123
124         return info;
125 }
126
127 void be_free_dominance_frontiers(be_dom_front_info_t *info)
128 {
129         obstack_free(&info->obst, NULL);
130         pmap_destroy(info->df_map);
131         free(info);
132 }
133
134 /* Get the dominance frontier of a block. */
135 ir_node **be_get_dominance_frontier(const be_dom_front_info_t *info,
136                                     ir_node *block)
137 {
138         return pmap_get(info->df_map, block);
139 }
140
141 #if 0
142 /**
143  * Calculates the iterated dominance frontier of a set of blocks.
144  * Also clears the link field of the returned blocks as a side effect
145  */
146 void be_get_iterated_dominance_frontiers(const be_dom_front_info_t *domfronts,
147                                          ir_nodeset_t *blocks)
148 {
149         ir_node *block;
150         ir_nodeset_iterator_t iter;
151         waitq *worklist = new_waitq();
152
153         foreach_ir_nodeset(blocks, block, iter) {
154                 waitq_put(worklist, block);
155         }
156
157         while(! waitq_empty(worklist)) {
158                 int     i;
159                 ir_node *block       = waitq_get(worklist);
160                 ir_node **domfront   = be_get_dominance_frontier(domfronts, block);
161                 int     domfront_len = ARR_LEN(domfront);
162
163                 for (i = 0; i < domfront_len; ++i) {
164                         ir_node *y = domfront[i];
165                         if (ir_nodeset_insert(blocks, y))
166                                 waitq_put(worklist, y);
167                 }
168         }
169
170         del_waitq(worklist);
171 }
172 #endif