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(const phase_t *ph, const ir_node *irn, void *data)
42 irn_height_t *h = data;
43 memset(h, 0, sizeof(h[0]));
44 INIT_LIST_HEAD(&h->sink_list);
47 static void height_dump_cb(void *data, FILE *f, const ir_node *irn)
49 heights_t *heights = data;
50 irn_height_t *h = phase_get_irn_data(&heights->ph, irn);
53 fprintf(f, "height: %u\n", h->height);
57 * Check, if we can reach a target node from a given node inside one basic block.
58 * @param h The heights object.
59 * @param curr The current node from which we tried to reach the other one.
60 * @param tgt The node we try to reach.
61 * @return 1, one of tgt can be reached from curr, 0 else.
63 static int search(heights_t *h, const ir_node *curr, const ir_node *tgt)
69 /* if the current node is the one we were looking for, we're done. */
73 /* If we are in another block we won't find our target. */
74 if(get_nodes_block(curr) != get_nodes_block(tgt))
77 /* Check, if we have already been here. Coming more often won't help :-) */
78 h_curr = phase_get_irn_data(&h->ph, curr);
79 if(h_curr->visited >= h->visited)
82 /* If we are too deep into the DAG we won't find the target either. */
83 h_tgt = phase_get_irn_data(&h->ph, tgt);
84 if(h_curr->height > h_tgt->height)
87 /* Mark this place as visited. */
88 h_curr->visited = h->visited;
90 /* Start a search from this node. */
91 for(i = 0, n = get_irn_arity(curr); i < n; ++i) {
92 ir_node *op = get_irn_n(curr, i);
93 if(search(h, op, tgt))
101 * Check, if one node can be reached from another one, according to data dependence.
103 int heights_reachable_in_block(heights_t *h, const ir_node *n, const ir_node *m)
106 irn_height_t *hn = phase_get_irn_data(&h->ph, n);
107 irn_height_t *hm = phase_get_irn_data(&h->ph, m);
109 assert(get_nodes_block(n) == get_nodes_block(m));
110 assert(hn != NULL && hm != NULL);
112 if(hn->height <= hm->height) {
114 res = search(h, n, m);
121 * Compute the height of a node in a block.
122 * @param h The heights object.
123 * @param irn The node.
124 * @param bl The block.
126 static unsigned compute_height(heights_t *h, const ir_node *irn, const ir_node *bl)
128 irn_height_t *ih = phase_get_or_set_irn_data(&h->ph, irn);
131 const ir_edge_t *edge;
133 /* bail out if we already visited that node. */
134 if(ih->visited >= h->visited)
137 ih->visited = h->visited;
142 foreach_out_edge(irn, edge) {
143 ir_node *dep = get_edge_src_irn(edge);
145 if(!is_Block(dep) && get_nodes_block(dep) == bl) {
146 unsigned dep_height = compute_height(h, dep, bl);
147 ih->height = MAX(ih->height, dep_height);
154 ih->is_sink = is_sink;
156 list_add(&ih->sink_list, &h->sink_head);
161 static void compute_heights_in_block(ir_node *bl, void *data)
164 const ir_edge_t *edge;
168 foreach_out_edge(bl, edge) {
169 ir_node *dep = get_edge_src_irn(edge);
170 compute_height(h, dep, bl);
174 unsigned get_irn_height(heights_t *heights, const ir_node *irn)
176 irn_height_t *h = phase_get_irn_data(&heights->ph, irn);
177 assert(h && "No height information for node");
181 void heights_recompute(heights_t *h)
183 edges_assure(phase_get_irg(&h->ph));
184 phase_reinit_irn_data(&h->ph);
186 INIT_LIST_HEAD(&h->sink_head);
187 irg_block_walk_graph(phase_get_irg(&h->ph), compute_heights_in_block, NULL, h);
190 heights_t *heights_new(ir_graph *irg)
192 heights_t *res = xmalloc(sizeof(res[0]));
193 phase_init(&res->ph, "heights", irg, sizeof(irn_height_t), PHASE_DEFAULT_GROWTH, irn_height_init);
194 res->dump_handle = dump_add_node_info_callback(height_dump_cb, res);
195 heights_recompute(res);
200 void heights_free(heights_t *h)
203 dump_remv_node_info_callback(h->dump_handle);