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