use edge counter instead of recalculation for getting number of out-edges
[libfirm] / ir / ir / iredges.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/iredges.c
4  * Purpose:     Always available outs.
5  * Author:      Sebastian Hack
6  * Modified by: Michael Beck, Andreas Schoesser
7  * Created:     14.1.2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2006 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * Always available outs.
15  * @author Sebastian Hack
16  * @date 14.1.2005
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #ifdef HAVE_ALLOCA_H
24 #include <alloca.h>
25 #endif
26 #ifdef HAVE_MALLOC_H
27 #include <malloc.h>
28 #endif
29
30 #include "irnode_t.h"
31 #include "iropt_t.h"
32 #include "iredgekinds.h"
33 #include "iredges_t.h"
34 #include "irgwalk.h"
35 #include "irdump_t.h"
36 #include "irprintf.h"
37 #include "irhooks.h"
38 #include "debug.h"
39 #include "set.h"
40 #include "bitset.h"
41
42 /**
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.
46 */
47 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
48
49 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
50
51 typedef int (get_edge_src_first_func_t)(const ir_node *src);
52
53 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
54
55 /**
56 * Additional data for an edge kind.
57 */
58 typedef struct {
59         const char                *name;
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;
65
66 static int get_zero(const ir_node *irn)
67 {
68         return 0;
69 }
70
71 static int get_irn_first(const ir_node *irn)
72 {
73         return 0 - !is_Block(irn);
74 }
75
76 static ir_node *get_block_n(const ir_node *irn, int pos)
77 {
78         return is_Block(irn) ? get_Block_cfgpred_block((ir_node *) irn, pos) : 0;
79 }
80
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 }
85 };
86
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)
90
91 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
92
93 /**
94  * This flag is set to 1, if the edges get initialized for an irg.
95  * Then register additional data is forbidden.
96  */
97 static int edges_used = 0;
98
99 static int edges_private_size = 0;
100 #define EDGE_SIZE (sizeof(ir_edge_t) + edges_private_size)
101
102 /**
103  * If set to 1, the list heads are checked every time an edge is changed.
104  */
105 static int edges_dbg = 0;
106
107 int edges_register_private_data(size_t n)
108 {
109         int res = edges_private_size;
110
111         assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
112
113         edges_private_size += n;
114         return res;
115 }
116
117 /**
118  * Reset the user's private data at offset 'offset'
119  */
120
121 void edges_reset_private_data(ir_graph *irg, int offset, size_t size)
122 {
123         irg_edge_info_t *info = _get_irg_edge_info(irg, EDGE_KIND_NORMAL);
124         set_entry *entry;
125
126         foreach_set(info->edges, entry)
127         {
128                 ir_edge_t *edge = (ir_edge_t *) entry->dptr;
129                 memset(edge + sizeof(*edge) + offset, 0, size);
130         }
131 }
132
133 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
134
135 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
136
137 static int edge_cmp(const void *p1, const void *p2, size_t len)
138 {
139         const ir_edge_t *e1 = p1;
140         const ir_edge_t *e2 = p2;
141         int             res = e1->src == e2->src && e1->pos == e2->pos;
142
143         return ! res;
144 }
145
146 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
147
148 /**
149  * Initialize the out information for a graph.
150  * @note Dead node elimination can call this on an already initialized graph.
151  */
152 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
153 {
154         if(edges_activated_kind(irg, kind)) {
155                 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
156                 int amount = 2048;
157
158                 edges_used = 1;
159                 if(info->edges) {
160                         amount = set_count(info->edges);
161                         del_set(info->edges);
162                 }
163                 info->edges = new_set(edge_cmp, amount);
164         }
165 }
166
167 /**
168  * Get the edge object of an outgoing edge at a node.
169  * @param   irg The graph, the node is in.
170  * @param   src The node at which the edge originates.
171  * @param   pos The position of the edge.
172  * @return      The corresponding edge object or NULL,
173  *              if no such edge exists.
174  */
175 const ir_edge_t *get_irn_edge_kind(ir_graph *irg, const ir_node *src, int pos, ir_edge_kind_t kind)
176 {
177         if (edges_activated_kind(irg, kind)) {
178                 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
179                 ir_edge_t       key;
180
181                 key.src = (ir_node *)src;
182                 key.pos = pos;
183                 return set_find(info->edges, &key, EDGE_SIZE, edge_hash(&key));
184         }
185
186         return NULL;
187 }
188
189 /**
190  * Get the edge object of an outgoing edge at a node.
191  * Looks for an edge for all kinds.
192  */
193
194 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
195 {
196         const ir_edge_t *edge;
197         if((edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_NORMAL)) == NULL)
198                 edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_BLOCK);
199         return(edge);
200 }
201
202 /**
203  * Change the out count
204  */
205 static INLINE void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs) {
206         irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
207         info->out_count += ofs;
208 }
209
210 /**
211  * Verify the edge list of a node, ie. ensure it's a loop:
212  * head -> e_1 -> ... -> e_n -> head
213  */
214 static INLINE void vrfy_list_head(ir_node *irn, ir_edge_kind_t kind) {
215         int                    err       = 0;
216         int                    num       = 0;
217         pset                   *lh_set   = pset_new_ptr(16);
218         const struct list_head *head     = _get_irn_outs_head(irn, kind);
219         const struct list_head *pos;
220
221         list_for_each(pos, head) {
222                 if (pset_find_ptr(lh_set, pos)) {
223                         const ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
224
225                         ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
226                         fprintf(stderr, "- at list entry %d\n", num);
227                         if (edge->invalid)
228                                 fprintf(stderr, "- edge is invalid\n");
229                         if (edge->src);
230                                 ir_fprintf(stderr, "- edge %+F(%d)\n", edge->src, edge->pos);
231                         err = 1;
232                         break;
233                 }
234                 num++;
235                 pset_insert_ptr(lh_set, pos);
236         }
237
238         del_pset(lh_set);
239
240         assert(err == 0);
241 }
242
243 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
244 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 {
246         const char *msg = "";
247
248         assert(edges_activated_kind(irg, kind));
249
250         /*
251          * Only do something, if the old and new target differ.
252          */
253         if(tgt != old_tgt) {
254                 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
255                 set *edges            = info->edges;
256                 ir_edge_t *templ      = alloca(EDGE_SIZE);
257                 ir_edge_t *edge;
258
259                 /* Initialize the edge template to search in the set. */
260                 memset(templ, 0, EDGE_SIZE);
261                 templ->src     = src;
262                 templ->pos     = pos;
263                 templ->invalid = 0;
264                 templ->present = 0;
265                 templ->kind    = kind;
266                 DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
267
268                 /*
269                  * If the target is NULL, the edge shall be deleted.
270                  */
271                 if (tgt == NULL) {
272                         /* search the edge in the set. */
273                         edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
274
275                         /* mark the edge invalid if it was found */
276                         if (edge) {
277                                 msg = "deleting";
278                                 list_del(&edge->list);
279                                 edge->invalid = 1;
280                                 edge->pos = -2;
281                                 edge->src = NULL;
282                                 edge_change_cnt(old_tgt, kind, -1);
283                         }
284
285                         /* If the edge was not found issue a warning on the debug stream */
286                         else {
287                                 msg = "edge to delete not found!\n";
288                         }
289                 } /* if */
290
291                 /*
292                  * The target is not NULL and the old target differs
293                  * from the new target, the edge shall be moved (if the
294                  * old target was != NULL) or added (if the old target was
295                  * NULL).
296                  */
297                 else {
298                         struct list_head *head = _get_irn_outs_head(tgt, kind);
299
300                         assert(head->next && head->prev &&
301                                         "target list head must have been initialized");
302
303                         /* If the old target is not null, the edge is moved. */
304                         if (old_tgt) {
305                                 edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
306                                 assert(edge && "edge to redirect not found!");
307                                 assert(! edge->invalid && "Invalid edge encountered");
308
309                                 msg = "redirecting";
310
311                                 list_move(&edge->list, head);
312                                 edge_change_cnt(old_tgt, kind, -1);
313                         }
314
315                         /* The old target was null, thus, the edge is newly created. */
316                         else {
317                                 edge = set_insert(edges, templ, EDGE_SIZE, edge_hash(templ));
318
319                                 assert(! edge->invalid && "Freshly inserted edge is invalid?!?");
320                                 assert(edge->list.next == NULL && edge->list.prev == NULL &&
321                                         "New edge must not have list head initialized");
322
323                                 msg = "adding";
324                                 list_add(&edge->list, head);
325                         }
326
327                         edge_change_cnt(tgt, kind, +1);
328                 } /* else */
329
330                 /* verify list heads */
331                 if (edges_dbg) {
332                         if (tgt)
333                                 vrfy_list_head(tgt, kind);
334                         if (old_tgt)
335                                 vrfy_list_head(old_tgt, kind);
336                 }
337         }
338
339         /* If the target and the old target are equal, nothing is done. */
340         DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
341 }
342
343 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
344 {
345         if(edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
346                 edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
347         }
348
349         if (edges_activated_kind(irg, EDGE_KIND_BLOCK) && is_Block(src)) {
350                 /* do not use get_nodes_block() here, it fails when running unpinned */
351                 ir_node *bl_old = old_tgt ? get_irn_n(skip_Proj(old_tgt), -1) : NULL;
352                 ir_node *bl_tgt = NULL;
353
354                 if (tgt)
355                         bl_tgt = is_Bad(tgt) ? tgt : get_irn_n(skip_Proj(tgt), -1);
356
357                 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
358         }
359 }
360
361
362 void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
363 {
364         int i, n;
365
366         if(!edges_activated_kind(irg, kind))
367                 return;
368
369         DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
370
371         foreach_tgt(old, i, n, kind) {
372                 ir_node *old_tgt = get_n(old, i, kind);
373                 edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
374         }
375 }
376
377 struct build_walker {
378         ir_graph       *irg;
379         ir_edge_kind_t kind;
380         bitset_t       *reachable;
381         unsigned       problem_found;
382 };
383
384 /**
385  * Post-Walker: notify all edges
386  */
387 static void build_edges_walker(ir_node *irn, void *data) {
388         struct build_walker *w = data;
389         int                 i, n;
390
391         if (! edges_activated_kind(w->irg, w->kind))
392                 return;
393
394         foreach_tgt(irn, i, n, w->kind)
395                 edges_notify_edge_kind(irn, i, get_n(irn, i, w->kind), NULL, w->kind, w->irg);
396 }
397
398 /**
399  * Pre-Walker: initializes the list-heads and set the out-count
400  * of all nodes to 0.
401  */
402 static void init_lh_walker(ir_node *irn, void *data) {
403         struct build_walker *w = data;
404         INIT_LIST_HEAD(_get_irn_outs_head(irn, w->kind));
405         _get_irn_edge_info(irn, w->kind)->out_count = 0;
406 }
407
408 /**
409  * Visitor: initializes the list-heads and set the out-count
410  * of all nodes to 0 of nodes that are not seen so far.
411  */
412 static void visitor(ir_node *irn, void *data) {
413         if (irn_not_visited(irn)) {
414                 mark_irn_visited(irn);
415                 init_lh_walker(irn, data);
416         }
417 }
418
419 /*
420  * Build the initial edge set.
421  * Beware, this is not a simple task because it suffers from two
422  * difficulties:
423  * - the anchor set allows access to Nodes that may not be reachable from
424  *   the End node
425  * - the identities add nodes to the "root set" that are not yet reachable
426  *   from End. However, after some transformations, the CSE may revival these
427  *   nodes
428  *
429  * These problems can be fixed using different strategies:
430  * - Add an age flag to every node. Whenever the edge of a node is older
431  *   then the current edge, invalidate the edges of this node.
432  *   While this would help for revivaled nodes, it increases memory and runtime.
433  * - Delete the identities set.
434  *   Solves the revival problem, but may increase the memory consumption, as
435  *   nodes cannot be revivaled at all.
436  * - Manually iterate over the identities root set. This did not consume more memory
437  *   but increase the computation time because the |identities| >= |V|
438  *
439  * Currently, we use the last option.
440  */
441 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
442 {
443         struct build_walker w;
444         irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
445
446         w.irg  = irg;
447         w.kind = kind;
448
449         info->activated = 1;
450         edges_init_graph_kind(irg, kind);
451         //irg_walk_graph(irg, init_lh_walker, build_edges_walker, &w);
452         inc_irg_visited(irg);
453         irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
454         visit_all_identities(irg, visitor, &w);
455 }
456
457 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
458 {
459         irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
460
461         info->activated = 0;
462         if (info->edges) {
463                 del_set(info->edges);
464                 info->edges = NULL;
465         }
466 }
467
468 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
469 {
470         return _edges_activated_kind(irg, kind);
471 }
472
473
474 /**
475  * Reroute all use-edges from a node to another.
476  * @param from The node whose use-edges shall be withdrawn.
477  * @param to   The node to which all the use-edges of @p from shall be
478  *             sent to.
479  * @param irg  The graph.
480  */
481 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind, ir_graph *irg)
482 {
483         set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
484
485         if(set_edge && edges_activated_kind(irg, kind)) {
486                 struct list_head *head = _get_irn_outs_head(from, kind);
487
488                 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
489
490                 while(head != head->next) {
491                         ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
492                         assert(edge->pos >= -1);
493                         set_edge(edge->src, edge->pos, to);
494                 }
495         }
496 }
497
498 static void verify_set_presence(ir_node *irn, void *data)
499 {
500         struct build_walker *w     = data;
501         set                 *edges = _get_irg_edge_info(w->irg, w->kind)->edges;
502         int i, n;
503
504         foreach_tgt(irn, i, n, w->kind) {
505                 ir_edge_t templ, *e;
506
507                 templ.src = irn;
508                 templ.pos = i;
509
510                 e = set_find(edges, &templ, EDGE_SIZE, edge_hash(&templ));
511                 if(e != NULL)
512                         e->present = 1;
513                 else {
514                         w->problem_found = 1;
515                         ir_fprintf(stderr, "Edge Verifier: edge %+F,%d (kind: \"%s\") is missing\n", irn, i, get_kind_str(w->kind));
516                 }
517         }
518 }
519
520 static void verify_list_presence(ir_node *irn, void *data)
521 {
522         struct build_walker *w = data;
523         const ir_edge_t     *e;
524
525         bitset_set(w->reachable, get_irn_idx(irn));
526
527         /* check list heads */
528         vrfy_list_head(irn, w->kind);
529
530         foreach_out_edge_kind(irn, e, w->kind) {
531                 ir_node *tgt;
532
533                 if (w->kind == EDGE_KIND_NORMAL && get_irn_arity(e->src) <= e->pos) {
534                         w->problem_found = 1;
535                         ir_fprintf(stderr, "Edge Verifier: edge %+F -> %+F recorded at src position %d, but src has arity %d\n",
536                                 e->src, irn, e->pos, get_irn_arity(e->src));
537                         continue;
538                 }
539
540                 tgt = get_n(e->src, e->pos, w->kind);
541
542                 if (irn != tgt) {
543                         w->problem_found = 1;
544                         ir_fprintf(stderr, "Edge Verifier: edge %+F,%d (kind \"%s\") is no out edge of %+F but of %+F\n",
545                                 e->src, e->pos, get_kind_str(w->kind), irn, tgt);
546                 }
547         }
548 }
549
550 int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
551 {
552         struct build_walker w;
553         set                 *edges = _get_irg_edge_info(irg, kind)->edges;
554         ir_edge_t           *e;
555
556         w.irg           = irg;
557         w.kind          = kind;
558         w.reachable     = bitset_alloca(get_irg_last_idx(irg));
559         w.problem_found = 0;
560
561         /* Clear the present bit in all edges available. */
562         for (e = set_first(edges); e; e = set_next(edges))
563                 e->present = 0;
564
565         irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
566
567         /*
568          * Dump all edges which are not invalid and not present.
569          * These edges are superfluous and their presence in the
570          * edge set is wrong.
571          */
572         for (e = set_first(edges); e; e = set_next(edges)) {
573                 if (! e->invalid && ! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
574                         w.problem_found = 1;
575                         ir_fprintf(stderr, "Edge Verifier: edge %+F,%d is superfluous\n", e->src, e->pos);
576                 }
577         }
578
579         return w.problem_found;
580 }
581
582 #define IGNORE_NODE(irn) (is_Bad((irn)) || is_Block((irn)))
583
584 /**
585  * Clear link field of all nodes.
586  */
587 static void clear_links(ir_node *irn, void *env) {
588         struct build_walker *w  = env;
589         bitset_t            *bs;
590
591         set_irn_link(irn, NULL);
592
593         if (IGNORE_NODE(irn))
594                 return;
595
596         bs = bitset_malloc(get_irg_last_idx(w->irg));
597         set_irn_link(irn, bs);
598 }
599
600 /**
601  * Increases count (stored in link field) for all operands of a node.
602  */
603 static void count_user(ir_node *irn, void *env) {
604         int i;
605         int first;
606
607         first = get_irn_first(irn);
608         for (i = get_irn_arity(irn) - 1; i >= first; --i) {
609                 ir_node  *op = get_irn_n(irn, i);
610                 bitset_t *bs = get_irn_link(op);
611
612                 if (bs)
613                         bitset_set(bs, get_irn_idx(irn));
614         }
615 }
616
617 /**
618  * Verifies if collected count, number of edges in list and stored edge count are in sync.
619  */
620 static void verify_edge_counter(ir_node *irn, void *env) {
621         struct build_walker    *w       = env;
622         bitset_t               *bs;
623         int                    list_cnt;
624         int                    ref_cnt;
625         int                    edge_cnt;
626         const struct list_head *head;
627         const struct list_head *pos;
628
629         if (IGNORE_NODE(irn))
630                 return;
631
632         bs       = get_irn_link(irn);
633         list_cnt = 0;
634         ref_cnt  = bitset_popcnt(bs);
635         edge_cnt = _get_irn_edge_info(irn, EDGE_KIND_NORMAL)->out_count;
636         head     = _get_irn_outs_head(irn, EDGE_KIND_NORMAL);
637
638         /* We can iterate safely here, list heads have already been verified. */
639         list_for_each(pos, head) {
640                 ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
641                 if (! is_Bad(edge->src))
642                         list_cnt++;
643         }
644
645         if (edge_cnt != list_cnt) {
646                 w->problem_found = 1;
647                 ir_fprintf(stderr, "Edge Verifier: edge count is %d, but %d edge(s) are recorded in list at %+F\n",
648                         edge_cnt, list_cnt, irn);
649         }
650
651         if (ref_cnt != list_cnt) {
652                 unsigned long idx;
653
654                 w->problem_found = 1;
655                 ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d node(s), but %d edge(s) recorded in list\n",
656                         irn, ref_cnt, list_cnt);
657
658                 list_for_each(pos, head) {
659                         ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
660                         if (! is_Bad(edge->src))
661                                 bitset_flip(bs, get_irn_idx(edge->src));
662                 }
663
664                 if (ref_cnt < list_cnt)
665                         fprintf(stderr,"               following nodes are recorded in list, but not as user:\n");
666                 else
667                         fprintf(stderr,"               following nodes are user, but not recorded in list:\n");
668
669                 fprintf(stderr,"              ");
670                 bitset_foreach(bs, idx) {
671                         ir_node *src = get_idx_irn(w->irg, idx);
672                         ir_fprintf(stderr, " %+F", src);
673                 }
674                 fprintf(stderr, "\n");
675         }
676
677         bitset_free(bs);
678 }
679
680 /**
681  * Verifies the out edges of an irg.
682  */
683 int edges_verify(ir_graph *irg) {
684         struct build_walker w;
685         int    problem_found = 0;
686
687         if (! edges_dbg)
688                 return 0;
689
690         /* verify normal edges only */
691         problem_found  = edges_verify_kind(irg, EDGE_KIND_NORMAL);
692
693         w.irg           = irg;
694         w.kind          = EDGE_KIND_NORMAL;
695         w.problem_found = 0;
696
697         /* verify counter */
698         inc_irg_visited(irg);
699         irg_walk_anchors(irg, clear_links, count_user, &w);
700         irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
701
702         return problem_found ? 1 : w.problem_found;
703 }
704
705 void init_edges(void)
706 {
707         FIRM_DBG_REGISTER(dbg, DBG_EDGES);
708         /* firm_dbg_set_mask(dbg, -1); */
709 }
710
711 void edges_init_dbg(int do_dbg) {
712         edges_dbg = do_dbg;
713 }
714
715 void edges_activate(ir_graph *irg)
716 {
717         edges_activate_kind(irg, EDGE_KIND_NORMAL);
718         edges_activate_kind(irg, EDGE_KIND_BLOCK);
719 }
720
721 void edges_deactivate(ir_graph *irg)
722 {
723         edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
724         edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
725 }
726
727 int edges_assure(ir_graph *irg)
728 {
729         int activated = edges_activated(irg);
730
731         if(!activated)
732                 edges_activate(irg);
733
734         return activated;
735 }
736
737 void edges_node_deleted(ir_node *irn, ir_graph *irg)
738 {
739         edges_node_deleted_kind(irn, EDGE_KIND_NORMAL, irg);
740         edges_node_deleted_kind(irn, EDGE_KIND_BLOCK, irg);
741 }
742
743
744 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
745 {
746         return _get_irn_out_edge_first_kind(irn, kind);
747 }
748
749 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
750 {
751         return _get_irn_out_edge_next(irn, last);
752 }
753
754 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
755 {
756         return _get_edge_src_irn(edge);
757 }
758
759 int (get_edge_src_pos)(const ir_edge_t *edge)
760 {
761         return _get_edge_src_pos(edge);
762 }
763
764 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
765 {
766         return _get_irn_n_edges_kind(irn, kind);
767 }
768
769 void dump_all_out_edges(ir_node *irn)
770 {
771         int i;
772         for(i = 0; i < EDGE_KIND_LAST; ++i) {
773                 const ir_edge_t *edge;
774
775                 printf("kind \"%s\"\n", get_kind_str(i));
776                 foreach_out_edge_kind(irn, edge, i) {
777                         ir_printf("\t%+F(%d)\n", edge->src, edge->pos);
778                 }
779         }
780 }