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