2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Always available outs.
23 * @author Sebastian Hack, Michael Beck, Andreas Schoesser
27 * This are out-edges (also called def-use edges) that are dynamically
28 * updated as the graph changes.
36 #include "iredgekinds.h"
37 #include "iredges_t.h"
48 * A function that allows for setting an edge.
49 * This abstraction is necessary since different edge kind have
50 * different methods of setting edges.
52 typedef void (set_edge_func_t)(ir_node *src, int pos, ir_node *tgt);
54 typedef int (get_edge_src_arity_func_t)(const ir_node *src);
56 typedef int (get_edge_src_first_func_t)(const ir_node *src);
58 typedef ir_node *(get_edge_src_n_func_t)(const ir_node *src, int pos);
61 * Additional data for an edge kind.
65 set_edge_func_t *set_edge;
66 get_edge_src_first_func_t *get_first;
67 get_edge_src_arity_func_t *get_arity;
68 get_edge_src_n_func_t *get_n;
69 } ir_edge_kind_info_t;
71 static int get_zero(const ir_node *irn)
77 static int get_irn_first(const ir_node *irn)
79 return 0 - !is_Block(irn);
82 static ir_node *get_block_n(const ir_node *irn, int pos)
84 return is_Block(irn) ? get_Block_cfgpred_block((ir_node *) irn, pos) : 0;
87 static const ir_edge_kind_info_t edge_kind_info[EDGE_KIND_LAST] = {
88 { "normal" , set_irn_n, get_irn_first, get_irn_arity, get_irn_n },
89 { "block succs", NULL, get_zero, get_irn_arity, get_block_n },
90 { "dependency", set_irn_dep, get_zero, get_irn_deps, get_irn_dep }
93 #define foreach_tgt(irn, i, n, kind) for(i = edge_kind_info[kind].get_first(irn), n = edge_kind_info[kind].get_arity(irn); i < n; ++i)
94 #define get_n(irn, pos, kind) (edge_kind_info[kind].get_n(irn, pos))
95 #define get_kind_str(kind) (edge_kind_info[kind].name)
97 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
100 * This flag is set to 1, if the edges get initialized for an irg.
101 * Then register additional data is forbidden.
103 static int edges_used = 0;
106 * Summed size of all users private data
109 static int edges_private_size = 0;
110 #define EDGE_SIZE (sizeof(ir_edge_t) + edges_private_size)
113 * If set to 1, the list heads are checked every time an edge is changed.
115 static int edges_dbg = 0;
118 /* a static variable holding the last number assigned to a new edge */
119 static long last_edge_num = -1;
122 static INLINE long edge_get_id(const ir_edge_t *e) {
125 #else /* DEBUG_libfirm */
127 #endif /* DEBUG_libfirm */
131 * Announce to reserve extra space for each edge to be allocated.
132 * @Param n: Size of the space to reserve
133 * @ Returns: Offset at which the private data will begin
134 * Several users can reserve extra space for private usage.
135 * Each user has to remember his given offset and the size of his private data.
136 * To be called before FIRM is initialized.
138 int edges_register_private_data(size_t n)
140 int res = edges_private_size;
142 assert(!edges_used && "you cannot register private edge data, if edges have been initialized");
144 edges_private_size += n;
149 * Reset the user's private data at offset 'offset'
150 * The user has to remember his offset and the size of his data!
151 * Caution: Using wrong values here can destroy other users private data!
154 void edges_reset_private_data(ir_graph *irg, int offset, size_t size)
156 irg_edge_info_t *info = _get_irg_edge_info(irg, EDGE_KIND_NORMAL);
159 foreach_set(info->edges, edge)
161 memset(edge + sizeof(*edge) + offset, 0, size);
165 #define TIMES37(x) (((x) << 5) + ((x) << 2) + (x))
167 #define get_irn_out_list_head(irn) (&get_irn_out_info(irn)->outs)
169 static int edge_cmp(const void *p1, const void *p2, size_t len)
171 const ir_edge_t *e1 = p1;
172 const ir_edge_t *e2 = p2;
175 if(e1->src != e2->src)
177 if(e1->pos != e1->pos)
183 #define edge_hash(edge) (TIMES37((edge)->pos) + HASH_PTR((edge)->src))
186 * Initialize the out information for a graph.
187 * @note Dead node elimination can call this on an already initialized graph.
189 void edges_init_graph_kind(ir_graph *irg, ir_edge_kind_t kind)
191 if(edges_activated_kind(irg, kind)) {
192 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
197 amount = set_count(info->edges);
198 del_set(info->edges);
200 info->edges = new_set(edge_cmp, amount);
205 * Get the edge object of an outgoing edge at a node.
206 * @param irg The graph, the node is in.
207 * @param src The node at which the edge originates.
208 * @param pos The position of the edge.
209 * @return The corresponding edge object or NULL,
210 * if no such edge exists.
212 const ir_edge_t *get_irn_edge_kind(ir_graph *irg, const ir_node *src, int pos, ir_edge_kind_t kind)
214 if (edges_activated_kind(irg, kind)) {
215 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
218 key.src = (ir_node *)src;
220 return set_find(info->edges, &key, EDGE_SIZE, edge_hash(&key));
227 * Get the edge object of an outgoing edge at a node.
228 * Looks for an edge for all kinds.
231 const ir_edge_t *get_irn_edge(ir_graph *irg, const ir_node *src, int pos)
233 const ir_edge_t *edge;
234 if((edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_NORMAL)) == NULL)
235 edge = get_irn_edge_kind(irg, src, pos, EDGE_KIND_BLOCK);
240 * Change the out count
242 static INLINE void edge_change_cnt(ir_node *tgt, ir_edge_kind_t kind, int ofs) {
243 irn_edge_info_t *info = _get_irn_edge_info(tgt, kind);
244 info->out_count += ofs;
248 * Verify the edge list of a node, ie. ensure it's a loop:
249 * head -> e_1 -> ... -> e_n -> head
251 static INLINE void vrfy_list_head(ir_node *irn, ir_edge_kind_t kind) {
254 pset *lh_set = pset_new_ptr(16);
255 const struct list_head *head = _get_irn_outs_head(irn, kind);
256 const struct list_head *pos;
258 list_for_each(pos, head) {
259 if (pset_find_ptr(lh_set, pos)) {
260 const ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
262 ir_fprintf(stderr, "EDGE Verifier: edge list broken (self loop not to head) for %+F:\n", irn);
263 fprintf(stderr, "- at list entry %d\n", num);
265 fprintf(stderr, "- edge(%ld) is invalid\n", edge_get_id(edge));
267 ir_fprintf(stderr, "- edge(%ld) %+F(%d)\n", edge_get_id(edge), edge->src, edge->pos);
272 pset_insert_ptr(lh_set, pos);
280 /* The edge from (src, pos) -> old_tgt is redirected to tgt */
281 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt,
282 ir_node *old_tgt, ir_edge_kind_t kind,
285 const char *msg = "";
286 irg_edge_info_t *info;
291 assert(edges_activated_kind(irg, kind));
294 * Only do something, if the old and new target differ.
299 info = _get_irg_edge_info(irg, kind);
301 templ = alloca(EDGE_SIZE);
303 /* Initialize the edge template to search in the set. */
304 memset(templ, 0, EDGE_SIZE);
310 DEBUG_ONLY(templ->src_nr = get_irn_node_nr(src));
313 * If the target is NULL, the edge shall be deleted.
316 /* search the edge in the set. */
317 edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
319 /* mark the edge invalid if it was found */
322 list_del(&edge->list);
328 #endif /* DEBUG_libfirm */
329 edge_change_cnt(old_tgt, kind, -1);
332 /* If the edge was not found issue a warning on the debug stream */
334 msg = "edge to delete not found!\n";
339 * The target is not NULL and the old target differs
340 * from the new target, the edge shall be moved (if the
341 * old target was != NULL) or added (if the old target was
345 struct list_head *head = _get_irn_outs_head(tgt, kind);
347 assert(head->next && head->prev &&
348 "target list head must have been initialized");
350 /* If the old target is not null, the edge is moved. */
352 edge = set_find(edges, templ, EDGE_SIZE, edge_hash(templ));
353 assert(edge && "edge to redirect not found!");
354 assert(! edge->invalid && "Invalid edge encountered");
358 list_move(&edge->list, head);
359 edge_change_cnt(old_tgt, kind, -1);
362 /* The old target was null, thus, the edge is newly created. */
364 edge = set_insert(edges, templ, EDGE_SIZE, edge_hash(templ));
366 assert(! edge->invalid && "Freshly inserted edge is invalid?!?");
367 assert(edge->list.next == NULL && edge->list.prev == NULL &&
368 "New edge must not have list head initialized");
371 list_add(&edge->list, head);
373 edge->edge_nr = ++last_edge_num;
374 #endif /* DEBUG_libfirm */
377 edge_change_cnt(tgt, kind, +1);
380 #ifndef DEBUG_libfirm
381 /* verify list heads */
384 vrfy_list_head(tgt, kind);
386 vrfy_list_head(old_tgt, kind);
390 DBG((dbg, LEVEL_5, "announce out edge: %+F %d-> %+F(%+F): %s\n", src, pos, tgt, old_tgt, msg));
393 void edges_notify_edge(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_graph *irg)
395 if(edges_activated_kind(irg, EDGE_KIND_NORMAL)) {
396 edges_notify_edge_kind(src, pos, tgt, old_tgt, EDGE_KIND_NORMAL, irg);
399 if (edges_activated_kind(irg, EDGE_KIND_BLOCK) && is_Block(src)) {
400 /* do not use get_nodes_block() here, it fails when running unpinned */
401 ir_node *bl_old = old_tgt ? get_irn_n(skip_Proj(old_tgt), -1) : NULL;
402 ir_node *bl_tgt = NULL;
405 bl_tgt = is_Bad(tgt) ? tgt : get_irn_n(skip_Proj(tgt), -1);
407 edges_notify_edge_kind(src, pos, bl_tgt, bl_old, EDGE_KIND_BLOCK, irg);
412 void edges_node_deleted_kind(ir_node *old, ir_edge_kind_t kind, ir_graph *irg)
416 if(!edges_activated_kind(irg, kind))
419 DBG((dbg, LEVEL_5, "node deleted (kind: %s): %+F\n", get_kind_str(kind), old));
421 foreach_tgt(old, i, n, kind) {
422 ir_node *old_tgt = get_n(old, i, kind);
423 edges_notify_edge_kind(old, i, NULL, old_tgt, kind, irg);
427 struct build_walker {
431 unsigned problem_found;
435 * Post-Walker: notify all edges
437 static void build_edges_walker(ir_node *irn, void *data) {
438 struct build_walker *w = data;
441 if (! edges_activated_kind(w->irg, w->kind))
444 foreach_tgt(irn, i, n, w->kind)
445 edges_notify_edge_kind(irn, i, get_n(irn, i, w->kind), NULL, w->kind, w->irg);
449 * Pre-Walker: initializes the list-heads and set the out-count
452 static void init_lh_walker(ir_node *irn, void *data) {
453 struct build_walker *w = data;
454 INIT_LIST_HEAD(_get_irn_outs_head(irn, w->kind));
455 _get_irn_edge_info(irn, w->kind)->out_count = 0;
459 * Visitor: initializes the list-heads and set the out-count
460 * of all nodes to 0 of nodes that are not seen so far.
462 static void visitor(ir_node *irn, void *data) {
463 if (irn_not_visited(irn)) {
464 mark_irn_visited(irn);
465 init_lh_walker(irn, data);
470 * Build the initial edge set.
471 * Beware, this is not a simple task because it suffers from two
473 * - the anchor set allows access to Nodes that may not be reachable from
475 * - the identities add nodes to the "root set" that are not yet reachable
476 * from End. However, after some transformations, the CSE may revival these
479 * These problems can be fixed using different strategies:
480 * - Add an age flag to every node. Whenever the edge of a node is older
481 * then the current edge, invalidate the edges of this node.
482 * While this would help for revivaled nodes, it increases memory and runtime.
483 * - Delete the identities set.
484 * Solves the revival problem, but may increase the memory consumption, as
485 * nodes cannot be revivaled at all.
486 * - Manually iterate over the identities root set. This did not consume more memory
487 * but increase the computation time because the |identities| >= |V|
489 * Currently, we use the last option.
491 void edges_activate_kind(ir_graph *irg, ir_edge_kind_t kind)
493 struct build_walker w;
494 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
500 edges_init_graph_kind(irg, kind);
501 //irg_walk_graph(irg, init_lh_walker, build_edges_walker, &w);
502 inc_irg_visited(irg);
503 irg_walk_anchors(irg, init_lh_walker, build_edges_walker, &w);
504 visit_all_identities(irg, visitor, &w);
507 void edges_deactivate_kind(ir_graph *irg, ir_edge_kind_t kind)
509 irg_edge_info_t *info = _get_irg_edge_info(irg, kind);
513 del_set(info->edges);
518 int (edges_activated_kind)(const ir_graph *irg, ir_edge_kind_t kind)
520 return _edges_activated_kind(irg, kind);
525 * Reroute all use-edges from a node to another.
526 * @param from The node whose use-edges shall be withdrawn.
527 * @param to The node to which all the use-edges of @p from shall be
529 * @param irg The graph.
531 void edges_reroute_kind(ir_node *from, ir_node *to, ir_edge_kind_t kind, ir_graph *irg)
533 set_edge_func_t *set_edge = edge_kind_info[kind].set_edge;
535 if(set_edge && edges_activated_kind(irg, kind)) {
536 struct list_head *head = _get_irn_outs_head(from, kind);
538 DBG((dbg, LEVEL_5, "reroute from %+F to %+F\n", from, to));
540 while(head != head->next) {
541 ir_edge_t *edge = list_entry(head->next, ir_edge_t, list);
542 assert(edge->pos >= -1);
543 set_edge(edge->src, edge->pos, to);
548 static void verify_set_presence(ir_node *irn, void *data)
550 struct build_walker *w = data;
551 set *edges = _get_irg_edge_info(w->irg, w->kind)->edges;
554 foreach_tgt(irn, i, n, w->kind) {
560 e = set_find(edges, &templ, EDGE_SIZE, edge_hash(&templ));
564 w->problem_found = 1;
565 ir_fprintf(stderr, "Edge Verifier: edge %+F,%d -> %+F (kind: \"%s\") is missing\n",
566 irn, i, get_n(irn, i, w->kind), get_kind_str(w->kind));
571 static void verify_list_presence(ir_node *irn, void *data)
573 struct build_walker *w = data;
576 bitset_set(w->reachable, get_irn_idx(irn));
578 /* check list heads */
579 vrfy_list_head(irn, w->kind);
581 foreach_out_edge_kind(irn, e, w->kind) {
584 if (w->kind == EDGE_KIND_NORMAL && get_irn_arity(e->src) <= e->pos) {
585 w->problem_found = 1;
586 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F -> %+F recorded at src position %d, but src has arity %d\n",
587 edge_get_id(e), e->src, irn, e->pos, get_irn_arity(e->src));
591 tgt = get_n(e->src, e->pos, w->kind);
594 w->problem_found = 1;
595 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d (kind \"%s\") is no out edge of %+F but of %+F\n",
596 edge_get_id(e), e->src, e->pos, get_kind_str(w->kind), irn, tgt);
601 int edges_verify_kind(ir_graph *irg, ir_edge_kind_t kind)
603 struct build_walker w;
604 set *edges = _get_irg_edge_info(irg, kind)->edges;
609 w.reachable = bitset_alloca(get_irg_last_idx(irg));
612 /* Clear the present bit in all edges available. */
613 for (e = set_first(edges); e; e = set_next(edges))
616 irg_walk_graph(irg, verify_set_presence, verify_list_presence, &w);
619 * Dump all edges which are not invalid and not present.
620 * These edges are superfluous and their presence in the
623 for (e = set_first(edges); e; e = set_next(edges)) {
624 if (! e->invalid && ! e->present && bitset_is_set(w.reachable, get_irn_idx(e->src))) {
626 ir_fprintf(stderr, "Edge Verifier: edge(%ld) %+F,%d is superfluous\n", edge_get_id(e), e->src, e->pos);
630 return w.problem_found;
633 #define IGNORE_NODE(irn) (is_Bad((irn)) || is_Block((irn)))
636 * Clear link field of all nodes.
638 static void clear_links(ir_node *irn, void *env) {
639 struct build_walker *w = env;
642 if (IGNORE_NODE(irn)) {
643 set_irn_link(irn, NULL);
647 bs = bitset_malloc(get_irg_last_idx(w->irg));
648 set_irn_link(irn, bs);
652 * Increases count (stored in link field) for all operands of a node.
654 static void count_user(ir_node *irn, void *env) {
659 first = get_irn_first(irn);
660 for (i = get_irn_arity(irn) - 1; i >= first; --i) {
661 ir_node *op = get_irn_n(irn, i);
662 bitset_t *bs = get_irn_link(op);
665 bitset_set(bs, get_irn_idx(irn));
670 * Verifies if collected count, number of edges in list and stored edge count are in sync.
672 static void verify_edge_counter(ir_node *irn, void *env) {
673 struct build_walker *w = env;
679 const struct list_head *head;
680 const struct list_head *pos;
682 if (IGNORE_NODE(irn))
685 bs = get_irn_link(irn);
688 edge_cnt = _get_irn_edge_info(irn, EDGE_KIND_NORMAL)->out_count;
689 head = _get_irn_outs_head(irn, EDGE_KIND_NORMAL);
691 /* We can iterate safely here, list heads have already been verified. */
692 list_for_each(pos, head) {
696 /* check all nodes that reference us and count edges that point number
697 * of ins that actually point to us */
699 bitset_foreach(bs, idx) {
701 ir_node *src = get_idx_irn(w->irg, idx);
703 arity = get_irn_arity(src);
704 for(i = 0; i < arity; ++i) {
705 ir_node *in = get_irn_n(src, i);
711 if (edge_cnt != list_cnt) {
712 w->problem_found = 1;
713 ir_fprintf(stderr, "Edge Verifier: edge count is %d, but %d edge(s) are recorded in list at %+F\n",
714 edge_cnt, list_cnt, irn);
717 if (ref_cnt != list_cnt) {
718 w->problem_found = 1;
719 ir_fprintf(stderr, "Edge Verifier: %+F reachable by %d node(s), but the list contains %d edge(s)\n",
720 irn, ref_cnt, list_cnt);
722 /* Matze: buggy if a node has multiple ins pointing at irn */
724 list_for_each(pos, head) {
725 ir_edge_t *edge = list_entry(pos, ir_edge_t, list);
726 bitset_flip(bs, get_irn_idx(edge->src));
729 if (ref_cnt < list_cnt)
730 fprintf(stderr," following nodes are recorded in list, but not as user:\n");
732 fprintf(stderr," following nodes are user, but not recorded in list:\n");
735 bitset_foreach(bs, idx) {
736 ir_node *src = get_idx_irn(w->irg, idx);
737 ir_fprintf(stderr, " %+F", src);
739 fprintf(stderr, "\n");
747 * Verifies the out edges of an irg.
749 int edges_verify(ir_graph *irg) {
750 struct build_walker w;
751 int problem_found = 0;
753 /* verify normal edges only */
754 problem_found = edges_verify_kind(irg, EDGE_KIND_NORMAL);
757 w.kind = EDGE_KIND_NORMAL;
761 inc_irg_visited(irg);
762 irg_walk_anchors(irg, clear_links, count_user, &w);
763 inc_irg_visited(irg);
764 irg_walk_anchors(irg, NULL, verify_edge_counter, &w);
766 return problem_found ? 1 : w.problem_found;
769 void init_edges(void)
771 FIRM_DBG_REGISTER(dbg, DBG_EDGES);
772 /* firm_dbg_set_mask(dbg, -1); */
775 void edges_init_dbg(int do_dbg) {
779 void edges_activate(ir_graph *irg)
781 edges_activate_kind(irg, EDGE_KIND_NORMAL);
782 edges_activate_kind(irg, EDGE_KIND_BLOCK);
785 void edges_deactivate(ir_graph *irg)
787 edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
788 edges_deactivate_kind(irg, EDGE_KIND_BLOCK);
791 int edges_assure(ir_graph *irg)
793 int activated = edges_activated(irg);
801 void edges_node_deleted(ir_node *irn, ir_graph *irg)
803 edges_node_deleted_kind(irn, EDGE_KIND_NORMAL, irg);
804 edges_node_deleted_kind(irn, EDGE_KIND_BLOCK, irg);
808 const ir_edge_t *(get_irn_out_edge_first_kind)(const ir_node *irn, ir_edge_kind_t kind)
810 return _get_irn_out_edge_first_kind(irn, kind);
813 const ir_edge_t *(get_irn_out_edge_next)(const ir_node *irn, const ir_edge_t *last)
815 return _get_irn_out_edge_next(irn, last);
818 ir_node *(get_edge_src_irn)(const ir_edge_t *edge)
820 return _get_edge_src_irn(edge);
823 int (get_edge_src_pos)(const ir_edge_t *edge)
825 return _get_edge_src_pos(edge);
828 int (get_irn_n_edges_kind)(const ir_node *irn, ir_edge_kind_t kind)
830 return _get_irn_n_edges_kind(irn, kind);
833 void dump_all_out_edges(ir_node *irn)
836 for(i = 0; i < EDGE_KIND_LAST; ++i) {
837 const ir_edge_t *edge;
839 printf("kind \"%s\"\n", get_kind_str(i));
840 foreach_out_edge_kind(irn, edge, i) {
841 ir_printf("\t%+F(%d)\n", edge->src, edge->pos);