3 * File name: ir/ir/iredges.c
4 * Purpose: Always available outs.
5 * Author: Sebastian Hack
6 * Modified by: Michael Beck, Andreas Schoesser
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;
99 #define EDGE_SIZE (sizeof(ir_edge_t) + edges_private_size)
102 * If set to 1, the list heads are checked every time an edge is changed.
104 static int edges_dbg = 0;
106 int edges_register_private_data(size_t n)
108 int res = edges_private_size;
110 assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
112 edges_private_size += n;
117 * Reset the user's private data at offset 'offset'
120 void edges_reset_private_data(ir_graph *irg, int offset, size_t size)
122 irg_edge_info_t *info = _get_irg_edge_info(irg, EDGE_KIND_NORMAL);
125 foreach_set(info->edges, entry)
127 ir_edge_t *edge = (ir_edge_t *) entry->dptr;
128 memset(edge + sizeof(*edge) + offset, 0, size);
132 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
134 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
136 static int edge_cmp(const void *p1, const void *p2, size_t len)
138 const ir_edge_t *e1 = p1;
139 const ir_edge_t *e2 = p2;
140 int res = e1->src == e2->src && e1->pos == e2->pos;
145 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
148 * Initialize the out information for a graph.
149 * @note Dead node elimination can call this on an already initialized graph.
151 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
153 if(edges_activated_kind(irg, kind)) {
154 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
159 amount = set_count(info->edges);
160 del_set(info->edges);
162 info->edges = new_set(edge_cmp, amount);
167 * Get the edge object of an outgoing edge at a node.
168 * @param irg The graph, the node is in.
169 * @param src The node at which the edge originates.
170 * @param pos The position of the edge.
171 * @return The corresponding edge object or NULL,
172 * if no such edge exists.
174 const ir_edge_t *get_irn_edge_kind(ir_graph *irg, const ir_node *src, int pos, ir_edge_kind_t kind)
176 if (edges_activated_kind(irg, kind)) {
177 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
180 key.src = (ir_node *)src;
182 return set_find(info->edges, &key, EDGE_SIZE, edge_hash(&key));
189 * Get the edge object of an outgoing edge at a node.
190 * Looks for an edge for all kinds.
193 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
195 const ir_edge_t *edge;
196 if((edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_NORMAL)) == NULL)
197 edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_BLOCK);
202 * Change the out count
204 static INLINE void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs) {
205 irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
206 info->out_count += ofs;
210 * Verify the edge list of a node, ie. ensure it's a loop:
211 * head -> e_1 -> ... -> e_n -> head
213 static INLINE void vrfy_list_head(ir_node *irn, ir_edge_kind_t kind) {
216 pset *lh_set = pset_new_ptr(16);
217 const struct list_head *head = _get_irn_outs_head(irn, kind);
218 const struct list_head *pos;
220 list_for_each(pos, head) {
221 if (pset_find_ptr(lh_set, pos)) {
222 const ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
224 ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
225 fprintf(stderr, "- at list entry %d\n", num);
227 fprintf(stderr, "- edge is invalid\n");
229 ir_fprintf(stderr, "- edge %+F(%d)\n", edge->src, edge->pos);
234 pset_insert_ptr(lh_set, pos);
242 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
243 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)
245 const char *msg = "";
247 assert(edges_activated_kind(irg, kind));
250 * Only do something, if the old and new target differ.
253 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
254 set *edges = info->edges;
255 ir_edge_t *templ = alloca(EDGE_SIZE);
258 /* Initialize the edge template to search in the set. */
259 memset(templ, 0, EDGE_SIZE);
265 DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
268 * If the target is NULL, the edge shall be deleted.
271 /* search the edge in the set. */
272 edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
274 /* mark the edge invalid if it was found */
277 list_del(&edge->list);
281 edge_change_cnt(old_tgt, kind, -1);
284 /* If the edge was not found issue a warning on the debug stream */
286 msg = "edge to delete not found!\n";
291 * The target is not NULL and the old target differs
292 * from the new target, the edge shall be moved (if the
293 * old target was != NULL) or added (if the old target was
297 struct list_head *head = _get_irn_outs_head(tgt, kind);
299 assert(head->next && head->prev &&
300 "target list head must have been initialized");
302 /* If the old target is not null, the edge is moved. */
304 edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
305 assert(edge && "edge to redirect not found!");
306 assert(! edge->invalid && "Invalid edge encountered");
310 list_move(&edge->list, head);
311 edge_change_cnt(old_tgt, kind, -1);
314 /* The old target was null, thus, the edge is newly created. */
316 edge = set_insert(edges, templ, EDGE_SIZE, edge_hash(templ));
318 assert(! edge->invalid && "Freshly inserted edge is invalid?!?");
319 assert(edge->list.next == NULL && edge->list.prev == NULL &&
320 "New edge must not have list head initialized");
323 list_add(&edge->list, head);
326 edge_change_cnt(tgt, kind, +1);
329 /* verify list heads */
332 vrfy_list_head(tgt, kind);
334 vrfy_list_head(old_tgt, kind);
338 /* If the target and the old target are equal, nothing is done. */
339 DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
342 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
344 if(edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
345 edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
348 if (edges_activated_kind(irg, EDGE_KIND_BLOCK) && is_Block(src)) {
349 /* do not use get_nodes_block() here, it fails when running unpinned */
350 ir_node *bl_old = old_tgt ? get_irn_n(skip_Proj(old_tgt), -1) : NULL;
351 ir_node *bl_tgt = NULL;
354 bl_tgt = is_Bad(tgt) ? tgt : get_irn_n(skip_Proj(tgt), -1);
356 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
361 void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
365 if(!edges_activated_kind(irg, kind))
368 DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
370 foreach_tgt(old, i, n, kind) {
371 ir_node *old_tgt = get_n(old, i, kind);
372 edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
376 struct build_walker {
379 unsigned problem_found;
383 * Post-Walker: notify all edges
385 static void build_edges_walker(ir_node *irn, void *data) {
386 struct build_walker *w = data;
389 if(!edges_activated_kind(w->irg, w->kind))
392 foreach_tgt(irn, i, n, w->kind)
393 edges_notify_edge_kind(irn, i, get_n(irn, i, w->kind), NULL, w->kind, w->irg);
397 * Pre-Walker: initializes the list-heads and set the out-count
400 static void init_lh_walker(ir_node *irn, void *data) {
401 struct build_walker *w = data;
402 INIT_LIST_HEAD(_get_irn_outs_head(irn, w->kind));
403 _get_irn_edge_info(irn, w->kind)->out_count = 0;
407 * Visitor: initializes the list-heads and set the out-count
408 * of all nodes to 0 of nodes that are not seen so far.
410 static void visitor(ir_node *irn, void *data) {
411 if (irn_not_visited(irn)) {
412 mark_irn_visited(irn);
413 init_lh_walker(irn, data);
418 * Build the initial edge set.
419 * Beware, this is not a simple task because it suffers from two
421 * - the anchor set allows access to Nodes that may not be reachable from
423 * - the identities add nodes to the "root set" that are not yet reachable
424 * from End. However, after some transformations, the CSE may revival these
427 * These problems can be fixed using different strategies:
428 * - Add an age flag to every node. Whenever the edge of a node is older
429 * then the current edge, invalidate the edges of this node.
430 * While this would help for revivaled nodes, it increases memory and runtime.
431 * - Delete the identities set.
432 * Solves the revival problem, but may increase the memory consumption, as
433 * nodes cannot be revivaled at all.
434 * - Manually iterate over the identities root set. This did not consume more memory
435 * but increase the computation time because the |identities| >= |V|
437 * Currently, we use the last option.
439 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
441 struct build_walker w;
442 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
448 edges_init_graph_kind(irg, kind);
449 //irg_walk_graph(irg, init_lh_walker, build_edges_walker, &w);
450 inc_irg_visited(irg);
451 irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
452 visit_all_identities(irg, visitor, &w);
455 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
457 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
461 del_set(info->edges);
466 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
468 return _edges_activated_kind(irg, kind);
473 * Reroute all use-edges from a node to another.
474 * @param from The node whose use-edges shall be withdrawn.
475 * @param to The node to which all the use-edges of @p from shall be
477 * @param irg The graph.
479 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind, ir_graph *irg)
481 set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
483 if(set_edge && edges_activated_kind(irg, kind)) {
484 struct list_head *head = _get_irn_outs_head(from, kind);
486 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
488 while(head != head->next) {
489 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
490 assert(edge->pos >= -1);
491 set_edge(edge->src, edge->pos, to);
496 static void verify_set_presence(ir_node *irn, void *data)
498 struct build_walker *w = data;
499 set *edges = _get_irg_edge_info(w->irg, w->kind)->edges;
502 foreach_tgt(irn, i, n, w->kind) {
508 e = set_find(edges, &templ, EDGE_SIZE, edge_hash(&templ));
512 w->problem_found = 1;
513 ir_fprintf(stderr, "Edge Verifier: edge %+F,%d (kind: \"%s\") is missing\n", irn, i, get_kind_str(w->kind));
518 static void verify_list_presence(ir_node *irn, void *data)
520 struct build_walker *w = data;
523 /* check list heads */
524 vrfy_list_head(irn, w->kind);
526 foreach_out_edge_kind(irn, e, w->kind) {
527 ir_node *tgt = get_n(e->src, e->pos, w->kind);
529 w->problem_found = 1;
530 ir_fprintf(stderr, "Edge Verifier: edge %+F,%d (kind \"%s\") is no out edge of %+F but of %+F\n",
531 e->src, e->pos, get_kind_str(w->kind), irn, tgt);
536 int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
538 struct build_walker w;
539 set *edges = _get_irg_edge_info(irg, kind)->edges;
546 /* Clear the present bit in all edges available. */
547 for (e = set_first(edges); e; e = set_next(edges))
550 irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
553 * Dump all edges which are not invalid and not present.
554 * These edges are superfluous and their presence in the
557 for (e = set_first(edges); e; e = set_next(edges)) {
558 if (! e->invalid && ! e->present) {
560 ir_fprintf(stderr, "Edge Verifier: edge %+F,%d is superfluous\n", e->src, e->pos);
564 return w.problem_found;
568 * Clear link field of all nodes.
570 static void clear_links(ir_node *irn, void *env) {
574 set_irn_link(irn, 0);
578 * Increases count (stored in link field) for all operands of a node.
580 static void count_user(ir_node *irn, void *env) {
583 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
584 ir_node *op = get_irn_n(irn, i);
585 int cnt = (int)get_irn_link(op);
588 set_irn_link(op, (void *)cnt);
593 * Verifies if collected count, number of edges in list and stored edge count are in sync.
595 static void verify_edge_counter(ir_node *irn, void *env) {
596 struct build_walker *w = env;
598 int ref_cnt = (int)get_irn_link(irn);
599 int edge_cnt = _get_irn_edge_info(irn, EDGE_KIND_NORMAL)->out_count;
600 const struct list_head *head = _get_irn_outs_head(irn, EDGE_KIND_NORMAL);
601 const struct list_head *pos;
606 /* We can iterate safely here, list heads have already been verified. */
607 list_for_each(pos, head)
610 if (edge_cnt != list_cnt) {
611 w->problem_found = 1;
612 ir_fprintf(stderr, "Edge Verifier: edge count is %d, but %d edges are recorded in list at %+F\n",
613 edge_cnt, list_cnt, irn);
616 if (ref_cnt != list_cnt) {
617 w->problem_found = 1;
618 ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d nodes, but %d edges recorded in list\n",
619 irn, ref_cnt, list_cnt);
624 * Verifies the out edges of an irg.
626 int edges_verify(ir_graph *irg) {
628 struct build_walker w;
629 int problem_found = 0;
632 /* verify all edge kinds */
633 for (i = 0; i < EDGE_KIND_LAST; ++i) {
634 int res = edges_verify_kind(irg, i);
635 problem_found = problem_found ? 1 : res;
637 problem_found = edges_verify_kind(irg, EDGE_KIND_NORMAL);
641 w.kind = EDGE_KIND_NORMAL;
645 inc_irg_visited(irg);
646 irg_walk_anchors(irg, clear_links, count_user, NULL);
647 irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
649 return problem_found ? 1 : w.problem_found;
652 void init_edges(void)
654 FIRM_DBG_REGISTER(dbg, DBG_EDGES);
655 /* firm_dbg_set_mask(dbg, -1); */
658 void edges_init_dbg(int do_dbg) {
662 void edges_activate(ir_graph *irg)
664 edges_activate_kind(irg, EDGE_KIND_NORMAL);
665 edges_activate_kind(irg, EDGE_KIND_BLOCK);
668 void edges_deactivate(ir_graph *irg)
670 edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
671 edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
674 int edges_assure(ir_graph *irg)
676 int activated = edges_activated(irg);
684 void edges_node_deleted(ir_node *irn, ir_graph *irg)
686 edges_node_deleted_kind(irn, EDGE_KIND_NORMAL, irg);
687 edges_node_deleted_kind(irn, EDGE_KIND_BLOCK, irg);
691 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
693 return _get_irn_out_edge_first_kind(irn, kind);
696 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
698 return _get_irn_out_edge_next(irn, last);
701 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
703 return _get_edge_src_irn(edge);
706 int (get_edge_src_pos)(const ir_edge_t *edge)
708 return _get_edge_src_pos(edge);
711 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
713 return _get_irn_n_edges_kind(irn, kind);
716 void dump_all_out_edges(ir_node *irn)
719 for(i = 0; i < EDGE_KIND_LAST; ++i) {
720 const ir_edge_t *edge;
722 printf("kind \"%s\"\n", get_kind_str(i));
723 foreach_out_edge_kind(irn, edge, i) {
724 ir_printf("\t%+F(%d)\n", edge->src, edge->pos);