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