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