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 Compute heights of nodes inside basic blocks
23 * @author Sebastian Hack
39 #include "irnodemap.h"
40 #include "iredges_t.h"
54 static irn_height_t *maybe_get_height_data(const ir_heights_t *heights,
57 irn_height_t *height = (irn_height_t*)ir_nodemap_get(&heights->data, node);
61 static irn_height_t *get_height_data(ir_heights_t *heights, const ir_node *node)
63 irn_height_t *height = (irn_height_t*)ir_nodemap_get(&heights->data, node);
65 height = obstack_alloc(&heights->obst, sizeof(*height));
66 memset(height, 0, sizeof(*height));
67 ir_nodemap_insert(&heights->data, node, height);
72 static void height_dump_cb(void *data, FILE *f, const ir_node *irn)
74 const ir_heights_t *heights = (const ir_heights_t*) data;
75 const irn_height_t *h = maybe_get_height_data(heights, irn);
77 fprintf(f, "height: %u\n", h->height);
81 * Check, if we can reach a target node from a given node inside one basic block.
82 * @param h The heights object.
83 * @param curr The current node from which we tried to reach the other one.
84 * @param tgt The node we try to reach.
85 * @return 1, one of tgt can be reached from curr, 0 else.
87 static bool search(ir_heights_t *h, const ir_node *curr, const ir_node *tgt)
93 /* if the current node is the one we were looking for, we're done. */
97 /* If we are in another block or at a phi we won't find our target. */
98 if (get_nodes_block(curr) != get_nodes_block(tgt))
103 /* Check, if we have already been here. Coming more often won't help :-) */
104 h_curr = get_height_data(h, curr);
105 if (h_curr->visited >= h->visited)
108 /* If we are too deep into the DAG we won't find the target either. */
109 h_tgt = get_height_data(h, tgt);
110 if (h_curr->height > h_tgt->height)
113 /* Mark this place as visited. */
114 h_curr->visited = h->visited;
116 /* Start a search from this node. */
117 for (i = 0, n = get_irn_ins_or_deps(curr); i < n; ++i) {
118 ir_node *op = get_irn_in_or_dep(curr, i);
119 if (search(h, op, tgt))
127 * Check, if one node can be reached from another one, according to data
130 int heights_reachable_in_block(ir_heights_t *h, const ir_node *n,
134 irn_height_t *hn = get_height_data(h, n);
135 irn_height_t *hm = get_height_data(h, m);
137 assert(get_nodes_block(n) == get_nodes_block(m));
138 assert(hn != NULL && hm != NULL);
140 if (hn->height <= hm->height) {
142 res = search(h, n, m);
149 * Compute the height of a node in a block.
150 * @param h The heights object.
151 * @param irn The node.
152 * @param bl The block.
154 static unsigned compute_height(ir_heights_t *h, ir_node *irn, const ir_node *bl)
156 irn_height_t *ih = get_height_data(h, irn);
158 const ir_edge_t *edge;
160 /* bail out if we already visited that node. */
161 if (ih->visited >= h->visited)
164 ih->visited = h->visited;
167 foreach_out_edge(irn, edge) {
168 ir_node *dep = get_edge_src_irn(edge);
170 if (!is_Block(dep) && !is_Phi(dep) && get_nodes_block(dep) == bl) {
171 unsigned dep_height = compute_height(h, dep, bl);
172 ih->height = MAX(ih->height, dep_height);
178 foreach_out_edge_kind(irn, edge, EDGE_KIND_DEP) {
179 ir_node *dep = get_edge_src_irn(edge);
181 assert(!is_Phi(dep));
182 if (!is_Block(dep) && get_nodes_block(dep) == bl) {
183 unsigned dep_height = compute_height(h, dep, bl);
184 ih->height = MAX(ih->height, dep_height);
193 static unsigned compute_heights_in_block(ir_node *bl, ir_heights_t *h)
196 const ir_edge_t *edge;
200 foreach_out_edge(bl, edge) {
201 ir_node *dep = get_edge_src_irn(edge);
202 int curh = compute_height(h, dep, bl);
204 max_height = MAX(curh, max_height);
207 foreach_out_edge_kind(bl, edge, EDGE_KIND_DEP) {
208 ir_node *dep = get_edge_src_irn(edge);
209 int curh = compute_height(h, dep, bl);
211 max_height = MAX(curh, max_height);
217 static void compute_heights_in_block_walker(ir_node *block, void *data)
219 ir_heights_t *h = (ir_heights_t*) data;
220 compute_heights_in_block(block, h);
223 unsigned get_irn_height(const ir_heights_t *heights, const ir_node *irn)
225 const irn_height_t *height = maybe_get_height_data(heights, irn);
226 assert(height != NULL && "No height information for node");
227 return height->height;
230 unsigned heights_recompute_block(ir_heights_t *h, ir_node *block)
232 ir_graph *irg = get_irn_irg(block);
233 const ir_edge_t *edge;
237 /* reset phase data for all nodes in the block */
238 foreach_out_edge(block, edge) {
239 ir_node *irn = get_edge_src_irn(edge);
240 irn_height_t *ih = get_height_data(h, irn);
241 memset(ih, 0, sizeof(*ih));
245 return compute_heights_in_block(block, h);
248 ir_heights_t *heights_new(ir_graph *irg)
250 ir_heights_t *res = XMALLOC(ir_heights_t);
251 ir_nodemap_init(&res->data, irg);
252 obstack_init(&res->obst);
253 res->dump_handle = dump_add_node_info_callback(height_dump_cb, res);
256 irg_block_walk_graph(irg, compute_heights_in_block_walker, NULL, res);
261 void heights_free(ir_heights_t *h)
263 dump_remove_node_info_callback(h->dump_handle);
264 obstack_free(&h->obst, NULL);
265 ir_nodemap_destroy(&h->data);