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"
43 * A function that allows for setting an edge.
44 * This abstraction is necessary since different edge kind have
45 * different methods of setting edges.
47 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
49 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
51 typedef int (get_edge_src_first_func_t)(const ir_node *src);
53 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
56 * Additional data for an edge kind.
60 set_edge_func_t *set_edge;
61 get_edge_src_first_func_t *get_first;
62 get_edge_src_arity_func_t *get_arity;
63 get_edge_src_n_func_t *get_n;
64 } ir_edge_kind_info_t;
66 static int get_zero(const ir_node *irn)
71 static int get_irn_first(const ir_node *irn)
73 return 0 - !is_Block(irn);
76 static ir_node *get_block_n(const ir_node *irn, int pos)
78 return is_Block(irn) ? get_Block_cfgpred_block((ir_node *) irn, pos) : 0;
81 static const ir_edge_kind_info_t edge_kind_info[EDGE_KIND_LAST] = {
82 { "normal" , set_irn_n, get_irn_first, get_irn_arity, get_irn_n },
83 { "block succs", NULL, get_zero, get_irn_arity, get_block_n },
84 { "dependency", set_irn_dep, get_zero, get_irn_deps, get_irn_dep }
87 #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)
88 #define get_n(irn, pos, kind) (edge_kind_info[kind].get_n(irn, pos))
89 #define get_kind_str(kind) (edge_kind_info[kind].name)
91 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
94 * This flag is set to 1, if the edges get initialized for an irg.
95 * Then register additional data is forbidden.
97 static int edges_used = 0;
100 * Summed size of all users private data
103 static int edges_private_size = 0;
104 #define EDGE_SIZE (sizeof(ir_edge_t) + edges_private_size)
107 * If set to 1, the list heads are checked every time an edge is changed.
109 static int edges_dbg = 0;
111 /* a static variable holding the last number assigned to a new edge */
112 static long last_edge_num = -1;
114 static INLINE long edge_get_id(const ir_edge_t *e) {
117 #else /* DEBUG_libfirm */
119 #endif /* DEBUG_libfirm */
123 * Announce to reserve extra space for each edge to be allocated.
124 * @Param n: Size of the space to reserve
125 * @ Returns: Offset at which the private data will begin
126 * Several users can reserve extra space for private usage.
127 * Each user has to remember his given offset and the size of his private data.
128 * To be called before FIRM is initialized.
130 int edges_register_private_data(size_t n)
132 int res = edges_private_size;
134 assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
136 edges_private_size += n;
141 * Reset the user's private data at offset 'offset'
142 * The user has to remember his offset and the size of his data!
143 * Caution: Using wrong values here can destroy other users private data!
146 void edges_reset_private_data(ir_graph *irg, int offset, size_t size)
148 irg_edge_info_t *info = _get_irg_edge_info(irg, EDGE_KIND_NORMAL);
151 foreach_set(info->edges, edge)
153 memset(edge + sizeof(*edge) + offset, 0, size);
157 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
159 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
161 static int edge_cmp(const void *p1, const void *p2, size_t len)
163 const ir_edge_t *e1 = p1;
164 const ir_edge_t *e2 = p2;
166 if(e1->src != e2->src)
168 if(e1->pos != e1->pos)
174 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
177 * Initialize the out information for a graph.
178 * @note Dead node elimination can call this on an already initialized graph.
180 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
182 if(edges_activated_kind(irg, kind)) {
183 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
188 amount = set_count(info->edges);
189 del_set(info->edges);
191 info->edges = new_set(edge_cmp, amount);
196 * Get the edge object of an outgoing edge at a node.
197 * @param irg The graph, the node is in.
198 * @param src The node at which the edge originates.
199 * @param pos The position of the edge.
200 * @return The corresponding edge object or NULL,
201 * if no such edge exists.
203 const ir_edge_t *get_irn_edge_kind(ir_graph *irg, const ir_node *src, int pos, ir_edge_kind_t kind)
205 if (edges_activated_kind(irg, kind)) {
206 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
209 key.src = (ir_node *)src;
211 return set_find(info->edges, &key, EDGE_SIZE, edge_hash(&key));
218 * Get the edge object of an outgoing edge at a node.
219 * Looks for an edge for all kinds.
222 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
224 const ir_edge_t *edge;
225 if((edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_NORMAL)) == NULL)
226 edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_BLOCK);
231 * Change the out count
233 static INLINE void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs) {
234 irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
235 info->out_count += ofs;
239 * Verify the edge list of a node, ie. ensure it's a loop:
240 * head -> e_1 -> ... -> e_n -> head
242 static INLINE void vrfy_list_head(ir_node *irn, ir_edge_kind_t kind) {
245 pset *lh_set = pset_new_ptr(16);
246 const struct list_head *head = _get_irn_outs_head(irn, kind);
247 const struct list_head *pos;
249 list_for_each(pos, head) {
250 if (pset_find_ptr(lh_set, pos)) {
251 const ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
253 ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
254 fprintf(stderr, "- at list entry %d\n", num);
256 fprintf(stderr, "- edge(%ld) is invalid\n", edge_get_id(edge));
258 ir_fprintf(stderr, "- edge(%ld) %+F(%d)\n", edge_get_id(edge), edge->src, edge->pos);
263 pset_insert_ptr(lh_set, pos);
271 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
272 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
273 ir_node *old_tgt, ir_edge_kind_t kind,
276 const char *msg = "";
277 irg_edge_info_t *info;
282 assert(edges_activated_kind(irg, kind));
285 * Only do something, if the old and new target differ.
290 info = _get_irg_edge_info(irg, kind);
292 templ = alloca(EDGE_SIZE);
294 /* Initialize the edge template to search in the set. */
295 memset(templ, 0, EDGE_SIZE);
301 DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
304 * If the target is NULL, the edge shall be deleted.
307 /* search the edge in the set. */
308 edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
310 /* mark the edge invalid if it was found */
313 list_del(&edge->list);
319 #endif /* DEBUG_libfirm */
320 edge_change_cnt(old_tgt, kind, -1);
323 /* If the edge was not found issue a warning on the debug stream */
325 msg = "edge to delete not found!\n";
330 * The target is not NULL and the old target differs
331 * from the new target, the edge shall be moved (if the
332 * old target was != NULL) or added (if the old target was
336 struct list_head *head = _get_irn_outs_head(tgt, kind);
338 assert(head->next && head->prev &&
339 "target list head must have been initialized");
341 /* If the old target is not null, the edge is moved. */
343 edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
344 assert(edge && "edge to redirect not found!");
345 assert(! edge->invalid && "Invalid edge encountered");
349 list_move(&edge->list, head);
350 edge_change_cnt(old_tgt, kind, -1);
353 /* The old target was null, thus, the edge is newly created. */
355 edge = set_insert(edges, templ, EDGE_SIZE, edge_hash(templ));
357 assert(! edge->invalid && "Freshly inserted edge is invalid?!?");
358 assert(edge->list.next == NULL && edge->list.prev == NULL &&
359 "New edge must not have list head initialized");
362 list_add(&edge->list, head);
364 edge->edge_nr = ++last_edge_num;
365 #endif /* DEBUG_libfirm */
368 edge_change_cnt(tgt, kind, +1);
371 /* verify list heads */
374 vrfy_list_head(tgt, kind);
376 vrfy_list_head(old_tgt, kind);
379 DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
382 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
384 if(edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
385 edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
388 if (edges_activated_kind(irg, EDGE_KIND_BLOCK) && is_Block(src)) {
389 /* do not use get_nodes_block() here, it fails when running unpinned */
390 ir_node *bl_old = old_tgt ? get_irn_n(skip_Proj(old_tgt), -1) : NULL;
391 ir_node *bl_tgt = NULL;
394 bl_tgt = is_Bad(tgt) ? tgt : get_irn_n(skip_Proj(tgt), -1);
396 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
401 void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
405 if(!edges_activated_kind(irg, kind))
408 DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
410 foreach_tgt(old, i, n, kind) {
411 ir_node *old_tgt = get_n(old, i, kind);
412 edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
416 struct build_walker {
420 unsigned problem_found;
424 * Post-Walker: notify all edges
426 static void build_edges_walker(ir_node *irn, void *data) {
427 struct build_walker *w = data;
430 if (! edges_activated_kind(w->irg, w->kind))
433 foreach_tgt(irn, i, n, w->kind)
434 edges_notify_edge_kind(irn, i, get_n(irn, i, w->kind), NULL, w->kind, w->irg);
438 * Pre-Walker: initializes the list-heads and set the out-count
441 static void init_lh_walker(ir_node *irn, void *data) {
442 struct build_walker *w = data;
443 INIT_LIST_HEAD(_get_irn_outs_head(irn, w->kind));
444 _get_irn_edge_info(irn, w->kind)->out_count = 0;
448 * Visitor: initializes the list-heads and set the out-count
449 * of all nodes to 0 of nodes that are not seen so far.
451 static void visitor(ir_node *irn, void *data) {
452 if (irn_not_visited(irn)) {
453 mark_irn_visited(irn);
454 init_lh_walker(irn, data);
459 * Build the initial edge set.
460 * Beware, this is not a simple task because it suffers from two
462 * - the anchor set allows access to Nodes that may not be reachable from
464 * - the identities add nodes to the "root set" that are not yet reachable
465 * from End. However, after some transformations, the CSE may revival these
468 * These problems can be fixed using different strategies:
469 * - Add an age flag to every node. Whenever the edge of a node is older
470 * then the current edge, invalidate the edges of this node.
471 * While this would help for revivaled nodes, it increases memory and runtime.
472 * - Delete the identities set.
473 * Solves the revival problem, but may increase the memory consumption, as
474 * nodes cannot be revivaled at all.
475 * - Manually iterate over the identities root set. This did not consume more memory
476 * but increase the computation time because the |identities| >= |V|
478 * Currently, we use the last option.
480 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
482 struct build_walker w;
483 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
489 edges_init_graph_kind(irg, kind);
490 //irg_walk_graph(irg, init_lh_walker, build_edges_walker, &w);
491 inc_irg_visited(irg);
492 irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
493 visit_all_identities(irg, visitor, &w);
496 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
498 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
502 del_set(info->edges);
507 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
509 return _edges_activated_kind(irg, kind);
514 * Reroute all use-edges from a node to another.
515 * @param from The node whose use-edges shall be withdrawn.
516 * @param to The node to which all the use-edges of @p from shall be
518 * @param irg The graph.
520 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind, ir_graph *irg)
522 set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
524 if(set_edge && edges_activated_kind(irg, kind)) {
525 struct list_head *head = _get_irn_outs_head(from, kind);
527 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
529 while(head != head->next) {
530 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
531 assert(edge->pos >= -1);
532 set_edge(edge->src, edge->pos, to);
537 static void verify_set_presence(ir_node *irn, void *data)
539 struct build_walker *w = data;
540 set *edges = _get_irg_edge_info(w->irg, w->kind)->edges;
543 foreach_tgt(irn, i, n, w->kind) {
549 e = set_find(edges, &templ, EDGE_SIZE, edge_hash(&templ));
553 w->problem_found = 1;
554 ir_fprintf(stderr, "Edge Verifier: edge %+F,%d -> %+F (kind: \"%s\") is missing\n",
555 irn, i, get_n(irn, i, w->kind), get_kind_str(w->kind));
560 static void verify_list_presence(ir_node *irn, void *data)
562 struct build_walker *w = data;
565 bitset_set(w->reachable, get_irn_idx(irn));
567 /* check list heads */
568 vrfy_list_head(irn, w->kind);
570 foreach_out_edge_kind(irn, e, w->kind) {
573 if (w->kind == EDGE_KIND_NORMAL && get_irn_arity(e->src) <= e->pos) {
574 w->problem_found = 1;
575 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F -> %+F recorded at src position %d, but src has arity %d\n",
576 edge_get_id(e), e->src, irn, e->pos, get_irn_arity(e->src));
580 tgt = get_n(e->src, e->pos, w->kind);
583 w->problem_found = 1;
584 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d (kind \"%s\") is no out edge of %+F but of %+F\n",
585 edge_get_id(e), e->src, e->pos, get_kind_str(w->kind), irn, tgt);
590 int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
592 struct build_walker w;
593 set *edges = _get_irg_edge_info(irg, kind)->edges;
598 w.reachable = bitset_alloca(get_irg_last_idx(irg));
601 /* Clear the present bit in all edges available. */
602 for (e = set_first(edges); e; e = set_next(edges))
605 irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
608 * Dump all edges which are not invalid and not present.
609 * These edges are superfluous and their presence in the
612 for (e = set_first(edges); e; e = set_next(edges)) {
613 if (! e->invalid && ! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
615 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d is superfluous\n", edge_get_id(e), e->src, e->pos);
619 return w.problem_found;
622 #define IGNORE_NODE(irn) (is_Bad((irn)) || is_Block((irn)))
625 * Clear link field of all nodes.
627 static void clear_links(ir_node *irn, void *env) {
628 struct build_walker *w = env;
631 if (IGNORE_NODE(irn)) {
632 set_irn_link(irn, NULL);
636 bs = bitset_malloc(get_irg_last_idx(w->irg));
637 set_irn_link(irn, bs);
641 * Increases count (stored in link field) for all operands of a node.
643 static void count_user(ir_node *irn, void *env) {
647 first = get_irn_first(irn);
648 for (i = get_irn_arity(irn) - 1; i >= first; --i) {
649 ir_node *op = get_irn_n(irn, i);
650 bitset_t *bs = get_irn_link(op);
653 bitset_set(bs, get_irn_idx(irn));
658 * Verifies if collected count, number of edges in list and stored edge count are in sync.
660 static void verify_edge_counter(ir_node *irn, void *env) {
661 struct build_walker *w = env;
666 const struct list_head *head;
667 const struct list_head *pos;
669 if (IGNORE_NODE(irn))
672 bs = get_irn_link(irn);
674 ref_cnt = bitset_popcnt(bs);
675 edge_cnt = _get_irn_edge_info(irn, EDGE_KIND_NORMAL)->out_count;
676 head = _get_irn_outs_head(irn, EDGE_KIND_NORMAL);
678 /* We can iterate safely here, list heads have already been verified. */
679 list_for_each(pos, head) {
683 if (edge_cnt != list_cnt) {
684 w->problem_found = 1;
685 ir_fprintf(stderr, "Edge Verifier: edge count is %d, but %d edge(s) are recorded in list at %+F\n",
686 edge_cnt, list_cnt, irn);
689 if (ref_cnt != list_cnt) {
692 w->problem_found = 1;
693 ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d node(s), but %d edge(s) recorded in list\n",
694 irn, ref_cnt, list_cnt);
696 list_for_each(pos, head) {
697 ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
698 bitset_flip(bs, get_irn_idx(edge->src));
701 if (ref_cnt < list_cnt)
702 fprintf(stderr," following nodes are recorded in list, but not as user:\n");
704 fprintf(stderr," following nodes are user, but not recorded in list:\n");
707 bitset_foreach(bs, idx) {
708 ir_node *src = get_idx_irn(w->irg, idx);
709 ir_fprintf(stderr, " %+F", src);
711 fprintf(stderr, "\n");
718 * Verifies the out edges of an irg.
720 int edges_verify(ir_graph *irg) {
721 struct build_walker w;
722 int problem_found = 0;
727 /* verify normal edges only */
728 problem_found = edges_verify_kind(irg, EDGE_KIND_NORMAL);
731 w.kind = EDGE_KIND_NORMAL;
735 inc_irg_visited(irg);
736 irg_walk_anchors(irg, clear_links, count_user, &w);
737 inc_irg_visited(irg);
738 irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
740 return problem_found ? 1 : w.problem_found;
743 void init_edges(void)
745 FIRM_DBG_REGISTER(dbg, DBG_EDGES);
746 /* firm_dbg_set_mask(dbg, -1); */
749 void edges_init_dbg(int do_dbg) {
753 void edges_activate(ir_graph *irg)
755 edges_activate_kind(irg, EDGE_KIND_NORMAL);
756 edges_activate_kind(irg, EDGE_KIND_BLOCK);
759 void edges_deactivate(ir_graph *irg)
761 edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
762 edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
765 int edges_assure(ir_graph *irg)
767 int activated = edges_activated(irg);
775 void edges_node_deleted(ir_node *irn, ir_graph *irg)
777 edges_node_deleted_kind(irn, EDGE_KIND_NORMAL, irg);
778 edges_node_deleted_kind(irn, EDGE_KIND_BLOCK, irg);
782 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
784 return _get_irn_out_edge_first_kind(irn, kind);
787 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
789 return _get_irn_out_edge_next(irn, last);
792 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
794 return _get_edge_src_irn(edge);
797 int (get_edge_src_pos)(const ir_edge_t *edge)
799 return _get_edge_src_pos(edge);
802 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
804 return _get_irn_n_edges_kind(irn, kind);
807 void dump_all_out_edges(ir_node *irn)
810 for(i = 0; i < EDGE_KIND_LAST; ++i) {
811 const ir_edge_t *edge;
813 printf("kind \"%s\"\n", get_kind_str(i));
814 foreach_out_edge_kind(irn, edge, i) {
815 ir_printf("\t%+F(%d)\n", edge->src, edge->pos);