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