3 * File name: ir/ir/iredges.c
4 * Purpose: Always available outs.
5 * Author: Sebastian Hack
6 * Modified by: Michael Beck
9 * Copyright: (c) 1998-2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 * Always available outs.
15 * @author Sebastian Hack
32 #include "iredgekinds.h"
33 #include "iredges_t.h"
42 * A function that allows for setting an edge.
43 * This abstraction is necessary since different edge kind have
44 * different methods of setting edges.
46 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
48 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
50 typedef int (get_edge_src_first_func_t)(const ir_node *src);
52 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
55 * Additional data for an edge kind.
59 set_edge_func_t *set_edge;
60 get_edge_src_first_func_t *get_first;
61 get_edge_src_arity_func_t *get_arity;
62 get_edge_src_n_func_t *get_n;
63 } ir_edge_kind_info_t;
65 static int get_zero(const ir_node *irn)
70 static int get_irn_first(const ir_node *irn)
72 return 0 - !is_Block(irn);
75 static ir_node *get_block_n(const ir_node *irn, int pos)
77 return is_Block(irn) ? get_Block_cfgpred_block((ir_node *) irn, pos) : 0;
80 static const ir_edge_kind_info_t edge_kind_info[EDGE_KIND_LAST] = {
81 { "normal" , set_irn_n, get_irn_first, get_irn_arity, get_irn_n },
82 { "block succs", NULL, get_zero, get_irn_arity, get_block_n },
83 { "dependency", set_irn_dep, get_zero, get_irn_deps, get_irn_dep }
86 #define foreach_tgt(irn, i, n, kind) for(i = edge_kind_info[kind].get_first(irn), n = edge_kind_info[kind].get_arity(irn); i < n; ++i)
87 #define get_n(irn, pos, kind) (edge_kind_info[kind].get_n(irn, pos))
88 #define get_kind_str(kind) (edge_kind_info[kind].name)
90 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
93 * This flag is set to 1, if the edges get initialized for an irg.
94 * Then register additional data is forbidden.
96 static int edges_used = 0;
98 static int edges_private_size = 0;
100 int edges_register_private_data(size_t n)
102 int res = edges_private_size;
104 assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
106 edges_private_size += n;
110 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
112 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
114 static int edge_cmp(const void *p1, const void *p2, size_t len)
116 const ir_edge_t *e1 = p1;
117 const ir_edge_t *e2 = p2;
118 int res = e1->src == e2->src && e1->pos == e2->pos;
123 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
126 * Initialize the out information for a graph.
127 * @note Dead node elimination can call this on an already initialized graph.
129 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
131 if(edges_activated_kind(irg, kind)) {
132 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
137 amount = set_count(info->edges);
138 del_set(info->edges);
140 info->edges = new_set(edge_cmp, amount);
145 * Get the edge object of an outgoing edge at a node.
146 * @param irg The graph, the node is in.
147 * @param src The node at which the edge originates.
148 * @param pos The position of the edge.
149 * @return The corresponding edge object or NULL,
150 * if no such edge exists.
152 const ir_edge_t *get_irn_edge_kind(ir_graph *irg, const ir_node *src, int pos, ir_edge_kind_t kind)
154 if(edges_activated_kind(irg, kind)) {
155 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
158 key.src = (ir_node *) src;
160 return set_find(info->edges, &key, sizeof(key), edge_hash(&key));
167 * Change the out count
169 static INLINE void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs) {
170 irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
171 info->out_count += ofs;
174 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
175 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_edge_kind_t kind, ir_graph *irg)
177 const char *msg = "";
179 if(!edges_activated_kind(irg, kind))
183 * Only do something, if the old and new target differ.
186 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
187 set *edges = info->edges;
188 ir_edge_t *templ = alloca(sizeof(templ[0]));
191 /* Initialize the edge template to search in the set. */
192 memset(templ, 0, sizeof(templ[0]));
198 DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
201 * If the target is NULL, the edge shall be deleted.
204 /* search the edge in the set. */
205 edge = set_find(edges, templ, sizeof(templ[0]), edge_hash(templ));
207 /* mark the edge invalid if it was found */
210 list_del(&edge->list);
216 /* If the edge was not found issue a warning on the debug stream */
218 msg = "edge to delete not found!\n";
223 * The target is not NULL and the old target differs
224 * from the new target, the edge shall be moved (if the
225 * old target was != NULL) or added (if the old target was
229 struct list_head *head = _get_irn_outs_head(tgt, kind);
231 assert(head->next && head->prev &&
232 "target list head must have been initialized");
235 * insert the edge, if it is not yet in the set or return
236 * the instance in the set.
238 edge = set_insert(edges, templ, sizeof(templ[0]), edge_hash(templ));
241 assert(!edge->invalid && "Invalid edge encountered");
244 /* If the old target is not null, the edge is moved. */
248 list_move(&edge->list, head);
249 edge_change_cnt(old_tgt, kind, -1);
252 /* The old target was null, thus, the edge is newly created. */
255 list_add(&edge->list, head);
258 edge_change_cnt(tgt, kind, +1);
262 /* If the target and the old target are equal, nothing is done. */
263 DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
266 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
268 edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
270 /* do not use get_nodes_block() here, it fails when running unpinned */
271 ir_node *bl_old = old_tgt ? get_irn_n(skip_Proj(old_tgt), -1) : NULL;
272 ir_node *bl_tgt = NULL;
275 bl_tgt = is_Bad(tgt) ? tgt : get_irn_n(skip_Proj(tgt), -1);
277 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
282 void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
284 if(edges_activated_kind(irg, kind)) {
287 DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
289 foreach_tgt(old, i, n, kind) {
290 ir_node *old_tgt = get_n(old, i, kind);
291 edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
296 struct build_walker {
302 * Post-Walker: notify all edges
304 static void build_edges_walker(ir_node *irn, void *data) {
305 struct build_walker *w = data;
308 foreach_tgt(irn, i, n, w->kind)
309 edges_notify_edge_kind(irn, i, get_n(irn, i, w->kind), NULL, w->kind, w->irg);
313 * Pre-Walker: initializes the list-heads and set the out-count
316 static void init_lh_walker(ir_node *irn, void *data) {
317 struct build_walker *w = data;
318 INIT_LIST_HEAD(_get_irn_outs_head(irn, w->kind));
319 _get_irn_edge_info(irn, w->kind)->out_count = 0;
323 * Visitor: initializes the list-heads and set the out-count
324 * of all nodes to 0 of nodes that are not seen so far.
326 static void visitor(ir_node *irn, void *data) {
327 if (irn_not_visited(irn)) {
328 mark_irn_visited(irn);
329 init_lh_walker(irn, data);
334 * Build the initial edge set.
335 * Beware, this is not a simple task because it suffers from two
337 * - the anchor set allows access to Nodes that may not be reachable from
339 * - the identities add nodes to the "root set" that are not yet reachable
340 * from End. However, after some transformations, the CSE may revival these
343 * These problems can be fixed using different strategies:
344 * - Add an age flag to every node. Whenever the edge of a node is older
345 * then the current edge, invalidate the edges of this node.
346 * While this would help for revivaled nodes, it increases memory and runtime.
347 * - Delete the identities set.
348 * Solves the revival problem, but may increase the memory consumption, as
349 * nodes cannot be revivaled at all.
350 * - Manually iterate over the identities root set. This did not consume more memory
351 * but increase the computation time because the |identities| >= |V|
353 * Currently, we use the last option.
355 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
357 struct build_walker w;
358 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
364 edges_init_graph_kind(irg, kind);
365 irg_walk_graph(irg, init_lh_walker, build_edges_walker, &w);
366 irg_walk_anchors(irg, init_lh_walker, NULL, &w);
367 visit_all_identities(irg, visitor, &w);
370 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
372 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
376 del_set(info->edges);
381 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
383 return _edges_activated_kind(irg, kind);
388 * Reroute all use-edges from a node to another.
389 * @param from The node whose use-edges shall be withdrawn.
390 * @param to The node to which all the use-edges of @p from shall be
392 * @param irg The graph.
394 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind, ir_graph *irg)
396 set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
398 if(set_edge && edges_activated_kind(irg, kind)) {
399 struct list_head *head = _get_irn_outs_head(from, kind);
401 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
403 while(head != head->next) {
404 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
405 assert(edge->pos >= -1);
406 set_edge(edge->src, edge->pos, to);
411 static void verify_set_presence(ir_node *irn, void *data)
413 struct build_walker *w = data;
414 set *edges = _get_irg_edge_info(w->irg, w->kind)->edges;
417 foreach_tgt(irn, i, n, w->kind) {
423 e = set_find(edges, &templ, sizeof(templ), edge_hash(&templ));
427 DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d (kind: \"%s\") is missing\n", irn, i, get_kind_str(w->kind)));
431 static void verify_list_presence(ir_node *irn, void *data)
433 struct build_walker *w = data;
436 foreach_out_edge_kind(irn, e, w->kind) {
437 ir_node *tgt = get_n(e->src, e->pos, w->kind);
439 DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d (kind \"%s\") is no out edge of %+F but of %+F\n", e->src, e->pos, get_kind_str(w->kind), irn, tgt));
443 void edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
445 struct build_walker w;
446 set *edges = _get_irg_edge_info(irg, kind)->edges;
452 /* Clear the present bit in all edges available. */
453 for(e = set_first(edges); e; e = set_next(edges))
456 irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
459 * Dump all edges which are not invalid and not present.
460 * These edges are superfluous and their presence in the
463 for(e = set_first(edges); e; e = set_next(edges)) {
464 if(!e->invalid && !e->present)
465 DBG((dbg, LEVEL_DEFAULT, "edge %+F,%d is superfluous\n", e->src, e->pos));
469 void init_edges(void)
471 FIRM_DBG_REGISTER(dbg, DBG_EDGES);
472 /* firm_dbg_set_mask(dbg, -1); */
475 void edges_activate(ir_graph *irg)
477 edges_activate_kind(irg, EDGE_KIND_NORMAL);
478 edges_activate_kind(irg, EDGE_KIND_BLOCK);
481 void edges_deactivate(ir_graph *irg)
483 edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
484 edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
487 int edges_assure(ir_graph *irg)
489 int activated = edges_activated(irg);
497 void edges_node_deleted(ir_node *irn, ir_graph *irg)
499 edges_node_deleted_kind(irn, EDGE_KIND_NORMAL, irg);
500 edges_node_deleted_kind(irn, EDGE_KIND_BLOCK, irg);
504 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
506 return _get_irn_out_edge_first_kind(irn, kind);
509 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
511 return _get_irn_out_edge_next(irn, last);
514 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
516 return _get_edge_src_irn(edge);
519 int (get_edge_src_pos)(const ir_edge_t *edge)
521 return _get_edge_src_pos(edge);
524 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
526 return _get_irn_n_edges_kind(irn, kind);
529 void dump_all_out_edges(ir_node *irn)
532 for(i = 0; i < EDGE_KIND_LAST; ++i) {
533 const ir_edge_t *edge;
535 printf("kind \"%s\"\n", get_kind_str(i));
536 foreach_out_edge_kind(irn, edge, i) {
537 ir_printf("\t%+F(%d)\n", edge->src, edge->pos);