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