2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Always available outs.
23 * @author Sebastian Hack, Michael Beck, Andreas Schoesser
27 * This are out-edges (also called def-use edges) that are dynamically
28 * updated as the graph changes.
34 #include "iredgekinds.h"
35 #include "iredges_t.h"
45 #include "iredgeset.h"
50 #define HashSet ir_edgeset_t
51 #define HashSetIterator ir_edgeset_iterator_t
52 #define ValueType ir_edge_t*
53 #define NullValue NULL
54 #define DeletedValue ((ir_edge_t*)-1)
55 #define Hash(this,key) (HASH_PTR(key->src) ^ (key->pos * 40013))
56 #define KeysEqual(this,key1,key2) ((key1->src) == (key2->src) && (key1->pos == key2->pos))
57 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof((ptr)[0]))
59 #define hashset_init ir_edgeset_init
60 void ir_edgeset_init_size(ir_edgeset_t *self, size_t size);
61 #define hashset_init_size ir_edgeset_init_size
62 #define hashset_destroy ir_edgeset_destroy
63 #define hashset_insert ir_edgeset_insert
64 #define hashset_remove ir_edgeset_remove
65 ir_edge_t *ir_edgeset_find(const ir_edgeset_t *self, const ir_edge_t*);
66 #define hashset_find ir_edgeset_find
67 size_t ir_edgeset_size(const ir_edgeset_t *self);
68 #define hashset_size ir_edgeset_size
69 #define hashset_iterator_init ir_edgeset_iterator_init
70 #define hashset_iterator_next ir_edgeset_iterator_next
71 #define hashset_remove_iterator ir_edgeset_remove_iterator
76 * A function that allows for setting an edge.
77 * This abstraction is necessary since different edge kind have
78 * different methods of setting edges.
80 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
83 * A function that returns the "arity" of a given edge kind
86 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
89 * A function that returns the pos'th edge of a given edge kind for a node.
91 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
94 * Additional data for an edge kind.
97 const char *name; /**< name of this edge kind */
98 set_edge_func_t *set_edge; /**< the set_edge function */
99 int first_idx; /**< index of the first possible edge */
100 get_edge_src_arity_func_t *get_arity; /**< the get_arity function */
101 get_edge_src_n_func_t *get_n; /**< the get_n function */
102 } ir_edge_kind_info_t;
105 * Get the predecessor block.
107 static ir_node *get_block_n(const ir_node *block, int pos)
110 return get_Block_cfgpred_block(block, pos);
115 static const ir_edge_kind_info_t edge_kind_info[EDGE_KIND_LAST] = {
116 { "normal" , set_irn_n, -1, get_irn_arity, get_irn_n },
117 { "block succs", NULL, 0, get_irn_arity, get_block_n },
118 { "dependency", set_irn_dep, 0, get_irn_deps, get_irn_dep }
121 #define foreach_tgt(irn, i, n, kind) for (i = edge_kind_info[kind].first_idx, n = edge_kind_info[kind].get_arity(irn); i < n; ++i)
122 #define get_n(irn, pos, kind) (edge_kind_info[kind].get_n(irn, pos))
123 #define get_kind_str(kind) (edge_kind_info[kind].name)
125 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
128 * This flag is set to 1, if the edges get initialized for an irg.
129 * Then register additional data is forbidden.
131 static int edges_used = 0;
134 * Summed size of all users private data
137 static int edges_private_size = 0;
138 #define EDGE_SIZE (sizeof(ir_edge_t) + edges_private_size)
141 * If set to 1, the list heads are checked every time an edge is changed.
143 static int edges_dbg = 0;
146 /* a static variable holding the last number assigned to a new edge */
147 static long last_edge_num = -1;
151 * Returns an ID for the given edge.
153 static inline long edge_get_id(const ir_edge_t *e)
157 #else /* DEBUG_libfirm */
159 #endif /* DEBUG_libfirm */
163 * Announce to reserve extra space for each edge to be allocated.
165 * @param n: Size of the space to reserve
167 * @return Offset at which the private data will begin
169 * Several users can reserve extra space for private usage.
170 * Each user has to remember his given offset and the size of his private data.
171 * To be called before FIRM is initialized.
173 int edges_register_private_data(size_t n)
175 int res = edges_private_size;
177 assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
179 edges_private_size += n;
184 * Reset the user's private data at offset 'offset'
185 * The user has to remember his offset and the size of his data!
186 * Caution: Using wrong values here can destroy other users private data!
188 void edges_reset_private_data(ir_graph *irg, int offset, unsigned size)
190 irg_edge_info_t *info = _get_irg_edge_info(irg, EDGE_KIND_NORMAL);
192 ir_edgeset_iterator_t iter;
194 foreach_ir_edgeset(&info->edges, edge, iter) {
195 memset(edge + sizeof(*edge) + offset, 0, size);
199 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
201 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
203 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
206 * Initialize the out information for a graph.
207 * @note Dead node elimination can call this on an already initialized graph.
209 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
211 if (edges_activated_kind(irg, kind)) {
212 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
213 size_t amount = irg->estimated_node_count * 2;
216 if (info->allocated) {
217 amount = ir_edgeset_size(&info->edges);
218 ir_edgeset_destroy(&info->edges);
219 obstack_free(&info->edges_obst, NULL);
221 obstack_init(&info->edges_obst);
222 INIT_LIST_HEAD(&info->free_edges);
223 ir_edgeset_init_size(&info->edges, amount);
229 * Get the edge object of an outgoing edge at a node.
230 * @param irg The graph, the node is in.
231 * @param src The node at which the edge originates.
232 * @param pos The position of the edge.
233 * @param kind The kind of the edge.
234 * @return The corresponding edge object or NULL,
235 * if no such edge exists.
237 const ir_edge_t *get_irn_edge_kind(ir_graph *irg, const ir_node *src, int pos, ir_edge_kind_t kind)
239 if (edges_activated_kind(irg, kind)) {
240 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
243 key.src = (ir_node *)src;
246 return ir_edgeset_find(&info->edges, &key);
253 * Get the edge object of an outgoing edge at a node.
254 * Looks for an edge for all kinds.
256 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
258 const ir_edge_t *edge;
259 if ((edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_NORMAL)) == NULL)
260 edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_BLOCK);
265 * Change the out count
267 * @param tgt the edge target
268 * @param kind the kind of the edge
270 static inline void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs)
272 irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
273 info->out_count += ofs;
276 ir_graph *irg = get_irn_irg(tgt);
277 assert(info->out_count >= 0);
278 if (info->out_count == 0 && kind == EDGE_KIND_NORMAL) {
279 /* tgt lost it's last user */
282 for (i = get_irn_arity(tgt) - 1; i >= -1; --i) {
283 ir_node *prev = get_irn_n(tgt, i);
285 edges_notify_edge(tgt, i, NULL, prev, irg);
287 for (i = get_irn_deps(tgt) - 1; i >= 0; --i) {
288 ir_node *prev = get_irn_dep(tgt, i);
290 edges_notify_edge_kind(tgt, i, NULL, prev, EDGE_KIND_DEP, irg);
298 * Verify the edge list of a node, ie. ensure it's a loop:
299 * head -> e_1 -> ... -> e_n -> head
301 static inline void verify_list_head(ir_node *irn, ir_edge_kind_t kind)
305 pset *lh_set = pset_new_ptr(16);
306 const struct list_head *head = _get_irn_outs_head(irn, kind);
307 const struct list_head *pos;
309 list_for_each(pos, head) {
310 if (pset_find_ptr(lh_set, pos)) {
311 const ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
313 ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
314 fprintf(stderr, "- at list entry %d\n", num);
316 fprintf(stderr, "- edge(%ld) is invalid\n", edge_get_id(edge));
318 ir_fprintf(stderr, "- edge(%ld) %+F(%d)\n", edge_get_id(edge), edge->src, edge->pos);
323 pset_insert_ptr(lh_set, pos);
331 void edges_dump_kind(ir_graph *irg, ir_edge_kind_t kind)
333 irg_edge_info_t *info;
335 ir_edgeset_iterator_t iter;
338 if (!edges_activated_kind(irg, kind))
341 info = _get_irg_edge_info(irg, kind);
342 edges = &info->edges;
343 foreach_ir_edgeset(edges, e, iter) {
344 ir_printf("%+F %d %d\n", e->src, e->pos, e->invalid);
348 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
349 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
350 ir_node *old_tgt, ir_edge_kind_t kind,
353 const char *msg = "";
354 irg_edge_info_t *info;
359 assert(edges_activated_kind(irg, kind));
362 * Only do something, if the old and new target differ.
367 info = _get_irg_edge_info(irg, kind);
368 edges = &info->edges;
370 /* Initialize the edge template to search in the set. */
375 * If the target is NULL, the edge shall be deleted.
378 /* search the edge in the set. */
379 edge = ir_edgeset_find(edges, &templ);
381 /* mark the edge invalid if it was found */
384 list_del(&edge->list);
385 ir_edgeset_remove(edges, edge);
386 list_add(&edge->list, &info->free_edges);
392 #endif /* DEBUG_libfirm */
393 edge_change_cnt(old_tgt, kind, -1);
395 /* If the edge was not found issue a warning on the debug stream */
396 msg = "edge to delete not found!\n";
400 * The target is not NULL and the old target differs
401 * from the new target, the edge shall be moved (if the
402 * old target was != NULL) or added (if the old target was
405 struct list_head *head = _get_irn_outs_head(tgt, kind);
407 assert(head->next && head->prev &&
408 "target list head must have been initialized");
410 /* If the old target is not null, the edge is moved. */
412 edge = ir_edgeset_find(edges, &templ);
413 assert(edge && "edge to redirect not found!");
414 assert(! edge->invalid && "Invalid edge encountered");
418 list_move(&edge->list, head);
419 edge_change_cnt(old_tgt, kind, -1);
421 /* The old target was NULL, thus, the edge is newly created. */
425 if (list_empty(&info->free_edges)) {
426 edge = obstack_alloc(&info->edges_obst, EDGE_SIZE);
428 edge = list_entry(info->free_edges.next, ir_edge_t, list);
429 list_del(&edge->list);
437 edge->list.next = NULL;
438 edge->list.prev = NULL;
439 memset(edge + 1, 0, edges_private_size);
440 DEBUG_ONLY(edge->src_nr = get_irn_node_nr(src));
442 new_edge = ir_edgeset_insert(edges, edge);
443 if (new_edge != edge) {
444 panic("new edge exists already");
448 list_add(&edge->list, head);
449 DEBUG_ONLY(edge->edge_nr = ++last_edge_num);
452 edge_change_cnt(tgt, kind, +1);
455 #ifndef DEBUG_libfirm
456 /* verify list heads */
459 verify_list_head(tgt, kind);
461 verify_list_head(old_tgt, kind);
465 DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
468 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
470 if (edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
471 edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
474 if (edges_activated_kind(irg, EDGE_KIND_BLOCK) && is_Block(src)) {
476 /* a MacroBlock edge: ignore it here */
478 ir_node *bl_old = old_tgt ? get_nodes_block(skip_Proj(old_tgt)) : NULL;
479 ir_node *bl_tgt = NULL;
482 bl_tgt = is_Bad(tgt) ? tgt : get_nodes_block(skip_Proj(tgt));
484 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
490 * Delete all in edges of a given kind from the node old.
492 * @param old the node
493 * @param kind the kind of edges to remove
494 * @param irg the irg of the old node
496 static void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
500 if (!edges_activated_kind(irg, kind))
503 DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
505 foreach_tgt(old, i, n, kind) {
506 ir_node *old_tgt = get_n(old, i, kind);
507 edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
512 * A node might be revivaled by CSE. Assure its edges.
514 * @param irn the node
515 * @param kind the kind of edges to remove
516 * @param irg the irg of the old node
518 static void edges_node_revival_kind(ir_node *irn, ir_edge_kind_t kind, ir_graph *irg)
520 irn_edge_info_t *info;
523 if (!edges_activated_kind(irg, kind))
526 info = _get_irn_edge_info(irn, kind);
527 if (info->edges_built)
530 DBG((dbg, LEVEL_5, "node revivaled (kind: %s): %+F\n", get_kind_str(kind), irn));
532 foreach_tgt(irn, i, n, kind) {
533 ir_node *tgt = get_n(irn, i, kind);
534 edges_notify_edge_kind(irn, i, tgt, NULL, kind, irg);
536 info->edges_built = 1;
539 struct build_walker {
543 unsigned problem_found;
547 * Post-Walker: notify all edges
549 static void build_edges_walker(ir_node *irn, void *data)
551 struct build_walker *w = data;
553 ir_edge_kind_t kind = w->kind;
554 ir_graph *irg = w->irg;
555 get_edge_src_n_func_t *get_n;
557 get_n = edge_kind_info[kind].get_n;
558 foreach_tgt(irn, i, n, kind) {
559 ir_node *pred = get_n(irn, i, kind);
560 edges_notify_edge_kind(irn, i, pred, NULL, kind, irg);
562 _get_irn_edge_info(irn, kind)->edges_built = 1;
566 * Pre-Walker: initializes the list-heads and set the out-count
569 static void init_lh_walker(ir_node *irn, void *data)
571 struct build_walker *w = data;
572 ir_edge_kind_t kind = w->kind;
573 list_head *head = _get_irn_outs_head(irn, kind);
574 INIT_LIST_HEAD(head);
575 _get_irn_edge_info(irn, kind)->edges_built = 0;
576 _get_irn_edge_info(irn, kind)->out_count = 0;
580 * Pre-Walker: initializes the list-heads and set the out-count
583 * Additionally touches DEP nodes, as they might be DEAD.
584 * THIS IS UGLY, but I don't find a better way until we
586 * a) ensure that dead nodes are not used as input
587 * b) it might be sufficient to add those stupid NO_REG nodes
590 static void init_lh_walker_dep(ir_node *irn, void *data)
592 struct build_walker *w = data;
593 ir_edge_kind_t kind = w->kind;
594 list_head *head = _get_irn_outs_head(irn, kind);
597 INIT_LIST_HEAD(head);
598 _get_irn_edge_info(irn, kind)->edges_built = 0;
599 _get_irn_edge_info(irn, kind)->out_count = 0;
601 for (i = get_irn_deps(irn) - 1; i >= 0; --i) {
602 ir_node *dep = get_irn_dep(irn, i);
604 head = _get_irn_outs_head(dep, kind);
606 INIT_LIST_HEAD(head);
607 _get_irn_edge_info(dep, kind)->edges_built = 0;
608 _get_irn_edge_info(dep, kind)->out_count = 0;
612 typedef struct visitor_info_t {
613 irg_walk_func *visit;
618 * Visitor: initializes the list-heads and set the out-count
619 * of all nodes to 0 of nodes that are not seen so far.
621 static void visitor(ir_node *irn, void *data)
623 visitor_info_t *info = data;
628 if (!irn_visited_else_mark(irn)) {
629 info->visit(irn, info->data);
634 * Build the initial edge set.
635 * Beware, this is not a simple task because it suffers from two
637 * - the anchor set allows access to Nodes that may not be reachable from
639 * - the identities add nodes to the "root set" that are not yet reachable
640 * from End. However, after some transformations, the CSE may revival these
643 * These problems can be fixed using different strategies:
644 * - Add an age flag to every node. Whenever the edge of a node is older
645 * then the current edge, invalidate the edges of this node.
646 * While this would help for revivaled nodes, it increases memory and runtime.
647 * - Delete the identities set.
648 * Solves the revival problem, but may increase the memory consumption, as
649 * nodes cannot be revivaled at all.
650 * - Manually iterate over the identities root set. This did not consume more memory
651 * but increase the computation time because the |identities| >= |V|
653 * Currently, we use the last option.
655 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
657 struct build_walker w;
658 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
659 visitor_info_t visit;
666 assert(!info->activated);
669 edges_init_graph_kind(irg, kind);
670 if (kind == EDGE_KIND_DEP) {
671 irg_walk_anchors(irg, init_lh_walker_dep, NULL, &w);
672 /* Argh: Dep nodes might be dead, so we MUST visit identities first */
673 visit.visit = init_lh_walker_dep;
674 visit_all_identities(irg, visitor, &visit);
675 irg_walk_anchors(irg, NULL, build_edges_walker, &w);
677 irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
678 visit.visit = init_lh_walker;
679 visit_all_identities(irg, visitor, &visit);
683 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
685 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
688 if (info->allocated) {
689 obstack_free(&info->edges_obst, NULL);
690 ir_edgeset_destroy(&info->edges);
695 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
697 return _edges_activated_kind(irg, kind);
702 * Reroute all use-edges from a node to another.
703 * @param from The node whose use-edges shall be withdrawn.
704 * @param to The node to which all the use-edges of @p from shall be
706 * @param irg The graph.
708 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind, ir_graph *irg)
710 set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
712 if (set_edge && edges_activated_kind(irg, kind)) {
713 struct list_head *head = _get_irn_outs_head(from, kind);
715 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
717 while (head != head->next) {
718 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
719 assert(edge->pos >= -1);
720 set_edge(edge->src, edge->pos, to);
725 static void verify_set_presence(ir_node *irn, void *data)
727 struct build_walker *w = data;
728 ir_edgeset_t *edges = &_get_irg_edge_info(w->irg, w->kind)->edges;
731 foreach_tgt(irn, i, n, w->kind) {
737 e = ir_edgeset_find(edges, &templ);
741 w->problem_found = 1;
743 ir_fprintf(stderr, "Edge Verifier: edge %+F,%d -> %+F (kind: \"%s\") is missing\n",
744 irn, i, get_n(irn, i, w->kind), get_kind_str(w->kind));
750 static void verify_list_presence(ir_node *irn, void *data)
752 struct build_walker *w = data;
755 bitset_set(w->reachable, get_irn_idx(irn));
757 /* check list heads */
758 verify_list_head(irn, w->kind);
760 foreach_out_edge_kind(irn, e, w->kind) {
763 if (w->kind == EDGE_KIND_NORMAL && get_irn_arity(e->src) <= e->pos) {
764 w->problem_found = 1;
766 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F -> %+F recorded at src position %d, but src has arity %d\n",
767 edge_get_id(e), e->src, irn, e->pos, get_irn_arity(e->src));
772 tgt = get_n(e->src, e->pos, w->kind);
775 w->problem_found = 1;
777 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d (kind \"%s\") is no out edge of %+F but of %+F\n",
778 edge_get_id(e), e->src, e->pos, get_kind_str(w->kind), irn, tgt);
784 int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
786 struct build_walker w;
787 ir_edgeset_t *edges = &_get_irg_edge_info(irg, kind)->edges;
789 ir_edgeset_iterator_t iter;
793 w.reachable = bitset_alloca(get_irg_last_idx(irg));
796 /* Clear the present bit in all edges available. */
797 foreach_ir_edgeset(edges, e, iter) {
801 irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
804 * Dump all edges which are not invalid and not present.
805 * These edges are superfluous and their presence in the
808 foreach_ir_edgeset(edges, e, iter) {
809 if (! e->invalid && ! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
811 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d is superfluous\n", edge_get_id(e), e->src, e->pos);
815 return w.problem_found;
818 #define IGNORE_NODE(irn) (is_Bad((irn)) || is_Block((irn)))
821 * Clear link field of all nodes.
823 static void clear_links(ir_node *irn, void *env)
825 struct build_walker *w = env;
828 if (IGNORE_NODE(irn)) {
829 set_irn_link(irn, NULL);
833 bs = bitset_malloc(get_irg_last_idx(w->irg));
834 set_irn_link(irn, bs);
838 * Increases count (stored in link field) for all operands of a node.
840 static void count_user(ir_node *irn, void *env)
847 for (i = get_irn_arity(irn) - 1; i >= first; --i) {
848 ir_node *op = get_irn_n(irn, i);
849 bitset_t *bs = get_irn_link(op);
852 bitset_set(bs, get_irn_idx(irn));
857 * Verifies if collected count, number of edges in list and stored edge count are in sync.
859 static void verify_edge_counter(ir_node *irn, void *env)
861 struct build_walker *w = env;
867 const struct list_head *head;
868 const struct list_head *pos;
870 if (IGNORE_NODE(irn))
873 bs = get_irn_link(irn);
876 edge_cnt = _get_irn_edge_info(irn, EDGE_KIND_NORMAL)->out_count;
877 head = _get_irn_outs_head(irn, EDGE_KIND_NORMAL);
879 /* We can iterate safely here, list heads have already been verified. */
880 list_for_each(pos, head) {
884 /* check all nodes that reference us and count edges that point number
885 * of ins that actually point to us */
887 bitset_foreach(bs, idx) {
889 ir_node *src = get_idx_irn(w->irg, idx);
891 arity = get_irn_arity(src);
892 for (i = 0; i < arity; ++i) {
893 ir_node *in = get_irn_n(src, i);
899 if (edge_cnt != list_cnt) {
900 w->problem_found = 1;
901 ir_fprintf(stderr, "Edge Verifier: edge count is %d, but %d edge(s) are recorded in list at %+F\n",
902 edge_cnt, list_cnt, irn);
905 if (ref_cnt != list_cnt) {
906 w->problem_found = 1;
907 ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d node(s), but the list contains %d edge(s)\n",
908 irn, ref_cnt, list_cnt);
910 /* Matze: buggy if a node has multiple ins pointing at irn */
912 list_for_each(pos, head) {
913 ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
914 bitset_flip(bs, get_irn_idx(edge->src));
917 if (ref_cnt < list_cnt)
918 fprintf(stderr," following nodes are recorded in list, but not as user:\n");
920 fprintf(stderr," following nodes are user, but not recorded in list:\n");
923 bitset_foreach(bs, idx) {
924 ir_node *src = get_idx_irn(w->irg, idx);
925 ir_fprintf(stderr, " %+F", src);
927 fprintf(stderr, "\n");
935 * Verifies the out edges of an irg.
937 int edges_verify(ir_graph *irg)
939 struct build_walker w;
940 int problem_found = 0;
942 /* verify normal edges only */
943 problem_found = edges_verify_kind(irg, EDGE_KIND_NORMAL);
946 w.kind = EDGE_KIND_NORMAL;
950 irg_walk_anchors(irg, clear_links, count_user, &w);
951 irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
953 return problem_found ? 1 : w.problem_found;
957 ir_graph_pass_t pass;
958 unsigned assert_on_problem;
962 * Wrapper to edges_verify to be run as an ir_graph pass.
964 static int edges_verify_wrapper(ir_graph *irg, void *context)
966 struct pass_t *pass = context;
967 int problems_found = edges_verify(irg);
968 /* do NOT rerun the pass if verify is ok :-) */
969 assert(problems_found && pass->assert_on_problem);
973 /* Creates an ir_graph pass for edges_verify(). */
974 ir_graph_pass_t *irg_verify_edges_pass(const char *name, unsigned assert_on_problem)
976 struct pass_t *pass = XMALLOCZ(struct pass_t);
978 def_graph_pass_constructor(
979 &pass->pass, name ? name : "edges_verify", edges_verify_wrapper);
981 /* neither dump nor verify */
982 pass->pass.dump_irg = (DUMP_ON_IRG_FUNC)ir_prog_no_dump;
983 pass->pass.verify_irg = (RUN_ON_IRG_FUNC)ir_prog_no_verify;
985 pass->assert_on_problem = assert_on_problem;
989 void init_edges(void)
991 FIRM_DBG_REGISTER(dbg, DBG_EDGES);
992 /* firm_dbg_set_mask(dbg, -1); */
995 void edges_init_dbg(int do_dbg)
1000 void edges_activate(ir_graph *irg)
1002 edges_activate_kind(irg, EDGE_KIND_NORMAL);
1003 edges_activate_kind(irg, EDGE_KIND_BLOCK);
1004 if (get_irg_phase_state(irg) == phase_backend)
1005 edges_activate_kind(irg, EDGE_KIND_DEP);
1008 void edges_deactivate(ir_graph *irg)
1010 if (get_irg_phase_state(irg) == phase_backend)
1011 edges_deactivate_kind(irg, EDGE_KIND_DEP);
1012 edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
1013 edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
1016 int edges_assure(ir_graph *irg)
1020 if (edges_activated_kind(irg, EDGE_KIND_BLOCK)) {
1023 edges_activate_kind(irg, EDGE_KIND_BLOCK);
1025 if (edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
1028 edges_activate_kind(irg, EDGE_KIND_NORMAL);
1034 int edges_assure_kind(ir_graph *irg, ir_edge_kind_t kind)
1036 int activated = edges_activated_kind(irg, kind);
1039 edges_activate_kind(irg, kind);
1044 void edges_node_deleted(ir_node *irn, ir_graph *irg)
1046 edges_node_deleted_kind(irn, EDGE_KIND_NORMAL, irg);
1047 edges_node_deleted_kind(irn, EDGE_KIND_BLOCK, irg);
1050 void edges_node_revival(ir_node *irn, ir_graph *irg)
1052 edges_node_revival_kind(irn, EDGE_KIND_NORMAL, irg);
1053 edges_node_revival_kind(irn, EDGE_KIND_BLOCK, irg);
1056 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
1058 return _get_irn_out_edge_first_kind(irn, kind);
1061 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
1063 return _get_irn_out_edge_next(irn, last);
1066 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
1068 return _get_edge_src_irn(edge);
1071 int (get_edge_src_pos)(const ir_edge_t *edge)
1073 return _get_edge_src_pos(edge);
1076 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
1078 return _get_irn_n_edges_kind(irn, kind);
1081 static void irg_walk_edges2(ir_node *node, irg_walk_func *pre,
1082 irg_walk_func *post, void *env)
1084 const ir_edge_t *edge, *next;
1086 if (irn_visited(node))
1088 mark_irn_visited(node);
1093 foreach_out_edge_kind_safe(node, edge, next, EDGE_KIND_NORMAL) {
1094 /* find the corresponding successor block. */
1095 ir_node *pred = get_edge_src_irn(edge);
1096 irg_walk_edges2(pred, pre, post, env);
1103 void irg_walk_edges(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
1106 ir_graph *irg = get_irn_irg(node);
1108 assert(edges_activated(irg));
1109 assert(is_Block(node));
1111 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1113 inc_irg_visited(irg);
1114 irg_walk_edges2(node, pre, post, env);
1116 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
1119 static void irg_block_edges_walk2(ir_node *bl, irg_walk_func *pre,
1120 irg_walk_func *post, void *env)
1122 const ir_edge_t *edge, *next;
1124 if (!Block_block_visited(bl)) {
1125 mark_Block_block_visited(bl);
1130 foreach_out_edge_kind_safe(bl, edge, next, EDGE_KIND_BLOCK) {
1131 /* find the corresponding successor block. */
1132 ir_node *pred = get_edge_src_irn(edge);
1133 irg_block_edges_walk2(pred, pre, post, env);
1141 void irg_block_edges_walk(ir_node *node, irg_walk_func *pre,
1142 irg_walk_func *post, void *env)
1144 ir_graph *irg = get_irn_irg(node);
1146 assert(edges_activated(irg));
1147 assert(is_Block(node));
1149 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
1151 inc_irg_block_visited(irg);
1152 irg_block_edges_walk2(node, pre, post, env);
1154 ir_free_resources(irg, IR_RESOURCE_BLOCK_VISITED);