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