3 * File name: ir/ana/height.c
4 * Purpose: Compute heights of nodes inside basic blocks
5 * Author: Sebastian Hack
9 * Copyright: (c) 2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 #include "irphase_t.h"
22 #include "iredges_t.h"
24 typedef struct _heights_t heights_t;
30 struct list_head sink_head;
37 struct list_head sink_list;
40 static void *irn_height_init(phase_t *ph, ir_node *irn, void *data)
42 irn_height_t *h = data ? data : phase_alloc(ph, sizeof(h[0]));
43 memset(h, 0, sizeof(h[0]));
44 INIT_LIST_HEAD(&h->sink_list);
48 static void height_dump_cb(void *data, FILE *f, const ir_node *irn)
50 heights_t *heights = data;
51 irn_height_t *h = phase_get_irn_data(&heights->ph, irn);
54 fprintf(f, "height: %u\n", h->height);
58 * Check, if we can reach a target node from a given node inside one basic block.
59 * @param h The heights object.
60 * @param curr The current node from which we tried to reach the other one.
61 * @param tgt The node we try to reach.
62 * @return 1, one of tgt can be reached from curr, 0 else.
64 static int search(heights_t *h, const ir_node *curr, const ir_node *tgt)
70 /* if the current node is the one we were looking for, we're done. */
74 /* If we are in another block we won't find our target. */
75 if(get_nodes_block(curr) != get_nodes_block(tgt))
78 /* Check, if we have already been here. Coming more often won't help :-) */
79 h_curr = phase_get_irn_data(&h->ph, curr);
80 if(h_curr->visited >= h->visited)
83 /* If we are too deep into the DAG we won't find the target either. */
84 h_tgt = phase_get_irn_data(&h->ph, tgt);
85 if(h_curr->height > h_tgt->height)
88 /* Mark this place as visited. */
89 h_curr->visited = h->visited;
91 /* Start a search from this node. */
92 for(i = 0, n = get_irn_arity(curr); i < n; ++i) {
93 ir_node *op = get_irn_n(curr, i);
94 if(search(h, op, tgt))
102 * Check, if one node can be reached from another one, according to data dependence.
104 int heights_reachable_in_block(heights_t *h, const ir_node *n, const ir_node *m)
107 irn_height_t *hn = phase_get_irn_data(&h->ph, n);
108 irn_height_t *hm = phase_get_irn_data(&h->ph, m);
110 assert(get_nodes_block(n) == get_nodes_block(m));
111 assert(hn != NULL && hm != NULL);
113 if(hn->height <= hm->height) {
115 res = search(h, n, m);
122 * Compute the height of a node in a block.
123 * @param h The heights object.
124 * @param irn The node.
125 * @param bl The block.
127 static unsigned compute_height(heights_t *h, ir_node *irn, const ir_node *bl)
129 irn_height_t *ih = phase_get_or_set_irn_data(&h->ph, irn);
132 const ir_edge_t *edge;
134 /* bail out if we already visited that node. */
135 if(ih->visited >= h->visited)
138 ih->visited = h->visited;
143 foreach_out_edge(irn, edge) {
144 ir_node *dep = get_edge_src_irn(edge);
146 if(!is_Block(dep) && get_nodes_block(dep) == bl) {
147 unsigned dep_height = compute_height(h, dep, bl);
148 ih->height = MAX(ih->height, dep_height);
155 ih->is_sink = is_sink;
157 list_add(&ih->sink_list, &h->sink_head);
162 static void compute_heights_in_block(ir_node *bl, void *data)
165 const ir_edge_t *edge;
169 foreach_out_edge(bl, edge) {
170 ir_node *dep = get_edge_src_irn(edge);
171 compute_height(h, dep, bl);
175 unsigned get_irn_height(heights_t *heights, const ir_node *irn)
177 irn_height_t *h = phase_get_irn_data(&heights->ph, irn);
178 assert(h && "No height information for node");
182 void heights_recompute(heights_t *h)
184 edges_assure(phase_get_irg(&h->ph));
185 phase_reinit_irn_data(&h->ph);
187 INIT_LIST_HEAD(&h->sink_head);
188 irg_block_walk_graph(phase_get_irg(&h->ph), compute_heights_in_block, NULL, h);
191 heights_t *heights_new(ir_graph *irg)
193 heights_t *res = xmalloc(sizeof(res[0]));
194 phase_init(&res->ph, "heights", irg, PHASE_DEFAULT_GROWTH, irn_height_init);
195 res->dump_handle = dump_add_node_info_callback(height_dump_cb, res);
196 heights_recompute(res);
201 void heights_free(heights_t *h)
204 dump_remv_node_info_callback(h->dump_handle);