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