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