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