2 * Copyright (C) 1995-2011 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
26 * This are out-edges (also called def-use edges) that are dynamically
27 * updated as the graph changes.
33 #include "iredgekinds.h"
34 #include "iredges_t.h"
44 #include "iredgeset.h"
49 #define HashSet ir_edgeset_t
50 #define HashSetIterator ir_edgeset_iterator_t
51 #define ValueType ir_edge_t*
52 #define NullValue NULL
53 #define DeletedValue ((ir_edge_t*)-1)
54 #define Hash(this,key) (HASH_PTR(key->src) ^ (key->pos * 40013))
55 #define KeysEqual(this,key1,key2) ((key1->src) == (key2->src) && (key1->pos == key2->pos))
56 #define SetRangeEmpty(ptr,size) memset(ptr, 0, (size) * sizeof((ptr)[0]))
58 #define hashset_init ir_edgeset_init
59 void ir_edgeset_init_size(ir_edgeset_t *self, size_t size);
60 #define hashset_init_size ir_edgeset_init_size
61 #define hashset_destroy ir_edgeset_destroy
62 #define hashset_insert ir_edgeset_insert
63 #define hashset_remove ir_edgeset_remove
64 ir_edge_t *ir_edgeset_find(const ir_edgeset_t *self, const ir_edge_t*);
65 #define hashset_find ir_edgeset_find
66 size_t ir_edgeset_size(const ir_edgeset_t *self);
67 #define hashset_size ir_edgeset_size
68 #define hashset_iterator_init ir_edgeset_iterator_init
69 #define hashset_iterator_next ir_edgeset_iterator_next
70 #define hashset_remove_iterator ir_edgeset_remove_iterator
75 * A function that allows for setting an edge.
76 * This abstraction is necessary since different edge kind have
77 * different methods of setting edges.
79 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
82 * A function that returns the "arity" of a given edge kind
85 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
88 * A function that returns the pos'th edge of a given edge kind for a node.
90 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
93 * Additional data for an edge kind.
96 const char *name; /**< name of this edge kind */
97 set_edge_func_t *set_edge; /**< the set_edge function */
98 int first_idx; /**< index of the first possible edge */
99 get_edge_src_arity_func_t *get_arity; /**< the get_arity function */
100 get_edge_src_n_func_t *get_n; /**< the get_n function */
101 } ir_edge_kind_info_t;
104 * Get the predecessor block.
106 static ir_node *get_block_n(const ir_node *block, int pos)
109 return get_Block_cfgpred_block(block, pos);
114 static ir_node *get_irn_safe_n(const ir_node *node, int n)
116 if (n == -1 && is_Block(node))
118 return get_irn_n(node, n);
121 static const ir_edge_kind_info_t edge_kind_info[EDGE_KIND_LAST] = {
122 { "normal" , set_irn_n, -1, get_irn_arity, get_irn_safe_n },
123 { "block succs", NULL, 0, get_irn_arity, get_block_n },
124 { "dependency", set_irn_dep, 0, get_irn_deps, get_irn_dep }
127 #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)
128 #define get_n(irn, pos, kind) (edge_kind_info[kind].get_n(irn, pos))
129 #define get_kind_str(kind) (edge_kind_info[kind].name)
131 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
134 * This flag is set to 1, if the edges get initialized for an irg.
135 * Then register additional data is forbidden.
137 static int edges_used = 0;
140 * Summed size of all users private data
143 static size_t edges_private_size = 0;
144 #define EDGE_SIZE (sizeof(ir_edge_t) + edges_private_size)
147 * If set to 1, the list heads are checked every time an edge is changed.
149 static int edges_dbg = 0;
152 * Returns an ID for the given edge.
154 static inline long edge_get_id(const ir_edge_t *e)
160 * Announce to reserve extra space for each edge to be allocated.
162 * @param n: Size of the space to reserve
164 * @return Offset at which the private data will begin
166 * Several users can reserve extra space for private usage.
167 * Each user has to remember his given offset and the size of his private data.
168 * To be called before FIRM is initialized.
170 size_t edges_register_private_data(size_t n)
172 size_t res = edges_private_size;
174 assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
176 edges_private_size += n;
181 * Reset the user's private data at offset 'offset'
182 * The user has to remember his offset and the size of his data!
183 * Caution: Using wrong values here can destroy other users private data!
185 void edges_reset_private_data(ir_graph *irg, int offset, unsigned size)
187 irg_edge_info_t *info = get_irg_edge_info(irg, EDGE_KIND_NORMAL);
189 ir_edgeset_iterator_t iter;
191 foreach_ir_edgeset(&info->edges, edge, iter) {
192 memset(edge + sizeof(*edge) + offset, 0, size);
196 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
198 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
200 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
203 * Initialize the out information for a graph.
204 * @note Dead node elimination can call this on an already initialized graph.
206 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
208 if (edges_activated_kind(irg, kind)) {
209 irg_edge_info_t *info = get_irg_edge_info(irg, kind);
210 size_t amount = irg->estimated_node_count * 2;
213 if (info->allocated) {
214 amount = ir_edgeset_size(&info->edges);
215 ir_edgeset_destroy(&info->edges);
216 obstack_free(&info->edges_obst, NULL);
218 obstack_init(&info->edges_obst);
219 INIT_LIST_HEAD(&info->free_edges);
220 ir_edgeset_init_size(&info->edges, amount);
226 * Get the edge object of an outgoing edge at a node.
227 * @param irg The graph, the node is in.
228 * @param src The node at which the edge originates.
229 * @param pos The position of the edge.
230 * @param kind The kind of the edge.
231 * @return The corresponding edge object or NULL,
232 * if no such edge exists.
234 const ir_edge_t *get_irn_edge_kind(const ir_node *src, int pos, ir_edge_kind_t kind)
236 ir_graph *irg = get_irn_irg(src);
237 if (edges_activated_kind(irg, kind)) {
238 irg_edge_info_t *info = get_irg_edge_info(irg, kind);
241 key.src = (ir_node *)src;
244 return ir_edgeset_find(&info->edges, &key);
251 * Change the out count
253 * @param tgt the edge target
254 * @param kind the kind of the edge
256 static inline void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs)
258 irn_edge_info_t *info = get_irn_edge_info(tgt, kind);
259 info->out_count += ofs;
263 * Verify the edge list of a node, ie. ensure it's a loop:
264 * head -> e_1 -> ... -> e_n -> head
266 static inline void verify_list_head(ir_node *irn, ir_edge_kind_t kind)
270 pset *lh_set = pset_new_ptr(16);
271 const struct list_head *head = &get_irn_edge_info(irn, kind)->outs_head;
272 const struct list_head *pos;
274 list_for_each(pos, head) {
275 if (pset_find_ptr(lh_set, pos)) {
276 const ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
278 ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
279 fprintf(stderr, "- at list entry %d\n", num);
281 fprintf(stderr, "- edge(%ld) is invalid\n", edge_get_id(edge));
283 ir_fprintf(stderr, "- edge(%ld) %+F(%d)\n", edge_get_id(edge), edge->src, edge->pos);
288 pset_insert_ptr(lh_set, pos);
296 void edges_dump_kind(ir_graph *irg, ir_edge_kind_t kind)
298 irg_edge_info_t *info;
300 ir_edgeset_iterator_t iter;
303 if (!edges_activated_kind(irg, kind))
306 info = get_irg_edge_info(irg, kind);
307 edges = &info->edges;
308 foreach_ir_edgeset(edges, e, iter) {
309 ir_printf("%+F %d %d\n", e->src, e->pos, e->invalid);
313 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
314 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
315 ir_node *old_tgt, ir_edge_kind_t kind,
318 const char *msg = "";
319 irg_edge_info_t *info;
323 assert(edges_activated_kind(irg, kind));
326 * Only do something, if the old and new target differ.
331 info = get_irg_edge_info(irg, kind);
332 edges = &info->edges;
334 /* Initialize the edge template to search in the set. */
339 * If the target is NULL, the edge shall be deleted.
342 /* search the edge in the set. */
343 ir_edge_t *edge = ir_edgeset_find(edges, &templ);
345 /* mark the edge invalid if it was found */
348 list_del(&edge->list);
349 ir_edgeset_remove(edges, edge);
350 list_add(&edge->list, &info->free_edges);
354 edge_change_cnt(old_tgt, kind, -1);
356 /* If the edge was not found issue a warning on the debug stream */
357 msg = "edge to delete not found!\n";
361 * The target is not NULL and the old target differs
362 * from the new target, the edge shall be moved (if the
363 * old target was != NULL) or added (if the old target was
366 struct list_head *head = &get_irn_edge_info(tgt, kind)->outs_head;
368 assert(head->next && head->prev &&
369 "target list head must have been initialized");
371 /* If the old target is not null, the edge is moved. */
373 ir_edge_t *edge = ir_edgeset_find(edges, &templ);
374 assert(edge && "edge to redirect not found!");
375 assert(! edge->invalid && "Invalid edge encountered");
379 list_move(&edge->list, head);
380 edge_change_cnt(old_tgt, kind, -1);
382 /* The old target was NULL, thus, the edge is newly created. */
386 if (list_empty(&info->free_edges)) {
387 edge = (ir_edge_t*)obstack_alloc(&info->edges_obst, EDGE_SIZE);
389 edge = list_entry(info->free_edges.next, ir_edge_t, list);
390 list_del(&edge->list);
398 edge->list.next = NULL;
399 edge->list.prev = NULL;
400 memset(edge + 1, 0, edges_private_size);
402 new_edge = ir_edgeset_insert(edges, edge);
403 if (new_edge != edge) {
404 panic("new edge exists already");
408 list_add(&edge->list, head);
411 edge_change_cnt(tgt, kind, +1);
414 #ifndef DEBUG_libfirm
415 /* verify list heads */
418 verify_list_head(tgt, kind);
420 verify_list_head(old_tgt, kind);
424 DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
427 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt,
430 if (edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
431 edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
434 if (edges_activated_kind(irg, EDGE_KIND_BLOCK)) {
436 ir_node *bl_old = old_tgt ? get_nodes_block(old_tgt) : NULL;
437 ir_node *bl_tgt = NULL;
440 bl_tgt = is_Bad(tgt) ? tgt : get_nodes_block(tgt);
442 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
443 } else if (get_irn_mode(src) == mode_X && old_tgt != NULL && is_Block(old_tgt)) {
444 /* moving a jump node from one block to another */
445 const ir_edge_t *edge;
446 const ir_edge_t *next;
447 foreach_out_edge_kind_safe(old_tgt, edge, next, EDGE_KIND_BLOCK) {
448 ir_node *succ = get_edge_src_irn(edge);
449 int succ_pos = get_edge_src_pos(edge);
450 ir_node *block_pred = get_Block_cfgpred(succ, succ_pos);
451 if (block_pred != src)
453 edges_notify_edge_kind(succ, succ_pos, tgt, old_tgt,
454 EDGE_KIND_BLOCK, irg);
461 * Delete all in edges of a given kind from the node old.
463 * @param old the node
464 * @param kind the kind of edges to remove
465 * @param irg the irg of the old node
467 static void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind)
470 ir_graph *irg = get_irn_irg(old);
472 if (!edges_activated_kind(irg, kind))
475 DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
477 foreach_tgt(old, i, n, kind) {
478 ir_node *old_tgt = get_n(old, i, kind);
479 edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
484 * A node might be revivaled by CSE. Assure its edges.
486 * @param irn the node
487 * @param kind the kind of edges to remove
488 * @param irg the irg of the old node
490 static void edges_node_revival_kind(ir_node *irn, ir_edge_kind_t kind)
492 irn_edge_info_t *info;
494 ir_graph *irg = get_irn_irg(irn);
496 if (!edges_activated_kind(irg, kind))
499 info = get_irn_edge_info(irn, kind);
500 if (info->edges_built)
503 DBG((dbg, LEVEL_5, "node revivaled (kind: %s): %+F\n", get_kind_str(kind), irn));
505 foreach_tgt(irn, i, n, kind) {
506 ir_node *tgt = get_n(irn, i, kind);
507 edges_notify_edge_kind(irn, i, tgt, NULL, kind, irg);
509 info->edges_built = 1;
512 typedef struct build_walker {
515 unsigned problem_found;
519 * Post-Walker: notify all edges
521 static void build_edges_walker(ir_node *irn, void *data)
523 build_walker *w = (build_walker*)data;
525 ir_edge_kind_t kind = w->kind;
526 ir_graph *irg = get_irn_irg(irn);
528 foreach_tgt(irn, i, n, kind) {
529 ir_node *pred = get_n(irn, i, kind);
530 edges_notify_edge_kind(irn, i, pred, NULL, kind, irg);
532 get_irn_edge_info(irn, kind)->edges_built = 1;
536 * Pre-Walker: initializes the list-heads and set the out-count
539 static void init_lh_walker(ir_node *irn, void *data)
541 build_walker *w = (build_walker*)data;
542 ir_edge_kind_t kind = w->kind;
543 list_head *head = &get_irn_edge_info(irn, kind)->outs_head;
544 INIT_LIST_HEAD(head);
545 get_irn_edge_info(irn, kind)->edges_built = 0;
546 get_irn_edge_info(irn, kind)->out_count = 0;
550 * Pre-Walker: initializes the list-heads and set the out-count
553 * Additionally touches DEP nodes, as they might be DEAD.
554 * THIS IS UGLY, but I don't find a better way until we
556 * a) ensure that dead nodes are not used as input
557 * b) it might be sufficient to add those stupid NO_REG nodes
560 static void init_lh_walker_dep(ir_node *irn, void *data)
562 build_walker *w = (build_walker*)data;
563 ir_edge_kind_t kind = w->kind;
564 list_head *head = &get_irn_edge_info(irn, kind)->outs_head;
567 INIT_LIST_HEAD(head);
568 get_irn_edge_info(irn, kind)->edges_built = 0;
569 get_irn_edge_info(irn, kind)->out_count = 0;
571 for (i = get_irn_deps(irn) - 1; i >= 0; --i) {
572 ir_node *dep = get_irn_dep(irn, i);
574 head = &get_irn_edge_info(dep, kind)->outs_head;
576 INIT_LIST_HEAD(head);
577 get_irn_edge_info(dep, kind)->edges_built = 0;
578 get_irn_edge_info(dep, kind)->out_count = 0;
582 typedef struct visitor_info_t {
583 irg_walk_func *visit;
588 * Visitor: initializes the list-heads and set the out-count
589 * of all nodes to 0 of nodes that are not seen so far.
591 static void visitor(ir_node *irn, void *data)
593 visitor_info_t *info = (visitor_info_t*)data;
597 if (!is_Block(irn) && is_Deleted(get_nodes_block(irn)))
600 if (!irn_visited_else_mark(irn)) {
601 info->visit(irn, info->data);
606 * Build the initial edge set.
607 * Beware, this is not a simple task because it suffers from two
609 * - the anchor set allows access to Nodes that may not be reachable from
611 * - the identities add nodes to the "root set" that are not yet reachable
612 * from End. However, after some transformations, the CSE may revival these
615 * These problems can be fixed using different strategies:
616 * - Add an age flag to every node. Whenever the edge of a node is older
617 * then the current edge, invalidate the edges of this node.
618 * While this would help for revivaled nodes, it increases memory and runtime.
619 * - Delete the identities set.
620 * Solves the revival problem, but may increase the memory consumption, as
621 * nodes cannot be revivaled at all.
622 * - Manually iterate over the identities root set. This did not consume more memory
623 * but increase the computation time because the |identities| >= |V|
625 * Currently, we use the last option.
627 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
629 struct build_walker w;
630 irg_edge_info_t *info = get_irg_edge_info(irg, kind);
631 visitor_info_t visit;
637 assert(!info->activated);
640 edges_init_graph_kind(irg, kind);
641 if (kind == EDGE_KIND_DEP) {
642 irg_walk_anchors(irg, init_lh_walker_dep, NULL, &w);
643 /* Argh: Dep nodes might be dead, so we MUST visit identities first */
644 visit.visit = init_lh_walker_dep;
645 visit_all_identities(irg, visitor, &visit);
646 irg_walk_anchors(irg, NULL, build_edges_walker, &w);
648 visit.visit = init_lh_walker;
649 visit_all_identities(irg, visitor, &visit);
650 irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
654 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
656 irg_edge_info_t *info = get_irg_edge_info(irg, kind);
659 if (info->allocated) {
660 obstack_free(&info->edges_obst, NULL);
661 ir_edgeset_destroy(&info->edges);
666 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
668 return edges_activated_kind_(irg, kind);
673 * Reroute all use-edges from a node to another.
674 * @param from The node whose use-edges shall be withdrawn.
675 * @param to The node to which all the use-edges of @p from shall be
677 * @param irg The graph.
679 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind)
681 ir_graph *irg = get_irn_irg(from);
682 set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
684 if (set_edge && edges_activated_kind(irg, kind)) {
685 struct list_head *head = &get_irn_edge_info(from, kind)->outs_head;
687 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
689 while (head != head->next) {
690 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
691 assert(edge->pos >= -1);
692 set_edge(edge->src, edge->pos, to);
697 static void verify_set_presence(ir_node *irn, void *data)
699 build_walker *w = (build_walker*)data;
700 ir_graph *irg = get_irn_irg(irn);
701 ir_edgeset_t *edges = &get_irg_edge_info(irg, w->kind)->edges;
704 foreach_tgt(irn, i, n, w->kind) {
710 e = ir_edgeset_find(edges, &templ);
714 w->problem_found = 1;
719 static void verify_list_presence(ir_node *irn, void *data)
721 build_walker *w = (build_walker*)data;
724 bitset_set(w->reachable, get_irn_idx(irn));
726 /* check list heads */
727 verify_list_head(irn, w->kind);
729 foreach_out_edge_kind(irn, e, w->kind) {
732 if (w->kind == EDGE_KIND_NORMAL && get_irn_arity(e->src) <= e->pos) {
733 w->problem_found = 1;
737 tgt = get_n(e->src, e->pos, w->kind);
740 w->problem_found = 1;
745 int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
747 struct build_walker w;
748 ir_edgeset_t *edges = &get_irg_edge_info(irg, kind)->edges;
750 ir_edgeset_iterator_t iter;
753 w.reachable = bitset_alloca(get_irg_last_idx(irg));
756 /* Clear the present bit in all edges available. */
757 foreach_ir_edgeset(edges, e, iter) {
761 irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
764 * Dump all edges which are not invalid and not present.
765 * These edges are superfluous and their presence in the
768 foreach_ir_edgeset(edges, e, iter) {
769 if (! e->invalid && ! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
771 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d is superfluous\n", edge_get_id(e), e->src, e->pos);
775 return w.problem_found;
778 #define IGNORE_NODE(irn) (is_Bad((irn)) || is_Block((irn)))
781 * Clear link field of all nodes.
783 static void clear_links(ir_node *irn, void *env)
789 if (IGNORE_NODE(irn)) {
790 set_irn_link(irn, NULL);
794 irg = get_irn_irg(irn);
795 bs = bitset_malloc(get_irg_last_idx(irg));
796 set_irn_link(irn, bs);
800 * Increases count (stored in link field) for all operands of a node.
802 static void count_user(ir_node *irn, void *env)
808 first = is_Block(irn) ? 0 : -1;
809 for (i = get_irn_arity(irn) - 1; i >= first; --i) {
810 ir_node *op = get_irn_n(irn, i);
811 bitset_t *bs = (bitset_t*)get_irn_link(op);
814 bitset_set(bs, get_irn_idx(irn));
819 * Verifies if collected count, number of edges in list and stored edge count are in sync.
821 static void verify_edge_counter(ir_node *irn, void *env)
823 build_walker *w = (build_walker*)env;
829 const struct list_head *head;
830 const struct list_head *pos;
833 if (IGNORE_NODE(irn))
836 bs = (bitset_t*)get_irn_link(irn);
839 edge_cnt = get_irn_edge_info(irn, EDGE_KIND_NORMAL)->out_count;
840 head = &get_irn_edge_info(irn, EDGE_KIND_NORMAL)->outs_head;
842 /* We can iterate safely here, list heads have already been verified. */
843 list_for_each(pos, head) {
847 /* check all nodes that reference us and count edges that point number
848 * of ins that actually point to us */
849 irg = get_irn_irg(irn);
851 bitset_foreach(bs, idx) {
853 ir_node *src = get_idx_irn(irg, idx);
855 arity = get_irn_arity(src);
856 for (i = 0; i < arity; ++i) {
857 ir_node *in = get_irn_n(src, i);
863 if (edge_cnt != list_cnt) {
864 w->problem_found = 1;
865 ir_fprintf(stderr, "Edge Verifier: edge count is %d, but %d edge(s) are recorded in list at %+F\n",
866 edge_cnt, list_cnt, irn);
869 if (ref_cnt != list_cnt) {
870 w->problem_found = 1;
871 ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d node(s), but the list contains %d edge(s)\n",
872 irn, ref_cnt, list_cnt);
879 * Verifies the out edges of an irg.
881 int edges_verify(ir_graph *irg)
883 struct build_walker w;
884 int problem_found = 0;
886 /* verify normal edges only */
887 problem_found = edges_verify_kind(irg, EDGE_KIND_NORMAL);
889 w.kind = EDGE_KIND_NORMAL;
893 irg_walk_anchors(irg, clear_links, count_user, &w);
894 irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
896 return problem_found ? 1 : w.problem_found;
899 typedef struct pass_t {
900 ir_graph_pass_t pass;
901 unsigned assert_on_problem;
905 * Wrapper to edges_verify to be run as an ir_graph pass.
907 static int edges_verify_wrapper(ir_graph *irg, void *context)
909 pass_t *pass = (pass_t*)context;
910 int problems_found = edges_verify(irg);
911 /* do NOT rerun the pass if verify is ok :-) */
912 assert(problems_found && pass->assert_on_problem);
916 /* Creates an ir_graph pass for edges_verify(). */
917 ir_graph_pass_t *irg_verify_edges_pass(const char *name, unsigned assert_on_problem)
919 pass_t *pass = XMALLOCZ(pass_t);
921 def_graph_pass_constructor(
922 &pass->pass, name ? name : "edges_verify", edges_verify_wrapper);
924 /* neither dump nor verify */
925 pass->pass.dump_irg = (DUMP_ON_IRG_FUNC)ir_prog_no_dump;
926 pass->pass.verify_irg = (RUN_ON_IRG_FUNC)ir_prog_no_verify;
928 pass->assert_on_problem = assert_on_problem;
932 void init_edges(void)
934 FIRM_DBG_REGISTER(dbg, DBG_EDGES);
937 void edges_init_dbg(int do_dbg)
942 void edges_activate(ir_graph *irg)
944 edges_activate_kind(irg, EDGE_KIND_NORMAL);
945 edges_activate_kind(irg, EDGE_KIND_BLOCK);
946 if (get_irg_phase_state(irg) == phase_backend)
947 edges_activate_kind(irg, EDGE_KIND_DEP);
950 void edges_deactivate(ir_graph *irg)
952 if (get_irg_phase_state(irg) == phase_backend)
953 edges_deactivate_kind(irg, EDGE_KIND_DEP);
954 edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
955 edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
958 int edges_assure(ir_graph *irg)
962 if (edges_activated_kind(irg, EDGE_KIND_BLOCK)) {
965 edges_activate_kind(irg, EDGE_KIND_BLOCK);
967 if (edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
970 edges_activate_kind(irg, EDGE_KIND_NORMAL);
976 int edges_assure_kind(ir_graph *irg, ir_edge_kind_t kind)
978 int activated = edges_activated_kind(irg, kind);
981 edges_activate_kind(irg, kind);
986 void edges_node_deleted(ir_node *irn)
988 edges_node_deleted_kind(irn, EDGE_KIND_NORMAL);
989 edges_node_deleted_kind(irn, EDGE_KIND_BLOCK);
992 void edges_node_revival(ir_node *irn)
994 edges_node_revival_kind(irn, EDGE_KIND_NORMAL);
995 edges_node_revival_kind(irn, EDGE_KIND_BLOCK);
998 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
1000 return get_irn_out_edge_first_kind_(irn, kind);
1003 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
1005 return get_irn_out_edge_next_(irn, last);
1008 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
1010 return get_edge_src_irn_(edge);
1013 int (get_edge_src_pos)(const ir_edge_t *edge)
1015 return get_edge_src_pos_(edge);
1018 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
1020 return get_irn_n_edges_kind_(irn, kind);
1023 static void irg_walk_edges2(ir_node *node, irg_walk_func *pre,
1024 irg_walk_func *post, void *env)
1026 const ir_edge_t *edge, *next;
1028 if (irn_visited_else_mark(node))
1034 foreach_out_edge_kind_safe(node, edge, next, EDGE_KIND_NORMAL) {
1035 /* find the corresponding successor block. */
1036 ir_node *pred = get_edge_src_irn(edge);
1037 irg_walk_edges2(pred, pre, post, env);
1044 void irg_walk_edges(ir_node *node, irg_walk_func *pre, irg_walk_func *post,
1047 ir_graph *irg = get_irn_irg(node);
1049 assert(edges_activated(irg));
1050 assert(is_Block(node));
1052 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1054 inc_irg_visited(irg);
1055 irg_walk_edges2(node, pre, post, env);
1057 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
1060 static void irg_block_edges_walk2(ir_node *bl, irg_walk_func *pre,
1061 irg_walk_func *post, void *env)
1063 const ir_edge_t *edge, *next;
1065 if (!Block_block_visited(bl)) {
1066 mark_Block_block_visited(bl);
1071 foreach_out_edge_kind_safe(bl, edge, next, EDGE_KIND_BLOCK) {
1072 /* find the corresponding successor block. */
1073 ir_node *pred = get_edge_src_irn(edge);
1074 irg_block_edges_walk2(pred, pre, post, env);
1082 void irg_block_edges_walk(ir_node *node, irg_walk_func *pre,
1083 irg_walk_func *post, void *env)
1085 ir_graph *irg = get_irn_irg(node);
1087 assert(edges_activated(irg));
1088 assert(is_Block(node));
1090 ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
1092 inc_irg_block_visited(irg);
1093 irg_block_edges_walk2(node, pre, post, env);
1095 ir_free_resources(irg, IR_RESOURCE_BLOCK_VISITED);