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