2 * Always available outs.
3 * @author Sebastian Hack
12 #include "iredges_t.h"
19 static firm_dbg_module_t *dbg;
21 #if FIRM_EDGES_INPLACE
24 * This flag is set to 1, if the edges get initialized for an irg.
25 * Then register additional data is forbidden.
27 static int edges_used = 0;
29 static int edges_private_size = 0;
31 int edges_register_private_data(size_t n)
33 int res = edges_private_size;
35 assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
37 edges_private_size += n;
41 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
43 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
45 static int edge_cmp(const void *p1, const void *p2, size_t len)
47 const ir_edge_t *e1 = p1;
48 const ir_edge_t *e2 = p2;
49 int res = e1->src == e2->src && e1->pos == e2->pos;
54 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
57 * Initialize the out information for a graph.
58 * @note Dead node elim can call this on an already initialized graph.
60 void edges_init_graph(ir_graph *irg)
62 if(edges_activated(irg)) {
63 irg_edge_info_t *info = _get_irg_edge_info(irg);
69 amount = set_count(info->edges);
73 info->edges = new_set(edge_cmp, amount);
78 * Get the edge object of an outgoing edge at a node.
79 * @param irg The graph, the node is in.
80 * @param src The node at which the edge originates.
81 * @param pos The position of the edge.
82 * @return The corresponding edge object or NULL,
83 * if no such edge exists.
85 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
87 if(edges_activated(irg)) {
88 irg_edge_info_t *info = _get_irg_edge_info(irg);
91 templ.src = (ir_node *) src;
93 return set_find(info->edges, &templ, sizeof(templ), edge_hash(&templ));
99 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
101 const char *msg = "";
103 if(!edges_activated(irg))
106 assert(node_is_in_irgs_storage(irg, src) && "source not in irg");
109 * Only do something, if the old and new target differ.
112 int is_block_edge = is_Block(src);
113 set *edges = _get_irg_edge_info(irg)->edges;
114 ir_block_edge_t space;
115 ir_edge_t *templ = (ir_edge_t *) &space;
120 * This is scray, but:
121 * If two entries in a set do not have the same size, they are
122 * treated as unequal, ignoring the comparison function.
123 * So, edges from blocks have extra storage (they are
124 * ir_block_edge_t's).
126 * Also add the amount of registered private data to the
129 size = edges_private_size +
130 is_block_edge ? sizeof(ir_block_edge_t) : sizeof(ir_edge_t);
132 /* Initialize the edge template to search in the set. */
133 memset(templ, 0, size);
135 templ->src_nr = get_irn_node_nr(src);
143 * If the target is NULL, the edge shall be deleted.
146 /* search the edge in the set. */
147 edge = set_find(edges, templ, size, edge_hash(templ));
149 /* mark the edge invalid if it was found */
151 ir_block_edge_t *block_edge = (ir_block_edge_t *) edge;
154 list_del(&edge->list);
160 * If the edge is a cf edge, we delete it also
161 * from the list of all block successor edges.
164 list_del(&block_edge->succ_list);
167 /* If the edge was not found issue a warning on the debug stream */
169 msg = "edge to delete not found!\n";
174 * The target is not NULL and the old target differs
175 * from the new target, the edge shall be moved (if the
176 * old target was != NULL) or added (if the old target was
180 struct list_head *head = _get_irn_outs_head(tgt);
183 * The list head in the block of the edges target.
184 * Therein all control flow edges directed at that block
187 struct list_head *succ_head =
188 is_block_edge ? _get_block_succ_head(get_nodes_block(tgt)) : NULL;
190 ir_block_edge_t *block_edge;
192 if(!node_is_in_irgs_storage(irg, tgt))
195 assert(head->next && head->prev &&
196 "target list head must have been initialized");
199 * insert the edge, if it is not yet in the set or return
200 * the instance in the set.
202 edge = set_insert(edges, templ, size, edge_hash(templ));
203 block_edge = (ir_block_edge_t *) edge;
206 assert(!edge->invalid && "Invalid edge encountered");
209 /* If the old target is not null, the edge is moved. */
212 list_move(&edge->list, head);
214 /* If the edge is a cf edge, move it from the successor list. */
216 list_move(&block_edge->succ_list, succ_head);
218 _get_irn_edge_info(old_tgt)->out_count -= 1;
221 /* The old target was null, thus, the edge is newly created. */
224 list_add(&edge->list, head);
227 * If the edge is cf edge, enter it into the successor list
228 * of the target node's block.
231 list_add(&block_edge->succ_list, succ_head);
234 _get_irn_edge_info(tgt)->out_count += 1;
238 /* If the target and the old target are equal, nothing is done. */
239 DBG((dbg, LEVEL_5, "announce out edge: %n[%p] %d-> %n[%p](%n[%p]): %s\n",
240 src, src, pos, tgt, tgt, old_tgt, old_tgt, msg));
243 void edges_node_deleted(ir_node *old, ir_graph *irg)
245 if(edges_activated(irg)) {
246 int not_a_block = !is_Block(old);
251 DBG((dbg, LEVEL_5, "node deleted: %n\n", old));
253 /* Change to get_irn_n */
254 for(i = -not_a_block, n = get_irn_arity(old); i < n; ++i) {
255 ir_node *old_tgt = get_irn_n(old, i);
256 DBG((dbg, LEVEL_5, "\tdelete to old target %n\n", old_tgt));
257 edges_notify_edge(old, i, NULL, old_tgt, irg);
263 void edges_invalidate(ir_node *irn, ir_graph *irg)
265 edges_node_deleted(irn, irg);
268 static void build_edges_walker(ir_node *irn, void *data)
270 ir_graph *irg = data;
271 int not_a_block = !is_Block(irn);
274 for(i = -not_a_block, n = get_irn_arity(irn); i < n; ++i)
275 edges_notify_edge(irn, i, get_irn_n(irn, i), NULL, irg);
278 static void init_lh_walker(ir_node *irn, void *data)
280 INIT_LIST_HEAD(_get_irn_outs_head(irn));
282 INIT_LIST_HEAD(_get_block_succ_head(irn));
285 void edges_activate(ir_graph *irg)
287 irg_edge_info_t *info = _get_irg_edge_info(irg);
290 edges_init_graph(irg);
291 irg_walk_graph(irg, init_lh_walker, build_edges_walker, irg);
294 void edges_deactivate(ir_graph *irg)
296 irg_edge_info_t *info = _get_irg_edge_info(irg);
300 del_set(info->edges);
305 int (edges_activated)(const ir_graph *irg)
307 return _edges_activated(irg);
312 * Reroute all use-edges from a node to another.
313 * @param from The node whose use-edges shall be withdrawn.
314 * @param to The node to which all the use-edges of @p from shall be
317 void edges_reroute(ir_node *from, ir_node *to, ir_graph *irg)
319 if(edges_activated(irg)) {
320 struct list_head *head = _get_irn_outs_head(from);
322 DBG((firm_dbg_register(DBG_EDGES), LEVEL_5,
323 "reroute from %n to %n\n", from, to));
325 while(head != head->next) {
326 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
327 // DBG((dbg, LEVEL_5, "\t%n %d\n", edge->src, edge->pos));
328 assert(edge->pos >= -1);
329 set_irn_n(edge->src, edge->pos, to);
334 static void verify_set_presence(ir_node *irn, void *data)
336 ir_graph *irg = data;
337 set *edges = _get_irg_edge_info(irg)->edges;
338 int not_a_block = !is_Block(irn);
341 for(i = 0, n = get_irn_arity(irn) + not_a_block; i < n; ++i) {
342 ir_block_edge_t space;
343 ir_edge_t *templ = (ir_edge_t *) &space;
345 size_t size = not_a_block ? sizeof(ir_edge_t) : sizeof(ir_block_edge_t);
348 templ->pos = i - not_a_block;
350 e = set_find(edges, templ, size, edge_hash(templ));
354 DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is missing\n", irn, templ->pos));
358 static void verify_list_presence(ir_node *irn, void *data)
362 foreach_out_edge(irn, e) {
363 ir_node *tgt = get_irn_n(e->src, e->pos);
365 DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is no out edge of %n but of %n\n",
366 e->src, e->pos, irn, tgt));
371 void edges_verify(ir_graph *irg)
373 set *edges = _get_irg_edge_info(irg)->edges;
376 /* Clear the present bit in all edges available. */
377 for(e = set_first(edges); e; e = set_next(edges))
380 irg_walk_graph(irg, verify_set_presence, verify_list_presence, irg);
383 * Dump all edges which are not invalid and not present.
384 * These edges are superfluous and their presence in the
387 for(e = set_first(edges); e; e = set_next(edges)) {
388 if(!e->invalid && !e->present)
389 DBG((dbg, LEVEL_DEFAULT, "edge %n,%d is superfluous\n", e->src, e->pos));
393 void init_edges(void)
395 dbg = firm_dbg_register(DBG_EDGES);
396 /* firm_dbg_set_mask(dbg, -1); */
400 const ir_edge_t *(get_irn_out_edge_first)(const ir_node *irn)
402 return _get_irn_out_edge_first(irn);
405 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
407 return _get_irn_out_edge_next(irn, last);
410 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
412 return _get_edge_src_irn(edge);
415 int (get_edge_src_pos)(const ir_edge_t *edge)
417 return _get_edge_src_pos(edge);
420 int (get_irn_n_edges)(const ir_node *irn)
422 return _get_irn_n_edges(irn);
426 #endif /* FIRM_EDGES_INPLACE */