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